|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/* v4 l; M& F1 N4 I9 x- S
* myfileio.c% h8 M" ~' I7 b' f$ n
* Apps
/ F& l3 Y" R* V7 f+ ]. o */
4 r: h2 R( I$ m# s
/ N& x- o* Q- m; U8 `; r; b6 S% Q#include "efi.h"
: Q) W" f8 C$ c6 a#include "efilib.h"$ n( k5 N: B& v; u* @: W& W- m- o8 Z
, n2 |- I& a& |8 a" s e, E7 f#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
- S& f2 J+ t$ q) |# _9 P
6 l5 ~: {$ |! v' Z, x6 Vstatic EFI_STATUS WaitForKeyOrReset(VOID)
C8 L3 ~* a8 ?$ J4 l# q. T+ o; _* F{9 l9 E" z, @6 l% w( l: j/ `3 e. R, F' |
EFI_STATUS Status;" D7 e6 e# q( {
EFI_INPUT_KEY key;
) G2 }* _5 Q8 _ H9 y; [ UINTN index;
% D4 ^& B: ]- M7 F, Q " [# |) V. R! ~- g2 \
for(;;) {
& F' M9 z, b* @6 w Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
8 V% q) A7 \5 {* ^. g% k$ z if (Status == EFI_NOT_READY)" p. j! _2 E3 @1 H4 C
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);& \1 ]: j9 C1 ?2 I
else
- X. Z3 _8 r6 [8 o& x3 Q break; X( T( O" C7 x1 B% F8 j
}9 k8 ~9 q; ?& i& D/ [4 _
if (!EFI_ERROR(Status)) {/ o. t) J o4 k; e; K9 Z$ C
if (key.ScanCode == SCAN_ESC)0 g R! w+ t1 E6 Y3 _4 W
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);: r4 q5 x- _9 i
}& p4 v0 W* ]' [: E) b: g* U
5 P* b6 \& G! d9 t7 H return Status;# ~* t+ }8 k8 B! ~% Z* b& b+ I
}$ ?3 Z2 c$ t* }; r3 G, D% C$ X3 m6 H
8 F. G& O, H5 T7 Y# W7 y4 f' `
EFI_STATUS" C, Q. |! H, C4 c4 w0 J
EFIAPI8 P' j9 w. M' R6 J7 |, ?. {
MyfileioMain (IN EFI_HANDLE ImageHandle," C5 a ]1 @+ ^4 h5 ]# x% q# U9 x
IN EFI_SYSTEM_TABLE *SystemTable)/ v% q8 n3 {+ y' y
{3 o2 V3 C! h0 H
EFI_STATUS Status;
0 J0 \% {$ ?3 ~1 x7 C EFI_HANDLE *DestAddr;
1 X4 x5 h& y. ]9 c EFI_LOADED_IMAGE *FileHandle01;
+ T+ {* W9 V: s" P EFI_DEVICE_PATH *FileHandle02; # ^* V Y' h5 T3 I
EFI_FILE_IO_INTERFACE *FileHandle03;
7 U7 D6 D- K& z: c9 [ EFI_FILE *FileHandle04;- e& j+ \' c5 }0 K( A
EFI_FILE *FileHandle05;, T3 e% g6 |; ?. [
CHAR16 *FileName;# x5 F6 c& i' H6 \8 P
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;$ X4 D! Z( W) B0 h3 j! B- d
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
4 ]6 W4 q4 m# \( u8 x int Bit0, Bit1, Key_Space;
# Y8 h% x7 p6 {
- U- x- V' n7 Z! t7 k FileName = L"NewFile.txt";
, u4 }. i5 x* R- [ Space_Key = L" ";" S% x- T! D" x( p9 U
; R" L8 u5 n @/ `9 q( X- p! M BufferA = L"ABCD"; v: b `5 Z7 o/ M/ N0 }
BufferB = L"EFGH";
' }* i J# g% m. W2 y* U BufferC = L"IJKL";2 N; w$ j! j8 @$ x* l' W% ]8 L b
8 s# |+ E# U- f" V0 Z6 s. I
Bit0 = 0xff;
6 u+ y* h% A6 F- q Bit1 = 0xfe;
: j6 M9 d/ H3 H1 w& y( @6 E
# L: g. Y/ @' w Key_Space = 0x0020;/ |3 `% o# z/ y' w! t) ~" C) X* n
' D* P" V4 C: _* v* D+ w
InitializeLib (ImageHandle, SystemTable); # t: Y* I1 i( L& p9 c' J
5 D. _+ V' d2 P* }* _$ Z8 [9 N DestAddr = AllocatePool (BufferSize); ! Y8 s- Q' K' {( C
0 T) b7 J5 V* ]" B4 g" o- f Print (L"Value of Bit0 is %x\n", Bit0);2 h% a7 w; z$ o8 i
Print (L"Value of Bit1 is %x\n", Bit1); * P) [' {. F7 _ C! i/ L
' h* i7 I' ]/ \; q: ] & d. ^: n6 i7 {$ O7 H* x0 U) C6 N) [
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);9 u8 M" z) g, ~9 L+ J* Q
if (EFI_ERROR(Status)) {
3 o$ w& R" n5 K1 m Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);6 i- N; i0 t* \0 b
return EFI_LOAD_ERROR;8 ?4 u& V$ ^% ]( b7 a; j% u! p% N* t
}; Q" M: ^ ]% j R. q
. @) l) Q0 w7 |) {+ B& o3 \
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02); H& L9 L9 T6 w: c
if (EFI_ERROR(Status)) {
$ s% p" v% G/ J4 w Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);6 V1 I) @# i3 v! [: g. G
return EFI_LOAD_ERROR;3 ~; J$ ]1 z- x5 U2 `
} / Y3 Q2 l* K' c g( X: e
' e8 w) E& T# e2 { Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);- y8 E4 G p9 O6 r4 X! u |2 B
if (EFI_ERROR(Status)) {
* G% S" g" t, n; Z# J Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
* d8 f, \% H# A" D+ P return EFI_LOAD_ERROR;
0 y( ?* Z7 B" [4 ~/ C }- o4 S* p' U+ Q# b' ~- W0 X6 R
6 i4 Z" e2 D6 V2 @* u2 q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
# d; r2 K; M( d. P. h0 S if (EFI_ERROR(Status)) {0 i% j; y7 v" `( R; `
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);5 u, L5 m( P4 x4 B( a* h0 n) U$ {
return EFI_LOAD_ERROR;
2 R/ ~4 ~9 K5 A, |* }% u* p }
; ]! t8 w$ J) u' u$ P7 z/ f' }% U+ ` : j. Q$ _! D, o! O' R E/ v- p
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
; j/ i5 i3 p: g0 c# M if (EFI_ERROR(Status)) {
# b- w6 \* \- C1 j" K. U7 H Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
9 l+ \3 K0 P0 C4 r u1 E+ e return EFI_LOAD_ERROR;
; {& _4 h1 g) t* o* h( B! X }& W3 a! C5 f: n' f6 g* l) P9 D$ G, u
5 `; i3 J9 _* N Status = FileHandle05->SetPosition(FileHandle05, 0); 3 {2 l+ |2 n6 T0 n' L$ M, Y. R
if (EFI_ERROR(Status)) {
6 C D1 O# o, \% w, _. \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" U d; Q6 P) p. ~. v6 j0 p! j
return EFI_SUCCESS;, N& Q4 I `; w- P6 {- D
}6 j) y$ P4 u& C
2 _* I8 |3 e( j7 ?. T2 R
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
i, W/ @6 f& q" s+ k5 ~! q Print (L"File Buffersize is %x\n\n", BufferSize);+ L/ n6 t; }& W! u O- F) U
if (EFI_ERROR(Status)) {
# T7 C! L' j" D* {4 i Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);- c8 O% A& X$ E3 H
return EFI_SUCCESS;
& q' T% b3 K9 s( n7 c } 3 g% G' c& S8 y
: b7 j& J8 a+ u+ M7 `; S7 X3 Z
Status = FileHandle05->SetPosition(FileHandle05, 1); - y* Z5 v# H+ z& K+ f) F1 @
if (EFI_ERROR(Status)) {3 X. o3 F9 i' E J6 I+ J. m- P4 E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 m x4 B$ X) s return EFI_SUCCESS;
5 ?$ r) l2 ?) T/ O# j* }/ z }
& J/ y+ ]3 c3 ~7 g
' s3 R9 q+ O9 C$ L Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
& y! b; Q6 \3 x+ L Print (L"File Buffersize is %x\n\n", BufferSize);
: P. p1 q ~# j: Z* Y! [ if (EFI_ERROR(Status)) {" {- X# T( ^/ j& `
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
$ P4 y8 D0 @' T% c5 d. G$ H5 k return EFI_SUCCESS;! M/ f6 w8 e+ J/ F/ j
}
3 E6 I) \) G+ _0 t5 ^ 2 P3 @4 \0 v2 u5 l0 I
Status = FileHandle05->SetPosition(FileHandle05, 2);
& z, ~/ b* f- G5 L if (EFI_ERROR(Status)) {4 b3 y1 `3 b8 `- n# f5 Y* J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 }- m( i; Q" V6 w. H* |0 K" b- u
return EFI_SUCCESS;; _1 p0 _1 Y3 A. \( _
}
- U. M* z8 s' P1 M' m $ r; X- B$ x/ p
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);+ {- S4 J; t# g
Print (L"File Buffersize is %x\n\n", BufferSize);6 i/ o5 u3 z/ M! l) v! X
if (EFI_ERROR(Status)) {/ V) E7 H3 ` T5 E1 T) L& P
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);7 S: |+ y* `+ D* I/ z
return EFI_SUCCESS;; u$ T* R' K/ p0 M3 a) t; \! E+ z% w
} - V2 [3 B; ~3 r/ {9 ^
# v4 r" V. l/ n( t% y4 }//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
% l( P. D# z+ V! n Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);5 f( Z: N' D5 w% k
Status = FileHandle05->SetPosition(FileHandle05, 2); 2 T2 X% O7 M- I+ M, v: u
if (EFI_ERROR(Status)) {
' `# J4 {+ d9 A4 B2 f8 M/ a. ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 Z/ _3 F/ p$ k* b
return EFI_SUCCESS;
5 W2 \! a6 v: J6 v! r* p% K }6 A# a; L! q4 K0 ~, `8 n- V% j e
8 n2 ~5 Y/ m/ u; g% D8 n+ [ Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
, D, `$ N' b- `. {0 A! c& b% B if (EFI_ERROR(Status)) {/ W$ D( o$ o& a2 h
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
* H$ f7 Z8 W1 G% Y# m2 E7 ^ return EFI_SUCCESS;
4 y) O) A% g. y+ [' P# ~) y, V } * c7 K' W. k4 g
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
5 Q2 r5 ^$ s$ z& Z//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<; F1 z+ y1 K$ w3 O
8 p* `4 _0 P$ u0 q, |, S3 V4 K8 J
Status = FileHandle05->SetPosition(FileHandle05, 10); ' F, O$ x8 c. \# s D$ n' [/ X
if (EFI_ERROR(Status)) {4 ]" l& C! E1 Y" p8 M7 Y: D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. h i% o1 c5 {: l5 m( C( L3 ^1 b
return EFI_SUCCESS;2 k7 n2 c: z x5 n
}1 o4 |% E X5 c/ ?& c, P9 N
! N$ L D. h+ L* T/ ^3 }
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 u$ D4 K$ w# }/ R: ~, ^* z( m- } if (EFI_ERROR(Status)) {
3 x1 p3 A' |- ~" h( A, Z& x l, U Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 O2 D/ p- J1 [
return EFI_SUCCESS;% M- e! o, r8 U2 q* k' G
}
) y/ H9 g/ k) @: t4 Y; ^2 \: e
4 y4 p& ]8 Q7 ~6 w# n p Status = FileHandle05->SetPosition(FileHandle05, 12); # l) _5 c) P m+ a E M1 g( t
if (EFI_ERROR(Status)) {, M& W2 K$ L0 T4 N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) o& o; a: L$ x+ K" S return EFI_SUCCESS;9 _ _( Y6 @- d7 s [
}' Q/ \. |4 ?7 G8 M) N( `" K/ [* i
+ i* `* a) O2 a2 H1 s: e" Z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);# f- ]- G# L5 Q; U: Z5 r& V! o
if (EFI_ERROR(Status)) {
e! J( e9 X. h# `9 X3 l: m Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
0 z& t- ~- \8 L( |8 i7 o5 Z! H return EFI_SUCCESS;
+ b, C6 D/ e" P- Y) r6 a2 ^ }& O3 {6 L- D$ O% J4 F; `# q: b
6 j' f% D, X' ]+ d" _ N$ X Status = FileHandle05->SetPosition(FileHandle05, 14); ; R0 n" A( x6 Q& j% t
if (EFI_ERROR(Status)) {
3 u1 y/ B( h) q9 K+ t& F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: _3 o: Q3 ]/ [# ~' z return EFI_SUCCESS;9 B! p3 k6 V" u# k* k `8 Z: ^
}
( ^% \" _" `6 F% ]4 x
' k S8 _# Y! J& G _5 N Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# A7 y Z3 j# J& X# X3 }* v if (EFI_ERROR(Status)) {/ }9 m" C* g9 Q8 b3 {" v
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 R- o% b0 q( H* l$ ?- O
return EFI_SUCCESS;# n& d+ t2 S0 J6 m! }
}
' L5 g8 H7 b: X4 D
: O4 g& R7 U3 H8 ^7 M' K! _ Status = FileHandle05->SetPosition(FileHandle05, 16); # I' a X1 D( e) R5 s7 I
if (EFI_ERROR(Status)) {
9 P, X- } v2 {5 l- X. N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ v6 A0 J0 k( ` R9 X% k& I; }0 Y" V2 ]
return EFI_SUCCESS;# D6 I# N: r, j4 D) E! x
}
7 E& Y. U: {2 k% h+ ^7 s+ U $ r. V: ^5 W! ^- M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
" E) E" i d8 I3 B8 d if (EFI_ERROR(Status)) {( m( ^5 K U6 g5 q4 h4 v4 ?; B$ H
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. ^! w4 G+ j1 k- k3 K# I return EFI_SUCCESS;9 e2 w% |( L; t, @6 c5 |. \( Y1 j. P
}
: L! {! M4 o5 s) z
( w( z0 U+ }- g3 T//---------------------------------------------------------------------------------------------------------------------------------------------------------------
4 w. d) |; c$ |' e
% J! T2 I1 {8 o5 m$ F Status = FileHandle05->SetPosition(FileHandle05, 18);
( B( X2 S( L8 i if (EFI_ERROR(Status)) {
: I; H& B) `- O9 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" p8 E, s0 [& s; q4 h1 x return EFI_SUCCESS;6 M/ f/ m- h& B& I
}
5 t( N9 B/ r7 m W ]$ }
" j) n% ]7 ?6 m& s$ v4 l3 K+ N Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
9 {1 {0 P1 s1 x5 X1 u( w if (EFI_ERROR(Status)) {
R8 R i# \; ~3 s7 ? Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
^' @& J; J' j/ e" Y return EFI_SUCCESS;
, _' k$ y4 ^4 L5 ?( D' { } $ V( w* t, t. `0 N* l
! o: k3 J) x8 a0 A0 n FreePool(DestAddr);
1 Z' S5 v: d0 L) D0 Y4 x$ E
. i1 W- X0 }/ b' d% `% g Status = FileHandle05->Close(FileHandle05);) k9 _7 f6 \4 a+ c2 N* I! [
if (EFI_ERROR(Status)) {9 z: M. d% |% \- s j8 ^- S
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
) y# }- J/ N. s: V% s7 R- h$ | return EFI_SUCCESS;
4 |, Z5 y* U2 @+ ^4 U }+ R% P& J" Z0 S' M& Z
: V r. ^+ |4 }+ L+ y Print(L"File Name = %s has already been created.\n", FileName);
. P0 @3 C, L2 E' ^3 Z4 a) D; {# s
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");; e8 V2 i3 M- W' [3 K8 m) m2 U# ]
WaitForKeyOrReset();
I, V/ ^; ^6 Z
) s, F% G) R4 M1 [/ h8 Y+ V return Status;
: X7 i1 z( J, g6 `1 L} |
|