|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*) ^; _+ J' S+ E4 f2 `. a+ E
* myfileio.c. h7 F% K3 C" w/ `; F* E
* Apps
' m! n5 x) x" W) _# s, [7 y */
; b, z$ j# y9 o* ^$ m, o0 E" F
! U- B2 ~( @; F4 F8 _#include "efi.h"3 N1 l4 o% t, P& L, f, ]& @' m
#include "efilib.h"* Y% P& p) f4 }) X- G
6 k3 {7 k( b# s3 V( D I#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE2 L2 h; h* k1 @* P* C0 ?
7 T6 l) m( B( r) Rstatic EFI_STATUS WaitForKeyOrReset(VOID)
! m$ y3 V5 z1 m{$ s! l% D, O' B0 i0 H* g
EFI_STATUS Status;
5 ^7 ]7 z& |' U* ^ EFI_INPUT_KEY key;
1 U& V; I2 B1 e5 i$ \5 H UINTN index;
+ A; @* O/ j3 O3 c) I9 l- p7 }" q
8 k& } ~, q7 O2 e2 E& V: J" P3 y for(;;) {' t' C2 d. L% n* w! U9 J! F( q
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);& h8 Q0 {9 g7 d; Z4 a
if (Status == EFI_NOT_READY)5 q& \6 G r- J5 C( |! [
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
1 O H; C# Z( ]/ G9 Q0 J5 A else5 s! W2 v# m9 b7 M( y1 j6 M! t
break;9 W8 s, E0 F1 w0 u8 p
}
; v) r% m9 M# |/ y/ O' w( J if (!EFI_ERROR(Status)) {. p0 S, s# L9 n7 E. k7 Y
if (key.ScanCode == SCAN_ESC)& U0 D' X; \2 S7 b
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
/ O7 C4 _( H1 y3 T( v }5 V4 x" Z, X( e4 n
' H& P6 C) J8 h$ X* }( w return Status;
5 \% |9 X, v/ f6 z$ z# H7 G}
3 m4 U, d" K, s5 \5 J
$ I# g% E8 {! |EFI_STATUS
& s$ I! A3 a8 M2 \) _5 f; Y* yEFIAPI
# H5 X# n3 i+ jMyfileioMain (IN EFI_HANDLE ImageHandle,
9 L+ h/ k+ u' S% j8 r0 _ IN EFI_SYSTEM_TABLE *SystemTable)3 h$ z9 c- d0 _
{+ \7 z+ e# Z; A/ `, J2 U+ f5 w
EFI_STATUS Status;1 z- ^7 F7 @- V2 i4 ~0 a
EFI_HANDLE *DestAddr;
: W! y6 a( O3 A+ K1 W: v EFI_LOADED_IMAGE *FileHandle01;
4 T, z7 N1 ~. }' ` EFI_DEVICE_PATH *FileHandle02;
1 b9 o( L3 R& m9 p( e4 S EFI_FILE_IO_INTERFACE *FileHandle03;
" M) s4 U- ]( y5 p& w EFI_FILE *FileHandle04;
$ x0 e# ~( F2 i+ c5 \ W EFI_FILE *FileHandle05;
# \, a$ M5 }6 o4 h2 R# ~ CHAR16 *FileName;+ Q7 o% h J+ B2 M9 W; M
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;- D& Y- {2 ~7 p y% N: z. O
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
& j/ O: R" K" ^1 \ int Bit0, Bit1, Key_Space; 0 H0 {' g! Z% s/ y
! {1 x6 O; M4 [( c/ \
FileName = L"NewFile.txt";1 }7 c6 G [5 I# z0 \
Space_Key = L" ";" g6 K/ L0 a7 u
0 a$ L; @ C3 u6 h9 }4 ]$ D BufferA = L"ABCD";) Q* I$ a3 z) n* V/ A
BufferB = L"EFGH";, E# k% b$ E: Q. y& h J K* T
BufferC = L"IJKL";, ^7 U& S8 d+ V( M
! @8 T [- C: Z S/ I7 A& x0 k1 } Bit0 = 0xff;. t. D5 {) x+ G
Bit1 = 0xfe;
8 S; p) h7 D/ B+ h
8 B* T& M% f: C/ O$ B/ { j# C T Key_Space = 0x0020; ?: T |. s3 _; q1 b2 h
: s( H: @% j# v4 M0 O2 }0 o InitializeLib (ImageHandle, SystemTable); ) U- \1 e+ e# R$ ^
& m4 t$ E; t- L5 N) A DestAddr = AllocatePool (BufferSize); ) w6 y& D0 r( R; H# g/ P
+ ]" c. N! P+ W8 Z) M' ^4 c Print (L"Value of Bit0 is %x\n", Bit0);
% |! F) N/ @3 h6 S Print (L"Value of Bit1 is %x\n", Bit1); 1 D+ g7 R0 T U( j* O+ y$ k8 a& A7 \
9 I. T0 G/ L' a2 R2 o- V" p5 o
$ q* ] l& j* w- N# l6 A, F
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
- L( h- ^3 L9 T, L B if (EFI_ERROR(Status)) {
% o. u8 Z# S2 Z0 s( \8 A+ U Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
2 V u) ] `/ X2 ] return EFI_LOAD_ERROR;
$ L& w' M' J2 \0 e/ Z: {$ _ }( k! K1 n8 X+ l+ P
2 G+ Q0 i2 ]) R
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
. s' g$ n8 p Y2 }' |5 z0 x if (EFI_ERROR(Status)) {
8 {5 h+ u3 L6 S Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
* K! |% r% h0 o# o& g) E return EFI_LOAD_ERROR;
- h' z! h& j+ R* ^2 n* k+ i+ n& a( X1 C ^ }
. t9 m1 r7 g. X+ ^/ e7 B. N
+ q# R* x- c# q5 m/ @8 Y/ f Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);* d* l1 N8 [: v2 m; Q! t5 g) B0 K
if (EFI_ERROR(Status)) {) {! s( }6 |7 H( e
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
' U9 O7 K$ o- Q- q return EFI_LOAD_ERROR;% {2 ^1 W* E# k5 `0 E7 m4 O1 [
}
$ T0 V6 m J5 ?1 F( s( [8 [5 [. o! ~
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
1 `' i6 G& u, d* V9 F$ R9 v$ w if (EFI_ERROR(Status)) {% ^* I& _# b Y9 t0 p$ @
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
$ t8 Z' r" ~, K7 w+ ]+ k) E return EFI_LOAD_ERROR;* G H1 S6 D' }5 T6 \' H v
} & M- D8 c# Y* I( i7 q" d6 a9 B
, h4 b! m6 K% t9 m- C+ l
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);0 O" T: A0 A7 _ h+ m2 y% O
if (EFI_ERROR(Status)) {
+ Z+ D% i7 T s4 p3 n1 H3 d4 u Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
8 ]1 W+ J; G B: \6 {! ^0 { return EFI_LOAD_ERROR;
& V6 G g6 U6 W1 e" Z }4 R4 I( g9 ?3 P7 s
8 E4 p0 }+ x W Status = FileHandle05->SetPosition(FileHandle05, 0); / h7 T8 ]( |3 x4 P( ?* [
if (EFI_ERROR(Status)) {2 d- p2 K% w% W2 S" M% S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 Z1 P: N, {) L) t8 b
return EFI_SUCCESS;% T8 E: y, o; }6 ?" a/ x6 h/ h/ l
}. c( \/ J6 ~4 }$ ^9 t9 a' V( b( y, t
% [0 F6 b' @9 Z% E: c8 ` Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
! Y" D& z- Z" `, i; J8 q* j Print (L"File Buffersize is %x\n\n", BufferSize);/ B' D$ Y. m* h
if (EFI_ERROR(Status)) {
- N8 B, t% y. }6 Y Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);8 R7 y2 m( L: D* F
return EFI_SUCCESS;
9 B$ a2 d8 u! z4 z6 F3 q% [9 x8 y0 L }
) `* W. Y, ]% X0 c s, y0 E7 ?/ F4 Y
Status = FileHandle05->SetPosition(FileHandle05, 1); " g: b0 O( Y* v/ R. I$ c
if (EFI_ERROR(Status)) {
5 r/ M. E. l x& i2 H5 |+ h& [8 ]8 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ K5 j! H$ y% A8 A return EFI_SUCCESS;3 L) b+ |9 a, ^% K$ K- T3 W
}
( W O$ {! Q, [: c: s6 w
8 V! a, ^) B5 k Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);8 B; g+ t8 z2 U& P3 P
Print (L"File Buffersize is %x\n\n", BufferSize);
6 e' ^1 m6 U8 J4 B& Q! D if (EFI_ERROR(Status)) {. i1 j" |( W- L4 o9 U
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);! H! v5 W5 m% R, I6 c& j1 m
return EFI_SUCCESS;: p) o; m. I" l! B
}
( W4 X0 ?4 A4 e! J5 I* G" K + K' X2 r# u K% H2 S4 q
Status = FileHandle05->SetPosition(FileHandle05, 2);
1 C' E- m+ m( E+ a. b* n if (EFI_ERROR(Status)) {' h; F- W( n4 K. I4 |' y. W2 D2 A
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 j9 y& u x% t/ J0 b( D
return EFI_SUCCESS;5 u2 a7 U/ _$ T8 [. Y# V
}; B/ f2 G1 `6 ?% l
7 G5 }) {8 |. H6 ? O Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
' U6 G7 l' x. L3 I8 ~ Print (L"File Buffersize is %x\n\n", BufferSize);
8 b/ s1 A; j1 q u* N: F# H if (EFI_ERROR(Status)) {5 Q- ]6 X! y2 o t+ [. [, @
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ h1 P* B- D2 h" Y; @( y
return EFI_SUCCESS;3 p6 u u# n$ k9 l
} & s Q# D' Q; e- H
: M F' x, ], c//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>/ |" _4 T9 J, o# {9 r0 e/ P
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);( t I3 ]5 P' q4 k9 N/ ]6 P& S$ s
Status = FileHandle05->SetPosition(FileHandle05, 2);
! W- P t, ^+ U. ~$ s if (EFI_ERROR(Status)) {
F1 A3 x4 V2 c" \9 ^! h% Q* t Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 x. p) d( `* z d return EFI_SUCCESS;! c$ C/ g" b6 X- l9 f5 w
}
- d. R+ J: Z4 R# S# I5 h3 }9 k# M; C: L
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
" f- S) M- ~0 l8 _ if (EFI_ERROR(Status)) {
/ u) U7 f8 _* G7 g1 p Print (L"Could not read file, FileHandle05 err code is %x\n",Status);# B$ }$ k+ ^$ e4 v6 }6 v
return EFI_SUCCESS;( n* y/ b. b; B0 I: p" H# r
}
8 d* a; H/ a% y' ~& P6 ~+ L Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);. i4 M& y: I5 ^2 Q4 S4 A H
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
' ?3 E+ u' Y+ q6 {
, Y2 C! I1 [) x( }, Y% O Status = FileHandle05->SetPosition(FileHandle05, 10);
+ Z; ^. R, R: _, r" B1 n if (EFI_ERROR(Status)) {
! u0 H+ j/ A7 V5 Q/ @0 j% H) s Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* _- }: n5 ^5 k. Z( c* l return EFI_SUCCESS;- ?5 Z4 r9 Q$ O+ {1 `( _* n0 O+ N8 f
}0 L' N5 f4 o. x! u
* X& n2 V+ C; j$ i M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' @% l4 m& G8 g0 Q! s) }
if (EFI_ERROR(Status)) {9 D# c) u0 L6 q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);3 C2 c% e' X. u( i
return EFI_SUCCESS;
$ ` m- p/ u% V6 p+ o+ ^5 D, W- M& Q } M/ k+ J- ]0 L3 S Q! |
; |+ J& Z9 l N* ?- T
Status = FileHandle05->SetPosition(FileHandle05, 12); 4 Z& Z" a5 b6 F, v( j# V
if (EFI_ERROR(Status)) {
4 f B& m( p! x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% A1 l2 e& O1 J' F( g6 f$ ?5 ?
return EFI_SUCCESS;
9 r; T! d2 Z, U1 b ]1 \ }" \% v1 [, H# f' w) w
. X6 K) g1 X9 F( ~ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ ~; p4 c1 q+ ^
if (EFI_ERROR(Status)) {" P5 l7 f- k; I6 f# V
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; {* ~+ @# }6 h* e, e& o, d
return EFI_SUCCESS;
7 G# e- n: A7 H7 M# ~7 d$ j }
B$ d, }! |! A5 `, I$ M. T" _6 r2 n/ c3 L. @0 X& V
Status = FileHandle05->SetPosition(FileHandle05, 14); & g! m( x4 K* d) g) P
if (EFI_ERROR(Status)) {' j0 ~8 ~* J( w% i) |: P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 e& v" \, W3 ?* a3 _, m0 i return EFI_SUCCESS;# P4 q9 g8 ]( F! q1 F* f) T
}' a. I+ w# k5 b. A
, i9 O- G& A ?, |/ o! c( k2 S Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! |# o* q2 X }0 w0 M4 _& I A
if (EFI_ERROR(Status)) {
3 W( h/ b" S. z, k, ~. j1 y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 X' `. D! ^- e6 [! Z return EFI_SUCCESS;
: v& r! P0 \" P7 Z( ^2 \. m }% f, y9 b; P; x
4 ?7 ^% D' S3 ?' }1 G
Status = FileHandle05->SetPosition(FileHandle05, 16);
2 g0 c Q. ^% F: X if (EFI_ERROR(Status)) {
. ^, v2 f6 m( u7 m# F2 [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ v: V6 Z! j9 z( m& X$ j- L
return EFI_SUCCESS;
/ c/ L9 B/ x+ r8 {% i. l& h }
* b; M7 \- H4 \8 R ! h4 J9 X8 C% d% X2 U
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);- J5 f# d$ V2 p1 j Q
if (EFI_ERROR(Status)) {
; x/ W% v. B2 ^( a: J0 N Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) Q. R* I; D5 Q: C' @( C% ^4 w2 E
return EFI_SUCCESS;
; ~' r$ S {% A3 n3 P# Q; A0 Z }
) L2 ?9 f! g, k2 [- C2 u+ T* Z" H; J' U1 C) b7 v% N. y
//---------------------------------------------------------------------------------------------------------------------------------------------------------------" u3 q, r. x- Q; Q z; t
* D4 Z2 i* F: D) E, U
Status = FileHandle05->SetPosition(FileHandle05, 18); 2 V" }7 i5 }& g! l' A' [
if (EFI_ERROR(Status)) {
) @- z# w: S( e, C( c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 z4 f- z; W* e1 O* [ return EFI_SUCCESS;
; i+ x: `! r' S, l _1 u5 @ }
1 ]( i' _6 m4 ^" B# x$ b& M1 m( j, ]3 B* ^ : s( ^6 k7 Y* r9 }# }2 i) i2 x0 c% F
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);( R0 ?. h( u6 P. n* P4 X G
if (EFI_ERROR(Status)) {: ~1 o* f M* V2 D6 f* N7 W
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: X- w! q! G9 b9 s" _
return EFI_SUCCESS;" M u: W7 V1 Q/ H3 g
}
( n0 x+ ^) h- r& N5 R
& ^, q1 w! w' w# ] FreePool(DestAddr); ; g% O& R6 c9 z$ [) P
( Z9 X( T1 E, b! G& a9 s! V
Status = FileHandle05->Close(FileHandle05);
- V4 n1 p" @% x if (EFI_ERROR(Status)) {
; R, c0 ?# D' u3 O5 g, b Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
/ Y2 n V; r& {, n2 F( q1 w, `1 N return EFI_SUCCESS;, V1 f" l: Q% @: S0 K: G; `
}" c: c+ S) \/ M: b( W
$ g0 f3 J% H* E2 [! Q. e% y) z3 T
Print(L"File Name = %s has already been created.\n", FileName);
5 K, g2 L+ t* |! N2 }3 m- H1 R3 D# z& k
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
+ ~0 ` W" R3 e, G WaitForKeyOrReset();
0 C7 X& Y7 f" F/ H# j, E
) Z( y% T4 f3 u% B" @+ k+ p7 t return Status;4 t( O8 d2 x( ~0 q; m2 O& x
} |
|