|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*# P# { w; M8 ?! H# U- Z/ S
* myfileio.c
% q7 {( v5 B' X, w8 [ * Apps& [, Z0 r1 j t0 ^
*/
! @( P# Y! t) ]- j C8 s& G* G& {( h! o$ ^
#include "efi.h"2 H* p9 v, i8 O, Q8 \+ y# O
#include "efilib.h"
: X2 ~( ]; b% A0 T; `+ F+ S$ [( H4 O( p) }
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE0 S( h/ g y) t/ F6 l: n8 \
7 C1 B) `8 C" C& @$ m- V4 M8 {% bstatic EFI_STATUS WaitForKeyOrReset(VOID)
% u; R0 ~% V" `6 U" M{
- m4 V- ]' }1 e4 V' C# v! D/ c' t EFI_STATUS Status;+ d! z) D: o$ e9 c c( G" z
EFI_INPUT_KEY key; W8 D' @ [5 v" U; `) C1 q
UINTN index;$ k/ E) S$ h# V! }- }/ a- t
3 P, p+ V! a5 {* O5 j( q, c% T
for(;;) {
0 s/ l6 x+ d! c& l Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
( V: \# z$ X K) j1 X- y if (Status == EFI_NOT_READY)
0 k7 M# n0 e9 F P$ @, u: q BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
+ d) E4 Z7 x! Y. J6 Z else$ }$ b' ~) i6 L- I! t$ n! s, r
break;, C, y+ g* A0 T% s3 |9 c
}
8 d: O; ~. Z- ?8 O if (!EFI_ERROR(Status)) {
$ h5 z5 x. D& t if (key.ScanCode == SCAN_ESC)$ u; e7 y3 s1 Q5 M
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);+ e# e( F$ S; n) u8 F4 U5 T
}
4 H# b( ?; |% K9 N3 q ( X! R& I. V1 g, M/ `# ?3 G+ ~- a
return Status;
3 P3 E- `% u6 C' D7 y* i9 T! q}9 z7 Y6 K. X% n0 W$ S& r8 z
) m( o; t. Y+ y5 N$ {4 BEFI_STATUS
q z/ Q+ i# M: i( _EFIAPI
, V5 W5 m, L4 MMyfileioMain (IN EFI_HANDLE ImageHandle,
' B7 S& o3 \! D IN EFI_SYSTEM_TABLE *SystemTable)
# A, u9 Z' \8 N" L5 I% w J3 i{
; {2 Y" ]5 j. _/ ~8 k, Q EFI_STATUS Status;9 u# I/ c' C4 ?
EFI_HANDLE *DestAddr;
$ M5 P: Q2 v E1 C! A2 g% T EFI_LOADED_IMAGE *FileHandle01;
$ u$ }1 b i$ H7 C$ s' s EFI_DEVICE_PATH *FileHandle02;
. I* p% i' G& S$ S# i8 z" m EFI_FILE_IO_INTERFACE *FileHandle03;
9 F) x9 T2 K& I' J' k EFI_FILE *FileHandle04;9 a1 B0 z# x; ? R/ N
EFI_FILE *FileHandle05;% s5 O& m/ n# _
CHAR16 *FileName;
( K; g8 z( `0 C: f6 y2 S" Q CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
, e% M$ M) A( H7 ] O4 e UINTN BufferSize = 8, BufferSizeKeySpace = 2;+ l1 }0 `7 d/ _: X2 x! B, @8 i0 r
int Bit0, Bit1, Key_Space; * I. z" q3 F; Y1 ~
5 q' J& l" I2 q, Q5 h0 N FileName = L"NewFile.txt";
[+ k; _5 T; o# m Space_Key = L" ";
0 U: u( i2 e4 u 2 i) T- p3 X# P% w2 g* u0 M
BufferA = L"ABCD";' h: k( n# z% w2 l. n; ~
BufferB = L"EFGH";, @% u6 ~; q7 }) T7 |
BufferC = L"IJKL";
N) O! ^6 s: o/ n6 n O5 Y7 h3 K* J' z% M3 L
Bit0 = 0xff;/ `+ m. a6 S$ D, ~
Bit1 = 0xfe;
# `) u, W; d& W1 K8 T " f; T" t! D' Q; }$ m. b4 ^; g* @
Key_Space = 0x0020;- ?: }% c& x- `; f
G5 i. c8 q, A; E" u3 { InitializeLib (ImageHandle, SystemTable); + [- \' [( `; o$ @0 ~ v. e' n
9 W2 A" m) G9 Q- f: H$ h DestAddr = AllocatePool (BufferSize);
; k2 T) o$ r9 }$ o8 `& M" ^1 N& u/ n$ Z* \" H
Print (L"Value of Bit0 is %x\n", Bit0);
0 L) S; X' _" e9 E: C0 J Print (L"Value of Bit1 is %x\n", Bit1); f6 b' A8 F6 h2 ]" |& S% T3 `$ i" b$ V
% w4 K2 n9 G, a* N" E0 T
6 V7 o) a8 Y) c {1 `: Z Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
. b/ s1 h6 s4 N/ |. t! h& d/ G if (EFI_ERROR(Status)) {
% w/ R6 W4 R- _" ~, \ Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
/ S9 c4 Z7 b) `: @( ^* ? return EFI_LOAD_ERROR;/ a @; \! ]/ N8 Q# N3 ^3 ^, R: T7 M
}
( j! j+ R! T, {/ v8 Y- `# Y6 i4 [) D" O# E
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
- J* S9 L0 o& {; z3 S if (EFI_ERROR(Status)) {' I% s9 G! s; V$ G& Y8 ?
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
. @& N* w( l0 l' a, H1 M% { return EFI_LOAD_ERROR;" v& v; s0 f+ A) U4 d
} ! N- C u2 S" t
: T+ \5 W3 i H' ~ Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);0 v. D* \: R x _; e4 y
if (EFI_ERROR(Status)) { K* x3 N* S0 I7 G% n
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);$ F; Q2 I9 T5 s: p5 _0 V7 i
return EFI_LOAD_ERROR;
. o z3 g3 z# [* N' h }
0 H/ |& Q/ \/ i/ [4 ]8 q, v9 ~6 W& K0 `) u- S3 S7 H' I$ Z
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);" D! M6 q. M8 G! L, z4 Z5 O0 u
if (EFI_ERROR(Status)) {
* \! A( \# A z9 ?2 _8 X$ t6 l Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);0 k! J% z- ^; z( {' g8 S$ n
return EFI_LOAD_ERROR;# A! m+ l# _# \& L# S
}
7 M/ g2 O% |& X+ p; I: ~0 b0 e! x
3 w# U# S: ?$ y( b+ w Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20); o+ S& d* Z2 v1 I+ P8 m7 s5 @
if (EFI_ERROR(Status)) {1 m& y3 l, k9 G6 F1 B2 D, a
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
% X' R' G- w& L7 E$ _0 a# r' @5 X1 \ return EFI_LOAD_ERROR;/ r! |3 ~3 Q; g3 `2 R
}" N! T" D3 c! A3 T+ f8 h
/ @# i: I8 D8 A8 {$ z+ y% E Status = FileHandle05->SetPosition(FileHandle05, 0);
$ a$ |8 d% k3 T0 e. P8 ? if (EFI_ERROR(Status)) { X8 b# R C, n. x
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 O7 v5 Z+ V2 R1 y6 A" C return EFI_SUCCESS;7 ~9 Y$ C; v& R5 w! S& r
}8 V' ^, E' I2 y1 ^/ q" Q
& t A, I: ^. g: D7 v# l# k- `
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);0 X4 o; P3 P) J- v( r* S) [/ O
Print (L"File Buffersize is %x\n\n", BufferSize);( x, c% M& _8 Q% X9 }
if (EFI_ERROR(Status)) {
3 N; v7 w1 g: m: F: H Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 s, Y, ]5 C# c! A( K return EFI_SUCCESS;3 W% J$ D" \( `
} - ~4 ?: B: C; H: t3 J1 @% n& F V; J
5 ?; w k: K( W/ S' _; O: [7 [
Status = FileHandle05->SetPosition(FileHandle05, 1);
- W' F) i, _9 |" o( Z4 p/ E if (EFI_ERROR(Status)) {
% e$ V$ {* f. W ~+ Y5 } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ U4 F" ?1 ], c9 ]( L. z3 n* b9 p return EFI_SUCCESS;
. b' _3 ~) \7 [$ W, {/ m }+ k1 k$ G' R; x: W# {
) h1 \% k) J* E) r5 P
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
5 B6 r' `4 F% |( f7 F! U# _ Print (L"File Buffersize is %x\n\n", BufferSize);2 p- b7 B5 g% s+ u4 D
if (EFI_ERROR(Status)) {
% A" A" z! q/ O( t2 x" A0 P4 Y Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 q. d1 P1 y" ~4 C' F return EFI_SUCCESS;* k. i y6 U4 Q
}
: _" G$ O- x! c; l! ^
8 ^! H( w( }$ ~5 D Status = FileHandle05->SetPosition(FileHandle05, 2);
+ f& ^8 f6 k; Q% @; [ if (EFI_ERROR(Status)) {
+ M5 d* P4 U9 s/ u6 U7 j. f Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. n L8 D8 `+ t8 ]( \+ e: O
return EFI_SUCCESS;/ O% _8 H! O0 l* D b6 R
}( X: d2 C, }: o7 s/ B* B
S, M( v0 d5 B, P" g; O: d2 j Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);! R$ |& V! R6 l( Z3 s9 D
Print (L"File Buffersize is %x\n\n", BufferSize);% ?' y/ I. T0 f' ] @+ [
if (EFI_ERROR(Status)) {
% v1 v. X9 K; p& S2 i( ~ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& W9 ~* N. x( ] return EFI_SUCCESS;
4 [5 S4 L5 X x) L5 I. L" x }
. y- a5 D/ L6 ~' L, `
4 B! i! Y- `" ^& z8 p; F* L//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>># R I4 P% R" y' d9 ~
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);9 b+ K9 j* R% t7 S/ A
Status = FileHandle05->SetPosition(FileHandle05, 2);
& T; O6 T: e% Y/ y2 y1 i if (EFI_ERROR(Status)) {/ M- i. o" @) D, [# ` {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 G2 [8 S8 Q1 }6 H
return EFI_SUCCESS;. D" T9 ?5 ^: F- d
}
) ?1 z, O8 h* S' |* s+ s6 I( V. ]
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);2 ]4 A5 k3 _6 N8 d/ s
if (EFI_ERROR(Status)) {8 x: o5 |4 W+ j; C7 k
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
% R8 y' |; f3 b8 I6 E/ [ return EFI_SUCCESS;
6 ~8 G. J9 L n) }$ A0 y. f }
: b9 r( `# l& V2 d Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
6 w/ T, ]- \( A/ M2 e//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<" F- @7 I$ n& T7 J, C
! |+ v( N" t& b& z( s Status = FileHandle05->SetPosition(FileHandle05, 10); , t. l! P4 n/ C" \' r6 }8 U. [% C
if (EFI_ERROR(Status)) {
& L) n; X. `# L1 F6 N' D Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 X- Y0 `* p+ ]5 Z
return EFI_SUCCESS;
; E2 Z' p, Q3 t" e& J; \5 q }* B6 X- O: ^1 R) o
3 q2 k( X: e6 {; D; ~
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);; z2 |2 i: Q2 G# P- f2 M( c4 W/ e
if (EFI_ERROR(Status)) {
4 Q2 R0 v3 b, E$ p- b5 n: r Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 X) a0 q$ K7 S: ?: \7 ^8 N$ } return EFI_SUCCESS;
' S2 P3 B7 ^( | }7 A3 y. R, _* c) X8 X3 P
: a1 M2 }6 U2 Q; h# K& R Status = FileHandle05->SetPosition(FileHandle05, 12); ! N) Y: `: g2 x5 n5 ^; k' k
if (EFI_ERROR(Status)) {
' q% J7 |1 r) M1 U, c( S0 x3 I( d/ \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; U Y' ]3 t, M7 I7 A: _ return EFI_SUCCESS;
6 {' j- _( Z* \/ Y2 E6 @/ u$ W }
- a7 D- B I6 L( H- }, }8 A# A ' ?, y$ L# x, ?; O0 K
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 ]" \1 k) Q- @- P if (EFI_ERROR(Status)) {6 Y% m/ f; ^* Y8 ]
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);1 A* u6 w# M7 l
return EFI_SUCCESS;
1 o2 Z' o2 Y7 Q; I/ ] }
5 c- v9 H: U: P
8 `% S3 C' ~/ i5 F: n' @ Status = FileHandle05->SetPosition(FileHandle05, 14); h4 d3 T& g1 }/ L; r# L! [: z
if (EFI_ERROR(Status)) {
6 \* A8 B+ k+ b0 q3 k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 |) h7 d- J+ }
return EFI_SUCCESS;; s; y _5 C9 G
}
, {4 r' T( U0 K+ a( y& J
$ g. j8 J3 x" O Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* P; r$ s6 ?( X$ G' J if (EFI_ERROR(Status)) {# v5 i r2 A! ^ v. w
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 B' A @) K0 S' E- d' A1 ]
return EFI_SUCCESS;
# g& a- X; X+ u( h/ h4 U }
( ?1 e* {3 ^ } v P" i6 T# A* h L/ E
Status = FileHandle05->SetPosition(FileHandle05, 16);
0 z! F# V* I, j' R- B! f# T& @+ E if (EFI_ERROR(Status)) {& o8 l; t$ ~3 P; K
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" o6 S* ~. P9 m8 ~) y& }
return EFI_SUCCESS;
7 `6 _" n T% k1 `4 W- l }, m, P2 ]3 |+ k. `
- Q5 w' K3 M, j Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);0 E. p6 o9 N1 Y( B
if (EFI_ERROR(Status)) {
6 o* ]1 w% l. y. s7 j Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ m. V( B X4 K* d return EFI_SUCCESS;
0 ?8 g, x, S' G8 r } ; y/ z+ y" D; k9 I4 x! m
I' T$ \+ ~8 J2 d3 Y( @) x, X% d
//---------------------------------------------------------------------------------------------------------------------------------------------------------------; \8 G& C7 ~3 Q, m" Q3 P) K2 {
% E9 m7 H; p! W" s Status = FileHandle05->SetPosition(FileHandle05, 18); 3 m; l$ ^2 ]4 ]: K4 @7 B
if (EFI_ERROR(Status)) {2 |+ w# e4 `$ f# v. d$ G7 k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( S0 W R3 |3 {. p
return EFI_SUCCESS;
7 R' g0 Z/ m. }0 c& T; y }
( y) K0 Z% ?5 \* B% k( L, V. q- Z
7 ^* j* h2 ~3 @; t$ L Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
7 m0 I: }7 @9 ?) a3 z3 m if (EFI_ERROR(Status)) {
% ? H8 Q/ Q) ` Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! V. x6 F4 _3 w
return EFI_SUCCESS;
. o/ Y Q8 K) V% l5 w } 7 K/ d, U, _1 R
- t( Y3 J2 j! R5 Z2 ?
FreePool(DestAddr); $ B! N# P( ?* [. z. Y
5 l) M [5 `7 G: d( u; P! ~ Status = FileHandle05->Close(FileHandle05);
O4 r$ ?; ~' D( Q7 K5 G if (EFI_ERROR(Status)) {# ~, Q; f6 i5 v
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);2 o t U7 O+ |/ ~9 o8 c
return EFI_SUCCESS;# w3 L+ d8 q* E ]3 A7 ^6 ~
}
8 o( l1 }4 o0 A! l4 `5 k * o" R5 o7 R8 }4 A- a
Print(L"File Name = %s has already been created.\n", FileName);
' |# V6 c R" B/ Q/ {6 t9 ^9 M: S5 t) ^: n) B
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
* @* L; `" g" V2 d8 K" _ WaitForKeyOrReset();% K# T# G2 ]6 |9 x8 B" h. h
; e( N; X% [; E3 I! G, T) G return Status;3 R; Q# |8 O8 v% `" z
} |
|