|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*, t i3 ~2 g& C5 c2 I0 a" j; {
* myfileio.c4 G! ~ U5 v* p) r+ j0 o. g4 T. ?
* Apps
: i) \1 ^; X# K. l */ u. ` w' F# t. X# N+ F
+ U4 V8 `0 v0 q" E& J4 \/ d
#include "efi.h", V' l6 E' e' @% c3 Z% P+ |; @
#include "efilib.h"
, k' S, h! g8 Q1 U. f! ^- W+ r4 T3 o% P1 C# i$ x& ], q
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE* o( T" F; f6 g2 l, x: [0 R& Z
/ N$ A+ H8 i% _) p0 L- Mstatic EFI_STATUS WaitForKeyOrReset(VOID)- U0 p# x1 Z& f" b R) r% x
{) i, _7 U4 x$ t7 c E
EFI_STATUS Status;8 e7 j! f- m! m% c3 Z
EFI_INPUT_KEY key;
3 ?6 q2 x9 y; o* K9 e; s/ P, a UINTN index;8 j/ D% B2 ]6 M: z7 ?1 J; B- y, F
- H# B! Q% J2 y' [/ [/ h for(;;) {
0 Y5 d* t4 j5 \: X! K+ s. M Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);' \. L8 R% ~/ e8 ?. X0 d: F
if (Status == EFI_NOT_READY)
: J- ^- b+ ^2 x BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);4 N* b6 X2 H' w, q2 d! |7 z
else( T/ p; Q6 s) c/ g- w& q, `
break;
5 _1 z% v# x4 c3 C- m4 P0 k) a }
7 \3 f7 }1 L3 [ b5 _* i) W. y if (!EFI_ERROR(Status)) {. Y6 M- h9 _9 A3 M- |4 ^
if (key.ScanCode == SCAN_ESC)% ^. {8 C) z2 s5 N
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
' u4 }8 z% G) V, n8 z6 G }7 y' \: z- P9 B% Z! V
) ?) i1 ^! _) w
return Status;
: {/ N+ V. z# h/ G6 h. n% q}# t" k3 e0 M/ G9 w& R: o: R. x
$ r; g! |& e+ A; P9 S: w7 `EFI_STATUS6 o) C5 M8 l6 {" L
EFIAPI
$ Y) m8 \: Y& q; N/ \* R& JMyfileioMain (IN EFI_HANDLE ImageHandle,
" ?5 ^$ N" m" x d% d9 i& q u. X- ?1 o IN EFI_SYSTEM_TABLE *SystemTable)
6 J* d6 B5 q, u1 x{1 q( o* L; ]1 a# {& }2 n% J5 M/ s
EFI_STATUS Status;. Z. V; ]9 O6 v) {) h6 `
EFI_HANDLE *DestAddr;
/ ?* y+ }% x' Y! ?6 v( w( Q EFI_LOADED_IMAGE *FileHandle01;
. C9 r9 v5 h$ o, G2 s" o8 j& }% `/ y0 Q5 n/ X EFI_DEVICE_PATH *FileHandle02;
9 @' C" T5 x( a6 G7 i- T6 u EFI_FILE_IO_INTERFACE *FileHandle03;
6 _/ r5 W0 K% s. @+ X, y* F EFI_FILE *FileHandle04;
% f, {6 S, m3 u/ P" _% C- N: m EFI_FILE *FileHandle05;
% I# N4 Z! f* ^- I1 t! Z6 x g CHAR16 *FileName;
3 D9 F5 [" J( c% ~- q3 e CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;& P3 ~1 m) d+ E: P/ ? t) `
UINTN BufferSize = 8, BufferSizeKeySpace = 2;; _8 M- i! K8 R
int Bit0, Bit1, Key_Space;
7 a- |) x9 h' ]3 Q# m) t2 {: {% Z; t
FileName = L"NewFile.txt";0 F' f/ e! D8 p
Space_Key = L" ";
! S% K# M: ^$ Q9 U5 l: v " e1 r2 G* K7 R9 i6 y
BufferA = L"ABCD";
4 i/ O7 ?% F7 _1 @- J8 S BufferB = L"EFGH";* F* k( L. @, t* ?1 Y7 n
BufferC = L"IJKL";
% o5 G7 ?+ }: c! A" U3 l% f# X
3 A* q+ p5 M. n" ^( M* p0 H: I Bit0 = 0xff; v2 ~' K: c A B: V7 K
Bit1 = 0xfe;
# O" O$ b$ N6 ?% P$ \" x 9 b5 I! d# `0 n: `$ i
Key_Space = 0x0020;
4 a3 Q9 L6 V, n4 K1 {' o7 ], l/ `5 N
/ w2 ~; a5 w- x" Q+ l InitializeLib (ImageHandle, SystemTable); - h; }6 F2 A" [4 N8 T# F
* g# ?. z. P' V: d3 ?+ y6 k DestAddr = AllocatePool (BufferSize);
2 _4 X* g! r. \) V9 P6 u
; X4 R0 B4 l' f! p7 l0 L3 y7 h% b4 x Print (L"Value of Bit0 is %x\n", Bit0);
* L4 E) P0 q' @' q Print (L"Value of Bit1 is %x\n", Bit1); 4 T- h1 e3 q) W% h I& l/ w- h9 c
4 m6 ~3 D2 G; H6 L. w
) g G: k& \! N p; z) G
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
4 Y! ?0 ~& {4 Z' b if (EFI_ERROR(Status)) {( O! S q) O/ y9 @( w
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
) w8 \9 @: V( [. L5 ^/ ^( ]* v return EFI_LOAD_ERROR;% X1 m* U# d; ~
}/ J* W0 l( R3 V" v- C: G- Q) l' T
/ d+ A0 w. N, ]% O- e/ r( a5 n5 H) M Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);( A6 }% C8 T4 M7 m5 @' V' ?# [( {
if (EFI_ERROR(Status)) {
6 P* Q' F$ l3 e3 O5 C Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
+ L" U8 g( C1 Z3 X+ a return EFI_LOAD_ERROR;5 l, w0 i! f" P' F- u
}
6 V9 q% |' G% N
1 I) r& W* X' ?0 ?" a0 Z Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);$ ]. A0 J/ \! f9 }
if (EFI_ERROR(Status)) {
4 F/ q! J! D- j! a Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);; Z. e' \* y& v4 Y3 A8 |4 ?
return EFI_LOAD_ERROR;
9 P/ Q+ F6 c8 X! M+ X7 W }$ b |% O: \1 F# W7 F/ k( N
- r1 F3 y. D) f5 K) v
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04); Q, X# F) S2 r& h
if (EFI_ERROR(Status)) {- H5 u" m$ ?) J4 i
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);% Q }6 ?" C% O: w+ G
return EFI_LOAD_ERROR;
* Y6 h# A& ^$ {. H4 p }
- e A" f4 y3 }- k4 z1 C
0 v1 e4 b$ f, ]2 I Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);0 N5 Y0 P7 N" X+ ^
if (EFI_ERROR(Status)) {
- k; J5 }4 T/ d! s- `; a Print (L"Could not open file, FileHandle05 err code is %x\n",Status);4 n( \5 W" e" A( _& U, [' D( l
return EFI_LOAD_ERROR; x/ p( j1 Y1 D0 E
}
( k( b. F2 J5 v' I6 m3 Y
( P( ?; a: Z% m Status = FileHandle05->SetPosition(FileHandle05, 0); % E9 ?# x: q0 E# h. d
if (EFI_ERROR(Status)) {. _3 N* E! P% F: }* y( e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- c) j6 i3 s. S+ |0 G; b( [/ T0 f) W
return EFI_SUCCESS;
. [6 Z7 T( ^& {" C6 f' f7 ^ }
9 \ R- d+ V! ?; p6 T6 d. p
0 F8 `" Q1 ], _/ q$ J; @ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);! Z& d9 B S/ l3 l
Print (L"File Buffersize is %x\n\n", BufferSize);) N C% p8 }) y/ b
if (EFI_ERROR(Status)) {% ~) b6 g. a& p: x
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' p0 A% }5 K; y6 q1 V% \4 F, O1 }
return EFI_SUCCESS;- f2 \2 {( o; d. X. n" w
}
. V( e. N3 q# {2 T" z( h
: k6 A, _. m& C Status = FileHandle05->SetPosition(FileHandle05, 1);
; L& y* M8 E+ Y9 P5 ` if (EFI_ERROR(Status)) {% G6 b5 D0 f4 f: Z; h8 O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 m$ @- J2 r: f- P6 e4 _: a3 z7 r return EFI_SUCCESS;9 {+ w6 y. _1 J/ D7 ]% O! j
}0 `% w8 d& X# G: g( K
" i/ L* F* A( p2 n
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
; g6 L- E3 x* N, }; W2 x! b" L Print (L"File Buffersize is %x\n\n", BufferSize);$ R2 J" g7 K$ Q; H
if (EFI_ERROR(Status)) {
+ b1 |- U6 }" R6 F3 u: T Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
) \3 E! {/ J3 T: V- ?) m return EFI_SUCCESS;3 U3 [4 i- _3 z8 Y4 W+ n: n2 O
}
, |" _5 t& Z- X$ t+ Z) l0 {
% a* U* `1 a& S3 Y! T Status = FileHandle05->SetPosition(FileHandle05, 2); 0 ~' V/ ]" }; e! G6 ]3 o# V; V W
if (EFI_ERROR(Status)) {
" b, z* }# z+ S' C r# O3 V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& x# r: y8 i3 O9 A9 ~ return EFI_SUCCESS;
$ S3 j! x! ?" c" \& S3 c' t- Y }' J# b; o8 c0 r, R* b4 B
5 C$ S" ]' @ y+ X ?' V7 G
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);! R; O& k( ?7 [+ V. X7 V1 p% Y: d
Print (L"File Buffersize is %x\n\n", BufferSize);; t# N4 e* N# V7 O% f& `
if (EFI_ERROR(Status)) {$ `; ^5 R" q7 j) f' q; `/ R8 b
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
: Y- B& j5 A7 P% H return EFI_SUCCESS;& g6 A8 s) \9 C5 y8 l4 D' |
} . r. _# s; ]6 e1 }# h
8 i2 h9 l6 S: w7 c
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>5 M+ k1 h. h; ?5 S( p, _$ g
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);$ {# H2 z/ Y4 c
Status = FileHandle05->SetPosition(FileHandle05, 2);
9 ?- q) L( ]: ?# E) f" K& l if (EFI_ERROR(Status)) {- K9 ]0 v9 V" ?4 S. I
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- c `( o# L9 e3 t" L' G, n
return EFI_SUCCESS;
( n9 a- d% b$ n3 b, X! O9 l5 `* J; ^ }
7 ~1 d1 P L5 P: \4 x( V0 Q, O$ p% a2 M) p& |, O
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);& b9 y% u4 f3 S' @( ?+ m
if (EFI_ERROR(Status)) {; p* i/ k0 @+ D- ^
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);, [5 p) {) ]) r# X% s/ B3 Z, _
return EFI_SUCCESS;
# l$ q$ S8 y% |2 y- d } 1 I- y# B! U# d4 u- H( y" p" _
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB); [2 C( u3 F6 i( U
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
, Q6 {% a' g, i. u4 z1 o$ t8 k- K3 `5 N1 Y& y- S
Status = FileHandle05->SetPosition(FileHandle05, 10); 1 Y' |& P$ c) B1 T9 a8 p% e& S- v8 i3 p, o3 q
if (EFI_ERROR(Status)) {
5 x A; \' o& @: u s Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ J' R. f( s J- P6 S* _
return EFI_SUCCESS;/ J& Y) T) u2 a8 }! y' n B
}
* p. Z6 D; X; @/ \
( c! L' _* w( y, j- ?+ Y: H Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
+ j% t4 C1 k2 T5 l# }4 ~ if (EFI_ERROR(Status)) {& h% s* Z8 Q3 i; n
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( b& G8 E: i, U2 {4 Q! k' ` y return EFI_SUCCESS;
% v' p1 C1 y U ^: J4 U% `# l }
6 b, j5 z7 E# G# @
1 J, ~7 P2 H4 l& S d Status = FileHandle05->SetPosition(FileHandle05, 12); + H, m f& E7 t1 p) \7 g: v/ D
if (EFI_ERROR(Status)) {
( J, ^% i" r3 h! R+ O: k& [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& ?6 V2 g) R9 v/ n% b7 Z2 A( n return EFI_SUCCESS;$ y- {: H0 H: t! A Q0 s
}& C) f. F/ Y3 j1 [! c [9 [8 v
5 P2 i8 x" w) W7 Y# D Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 l J% l! c H9 ~
if (EFI_ERROR(Status)) {+ ]9 A2 s3 V- ?) D% m* G
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( B7 e ?0 r6 g# q" X$ v# t4 x5 k return EFI_SUCCESS;% L4 U4 K% s( i! L9 v3 J8 g
}% u: F' k" Z6 }- d8 F- k: C; [5 o" i
# ]7 ?& I' `3 \8 n' w4 g
Status = FileHandle05->SetPosition(FileHandle05, 14); ' x' |" ?' U1 O3 d, H( s- q
if (EFI_ERROR(Status)) {
& a$ n. H- y1 Z+ k! X+ b7 R- C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
^3 B' ]9 T% X I: V, R return EFI_SUCCESS;
# C. J) Y& ^( Q# m i. L6 d }( i" u; r; v0 E( G# U
: u+ f9 l# B& [- Q& E9 S Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);0 N2 \/ P" F8 Y/ f( i v& ]0 D! S, W t
if (EFI_ERROR(Status)) {" T, l0 o! K( I; B8 n2 q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 T. R- Q; A f return EFI_SUCCESS;
# K, Z& V, R$ \( A }% ^ X4 v5 x. T1 U
' m5 Z: T4 Q4 E7 i' V# T
Status = FileHandle05->SetPosition(FileHandle05, 16); 9 O5 C* v5 {, c0 H* g
if (EFI_ERROR(Status)) {6 u7 Q* L: U' k. N' h( `3 k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; b3 k5 O* y# w/ j8 N return EFI_SUCCESS;$ b3 A7 q' l7 \$ z$ c- {
}
5 D7 m5 Y x2 f( i ' {% n0 O6 c& u5 U2 {: ]
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
d# I# ^6 r7 m. B. |% B if (EFI_ERROR(Status)) {' Q6 A4 r) c" b# J& e) i. ~* q& t
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( i0 p1 e8 Z: P$ ~1 V( t return EFI_SUCCESS;) w6 N$ F4 s4 L$ G- s. N
} 5 {" x! o2 O- [! F7 ]/ @7 {4 o
2 j1 @# y+ a2 A& ~* c( S
//---------------------------------------------------------------------------------------------------------------------------------------------------------------/ U- O, }0 ^7 L0 U
$ G ?* B* P7 k" j; V) u! e
Status = FileHandle05->SetPosition(FileHandle05, 18);
6 L% B# z! ^$ a6 S if (EFI_ERROR(Status)) {
3 i' U5 S3 W0 n0 p& \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! W; P4 |) C/ G- w) { return EFI_SUCCESS;# q- B t! |) [) h8 m; L& B& ~
}
; u/ g; W0 g5 ~0 C& g) c9 a) K
" s* \5 W2 V9 R* o& [8 L l Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);- ?/ _8 T! ~& p+ A: I
if (EFI_ERROR(Status)) {
3 H* ?1 R! B% i" V' U! n Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 X O+ F. K1 b. @, l
return EFI_SUCCESS;
, t2 U( C- S( P2 l- {' o/ R1 B } 4 l2 `% Y, Q( O3 Q6 _# c+ X
F H* b i% C, i% Y w
FreePool(DestAddr); 4 @" _- C! H1 H; x: M* x$ c/ `6 [
8 _# ~0 `! I, }5 q- a/ b% @ Status = FileHandle05->Close(FileHandle05);
% @! c% m4 M+ N if (EFI_ERROR(Status)) {
6 V1 i7 I$ A% _. F4 Q$ t, g; D Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
; o j+ h# d& X6 Q return EFI_SUCCESS;
- ~: ?: L8 F, t( Q }
0 Z$ {% Q p. t6 H# h
; f! F) E1 b0 F3 i Print(L"File Name = %s has already been created.\n", FileName);3 J; \+ b. |7 n! O$ D5 e
" W" [2 p9 A7 R" x3 { Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");8 F. Z) r: K+ y6 ^& Q/ `
WaitForKeyOrReset();+ b9 o8 ^ P- e
, X0 O$ F% e3 z/ O
return Status;
) h, y8 N; T \$ j- \6 r} |
|