|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
7 |! K \- l& Q p/ d; \* z2 D3 a$ Z * myfileio.c
4 n7 L Z1 S4 \7 ^# {4 R * Apps& l# U) A* `7 u- W6 U! |
*/
( h* I9 [6 r+ i0 A6 W0 b# f+ }& a+ q) v, f0 r! a' f1 }# q( ]
#include "efi.h"
3 X, C/ D! L' A5 x#include "efilib.h"
1 {+ d0 \" x) v& P# n( `9 R
- s* Y3 H: Z l1 b#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE" s6 @: l, |: Z: D+ i# z
. @8 p$ }; J# U: v, nstatic EFI_STATUS WaitForKeyOrReset(VOID)
% m( r+ b+ [* Y7 A: T9 U{
, Z; Q; k6 C7 p EFI_STATUS Status;
( K5 o0 H: p0 e EFI_INPUT_KEY key;, E8 o' O, {1 ^. Y3 U2 }
UINTN index;
& f( J5 i! G' v' D3 p' ~6 f0 W : k/ |7 h( p7 l2 N' }" t" T
for(;;) {
- d y5 z6 j! g5 O Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
. ^4 {- G; c& k if (Status == EFI_NOT_READY)
7 m* _+ c- G3 S& ~0 D$ k BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);# H7 G# }% C, E W( u0 U! A
else+ L9 c+ c+ G6 K1 p
break;
0 w6 J z; W, [9 E }
9 c% D8 e$ L2 Q; r/ Q$ H8 [ if (!EFI_ERROR(Status)) {$ A2 K* u0 ]( }9 n) L
if (key.ScanCode == SCAN_ESC)( Y+ A4 y% ]9 T, H; T
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
( U+ C! r1 O" d* H8 b }
/ ~/ y$ u5 K2 m/ D: G# s5 R
- ?4 K. d, W* X& f; V return Status;
. ^+ S, I( A7 y/ E# a8 v; M& V/ E}
7 E! V5 g9 C$ g8 V# b5 ]. s. S8 S; \! u; t
EFI_STATUS9 Q7 {; ~ M9 [+ F3 f( J I
EFIAPI
( X% p J% D/ _) E1 V5 T5 z- BMyfileioMain (IN EFI_HANDLE ImageHandle,) V3 C: S3 x6 [) N B* z, D& z6 D
IN EFI_SYSTEM_TABLE *SystemTable)& {* o8 H5 K7 M* D+ J/ V/ r
{2 `: I* L# {* h4 @2 D E. _% t
EFI_STATUS Status;. A0 w, }& U4 y$ f4 l0 p
EFI_HANDLE *DestAddr; ; H0 l T0 h( F
EFI_LOADED_IMAGE *FileHandle01;" _8 k8 |% g# Z7 p: V) U
EFI_DEVICE_PATH *FileHandle02;
2 F3 d6 i2 N4 F EFI_FILE_IO_INTERFACE *FileHandle03;
2 ]/ C# f0 n( B, g, u; R# [+ ? \# j EFI_FILE *FileHandle04;9 |8 d/ z4 M* ^: l5 V( j( ?
EFI_FILE *FileHandle05;
: c) R) i% g: H CHAR16 *FileName;% A+ K; z2 i5 E3 Z: _
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
5 d1 d! X% K. o) ]+ } UINTN BufferSize = 8, BufferSizeKeySpace = 2;* P1 V/ x7 ?" N3 i6 S( A0 m
int Bit0, Bit1, Key_Space;
: r2 `5 L$ a, f
+ Y N5 o7 c+ a FileName = L"NewFile.txt";
0 \0 T& q( A# }9 e2 T Space_Key = L" ";7 P1 h3 \( h" w- z" I
0 G' _% R2 N* [0 u! y% F) r1 N BufferA = L"ABCD";! C& [, o* R- _4 \9 A
BufferB = L"EFGH";9 F* b Z4 G( e h U' b8 B( H
BufferC = L"IJKL";
" B9 E& |) @, v! q4 G7 C! M: ~. g1 C( H6 v
Bit0 = 0xff;* _0 ~8 L5 R& ?0 Z" {" P) f
Bit1 = 0xfe;1 ^! g9 r h) P+ g+ h
, \$ X9 K* A |" { Key_Space = 0x0020;6 {) v0 N4 T+ Q/ _6 B# Y
8 Y. m5 \) H0 ~: _% H$ l
InitializeLib (ImageHandle, SystemTable); ' d& e6 y* J$ a0 R
) z* d! f) M1 H2 |# m' v- z DestAddr = AllocatePool (BufferSize); 9 ^& |. {& I' t2 d2 O; e3 i% f
O" C8 T/ `9 S" v4 k% V8 {$ n Print (L"Value of Bit0 is %x\n", Bit0);
. U1 P7 z, K1 k8 K% e! x Print (L"Value of Bit1 is %x\n", Bit1);
1 W; @! T7 c& r+ ] k' y
3 ^+ L" ^( t" ?$ B, B6 P$ f. V
" `' }' [5 Y0 L7 H% m1 n Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);! H3 n9 B0 X3 M t& ]
if (EFI_ERROR(Status)) {% L Y( ~, y% J8 {; D/ N
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
6 E, `' ], X% _: T( a return EFI_LOAD_ERROR;
; k* B/ J8 R9 w }
7 F/ L0 s. F, o- |+ d; x9 y9 l$ G7 f, {8 G% \/ s
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);% l5 e! e. n, W! }/ m1 }5 C/ X* g" ~
if (EFI_ERROR(Status)) {6 R: a: G' p9 @: Z J2 k0 y
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
% v8 v: u. w4 y" D4 _8 p return EFI_LOAD_ERROR;6 k: I |6 p8 \8 y, j) F0 ^
} 6 E! c4 L; l8 ?0 K. t
5 I( K. E9 T7 o, a1 e. n2 S
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
- x' }- O6 `/ ? if (EFI_ERROR(Status)) {
8 C! v1 R; ]- T Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
" i, l9 \9 ^/ k/ E, y return EFI_LOAD_ERROR;) n4 K8 d/ x# ]0 C
} j G7 k/ }$ m) ?
: j* C- _ h& B Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
6 ^- G7 W% s3 ?" J if (EFI_ERROR(Status)) {
6 C. B+ s4 n; v$ Q9 z0 { Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
" I# h& [6 Z4 k7 q return EFI_LOAD_ERROR;
) b" `+ X, W* ] } : G1 F" N' f$ j3 F3 ?/ j
% j j1 M# K3 s/ m Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
+ g0 b" E" Z+ q) V+ x l0 G if (EFI_ERROR(Status)) {+ i, `4 ^2 J2 ^; o
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);& a2 d, A# j% B
return EFI_LOAD_ERROR;
/ U) Y# D/ N+ f' n }* I ~ d1 F }2 q6 s, l# @3 J* ] H, [
, @; y+ e/ t+ a% L" `, e1 L' t Status = FileHandle05->SetPosition(FileHandle05, 0); * K/ }, Z2 d) Q
if (EFI_ERROR(Status)) {) j9 \! r$ [3 m5 f: ^5 j) d
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 G# K: l8 x A
return EFI_SUCCESS;
2 l0 ]8 g& u/ A% t$ L }
+ T' q j" c& u8 O$ n" }" |/ s
) P( d2 X/ v6 Q% t$ i# a+ p Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);0 {# @" Q$ ?3 Z2 Z2 ^3 D+ Q* ]
Print (L"File Buffersize is %x\n\n", BufferSize);
$ ^+ a6 n. d; i$ C7 h$ I' ^ if (EFI_ERROR(Status)) {
# ]- S* h1 `7 B: a Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
: I) q, z2 W2 p& X3 u& g9 _5 }9 E return EFI_SUCCESS;
* r. i3 W7 d$ `+ d+ u2 O }
+ F: A% H. k0 [5 V( ^
2 K2 P1 ^4 {5 \# e Status = FileHandle05->SetPosition(FileHandle05, 1);
: ^$ b. J6 o5 }: e) \3 C if (EFI_ERROR(Status)) {6 p; k) N2 d% C0 i. w' p
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 p% b: M, c2 k: R3 ?; F U# f( A
return EFI_SUCCESS;9 g4 w0 c* B/ q) S# J# |
}. ~- y9 b7 x# C! {
: R! T1 }( G! O7 u
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
2 P5 s4 D* @$ k" `' ]4 h Print (L"File Buffersize is %x\n\n", BufferSize);
# p) V# u9 U/ k1 {( V) j if (EFI_ERROR(Status)) {; n2 f8 @# h; l( s- P
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 G4 |* y9 L6 D$ u+ t return EFI_SUCCESS;
3 ]6 u% _" A7 T+ t" G1 o- b } " j6 s) E4 v% S0 p$ k
4 R* ]- ~ S+ W) T
Status = FileHandle05->SetPosition(FileHandle05, 2);
, Y7 M& ?- R4 C* Y/ d6 c9 J if (EFI_ERROR(Status)) {
) G' x+ S, a$ D/ h& D v, \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# B& T$ T k: e+ t1 q return EFI_SUCCESS;
' |: \0 s! J& k8 P" u# X, l }
C' F) a; h; {, X3 I Z7 m1 k# S: K# Y" g
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);5 t# Y- z- f6 O5 R. N
Print (L"File Buffersize is %x\n\n", BufferSize);7 d9 g g+ n, Q5 s) ?& _5 H, m
if (EFI_ERROR(Status)) {
/ A8 {9 R& P8 i- } Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, o! x( q6 e7 L: I1 v+ C
return EFI_SUCCESS;3 p& k' i& F: j
}
# L9 u1 z! K. m( l5 ? 8 K H C" k! R: z8 Y& g* T
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
, @: F! t T* g/ \7 f1 B7 f Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
0 c. ?0 v( J, \7 x+ W5 _2 ]6 Q Status = FileHandle05->SetPosition(FileHandle05, 2);
. e' y% G# H1 |- n: [ if (EFI_ERROR(Status)) {
4 o5 o% Q9 q0 R) P4 F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 Z. l ]1 S' O# {, Y& i/ C
return EFI_SUCCESS;
; ]" Z* |8 \: I8 \/ ^ }
6 W, k. D7 z+ A) g! A6 D$ _# x& {7 b& t6 b
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
; c0 D( J2 C6 r+ c if (EFI_ERROR(Status)) {
& u' K+ u* w6 N1 A( @: g Print (L"Could not read file, FileHandle05 err code is %x\n",Status);( _0 \ X! S3 u) L, }; {
return EFI_SUCCESS;2 E1 `" _9 R: V$ w
}
( P8 A" Y O Z# c- F0 E h* Y Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
' d) D$ j9 X o# j: H% F* W- F//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<& Q0 i& y5 ^5 s; S
; o7 C0 }( r! a0 G% | Status = FileHandle05->SetPosition(FileHandle05, 10); 6 D" _$ l/ i# e6 V5 K
if (EFI_ERROR(Status)) {: i; i1 }7 j/ b4 c
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! ^9 k% w6 d/ U5 `' n* F& f3 }( B; Y0 R return EFI_SUCCESS;
9 I; m. \9 K! b" k) n' h! B } d( _, Y3 U- H" a
8 H9 g: F8 r& _& n Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ ^/ c6 [7 @/ k' R
if (EFI_ERROR(Status)) {
% @0 M$ D/ }! O Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) C* |4 R7 r; v( X" e$ t return EFI_SUCCESS;4 v! M& ~' E, T$ f5 @
}; ]2 F, a, z+ @
; F. o2 U1 [# P+ S
Status = FileHandle05->SetPosition(FileHandle05, 12);
7 p+ D4 d0 t) K$ C8 \: g1 Q if (EFI_ERROR(Status)) {& X' O, p. m/ T7 w$ M
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 _/ j* |0 ]0 W3 I! g return EFI_SUCCESS;- W) A8 |2 K# P4 N) M o: n ^
}) C, k$ h5 K6 E0 T6 K
4 h. e9 p! _ a! A( w2 b! r
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);; P( \- a& [% h6 w
if (EFI_ERROR(Status)) {
# c+ _4 |3 A8 u+ q Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) ~! N" n$ d% z" i return EFI_SUCCESS;
! U }' S2 o% {7 f. ] }
0 |' m- q' K z& D9 g7 X/ Q4 l# r3 A# I7 f7 V" }
Status = FileHandle05->SetPosition(FileHandle05, 14); 9 l8 a4 s1 U% i/ t
if (EFI_ERROR(Status)) {
4 E2 O4 _. | t- K, p. d# O1 J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ ]3 _$ H0 h; F' Y# o: B6 }& @$ m
return EFI_SUCCESS;( o/ F3 c+ I& L1 ? p9 h$ I7 Z
}: X! e+ ]# ]& t- @* f, {
" Q6 p G; ], h# B Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& b% j- ~' d# z% \ if (EFI_ERROR(Status)) {! u1 B. `: c# F; O6 M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
+ @- _% a4 [5 ~ return EFI_SUCCESS;3 I V3 `# D, s
}% f/ V- _+ Y* X5 k
3 {9 Q) s( w1 T0 |% d5 Z) L
Status = FileHandle05->SetPosition(FileHandle05, 16);
* Q4 f# ^" c( p# k if (EFI_ERROR(Status)) {
, G; ~: Z/ o; T: a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* @0 B0 ?. `2 @+ N2 L return EFI_SUCCESS;- [) n4 o8 m4 w# D# e4 _' n
}) h, V; X: u/ C3 g. K; E1 {1 }
! e" t1 \) K+ _ U( f- N1 l. P( z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
6 Y8 @2 t: z* m$ y8 E, u if (EFI_ERROR(Status)) {
, j$ `$ q" s! ]- J! A0 ` Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. }; _& ^3 f8 I- _# |8 [3 P3 U return EFI_SUCCESS;/ w! F5 _% l8 R
}
8 ~ Q7 k2 O# v6 \( @% C* ~' B# Q6 c/ |3 D7 d4 P& V+ p9 s
//---------------------------------------------------------------------------------------------------------------------------------------------------------------4 |4 A+ ]: ]* ^7 I! |
8 N* }7 t6 c, k* f' ]2 q; `
Status = FileHandle05->SetPosition(FileHandle05, 18); , s" e, C3 M, M4 a/ ]" R& o
if (EFI_ERROR(Status)) {' E. @+ E0 P% P' S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 I4 ]# @* Q7 T' o return EFI_SUCCESS;) [6 t" Z# b" M% R
}
3 Z% ?. ^ Z7 j
7 v! u& K% V$ l$ ` Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
/ r1 F& |1 O& J3 L' T% [ if (EFI_ERROR(Status)) {' @7 B& v3 P$ x9 d9 W3 n: I. ]
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
& @ X) v w4 Y: F0 E! P" s1 m/ S return EFI_SUCCESS;
3 I+ B4 J6 N7 J4 w/ }* I# e }
. Y( H, _& V5 k) m1 k$ e
* r, Z: W) o3 }( b2 C FreePool(DestAddr);
. K4 j4 I2 P& \) u1 h" m! p! Y; g+ R( v6 D, D% a+ `% M: A
Status = FileHandle05->Close(FileHandle05);* Z8 R; M) _# h. B1 V
if (EFI_ERROR(Status)) {
# q2 |: I, E7 w- }2 c0 s; B* D Print (L"Could not close file, FileHandle05 err code is %x\n",Status);) K& w* @7 g9 B/ ?9 l) k
return EFI_SUCCESS;& _2 p- S& ^& z% X# C
}5 c! P% b2 S. _" E7 ~: A9 B2 ?
3 a; Y t, |9 M1 n3 ^1 q2 i) P, J Print(L"File Name = %s has already been created.\n", FileName);
) k! |4 S$ B) C' f' F1 o7 `9 _5 O2 h" q( x! O
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
) _3 R4 W) V) h' m9 y WaitForKeyOrReset();3 x# t& g: K8 _0 o7 h9 `7 w* X6 p/ J( T
) f1 C5 W5 B0 O: a7 E3 \ return Status;
- c9 C' T4 S$ i1 i* r i/ R& @} |
|