|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*8 `( z& E/ Y) @& M. \) N3 Y
* myfileio.c+ Y8 r. q# b1 X$ W+ n
* Apps
5 Q: s+ f+ O, {& Q */
+ |. j: R# _, g0 C" H( d1 k" v+ b e/ w* ~: @
#include "efi.h"
) Y7 B/ s" J3 e* F$ F# @% R. i#include "efilib.h"( u1 w" ?8 @- G- B e
5 I! y; C6 o x8 p; j! C#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE: a( O* v4 o) Y
* g3 ]2 ^6 y4 b2 q% M. Y9 D/ w3 rstatic EFI_STATUS WaitForKeyOrReset(VOID)( y0 u5 G! R% M2 [+ i% W+ u
{
; z8 @8 m0 K R7 M EFI_STATUS Status;
! }9 S1 q! h1 l! @) k) P& R: j0 s EFI_INPUT_KEY key;. o5 ]6 [, F3 W! A
UINTN index;* c2 l4 `! \. T: j. h, W7 K
# ]8 s2 Z6 n4 k M for(;;) {
; W* [ P4 Z# W. d$ w( @3 U Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);0 Z/ F- _% G3 i" o, X" t% F0 B/ v2 d
if (Status == EFI_NOT_READY)) U0 p& b. T8 Q/ M4 h) L. p
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);( a8 h) Q8 ~( ], B) _
else
9 p3 M. D4 R" s3 m, z4 [ break;) z. c% V" j0 H- e% s. T1 P% z$ e
}
; O0 M8 u% x6 a if (!EFI_ERROR(Status)) {9 p6 O5 m2 ~: z$ X
if (key.ScanCode == SCAN_ESC). q( w9 s! Y3 F8 r, S# @! z1 J
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
7 `( C4 i' }4 Z }
& `7 M* E) |3 I; c% \ `0 {7 H
8 W7 u3 @- I3 k2 j8 j7 Q' C5 N0 ^ return Status;% A5 V7 p: \, b- _/ x0 T+ D% S
}% X: L8 H4 y& d4 p5 a+ C
, M, }8 j3 J& V, |0 D/ l% oEFI_STATUS
4 Z' Q1 g* T" n- X8 ^( q0 wEFIAPI
0 S4 w3 x+ T( [! d% N- D3 dMyfileioMain (IN EFI_HANDLE ImageHandle,
7 P- D: E! h0 n IN EFI_SYSTEM_TABLE *SystemTable)0 Y* `' r7 |; v# g- l& ]
{6 S8 ^) u2 g% L3 P2 t0 @1 m
EFI_STATUS Status;' n( H2 T; c3 j( _
EFI_HANDLE *DestAddr; , c7 ^4 y' w8 [1 z& m
EFI_LOADED_IMAGE *FileHandle01;, i- q- T4 X% b7 A* y$ ]+ U( b( _
EFI_DEVICE_PATH *FileHandle02; " w c- j! M4 X9 L5 Y! _
EFI_FILE_IO_INTERFACE *FileHandle03;+ b1 ^/ C5 }+ f" ?- M4 x
EFI_FILE *FileHandle04;& r1 {3 x# U' T4 h4 E
EFI_FILE *FileHandle05;
1 x0 s3 l6 g; s* D1 r9 ?2 j CHAR16 *FileName;$ A7 B# o- Y/ ]8 u: R! C H
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;! b! h* Q& C' N4 S, ~) u) k
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
: P8 [3 A3 e% I. K9 k. p- h int Bit0, Bit1, Key_Space;
& C, P- ^; J5 ]; Y7 V) T
; g9 \& p* B' w" { FileName = L"NewFile.txt";2 j( |/ T; T |! W* ~* O2 H
Space_Key = L" ";
0 V& ^! V6 C: S/ F6 H8 Q; F
& O/ `' X) T% C& [, _) M5 `2 m2 r BufferA = L"ABCD";' f. B9 s+ a5 `' n. j1 u' z T
BufferB = L"EFGH";6 }# P6 l$ r$ h! q$ K5 B$ Q
BufferC = L"IJKL";
! A+ I9 c" d: N, z: l' }* k B6 [2 ^
Bit0 = 0xff;
Q8 `0 N; y o# O7 D# j3 z Bit1 = 0xfe;: L. z! H/ S+ {% F! e! H- q# D5 \
9 F; ~6 H9 y/ h+ g. N- v Key_Space = 0x0020;% K8 x5 Z* ^4 F( C* `4 {5 _
1 r7 S$ l" e: N# ^9 e' o
InitializeLib (ImageHandle, SystemTable);
; H i$ h8 m2 P; [' z$ i+ z% n: V3 l5 z# s, P2 Z2 D2 c% G5 e5 ~% D& e
DestAddr = AllocatePool (BufferSize);
% ^& F1 P% F' D' j2 {: ^1 |) n: q! `' f5 Y1 z. ^
Print (L"Value of Bit0 is %x\n", Bit0);; h: ~/ K2 A: j" `9 f0 i
Print (L"Value of Bit1 is %x\n", Bit1); 5 w f* F& e8 b6 f- p
" h& h7 T1 l! w! L, @0 H
* \2 W& F* Q8 W2 I. T" _* @1 |
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
( U0 z3 U1 F: I if (EFI_ERROR(Status)) {
( O- o3 z+ ^6 Y5 ^5 b# }4 I/ R Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);) W2 g4 u h- F
return EFI_LOAD_ERROR;+ v2 y9 v; P; V6 ?' [5 }. n
}
, A6 {- G8 y+ z, r( Q( J6 m* |0 d
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);! A) [) H, p! f3 O
if (EFI_ERROR(Status)) {2 U) l2 r( g1 Y2 Q0 G) Y
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
9 G2 Y: G% O8 A4 w" X+ K return EFI_LOAD_ERROR;2 Q! S5 c& J- s" l
} 2 x, I3 W' h- Y) t& E
! N9 ~, [5 o7 p) a. v
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
" n) w9 S& Y3 E! N" Z) [ if (EFI_ERROR(Status)) {& b! {2 ?8 E( g% L- |
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);* ?3 z/ u) i- ^' Y: e4 i U4 j
return EFI_LOAD_ERROR;( [0 [' w f* T; n
}
! Z8 E$ c( f M! |8 K b9 b
8 K- \# R7 \! Q9 U/ y; } Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
9 {( e# v/ [4 |- k( X$ U( Y/ l if (EFI_ERROR(Status)) {
" K$ G" W! O" M. I/ [ Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);& N. m8 v( r% e, r& [
return EFI_LOAD_ERROR;+ e7 C3 k$ K! h$ b! q4 w3 L% z
} ( f# O1 b. o3 q2 i
. [* @1 H% d7 | Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
% M [9 O; F2 ~ if (EFI_ERROR(Status)) {
2 R' y7 g: @+ p$ N& f% n8 ~" Y* d' m Print (L"Could not open file, FileHandle05 err code is %x\n",Status);. |" j1 p7 q5 g
return EFI_LOAD_ERROR;
& I/ A, H5 I% t1 U9 b }
" Z. p' G; U/ E8 |
! W1 r. B* W% ~% t Status = FileHandle05->SetPosition(FileHandle05, 0); 5 k% Q+ [ p" x
if (EFI_ERROR(Status)) {1 U. m& E! G+ S$ L8 s8 Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" K/ z2 p5 h* S; }5 k3 F2 ]5 d3 u return EFI_SUCCESS;
. T) B$ T1 w5 K; U5 K }
4 i+ K, m7 Z+ p) a/ R) z$ i1 F' v1 ] 3 f) G" v& n# Z1 R6 h+ i
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);$ Q9 e0 z7 k M8 F
Print (L"File Buffersize is %x\n\n", BufferSize); A8 c) P8 s$ V7 \
if (EFI_ERROR(Status)) {
/ G# A- `: ?8 |' c Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' z' [; F. q( y
return EFI_SUCCESS;' g% f! n, u" t8 v$ W% N
} j( u! W8 c. a1 ^+ K" S
; x' C/ @. `% a* B Status = FileHandle05->SetPosition(FileHandle05, 1);
# L# |" z* m0 n+ |' I if (EFI_ERROR(Status)) {
/ N) }, _; _' O! W2 z3 Y1 A- m6 _4 ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 f+ L2 v! I U E: f; t0 q0 b$ m
return EFI_SUCCESS;
2 F, O* k" ^3 C }
* W) q1 o# F* I% _8 j" y( ~- } . c/ z- z' p9 ]1 M g6 {
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
) s# f9 t7 [9 M4 q' X; d Print (L"File Buffersize is %x\n\n", BufferSize);
, W* w/ i2 A# R3 A1 {1 a: y if (EFI_ERROR(Status)) {7 Q. c; [4 A) N' E2 t% \
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);" R/ a) q8 ]7 i" ]; ^/ W! K
return EFI_SUCCESS;
# o4 P2 b# _5 U5 r/ H } 8 a: J. s) y6 F. M& Y6 ~3 t- I
: W# w! t6 |5 w
Status = FileHandle05->SetPosition(FileHandle05, 2);
. l) Y: B7 ~" L2 j1 N, s' k if (EFI_ERROR(Status)) {
* y+ y& Q$ g& [% d Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 W5 W' g2 |, H: G) D return EFI_SUCCESS;
" k8 V& K( ^- @+ U* I5 Y }
! L. \2 _ F9 a) w
* \* Z2 y2 K( z3 M* U Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
$ n) y8 C( d% P4 I! _* E: X Print (L"File Buffersize is %x\n\n", BufferSize);/ x% w* L4 j2 T
if (EFI_ERROR(Status)) {4 V1 z5 J' H8 [$ L8 c
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
$ T: {* J' R* v' q3 ~$ ~ return EFI_SUCCESS;: l7 s6 [! O- W' _3 Z9 I
} 0 y6 J& ]: ~/ Y6 J& I! Q7 ?& i
7 t' ^3 j4 p6 v( J' H$ i6 G
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
% `8 w$ T; I) T8 P& y/ Z" o7 E2 f/ K ` Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
+ q9 O; T3 O) A2 z; y# O& }9 F; f Status = FileHandle05->SetPosition(FileHandle05, 2); ( O1 ~" u0 G9 t
if (EFI_ERROR(Status)) {/ ? ~: |) r( q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* _% {# T4 D. t: d* ?( k return EFI_SUCCESS;
6 X9 H( j8 Q5 g: t2 k# t% g2 q }2 a4 z0 D6 T& c
' u% x. v4 E5 c- d0 B
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
- |& F" x- W/ u% R if (EFI_ERROR(Status)) {# {1 P/ r8 a: C" k2 s
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);* v# v, {6 Y( m4 D8 q5 d1 V4 T& \! h
return EFI_SUCCESS;2 _5 u" z. I* D" a
} 8 G( r5 o& s- z5 |4 M' Z/ D
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
+ T& y) T6 j0 G0 p! G8 k+ N& r& V" P//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
) g, d! L( V3 G& t
7 U8 j& c5 z1 o) G: [( ~ Status = FileHandle05->SetPosition(FileHandle05, 10); , y6 K9 O: Q9 W# U; ~
if (EFI_ERROR(Status)) {
0 @0 D. a9 U4 y2 q# f Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( z3 V4 P0 I; v" C; h' p/ f return EFI_SUCCESS;
& H* D7 S: m( X# a2 K) ] }9 \, r( G% \# Q: j, Y8 n
. I/ T% W2 L% A4 E7 f4 Q) L. ~
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( Z. N* N$ V3 c. o8 v1 R/ a% S if (EFI_ERROR(Status)) {
W) J* D9 W. T0 n Print (L"Could not write file, FileHandle05 err code is %x\n",Status);+ @' F6 O M) N
return EFI_SUCCESS;! K' F F6 d2 y0 \
}, r6 k' Z% b* g: {
# f& i q# q3 m9 l% h Status = FileHandle05->SetPosition(FileHandle05, 12); 6 b1 a, ^1 u6 Y8 K0 q+ X
if (EFI_ERROR(Status)) {
: u$ Y) C* P7 z$ Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
x$ ^; j ]+ w return EFI_SUCCESS;# N" g: W8 l1 s: Q, j
}
& v- u" T# @. k) s2 \
0 w) G3 y3 h. v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 m8 r! A+ H# Y& K: U4 t if (EFI_ERROR(Status)) {
9 K; S: Z' C, |' C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 ? i3 q( f7 C! ?+ x return EFI_SUCCESS;6 s& G* O/ }5 q/ R5 T/ P0 D
}
$ J. o1 D: `+ N" V5 s4 l
$ R+ l) w2 Y# c9 M; X; S Status = FileHandle05->SetPosition(FileHandle05, 14); 7 p9 A, l i" J X' \
if (EFI_ERROR(Status)) {
4 w/ o+ w8 Y! b: O! s$ e$ {: R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, Z/ P' u4 X8 m" M- A return EFI_SUCCESS;
3 J* h* R9 q( @7 A# l4 d7 |$ b }
; h$ g! X' @" F" q3 {7 n7 E8 p4 Y
8 C# P V$ t7 y+ H' u5 R Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 x2 n/ Q1 G+ z$ K6 R if (EFI_ERROR(Status)) {
/ X, M9 m4 G( ^+ @% x- I5 t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. i- a1 y3 Z \6 x& O
return EFI_SUCCESS;
/ H2 m: f9 i$ T$ V- v }0 f ^ p' B/ ?: V% A
$ S1 g! k5 `5 [! P
Status = FileHandle05->SetPosition(FileHandle05, 16); 8 T: i9 Z* u" p# }9 t$ d! d' F- t, C& e$ A
if (EFI_ERROR(Status)) {0 h- j* q8 M+ W: R) Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( R0 s5 a; {7 ]* ] return EFI_SUCCESS;
$ X, f& L# f$ ^2 A$ w }
7 z+ Z _, A2 i2 [) O' r 3 c1 _7 ^9 d1 T6 I$ R4 ~. T
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' ]& l5 ~- I- i+ f5 E
if (EFI_ERROR(Status)) {" G( s6 b* `" B5 Q' Z$ G" _" B4 O j
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% \9 P' U3 i6 h& w/ x return EFI_SUCCESS;* K' P7 ]% j( W" p
} A! w' r3 C. w+ U) A/ B
0 x2 ]( y, _. `/ H
//---------------------------------------------------------------------------------------------------------------------------------------------------------------8 _4 u3 P+ E# ~; v# r, }
6 X J9 {7 v6 @. |$ i* s
Status = FileHandle05->SetPosition(FileHandle05, 18); & v" \3 T5 T1 a) q
if (EFI_ERROR(Status)) {0 C9 w' b; a. A, _7 P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. a; f8 u' K) @4 [2 H9 f
return EFI_SUCCESS;
$ N: a! }" |8 y4 \3 P! @- k1 f }
. U' Z& M2 Z0 k+ H$ C
" u1 T' Z0 d# C Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
( s# B4 o2 z4 f5 x0 {1 w# \$ Z if (EFI_ERROR(Status)) {
$ \8 }2 K1 t- c Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
+ R2 Z |: c+ d return EFI_SUCCESS;
% W k' B+ Y5 P' d( O; p } 6 ]0 q$ T" `( u8 [9 `" r8 {
: ^. m- S( L% S- `& ?5 ]
FreePool(DestAddr); / k- a% C$ H, Y7 y
( b3 B4 ^6 q$ ]! A5 n) T$ y
Status = FileHandle05->Close(FileHandle05);/ B% m2 _+ O+ D3 R. u" [* G! n/ f
if (EFI_ERROR(Status)) {
1 k _5 O7 Y" D# U3 l4 s: ` Print (L"Could not close file, FileHandle05 err code is %x\n",Status);, A7 J; j% D! U: W4 ], O% {3 q
return EFI_SUCCESS;9 C7 f$ l5 ]# s f; I2 F d. f" ?
}9 j9 ^3 M: G. e- R
8 e& r6 ^( d( h! s2 M9 c1 X
Print(L"File Name = %s has already been created.\n", FileName);
2 f# ^( N) V/ r) g4 z' S9 A% k- x; R; C; A
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
$ o3 _- e8 b3 F9 q WaitForKeyOrReset();
' G9 N( K; C, p( L 5 [6 Z9 U4 x( V" |9 g t/ k
return Status;
% a: t/ F. D# c, J' ~, j- |# t} |
|