|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*6 c/ ^0 x$ l7 U+ b. X; s
* myfileio.c6 F7 x* k5 P6 n+ s+ x" T: r
* Apps' ~$ t. v$ m$ A7 x, p5 S+ x0 n
*/
5 R" ~! m* X5 z$ I' o# @7 A
. j9 f! o, }1 o: p# W# L#include "efi.h"
0 Q9 C' G9 v: w+ ]9 O#include "efilib.h" l, }9 G" P- w8 I- |1 p
8 e8 R5 v% P$ ]9 p" K0 i#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE2 Y) L6 i/ `0 c! x
8 @, i& _' b: @8 p6 d$ P% \+ Wstatic EFI_STATUS WaitForKeyOrReset(VOID); ^) v& J( b Q8 k
{0 G2 ]$ s4 e/ U5 R5 M9 O
EFI_STATUS Status;: k' z j' p; b0 c% }, B
EFI_INPUT_KEY key;: H$ `8 h; g3 y- K m( L) k) d
UINTN index;" E, V" `9 d ^
; n# Y) W4 F' y0 I for(;;) {
8 F. t: Z* c2 Y! @ Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
5 q1 [+ H' j0 m+ a$ Q# E if (Status == EFI_NOT_READY)
2 b2 v R; x% q% c, ~ BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
: e% C% u, [: [. `% r' F else
6 ^& \' E# X5 o; y" L. r3 j3 ?% p break;9 X$ |3 \" H2 u
}3 V" C! e5 v2 T* ?
if (!EFI_ERROR(Status)) {
: s: a! X7 T6 l1 I$ L0 ^2 S2 [# l if (key.ScanCode == SCAN_ESC)1 t4 W1 Z# h4 U" `2 X7 _8 u
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);+ Z% l/ S7 K4 {2 L, h( c& ?& O: ~
}
9 r. h6 ~# E9 P0 j# V: \& o
- p! K: G4 d& q: I3 v' b return Status;
* I2 X$ j: B1 d) n5 i# [- x}
$ N( T/ A: G" L5 \7 Q0 \3 \0 D% f, U5 x1 `
EFI_STATUS8 b* I; M, Y: y
EFIAPI" T* G. j% w& ?- F C; ~
MyfileioMain (IN EFI_HANDLE ImageHandle,
' N( i! Q5 o8 j* M; c+ J& z0 Z IN EFI_SYSTEM_TABLE *SystemTable)
: O* R$ L) ? x5 I{
( K( \7 l: I/ @" j EFI_STATUS Status;9 T1 F" ]: E2 R
EFI_HANDLE *DestAddr; 8 r9 S" M4 j& _) A" L
EFI_LOADED_IMAGE *FileHandle01;! U& ]# R( H! d+ O( l# M
EFI_DEVICE_PATH *FileHandle02; 0 t. s: z q$ M" D8 d
EFI_FILE_IO_INTERFACE *FileHandle03;
" _# g" ~$ K+ g9 O9 X# o7 B2 u EFI_FILE *FileHandle04;3 M0 v W$ `2 r" c: \; l
EFI_FILE *FileHandle05;2 n$ }+ c( o l0 g( w0 H: x O; y- U
CHAR16 *FileName;
9 S% [: ?" L4 q4 \5 b CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;5 T4 P$ H) y3 a4 V2 O5 ?
UINTN BufferSize = 8, BufferSizeKeySpace = 2;( d7 g9 O3 p' ~) v# m
int Bit0, Bit1, Key_Space; 8 a% a4 E1 G8 w8 g9 Y
; x4 Y. f4 [$ _/ A
FileName = L"NewFile.txt"; t& L( G- {2 a$ h% }: B0 S! |% ]
Space_Key = L" ";& O2 P9 d' E/ r5 }/ [) d
9 [( p) \8 a; \. }0 o. a) T( G BufferA = L"ABCD";
/ X! ?# K- g# @& ]( z- ]1 i BufferB = L"EFGH";
8 J& x/ |# L- h% J' C7 h BufferC = L"IJKL";* `" T2 L" F* v7 Q( S; k
3 X) w4 C# N; V
Bit0 = 0xff;
2 J* u5 c& Y# | y8 J$ N Bit1 = 0xfe;
& B& z/ A/ k4 w7 L" _ " i" A7 I+ G+ r
Key_Space = 0x0020;8 R6 S7 J. A7 ~6 t, _
8 A; N: d: G t# l InitializeLib (ImageHandle, SystemTable); " U: |7 t- S ~+ b9 l4 n
' W! G% G# C9 I. J! D! H. `' L7 M DestAddr = AllocatePool (BufferSize);
: C* T: B I5 p3 j, o3 k; K6 n
3 S% A5 T; b7 f; _: h Print (L"Value of Bit0 is %x\n", Bit0);! Q$ m" O" q$ G# s/ E' A1 `
Print (L"Value of Bit1 is %x\n", Bit1); + z1 Q+ j6 @0 ?4 I5 N& v' P
2 V, i$ a* O- C: c7 Y
( l( q" `8 R; @/ f( P Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
- N% \! D2 t, B if (EFI_ERROR(Status)) {1 A( F0 F& J! i5 g2 A6 F( M
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);# S9 w! h7 G* O
return EFI_LOAD_ERROR;; B; n3 @" Y6 i5 y1 F" m6 W; v
}: `4 a! V1 o: x* z% H; Z
4 m2 U; j- b Q; {/ z. j# j6 ]& | Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
' P# L$ O2 Z h) |- O$ q e: u if (EFI_ERROR(Status)) {
- m: g# K. F" V. T$ V7 b Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
! [0 K& _2 y4 A! X$ \+ G) V% m/ f return EFI_LOAD_ERROR;
$ K3 V5 f! d% I, E, G } 1 s6 ?$ O$ x' `, x5 t' D. D
1 E& }2 w3 ?# q9 l, K/ b
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);, }, y3 \5 _; q8 D. i
if (EFI_ERROR(Status)) {
2 i. C# f. F- c: P9 A. a Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
* C: w% b& Y! V. H3 g7 S return EFI_LOAD_ERROR;
& V7 N0 _& t; T, { } _! X0 \* M3 s6 P5 V! s
; T0 P. p, g" i3 m+ q
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);# N: l" m2 ^ b7 @* c' |0 Y _
if (EFI_ERROR(Status)) {
) F$ n/ x$ m1 e+ R Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);: E( y9 E5 U" P, \7 g+ b3 k2 n; d
return EFI_LOAD_ERROR;% S7 }/ j8 U, z8 \! e- L
} & l2 C" V* N& `" d5 }; O4 o
% T u) W/ o$ D0 q) U9 T, U Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);3 U" n+ a& |' f$ |1 Q( U, B( b7 P. l3 i
if (EFI_ERROR(Status)) {$ l, b0 C6 m* Q5 ]/ D
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
0 E! ?; V5 A' [. p5 g1 T/ C return EFI_LOAD_ERROR;" v; W: O2 j, ?- X6 w% O; U. I$ b
}$ d% C" a- \, R* ^% U; z+ a
8 ]. ?! Q( ]+ \5 C4 T; j0 j+ w1 w
Status = FileHandle05->SetPosition(FileHandle05, 0); 5 Q, t. _" y1 d+ @
if (EFI_ERROR(Status)) {
. u7 h! w4 ?" c: e Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, N! Q5 _- Z% b return EFI_SUCCESS;8 p, ]7 R; L7 x0 [2 Q; D6 @6 `9 z7 M/ I
}
5 t( R$ R4 r6 R
9 e4 f. o3 s! O0 {' I3 S* l6 j0 [ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);' L0 h' f; m) X; ?3 m3 w( J5 }
Print (L"File Buffersize is %x\n\n", BufferSize);
" f" {* i1 F6 X0 y if (EFI_ERROR(Status)) {1 ]5 O; O L9 m
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
/ u4 E2 A* d+ \/ d. E$ E% T, S return EFI_SUCCESS;
8 B# L" F& d% @6 N } " g2 ]0 _* q: }3 x' Z
% _: n0 e6 t' x# I
Status = FileHandle05->SetPosition(FileHandle05, 1);
' d0 Y. ]4 m) b$ |" e( [6 b if (EFI_ERROR(Status)) {
4 m: q$ w/ c7 |! k( D. ~5 m6 b# h Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 Q! z: I ~ K7 @: v) V* @: a! g: H
return EFI_SUCCESS;7 @5 N4 _: ~) z3 w& `3 c7 Z
}" ]1 t4 E6 i) e
7 E- W. @) z. S5 U- u' p, H0 b Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);/ P5 G* l! T# e0 q7 z3 _9 M
Print (L"File Buffersize is %x\n\n", BufferSize);$ i; i* l2 g0 y1 e+ O) l
if (EFI_ERROR(Status)) {* H1 [" i5 ~& h" [
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
( Z3 m; w4 }9 b, U7 [* F return EFI_SUCCESS;
6 X, n a: ~% [ G1 z- t) A' E I }
7 o" B* W }4 h / _ f9 |% A9 }( ?+ k8 n
Status = FileHandle05->SetPosition(FileHandle05, 2);
0 p: L7 r3 s/ Q% m9 e if (EFI_ERROR(Status)) {
3 _7 N9 I. Y$ G6 ]1 r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 o& Z; C3 f1 s6 } return EFI_SUCCESS;; \! s+ Z. ~. [4 }% |) K! k
}
8 c% m7 N) ]& _0 ` ! c; N$ M: P. }% G0 `, t
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
4 e# h; Y: A7 H8 y Print (L"File Buffersize is %x\n\n", BufferSize);; u/ a8 @, q0 h. l
if (EFI_ERROR(Status)) {
' K7 D& z% e" u4 G1 F& o Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);9 m2 P) h5 n, p) y1 z
return EFI_SUCCESS;/ b+ o: s/ R( q. o9 m
} ) d' c& S" Q% n- B+ Z
# J" s2 f/ _8 n) b7 b( |8 O//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>/ n* v* m1 L& G; C, F& _* `/ q% _
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
: s, b- T8 e* S0 \ Status = FileHandle05->SetPosition(FileHandle05, 2);
* H/ c5 I7 L; N( t% t if (EFI_ERROR(Status)) {
/ L5 C1 }' l$ S5 O# w# N9 n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 O* d4 I+ y# i/ r; b5 |: N. y return EFI_SUCCESS; L* K& d- C1 U W* M. h
}
! T$ a4 E4 N) @: f0 Z/ P" x1 K
8 Y1 R) B, t* u1 u$ Y* E Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
' T# H8 Z; e0 H- Y- j! f, w if (EFI_ERROR(Status)) {
3 d0 i. I9 e. }$ @; b1 O- f- x# S8 N( i Print (L"Could not read file, FileHandle05 err code is %x\n",Status);5 ~6 w3 Y* x7 Y. p; Z
return EFI_SUCCESS;
y" X" ]6 Y' X& G0 X4 L& Z }
1 V0 c6 H* r2 G3 I; s Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
0 b, }* Y Q) i# ^' o ]2 o//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<$ s) |8 a3 |. d5 I
' g: Z9 H5 ? q N0 [0 B4 W. L/ b Status = FileHandle05->SetPosition(FileHandle05, 10);
4 j' L$ O. @$ B9 z/ k, Q if (EFI_ERROR(Status)) {2 S0 @& |- T# j2 q. V. G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 P. I" T" S. H8 q w! C
return EFI_SUCCESS;, E% ^( G0 G( G" d$ v
}
6 {% m' Y8 n1 f" M* G- A! G
# W! C* b" W V' Z( G Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);" u# k. W( [- |5 J, U" D3 C
if (EFI_ERROR(Status)) {1 I( ^% T, f/ H* |5 j
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" f8 h, j+ X% i5 f3 O9 d: P return EFI_SUCCESS;, `2 x- w5 x E5 H- ^7 G& e
}2 o5 h0 C- A! q' c0 Q
& n5 A% Q' \$ t. B: m
Status = FileHandle05->SetPosition(FileHandle05, 12);
4 a: y0 O ~: Q0 k if (EFI_ERROR(Status)) {1 Y: F* ?% J1 P7 W. ^
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ G# [1 {( t* F0 u return EFI_SUCCESS;
5 B6 P5 H" f( e! s9 F' F9 K }$ K. k3 H# Q9 _9 D, R2 V$ U
& L/ E ^" P8 C! j o Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 i- Y7 B0 `( N6 Y if (EFI_ERROR(Status)) {. t3 f! x" l. Y2 p9 c
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* P, M" v* y7 ^. r* @$ w- E
return EFI_SUCCESS;
. s. s2 A3 w) i' F$ {2 P }
4 }2 o0 A7 R, f* P5 [1 Z6 `' x( S' _& l- g, m
Status = FileHandle05->SetPosition(FileHandle05, 14); 8 U! K$ c7 a: |) `5 o
if (EFI_ERROR(Status)) { q/ S4 V" o5 c
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; Q! T7 R8 Y# J* K
return EFI_SUCCESS;; O# n) O" c. d3 E1 H9 S
}/ g. V1 F# K9 J1 u3 [# W% y
; K8 |. H- i+ W7 I% G/ f" V9 p' e
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
7 s0 N8 D& B W4 c2 j/ F. J& Q4 P+ \ if (EFI_ERROR(Status)) {! M. o8 T. L$ o
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; o( |0 W$ I: l) X6 E! d. t" t
return EFI_SUCCESS;
& Y5 |3 ~: L8 J: F }
3 K" u$ W: T9 I2 f6 ~+ q) z/ Z( W9 R) c/ j8 o: g
Status = FileHandle05->SetPosition(FileHandle05, 16); * Y+ j. O% Q1 R% X: [' E3 s
if (EFI_ERROR(Status)) {
7 b% f9 k, |. m& l0 I Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ o X1 i4 s6 U* q) D return EFI_SUCCESS;
0 @7 L2 u C. d# u3 { }
' R2 [4 I0 @# o5 Q. J
- z; @( [( J( G. {) W7 z# S1 C Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
6 v3 {- ?0 l* C& m if (EFI_ERROR(Status)) {
+ V" d" a, B4 B- |) J Print (L"Could not write file, FileHandle05 err code is %x\n",Status);- T; N' r. j J# t7 h
return EFI_SUCCESS;
1 Q7 e3 N) m |5 G2 w# G1 j$ W } 9 w/ q8 y7 O) C3 h; I! s
: }/ X Z) T r$ W//---------------------------------------------------------------------------------------------------------------------------------------------------------------
. }8 j& J. `5 _2 _' t5 H2 T' b: b" X: a( W8 k5 E, O
Status = FileHandle05->SetPosition(FileHandle05, 18);
' z C, a1 q$ G! `2 X8 l if (EFI_ERROR(Status)) {
0 N) L6 Y7 e% \ E Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. u' G5 y/ t" d
return EFI_SUCCESS;
/ x! Y6 b1 @6 g z }( n7 v" A/ y3 v2 }0 y7 P6 E
0 f! I6 k* t" O Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC); ~5 E4 l/ h: ]5 v( r
if (EFI_ERROR(Status)) {% `+ E$ ], }8 `% O S, I7 H* }
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 V Y/ Q! h2 S5 F8 \/ x i! z
return EFI_SUCCESS;" s1 N8 E$ B/ f3 A" d
} 9 b( k, F7 ?4 D( d5 k7 E
& r- x8 y; }" Q! q# w6 N* p FreePool(DestAddr);
" x6 P- k. N( b4 f: d( `6 A+ D' B' J, V0 v' Q( N. r. C
Status = FileHandle05->Close(FileHandle05);( w7 r, r1 s) D5 t( J ^
if (EFI_ERROR(Status)) {
) `' b/ u0 k) u/ | Print (L"Could not close file, FileHandle05 err code is %x\n",Status);8 @' {' k w" E$ r, M4 c) Z1 d3 m- ?
return EFI_SUCCESS;5 v1 V8 B+ a& l8 x2 I# p. i$ c" ?
}" [& E* U5 V2 n* D: V0 C/ k* Q. `! G
: h% N) ]& n% k& N Print(L"File Name = %s has already been created.\n", FileName);& T' B5 g4 D3 c5 Y% G
: g- t& m$ p& }: O1 ~ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");/ O3 o v% j, F! R# s
WaitForKeyOrReset();) a& n% L6 x0 A: q
9 v0 ?' P, h) g" ^2 X5 e1 a a
return Status;
& w1 m. K5 H( v1 f2 ^) U% e} |
|