|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
. X8 R: J" q# O$ G! n. U7 G * myfileio.c
8 S7 a: s. C# x: c5 F4 W* `" g8 R * Apps5 n4 a& k% Q% K8 m& u% q& U
*/8 J4 e! M( T0 e
( |; c3 Q; p5 h* I#include "efi.h"+ g3 Z% v& B3 d& {
#include "efilib.h"( D; W5 c/ U- v! a
; S1 R( h; e6 r1 o! }: }$ p#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE. M) M& B _" `$ r
2 u+ I9 C& S$ B( n, ystatic EFI_STATUS WaitForKeyOrReset(VOID)0 t( R+ k, P+ @% H4 j5 }! V
{8 ?& b, H; H- C9 @
EFI_STATUS Status;
# z& D B' o, h2 T$ X/ d0 v EFI_INPUT_KEY key;
1 V" E& x, N. Y, u UINTN index;
# B' T8 a2 }, U; X) J9 v ; S. Q* \' v* K/ l0 I1 [! \
for(;;) {3 b$ q' K1 \ | ?/ {7 ?* A7 X
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
# p# H1 ]: U' F5 T/ r if (Status == EFI_NOT_READY)
; a" u* A l' w& g, k* @ BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);9 F/ |: p. H; O) N& F
else
8 I7 ^. K0 g% [7 W9 g2 r break;+ m" c( L$ B, H; _5 e7 u
}8 P& L& M' X, I
if (!EFI_ERROR(Status)) {
8 M, W+ P* E/ U' {/ X) U) j; u1 T if (key.ScanCode == SCAN_ESC)- W4 W0 P5 G2 v, ?6 ?- E6 h
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);1 Y/ X# r4 L! \" Q z/ w7 _0 i
}
6 q8 H n8 J1 M2 A6 Y+ a
6 i' N, M1 k F& }3 l8 e return Status;
; ~; ?; {4 y) y/ A}& f- B. x& ?8 r( E9 y, b/ o
! Y# ~3 i* j/ z! wEFI_STATUS5 m, f. J( Z( f$ U6 I
EFIAPI @) @/ G. [% J a. x
MyfileioMain (IN EFI_HANDLE ImageHandle,
+ w! W( _3 v% W2 O7 g: F. V IN EFI_SYSTEM_TABLE *SystemTable); j$ g' b* \8 Y, \6 y7 N
{
% V$ ^/ P9 z0 ^4 _6 @ EFI_STATUS Status;
" f/ I* v" q$ R( {. K EFI_HANDLE *DestAddr;
! A- g0 @* B. R% M EFI_LOADED_IMAGE *FileHandle01;
: @6 l& E$ s! a3 ^3 [5 I EFI_DEVICE_PATH *FileHandle02;
% J7 ~. o; C1 \7 ], L! r EFI_FILE_IO_INTERFACE *FileHandle03;
4 Q5 F& I& s. D/ R+ C* H EFI_FILE *FileHandle04;
3 i) q3 z% j& B8 H/ e EFI_FILE *FileHandle05;
/ T/ ~ ^, h' X$ c CHAR16 *FileName;
7 j; d% D& Y/ Z. C2 d6 h" U6 I- ` CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
4 ] |' P( W5 C0 }/ C UINTN BufferSize = 8, BufferSizeKeySpace = 2;
3 _+ v1 L$ F( T: B% V" O. ^# m int Bit0, Bit1, Key_Space; s( E% N6 L" P: ~) n
. }6 z( m# f- l8 {) T FileName = L"NewFile.txt";
& s7 g) I3 k4 t _& t Space_Key = L" ";- `3 A+ u1 L, @% g" e8 J" X
. M6 R U! j8 L
BufferA = L"ABCD";
4 V5 p) w( W! R2 N9 W BufferB = L"EFGH";
) W- W2 ]; i/ h/ X0 C5 p BufferC = L"IJKL";1 y! u: e$ G. Q0 u6 g, H9 `6 ^
5 Z( C5 k( y+ h' ?4 @ E Bit0 = 0xff;( y1 z* W2 U; J5 ] O
Bit1 = 0xfe;
! Y% q# s( N" Z4 e* t5 p. ]( `% Z; v
8 R! Q8 B& [9 c+ d Key_Space = 0x0020;' b8 A. g6 T# P) c
/ p- U1 }; |- ?) i2 S: x% o
InitializeLib (ImageHandle, SystemTable);
3 t2 r) D3 J& p
( e& _* k8 n2 c- }: _ DestAddr = AllocatePool (BufferSize); # ]0 P2 ?( j1 F* A7 g) E
! W$ F0 {+ H) X( i. i# W( q Print (L"Value of Bit0 is %x\n", Bit0);' v. t; i7 G" @1 a
Print (L"Value of Bit1 is %x\n", Bit1);
- U2 O2 ?: O1 B5 q0 \! ~4 O3 u# C# J5 _& Q9 A* E2 N" E
3 q: l: ?, y2 ?8 r# y Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
- E* _' v( t; W$ z if (EFI_ERROR(Status)) {
1 i; z9 A$ v3 z6 Q8 P* U/ I7 q* G Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
. Z e3 h2 O u* W! O& q return EFI_LOAD_ERROR;
0 a: d/ \, p3 Q4 H } O6 F. `. P0 z* j
~" g6 ?& m% w9 @( H, c( E Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);& g7 Y3 B3 _" M& d0 M+ p2 _8 c
if (EFI_ERROR(Status)) {
- O* b9 w' z8 K3 N2 K0 p# B Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
# R, u5 E1 q) a: | return EFI_LOAD_ERROR;/ A7 h5 Z# d; z' ^; O
}
" s( L+ h" j( ?7 U0 C% s/ } * f: ?/ v( F" S4 j6 s+ {/ {$ s( ^
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);, D! n& g1 j2 s/ M; t: {7 c
if (EFI_ERROR(Status)) {
2 i% L) M5 W+ | [" B Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
O0 {* W7 W9 x& L2 Y return EFI_LOAD_ERROR;
% T+ x0 w' J7 M) r& _ }
; J u2 t/ U R% f0 f
0 D, [/ K3 ^! Z0 @' n6 ~ Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);2 y$ |3 U8 t6 ?
if (EFI_ERROR(Status)) {
2 B7 G: Q" K7 F2 J# ~ Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
) M5 R* z, V+ k/ y$ N! W return EFI_LOAD_ERROR;
3 G5 G" f. o& S% Q# @* w }
; M5 h( u( }' K: J; ]! T. S1 { 8 }8 i; W( f/ {
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
' ~# z& C, e- M9 O" W if (EFI_ERROR(Status)) {
5 ^/ \0 V4 e" H Print (L"Could not open file, FileHandle05 err code is %x\n",Status);, j% Q- o- X( W4 V$ ^; e; r, {
return EFI_LOAD_ERROR;* Y+ S; z {' ~7 p2 f" C! W
}
2 V# n# u8 Q: s; ~4 ?
8 l1 q( s: P1 l: D0 e6 _! J Status = FileHandle05->SetPosition(FileHandle05, 0); ' _" {. U& F( P, L
if (EFI_ERROR(Status)) {( {1 i/ a7 O2 m( b; C2 b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. O" A+ M9 o9 U q3 ^: T; H- a
return EFI_SUCCESS;5 B4 J5 \0 A* t6 h$ Z5 h; n
}2 t' n+ a- f0 G; @
% i0 q1 K7 r+ i6 U: ~ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);& R3 |( k, C0 w6 T6 h8 w
Print (L"File Buffersize is %x\n\n", BufferSize);
! O1 Y3 w0 r- g( Q: L9 O8 H/ u if (EFI_ERROR(Status)) {6 G8 W N: t' k( I3 i
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);7 P$ ^6 r+ g4 y0 t, Z
return EFI_SUCCESS;
( n/ f/ G! g1 Y }
+ U. k: {( B) U: e+ f, w/ _+ Z6 Q3 Q( s; w1 O5 n
Status = FileHandle05->SetPosition(FileHandle05, 1); ' ^& R4 A$ x2 N* O2 ?0 a3 G3 b
if (EFI_ERROR(Status)) {
! M) O9 g# O/ O( n* E% }; m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. U7 ?8 X) J7 b. o, z9 ]* r0 t
return EFI_SUCCESS;
% o n; L4 c9 R, S9 m; T }9 u6 n4 S2 r" Z' t* F
6 l) c8 T7 W& k; l% h1 j, P Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# y- F- b0 B7 H. Q: D2 R Print (L"File Buffersize is %x\n\n", BufferSize);
2 c$ l; ] `+ M \ if (EFI_ERROR(Status)) {
4 B& N/ [6 R0 |# L. ^2 _ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& U. n* z3 K/ ]& m/ K6 V' Q5 ^ return EFI_SUCCESS;
2 o7 ^+ S7 y9 X- M8 S }
# ?4 ^. N0 x5 t; ^ @! J; V, s : `7 H. A$ b- {. j; e1 Y
Status = FileHandle05->SetPosition(FileHandle05, 2);
4 ]1 ~" g! d: v$ S; L if (EFI_ERROR(Status)) {
3 e$ c7 P- K4 r# M% V# L9 d Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, k7 @* t8 E7 W) Q return EFI_SUCCESS;% f% @2 D6 A( Z% ~$ r
}
; g9 I) i3 D8 h/ Z( Q/ J& J8 ^ : d8 H0 F, O( z
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);. W4 e5 |! M. F
Print (L"File Buffersize is %x\n\n", BufferSize);
# Z* j& f, Y' _0 C: K if (EFI_ERROR(Status)) {
! C( X' h5 e7 _+ d( }/ ? Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);) g4 E" @ q3 v% |& a3 n
return EFI_SUCCESS;
$ j" K& l j1 v) v% d! g1 k }
1 d E/ B7 ?# i$ b, k
, S0 W7 C; D1 x& x4 n6 s. S- P//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>9 z; J( p/ g* v, Y6 S" K+ ~5 h4 G; D
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);6 V# B8 ]) V9 ^. k0 Z
Status = FileHandle05->SetPosition(FileHandle05, 2); 1 x* j# f' t8 K, s& h- I
if (EFI_ERROR(Status)) {
: ]( E8 A& M0 H9 y: _, x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 j* Y% J1 H8 v7 K+ e0 a4 S; h/ E# G
return EFI_SUCCESS;- Y/ c& a+ F9 _8 m x# t9 w* [4 P
}3 ~4 t' C2 f: i. [: z
8 Q# D, I- m9 w% U6 Q" _
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);) n! r8 L. c5 [ @
if (EFI_ERROR(Status)) {
e0 E9 h1 [, Q+ S8 j Print (L"Could not read file, FileHandle05 err code is %x\n",Status);+ Q" z: Y" f: p5 ^8 a2 ~/ G
return EFI_SUCCESS;
! J% Y' g3 _4 `& a x0 Z3 X/ y3 K }
) ~6 E2 k3 S; H5 n Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
5 O) S4 W) i* \5 @//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
# G: ~, M! a3 T/ A8 {( t2 F; v: K4 _
Status = FileHandle05->SetPosition(FileHandle05, 10); ' ?/ l& l$ S2 B+ Y5 } V* L
if (EFI_ERROR(Status)) {
8 E% s5 u. H' f% `5 F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. w" h- e* G6 Q' Y& _/ E; d return EFI_SUCCESS;$ p' _' K9 ~* v! f! C; r& `
}( t& C( t2 t. w! {
- B8 r" T5 B# A E, }+ k! X
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; v4 Z& ~ | a& L% _8 p if (EFI_ERROR(Status)) {( ]; W. O5 R% w9 G% @7 E
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 s6 [; u0 w" m* n
return EFI_SUCCESS;
/ M: Z: F6 a( N2 \3 C/ u }' ^7 I& }" `* f6 z
7 q3 Z8 H& V9 Q7 c7 G Status = FileHandle05->SetPosition(FileHandle05, 12);
- }0 x3 L q. w& i! Z S if (EFI_ERROR(Status)) {
, @% P+ b( T* H5 e Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- e7 {# S2 a, u
return EFI_SUCCESS;' N* h4 Z) P; Q7 U2 _
}3 @* u; h6 o+ D# v/ {
0 O" E! `! _. h3 X9 C2 a- [# p- K Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);1 O1 j! r# b6 ?( E
if (EFI_ERROR(Status)) {
( ~- M) x8 _2 ~) V& R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
h) [( e% J) F! y3 J& L/ M m return EFI_SUCCESS;
8 B+ l/ J+ \" c) G/ A& g }
* W% _* f& W/ h, x* g% w8 U$ G0 @3 D8 |; |* I) x" H$ w
Status = FileHandle05->SetPosition(FileHandle05, 14);
% X% B, H1 ?9 N3 C; C* `- F if (EFI_ERROR(Status)) {
; m$ J7 {" ~- ]" n) d+ c* X$ }# e: @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 e, P3 s2 M6 ?
return EFI_SUCCESS;4 ?1 z9 r R# x) D9 Y4 S' C" M' T
}
7 ~& K0 V& h$ g
7 P) A" l+ C. x5 ~3 i" ^; u! G Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 Z1 G$ k+ Z5 A' I; r if (EFI_ERROR(Status)) {8 p+ X6 M3 s' x8 r7 f
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( O; r. ^* R! c& g+ j3 { return EFI_SUCCESS;! ~& j- c, f# a; P# Y1 l
}$ |. k! s6 R0 o5 W4 ^( p
4 c+ ?' I0 @: R" H( B: W
Status = FileHandle05->SetPosition(FileHandle05, 16);
. l6 q9 }9 c9 }. @" W- v* I) z if (EFI_ERROR(Status)) {
$ B; K8 n z$ c3 l: B1 n6 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* F7 n1 F. S# ]
return EFI_SUCCESS;
9 q% ?2 v3 y& q- w+ w }
2 w% z; T! _5 }; L ; B/ w: K- i5 L) j7 j0 L% |
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);9 S- r O( T& S1 t2 Y( v
if (EFI_ERROR(Status)) {
2 B+ x& q) W; U" E Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; Z+ C2 m( K/ Q0 [2 ^* J4 q; m
return EFI_SUCCESS;
. I$ g. I/ \2 G! R3 ^0 }/ w f }
. l% G3 v! ^" j) d+ j. R9 i8 C9 T
//---------------------------------------------------------------------------------------------------------------------------------------------------------------2 i$ } H: O9 O% q/ \
: z u1 h- p R9 R8 W0 U$ W
Status = FileHandle05->SetPosition(FileHandle05, 18);
3 b$ r8 J5 P/ k' B if (EFI_ERROR(Status)) {8 R" C: s/ h6 ]# B. ^" s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
q) s. V5 O6 ~ return EFI_SUCCESS;2 I( W! l$ O3 k# z6 R1 v0 z3 n
}7 ?* q1 S; U# z2 `
0 B# E7 e& ?6 {/ X- t Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);% y. N4 t. D$ ?2 _0 B* P* P
if (EFI_ERROR(Status)) {
4 g4 p: h9 F5 l3 y [" s+ e3 s Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! P2 p3 T9 N i+ d- y: X
return EFI_SUCCESS;" ?0 W4 ~7 b5 R$ ?4 |8 \ l
}
% N; V, I( Z* y, b- @ P! i+ H' Q5 | ) g' C4 I5 D1 i) J8 {' b7 h
FreePool(DestAddr); # ?6 ?- g" U4 L2 K- k/ J
" \9 {9 y, @/ C# K Status = FileHandle05->Close(FileHandle05);, x- s$ Q! \, S" ]
if (EFI_ERROR(Status)) {2 H1 C( N5 R" y
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);. ^) G& o$ G, o0 L$ x4 F0 a
return EFI_SUCCESS;# O7 K5 E4 B) O
}# N, |5 k7 s ^
1 A5 H) e& }/ {& Z Print(L"File Name = %s has already been created.\n", FileName);0 L. Q! d3 a6 B
6 P& l! P! I# G. T2 ^6 u
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");% l! |% T9 h5 o: [
WaitForKeyOrReset();7 R2 N) R( t" J& v
7 O* |2 X( }4 T- c/ J; ?3 C return Status;
: G9 u; b1 H% J1 T5 i3 |} |
|