|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*. b7 b ?, `* {2 S. {7 M. I2 X
* myfileio.c8 r4 \, y' l3 t2 L+ {
* Apps1 v9 \, g7 D- ?4 P8 s/ u* u
*/3 @ [& A- I1 |# }
# x9 B3 j! m: B" J2 y2 B
#include "efi.h"
' \* a6 o' v- M, i- r) ?#include "efilib.h"1 z, t9 S' l6 T! y& q! h7 p
9 [! v, r! M7 G+ ^, e3 [0 W6 A- V
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE. W0 {$ h0 [5 p, P/ A/ G* ^
( z6 V m) K3 B' Dstatic EFI_STATUS WaitForKeyOrReset(VOID)
. [7 @' i' e& J1 e. N{: H1 \5 ?- h# @* F5 s3 Q
EFI_STATUS Status;
$ x& J& M2 e9 t3 S l EFI_INPUT_KEY key;1 x3 A0 h1 T* d( D& w
UINTN index;2 I( W. u2 D, L& e8 s: m
; p1 a+ p- E* {# Y
for(;;) {; ]9 I0 H/ M' R/ W2 E6 S
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
' V" [# a, `3 Y# `5 V if (Status == EFI_NOT_READY)/ z) ?& N K: p0 R" ]
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);. i* F$ \, g) v: e& q/ a
else+ s5 X$ ? ~2 `, c8 G' w# \" U
break;$ f& A2 O3 L6 y6 ~* e( g z. S
}1 q& W+ w8 T2 z
if (!EFI_ERROR(Status)) {
: B5 |6 M0 @/ \8 l' u2 d if (key.ScanCode == SCAN_ESC)
( ?3 \; b8 d3 ^2 b) {% D RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
3 W0 d) H, s0 f/ g; A }, D" V& ^8 C M$ ~1 t" p
; w5 ~6 d, G" m. N6 W return Status;
- u% X# Q2 C" q4 ^}, b3 K7 y* s* C: R
: J" @" @' P1 s9 q! A6 A6 N2 w- V
EFI_STATUS3 K$ s( t& z% z
EFIAPI U2 F& V7 D: Q3 c& O# W
MyfileioMain (IN EFI_HANDLE ImageHandle,8 j9 [/ [) h N0 |' D" q3 s
IN EFI_SYSTEM_TABLE *SystemTable)
a. d5 q8 `" q* b* E{+ M/ W! G0 |( H
EFI_STATUS Status;
K% |: ]( o' p) ^' h* V EFI_HANDLE *DestAddr; $ k1 @4 Q0 K$ j8 p
EFI_LOADED_IMAGE *FileHandle01;
) L7 s; n& o9 s8 {6 n EFI_DEVICE_PATH *FileHandle02;
, j: g9 ^' N$ l; {. q1 G8 \; V EFI_FILE_IO_INTERFACE *FileHandle03;; ~% G' |. J) `
EFI_FILE *FileHandle04;7 E: Y6 j3 w. m% `0 Z
EFI_FILE *FileHandle05;
0 u+ W8 a& z' p! \1 y6 o CHAR16 *FileName;4 {0 P, Q/ }6 W6 t! k$ `% ?: c
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;$ [5 e* h ] V) S4 i( B! k" O0 g& m
UINTN BufferSize = 8, BufferSizeKeySpace = 2;" U! p* h- W9 x i3 W& c
int Bit0, Bit1, Key_Space; + e. f/ Z3 Q! p% C6 d. q4 X
, W N ^1 n3 W' G
FileName = L"NewFile.txt";
5 Z2 m- \) h* k& n Space_Key = L" ";; m" {( c3 d2 w+ `0 I
2 ?$ A7 ?4 n; }$ n: v BufferA = L"ABCD";
& k( @/ W( D1 m BufferB = L"EFGH";
/ [& ]' ]5 Z$ I9 h: Z0 Q: _# B BufferC = L"IJKL";- j1 i" n# K5 F4 d! A. r+ S
7 H7 V# t; }* [7 f Bit0 = 0xff;
4 H, Z. v! y: \6 p" L5 A Bit1 = 0xfe;
! {# C/ s d& c2 I P q
7 S3 `4 ]+ N y& T0 X" k Key_Space = 0x0020;, b6 K! U# U: Z
, Q- M- S, B% S9 j7 x InitializeLib (ImageHandle, SystemTable); 9 |0 J+ F$ v) w/ d9 C1 _8 g) s
2 \% b {* ]% p3 c6 B
DestAddr = AllocatePool (BufferSize);
3 p* T+ l! s; S# e& t, H+ F
" ]3 f% A/ h" i- ~! a" N% }% a Print (L"Value of Bit0 is %x\n", Bit0);
/ T+ X% a: f. z Print (L"Value of Bit1 is %x\n", Bit1); / H, c M) W& _; G4 ~% E- ?) }
) I& [* V( O" F9 d! J) m* R5 d
. R0 x6 B( z3 `7 {# O. q- D
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
6 t/ [7 ?. f3 C4 q5 u if (EFI_ERROR(Status)) {
2 r) P6 G, M% e& E# O8 F Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
( c1 m, t2 |# y b3 K; f return EFI_LOAD_ERROR;7 w. x- _7 E. f/ m' W9 j& c
}
% x. i# x* O; ^2 h B' ?- H% J! F- n6 {* ^1 Z/ C- H x7 }& \2 B# \
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);/ a) K7 V* n9 n
if (EFI_ERROR(Status)) {
# f! [5 Z0 Q, q7 j Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);: \7 U: [% k# U5 ?9 [
return EFI_LOAD_ERROR; T7 T9 m) q* [5 `% U
} " _% [9 C% ?; h: A
* U+ Y9 F, t A1 f& Q% x, V2 U
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);8 i! G, r1 @# @! i/ E8 x
if (EFI_ERROR(Status)) {
! @ k. I# J- @ Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);. q2 J. k/ K, c( q" r
return EFI_LOAD_ERROR;
( F, M0 y: {3 M' u; f {! R }
$ `5 C4 m# k1 A, B# b
+ r1 d& O+ x" } Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);, Q5 u2 U6 }% Y }1 {" V: Z, T
if (EFI_ERROR(Status)) {: d* K9 R- M9 h$ X3 R L9 Q" ]% n- U
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
; o: h% \" K% } return EFI_LOAD_ERROR;
- D, O+ f- k' c9 F7 [ } 5 W3 O( e" F8 y$ I" L' ]1 n9 {
% Z! C8 f( |: w- [ Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
0 J+ s8 b( \# J- Z% {: |" @& }$ p if (EFI_ERROR(Status)) {
3 ]# B8 m2 C, |& f9 T6 {, i Print (L"Could not open file, FileHandle05 err code is %x\n",Status);+ C9 M5 h4 Q% r# \5 k$ l
return EFI_LOAD_ERROR;2 P% u' m' j* U0 a
}
/ S( b4 U& ~. J% u% [ - X3 [' C/ X$ n2 h
Status = FileHandle05->SetPosition(FileHandle05, 0);
! g. S+ C/ c: O if (EFI_ERROR(Status)) {3 ~, g$ w1 f0 b+ N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 j% K: x, ~0 a7 l
return EFI_SUCCESS;. p! F& H2 i6 Y* W! y) |7 C) A7 F1 z
}8 m: |! d$ l X
; n$ {0 G; b: Z x2 F. n Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);1 `9 I5 _! e' K
Print (L"File Buffersize is %x\n\n", BufferSize);/ z5 E3 B$ h4 h4 G! K
if (EFI_ERROR(Status)) {
0 {% U& u( M' s, i/ K Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 j8 c K. ]. m2 K* | return EFI_SUCCESS;
8 C. I, o( ^& `) a } 9 K$ M) W! o" p+ B6 k7 E1 Q
4 ?+ V0 S% o9 C# a
Status = FileHandle05->SetPosition(FileHandle05, 1); ' |5 @, `. f* ?1 h
if (EFI_ERROR(Status)) {- J0 v, b0 _6 C
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 P- \" q8 [5 q# c
return EFI_SUCCESS;; f6 @7 q: a, q$ Q$ G3 l: I/ j
}
/ N$ t2 F3 H0 F0 ^) B
) R6 l1 A& C( a3 c Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);/ n: Z2 W" X( D, K
Print (L"File Buffersize is %x\n\n", BufferSize);" R% F A3 q7 n0 P! {
if (EFI_ERROR(Status)) {
& _3 ^/ j" r0 j+ D* E& h q" h* q Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 M* U: O/ C9 c- ^ return EFI_SUCCESS;
8 ~; A8 ~. w4 A( k: q }
! u* w5 W1 U M, J+ q9 O8 @ ) Q+ w! A7 l: m0 I2 y- X
Status = FileHandle05->SetPosition(FileHandle05, 2);
( Q& o8 t" Q7 p x8 N- s( w/ ? if (EFI_ERROR(Status)) {
6 ]# x ~4 j; j( d Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 @" D: E$ }( [) q O% n, R1 ^6 ^6 l
return EFI_SUCCESS;* E. Q/ p4 k0 y9 ]7 g3 g% x' \
}
- T" s! C/ t h; h/ n7 P" B . h. b3 V, o, P& s& h/ m
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);0 W& \3 v# R; P, u2 O j" p6 G
Print (L"File Buffersize is %x\n\n", BufferSize);
$ Z3 c' `+ g0 f2 T; @ if (EFI_ERROR(Status)) {# u6 Q. J# T" |- ^/ | S; `5 Q5 L# ]1 j
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
+ c/ ]5 q$ U* p& Q+ O1 Q return EFI_SUCCESS;! _7 t* N5 O) G) K
}
7 K# K. X9 } e7 [9 t 3 r$ e# C: {/ C* R- I& k0 D
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
s( W- _# k+ C$ K! s4 D( m Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);2 y! w5 X% Y& M+ i9 h% }
Status = FileHandle05->SetPosition(FileHandle05, 2); 4 l3 _' R ^( F% P# w# Z
if (EFI_ERROR(Status)) {
1 x. y4 V1 Q: h1 x3 o1 G* o* ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( b: [ _& \* W2 f- U return EFI_SUCCESS;
+ b. Y' j& h$ x( H }
7 L- Q4 O6 t1 H( P0 H k6 Y/ O) ` ~5 [! t
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);* ^" I6 R* d! r) a. I3 P# @8 S( X
if (EFI_ERROR(Status)) {
9 a' S7 V7 ~ y Print (L"Could not read file, FileHandle05 err code is %x\n",Status);* _1 {" z3 {& A' X9 v* u9 b
return EFI_SUCCESS;; N: d% k3 L0 V
} & y' L# A8 Y* c% C% a k) @
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
) J. g: u2 n2 X# E//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
2 ^0 y( {. e8 Y
7 {; S3 w$ z& ~ Status = FileHandle05->SetPosition(FileHandle05, 10); , T: p% g: _# \# X& o; Q
if (EFI_ERROR(Status)) {
: `& d& z) B/ q: i1 ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 s- q+ B9 X! g. E return EFI_SUCCESS;# _9 T. T, f' D
}! E( O/ b! g5 y+ p
2 l0 n- w- D0 r$ g. `
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
: h G5 j! t3 C6 C if (EFI_ERROR(Status)) {5 x% C: }/ N" J
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 H% P7 k$ W& ?& h3 a return EFI_SUCCESS;3 N' k r* N t) l2 O( q
}
- l6 c0 m/ |/ I1 I" F* O) \; I, N$ ?3 L) P9 r6 p+ m
Status = FileHandle05->SetPosition(FileHandle05, 12);
1 ]/ }: l0 Q7 F( N1 y9 N if (EFI_ERROR(Status)) {
1 j) d! k. A" ?/ l) w9 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 @$ t" v# m# a
return EFI_SUCCESS;$ b. j4 N% @7 U1 s" E
}. L K3 H0 B A. [. t
' \8 o# Y/ g" e( |6 b* y/ A8 x
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 ~; S+ B9 p' |5 G8 ^, a: O
if (EFI_ERROR(Status)) {, G g, | S, w6 x& [
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) U! V2 j! \7 l7 F5 N! q return EFI_SUCCESS;
; s: v( t/ j7 Q9 v }# @$ t. c7 C; [4 {4 Y% L
2 F) T( d3 h9 a" Q; c Status = FileHandle05->SetPosition(FileHandle05, 14);
- ~* g# O5 Q! r" @ if (EFI_ERROR(Status)) {
) l. p8 r* ]2 C/ r# f6 M* j" n7 b Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 z5 s! O: ^0 I: O) H, q return EFI_SUCCESS;: @ b$ i" R) \2 X( y" V% ~
}( R; p* B, U$ c- z: s
. N: F" U1 n6 F+ f- g Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 m. Q) K+ E8 z/ g. h- |- t if (EFI_ERROR(Status)) {
$ c; d( w" f4 _- z4 K& f Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ @- ?8 n7 ~, R, T
return EFI_SUCCESS;! j. P: I/ E c& s+ a
}
6 Y: s" O4 M6 I7 i
& A: m8 t9 q- |% b9 V4 O9 h2 n Status = FileHandle05->SetPosition(FileHandle05, 16); / |# O! W! I) b8 v3 W2 [
if (EFI_ERROR(Status)) {
4 Q" Z" Q4 ?& c: e% D Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 X3 |$ y& x& c return EFI_SUCCESS;1 u. A8 `# a: u% x$ A
}. o4 C, O: A: F2 g$ D! w
" l- W2 U& {3 o4 a: w! t Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 T; i' c+ H( \0 @& d) {$ j if (EFI_ERROR(Status)) {
* m. M( I c3 ^+ [; F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& t7 `, n0 b4 p
return EFI_SUCCESS;9 p0 y* j/ ~3 W; S! E
}
8 W* `$ Z+ G* n z# K9 V1 Q" d! @5 q7 ~* C( i
//---------------------------------------------------------------------------------------------------------------------------------------------------------------8 O/ ^! R% n8 D5 n5 j
" Q. L6 x$ O. B; O- g6 y Status = FileHandle05->SetPosition(FileHandle05, 18);
! t& R; O* [4 } if (EFI_ERROR(Status)) {
1 @0 \$ p% z1 E2 n& I4 [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, _& P7 c0 T* u; b/ ` return EFI_SUCCESS;
& D9 G+ [- Y( y& H }& B3 Z7 V- K9 K1 g' S8 l" c
8 }3 \; a6 ] \; X4 q; p" } Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
2 [2 `" b" y: J if (EFI_ERROR(Status)) {
) l, p q" |4 W! { Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
5 e g8 K# N; _7 v, r3 q+ y' b return EFI_SUCCESS;% V+ G/ O' y' ^( a V$ q8 j
} 2 M' [ p0 ^/ h& j+ R
: Z- ~) ^. Y7 o# w
FreePool(DestAddr);
) u% c o- }7 t6 x6 s
' |; I1 E7 A; X+ z! t1 g3 D* z, |- H Status = FileHandle05->Close(FileHandle05);
. f. m% Y% K" }9 M/ q1 D( y if (EFI_ERROR(Status)) {
: e. L) u% g8 K# \2 t& ] Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
) d7 I; D/ K9 V) m- T0 _ return EFI_SUCCESS;
. a- ~9 E$ J0 o/ k6 H9 |/ [# V- { }8 v5 ^# I4 h' J
7 M, N8 W4 u" s$ \8 r6 |
Print(L"File Name = %s has already been created.\n", FileName);
$ g( r, g y, l1 d/ _* `7 b3 X+ U* I+ L( L6 D! X
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
; e! L. W9 o4 t2 M! f2 { WaitForKeyOrReset();
% L" C; j, ]7 T K( y
8 C& X c0 k9 {* X9 V return Status;
; }3 d* I5 Q" L4 ?/ Y} |
|