|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*! H* J. X* E# y; G
* myfileio.c
9 i! S: D0 V& Y, \* v( h * Apps5 Y) b- p+ T& P* s# t$ v8 K
*/- }7 P# o" j5 f$ o5 [+ T
- Q( w8 }9 Y5 Z% Z4 C#include "efi.h"
4 t" H% K: J/ w8 F#include "efilib.h"2 @* n8 w+ J; ~, O
I7 u" n: q1 g2 X( ~* t#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
) [9 q+ [" p! q0 n8 J5 {5 \3 d" s0 q: C0 f: s. u% E7 M$ J% g
static EFI_STATUS WaitForKeyOrReset(VOID)6 _# e6 N. N9 n1 i
{: r% K/ k, H# T- A# ^' X7 _
EFI_STATUS Status;
; j. K0 x! }7 Q7 H EFI_INPUT_KEY key;; U" `* c; \8 o% f9 g" b7 j- L+ q t/ a
UINTN index;5 Q) x7 N b! E! e. Z; W* `! c5 I
* h i3 ]/ a8 P" U) o( M4 [ for(;;) {* P/ p' \5 Q! E7 A& J0 ?: v+ i' y
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
, Y9 ~# i$ ^& \ H if (Status == EFI_NOT_READY); {' ` `: C) p1 x D* d
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);) B% C! x: H* z$ i" N4 y
else
, z, h, }, w( e6 \ break;; S* E/ L9 n6 t5 ^
}
3 r4 U! V3 W& H0 n: O- b# \ if (!EFI_ERROR(Status)) {6 @, @0 D; Q5 u7 d3 F/ v
if (key.ScanCode == SCAN_ESC)
2 l' K0 |6 Z, z; T6 t RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); J) }& ?# A6 t! q, n; G; r
}2 b: j1 e! n6 Z
: w# U+ R+ X3 [4 H return Status;+ @* h0 B$ x$ x9 n! v# i
}
& h/ }0 e0 b1 ]3 _6 J( p! a \
- I9 G" u$ ]1 Z5 `EFI_STATUS- w' g5 B7 \! E$ j7 i; N! P
EFIAPI
; j" S# Q* S, O& L# O6 m5 fMyfileioMain (IN EFI_HANDLE ImageHandle,* S1 Q. ~% j2 [# i( m
IN EFI_SYSTEM_TABLE *SystemTable)9 P4 k4 [# Q3 V# u3 L- y3 }
{
% ^- O3 J* i* B, i9 V, i EFI_STATUS Status;
3 H# l# v9 B, X( N5 O2 `1 p3 S EFI_HANDLE *DestAddr; + g }1 S0 X8 a2 o8 Y
EFI_LOADED_IMAGE *FileHandle01;
3 E; c* v8 q6 P$ d* m5 O+ y I EFI_DEVICE_PATH *FileHandle02; 0 b9 g' C" G* Q Y% Q9 j
EFI_FILE_IO_INTERFACE *FileHandle03;
; g% {* B5 m1 X! v EFI_FILE *FileHandle04;# f9 Z! S% c! v* G9 Y# s* D
EFI_FILE *FileHandle05;- z- N8 ^5 B& p( M
CHAR16 *FileName;2 d$ A- s9 e4 u7 X3 M
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
2 n0 \+ z" ~" r/ w7 V* f UINTN BufferSize = 8, BufferSizeKeySpace = 2;. J) U( ^4 D8 I4 b6 R; |
int Bit0, Bit1, Key_Space; ( G/ C" v7 N; D3 K
) f( e+ ?$ X% c% C FileName = L"NewFile.txt";8 N, \8 c/ f5 {' W, U
Space_Key = L" ";- @4 n f( e. m; R
4 ^: X. j5 {, }' z0 O; Y, X
BufferA = L"ABCD";5 b N4 m9 V/ x( x, k
BufferB = L"EFGH";+ {5 z" k9 n* Q( h" o0 w) k& y
BufferC = L"IJKL";
% d; h$ F+ `) |& y( \4 w/ y
9 M1 \) b8 D9 g- U0 L9 S% z9 g& @ Bit0 = 0xff;
* P2 v4 V% h+ X/ s Bit1 = 0xfe;
0 [# L8 n3 w- v2 k 5 U' }% m0 q' X k& x" H- O
Key_Space = 0x0020;
* h& d2 b5 X& |, d0 o 3 i9 b2 v) p; e, U# o
InitializeLib (ImageHandle, SystemTable);
, P( g6 {5 @. i3 C" R
3 C; t! ^0 i8 ^$ U, ^4 D8 T DestAddr = AllocatePool (BufferSize);
2 n$ D9 Z: U3 X/ l) T; ]) z/ f7 v! _* v# V
Print (L"Value of Bit0 is %x\n", Bit0);
5 [/ C2 \* Q z) u$ ] Print (L"Value of Bit1 is %x\n", Bit1);
% Z8 O- n3 Z0 i F" z: q' w( `5 S/ I/ u Q& @, q+ p
8 t1 z0 G9 g8 x. ]6 C4 v1 P+ M
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
% S# m* @9 @9 j) |$ ^9 @5 b6 t if (EFI_ERROR(Status)) {) r) x N5 s/ ]- r2 ~* D9 A
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);1 ]0 w& |( d& d% @1 f
return EFI_LOAD_ERROR;' v( D5 h, V6 Z
}
: i' \- B" `& K1 n0 H+ w7 M7 h8 m- L" r. v9 ?2 N; B9 p6 V! i
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);+ q# M! n# K! h& ?9 ^0 w
if (EFI_ERROR(Status)) {
6 y s" W- q5 a Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);. U) Y4 b7 _9 r
return EFI_LOAD_ERROR;& Y r$ G1 n; |- b7 F/ S2 U8 |" z
}
( @7 s. f. A. N 9 L- _- G1 e3 ]! w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
) e5 J% i: u! x- A& E( a if (EFI_ERROR(Status)) {
' y" ^! i$ ^/ k/ i) }" {8 H Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);& m$ I- l2 ?' D& J! p/ c4 W) E+ s
return EFI_LOAD_ERROR;
0 e- f6 P, q% ]/ ?( b- h, U }% ~7 n B- F2 d# K7 S& x% c
! r9 I+ V/ u4 d6 }' k/ w Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);3 @* g- d4 N9 B- \0 c7 k; G L
if (EFI_ERROR(Status)) {
4 M" V- _) D6 x# A+ s6 v Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);5 [7 K. d1 ]3 k; \* `
return EFI_LOAD_ERROR;1 Q$ m! ~' A# n- g7 n0 V4 F7 f
}
1 Z x5 q% v! T8 v _
$ s, }0 w Y& [- ` Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
2 w, `' |: `3 l! n8 G if (EFI_ERROR(Status)) {" E3 ]' E/ p- T( g, d [$ @: D
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
6 r' Z* i' R" X; I' o8 T return EFI_LOAD_ERROR;
& B& V% e6 V" g! M) ~0 O$ a }0 q" z5 T8 S. v: |# o: @
4 J" Q9 V- j6 o a( ~
Status = FileHandle05->SetPosition(FileHandle05, 0); # ]6 k' B# D- L: D
if (EFI_ERROR(Status)) {! j+ k" D: o( C8 n g
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ j, N0 R; V% U8 a% _ w return EFI_SUCCESS;0 S- ^& c- I1 p! y
}% j* X! X7 p( }5 g' t
" L1 P4 c3 i4 x/ l Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);. [; v$ p E/ B" q1 w9 G8 w0 R1 H7 N0 W) b
Print (L"File Buffersize is %x\n\n", BufferSize);
) e/ D$ G( p4 S5 L$ h! }% f if (EFI_ERROR(Status)) {
' q4 d" ~$ q/ c Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);$ U4 I# z# \2 k
return EFI_SUCCESS;7 a1 V9 Y; c- c6 K+ F
} ' }/ a1 l7 X: ~ ~2 M! j
; ` A I# N* Y- }) m- q Status = FileHandle05->SetPosition(FileHandle05, 1); 1 \/ N$ O# Z# o8 m0 l; Q$ t' i$ Y0 b
if (EFI_ERROR(Status)) {& |9 C b6 Y7 Q$ F. q. f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 ~2 h6 p7 _5 r9 U+ \9 c
return EFI_SUCCESS;
8 O5 |/ }, J* ^" ^5 {1 f: y }
1 ^# d! q& j3 ^' b 3 n1 [3 X2 \; }9 c
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);* o# s% ~6 i4 s$ y: u2 D$ `
Print (L"File Buffersize is %x\n\n", BufferSize);
/ w/ w% o8 P0 G9 [ if (EFI_ERROR(Status)) {
& U- F- q% p! M Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
, ~3 Q4 Y- q4 n% f9 j) H return EFI_SUCCESS;
6 x5 b0 i2 [, m7 R6 s }
3 Z5 l- T$ e% j
# t7 r6 O. U& k3 ~6 G+ X Status = FileHandle05->SetPosition(FileHandle05, 2); ' |9 |2 U3 X6 {* g* Q
if (EFI_ERROR(Status)) {/ x7 t0 K/ d% u! t: T7 [& [
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" w) A) d, J9 V+ x0 C8 l1 Y return EFI_SUCCESS;% f/ u5 s/ k# P' ~# X8 M+ c
}* N6 @( l ]7 G2 g# M8 K! P5 v
, p" d0 s1 f3 y; ~ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);; \. H9 T. b0 m5 S9 Y
Print (L"File Buffersize is %x\n\n", BufferSize);& O/ F$ E0 i- Q9 ]+ e7 {% X' {
if (EFI_ERROR(Status)) {: m1 t6 z* G& c1 o+ t5 }
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);6 x& p* b" t8 S8 ?
return EFI_SUCCESS;
# k0 k7 S( D! M( | }
5 G$ R9 h; W" `
- w% K# n8 w+ i8 }//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>7 a* q6 R# f; |! {6 a; Y
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);9 o. l( _: c$ P5 o" c' R) I
Status = FileHandle05->SetPosition(FileHandle05, 2); % b# F! { r S; L' W
if (EFI_ERROR(Status)) {
0 M% b0 |. S! C0 v Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 s8 O9 e( i9 s8 Z
return EFI_SUCCESS;* V5 }8 I0 \) i/ D. ]! i
}1 `3 \3 f% k D8 n* j% f. B
8 q6 `* r+ Q3 | Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);7 E. A1 E% U) ^ u$ H% O& P6 \
if (EFI_ERROR(Status)) {4 u7 R: T2 g! A: I
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);$ K1 Q& G- ~. g3 I0 k+ Z1 Q. @
return EFI_SUCCESS;
" L8 V! }+ e' S } ; h& Y5 \1 q9 O( l5 }- J
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);- Z4 W3 Q, P) Y3 I: X2 g
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
2 c5 g. C* m9 [( U) I
. Y1 H) l( P$ G/ T% z. U/ z Status = FileHandle05->SetPosition(FileHandle05, 10); ) A L0 T1 t( y% [. H; G4 L
if (EFI_ERROR(Status)) {
4 E- H3 _2 V( ~2 c. n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; b5 p5 z2 D" @, a# S/ p
return EFI_SUCCESS;! [& E$ a! O4 T) G9 r% h0 h& O
}
# z0 M$ _5 C# d( f8 i: R
9 U6 S# u# N/ j: B Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);5 S5 u% D$ z4 B) w
if (EFI_ERROR(Status)) {3 F* B7 j( S) r5 r
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; `+ n O& n: c& n6 O
return EFI_SUCCESS;: e3 `3 K8 N' {5 y( {0 t
}6 {7 T8 Z: e8 u# J5 A
' W3 ~! @8 A; s1 }( Q Status = FileHandle05->SetPosition(FileHandle05, 12); / _& }; Q" b8 L1 ~, S! B
if (EFI_ERROR(Status)) {
* s- G Z y/ S1 W5 c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
\& \3 x3 B; h. Z" I: L7 h( k return EFI_SUCCESS;3 s g. C- _# g1 J" k7 E
}
2 W. V" t4 o* ?- S* ~: ?6 q* O % `& z3 M G; h5 O* H
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 E8 q6 _) R2 k7 y) M8 r if (EFI_ERROR(Status)) {+ ~5 w, k% N0 p9 u: E' y n7 ?
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: @8 u" F, ?2 y4 x5 z return EFI_SUCCESS;& A; o/ {. I/ _+ x M& o
}: }$ V+ k) r! x* S" R
+ A' s& x3 ` x
Status = FileHandle05->SetPosition(FileHandle05, 14); ' l: M! s: ?1 \' J) h; X
if (EFI_ERROR(Status)) {/ Q4 c' K5 O* ?) A' W' Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) h$ c2 ^+ ~1 _, J3 d9 X2 x
return EFI_SUCCESS;
1 I3 W$ r3 N q) Q$ g& Y }
8 ^9 [ f& o9 h: J* R
, }3 r5 ^/ M+ T& Y8 J8 O8 q Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
+ @8 I2 ^+ Q$ O7 E, Q if (EFI_ERROR(Status)) {8 V$ f) i9 ^+ C0 }2 @/ r, i# h( j
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) ]; m' q8 [& {9 Q1 t
return EFI_SUCCESS;
. x( Q; P$ ?% _& [3 h" [/ X" X( [ }( y! u6 l+ r8 c, f% o" `
$ w& L; H' Z' J9 c9 \0 k! V1 i Status = FileHandle05->SetPosition(FileHandle05, 16); * h) l. G. P& }' h
if (EFI_ERROR(Status)) {# V8 a, @- M" S4 _) u! s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" N5 O% R% m% b( i U' B return EFI_SUCCESS;
+ j, k6 R# o& J" K! { }: X& C& L1 [ f
8 v% b6 U( b/ j5 y( e( p" D* e
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 u2 ?- \" Q3 Q8 K. P6 } if (EFI_ERROR(Status)) {) A! y6 m6 j- ]+ X& K: i
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 O- f% Y' A* y+ }# { return EFI_SUCCESS;. y" x$ X6 F+ [0 |0 I3 P
} ) P% X% v5 o! H& R( c
7 N# l I, S L/ [4 \+ G' f7 I//--------------------------------------------------------------------------------------------------------------------------------------------------------------- U! U+ B% n0 u6 ?5 g, c3 t/ K" c
B* M2 \9 `) S7 O+ I) v/ } Status = FileHandle05->SetPosition(FileHandle05, 18); 5 y; u$ X! d: E+ _
if (EFI_ERROR(Status)) {" b/ B: |) b+ B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); @; z1 Z) Q! t: z
return EFI_SUCCESS;+ j4 l& O$ j) A$ b9 G$ q7 J/ W
}4 l7 b) `; M% ?$ D- e
) G; k k1 c- z4 n: r
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
+ a4 W/ l4 h0 J% o# {3 h/ y0 z if (EFI_ERROR(Status)) {3 L R4 }+ W+ U1 [, K2 M7 e
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
+ S( U( B2 ^( N) R return EFI_SUCCESS;
0 W; F1 D. Q2 G( j4 G& y } & S$ @& H5 E$ G. Y
$ Y, h( l% \4 H R, e
FreePool(DestAddr); / x' A7 C- d, |& p
" H+ q% r- f/ q, d3 s, j }4 n* F; T Status = FileHandle05->Close(FileHandle05);
9 t/ X; M4 V: ^ if (EFI_ERROR(Status)) {
, E+ i( W. ?# ^* o& h# i Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
0 L' C# c/ o# _) W return EFI_SUCCESS;" A3 S3 ~* c' r+ u- k
}: j t7 I" n2 A+ k: T
0 w8 _& X5 I; F, ]% D0 f M Print(L"File Name = %s has already been created.\n", FileName);: x2 S B. Q# _
( G4 x9 H+ o/ L& U" N/ |' r! D
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
0 H: ~4 V8 M5 n$ i$ q WaitForKeyOrReset();
! r/ L! K& k0 q9 F* [ - i4 R% d1 Q6 I
return Status;: [6 ?3 t! U9 M/ @# ]* G5 u7 V
} |
|