|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*2 p9 l/ [* u3 z0 Z' ?
* myfileio.c
; l) l$ r; q. x% ? * Apps0 u+ k0 G# ~) {- P2 T
*/# o' R, e2 b* q
" q, O' q, J. W( j% h: Q! [
#include "efi.h"
1 ~! G$ i/ ]4 ^* o; q3 J3 B% Y#include "efilib.h"
6 ^& r; ]5 M; ^8 R2 @8 m) g4 n. S) f w
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE! e [: n: o" B! |- q
6 X7 O8 t4 T# d. Zstatic EFI_STATUS WaitForKeyOrReset(VOID)
/ f l/ \; y) r, Z: y' r0 b2 a{4 Z' Z3 ?0 i# ?" i5 L' E' x2 Y, N
EFI_STATUS Status;) q3 U; ~2 A6 H0 T) M e
EFI_INPUT_KEY key;- H% J$ R! ]; P, F# q
UINTN index;9 ]& W. L! m) U5 m; Q1 I4 t
( C- i& N7 T- e2 p% H6 \+ _- X: L for(;;) {
! c, ]! `& n* ~# W, c9 v8 t Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);; P. M* m& z$ S% Y! @0 a7 b
if (Status == EFI_NOT_READY)
1 w. Q `' W; z& h r BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
3 i, N/ F6 u& N2 q7 @$ k else
4 p1 W3 B ~; X, f9 h5 d3 V& c break;
+ j* u4 O1 O/ u& E/ N" w* { }
. w/ r! ~1 M& k {* H# c if (!EFI_ERROR(Status)) {
6 ?) n/ w* [5 ?; _% F, P0 ] if (key.ScanCode == SCAN_ESC)
+ l/ o/ t2 p9 G! ^ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);3 w Y9 }( v9 \/ V9 @6 H
}! V7 R& i! ^/ F/ E) r* [
) P0 L6 t0 q$ {( r" \! ?2 j return Status;
K* [- k! ]. H& j" v3 E% J$ P}. Y: l: g1 D' w9 q) q$ v0 o, _) u6 g
' V& c, ]2 b7 F/ }+ n9 g, g6 p
EFI_STATUS
0 C2 P6 a4 q8 a2 [, k; FEFIAPI$ @! D& |, ?0 Q3 P+ q
MyfileioMain (IN EFI_HANDLE ImageHandle,
$ U# `" ?+ Q3 T- }/ Q6 j IN EFI_SYSTEM_TABLE *SystemTable)
6 ?. |) c) V! X6 K5 n% k{
' [- G, \6 X6 }7 w EFI_STATUS Status;( ^( m2 f ]% l/ ~8 `0 S
EFI_HANDLE *DestAddr; 3 o+ Z- f7 z8 M7 a5 `( ]$ Q
EFI_LOADED_IMAGE *FileHandle01;
; N- e2 N& l) ~& M" O" z% Z EFI_DEVICE_PATH *FileHandle02;
: @* V3 d$ q8 ]! N f; P# u/ ~ EFI_FILE_IO_INTERFACE *FileHandle03;6 q. ]! s% z1 {. |* B: z) D1 l
EFI_FILE *FileHandle04;+ b7 ]" l/ L1 B( `2 }
EFI_FILE *FileHandle05;
4 ^! d& b& @' z CHAR16 *FileName;/ c; Z. Y4 f' B8 t
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
5 f3 N" K5 P+ `. w4 R [ UINTN BufferSize = 8, BufferSizeKeySpace = 2;3 J6 R: \, |' v0 H" c( \
int Bit0, Bit1, Key_Space;
7 t- N9 f3 i( {- U3 Z+ |$ z' i$ w4 l) U
FileName = L"NewFile.txt";
7 K* C' ], f, W: o ~! q+ B Space_Key = L" ";
( ]/ m7 v+ S2 c* y M, }/ A
: V" @0 v5 e0 b" s BufferA = L"ABCD";
6 w. w' L! B- y* O BufferB = L"EFGH";% A" M3 o4 {: E
BufferC = L"IJKL";
" B1 k- L- x) G) U$ O2 v' G. A! |8 s7 S
Bit0 = 0xff;
! d0 `: I& K* L1 U Bit1 = 0xfe;2 ]9 |7 ^- R- t8 e' G
3 I( I, K4 a4 K5 l7 n6 O$ o% ^ Key_Space = 0x0020;
# r$ p8 \0 x0 T A( Y
6 L$ B! x7 p Y% R+ {- f' x9 m! a InitializeLib (ImageHandle, SystemTable);
9 n& E8 F( ?& _! O
( F1 n( D7 x3 ~* } DestAddr = AllocatePool (BufferSize); ; e6 u0 ?* }, r( h8 K
/ K/ m! z W- }' z o: E
Print (L"Value of Bit0 is %x\n", Bit0);
* _: L, ]4 y( a4 G# B Print (L"Value of Bit1 is %x\n", Bit1); 7 Q+ l) G' Q, i; h* \/ y. g( L
, `0 w+ H: B8 j5 [+ i; x 7 Y o" X* J3 Z, d2 S! s4 i
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
) l k8 t5 |! ? if (EFI_ERROR(Status)) {' e* H0 F# D3 B( I) q
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);5 b/ @( o+ h0 B1 J7 y
return EFI_LOAD_ERROR;5 P+ c9 M D: `! Z+ p8 h3 O
} R- \4 |; H `( J- K
$ C& u+ O6 x8 C! N" M Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
4 d+ }& [9 j" ~5 {; x if (EFI_ERROR(Status)) {- D' I$ Z5 N/ {- H3 Q/ z3 b
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
! j, x) b' H5 a! E' {( d return EFI_LOAD_ERROR;/ N; N& G, G2 p! j% g: e( }2 M
}
7 m' C1 o" J2 @. K( M ) `/ i) @9 s0 Y4 S2 ?
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);; Q8 a e* R, S1 i' ~* b, C2 N
if (EFI_ERROR(Status)) {% F6 l+ o! ^3 O. c0 f! ^' k% M# m
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);9 K* p, ]3 D0 |7 U I, Y
return EFI_LOAD_ERROR;
5 u3 b2 f4 V" c9 G2 G% E }8 G/ K$ e) x, z# m5 V% M9 F! x4 T: C
% @8 V- [7 K% b" f4 i+ r3 F Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
3 t0 H7 R& L1 D; B6 A2 k v5 x if (EFI_ERROR(Status)) {( [9 N! A2 t; h1 i- n4 K" a, k
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);7 r, }6 v% z5 t& B
return EFI_LOAD_ERROR;
. ~3 r; i) i! i( J& R, M' E4 y }
o% B6 h' v8 M/ ?% a, J
* {6 Z. y& d- V+ m1 h6 ^9 r- A Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);& K0 a& j; M' d( d
if (EFI_ERROR(Status)) {! W# V! a! M) A& z2 B
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);! c3 N6 ]; A% D R3 a2 K( b; l1 x9 o
return EFI_LOAD_ERROR;
9 X8 a( |! O1 B) B# ]1 k } i3 O! ?# _8 J, X9 x
. V9 j; c7 g/ [, N% D! _; r8 W
Status = FileHandle05->SetPosition(FileHandle05, 0); 2 Q' u' V1 q+ K# y
if (EFI_ERROR(Status)) {0 R, O: h8 Y5 f( P0 s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, C: [3 ?, b) h
return EFI_SUCCESS;2 a$ F; t5 @& d
}2 ^9 s# Q( s/ k. v+ ]! y& e8 p
% r# C! {2 K' U9 U# A1 h Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);6 p \+ U j5 @9 b5 ?
Print (L"File Buffersize is %x\n\n", BufferSize);
- X9 p6 R, ~5 \ if (EFI_ERROR(Status)) {; z* D- t1 G/ w7 t- d: j* n
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
! G1 f( j- v# x return EFI_SUCCESS;$ h/ e& @8 A6 ]/ l A/ l
}
2 k7 J# ]( u' o. W7 I4 l% I
6 e( }) M' [; A* j- V4 q Status = FileHandle05->SetPosition(FileHandle05, 1); 1 a& z: j' x: D$ H* q( Q, q
if (EFI_ERROR(Status)) {4 R, e8 h- m9 T7 ^
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ B1 \( W5 `3 A0 k( [ return EFI_SUCCESS;
5 `, a9 s! @" ?8 ^ }
& f, E2 W" S$ n% A& h3 M2 u 4 G: ~7 O8 B5 p0 a0 T8 u
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
5 A" W' H; `5 G+ y' a: ? Print (L"File Buffersize is %x\n\n", BufferSize);
2 D+ k% ^8 z/ Z2 [5 `/ w if (EFI_ERROR(Status)) {; S2 b3 f- g5 m! _) z. U, G; y* O
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);% R9 Y0 q( y! m1 K, T) ?0 t9 T3 a
return EFI_SUCCESS;
1 |9 ]+ Q6 n" B3 L) Y }
0 G# J! a, P% B, `0 z: J6 ?
, l" a+ x( |% H9 s5 r; A Status = FileHandle05->SetPosition(FileHandle05, 2); . K2 `1 i1 @! G# L6 X' ^4 D3 m
if (EFI_ERROR(Status)) {
" u/ U, A2 r+ g& ?* k1 \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 ]9 ]3 b: J8 f+ I6 D9 f, k: f" p
return EFI_SUCCESS;4 ]1 ]7 g6 o1 j# G ?1 I) Y
}5 w! ]- S/ a# Q5 I5 l
, V3 [$ S. ?1 g- @& l Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
: m$ X8 ^0 J8 @ Print (L"File Buffersize is %x\n\n", BufferSize);- \" T2 p3 U' w8 @* ]" S
if (EFI_ERROR(Status)) {
- P- j3 q0 g6 K. r Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
; m+ e/ N+ c4 o' c s7 p$ Q$ I& |, C2 i return EFI_SUCCESS;& t: l5 e! S: X2 E/ h
}
: m9 t H: J, Y; Y$ e; N& H$ J" @
/ v" v o+ R9 R! @1 g2 }/ c//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>+ ~# c" _. |- z4 N! w u0 V" d
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
. U$ s! m, K6 h; f& y Status = FileHandle05->SetPosition(FileHandle05, 2); & w o( [3 I% A t. b' w
if (EFI_ERROR(Status)) {
0 S8 o; d) y" H# Q# c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' Y# p( w$ X/ ]1 D0 l
return EFI_SUCCESS;
; ^! H8 O' @9 C" Q: X/ z/ o }7 Q* b- I, e& \+ |+ ~6 i
, v! N, X+ B' S" I Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
7 V1 v- p, V1 R6 H if (EFI_ERROR(Status)) {1 a$ T! ^, h/ w2 V
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
2 l( Q: o4 M2 M1 o c+ K' a' m7 l5 a return EFI_SUCCESS;
; U' J' g3 }; r( _' k' i }
; c5 B. i4 N4 h) t2 u1 { Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
t' @5 u, ~. P: {! i" F//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<# p6 w0 b7 _3 {% f- b/ A
/ E4 ?* m2 n6 j' W# x/ H n* u6 X
Status = FileHandle05->SetPosition(FileHandle05, 10);
# W7 ^4 c* A. _9 {( L if (EFI_ERROR(Status)) {
6 M) o; s" Q4 o1 v0 [8 }# l" h1 @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 X/ G. c" O. \+ l return EFI_SUCCESS;
" }6 o/ M# @7 s; V }
- _. a# \4 u4 k, N, `- q% h
- K5 i! E* C: r2 n Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; [5 a4 }( C& q" p2 G% T$ Y' O9 c if (EFI_ERROR(Status)) { p3 P7 k6 c) T; G: S
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% I- E4 B4 ^" M* f1 b
return EFI_SUCCESS;
: N! p! Y# B' X }
1 l4 l9 \& D' t6 l% t6 K( Z n' ?9 m6 U% c V. i# Q2 [( l+ ?- X
Status = FileHandle05->SetPosition(FileHandle05, 12);
- D) o$ X6 ?3 ?' R" J if (EFI_ERROR(Status)) {0 c+ Z) z E$ D( }, ]6 o
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! E2 M! I+ A( _, C, P6 V# n9 }0 E7 z
return EFI_SUCCESS;7 n6 n; R0 H. u* V, |$ ]/ K
} P6 p6 k% X: k: i3 }; {
2 g2 Y! Q: c) S; ]% F( B
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 z! C% Z; g4 W6 Y0 u
if (EFI_ERROR(Status)) {
+ Z6 H! z V( Y; d M( y+ p Print (L"Could not write file, FileHandle05 err code is %x\n",Status);8 w) T4 H+ k$ J
return EFI_SUCCESS;
' K7 B# n/ ]2 g# O0 d: c% W$ a" [) X: @ }
3 N$ J0 h. b& z1 L; w: [
6 e* S( h& `" g Status = FileHandle05->SetPosition(FileHandle05, 14);
- Q. Y5 R2 _, R if (EFI_ERROR(Status)) {
. q$ r2 t+ Z: Z3 I Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 N2 o; g& }- g9 |
return EFI_SUCCESS;
/ }; ~# B. o# x. f7 i8 [! O" t0 c4 T9 L }
. m8 u# o8 J8 V/ M4 C
- I' k$ O* h0 O8 O3 F5 J- W Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);0 d- i7 Z; |9 Z* i; G- o7 M- Q8 X+ V
if (EFI_ERROR(Status)) {
; E0 ?7 B: I9 b1 C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
\* i0 Y( p6 ^! K1 a) J5 F return EFI_SUCCESS;' r, m3 v9 t% |2 d7 [
}- p# g& J0 T% b5 w
4 W4 V; B* o3 D* Z5 w5 l8 | Status = FileHandle05->SetPosition(FileHandle05, 16); ; V0 t; z/ g# ` h# s
if (EFI_ERROR(Status)) { ^6 G2 o7 x% }) Z
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( |. I0 x* `9 b! l
return EFI_SUCCESS;6 x+ x7 {# |6 `9 X
}
# z9 p1 N5 L/ |; Q- O/ f
0 v# t$ j: t% F: \; _9 s) c! J9 _ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: d/ [. e. H3 ?- [
if (EFI_ERROR(Status)) {" l* u+ K" J) ?! |0 k* m4 y* |- D
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- D9 v; H4 t; x0 g* @. R return EFI_SUCCESS;2 l: i0 H s0 {& v1 f
} & D$ m) p8 a5 d) b& i$ P9 n5 {
' L3 E" J8 {' S! \ H$ X1 D: X
//---------------------------------------------------------------------------------------------------------------------------------------------------------------" L! E) F- A% z4 t
" z0 B! ?5 |1 Z# |) k7 E
Status = FileHandle05->SetPosition(FileHandle05, 18); % i' C$ b+ c8 `: j' `. r
if (EFI_ERROR(Status)) {
7 P- X0 U( l) P* ] Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 S6 g% ?4 k' I& c. k2 t return EFI_SUCCESS;
; k0 v4 `6 V# |3 a, { }
" M( p" `* r0 E* C; w6 c
5 g- I$ t# w3 m' w' ~$ u8 E Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);9 a, @1 _. p" l2 o$ N3 v
if (EFI_ERROR(Status)) {7 e8 |8 f1 ]. I" S( K6 H( B& ^5 t
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);, J( Y$ |* q( `! g; c
return EFI_SUCCESS;' h5 u' \" X; m& j" q! x5 u
}
4 O3 n/ x) m/ j7 i
. Q+ z: E) X9 ^" k FreePool(DestAddr);
3 w3 g$ e$ L2 o% P% b
7 Q) k* T# T2 u3 h4 S Status = FileHandle05->Close(FileHandle05);; T- `9 I2 _# t! f- {* t
if (EFI_ERROR(Status)) {! z# ~( E* D- s( w& L, J v5 N7 G
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);: G. b3 H8 g( X5 D
return EFI_SUCCESS;
$ Q! w, c$ @* J2 U2 ^ Y }
9 A- e& k# }3 G; a/ { 6 x4 [; B$ |! r- X
Print(L"File Name = %s has already been created.\n", FileName);
# g" D9 p A x) R5 P% ?2 [# F7 K4 H( U( e2 [( I q1 b5 ?! t I& U
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
) R) |5 \2 d F. h& ]) r4 d* _ WaitForKeyOrReset();4 m) r1 ~: c) b8 x: D& d2 z' E
' s; C/ p& x' X return Status;4 _9 O2 @, d- v$ O
} |
|