|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*2 ^; K( v& Z: f- B. x$ J
* myfileio.c( M% g$ k; G4 U+ O& b
* Apps
8 N; m0 M4 B7 w; H: W */, I8 ]6 x3 T0 {
9 @$ {7 ^2 \( I. V: s% \5 ]: H#include "efi.h"
) X% b: v& \0 m#include "efilib.h"! b& W! m, L4 k$ u8 c4 p) U: H
( }! m4 Q# d! i% S, r/ |
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE: X9 f- U' e* R" h4 G8 u
+ e: y) A% J* j2 h' t, K+ K. Mstatic EFI_STATUS WaitForKeyOrReset(VOID)
8 F8 X! O( q1 A- c; c7 N{. r0 A' O( D) N V% R( g1 o O
EFI_STATUS Status;% W9 ~. u1 D m6 X2 f0 v9 R, {
EFI_INPUT_KEY key;
1 K% Y. e6 V, g7 X% Q6 T UINTN index;
7 ], A! ?6 p# e3 [ 0 E# P8 p' I! O9 Z, ~
for(;;) {, Z0 }; ?! I" d5 \+ A k
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
/ H0 @. l' o! M' H; ^; L if (Status == EFI_NOT_READY)
0 r/ U. q; ^! x. K3 j BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);- Z8 R9 Y5 s6 o& P- ?, v" f
else' r/ {; X% I. S. q4 a; s5 I) b
break;. ?; L1 C4 j h2 |% q
}
' U# y8 d7 ^4 D+ r. o; Z s3 _ if (!EFI_ERROR(Status)) {
$ h5 Z" k: f$ X3 Y7 ^/ Z( v! _ if (key.ScanCode == SCAN_ESC)% }% W# W6 r% F# N9 @) i) o
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);' N5 C+ t* R$ f' B! y8 l' T+ h
}" S8 Q t) c4 E8 D: z: t) X" F+ Q
+ J: x: q" f9 |* _% h+ U4 F
return Status;
# n7 m! [/ p* L& r, Y/ a+ M}
$ C: s1 Z4 a! U* p& G# M0 B
N; e4 ]8 d8 l9 ]6 @0 v/ F3 eEFI_STATUS8 A( {' s7 S* ?0 d
EFIAPI/ P! n" o$ }( r/ m
MyfileioMain (IN EFI_HANDLE ImageHandle,
, s/ p$ N% n4 y IN EFI_SYSTEM_TABLE *SystemTable)
. R1 G7 q" V' {2 U' Y- H5 L{
( q- N: D# l+ b" m3 u3 i; n EFI_STATUS Status;
6 p) x/ ^, k3 o# @8 n- ~ EFI_HANDLE *DestAddr;
" I: `5 X) ^7 R9 x! t" C EFI_LOADED_IMAGE *FileHandle01;+ |* n6 }. }9 l) j$ R, o+ C
EFI_DEVICE_PATH *FileHandle02; % w8 O9 K* m: I* W1 p
EFI_FILE_IO_INTERFACE *FileHandle03;, M8 m) v5 v; w* x8 v( h' Q" T5 r5 Z
EFI_FILE *FileHandle04;. ?$ j: [" @: X6 p1 \& k$ S
EFI_FILE *FileHandle05;, m6 I! H- L0 ?$ ~, F
CHAR16 *FileName;2 ^9 r( S) f+ F: I; h: H
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
* R- ]. ?) `1 x5 Q! T UINTN BufferSize = 8, BufferSizeKeySpace = 2;$ f K- Q l" S* x+ [4 }0 H
int Bit0, Bit1, Key_Space;
* b$ }" ]/ {' r
: |1 N! x5 z# ` FileName = L"NewFile.txt";; Q* s: f3 h# P$ u
Space_Key = L" ";) A- E4 X. i' ]' a- s( H
/ Y9 o- {! H$ f2 c
BufferA = L"ABCD";
6 K' J: h+ A$ C* b8 w BufferB = L"EFGH";
5 C* ? Q6 Y5 c7 a @3 T2 b) Y BufferC = L"IJKL";
: P$ ?& r+ P# H( D! S
8 _/ ~, P* Y' i Bit0 = 0xff;
/ A, d3 \* _! r Bit1 = 0xfe;
1 w) \" D0 k& m8 [, p # a% l1 F( V7 d
Key_Space = 0x0020;
. b7 Y$ U1 W; w( \* h) v- B$ n 2 ]8 B o O5 @7 q1 i
InitializeLib (ImageHandle, SystemTable); / H+ X% a8 R9 L0 S5 i) C6 t3 w5 R
7 y6 W5 i4 _4 V& o
DestAddr = AllocatePool (BufferSize); " W7 w3 R+ }* T8 ^0 t5 z
% Y- a* x: l" r: z+ N$ p
Print (L"Value of Bit0 is %x\n", Bit0);
) z4 A C/ P2 n( I8 t Print (L"Value of Bit1 is %x\n", Bit1); $ J+ f; g9 E: h/ t3 J
, }! F1 s7 E+ T7 N/ h) j- V: [, J
1 \3 k( c1 k7 { ]
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
) \0 e& X1 ?5 R: \& w H if (EFI_ERROR(Status)) {8 R: W; U, R9 j
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
4 z1 u r9 @" d$ U$ M return EFI_LOAD_ERROR;' i3 W! V3 z/ u: W2 e1 S0 `! N
}
- ?/ ^ u. C/ O$ o6 Z+ b# S. C% x \2 A/ ~
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);" m4 e. u! h% Q- U
if (EFI_ERROR(Status)) {
( C, H' q6 F! W1 L" S' T Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
7 E" k* W/ h, b, z3 k" Y, D- ` return EFI_LOAD_ERROR;
" q# U, \! F8 N4 q }
0 I# G# ]7 H. l, R9 E ) S* B4 @% d* }. v6 Q
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);6 S8 T! Z" J, j: w. a
if (EFI_ERROR(Status)) {
# p# \" S8 z4 M% J% h& B Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
7 C$ d- ?! w: U) r, e3 J! [" _ d2 e return EFI_LOAD_ERROR;
6 w2 i0 l B2 G( ], K }( G+ y* t, P/ o! e0 D
1 n- l5 i! q7 l3 |0 F
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
) b; L8 k l% y/ y if (EFI_ERROR(Status)) {
$ H5 [8 N" F+ i% _( f1 A5 D( U Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
3 K. u. w! u$ L, M% j return EFI_LOAD_ERROR;3 r o5 c! V! o. E+ x
} / u- S. h- k! t/ l: {1 E
- x, }* n/ p" C; V8 I/ v4 a9 }2 l
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
8 b" E8 @7 W+ S8 N if (EFI_ERROR(Status)) {# O0 a+ [! T1 ]
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
" M- k# X7 o1 p1 ]9 e+ E2 V) b return EFI_LOAD_ERROR;$ H9 {8 C! I( {# i, V% x1 p3 ~
}& p' Y" L; r/ _9 E! i
/ s* z4 f% J& d6 V5 ^! `! Q Status = FileHandle05->SetPosition(FileHandle05, 0); / b1 L7 @2 [8 u5 ^8 l
if (EFI_ERROR(Status)) {. m/ m4 r4 ^* g% C ]: O3 m! g
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 \- J, U1 H" @ return EFI_SUCCESS;, w% h, V+ N7 U/ I
}& O* n/ S, n+ o, e8 B
0 S+ j4 u# {8 s- q H) s8 K Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);% J5 |6 B# @; P! {+ e) g& ~
Print (L"File Buffersize is %x\n\n", BufferSize);: f! V9 c- m/ @0 l
if (EFI_ERROR(Status)) {# d! R; h+ j' b
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 Q2 D2 n/ K! O/ f$ t4 x return EFI_SUCCESS;) Q& i2 v% S, Z) |1 u, _/ W+ g
} ' K' a- W/ _ w) X. A G
4 I. R1 Z* O6 s2 O
Status = FileHandle05->SetPosition(FileHandle05, 1); 0 N9 p6 b7 R" i1 H' X
if (EFI_ERROR(Status)) {4 _7 c' V) w1 v0 W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 G* I- k% w5 f8 a5 G+ s
return EFI_SUCCESS;
. i) p. y& M8 T3 ~* K/ a) P }- h( I% W6 S1 ^4 f, b: ^6 ?
1 g$ P* g9 D6 p, D) t/ h8 y' g Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);3 k+ v# \+ G( `: \ H0 `3 {; i
Print (L"File Buffersize is %x\n\n", BufferSize);/ c, v7 J" U# Y
if (EFI_ERROR(Status)) {8 K- V. F( c) y$ k
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ @' Y2 W$ a. _! e$ J- F
return EFI_SUCCESS;* a% ^# B4 t% I* `( |
} 6 c! s1 i4 u* {9 d6 O Z
x' }% Y$ h9 I# ~# S: Z
Status = FileHandle05->SetPosition(FileHandle05, 2); ! z9 }6 B" C0 p$ m+ \% \' P- A3 j
if (EFI_ERROR(Status)) {
7 r! U: `6 o% i, ?+ J- v% m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* J. m# E7 T! E6 t) G
return EFI_SUCCESS;
; O) y7 }% ?) |' @% [6 { }
: f) {, P8 i, K" c) k1 r
& W3 h+ i$ A; M' v Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
5 l* K0 J! i6 y a Print (L"File Buffersize is %x\n\n", BufferSize);, X) _' }5 P. R; Y z5 `+ M x
if (EFI_ERROR(Status)) {. C8 u* a D* s J/ B- _
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);$ g' \* [; y9 `! B
return EFI_SUCCESS;
, o- K S3 k, ?9 O1 { }
0 k$ s( f# k3 r0 ? , ]4 m; P" j2 |* h1 D @
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>0 C/ W5 m2 S" l8 K1 d
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);2 F7 B+ J( o0 e
Status = FileHandle05->SetPosition(FileHandle05, 2);
0 y! [2 w! \9 h: ? if (EFI_ERROR(Status)) {
4 X% k4 V5 R7 J, w6 o$ d Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: U+ v( V/ U0 u6 O! O return EFI_SUCCESS;
# D2 j. P( Y* b; i2 h) v }
% F; G/ L3 B& E+ Y6 T1 a
: U! K/ K2 a1 T( c$ V$ a Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
$ F) K9 e6 b+ j3 U if (EFI_ERROR(Status)) {7 F! Q, X9 D/ f
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
2 V8 ~) p& J; K/ @% a5 i" b% X return EFI_SUCCESS;
; y! M3 p" a) L/ v }
; Y, Q) u6 i! s) N Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
& c) c9 E0 v7 O+ ^//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<: I1 M% i6 o/ i: a1 k9 F7 q5 H
3 g. t" i: `$ J x; @/ m! b
Status = FileHandle05->SetPosition(FileHandle05, 10);
5 t+ B4 @& Q4 z. B* [8 T if (EFI_ERROR(Status)) {
7 k( ^( l. w0 w: Z% @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. u0 |4 Z1 k M$ v& V
return EFI_SUCCESS;
* Y8 F: s6 K/ n8 |7 s( q7 N* { }
% f" K& M5 {. o2 E o4 U8 H4 p7 k' w" B% \
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 ^8 V7 \" m5 s& }0 z- x) n5 F
if (EFI_ERROR(Status)) {
0 c! c8 C# e0 Y2 w Print (L"Could not write file, FileHandle05 err code is %x\n",Status);+ J9 m- v1 S) G( l" P( h$ U
return EFI_SUCCESS;
( @. }# b4 j3 U* A q9 M" K! `' y }
Z: K& |% V$ e- t) o+ }8 X7 a9 G1 Z$ l" p& m
Status = FileHandle05->SetPosition(FileHandle05, 12); ) W8 X( K, l7 H
if (EFI_ERROR(Status)) {
0 P6 v& {8 W9 O" u3 t7 J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 Q6 u6 p2 H* U, H5 F5 ^# ^2 y" n2 K! T return EFI_SUCCESS;
^/ `4 S- b3 `: e }, _) G& G: `& ]+ n1 S
! _/ ]" f& y$ g% j, s1 h Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 Y8 S) h' A. j0 S" q if (EFI_ERROR(Status)) {# \/ E: N3 C" p! Y4 U9 M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 ?" k# H/ l9 M# _
return EFI_SUCCESS;
% [. w3 a' P/ O3 p! A! a } B! H+ L8 g# c+ s, M
4 w, |2 {, e, Q" ] Status = FileHandle05->SetPosition(FileHandle05, 14);
- ?$ x# D! c( F' E if (EFI_ERROR(Status)) {! {% K, I: ]' m Q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 U5 ]: P) x7 u `; {. D
return EFI_SUCCESS;. J+ I$ X" G7 e' \6 n
}
6 z4 Z" `7 S& v% b" y( o) G
% G/ P2 Q% @! G3 F0 ^8 J Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' z9 W% v5 r9 h/ V5 f
if (EFI_ERROR(Status)) {
4 h; g4 g. B, @+ h2 E+ l Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) ~/ q' c8 Z6 {9 f0 u% e return EFI_SUCCESS;- C: t. w- b# V) k! k, m4 a/ H
}
: j8 ^: q V" H- V. v E1 g
, A0 J1 i4 d6 D3 R+ y% N& h: f/ p Status = FileHandle05->SetPosition(FileHandle05, 16);
2 P" E8 _1 h, F. `' @& @8 Y if (EFI_ERROR(Status)) {
2 t( w6 e- G, Y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 F! X! A* h# R/ U. p
return EFI_SUCCESS;6 Y: H1 S% a! s. R3 J
}
( R6 y" _( m8 ~# ^' C! ?6 e - k T3 y) M* d( B- L
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* k4 ?% m5 z6 D+ W8 K* _( A if (EFI_ERROR(Status)) {* h! e/ h$ \% @ a' B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- W/ n( Z3 M' i% i. G return EFI_SUCCESS;& L4 t7 m5 w! l+ k! S
} " M' B) q8 B* {: |6 C
7 |, ~( S2 S, C3 f# D$ M
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
* I6 k% ~$ Z K0 W6 V9 a* R* g- Q5 A k* c! {
Status = FileHandle05->SetPosition(FileHandle05, 18);
; W! n9 x, k# q8 O if (EFI_ERROR(Status)) {
/ f3 y" z- R! r; x; j3 N! t Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 X& R) M) M/ g4 T2 t9 e1 \3 k* t
return EFI_SUCCESS;
# v; d" T0 A! E; d! {' p }& s, e7 f# O/ W" ?( F" T
% N+ J* M6 d0 y" g
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);3 ~# N2 s' @3 m0 U! u1 d- N+ Y
if (EFI_ERROR(Status)) {
1 o: Q1 H' e" G) Y% |& v J Print (L"Could not write file, FileHandle05 err code is %x\n",Status); m+ \; b' Z+ y; h7 Y
return EFI_SUCCESS;
+ ?6 e0 Q$ `; |8 Y# O( [) D: f% j } ( q; u1 t. [0 D! G- e( ~
4 @6 U) Z9 F' x/ I' x! \$ Z `: r1 ~ FreePool(DestAddr);
N$ n' o; Q$ ?% Z8 o% R3 S4 f" ]5 U0 B) p
Status = FileHandle05->Close(FileHandle05);
9 ~9 z' I0 J4 H: V0 f3 ` if (EFI_ERROR(Status)) {
* Q' X& S7 D1 z: b- S, b. v Print (L"Could not close file, FileHandle05 err code is %x\n",Status);, g" B& X5 j# ?/ q3 m0 S
return EFI_SUCCESS;6 i$ ~) q( y3 {) ]% T
}
4 _4 k' x# o0 w: H+ P' c) Y
( R7 V* m* X2 t* J1 [ Print(L"File Name = %s has already been created.\n", FileName);7 S$ u& F' M: C2 S* @, p
B( K% R: Q2 ~& N- K Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");: F0 D0 z/ f2 J! j: u. _2 Y; s* K
WaitForKeyOrReset();8 \- e, c l9 W
F0 [( V, [/ ?
return Status;
" n" Q( n+ d/ |; P8 x} |
|