|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
3 q% V: ]7 x1 ~* B& N. S% a * myfileio.c
Z6 K2 D" u2 x& R * Apps
+ X' F, {$ k7 }8 o0 q$ L */
n4 [ Z. d+ P9 K" @% k; t. P; e9 h. u, @& }
#include "efi.h"
* N# S' u9 p- N#include "efilib.h"' A: N5 ]' M) W0 Y x
! }' O" e- B4 V4 @
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
2 X# G0 P! m- F& h+ r( x; S( X; N
static EFI_STATUS WaitForKeyOrReset(VOID), a+ G! S# Z$ C7 R5 u' H
{" j' ~/ [( p( ~, T( U, U5 @
EFI_STATUS Status;
* j- x o1 f* o3 R+ s$ h EFI_INPUT_KEY key;" d9 K. M' g6 T0 [
UINTN index;
/ ~0 H9 s3 Z. I! | ) m, m! E! _! Z# k+ t8 D
for(;;) {6 K" `5 c6 M: ?# j9 J- o7 z9 B
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);1 s: M3 m0 |7 w. V& w9 F8 ^
if (Status == EFI_NOT_READY); z C0 ]2 ?0 C7 P
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);2 Y# a6 I, v$ ^! s
else& |) x& ^5 a. |# d/ ]
break;4 s! b) W% s h- w' ]* f
}) S$ d$ P* x; O0 q T F
if (!EFI_ERROR(Status)) {
* `8 p" v) Y0 R1 u8 Q+ f4 D9 R1 j if (key.ScanCode == SCAN_ESC)
}* R6 n0 {7 w% c* R. Z1 x RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);# j" p' E$ H, c" j P
}
# c. C' z+ c8 j4 G0 P. @# Y
( v* f' ^1 c9 u% X, a- `9 c, r return Status;( x5 }8 z- @5 g7 G! k
}; Q' I' k$ `, H9 r6 ^, j
, A1 J: i& b, e! \0 JEFI_STATUS
% }+ Z" J% l) A. |EFIAPI* e- g: @9 ]! F
MyfileioMain (IN EFI_HANDLE ImageHandle,4 F2 `, x1 V8 r* D
IN EFI_SYSTEM_TABLE *SystemTable)
$ A' j1 n. l7 r{
[" w. }6 c h! W EFI_STATUS Status;
& J7 B U4 W* \3 ` EFI_HANDLE *DestAddr; & I, ?6 d% i7 H7 D7 ^
EFI_LOADED_IMAGE *FileHandle01;) x( I) t$ K, o& _
EFI_DEVICE_PATH *FileHandle02; * x/ T1 i! j! |5 l" Y( C F
EFI_FILE_IO_INTERFACE *FileHandle03;, {* D+ D& S0 u, g. C/ v* u
EFI_FILE *FileHandle04;
' y5 Q% x" L4 B0 O EFI_FILE *FileHandle05;
% V$ A2 r. Q: E& A9 a' S CHAR16 *FileName;
7 z0 X. Q5 T% ]( h( a CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;8 O4 x: K* {8 y0 h! n$ d0 P7 u
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
3 T/ T* j8 L! Z: m/ t int Bit0, Bit1, Key_Space; B) Y$ f4 |. Z1 G) ^
8 S' g. V' A+ X; x! x
FileName = L"NewFile.txt"; b! Y3 o. {3 q4 j( P
Space_Key = L" ";: D) J+ M* k# j: X
9 f0 J3 p0 U0 t9 n BufferA = L"ABCD";$ H7 c6 b, K4 e' x0 R
BufferB = L"EFGH";
: [& ]2 j" |2 }: ~$ B/ g; G2 C1 V BufferC = L"IJKL";
' v+ c* S6 i) C ~7 G
2 R8 m2 J4 L) ?" ` Bit0 = 0xff;
! g: ~- W- ?+ E s4 D% u2 r! a* v Bit1 = 0xfe;
$ |9 n* ?0 a" I/ W0 m , ]4 `: {8 I6 w: z( p3 Q. C
Key_Space = 0x0020;
, N8 }- S2 ^4 u9 ]
* B5 d& Y. d9 h5 L) { InitializeLib (ImageHandle, SystemTable); 0 O8 h; q4 m' ], S
/ g8 y; i2 L5 |( Y1 h5 o% E DestAddr = AllocatePool (BufferSize); 1 ^2 _. L! J- n
# T8 [7 I- ~, B+ D9 g3 L' q; Z
Print (L"Value of Bit0 is %x\n", Bit0);
2 Q. j, p/ N/ } [/ r Print (L"Value of Bit1 is %x\n", Bit1);
1 b; C! r. }8 v v
3 O7 D8 s$ d1 G ( B5 W! ?% O* ?4 E0 u
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
, S+ a2 @1 e* v" c q; u7 Y" ^ if (EFI_ERROR(Status)) {6 f% X) f9 [8 [
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
7 {) O2 g( D" u6 S u& Y* s7 ? return EFI_LOAD_ERROR;
3 p% Q, n( p# G4 Q8 R }
( l( Q$ D7 _5 r; _8 F5 L# K) c$ h
9 J+ Z$ d% q P( h8 a, v Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
) w* t4 D8 P) m* X" c if (EFI_ERROR(Status)) {3 K3 `% d8 {; v, R0 E5 _ k
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
. L/ V4 f1 R* T V3 p return EFI_LOAD_ERROR;
" r8 y9 H% H; z" m } ( W# c8 o5 j c5 y; a" d
0 V' C# K% v7 l5 @6 p! j) w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);5 _9 t1 \8 c/ D% A. m6 l0 D
if (EFI_ERROR(Status)) {2 w& _7 k# W; B. @6 J9 x
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);: B; K( K. M/ @5 S1 {0 l
return EFI_LOAD_ERROR;
) @, j( j' m. v3 Q& y ^ }
2 K' m+ s+ }8 s! M& e- N) K' S4 y6 C
/ r+ A. }& B4 q* X* z8 v Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
2 }8 D# r( y5 Y/ T0 \/ u; z! V) c$ m if (EFI_ERROR(Status)) {. e M3 ]# o- v' p0 I1 A% e5 C
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
1 {7 ]6 O) s8 B5 D. U/ c return EFI_LOAD_ERROR;
9 O: D% ]2 J/ R# j0 a1 q }
' e/ b7 o! {) g' i3 u
* b, C* ]8 E6 \! Z Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);* \8 Y" R2 Y3 Q; V" \
if (EFI_ERROR(Status)) {
" L* N! ~% b8 C# j3 Z( Y# t7 R Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
1 n5 u% `3 i+ \4 V4 }. D/ D return EFI_LOAD_ERROR;
3 c6 r8 `- ~% a* u0 M q( V }
" c+ f: z; V! h1 W! {, t8 p; f% O 3 w R+ f) b, t5 e
Status = FileHandle05->SetPosition(FileHandle05, 0); * ~8 g; D+ \- c* q7 \
if (EFI_ERROR(Status)) {
8 J1 M$ J8 |. l Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ A W1 N* Y4 q
return EFI_SUCCESS;
- K) ]# T9 B X% u1 C }$ _4 C7 G6 | J
3 i2 j3 h. L$ ^: M2 U
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);; i$ d* I0 [6 h6 U
Print (L"File Buffersize is %x\n\n", BufferSize);
6 b$ e0 y$ z. c if (EFI_ERROR(Status)) { a& u8 ~8 M4 c- o0 R0 U0 _- [- L2 K
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
6 \8 h1 }6 @# Y* o' h2 p# g return EFI_SUCCESS;
7 F9 E+ q1 p) t } . G" H' A5 k- T$ n3 h. D8 J
6 z& O2 X3 a4 c+ f& j- ]* Q" ~
Status = FileHandle05->SetPosition(FileHandle05, 1);
g, \- T& n0 x if (EFI_ERROR(Status)) {; K4 C1 N$ r. }/ W! k( _+ n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' H$ M' D2 m! g- A8 m4 O u
return EFI_SUCCESS;
0 f" E0 w/ h# U2 ` }
/ D: g- J6 F, Y& u; E 8 c6 ~8 w4 v+ ^3 [+ ?
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);( U; v$ E5 u% w5 t% F
Print (L"File Buffersize is %x\n\n", BufferSize);
; B5 v" L3 A. ~- X if (EFI_ERROR(Status)) {
0 d4 a/ `* v6 Y/ j' B Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);. ]9 Y* S$ {. L- U5 X$ F
return EFI_SUCCESS;
% l5 L4 r4 o: i' ]# }4 S i. c }
) b+ n& G8 ?( d, P) \9 E4 g 8 _% @/ Q4 \1 g4 n) [7 b; B
Status = FileHandle05->SetPosition(FileHandle05, 2);
8 k7 X& g$ C5 E6 {2 K! H: F+ v8 D, x if (EFI_ERROR(Status)) { T7 r( T5 _* L; n6 I' }4 M
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ A% k$ a" m, O) E& U: L6 w
return EFI_SUCCESS;
/ l U Z- U1 ^2 {; w5 s$ l1 ~ }
# _/ D9 {; C/ P7 h; e: w : U% ^+ t5 v v6 m2 ~% U; i0 t+ D6 q
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
1 F$ ~( r6 t2 Z* F9 l Print (L"File Buffersize is %x\n\n", BufferSize);2 }8 I9 k) [ ^# M- K+ Z
if (EFI_ERROR(Status)) {; ]- L0 R: y! t+ u1 L
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);* ?7 u6 |. t: ]; M9 a0 Y' ]( }
return EFI_SUCCESS;6 e0 ?: H" Y, k2 [8 n
} 4 t9 U. z3 i4 }; p6 G% L
& e7 N3 d! }: Q" Q' W9 c8 t1 `//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
4 w/ t7 f, p" |: w( h5 p Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB); ]8 _) ]8 K: w
Status = FileHandle05->SetPosition(FileHandle05, 2); 6 ^ z9 \* H4 \: k/ w* v4 _& {4 `
if (EFI_ERROR(Status)) {
+ _4 |+ { U7 s0 z2 s Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* Z7 B( w6 m2 A7 _% E; |
return EFI_SUCCESS;2 ]4 [% |6 R. r4 d! \- C3 }2 y+ Z
}% c* A3 s4 `: H
5 K- J+ O' }( m6 G; A Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);, b3 _; w/ s) ]2 t1 }6 [9 L* X
if (EFI_ERROR(Status)) { o2 V# L$ w' n
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
# C5 I% S' i2 l: @, a f* s return EFI_SUCCESS;9 d* m3 |( a" m6 r! V1 i0 T
}
0 ]3 E+ H' `9 w" @1 }/ q. o Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
# z. m9 P. _. E//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<! B8 w+ [; j& D( A9 j
4 v$ ^; @( @- ?# J Status = FileHandle05->SetPosition(FileHandle05, 10); 6 F+ ?8 n" D5 h4 b
if (EFI_ERROR(Status)) {6 A/ X ?/ @8 @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( S$ I% r) ^' b, a0 D A return EFI_SUCCESS;4 v$ l' N% U1 d v% n
}
. {) |# e( U& \# A2 m7 P- I8 R* b: @
% ?! p' A! ?) S+ l% {2 T( y9 ` Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);7 m% t+ ^2 C& B' e1 l+ q6 T, D
if (EFI_ERROR(Status)) {
5 Q" E: J; y$ q5 \/ C5 b Print (L"Could not write file, FileHandle05 err code is %x\n",Status);, |" S0 Y5 B# D2 Q1 q" M
return EFI_SUCCESS;
* N1 Y+ k% `& C }
7 T7 r- n. a. p b5 M( v8 I* G" }3 P6 a
Status = FileHandle05->SetPosition(FileHandle05, 12); " U& |/ o1 Y: q
if (EFI_ERROR(Status)) {
1 v$ f1 j8 g) @) u9 f& c5 \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ M2 V1 w: T1 }+ H$ W return EFI_SUCCESS; t1 P# a! a* f* k
}/ s& k" N2 ~8 U8 k$ {7 Y& V
, ?3 v' Y1 v) m* ~. I. i, ]
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* E- v% m+ _ A' [ if (EFI_ERROR(Status)) {" A, t% M n3 K% ~, C5 x; u
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ A D$ h7 s$ n$ z return EFI_SUCCESS;
. r+ q( t/ L$ |) c% ~+ M3 @ }8 y; m) V4 e3 X0 B+ C
, P6 O) q2 M. |( F: J d Status = FileHandle05->SetPosition(FileHandle05, 14);
+ s! k l& z/ i' k" o* K if (EFI_ERROR(Status)) {
0 |# S# A( c( `0 D+ |8 y( Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 Z7 ~( j! G; h6 J/ }) N return EFI_SUCCESS;5 @1 F" K: P% X0 v- s4 B- n& Y. T
}' i* ^5 E4 ~4 i/ [& Z7 e1 |
1 ^. x' L: y3 G0 E/ [: o) u
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
! q& h8 E/ K% ^7 } if (EFI_ERROR(Status)) {
* ?* X: o& ]1 _" ^) s1 ] Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% O/ N7 N$ l. {4 n$ O0 I X# _* ^
return EFI_SUCCESS;
4 _$ R2 @/ c" T" i! k1 E2 f }
# z) V" k! n8 a$ K: f8 R! |* g6 x
Status = FileHandle05->SetPosition(FileHandle05, 16); ! R" e' H$ e3 y0 W- P" n
if (EFI_ERROR(Status)) {0 g* T" Z5 i" ^. w0 C$ ~. L
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# V& n' R" Q4 Z
return EFI_SUCCESS;- }" }) e3 G9 M! s: i! Q
}
: ~( J3 O5 C& L0 x) H! \3 z ' c+ I, t! d# Y- R) \2 ~5 L& m
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);1 I/ {1 C4 p, ]0 M" T$ B3 o. Y
if (EFI_ERROR(Status)) {
5 w$ `( H6 z* ^5 j: C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: }6 p. U4 _" x7 R1 g! m5 c return EFI_SUCCESS;) Q. v$ A0 D1 p; C! }
}
2 @9 z1 d1 ~* p& R0 g0 q* ^+ x& ?$ g) T; Z
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
. U8 y0 L: H+ L5 q1 j F1 i1 p" j; [! C5 O, m- K2 d. U
Status = FileHandle05->SetPosition(FileHandle05, 18);
0 P L9 R0 x4 _* \3 b if (EFI_ERROR(Status)) {
2 k! Y; k* _2 y6 O L# | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 v2 n- }# G9 _* Q( x! K0 }& G
return EFI_SUCCESS;
: P0 T# \* }9 W$ {- \; ?& Z* [ }( l o/ s5 y1 a$ c2 l% o
+ c) a: A4 w' R. a& W Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
* d9 E; v% A h8 z* B& W+ S) F if (EFI_ERROR(Status)) {) t! l) b) I) d6 i
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
P& J' D0 o1 c) Y return EFI_SUCCESS;
+ n. Q S8 B/ I+ E" ^ }
0 P5 L# Y$ i; v! Q( U
7 ^) V. {( `4 k FreePool(DestAddr); 1 h; V4 K E, @& ?( z2 ]" L
- d) B6 F Y$ l3 H Status = FileHandle05->Close(FileHandle05);
' {& x, x7 V1 H: X3 \( ^ if (EFI_ERROR(Status)) {
5 H; p# a: l1 E Print (L"Could not close file, FileHandle05 err code is %x\n",Status);* X1 o0 z7 y2 c9 ?$ o! Y
return EFI_SUCCESS;
; H. i7 m( B& I( J }6 ~* P. C5 K3 g7 s0 k; ]
% t @6 M: d# {. E0 P" }* I Print(L"File Name = %s has already been created.\n", FileName);
% z, P$ ?* o2 \. i
) t4 {0 b, J7 L0 D6 H Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
0 ~3 i# b) ~7 j5 P7 G6 |& f" }- S WaitForKeyOrReset();: P; N- z" S5 b, v8 g& H# v- E
" V& p. l8 ]# H; {
return Status;: @: `& {# d- w
} |
|