|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*6 v' U- n# J; H- Q c: Z L! ~
* myfileio.c
6 \1 e% I+ n" w6 Q& e. y4 j& ?5 B * Apps
( y+ X2 r" }+ m8 R; c* G0 n2 V */
8 D) n: c; F# w8 w" D, r9 k4 X" V. W0 m6 m0 v9 S
#include "efi.h"
' ^; I! _" w# z1 M#include "efilib.h"! u! M3 O# v) t
! Z' H% {+ ?3 X; \3 R#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
. r% S* N% R! {6 \6 h. B$ D- A0 `* ?( |; i3 s0 F1 K9 Z
static EFI_STATUS WaitForKeyOrReset(VOID)
! [" W5 F! u s# c% Z{
- O) S- R! W8 H; ^7 Y EFI_STATUS Status;
4 p/ p1 M4 k% @$ _ EFI_INPUT_KEY key;; H5 P6 s8 k: H: V- L
UINTN index;
1 I( o' ?: N: q% G 7 { K8 x, {7 S I6 a
for(;;) {
8 G0 Z! |; }# x Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
- u, _ ^, x/ x0 C! F if (Status == EFI_NOT_READY)
9 g# h+ o1 b9 S- ^/ u: U BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);0 f1 b! w. D. P7 S% P' _
else3 Q( L0 a `* s, T; y. H. ]6 r
break;
* M/ _' q0 n8 c }
! \1 \, t& H- B! i8 B' v6 z6 w if (!EFI_ERROR(Status)) {4 P* N; u, q' w6 Q- V6 @
if (key.ScanCode == SCAN_ESC)
0 i8 v. s! L- j% ~/ ]# B RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
3 M' W. z6 _! W) ]5 C1 \& \1 p) t3 d }' v5 m# X* }8 G& O8 K$ x; t. T: N
$ l% r# B$ I: F Q2 y2 b# u return Status;
% R9 J2 @7 q4 ^0 T6 Z}& _1 h! ^* P( h
2 \- s5 m& Z n
EFI_STATUS
& ?2 ?" u' F) V! {; O/ gEFIAPI& _7 U; ~- q; q
MyfileioMain (IN EFI_HANDLE ImageHandle,) L! J4 z9 l6 `( C+ V' C
IN EFI_SYSTEM_TABLE *SystemTable): `7 h+ E! }' j( h: F1 T3 V) `) a
{: D, x9 |: M/ ?/ d, \
EFI_STATUS Status;9 N% U3 _ ^' h) W! i" N
EFI_HANDLE *DestAddr;
* i: e. [# @& ?5 X3 y3 S EFI_LOADED_IMAGE *FileHandle01;
2 c. D7 G4 `- W5 P1 ~ EFI_DEVICE_PATH *FileHandle02; 6 X9 g; c3 k4 u4 T; h& ]! H, u: Z
EFI_FILE_IO_INTERFACE *FileHandle03;
/ V8 z$ J9 L! f& [5 p8 a$ P EFI_FILE *FileHandle04;
y; _1 |. j) s, l8 n7 @ EFI_FILE *FileHandle05;, M7 y) S2 `+ x. S$ F# c& t3 I
CHAR16 *FileName;% J* z$ B1 U8 g( D( C
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;1 X7 {$ T( J1 o' G% f# Y! P6 o
UINTN BufferSize = 8, BufferSizeKeySpace = 2;" U ^ W9 V7 |
int Bit0, Bit1, Key_Space;
4 i8 R. K5 g& B2 H# G) A+ I+ u6 n
& m7 t: O- i- J FileName = L"NewFile.txt";9 G9 `. ^9 t0 r% J' Y" e: ^! {
Space_Key = L" ";
8 i0 _! M2 g4 H: [ * y$ n' O; r. y. g1 k
BufferA = L"ABCD";
* e9 o" y( G- T5 R BufferB = L"EFGH";/ H& d! `) |5 Z {+ i# y
BufferC = L"IJKL";+ y8 e, Z! W6 p" N
3 e1 i- g6 ^6 s9 E2 u; J# {3 ^ Bit0 = 0xff;
9 a6 P/ e! S7 \" S Bit1 = 0xfe;. H( L3 W1 f2 _
$ e, \1 J8 d; |+ [
Key_Space = 0x0020;
$ G$ m$ ]+ t! m( s; Q5 I$ F: W
# n% U5 J; M3 \ InitializeLib (ImageHandle, SystemTable); 2 W" N9 S- r3 B
# q J$ \1 b. [0 c+ q: e: c DestAddr = AllocatePool (BufferSize); 5 T- d0 h1 e% F; D' A+ X% ^, H5 A
# x% J( q5 d6 ~8 Q8 e1 `- x0 n Print (L"Value of Bit0 is %x\n", Bit0);
! b2 [7 m' L X3 Q; c+ Z Print (L"Value of Bit1 is %x\n", Bit1); / J! I$ v0 }$ c9 c# j
, Z0 C* Q7 G7 E
2 b, P" ~- N* j( }/ e/ d. G
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);; K5 `- e9 W5 L; `
if (EFI_ERROR(Status)) {( g K& j0 d5 @
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status); g- [ u0 F, e, M# \
return EFI_LOAD_ERROR;
2 t2 V! M T9 i: F, J/ U O# M$ M }
' j$ e" a w6 \$ {/ z4 K/ f% m( b' V6 l5 a9 ]
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);; [0 M8 }4 _/ L9 p* U( \
if (EFI_ERROR(Status)) {
4 O. R7 _# G& U Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);. G {1 g% y m9 B% v+ h: E' U
return EFI_LOAD_ERROR;
' T W) j( @5 X" |3 [4 y: _; J! x } . u7 _* P0 H! a9 E
3 @, J9 ?0 ]% C7 s Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
- H1 t9 O) |5 x0 |7 I- a if (EFI_ERROR(Status)) {
! ^' z, q, }9 p, g Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);9 R* N, l5 m8 l4 ?: Z; o' K" z
return EFI_LOAD_ERROR;/ x- d4 ` _* l; B% [+ v
}2 d6 V/ ?- w7 w' ~2 b" Y* C% U
$ d9 ~1 _' E& `1 D2 Z; Q
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);0 y9 m) e' z4 l1 S/ o
if (EFI_ERROR(Status)) {9 n# r( o, i( f# ^" f: O
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);, E7 a" B8 [4 I' ^
return EFI_LOAD_ERROR;
; w7 c$ _+ C; W5 j8 L }
4 x9 G# @) p/ ~: S! D7 \
7 g, A" O W+ \- k: q% I Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
x5 k8 y& v9 X3 d if (EFI_ERROR(Status)) {
5 X( T1 g) z2 g4 g6 O' a, J Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
7 }; s b$ @" L/ `, v" m return EFI_LOAD_ERROR; H! O3 L" P8 a/ P; p
} t1 w( J/ }2 p8 f6 u' P* a
5 l5 T; ~# z; @) M7 C" j Status = FileHandle05->SetPosition(FileHandle05, 0);
. \6 n: N" ~3 ^9 J8 Y `! a& A if (EFI_ERROR(Status)) {: z+ a( ?! L; j
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 L! F! [9 R$ x1 ]. Z
return EFI_SUCCESS;
5 I u' `9 T2 P+ G& {# i }1 N1 a: M" G8 R3 d' u# j5 m
9 r, |0 s) {) O; {5 h
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);+ A% Y1 s1 C% l+ H* o Y1 s
Print (L"File Buffersize is %x\n\n", BufferSize);
5 J; }( G, f" r5 Y& Q5 ]3 r" O; T if (EFI_ERROR(Status)) {# X# Y8 _4 B1 T& l5 E2 v% ]5 v
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);& ?: i; F @* N, R
return EFI_SUCCESS;
* ^ ^# b3 ]" U2 l. ?6 m } 9 C9 H6 J0 \ Q O2 @9 D
# g% L5 j0 F- \7 w( }# L Status = FileHandle05->SetPosition(FileHandle05, 1); ! w! E0 I. d% Q& u; k7 o9 Q
if (EFI_ERROR(Status)) {
! {. F& r6 b' I1 q3 V5 C$ n* O" o: p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
- h0 N* o5 c6 K* X x& { return EFI_SUCCESS;4 C: k# p. f/ I- x c0 W
}- h) W/ q% @( Y* d
( C6 |& m/ Y3 x+ `4 ?
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);. J# }0 ~0 G! J( H$ G* G
Print (L"File Buffersize is %x\n\n", BufferSize);! T, b6 @; ~/ w1 `
if (EFI_ERROR(Status)) {
1 }) {. m4 H- b! q+ N# L& x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 D( G& C9 R, K7 c% j3 t return EFI_SUCCESS;
: E* t, P1 o1 S9 v4 \: |! V$ |8 j }
3 Z' K) t1 O! _1 i 5 t5 ]/ U/ r3 m
Status = FileHandle05->SetPosition(FileHandle05, 2);
: g$ m+ i* [7 J6 Z: N' B if (EFI_ERROR(Status)) {6 X$ W3 Z' @( N b! z7 V; @, g
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ {2 s5 Q+ Y. j/ c* v, Y {! W return EFI_SUCCESS;7 c- F1 j$ K( ~( a6 a! ]
} }7 R" w' A& o9 r0 g4 O b4 [
: Z& w( A1 g! T4 R: y$ l6 v" c Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
# N2 X+ [5 w8 r# c, }; w Print (L"File Buffersize is %x\n\n", BufferSize);8 {# Q: O5 A. @7 A+ X0 n3 A
if (EFI_ERROR(Status)) {& d5 e* S' q2 ?# O5 u
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 n: ]1 k. P& f$ o" o$ | return EFI_SUCCESS;
5 T4 E3 q. }! L" S" _ } 1 w8 u$ x6 j/ k' k3 b" n
, i" W, f i5 k& z: a9 [//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
) H# w8 _/ \" B6 Q& Z2 u7 k Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
' P3 X4 T! {" v; U# L* J Status = FileHandle05->SetPosition(FileHandle05, 2);
! l8 S1 r/ W: @# N/ q if (EFI_ERROR(Status)) {
8 b: r" Z7 ?% U2 ?- d2 j5 ^4 A Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! a5 W9 t+ U0 C2 E- U& s! Y; O( ^- C return EFI_SUCCESS;: S; h9 c9 `- X0 c
}) ]% N* r R/ {
6 }; J" ]! n+ D) m8 }/ p
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
" I3 f8 Y. h. p if (EFI_ERROR(Status)) {
H% V% {- P' ] z Print (L"Could not read file, FileHandle05 err code is %x\n",Status);% j3 E' E- |+ H# n& I5 w
return EFI_SUCCESS;. p' v) K% B# c2 E; `5 ~6 y7 C) y7 r9 f
} # _1 j2 n9 _% j! W5 Y
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
9 Z$ `1 y# x& d//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<3 L# t9 T8 A! ?6 T# k
, L+ c- A9 f; ?# ^: z5 r2 c4 f Status = FileHandle05->SetPosition(FileHandle05, 10);
7 B3 v% C& f0 b# d! ~# u/ \ if (EFI_ERROR(Status)) {# x, f6 D, E3 c: E+ z/ \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% e6 i5 a- I9 C9 {
return EFI_SUCCESS;
; {5 r; u0 N5 B9 O5 ` }
& F# D: T: u/ O( }; g4 s5 C ) a/ o9 B' N1 Z% I: t* e/ \' G5 t
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
7 q) ]# Q7 g+ H* o& z& e; x if (EFI_ERROR(Status)) {
5 j7 p8 w2 r+ I. j6 [' s' X Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 {/ `1 @! S, t8 P/ ~ a return EFI_SUCCESS;& p7 r7 i$ M5 j, p4 \
}0 \9 W8 ^+ J% A8 ?
1 c- ?, P$ Z. m7 s Status = FileHandle05->SetPosition(FileHandle05, 12); ! H- Z0 R2 M, g- J
if (EFI_ERROR(Status)) {
$ E/ M2 v. }( N) X9 N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! y! K7 Q1 A) B
return EFI_SUCCESS;0 u5 [) e. y8 B5 E# L. h7 e
}1 Z' A0 r7 Y$ m
3 Q! c0 h# V2 i9 _& Q6 `- ~ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; a+ r, P; m$ c if (EFI_ERROR(Status)) {4 I9 D$ j% z+ s3 Y4 B+ C3 N/ V
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);( H# i" Q0 j8 L8 l4 p9 j
return EFI_SUCCESS;
4 n/ F; Y% `& s- T* K }
+ a, y( M- q2 ~. ~7 p# J& G
4 y( z& m; w% m- O+ H) C Status = FileHandle05->SetPosition(FileHandle05, 14);
- K) r) P, Z3 |: i& g D" Y* l if (EFI_ERROR(Status)) {
( @. _% W4 H L/ m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 B* V( c5 i# @$ S ^% Z& K& y) A return EFI_SUCCESS;# v' C( V0 |$ k. ?
}, F( `2 f3 @/ v. ?6 f6 d" A' U* c
( \" a4 t; w( G& D4 \$ U( b2 S$ a* I
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);/ K1 m! l" _9 Z
if (EFI_ERROR(Status)) {+ Z1 C0 `8 K# d2 Z
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);, a7 ~9 H; R6 \3 _9 N$ }6 D
return EFI_SUCCESS;; t7 t7 a/ \. F
}
0 W9 n- |9 S3 V" F9 m/ u$ C
$ Z& |" }: @+ Y x. _/ x1 @ Status = FileHandle05->SetPosition(FileHandle05, 16); ; V* f2 L7 ]% e T5 d* Y# }
if (EFI_ERROR(Status)) {5 X# S, @! [1 X& ~7 V0 z
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ Q# i' x! h4 k# ]3 Z
return EFI_SUCCESS;' T) G- C+ v6 H) [9 B+ R: ?
}
4 t0 i/ ~: v, t' C
8 q' r4 z0 d, X, H Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
+ z n' z8 B, B& ~4 c; | if (EFI_ERROR(Status)) {
" @$ v' Z3 d$ _( O' U5 c, U Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' s+ A# y& q$ s0 }5 o$ r
return EFI_SUCCESS;
9 S' G1 `1 e1 D5 w9 A, ?( j } $ ]& [6 s5 v1 h/ y
4 d% L) ]( p# i/ T% O% V
//---------------------------------------------------------------------------------------------------------------------------------------------------------------" N, D* p) V2 p2 y( `
4 I. y1 [. ?" z. ?( Z
Status = FileHandle05->SetPosition(FileHandle05, 18);
* m0 \) P8 l$ ]; O1 N, F if (EFI_ERROR(Status)) {; [$ Q8 x" D0 _' g& V
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% j/ V% z$ z, T
return EFI_SUCCESS;
1 A9 { R ~# _: c }9 x% v( ?: }+ X* w! ?. A0 i( B
9 C/ C: h" }- s) _4 p- J5 F
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);1 Z2 b9 z0 M: B" x" S+ j$ M, [
if (EFI_ERROR(Status)) {! a) o* v1 I* g5 h
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 @9 }: T/ M6 N2 c. c$ a return EFI_SUCCESS;
c3 H! F" D4 x) r } 5 a4 O+ U9 ^2 h9 Y3 }) k
, n. w3 T' `$ w* x: n% k3 w% A FreePool(DestAddr);
' C+ r2 b& _( V: C1 D- ?1 P
: E+ n3 ?" X+ r: @, D- e Status = FileHandle05->Close(FileHandle05);
7 M! e) o h# |1 { D if (EFI_ERROR(Status)) { V4 p* s& O* R$ B6 C5 V/ z6 Y$ y. V
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
8 h5 \- m3 {5 Q2 O% C return EFI_SUCCESS;0 e( _3 \5 \+ `
}# D7 h+ m' A. D* x
2 @ y* \. m) G9 S+ _# M
Print(L"File Name = %s has already been created.\n", FileName);
, ~7 H$ M s `, p4 Y! U+ y8 e
8 ~+ j& p! \" K& i- K Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");2 t+ D2 |/ T( X' v
WaitForKeyOrReset();
L$ E1 h# a# J, R
9 T1 e2 n2 Z2 x return Status;
5 B% F: a0 j3 H3 {0 f3 ]8 d( n} |
|