|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/* ]! B) }2 `6 Y; _7 }
* myfileio.c
- M! K j6 |; C/ \' a! R * Apps. p D5 }% {9 I- C" S
*/) Q2 i/ _1 P# e
2 Z+ }" j; r$ T#include "efi.h"+ C% d% B5 K7 {* b2 y
#include "efilib.h"
" g0 e$ \4 [! {; A
4 x# @* Z6 i/ C#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE* w3 F+ q' ?6 _
9 }, f/ |9 z% Z/ Qstatic EFI_STATUS WaitForKeyOrReset(VOID)9 @" @4 @! h8 |" c/ P" m
{
. U, h' O; m# w4 q EFI_STATUS Status; X/ X* {5 X; D K
EFI_INPUT_KEY key;. |- n9 X* W7 ^& a2 N
UINTN index;
1 g. W# y9 S1 V$ Y: j, n 3 z, }* v. q6 c4 {) {
for(;;) {2 v$ `8 G% x: X' X* D3 U+ q
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
8 E* Y. m7 L% c2 p6 W7 u if (Status == EFI_NOT_READY)
2 X" ^& {) ~& O BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
/ h! Q5 w8 D& L) i$ | else+ g6 f+ i2 Z8 @$ F
break;
& k4 D. X8 j" E' n5 q8 c/ K3 N }) B5 p9 J, l0 G6 e
if (!EFI_ERROR(Status)) {
$ j* e9 c2 o& F( O- A if (key.ScanCode == SCAN_ESC)' E$ `. y) g r0 Z2 M# n& W; J
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
# L. g0 n6 ^& D/ l0 L" O }1 T/ e7 W, w0 W q: c1 C3 z
0 e3 O2 l, v; k! g2 h9 x return Status;
, s8 M* q1 U0 l P' {, x}
- w" K2 r4 `& B/ \8 A
4 l$ d+ W' I6 J6 ?8 REFI_STATUS$ g! q6 ]/ m! ?$ q
EFIAPI% h; H! S( k. u" B% u( K
MyfileioMain (IN EFI_HANDLE ImageHandle,2 p, P) {" ~" T5 E% w6 k1 g( S
IN EFI_SYSTEM_TABLE *SystemTable)
3 E: W2 q/ T/ L `' {- O{: J2 D' b9 ]4 b8 l( c
EFI_STATUS Status;) V% Z2 G9 i& w
EFI_HANDLE *DestAddr;
- |7 `+ W) l. I& ]+ k EFI_LOADED_IMAGE *FileHandle01;
3 Z% l5 }; w4 m EFI_DEVICE_PATH *FileHandle02; - r2 @# l+ j$ F# p. W
EFI_FILE_IO_INTERFACE *FileHandle03;
+ Q R% ~3 l5 I EFI_FILE *FileHandle04;
0 y* ^: V5 \) T6 c9 i$ _ EFI_FILE *FileHandle05;/ F- a- F4 _6 b5 j7 U) w
CHAR16 *FileName;* j" v# Z g4 l( G. K# J
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;, I# V2 K( w9 k3 S
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
: W3 U9 _; h- m& t8 [ int Bit0, Bit1, Key_Space;
, z& s+ |- j( B& T% n& A3 G
7 D: R$ M, N' I& l7 A FileName = L"NewFile.txt";
4 k% b ]/ a! c- F1 z Space_Key = L" ";
' j3 e3 R1 B" \' O( b, \ 0 t3 s0 X3 X, t. E1 z: q8 L
BufferA = L"ABCD";+ g& w: y3 R: J% l" @7 J; b3 s- D' p
BufferB = L"EFGH";
# p/ A" M$ j1 x% f; x3 c; b BufferC = L"IJKL";5 ~' }1 \* m5 z; `- L
) {9 A% K4 n8 t% t Bit0 = 0xff;+ [, t+ I5 U7 m5 B
Bit1 = 0xfe;
8 S- v- F6 z; D0 y $ E' J+ I$ x8 H5 e4 ]
Key_Space = 0x0020;
5 N1 a* [" v/ D- a& p1 A. W2 a # q% f+ B5 Q9 S. D! \) v |$ s
InitializeLib (ImageHandle, SystemTable);
+ \, \" O1 ^9 Y, l5 L) _1 L; u
DestAddr = AllocatePool (BufferSize); 7 i9 S# w1 v. \9 _; `( F0 @5 q
; q2 g- U% `+ u" E: r
Print (L"Value of Bit0 is %x\n", Bit0);
- O: a9 Q0 Q* K8 n' N# X- }2 X4 ^ Print (L"Value of Bit1 is %x\n", Bit1); . k q: O: f! x. t# F% x5 ]
/ p: c& b3 k! l) p7 [. H* \" C
( [ H* n D# s Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
f7 R `7 m5 f# }1 V if (EFI_ERROR(Status)) {1 C, K/ W3 b7 d6 s" r
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
. K+ }: a2 a2 A4 ]) Z return EFI_LOAD_ERROR;8 E; S1 g( ]1 Y! F, o# e1 I
}- f. D5 P7 v1 Y: i" X, ?; a
$ F0 {0 P; c# I1 r# f G Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);# }0 y, _# V$ N8 {5 Q" N3 |/ Y
if (EFI_ERROR(Status)) {
* @1 l6 X$ o# v# a) ^ Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);, I3 k' W% ~/ t+ P
return EFI_LOAD_ERROR;
1 J( f' ~9 x. T }
5 s7 y3 @7 j7 N, w; D% S/ x 7 ?, b: }0 I( G" a* w% M
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
$ A9 u2 j$ R: g" r l% o if (EFI_ERROR(Status)) {
H8 U6 K% [: ]+ ^3 s% B Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
" T$ k2 U6 T( _ return EFI_LOAD_ERROR;
4 e0 p. X' m) ^. B g6 o! \% B }3 Z; z/ a6 i# W* g+ q
3 A# s: t; r5 V8 x! E Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);: Z K. W3 H& Q* G! ~' G/ O; x4 O6 |
if (EFI_ERROR(Status)) {* R* W& J5 K9 G) m, a" `
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);; z8 M! O) u7 I9 m
return EFI_LOAD_ERROR;
( o6 B/ F8 e! ~$ n0 a; R' t0 M1 { } j% r1 V% _% g# O! j6 A7 {
2 ^5 {3 V2 \: J+ N
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);1 N& V$ `& M/ f U
if (EFI_ERROR(Status)) {
+ n! N0 S6 F) } Print (L"Could not open file, FileHandle05 err code is %x\n",Status);7 _6 ]. \4 W: F; U: Q$ @/ M- ?
return EFI_LOAD_ERROR;( \$ A K4 @- X8 @4 y
}
7 ^) }% T3 b. ^ 5 j( _( h% i' U5 R- C. z2 g
Status = FileHandle05->SetPosition(FileHandle05, 0);
& w0 f+ x; G0 u- n2 Z: O) O if (EFI_ERROR(Status)) {
. U6 {3 J- r! \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; ~: z8 c; ~' B7 q" w return EFI_SUCCESS;
" g4 U1 E! ^* k4 B0 ?1 E6 i4 Z }
! @1 ]' }: y( H ) q/ r$ S! f. J$ m2 \1 s ^# k
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);. Z2 G, _' s( e/ y
Print (L"File Buffersize is %x\n\n", BufferSize);
( ]) o4 x( y$ _% V( R* i8 B0 ] if (EFI_ERROR(Status)) {0 E \( Z# r# [: t. J0 V3 V
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);8 K; \/ W; p) c( |
return EFI_SUCCESS;! O O5 ~- u" H+ t3 d; v
} ; Q+ E5 H- \* R C: j
/ _+ v7 m/ L3 ~% A, S/ y1 _8 B
Status = FileHandle05->SetPosition(FileHandle05, 1); % @) y2 u6 m7 }$ O
if (EFI_ERROR(Status)) {
/ N, q4 p/ W( R0 ]; b0 Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. i5 G. {. W5 f6 P( ]! o# Q
return EFI_SUCCESS;
; L y* W) y6 d3 }* }0 @, p }) n, u9 ]. E: G" }- |" W' Y4 S
0 X8 y" Z' W# [, {& Y Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);' }( h w+ @' {* J$ J% K N- G G/ N
Print (L"File Buffersize is %x\n\n", BufferSize);0 B; F/ _) c% ^: h. {. s
if (EFI_ERROR(Status)) {
% X# C2 ?( t, D1 Q Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);4 A+ Q% V5 X% Y1 C
return EFI_SUCCESS;
0 K' v7 |4 f2 N }
# B4 T! e5 w. x/ x
9 g! r o d% g1 F& {/ Y9 ^# R% Q Status = FileHandle05->SetPosition(FileHandle05, 2); 1 {6 S3 O8 t) n& ~% @
if (EFI_ERROR(Status)) {# I' y9 o9 R, |2 O0 B/ D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' L; D9 p. I) E7 w k4 g
return EFI_SUCCESS;
/ s# q. u* `5 k" M2 y. _ W3 L }
! {( j, \# r% K' i3 W( n
9 c5 I) [, r8 N Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
, a/ n: Z! y+ e h2 F+ R( N) T Print (L"File Buffersize is %x\n\n", BufferSize);
; Z2 x) ?( |; C8 U& V* ~ if (EFI_ERROR(Status)) {
( Y9 @: S- u6 D: a5 x" H6 {8 {! v Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
+ b; l+ X4 m! c; T' ?1 n return EFI_SUCCESS;4 Z+ Z* z% |7 Y0 B& O. i
} 1 u x+ }, Y4 R- }+ |6 e
- m0 N+ r4 o4 F1 Z* f8 R
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>/ b5 T& b- }2 V2 ^
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
) n; f3 r E) q2 }- m& I# R- T! [ Status = FileHandle05->SetPosition(FileHandle05, 2); 5 l+ |( d5 N6 w/ I3 ^
if (EFI_ERROR(Status)) {. n6 m+ j. b9 m8 H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 j0 E: A8 x0 R* w
return EFI_SUCCESS;
. L U" \4 T/ T' W }
2 C9 g/ L- c H* [
+ u+ f4 H) _3 d; F- P Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
1 s8 S& Y: q* A5 Q. x! c2 d+ c if (EFI_ERROR(Status)) {3 ]' Y" a; h [7 P
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);# k/ \( ~8 j0 m V1 n8 s9 [1 U
return EFI_SUCCESS;
+ m/ [4 @% o# [ } ( c& I4 \* B0 p, j0 M# d8 P
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);+ T( G% @6 Q, i# s# s
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<' M% u9 J2 m5 ]3 K9 W$ G# i r( q0 Z
0 t9 R: ~ ^; @; Z/ U' J3 g+ M% x Status = FileHandle05->SetPosition(FileHandle05, 10); 6 E: o# k3 q/ E3 ?' q
if (EFI_ERROR(Status)) {
. L {- F. f0 [! C9 n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); \+ W. p6 b* p- X4 D
return EFI_SUCCESS;2 U5 r0 S' v. F5 P7 [9 `4 `& u* \
}, s$ Y- c* l/ C- O
* N8 _; @7 Y' p$ }; l Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' v4 D- `0 e: _- X4 M* p
if (EFI_ERROR(Status)) {
; i4 W) L4 z& d$ b! r" O Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" E a! N& ^" L$ x2 Z return EFI_SUCCESS;
% l( `/ O% ]) N } K0 i$ A6 W2 `+ C( L4 t+ v
5 w3 B' x; p, ~; s, P! d5 s Status = FileHandle05->SetPosition(FileHandle05, 12); . K% H; w- Z; t7 r$ B1 ]
if (EFI_ERROR(Status)) {3 u& m7 ] [, P. u8 B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- i/ g9 p0 Q( W5 G$ _
return EFI_SUCCESS;2 f, m4 I2 b' `
}
0 z- F( K/ V; b: O: i) ^
2 o: D( [, F( a/ R w$ M- E Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& u6 ~6 L' g4 _/ S9 N3 h3 S3 e9 O
if (EFI_ERROR(Status)) {3 u- Z/ D1 n4 d6 Z1 a& b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& {& b9 I- P3 Y3 M+ Q2 d! r* ~ r
return EFI_SUCCESS;. O- E. z8 F. C2 X3 A3 X& J: r8 l
}
2 z9 O' ?! X2 ~) H% X1 c; Y7 A# P0 x
Status = FileHandle05->SetPosition(FileHandle05, 14); , q% P Y/ c0 I. h0 ?
if (EFI_ERROR(Status)) {
5 ^1 g& G2 j4 |% g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* \: ] N( U* f9 a" w4 z) C; h& N
return EFI_SUCCESS;" ~5 O7 s e# y0 ~# V
}. j+ w p+ p' X" T5 @& @- P
$ T# G i, Z# v+ V
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ E3 L7 w& e; O8 L) R d$ a8 Y if (EFI_ERROR(Status)) {
% a2 n0 d- U( O& a1 _ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ @( ~- f5 V5 K. `* e. M. O+ i# i+ D. a
return EFI_SUCCESS;. t9 @' M. y. |$ Z" }/ ^2 x; i
}9 M" V, c, i* r' S6 b
1 M- ]1 e1 w. e& b t% s/ K1 P Status = FileHandle05->SetPosition(FileHandle05, 16); 0 M" J9 J% G. f! W5 n, R9 I
if (EFI_ERROR(Status)) {
8 z8 g2 _ P, O& j' ^; q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 s5 o! p0 D+ }" M: I$ z1 Q1 n/ D
return EFI_SUCCESS;
7 N* k! T8 J1 `$ F% h$ V/ w }2 @+ I; l! N8 @7 d$ t2 |$ S6 e
9 T/ M; r6 a; K Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);5 `4 E4 s, l- v, `( z
if (EFI_ERROR(Status)) {
7 {( i/ j+ {8 T* j& j! {0 B) w0 _. `8 }2 n Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
* u( u' l5 ^% p- F- O return EFI_SUCCESS;% _5 ?4 [ X* q1 w
}
1 z3 W3 A7 n g0 B
5 B/ {+ N I d( m2 s* [! a. W//---------------------------------------------------------------------------------------------------------------------------------------------------------------
; V& K7 m7 a1 X
( `4 ?4 q9 x7 y# ]1 J7 ` Status = FileHandle05->SetPosition(FileHandle05, 18);
5 o, {1 q$ w2 s* @6 O) o/ A if (EFI_ERROR(Status)) {& c5 y: c# @( [: l
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 Y; `+ p# i1 P6 U7 R
return EFI_SUCCESS;! ]3 O7 N* x9 ?% M
}) N% W1 W0 F% A B6 h# h
7 _6 `4 ~3 Q# m; L$ q9 l0 I Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
) s' T4 e& M5 m7 O; F$ }9 @$ i if (EFI_ERROR(Status)) {& p* R; F. `0 z1 r0 q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& K: ]9 K! G+ z' R. K% e
return EFI_SUCCESS;7 d6 t' [, D, p) N" K# U. s7 c
} 9 S' r0 q" b2 b+ j1 x$ m3 [
, J1 q/ P* a# ~( B2 G! m$ P9 M5 M" G FreePool(DestAddr);
: K) @% j) W0 k0 z, W& z, }. C7 L
1 N& u" c- u( P g+ `! i Status = FileHandle05->Close(FileHandle05);/ d/ h3 A" b, ?0 V
if (EFI_ERROR(Status)) {: I* i. W$ O3 w
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
0 ^' h: Y7 m- {7 Q* ?: y. n3 J return EFI_SUCCESS;% q, f5 U' `( ~! _7 {. ^# X+ _, M8 E
}
1 Y3 V$ T) ] F9 U- }# z
) d( z; _8 z7 ?5 N8 C2 G Print(L"File Name = %s has already been created.\n", FileName);- ^) Y( R' v& S
8 w. }9 c: R |
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");/ u; C& A A# d# w. S' E: L3 q$ ^
WaitForKeyOrReset();
9 `. z9 c/ C& v9 p6 M% a4 R9 A & e+ v3 I7 | O4 ^9 ]
return Status;! U) M/ y7 q, s% |! J3 T
} |
|