|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/** d: \" W+ H* w1 x
* myfileio.c
0 Q- z$ e% l j$ C; b * Apps
" p; b e) Y1 z9 N */* n! C4 M# ^( n, P+ W
' [( M) r4 V0 L1 k! i9 x; L n
#include "efi.h"' f; P ?: D' i8 f
#include "efilib.h"' _' P/ v; e0 j6 B) Y
8 ~/ L- L$ _) V$ @ A' S) G
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE! f/ _$ l; }( ]" f2 A
, Q; z: w; N8 I1 g6 A
static EFI_STATUS WaitForKeyOrReset(VOID)$ g5 a. N) z3 v4 B
{& u$ v+ F( b- Y; a% o
EFI_STATUS Status;
/ ^1 b7 ~) {' o+ r- {0 N EFI_INPUT_KEY key;
l3 Y! I- @! i UINTN index;
- G7 s, E4 i! |- M; |1 K 9 I7 i: X1 b" O8 d$ N: ^
for(;;) {
( a; z- g/ z4 ^ Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
$ w8 f/ Q6 a" B7 p' ], d if (Status == EFI_NOT_READY)
( A" `9 h0 |" s& `6 b# s BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);: F* {% b9 U2 j* Q u
else6 [9 _! J( b. ^6 n
break;3 B6 r+ p+ d5 J5 j! @) C
}/ B4 @" q2 W; ^# a- ^
if (!EFI_ERROR(Status)) {
( P A6 `! K" s7 ?' {. h+ K( W2 U if (key.ScanCode == SCAN_ESC)' U# X8 X. Y5 K4 o
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
' @6 z! g3 L6 X1 Z0 A: U! y }. {: k {3 Q0 G7 x
z: R7 |1 w" b0 k. L
return Status;: _8 E, @, b. R; s" f
}
% x; \0 t" N: H/ o/ w# H. B
0 S$ n2 y. T+ \EFI_STATUS9 {& D( P; ^7 h6 [" Z4 F' v9 H
EFIAPI
' T/ ^( f" t p7 T5 L0 F/ |MyfileioMain (IN EFI_HANDLE ImageHandle,2 h5 n( O4 o! r* G
IN EFI_SYSTEM_TABLE *SystemTable)4 x3 ]0 v4 J$ k. }# ^* j
{6 t/ |7 D) V, m5 Y$ m. _7 f
EFI_STATUS Status;
4 K0 b3 ^: A' u1 r EFI_HANDLE *DestAddr;
) v3 a0 v% i; s9 }5 _/ i- J3 w EFI_LOADED_IMAGE *FileHandle01;8 o; q: ^9 L4 O, J- p0 `
EFI_DEVICE_PATH *FileHandle02; : v! i2 B1 {0 r: D
EFI_FILE_IO_INTERFACE *FileHandle03;# z: l7 `9 P9 s6 S
EFI_FILE *FileHandle04;9 j5 X1 e: Q9 D3 Y0 ?' T m
EFI_FILE *FileHandle05;
4 d0 Z) F3 z& F# x% ~2 f CHAR16 *FileName;/ Z; j8 g3 |8 n
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;; ~9 h; E5 ^- y8 _
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
8 M6 g; N; v) F9 C, R int Bit0, Bit1, Key_Space;
; K# V! C. D H) V: o
, Y' S- n9 X. Y" d FileName = L"NewFile.txt";
- j! T9 J% i2 p* N Space_Key = L" ";( q& n3 B( ]( M
- m& V, T8 K q, ~7 g( f) Z
BufferA = L"ABCD";& y$ Y: A6 V* i& u5 |0 T r$ W8 j
BufferB = L"EFGH";- G, @' R6 V6 b( b
BufferC = L"IJKL";. s1 G% q9 P% S$ S+ E
. Y* f* K; Y, m; g5 _ Bit0 = 0xff;
- o& [! f* f2 S/ V' ?8 E Bit1 = 0xfe;
5 h1 J1 `; T% s$ V3 r9 o
8 H$ e, m. x1 u& [7 [+ U. @ Key_Space = 0x0020;
H8 T0 O$ e' t/ v) Y* _( u 4 K5 s6 p5 W" x
InitializeLib (ImageHandle, SystemTable); * d7 M0 a Q; x2 V
8 ^2 m' A6 o7 E7 l2 Z: ~ DestAddr = AllocatePool (BufferSize);
, N5 s0 C5 M W9 A2 K3 h) P# q- R! @- v% A: H0 o8 h
Print (L"Value of Bit0 is %x\n", Bit0);' e0 {, g. s/ g1 R/ K! ^1 G9 F, ?
Print (L"Value of Bit1 is %x\n", Bit1); 3 p8 u' V, d8 r; ^
5 @) ?9 t" _! T+ S
1 e/ ^$ n3 ~, y Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);1 b/ K& q; _% G& D9 b% c# z
if (EFI_ERROR(Status)) {
4 p% c7 H) t0 m6 C! S# p& o7 [ Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);6 T5 V. ?0 A: l l/ M
return EFI_LOAD_ERROR;+ e( b+ W/ U/ C7 q% Q u# }6 v
}. f$ n' H* c: A* d! y
: s7 w8 Y. q. K0 G' |
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
: M. |+ _# x7 d' d3 P, U( b* @ if (EFI_ERROR(Status)) {
, }/ n! f5 U6 \2 u( O& T Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
5 n' w: q7 X" P return EFI_LOAD_ERROR;- n- P. J1 v! A. F* @
}
$ \3 K+ S1 M' G k
: _" E8 j# Z ~$ {4 i0 r h Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
+ C. B/ f# O4 @" w+ ~5 i9 f) r if (EFI_ERROR(Status)) {+ m# u ^- M! z
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
4 f& a* r w! t8 c/ C& A7 z. z return EFI_LOAD_ERROR;
6 s. l5 ~6 U5 k5 i' a }
* U4 l4 h2 C6 L6 T6 k; e8 r) s
% S; i- i9 V% o/ R* K Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04); f% }; ^( ^- A$ W
if (EFI_ERROR(Status)) {
J, P& g- }3 b) Y Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);# }* G; C0 P8 v, m* N
return EFI_LOAD_ERROR;
# s! d# C: x7 c }
7 h6 _6 m( y) H+ k. m! L3 n 8 w# r/ L: H9 e! w6 E; x0 e7 }
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
3 w" _$ N; r) u if (EFI_ERROR(Status)) {
. F5 w, X& t# o6 m Print (L"Could not open file, FileHandle05 err code is %x\n",Status);0 j: D7 f. b7 s" @# Z
return EFI_LOAD_ERROR;, i: w; Y. e8 L+ `0 e% l8 e
}
9 x% o0 g0 a T/ N 5 o& L5 e+ ?; o
Status = FileHandle05->SetPosition(FileHandle05, 0); - o( m5 H6 _3 C7 ]7 c& m4 @& {
if (EFI_ERROR(Status)) {+ q8 j+ J+ P% c* L1 Z* s+ m5 m
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' X* f% T: a- a0 c9 _ return EFI_SUCCESS;
+ S3 I$ A+ l: q& `7 s0 }: v5 i, i }
2 u) I: d& T( R1 ?/ f" |* g- L
% W( E; H: g+ J0 W1 @ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
5 ?$ f1 C J% s5 h Print (L"File Buffersize is %x\n\n", BufferSize);
- p. X: r& ?) a* I1 | if (EFI_ERROR(Status)) {. H' h7 n. H8 ?1 e
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ k$ ~$ e% w! |% ^2 P; Q5 i1 i
return EFI_SUCCESS;+ b5 C0 x( l$ l
}
5 n) a i7 r0 H4 F0 G* s6 Z6 w) d% Q+ Z- A; K7 u6 n
Status = FileHandle05->SetPosition(FileHandle05, 1);
( T2 Q9 [1 }3 Z" l( ]7 ~2 r7 r if (EFI_ERROR(Status)) {" r7 @0 I- W4 c" Z" K# V
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( H5 @' J) l5 i3 ~# N1 A/ Z0 Z return EFI_SUCCESS;* f& l4 u+ l/ J6 ~8 a
}3 K5 m o( b2 k# V( E* Y/ ^
0 Q. b. g j: X4 J% Q+ x, \ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
$ N- Q7 g4 z( v' q Print (L"File Buffersize is %x\n\n", BufferSize);) r( g0 q3 S7 U4 F
if (EFI_ERROR(Status)) {% C1 d& O1 b- N ]. E
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 r: y1 h# o% y! Y: L/ ^5 i return EFI_SUCCESS;5 i5 f C1 p2 d
} $ P( ~& G8 M {8 P4 @( G
1 A2 W! |, n% V0 `" d, f4 Z% f
Status = FileHandle05->SetPosition(FileHandle05, 2); & p" X; |1 H- ^0 q$ A6 n( I
if (EFI_ERROR(Status)) {
( D* @: b- _" I$ D8 U Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 d* _6 m0 d8 Z return EFI_SUCCESS;6 k. b& H, Q; Z# S
}1 V$ {: [1 x( |( k
/ D7 {1 D3 K) v' o1 J
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);6 y1 a' P! n' z+ c2 n2 x8 r
Print (L"File Buffersize is %x\n\n", BufferSize);4 @& @, l! e" {; d
if (EFI_ERROR(Status)) {
& H1 F3 A% N3 p) X/ [: o Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);2 E5 F5 i/ }$ N: L$ R2 o/ U
return EFI_SUCCESS;/ s1 d) ~" a7 i0 `
} ! S3 s5 q2 X0 b9 I4 |3 R
: [. D% x* B! r3 l: {2 f9 z//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>" N2 A3 G ?* m+ Q# b
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);3 P' j& J: y( L/ l, e
Status = FileHandle05->SetPosition(FileHandle05, 2);
4 M: n) U6 [: i if (EFI_ERROR(Status)) {5 K$ e% T D' b$ P0 b! g. L/ S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; K S# j0 n3 ?- g1 `+ D, t8 ?
return EFI_SUCCESS;0 Q0 b; |: d2 t" d# [
}! \* A9 M7 \% Y5 v9 g' U
* Y+ M7 a% m$ P) }7 f" c
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
5 `. S/ r3 L: b/ T0 D O if (EFI_ERROR(Status)) {7 Q8 g4 b3 M, a
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);3 R; ~8 [- x* R# h. o: M
return EFI_SUCCESS;' q; E% T$ Z( R( p& e
} 1 M6 d: A( Y) N# G
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);) H( a- U6 T3 Y; r( s: Y; d
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<3 ] q- a6 ?6 A/ X# _8 D+ t
2 Y6 z0 J2 g. h0 W$ a) H
Status = FileHandle05->SetPosition(FileHandle05, 10);
8 T0 f" C& Y1 l, j if (EFI_ERROR(Status)) {
: j) ~9 m' v' L! _8 `0 ~ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; I6 E5 T% j0 x/ i1 B return EFI_SUCCESS;
- S! `9 _* @0 `8 q2 ] }
; x0 H2 V( }$ U
5 x7 L! I: @2 u! j Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
! [# Y, m+ t7 v9 B0 D if (EFI_ERROR(Status)) {
9 B6 J: d3 S* Z, }* l* a Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 J; C5 G! w; E1 R3 a
return EFI_SUCCESS;$ Q" E6 O4 [0 o) d' \
}
9 H9 v8 c, ?. c- c
: {. q& r X4 j Status = FileHandle05->SetPosition(FileHandle05, 12); 7 G, {( t' X* O( d( b9 ]- J$ m
if (EFI_ERROR(Status)) {8 p1 |4 A) Q! u' y# U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
p. W+ A$ J2 U( j* u return EFI_SUCCESS;
2 R/ ~# v: ~8 Y }
8 |+ w' X! u7 `, g
' \: B7 M7 x i$ m. ]0 [( I Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; U7 R9 V. g. X$ r6 ?# g if (EFI_ERROR(Status)) {" p5 Z7 @2 x# w( w. G+ X
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' l1 |( s9 e& f: j+ g* d4 h9 l return EFI_SUCCESS;
8 l% L6 P! G1 n& O9 N; n) ]- ^ }( ~- T8 G. |$ F- p" C5 n& [
/ j- s9 m, _4 Z p' z
Status = FileHandle05->SetPosition(FileHandle05, 14); / @. I! M% s a" }- D) U0 W
if (EFI_ERROR(Status)) {
, _+ L* ? \/ P2 \. k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 h4 q; z4 K% _# c; O. \ return EFI_SUCCESS;
$ k9 L) l: y* n: t }$ n8 h, G; [# f+ }
% R4 B; B: W- y$ u% g, A Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# Q% I; ]* P9 Y/ o6 @7 t9 u if (EFI_ERROR(Status)) {
6 @& y2 a5 d6 B. ` Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
{5 u6 H4 i) Q6 B( U5 y3 j return EFI_SUCCESS;
( N7 C0 f5 I: g% A- f( z }
1 b9 k: E! G% S. A" j% D; g1 k# R. V1 Q5 N
Status = FileHandle05->SetPosition(FileHandle05, 16); * U; y, Z4 k0 G8 `. M
if (EFI_ERROR(Status)) {
9 c# H. E" l E* o. f Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( t: r9 }+ \: [5 w2 u
return EFI_SUCCESS;5 _0 k- ~0 j; e k; a0 W5 T2 p
}+ U0 x: i' W. O* g
1 O# N0 v1 `- y9 I: O# w5 ?2 m
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; {6 T' y2 P4 _1 A if (EFI_ERROR(Status)) {
: K7 S; w I& d; `! P0 s Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" M" L& h- y' {: a# O. e7 l: J' z6 l return EFI_SUCCESS;7 W0 e% p7 |+ K7 W1 Y$ L6 _
}
4 p- Z3 ]; C& j. L! C7 Z5 x# Q
/ j8 u8 t$ |/ _$ u. f7 T( P$ D//---------------------------------------------------------------------------------------------------------------------------------------------------------------
0 g6 n4 I2 E4 u; ]6 ?. @5 d2 ^" S
Status = FileHandle05->SetPosition(FileHandle05, 18); * g4 j) F1 J. t# [. \+ b6 q
if (EFI_ERROR(Status)) {* k4 x% g, b# S0 c1 i) _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ h! V' q( _( {& J( p. _3 M, O
return EFI_SUCCESS;$ H6 V" @8 X) e" l. I& ^
}8 a- g2 r& E; ]& Y. i* d
+ C2 b' Z4 N4 ^ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);5 ^7 {% j! b' s; u0 P& a% z2 X; X
if (EFI_ERROR(Status)) {
9 {7 Y5 z- r# a1 u. R# b8 q Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; ]' n3 k) i! U1 G a' O( O9 L
return EFI_SUCCESS;* i) X, C: U) |( V) B- v' d0 ^
}
" T3 J* C+ b6 Q& x* q7 W8 D2 I" W
9 C& ^1 r$ ]. D( u3 O FreePool(DestAddr); 3 \4 @9 L; b" H9 x7 K9 ?4 H
& ?& b5 A* J6 h' R( K
Status = FileHandle05->Close(FileHandle05);
3 t c9 T9 W5 V: n1 M% l if (EFI_ERROR(Status)) {7 b8 F, s( ~" ^( f' p
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);( g* ^# ?! C, s7 D2 j
return EFI_SUCCESS;
9 r7 {1 a3 n' |; d0 z3 B }2 f% i: \7 @ X# C. m2 B
" c% j8 g4 q: h2 B! }. M- O Print(L"File Name = %s has already been created.\n", FileName); w, F, ?. v3 Q7 X; a2 \
% c* R \6 h: _' F3 k" J Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");, \6 b5 ?4 G$ _
WaitForKeyOrReset();4 Z5 r' e; ?4 F) q: P% E8 \
7 a% j& a% l- A1 z* G
return Status;) c q5 `' p. \2 N6 P$ B
} |
|