|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*7 g @& r0 p4 E& L+ [# K
* myfileio.c3 C; ^+ n* R1 o% o
* Apps
0 k0 ~! I* g" k' W* J. h */
# ^ }+ S+ C1 T
2 V9 t a1 n4 h" W- j+ L8 j( J#include "efi.h"
! j$ h2 O+ l w! E#include "efilib.h"5 Z8 z! r+ T. ]
; o2 v/ ?/ F2 r ]& {( G' |2 R
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE8 t5 g& h0 t: T# p3 y
( g1 d t+ r* c
static EFI_STATUS WaitForKeyOrReset(VOID)% B8 H% W% v4 Y* W1 [$ d
{3 D' `# C H: a1 i( h( K
EFI_STATUS Status;
6 v: t% X4 M9 H EFI_INPUT_KEY key;
1 ~! y' c9 m! g) f* d. B UINTN index;8 v% W9 N! P5 Z1 T0 F. t
$ O7 t3 a: d0 I$ W0 F$ ~ for(;;) {% l$ Q- l, D7 h7 `: v- k) y# D
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);+ _; h5 s9 N S( s% @
if (Status == EFI_NOT_READY)
, |8 {& Y/ R4 j, t& y* K1 H( ]- b( o BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
" `4 |+ ?* g; Z! O' u+ E, p* O else' ^* R+ ]9 e2 X& B7 X4 k
break;2 _2 W' T [# V7 d" t0 d$ x( k3 D. l
}; ^) Z- A; `9 j! m% z6 D
if (!EFI_ERROR(Status)) {% X& p1 e4 N8 e" K8 _7 n' F
if (key.ScanCode == SCAN_ESC)+ c7 H: [. f# F7 Q, M" T7 u
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);7 }$ \3 A$ a5 Y! Z. D3 r, c
}6 R" g; p2 y3 g* r; G; N6 }
, f% @& w0 S$ M. T# O return Status;8 P9 ?0 ~9 d; r. o: M5 E9 O& T7 @! U- T
}+ g! J) U& }# k
- s7 Q* c" r' O3 t2 H
EFI_STATUS1 N3 q# A, R; b" g( R4 r8 S
EFIAPI5 O* p. C$ C8 q3 A# N' Q, l
MyfileioMain (IN EFI_HANDLE ImageHandle," X f1 ?" K0 b7 V
IN EFI_SYSTEM_TABLE *SystemTable)2 j3 g K; ]4 j' r+ x
{. B1 Q. K+ { S) c& |/ s8 d
EFI_STATUS Status;/ }" F& b6 N9 v9 w5 c1 `
EFI_HANDLE *DestAddr; 2 t, {* R' J) }! F" k
EFI_LOADED_IMAGE *FileHandle01;
) E4 s { c. m9 w. b; o EFI_DEVICE_PATH *FileHandle02; $ o* {* V& \: @9 C1 w9 o8 ?' L# s
EFI_FILE_IO_INTERFACE *FileHandle03;
4 G7 T8 {# ~( E/ |# l2 Z6 i EFI_FILE *FileHandle04;
# m2 l% s) y2 G% E EFI_FILE *FileHandle05;
6 \4 s7 q$ z6 I CHAR16 *FileName;
6 b1 P6 i% Z) }5 h. u% n+ } CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;+ ]# E5 l T9 d9 z
UINTN BufferSize = 8, BufferSizeKeySpace = 2;6 @# m H; _8 f& {: D/ Y
int Bit0, Bit1, Key_Space;
) V T$ N, U8 r
5 C" l1 h; p# W9 {0 P. S2 g# O. B FileName = L"NewFile.txt";
+ {( g) k" A6 j( s7 r" D Space_Key = L" ";7 X1 v2 g5 Z$ D( H, y; m
) J* ^) X7 c3 I, U9 h0 L' q) u# R
BufferA = L"ABCD";- O7 i- J4 y$ B" i- Q- S) Y+ F
BufferB = L"EFGH";! c/ J1 L( a3 o0 x' `
BufferC = L"IJKL";
+ B5 l& e* O2 Y6 U9 D# u; o
; I# L' E) M g' c( k3 C: K Bit0 = 0xff;' _3 D: h; T5 u+ p4 R+ I* h
Bit1 = 0xfe;% ?: i# N9 I9 {
6 [. z/ A4 c D5 i Key_Space = 0x0020;$ H- X+ d- C( `
{/ o- ?( x1 C- q" _5 a# ~6 s5 E
InitializeLib (ImageHandle, SystemTable); 0 y) A( l" L* A5 F1 ~1 M4 x
: K+ w; Z/ P, D' t6 Z0 b
DestAddr = AllocatePool (BufferSize); " ]" ~ r2 D& z! Q* n
% Y0 v( P. n: L% O
Print (L"Value of Bit0 is %x\n", Bit0);
& o7 j+ D! e5 m4 {/ J Print (L"Value of Bit1 is %x\n", Bit1); & T$ V5 y. V7 S9 V! F9 g+ ^3 w
! G# x! _* R& @
( s2 D/ B/ C! c' ?
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);; s: U- V( A1 o1 W& S; R8 P1 t
if (EFI_ERROR(Status)) {' W5 t r I) D$ z
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
. k+ F1 _- e1 m return EFI_LOAD_ERROR;, b% B( C) W; g6 z$ D
}
' \% I8 L9 X4 Z; Q, ~/ z, E$ v& u) i n# p1 @# p% ^' Q; q) U& G
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);* X+ N1 g) a r3 }
if (EFI_ERROR(Status)) {! a! P, `: Q. S* q6 }6 y" O; W) A
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
3 g# _0 F- L& H9 \ return EFI_LOAD_ERROR;7 o! }- C; Y, K) s+ _
} & ?2 u7 `. r6 n* a
% i: ^& t4 s9 C# q" H% Z
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
: G9 r9 [( J7 w* M T$ c% F6 y if (EFI_ERROR(Status)) {
$ X. Y I" I7 p& n Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);) }, e1 t3 ]; X* K$ _0 }) f
return EFI_LOAD_ERROR;
~3 N' Z i2 |/ p8 x! } }
% R# H+ O9 j# W9 P) u* x+ y+ |1 u* O: Z/ |) N2 i5 z; g8 Y, A
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);4 q. D4 d- b0 L! j; W
if (EFI_ERROR(Status)) {
* Y+ |$ h0 @0 z$ L6 d Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
, f# D8 q# q$ i& F/ f return EFI_LOAD_ERROR;6 W0 R' c0 \: ^8 C8 ], [; w1 ?+ y
}
+ N+ _9 H5 V6 G
8 y3 z$ d" J5 Y Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
# @7 M9 i B' \, v: {7 l if (EFI_ERROR(Status)) {$ j1 n8 U, r7 @( W
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);* M6 j9 g& z6 N. J& W1 V5 D
return EFI_LOAD_ERROR;
5 E% X) g- I# p; k; P }
6 p5 H) c7 p) ?/ o5 t
: ]* B8 s6 ^, z. }% ?! T Status = FileHandle05->SetPosition(FileHandle05, 0);
1 [# h# _3 h. }( {; m4 v) A0 F0 U if (EFI_ERROR(Status)) {
, [5 |- ~1 x5 O. q; ?. U7 h* \3 [( p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 [5 z O# n: |6 N9 w4 R- j
return EFI_SUCCESS;7 b' g6 ?- g! m. C# J
}4 w( t1 y6 Z( \6 ^" ~5 K% \
& M" ?3 c( u. G; U- ^4 S3 ?7 o Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);3 }6 V4 B+ F% a* a+ L# F( \7 s S
Print (L"File Buffersize is %x\n\n", BufferSize);, R! J3 d5 d, D: i# M9 M6 F
if (EFI_ERROR(Status)) {
) R) H |' [3 b1 | Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
% F# r8 j) s: B5 Q) D* v" m return EFI_SUCCESS;& L; o5 f1 B% K& S
} 3 j& ~# I5 i, m) U7 R9 u3 ]$ ^
: Q/ M! O& t8 t0 V; Y5 d Status = FileHandle05->SetPosition(FileHandle05, 1); 8 Q* r A4 {4 M3 _7 k6 G
if (EFI_ERROR(Status)) {
7 a! z4 e. G1 y- Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 |/ h! A4 b/ \* G2 R3 M( K
return EFI_SUCCESS;, ?& [4 f4 }: f
}8 m- e [* R) n0 B% N/ J7 z
$ [5 a/ q/ H B2 q, ?+ Z Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
) G3 l9 O+ r- N* y! T Print (L"File Buffersize is %x\n\n", BufferSize);5 x( @+ C7 f% F0 _' c2 d
if (EFI_ERROR(Status)) {9 H2 b% X3 E7 O4 y( W
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ P5 } F" D Y9 _
return EFI_SUCCESS;* A R6 L, k* X, U c0 c
} 7 b! L. M# T8 {
9 R7 ]8 e. [8 |$ Y4 D
Status = FileHandle05->SetPosition(FileHandle05, 2);
1 K6 N7 r3 h+ b5 M if (EFI_ERROR(Status)) {
. J# M2 u4 F8 |8 r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; c+ a4 w: G+ e0 ~8 P, s return EFI_SUCCESS;
- B- Y7 X8 K9 w6 a, v- W4 d* n K6 E }
) E* ~9 h+ s2 E+ \. b8 R8 b$ @4 r9 Z
0 n% ^+ D0 t4 f U' S3 a Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
, N3 b* Z4 W$ ^' H* h- i Print (L"File Buffersize is %x\n\n", BufferSize);
3 Y8 W2 o: v/ [3 u& t* o if (EFI_ERROR(Status)) {; w. X8 j: L6 ]0 F; z) q& f2 x8 m
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);5 b2 \$ c4 } k( l( b
return EFI_SUCCESS;
* u7 A* H9 Y* G- G0 E) | }
4 q8 C6 w; O# W% E8 P% j1 j% J3 k
1 j# h' y* B/ v+ M! m//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
0 d, j1 t! s. v1 W. N* p Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
) T7 z; ~4 J' i% U Status = FileHandle05->SetPosition(FileHandle05, 2); n& R+ T" o) [5 w+ {
if (EFI_ERROR(Status)) {( t6 Z" {3 y4 T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; Q" I# } Y/ u) u7 q% O
return EFI_SUCCESS;
/ p+ {$ k {" k- p( C% \ }
% L& f9 B& `4 u& R
6 e, v- d' ^3 l3 V Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
; I# A1 Y8 c$ a( e: X if (EFI_ERROR(Status)) {3 e! X9 L: a! O! O0 V
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);5 k& |% E+ I' V7 `. @3 p
return EFI_SUCCESS;
& B$ ^6 ^' F. D! N( u. O F }
. y+ o. g% W2 {6 Q( R Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
" f* ?" C: u1 i$ M/ ~//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<) I. L# l& K; b- V# A( h) v, }
) @+ t# l- U2 s- M) D* A0 h# Z. e
Status = FileHandle05->SetPosition(FileHandle05, 10); ' R2 A7 V5 c4 P3 o6 Z
if (EFI_ERROR(Status)) {
, S! M p9 w1 t Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 }! W: ~. f. `) @3 \
return EFI_SUCCESS;7 `, u- K5 E! }/ ]: i4 M
}
( }% c2 I5 |9 e" u4 Q- C
* w1 E* o1 T3 j& D9 b: B+ Q Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);2 |+ ~) y, K4 ~) W c' c9 l. M
if (EFI_ERROR(Status)) {0 x# {9 m' N2 J5 l1 G. b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 L1 B# |# W8 w: k, @( b+ A return EFI_SUCCESS;
7 H8 j1 d" K% ]" `( M4 y }
5 m* ~" i. r+ L& Y# B% N5 Q$ k6 K
4 Z# @8 t8 U1 [& D' k7 S Status = FileHandle05->SetPosition(FileHandle05, 12); 5 s l" F# J- O" O) F$ e9 ~" H
if (EFI_ERROR(Status)) {
2 Y. _* B4 z9 [) V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 f" j4 i. m2 u& ^- M return EFI_SUCCESS;0 V5 s* l- H, J, j
}. Y4 V% F' t) R
( |/ v5 ~# f9 ]; E8 @7 q5 O: y
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 h( X- b. |6 z; s if (EFI_ERROR(Status)) {
3 H" C% w* M6 z8 O8 L3 q Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% {* s& F2 o: S return EFI_SUCCESS;
. B6 Q/ w( R5 b' j }
4 J2 Y( K$ c/ [
7 k; ?6 n+ t5 v3 J; H! {( c2 }1 _ Status = FileHandle05->SetPosition(FileHandle05, 14); 5 ]3 i4 _- Q: t d+ }: M, Q
if (EFI_ERROR(Status)) {( f0 g! K8 N# z
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; l7 }) S" `; i$ o return EFI_SUCCESS;6 A( S1 O4 K" v/ }- {
}( W* v" y- Y3 U% O# b5 T
8 J0 D; s( h3 K& J' k; L. l1 o Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
' P |& B* r5 T% [ R) t/ G( s if (EFI_ERROR(Status)) { L6 v) o, z- m% G
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ b7 Z& j! ^$ M2 R$ S0 J return EFI_SUCCESS;3 D0 o. m& W1 A% O. o4 K) `9 y' l
}0 ]! j q+ ?; s6 I! n d8 |5 [& U
/ e& A5 u, N* ~' I* Z- Z
Status = FileHandle05->SetPosition(FileHandle05, 16);
5 s8 Y( O7 d5 o4 V& d if (EFI_ERROR(Status)) {! m- G2 r6 F" m
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# M7 E( a- h7 x; {+ }$ r2 p$ d: |$ H
return EFI_SUCCESS;
( r4 L, B7 x' P' O! S; X: f' I }/ D1 z. {" H r
4 H, e8 L2 x. ], h$ |2 @
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 ?3 |) ~" r7 O0 Z3 `. Y$ e if (EFI_ERROR(Status)) {
2 ~9 O7 E. i$ A$ E( g" G Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( L) }3 g+ e6 P' T2 W% n; F$ T return EFI_SUCCESS;
6 s2 y0 I& {1 K$ t+ f* ?. U }
" u7 D0 P0 t. E- E, @7 d0 |8 I# T* ^: R# H$ K, u; V# i2 n
//---------------------------------------------------------------------------------------------------------------------------------------------------------------+ B* P) {; d" W: p& {
2 P9 E8 v/ M+ n3 K% l' @8 O H4 y
Status = FileHandle05->SetPosition(FileHandle05, 18); 1 p& X/ }0 r+ |5 ^
if (EFI_ERROR(Status)) {
/ F+ W- p% L$ k6 M; V; G+ a$ r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 e. \( }$ j) o& j
return EFI_SUCCESS;, T; q$ c+ N. ~9 s, X
}8 I! |9 K9 v) h0 u# @% `+ y
! Q3 i7 P4 l+ \: ?3 W
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
5 H b* X) @, P C0 X if (EFI_ERROR(Status)) {2 f% b& q' c* Z
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
S2 I( w; E1 p' h D* h/ { return EFI_SUCCESS;4 Z. i4 @# D) ]2 g0 }0 Z7 o @
}
1 [5 p( U2 G5 x5 ]- \0 [+ L( ` 1 l; U! O6 E) Q2 n
FreePool(DestAddr); 9 d4 A3 I; O; c6 o+ _8 p
; z; A, H/ m5 g1 X5 K Status = FileHandle05->Close(FileHandle05);: \. ?5 ] v1 d# x3 F% l7 O `
if (EFI_ERROR(Status)) {3 R4 B: A7 l6 r5 g
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);$ `7 X) `9 q8 q* g
return EFI_SUCCESS;
6 m0 K( @7 S, U0 P( Z3 r% u& ?! z8 I }
' q5 B& s ^2 n/ ~2 f! s' r6 O
# U3 T" h: m1 f8 Y Print(L"File Name = %s has already been created.\n", FileName);1 K' M6 ]; \ f( Z
1 {9 W3 [- A2 x. L8 q1 Y% A+ x) S Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
' ~" T W* ^; V/ a9 [ WaitForKeyOrReset();6 N4 k4 l3 l" G# z% `
$ m* M( k) I$ [& ] return Status;
J; m+ |0 L, Y} |
|