|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*) R+ U$ m J. n% C* }; ]7 b& S$ ~
* myfileio.c
) ^ z1 b1 z2 \- Z" _5 _' r * Apps
# {: x3 ^7 d8 R5 v, M1 | */0 k% n1 U' I+ }/ T4 R
6 g* J: P6 w( C! p9 g2 N( S3 H% i8 J#include "efi.h"
! v! F* e( N3 ~* H#include "efilib.h"
2 @9 m2 Y* _, P! |5 m: x K' d: p" o) e: J3 A" e1 S9 e
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
N2 r/ g# Y F, j% s6 O0 x; a# f) C* Q$ u1 a
static EFI_STATUS WaitForKeyOrReset(VOID)
/ R" |0 w* z& |' K5 L8 H3 q{
* J. O# H/ n( w' b EFI_STATUS Status;. j4 ~ s) ^7 |+ w9 P6 o. {
EFI_INPUT_KEY key;
1 \1 Y" O! h7 \ UINTN index;0 e- @; ~( D+ l* l
+ P& H. R Z' c for(;;) {. c4 T) z3 N( Y/ G5 a
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
, s4 ^% C, Z' H% B7 X8 \ if (Status == EFI_NOT_READY)
1 n4 d) C; I! f- V+ w9 L- y BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
3 D- r8 s6 R" T5 q9 q' G6 T else
& [# `* h& I4 c: R# Z- Z4 P! I break;/ ~4 H8 T2 b2 ~& h
}7 g; p3 R& ?$ T6 _
if (!EFI_ERROR(Status)) {( {9 L q. v( l+ P0 n" c$ x
if (key.ScanCode == SCAN_ESC)
3 d+ f' b1 v- d$ r6 J [ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);8 X# z+ I7 Q& X" V" W/ E
}
0 }- B; d+ R, W( @! q6 O. E8 h
+ e: g- e! K6 K5 u$ D) Y; @0 `6 w5 e return Status;
- J4 M4 |/ c( @. w$ y1 z) C}
- E' R1 s; ?* \7 q% {+ N4 ?# w0 t6 g# [& z. S u% Z! w
EFI_STATUS
& Y" }0 Z' E# Z3 d- k+ e7 \EFIAPI2 Z# [0 n( M4 ]/ @/ Q8 n; d. G8 G
MyfileioMain (IN EFI_HANDLE ImageHandle,3 L2 [' J" b# N# O; B
IN EFI_SYSTEM_TABLE *SystemTable)# V# p& s0 F# X' d1 I: x8 w
{# Y. k: ]3 V* G m, U) S
EFI_STATUS Status;# `# h# Q, }) h% W4 S4 R4 C
EFI_HANDLE *DestAddr; ! n5 H7 \3 @! Q- O6 X3 U; \
EFI_LOADED_IMAGE *FileHandle01;5 b( _4 y( I0 R% K9 X E" B3 u' Q
EFI_DEVICE_PATH *FileHandle02;
5 k! q( _0 b2 P4 | EFI_FILE_IO_INTERFACE *FileHandle03;" G+ I8 _) b* W0 \! r. V* V9 s+ S! } T
EFI_FILE *FileHandle04;" O, U' @. o d6 \, o# F& r
EFI_FILE *FileHandle05;
7 Y r6 S' H( i) ~ CHAR16 *FileName;
6 F& H& l4 ]0 r g$ D( | CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
, t$ n5 z( \) q! `$ j1 _5 B& O( | UINTN BufferSize = 8, BufferSizeKeySpace = 2;
3 K& n: H' m* o4 [' `2 q int Bit0, Bit1, Key_Space;
) `/ o$ [2 [( i1 f4 s0 Q. c
+ K& W5 A6 R3 k% C* }4 e FileName = L"NewFile.txt";
: S1 z0 u9 w8 |; V- B Space_Key = L" ";
3 x5 w: O1 L& x3 Z! U
2 J4 o! Q% a* r* c; k& k BufferA = L"ABCD";' _, |/ T/ [" o9 |$ B: h' d' S
BufferB = L"EFGH";9 [( S. `) i5 R$ A3 ]3 S
BufferC = L"IJKL";
. _, E- q* B" t
- e- C/ R4 A' f Bit0 = 0xff;; U t( [* h" s. U. T" q; K
Bit1 = 0xfe;% Z0 |: D: t: D$ r' ?
' r' f5 F% M# M$ B" M% M Key_Space = 0x0020;! i5 R) g" @: P1 \+ O4 h' v) e! K
5 U$ C& u4 Y4 D5 I3 ~! s5 F InitializeLib (ImageHandle, SystemTable); & K( O& d0 D: k; g0 w
' x5 Z$ }7 s5 ^
DestAddr = AllocatePool (BufferSize);
, s$ x/ [0 {& Y9 ]0 @+ l5 p* X7 `) M) P% f3 @- B+ M
Print (L"Value of Bit0 is %x\n", Bit0);
. J; G. F' o# E' ]' f! } Print (L"Value of Bit1 is %x\n", Bit1); ( y' T8 T0 h0 {9 z0 B' }" P
1 G. e0 H! p' s
) s: Z* t( d1 G4 E+ r! K& R: C
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);, ^% B8 Z$ h1 G! C/ u
if (EFI_ERROR(Status)) {5 S5 y2 P8 s( B
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
4 @4 N2 X/ L7 E5 t, n return EFI_LOAD_ERROR;3 c7 H( V/ X9 D4 B: d n) A' Q
}) ^3 |4 }: q' L I/ C8 T. v% o
8 e' t7 w' u3 M
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);1 t9 Q6 k1 L+ I1 ]5 n1 L: Y) \
if (EFI_ERROR(Status)) {0 \6 e( z `4 k4 ]# T, P
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);4 d& i) }+ s, _" i+ z- D
return EFI_LOAD_ERROR;
9 L5 F; g8 d4 G% Q9 {" x8 Q2 R9 ^# b } # F7 o ^3 m% A/ L
4 `0 O7 A- U: k$ ?! n
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03); j$ B7 C1 V% c9 d
if (EFI_ERROR(Status)) {2 v* k/ f2 ]9 j5 [& u3 o- E3 B
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
0 Z, o* m+ O1 O4 r return EFI_LOAD_ERROR;
! ^) k, D. P. F0 [$ t- {+ e }
/ ^; l8 J% c6 \7 m. ]* k6 m2 S; J! C/ n9 t7 o1 S4 N
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);" U3 G0 j; F2 P: Y5 v5 ~+ n
if (EFI_ERROR(Status)) {
. V. @4 B5 q9 ?+ O8 ]4 o/ w* Q Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
- ]9 v! c3 g/ N5 F2 o1 u return EFI_LOAD_ERROR; n6 S ~( q! y; g7 S5 B
} ' i4 m- S; d/ V d! y! H
( V, A) X! |6 {; l, L
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);5 S. I- Q$ {0 H
if (EFI_ERROR(Status)) {7 e c9 @4 ?, a; K8 K. K9 B* s
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
C: U* [% e: {% {/ S. F s return EFI_LOAD_ERROR;' c9 k2 F5 p' s1 L/ B+ d
}' l6 p s& r8 Z* _( A8 E
' N5 j* E% N! ^, H+ J3 o
Status = FileHandle05->SetPosition(FileHandle05, 0); 2 F. m( M4 b; }2 k
if (EFI_ERROR(Status)) {! l. V; L: Q! T7 W- F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 D- t% |4 \/ Y/ X& \( s
return EFI_SUCCESS;
% L* V/ W) @9 }# B% i }
7 Z- n' E0 j) C4 k7 L! l% z6 w! H 6 n' V8 Z6 j9 Q( q* ]. e/ x( l
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);; o, n$ |: N/ W) m( Z) s
Print (L"File Buffersize is %x\n\n", BufferSize);
' ~! a, S/ M+ s* e if (EFI_ERROR(Status)) {
# u. B( ]8 c1 T# O/ x$ W# d Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);. v2 T. ~8 E/ n8 |8 I
return EFI_SUCCESS;5 | ^5 M3 h# i J* j) n
} 4 X+ g" n/ x/ _+ t- P# f8 L. ~
* J0 z. V# z! p6 T& \' B Status = FileHandle05->SetPosition(FileHandle05, 1);
* v2 U: z6 ~# b2 t* ]1 J- g if (EFI_ERROR(Status)) {5 w% l1 ]! X; C1 q3 u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 |+ N# |: r, m: O return EFI_SUCCESS;+ F4 U+ `5 {) T7 t8 e
} r0 e: ~. d$ B
0 R5 M6 C1 ^9 R U1 K, ^, e
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
, J1 }; o0 k) R- v( q3 s Print (L"File Buffersize is %x\n\n", BufferSize);8 M' D# e* O t9 h! @5 q
if (EFI_ERROR(Status)) {5 X4 m# _% z6 |' x* F* X
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& _4 L& d4 v2 S0 `+ d! D% ? return EFI_SUCCESS;
! g X/ y' b& x% F# D: D } 3 p, B- Y" F& j/ y/ _% `5 z9 j& c/ }
, P! t) V* U# n' x Status = FileHandle05->SetPosition(FileHandle05, 2); ; P M4 `/ T& y" i/ y4 z
if (EFI_ERROR(Status)) {9 f# ?7 Z' [* u# H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 E' Y7 Y* K3 j& K
return EFI_SUCCESS;
' r# V4 {+ T( }' o }
8 L$ v" |) \8 _6 ~) L0 q0 B' N ; y3 `$ D3 x. Y- m0 S
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);3 i8 k [; n, n' h5 P8 J
Print (L"File Buffersize is %x\n\n", BufferSize);
4 z, b$ Q3 l8 x: n if (EFI_ERROR(Status)) {
! h w+ i# J) ~, \; e$ g Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, i9 P1 K# a0 n
return EFI_SUCCESS;1 h4 I0 Z6 v0 |: t9 h% M( k
}
3 @1 z. L1 R8 u
4 x: ]) {0 y: b//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
* N2 y# L+ ^1 f; D) h5 Y$ _2 ~ Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
" B# w: n C5 Y4 Z Status = FileHandle05->SetPosition(FileHandle05, 2);
1 n {, g; y' ~2 N if (EFI_ERROR(Status)) {
6 [7 r# `% n/ V3 ~$ r1 k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
% h- f7 B ~1 ], D" _ return EFI_SUCCESS;
% C3 h, N, @! T) | }8 B- P5 z1 R- g
; B y7 w( M3 S+ S+ i1 l+ E Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);- ?- u$ C3 X l
if (EFI_ERROR(Status)) {; [- a$ U4 o/ H0 a$ T/ ^
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);" L0 J: l" K/ t! g% N) x' c. w
return EFI_SUCCESS;
% p. _6 Y' L2 [: a }
4 E+ C, u% ^7 L6 g# U Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);4 C. U, X3 H. @( ]
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
, s$ }0 d' u0 S& P4 I
0 k3 G/ ]2 A: b- J Status = FileHandle05->SetPosition(FileHandle05, 10);
% s% e6 h) t+ L# T if (EFI_ERROR(Status)) {% M* @6 g8 g& ?9 }; }. c* \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! u* H& s8 S6 n& f! N$ o return EFI_SUCCESS;
( ]" L, e# Z( l }
+ h) R, U8 e+ R1 @. g
# C+ C" p: m/ p Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
' z" J9 o- }$ G- f3 O V0 S if (EFI_ERROR(Status)) {
! H h% H$ J }; W! o d+ ` Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 g& h9 c2 L8 { h
return EFI_SUCCESS;
9 h3 I* M+ [/ @* ?- Z9 i x( p' B }+ `4 E& A2 y/ q9 E$ |
; G0 T6 z3 F$ V3 h4 d n$ m6 ]. f Status = FileHandle05->SetPosition(FileHandle05, 12);
6 t; j" m" i! W) L6 c if (EFI_ERROR(Status)) {/ Q7 \/ {; a8 ~: ^3 G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" j0 l. x9 z9 r) C: m9 d/ C
return EFI_SUCCESS;4 L( V$ F! G! T1 u4 ?* w, m
}
3 M3 q1 Q( v! I* y+ I8 J
' E3 u8 M: m( d- N1 N& o p Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 O% @4 Z. `" p if (EFI_ERROR(Status)) {9 I/ _9 B' j) p/ ~( J
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 _# C! S5 J6 m% u
return EFI_SUCCESS;
% B, u0 |% u. M' p/ {; | }
, |9 h& d2 b0 S/ T+ l+ H
# ^& l4 p J, X; U1 g. _ Status = FileHandle05->SetPosition(FileHandle05, 14);
7 _. b) J+ |0 z; ]% }! E if (EFI_ERROR(Status)) {4 [: n& {1 ]( p2 u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# }! C/ C0 O5 N/ y( J( Q; f return EFI_SUCCESS;
Z5 h/ Y+ D+ y8 U5 }4 T$ A }
8 Z+ `3 o# [0 W0 z 5 J. I& F, y( I( T( z: ~3 A
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);, G& K. F. p3 w7 G8 D
if (EFI_ERROR(Status)) {9 {3 M& Q; `2 y7 L) Y" i
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 V2 g" D6 C0 P7 b3 d( W return EFI_SUCCESS;
3 d4 {# V. @4 O }
' L; r& h* m1 h' o- m& n1 U4 U6 i) U" v
Status = FileHandle05->SetPosition(FileHandle05, 16);
8 P. N. i. r, t( ]$ m if (EFI_ERROR(Status)) {
: J) G2 Q8 I2 M$ g2 [7 Y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); w. K H7 J& Z0 e
return EFI_SUCCESS;
; u) z! D0 ?. z; S/ X1 A }
" S8 V, _$ A4 T2 d6 g; O
" ^# w+ q; ~4 F( q4 X3 O! K Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);) w4 v, I: z* d( |5 Y8 V' j
if (EFI_ERROR(Status)) {/ |3 K- W# P. E* d
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 Q, K: ]7 Q7 |7 O return EFI_SUCCESS;
$ P5 G# r0 t, k. c* ? } " @# j9 i* ?- c, F$ T
N. Y' v7 U6 K. Z2 `1 e
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
`* S0 D! R3 w6 |/ C
+ P8 i5 ^) f& @0 s# V; {5 j3 Y Status = FileHandle05->SetPosition(FileHandle05, 18);
# F9 j0 B1 a' t+ M9 o. ]+ S( _8 s if (EFI_ERROR(Status)) {
* K+ i5 `% c+ Y% l! z4 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 F+ t) i; l6 h% ^( {" @
return EFI_SUCCESS;; K" r& _4 ~1 a+ P
}
6 F& P: m. S/ Z9 x
9 A- Y6 G4 R/ j+ I" b5 V, B! [ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);& d, s+ P, q5 l0 \) o) x
if (EFI_ERROR(Status)) {- F$ m. @3 H* \1 M2 G9 o2 \( b5 R! u
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 G% _) l5 I4 Z n6 \
return EFI_SUCCESS;
3 N# Y' o v5 @# X& O } 4 ~+ k& s6 o# ~
9 ]' B+ G3 I2 F1 o
FreePool(DestAddr);
- H# Q2 O' c" ?- E: A+ `6 y0 o& E8 l( V2 j% L% q" ?! W
Status = FileHandle05->Close(FileHandle05);
" E- W% I1 [+ Y, b. d if (EFI_ERROR(Status)) {
/ W# D6 B) Q* D2 R$ O4 N8 B Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
" ~1 Z' T7 i" D3 m return EFI_SUCCESS;
% x' Q( Y6 D$ E; N& Z/ k5 o }
) R) ]8 n& t7 ^8 q5 X0 f& Y
: N4 {- ^3 a2 D* a8 A# @( S# j Print(L"File Name = %s has already been created.\n", FileName);
& ~' W5 r+ ?. L+ H# {
! a) H8 e' o/ J' ^ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
! A2 `3 ?; i. S( n* s$ a' z) f WaitForKeyOrReset();
# Y1 U& C$ L; ?3 s _ 3 F( j2 f7 w* Q6 E+ ?
return Status;
) U* ~$ X: p5 `) h) E% v0 K} |
|