|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*, R" m; L! M( [ T* [& K
* myfileio.c
4 u; y! q* k4 w, a. ^( C * Apps$ e0 I; G2 U% P) i4 m8 l
*/
, t; W; v3 Z- x& I7 b. @ V( d
) C M* b: ~! G9 Q3 D#include "efi.h"
0 C. ?/ g7 ?0 G#include "efilib.h"
, V7 g' A- X+ n: K% J- n6 _; n4 P+ C
# T& g# b8 c% |8 \% E( D; y; i: V9 ]#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
6 G( W# ^5 L. B9 \/ S! B8 v- n
8 h0 { B0 P( pstatic EFI_STATUS WaitForKeyOrReset(VOID)3 K0 P) l5 n8 A) D \% s
{
/ a# S+ ]' G1 O+ n+ g( z+ k EFI_STATUS Status;
7 y8 i/ @( U% R& @ EFI_INPUT_KEY key;& ^& V7 a( [7 p7 h- Q) v2 A' S
UINTN index;9 U: i* I+ i% k4 n; Y* G
3 [1 c8 K; G+ H$ a/ i4 |( U& M
for(;;) {& t% _! [# j3 b Q# M
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
2 ~! ~# R2 h& O4 I r& G if (Status == EFI_NOT_READY)+ ~" {+ Q8 P" `' E) z
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
; W2 y; P6 l: S6 E8 r" K8 z else- Q+ q+ a- y) n
break;$ k- q4 F" W o8 H! }
}4 f% [! j3 k- T/ U
if (!EFI_ERROR(Status)) {
7 A5 ^+ V2 l. w/ F6 S/ W if (key.ScanCode == SCAN_ESC)
1 C4 X# E# Z+ b) p: C# H; |) n! k) H) L0 S RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);5 a' I5 O7 p( e0 u
}
( F" y+ R7 {3 y u* z* b8 [ + U+ c+ D# {3 j! \# g# b) S
return Status;
B$ J7 A; \: J( W8 ]( S2 v2 u}
3 h e& q0 |4 @" w+ U
% G* l! C' ? @8 N; CEFI_STATUS
3 ~! e% k+ H, s: q1 n% SEFIAPI
{) v) S3 h. c( p# S. k) PMyfileioMain (IN EFI_HANDLE ImageHandle,! k$ p \/ k$ C0 j) I0 G$ Q5 a* F
IN EFI_SYSTEM_TABLE *SystemTable)
( q' Z# F4 T5 g$ C% D* [# P{ p6 H! s2 s# O \
EFI_STATUS Status;
% F, F/ X( m/ T, {0 R, I! c! k' B EFI_HANDLE *DestAddr; ( E9 o% D- q0 N- q
EFI_LOADED_IMAGE *FileHandle01;
# A; F. A8 @8 f7 I& O EFI_DEVICE_PATH *FileHandle02;
- {( {8 H% f. \' u1 ^6 ?1 z! x% { EFI_FILE_IO_INTERFACE *FileHandle03;; _& I3 y* W- A i8 ^5 y
EFI_FILE *FileHandle04;
" ?* H ?- U$ `! S3 h+ j EFI_FILE *FileHandle05;
F/ }- Q+ V I. B) L4 a1 A CHAR16 *FileName;# u; ]# I7 e7 o! T. x
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;. s/ I% I! k, d& I5 Z
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
5 c- q) ^) V$ I$ L" w, F int Bit0, Bit1, Key_Space; # m$ w9 I" x. U! b+ D; p
6 J! F; ?! G" [2 u
FileName = L"NewFile.txt";4 E6 @4 g- U+ N
Space_Key = L" ";
* L1 R- Y4 e- F 7 h5 V) L; b0 ?1 R9 z* I# O5 b+ ?/ y
BufferA = L"ABCD";
% ~; Y( Z/ D" Y BufferB = L"EFGH";
- E- p/ e* m9 L- O5 r$ X BufferC = L"IJKL";
5 |) t( n0 I* |% z. ~9 E! Y# X3 F; h: k4 O
Bit0 = 0xff;; a9 q5 j ], K
Bit1 = 0xfe;
: e3 O u$ R1 H2 X' ]
! Q M1 ~) L3 X, _ Key_Space = 0x0020;
- [; S o' S, ^$ ^1 ^ 3 _: n7 u, E2 \, K8 D1 q( W) p* l
InitializeLib (ImageHandle, SystemTable); ! S* P" ?, F- W+ L
; |* Z" ?, k B' D DestAddr = AllocatePool (BufferSize);
7 G: @) F- A3 D. V3 c' n. x$ E/ @, [6 D# f9 L. ?3 i. _
Print (L"Value of Bit0 is %x\n", Bit0);: M& b0 ?5 r, J! p" w% h
Print (L"Value of Bit1 is %x\n", Bit1); % S8 V/ _& w9 Z# b' [6 r8 Y3 J; J
, y3 D/ j5 i. s' D: N) D+ {5 C % L$ y8 z. h4 k6 T& b# J) o
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
4 i9 r G% u' h5 W1 i: R1 ^ if (EFI_ERROR(Status)) {$ V4 {) a, d2 W8 P, _9 i
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
# ~" i8 @9 \/ a return EFI_LOAD_ERROR;
/ U/ }! @" e3 @" l2 q5 z! P }
9 L1 @* G8 h8 X9 N2 W+ q; {
3 `$ h! N6 K% F" P Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);5 v) |9 W2 ?. k1 D+ H0 Y6 {9 W+ l
if (EFI_ERROR(Status)) {# e! K/ S t* Q' n1 Z, `1 o4 z$ @6 @
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
. s" s; y# r2 E return EFI_LOAD_ERROR;9 I0 x; D" E% z
} ; t0 O) t# m& E
4 M) ~/ y( Z1 I( I& D
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);1 r- U6 ?; Z0 P0 Z" a
if (EFI_ERROR(Status)) {
% r1 S! f) K. v! c: L& Z Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);' ~' s7 C# h& _1 f7 E, X D& ]4 F
return EFI_LOAD_ERROR;0 g1 B: i, P, k }8 }
}+ [! Y, H% n( m$ m8 l; H( ~
# Z W! ?4 |2 ]9 K) q' G7 T Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
; b' {! q, K+ @8 b if (EFI_ERROR(Status)) {& a4 R9 m+ o: q! F8 |
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);5 w9 X4 a9 I U, n" y9 [
return EFI_LOAD_ERROR;/ q, l k+ |+ c8 w1 r
} " j9 K; N% U" M3 @# g, ^. {+ M
. I& ~4 J" @( {6 t/ I1 m: [4 @0 n Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
& _0 d# [* C0 |2 z# z if (EFI_ERROR(Status)) {
1 W, o# @, a) [" @) h Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
: e& r' a0 w. j& t return EFI_LOAD_ERROR;
_- ^3 k8 s1 i% T7 k& ~ }
8 [$ r! j, B0 {3 a5 I' }; O
2 Z+ U9 y7 X @: f; a3 L; q# F Status = FileHandle05->SetPosition(FileHandle05, 0); 8 z$ h, D# r& H( z
if (EFI_ERROR(Status)) {: ^6 b4 Y+ N- H/ [ N/ X; ]% |' B. W/ N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 G: ?# [2 i# n* n
return EFI_SUCCESS;2 F) P/ p# \+ G9 W( r) Z
}' m' i* `7 u; f8 I
0 Y2 ~8 k u. n+ E5 c Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);3 o5 `& F0 O v s
Print (L"File Buffersize is %x\n\n", BufferSize);
( Q" D! _- J" j f( R if (EFI_ERROR(Status)) {% b$ M( ]2 }; l7 J
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);# O& c1 y& f# B
return EFI_SUCCESS;
7 A5 ~$ N# o' p* [$ V4 T- A } : J& L5 ^& D; ^9 X8 w, ]& m
_. w0 z# r) u0 N Status = FileHandle05->SetPosition(FileHandle05, 1);
% b7 h- Z4 y) g: A if (EFI_ERROR(Status)) {
& v/ z$ K" t7 S# f& w Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# Z G* C. g; i" [- c( L5 l1 [8 M return EFI_SUCCESS;+ M1 s; c' V% s9 V$ w% a
}) r* d- ~6 c, ?! w. V
" e3 ?1 [& t! ~$ S9 o, D, G Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# d3 a6 x H$ H" L Print (L"File Buffersize is %x\n\n", BufferSize);7 j' G- U7 H2 F
if (EFI_ERROR(Status)) {
% N9 p2 ?. i0 j6 J5 ` Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 d# r5 T! X$ S$ X$ G return EFI_SUCCESS;* {) t) l- Z+ V4 s5 M; }
}
& B* b/ Y: ?$ b# U
7 [: G6 h9 z+ i% C4 F! A Status = FileHandle05->SetPosition(FileHandle05, 2); " o& G# h5 D, z9 w5 c
if (EFI_ERROR(Status)) {
Y! M% B4 \ v/ d* V* a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); }; R8 q. p0 Z4 c5 P |/ N r
return EFI_SUCCESS;" Z' X7 u/ n$ P8 a- |
}
/ |* H- H) y0 |9 @
1 ^5 c& Q; h+ Z6 G0 b) P) F Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
6 E) `5 o% {; Q7 T0 \: n, z Print (L"File Buffersize is %x\n\n", BufferSize);
3 c3 t9 V$ }/ d5 [ if (EFI_ERROR(Status)) {' @! h: G7 ]" ]% Y7 {* r+ j, ]' x6 T
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
8 K4 C. [: [! k return EFI_SUCCESS;; N ]. G5 H0 j: r1 t7 q- q
} $ A4 T7 a0 O$ f8 A: }: G M
5 S6 [/ M8 @% l2 @
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>* E+ b( t% B. |1 n4 d
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
3 m- ]8 H+ V( ?* k# `$ p! R! o Status = FileHandle05->SetPosition(FileHandle05, 2);
2 ]/ E- i4 d# l: B" r) I( } if (EFI_ERROR(Status)) {3 c% V l1 {6 q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' |7 Y% @4 f4 V" O1 U+ C" T return EFI_SUCCESS; R& F; g8 i3 D
}
+ y" i7 o' k' {0 S; B. |+ r: S! ]5 ^- X
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
+ ~9 k& T: `. M if (EFI_ERROR(Status)) {) v! C# I( ]4 c1 d7 s
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
! l0 h$ Y- R+ E9 E return EFI_SUCCESS;9 {1 f0 B0 {5 w+ E8 {3 [
}
' M/ ~8 h7 j3 T. x. E0 l* y" r Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);8 S: V0 n3 p6 X' @
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
4 c0 B- b: j* I( g; W1 _; F% i+ T) l. {3 Y9 e
Status = FileHandle05->SetPosition(FileHandle05, 10);
3 r. m( ]( j5 {9 K if (EFI_ERROR(Status)) {
5 ^6 g; w" K; k+ X2 L Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
% }; H' c: U0 L o& ] return EFI_SUCCESS;
' U1 T& j5 J* c2 B }- q$ b/ N% N2 w O! X
8 o, p0 p" D- S; q8 R6 n
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
! }* d# E. Z/ j" v- q# A if (EFI_ERROR(Status)) {
6 c4 A0 Q) Q% b5 d* \6 d Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! s' O; W7 u! h! t w" G return EFI_SUCCESS;
" z( D3 `6 q4 j& C ` }
) \9 d4 A* C" M& [
& m, i" _ `( X$ G7 t0 K! H. h Status = FileHandle05->SetPosition(FileHandle05, 12); " h& G1 P8 l! e- c* S6 V$ Q
if (EFI_ERROR(Status)) {
4 `, M6 `' H4 C" a7 J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. q. U& K$ U3 g0 m. k+ e F return EFI_SUCCESS;# C# l' x; q+ a0 f7 s
}; Y. D% G3 ~6 L3 r% P6 {
9 N' G$ k1 f. |/ V, k9 Z
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. B0 W/ ?8 Q. n8 Q0 m- @+ L `
if (EFI_ERROR(Status)) {1 l2 S% |8 M y- v# S& I1 f
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ K1 P. M/ P4 M* d0 s0 J return EFI_SUCCESS;4 v; u9 W1 F$ N- ^; L( L. o
}3 q" w0 m+ w0 j. }
9 H3 `1 I, b7 ?0 ]' x Status = FileHandle05->SetPosition(FileHandle05, 14); 0 c ^: D) t; c* h0 I0 C
if (EFI_ERROR(Status)) {
# D7 a e+ I8 { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 l- L* Z0 s3 e return EFI_SUCCESS;1 n7 n* u% {& H: B
}
7 k7 p8 n8 N% H! q- U
) z' h8 H6 E5 R9 R Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);$ T8 K) R! ?( E/ e/ t8 D- U& M. Z
if (EFI_ERROR(Status)) {
+ _! i+ ^' i7 {+ V* R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
0 J. ^* E$ C2 f# E& N return EFI_SUCCESS;1 _/ \: Z" Z- r1 @1 B
}. f; i. O6 @7 b/ g% y
5 l: P4 N c0 x H3 X2 p3 k
Status = FileHandle05->SetPosition(FileHandle05, 16); ! M0 C; v+ L! Z: x( U. M% [! B; U
if (EFI_ERROR(Status)) {
3 P. T; |6 A& }/ [! e! ^" q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: c. t! j& u! Y, D- k j% a
return EFI_SUCCESS;" N9 d. [* d2 d, d. E- ~ c0 v( S
}# P% ^8 g+ _7 _5 U6 M
6 |) N( U$ ~/ ?8 F ?* I( J1 ` Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 }+ p' ~" g2 H5 ~% e# Y5 q if (EFI_ERROR(Status)) {# \9 W3 \% ^: i7 x$ `/ M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; m. W* [* M; j% G8 n' y/ @4 W
return EFI_SUCCESS;+ ~7 i8 d, n4 j5 m* B6 S
} , {: X9 D/ w5 I' w; u. Z4 ^
7 z3 T$ \( J/ _, M//---------------------------------------------------------------------------------------------------------------------------------------------------------------+ ]2 M( Q L x6 H
+ A$ D! b% r( s& K# F& E$ e3 @" f Status = FileHandle05->SetPosition(FileHandle05, 18); 8 m/ C! ]% K9 S% F' y' J
if (EFI_ERROR(Status)) {0 O% ?! J# e8 H- i3 o4 i+ _( k, G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 q% `3 F( g5 d" h: o
return EFI_SUCCESS;
* i. [( Q( L& B }
* B! b1 e0 w% m , N" W. l, l* G
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
+ N' s- X q+ a" }9 { if (EFI_ERROR(Status)) { f( ^, i0 I( p4 {
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
5 [" n7 S/ y+ }2 v4 I# z& V return EFI_SUCCESS;
6 P) s" g& a. C2 ~ } + r o* T5 z. R& G
/ ?* m: C* f& x' @8 V' c FreePool(DestAddr);
( w- q9 S9 U2 m2 y6 s; Y
) P- D" b5 M" o Status = FileHandle05->Close(FileHandle05);
6 _9 {$ F. N( C& E if (EFI_ERROR(Status)) {5 y* S8 c: H6 Y
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
' b) l: \6 P% K, G, u return EFI_SUCCESS;
3 b+ q( Y. O' g b0 Z0 p }* X3 h' ?1 u% c" A7 Y1 p5 g- q- D
6 P5 g8 g5 h- r3 i7 a
Print(L"File Name = %s has already been created.\n", FileName);
/ K, b3 D! F5 R7 h
3 w9 [; v$ Y. d Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");5 e( {$ Y$ r$ s d" G A9 ^8 B0 S
WaitForKeyOrReset();
5 Q0 _ A) E! j ) F8 S6 n3 ?( \/ ]
return Status;
. q: t. N& L ~+ {, T) \: V# c} |
|