|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*: N3 L" F- o& c( N. A6 b, ?% `
* myfileio.c
& U; S) y# w) P& P. d * Apps$ M) m+ _) y# g: ?& t% ?( Y
*/
0 @3 b+ r1 i+ q3 m% N7 ]: u, W# o0 c! F4 k8 }9 c1 b( h
#include "efi.h"
( g1 }. ^5 a1 Z5 C5 ~#include "efilib.h"
$ r. C7 s- F+ {3 r4 j/ q( i
+ i, y; P8 G6 W+ q+ k#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
4 X- O4 L, t* Z- G' ~" K& T- r2 ?6 e0 y0 o+ ? n o
static EFI_STATUS WaitForKeyOrReset(VOID)! G5 O( u) d8 z- e" Z+ k
{
/ x$ J0 U) q3 E EFI_STATUS Status;# k! ^8 Z: A4 ^* [8 _: m5 y5 s- B
EFI_INPUT_KEY key;
4 h' L# L( K4 `, \ j" E& d( P UINTN index;0 u3 C1 E( H- s. V9 ?
! v d/ _) h1 O `0 }
for(;;) {
" J# Q8 [- F Y, b- M Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
6 C: R2 h/ I. m2 G+ h, l! E if (Status == EFI_NOT_READY)
9 s( V; [- k- p: L6 Z0 H BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);/ X/ o: I a) d+ b6 r- X6 }
else
4 a, D- p! S! t. C break;2 h- J l# N: q9 o
}
& `5 L1 T8 t% S0 d; l if (!EFI_ERROR(Status)) {
$ y _- W: Z% `" Z% |) w if (key.ScanCode == SCAN_ESC): W$ z( h8 d; c3 A1 X1 Q6 Z7 l# J- b
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
" A V% Z3 N/ I) D! { }
% i( F% t0 x6 D) l/ N- r0 T! q 2 l9 Z j/ \4 {. t- {( y% Y
return Status;: A" z9 [- ~. K. k* }% M
}, e" S7 j) e( }/ z5 s1 @ l! [6 T
' n, Q* z( B' X+ s+ L* E, f
EFI_STATUS6 h0 g" A- o+ [1 |0 g" ~9 _/ m
EFIAPI7 b$ r; {, |# _/ I
MyfileioMain (IN EFI_HANDLE ImageHandle,! b8 H" V, c* l
IN EFI_SYSTEM_TABLE *SystemTable)
4 D% W4 i5 i. p: ^% t. z5 d{
, i! g6 f# A. S3 y: E. Z EFI_STATUS Status;
3 i: N. s. H- L EFI_HANDLE *DestAddr;
$ ? `/ D( t) C" X EFI_LOADED_IMAGE *FileHandle01;! B5 [1 ~* Y/ j! C3 ^
EFI_DEVICE_PATH *FileHandle02;
7 D- E+ C9 M+ l" w EFI_FILE_IO_INTERFACE *FileHandle03;% V$ \3 F0 u0 E/ I& }
EFI_FILE *FileHandle04;2 B4 u8 j; _4 C
EFI_FILE *FileHandle05;: I. v4 T* i. e- W- Y7 Y( f
CHAR16 *FileName; @# q# L# I0 A0 U, u
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
3 I, U+ a5 T5 t4 |: S- o7 W UINTN BufferSize = 8, BufferSizeKeySpace = 2;+ u' I e6 t- ^9 l. _0 v
int Bit0, Bit1, Key_Space; 4 \' H. U+ w+ n
1 a# ~# y+ w u/ l FileName = L"NewFile.txt";& |& [6 N2 a- a+ x K
Space_Key = L" ";
% _0 q: M7 c& V
& Y, g1 b5 ?9 C BufferA = L"ABCD";6 S [; J, S3 @
BufferB = L"EFGH";+ z( S+ P* k/ F4 m3 ?8 Z3 z8 M0 l
BufferC = L"IJKL";! S( c' o" h: [7 v# n L3 {% G& Y/ y
: ~$ R' T4 J- N' `
Bit0 = 0xff;
- i J9 D6 A. _+ C) ] Bit1 = 0xfe;" b! J. a" B6 a6 X2 ^# |# q
2 Q) I* l3 r3 M5 o+ p6 w
Key_Space = 0x0020;
8 |1 u; j: M7 C+ M8 ]2 ]
- J! K5 ~* l5 ]# b0 S% S9 t) |+ _% T InitializeLib (ImageHandle, SystemTable); 4 s# R. [. @ f3 |& D6 x/ D+ U
4 ~" {+ z5 X; M
DestAddr = AllocatePool (BufferSize); 0 a7 t( n% a! k D( M' S
( O& @: x6 y( L' J% L" e
Print (L"Value of Bit0 is %x\n", Bit0);3 E) Z# d1 a; m6 i7 s, _5 _1 r1 G
Print (L"Value of Bit1 is %x\n", Bit1);
& P: W1 ^; @+ P2 y8 D* A* J) R1 S7 e! S( Q
) n' U9 ~5 N5 c: f Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
$ I7 v' n8 {6 [# F, @ if (EFI_ERROR(Status)) {
& z; |4 g$ y3 @. p Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);( D/ x* Y b' n! l5 u9 A& o
return EFI_LOAD_ERROR;% H' A' R* G$ }# T z0 ~' J
}$ r% h3 T1 B) }
# \7 F* Q1 I& \# A1 E8 s; W6 o! {
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
% p2 d/ }: N. m/ _ if (EFI_ERROR(Status)) {. p: _7 d2 D9 T; m* W- H' {2 M3 C B
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);! {& w) c) q2 l
return EFI_LOAD_ERROR;
! M# I/ q1 P7 A1 S }
7 r3 M6 b. E; ^1 M3 G + i q' j7 U8 a) {
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03); b9 e/ y( ^& g% v5 o
if (EFI_ERROR(Status)) {
& { ]6 u) E N$ T3 F0 c Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
. ~. |& D A P ^( u8 w! _6 \ return EFI_LOAD_ERROR;: z6 M% T6 T& d% d- x3 F; g7 ~( p, h
}+ ~' q9 z1 _" H; S) L: F' _
7 X5 R2 S. P: d r Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);5 z4 v6 b5 L s5 i9 V
if (EFI_ERROR(Status)) {# L# R; L. C1 ]$ {! Q3 s: H+ O& S
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
% h) n) y$ o# e0 S0 X. I return EFI_LOAD_ERROR;, _6 j& H" m; J% o
}
' w8 b- S$ ^6 s- D. r4 @' Q / b5 b8 i) y0 b, D- J
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
- D0 J5 S5 c( T! e" a# ? if (EFI_ERROR(Status)) {9 U) D, d9 H( d1 A2 @' |; d
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
u* w3 J! d6 K, Y6 l v. u) G return EFI_LOAD_ERROR; B$ K: B; z4 q; B: X& { x
}3 Z7 q% v+ h% Y
9 k( _& p0 W" w Status = FileHandle05->SetPosition(FileHandle05, 0); ; H& \! a7 n9 H+ r
if (EFI_ERROR(Status)) {! f- h. D' h2 b) N0 I% N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 H( O+ i4 Q5 K
return EFI_SUCCESS;0 }* U' q2 w; _
}
$ v5 {: z; M( `, @9 r1 L, B1 z
, n6 u5 s* y, r2 y Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
7 @& F) X# c1 U2 m# |0 {9 ~ Print (L"File Buffersize is %x\n\n", BufferSize);
' m9 B- g' g% s) |& b, |+ n if (EFI_ERROR(Status)) {
2 I" |5 J) T$ Q; w, Q Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);; d7 H* c$ M% K9 c, H( G: o- c4 E
return EFI_SUCCESS;! d) T6 Z1 w' l7 C0 @
} 6 S; d o% D _8 x4 } X% F
4 K/ V2 S- R0 u. L
Status = FileHandle05->SetPosition(FileHandle05, 1);
) z! e# ^# n2 d+ l& A; L if (EFI_ERROR(Status)) {
, Q: d/ ` B( @3 ^( |, X Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 p8 _1 n4 j) `' ` { return EFI_SUCCESS;1 y4 D. v8 |" @* |! K/ k4 k, [
}% \+ V$ H3 l/ R- A
3 _' A( ^( B* ?1 H9 ]. x
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
/ g1 R0 |$ l) y/ C4 } Print (L"File Buffersize is %x\n\n", BufferSize);4 T. C) j9 f; [) s6 ^
if (EFI_ERROR(Status)) {
% E: K+ B! j9 ?; |$ C Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);# f4 F) u3 u! n+ L, f( x' P
return EFI_SUCCESS;2 }0 o9 k2 j1 J1 K# o5 D
} & Y' E3 j" e4 S4 [
5 k! w$ M! `/ c7 q6 w3 w, a Status = FileHandle05->SetPosition(FileHandle05, 2);
, z& m' C/ _, a" p2 J) @" Z+ c2 ^ if (EFI_ERROR(Status)) {
' f$ C& \1 `$ ]% j( @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 e# ?& a1 Q% v, Y4 c; Z% v* x/ ^ return EFI_SUCCESS;
' _' E6 X* j% K: j# A) [ }
2 V' |' H# ^" r% b0 X! O5 u I; b 0 \1 Y( l5 A+ F' X+ W; }
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
) g3 k4 f/ Q5 n9 G2 l Print (L"File Buffersize is %x\n\n", BufferSize);1 X+ Y1 s7 \$ w9 l% |1 ]
if (EFI_ERROR(Status)) {
0 l0 E7 q. f: Z, \1 ^& D Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);# e6 o! |( \& s5 }$ _
return EFI_SUCCESS;
8 w8 t, ]3 R6 x+ L' P, @# B }
9 v# L. ^- u/ ^& B: B2 ~ p+ {+ z' s) u M; f
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
0 T/ u' e2 {( |1 l! c' Q1 m; p Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
. D0 ^9 ~( |1 c- `: Z9 d) p% K Status = FileHandle05->SetPosition(FileHandle05, 2);
3 K0 u9 d0 g& o5 K# N if (EFI_ERROR(Status)) {
& Z7 ]% v; t, {: ~ K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) Z! {( c+ t; q+ L. K$ O return EFI_SUCCESS;. N0 E# ?8 ^$ S
}
- ^: p0 a4 i, Q `$ J( n% I) C+ I$ R J r" a
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
3 N" {+ k7 ?2 O$ |6 P( _! _8 ? if (EFI_ERROR(Status)) {
$ p- ~& [6 Z% Z1 X' @" ]# v Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
4 \! i9 J- S0 m return EFI_SUCCESS;
' t, X6 K8 C# n' P: R }
8 t) V# O0 y' h; u& B0 x: ?/ R! j Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);/ h, b, a {& _! A) Q& l
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
% m# U) ^: m% F4 x: W% w) o
2 a$ H5 ]8 V7 [' q Status = FileHandle05->SetPosition(FileHandle05, 10);
. M/ q' V! B" Z, z ~' ]" I if (EFI_ERROR(Status)) {
! X. r. b& w# t' y. C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ H4 t0 P( u$ e" K6 j# X
return EFI_SUCCESS;
* i8 c) L" B+ Q }
5 ^4 [7 |5 X$ O( _9 a, o
/ `+ j$ c! _% m! q- a3 a; V Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
" ]* j. _/ F$ a i1 h if (EFI_ERROR(Status)) {
7 u# b! k; l6 h e6 n! Z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 W: j8 X1 {$ [5 {% S [1 A) A
return EFI_SUCCESS;; \) F" y1 A& X2 W; @1 `
}) z: b% b% }" A$ o; e) Z
0 b5 W9 m. z; t
Status = FileHandle05->SetPosition(FileHandle05, 12); ; I, T7 J1 {! Y8 R
if (EFI_ERROR(Status)) {
5 ^& c; M1 d: ?5 X6 B& C7 z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 D& @4 i: V. v* j& Q
return EFI_SUCCESS;
3 C2 u* |7 Z0 m5 W, A" I2 I8 m9 S }
# j4 m" b6 t2 L; c
) ?- m- A0 T, b/ f N; b! F) d y Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);" s' m+ d# n2 R0 Q! j; W/ b1 e5 [
if (EFI_ERROR(Status)) {
- B8 ]# a3 @2 {' j- ] Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- U4 U4 D! @/ e3 U P: ]+ ` return EFI_SUCCESS;
! w; I: F$ t3 ?7 @- B }
$ W4 X. T) {% K8 I3 }* o! ^4 `% f, B3 _8 m
Status = FileHandle05->SetPosition(FileHandle05, 14);
* l: b& H6 X4 \+ {; a( O if (EFI_ERROR(Status)) {1 _# K6 H: T0 w; K. f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 O+ o& u) ^" K: _* k return EFI_SUCCESS;
. H5 Q: W4 E8 J+ I/ m }
( V. x l: p7 a$ A* D
/ a8 o3 }! w" Y* X! E; N/ A Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ X2 ?) ^* L1 e" @5 T6 f* D* O if (EFI_ERROR(Status)) {
, c/ Q3 i* F1 ]: e# e3 P* `, I Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% u( p! b6 E$ R4 T5 b* W
return EFI_SUCCESS;4 x$ o, ~3 t, _
}& D/ f4 R" o# P9 B9 Y/ N7 X
/ |4 X# i6 G7 d# }9 X
Status = FileHandle05->SetPosition(FileHandle05, 16); ! p2 l9 J1 k$ D4 x V4 L0 |
if (EFI_ERROR(Status)) {* r: ?4 G7 S9 _; @* R3 E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' L! P0 V2 R/ j
return EFI_SUCCESS;/ V; @8 B" o7 c8 k! L; {8 G9 P
}1 o" Y3 T9 C4 i( q" a
0 d/ _2 N5 f2 r, C, n
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& I! b. u! W0 `) j if (EFI_ERROR(Status)) {
! D) c% O9 l8 L" d( S3 |: V Print (L"Could not write file, FileHandle05 err code is %x\n",Status);3 O& _: ]) F' O: a! W. M2 s# U
return EFI_SUCCESS;
4 U5 p( Z% M0 t/ ~ } 8 Z+ {. Y/ b2 S' n4 u
! w. b% ]: D8 v* j1 w//---------------------------------------------------------------------------------------------------------------------------------------------------------------
& d1 Z2 _; T, N0 [* R; m+ c
5 D* S% l) L0 q4 c Status = FileHandle05->SetPosition(FileHandle05, 18);
0 m0 V/ q) a. `% `- @6 W) y9 @ if (EFI_ERROR(Status)) {9 M S. {0 v& O6 j
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# W/ f/ J( E1 a% B& D return EFI_SUCCESS;
+ V2 ^( M9 Z; ]3 G) D3 I) Y; ~ }% L% l+ b7 }6 `2 T9 a" N- S
! C% E' Z& o) y. O( w
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);. {, x# f0 ` S6 T0 Z
if (EFI_ERROR(Status)) {
0 F/ ], }& q! ?3 H! ~7 x Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 i# B3 L7 r% ? m* F; A( { return EFI_SUCCESS;" A: ~0 k! j' J0 o
}
: g6 j' ?2 Q) a 5 |5 q# K' l( Y
FreePool(DestAddr); ) P$ w- i+ ~ q ~& Q4 l
2 B6 ~& @; t+ M5 x6 F8 E Status = FileHandle05->Close(FileHandle05);; b) K6 X, M: n
if (EFI_ERROR(Status)) {" a0 |' v9 r2 d1 K4 b9 S+ a
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
" D: |( h4 k; q return EFI_SUCCESS;! P, o: H+ R5 I, G5 T( y! ?+ j3 \
}
# D! y6 h8 U6 i' w$ `& a & {5 { Y7 X$ A/ w& O* o& f+ E: G
Print(L"File Name = %s has already been created.\n", FileName);$ Z3 e2 O8 ^5 }# I( s% O
5 Y' L! n: d& B" A4 ^9 d/ p G6 ^ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
+ p" S2 d/ r5 p! j# T WaitForKeyOrReset();: t, k2 S! f: G, R( ?: K
$ B5 J% f+ B: C return Status;
, r' H1 T; L4 q. k: M} |
|