|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*5 b' M. l( {7 C+ O: l
* myfileio.c
3 G: d( Z% n Q3 `! b# E4 G% n * Apps
8 Z. `+ ? E6 ?3 ]8 M2 v */" T; I' j/ I6 \; u- x5 U1 c* O
) H! \+ ^2 z9 a9 [#include "efi.h" `+ \) @' a+ d |9 u5 n
#include "efilib.h"
5 f2 R9 E5 u. w0 G( N2 A z' N I( {% L
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE1 Y4 I( s& \" Q
' \( i9 ]: b' K5 Y# q. r1 @static EFI_STATUS WaitForKeyOrReset(VOID)
g+ w+ f! j, g/ o f, t{
3 B. q# U' U' D; X* ~ EFI_STATUS Status;* u" t3 h6 l1 S3 s
EFI_INPUT_KEY key;2 S2 H- g/ b* S c3 i' b
UINTN index;
8 S9 S# B: v( f! c
- L: N& @! z) }; B. m1 o for(;;) {7 m5 U- Q9 q) L* a# m; `: H' z9 s
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);' y- [1 {8 U6 ~, G, Z5 O
if (Status == EFI_NOT_READY)
0 e6 w2 u. k2 W+ s' G9 P BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);) ^5 z0 T! s& y' J/ h8 E
else8 j" Y t! N1 [& \: n# e$ S
break;
" j: V; e2 l% y) O) ~4 A }: _8 X+ R; I6 D( N
if (!EFI_ERROR(Status)) {
5 a/ c3 h- g; F# F6 l if (key.ScanCode == SCAN_ESC), Z: f9 V; `8 N0 m! E- y
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
7 D2 B1 _9 _6 m1 \6 p7 B2 A }# {; W) ]- z2 B5 d" X
( e0 O; u. f% x, G' Z' P7 I/ ]7 S
return Status;
, E( e: X' \# r# S. t7 W" {! y}; V A! S5 `6 g
( O0 B/ K# D: f
EFI_STATUS# s; K: K( r$ H4 v
EFIAPI( _1 `# R- W2 K4 f4 S" D
MyfileioMain (IN EFI_HANDLE ImageHandle,. g( v; }' F x9 V
IN EFI_SYSTEM_TABLE *SystemTable)8 v( T3 z( e! t( ^1 W r
{
- a4 s3 G) l% j' h- i. }: ]( X EFI_STATUS Status;
. Z/ q9 a- x# h3 T! ^ EFI_HANDLE *DestAddr;
) P1 V5 W, {# [* |) |8 Q EFI_LOADED_IMAGE *FileHandle01;
# O3 ?- s2 b: i1 ]8 q; G3 ~ EFI_DEVICE_PATH *FileHandle02; % b) `7 r. t5 t
EFI_FILE_IO_INTERFACE *FileHandle03;# o' b- @" m% _5 R2 [9 z0 l
EFI_FILE *FileHandle04;
8 }/ Y1 Y- \ Q5 p% x; Q) N1 t7 M: F EFI_FILE *FileHandle05;
0 V- w" n0 K* k CHAR16 *FileName;
, t& k1 g/ W* a3 I, m+ e CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
2 \9 h, J, b+ u, d4 O# O1 S* M UINTN BufferSize = 8, BufferSizeKeySpace = 2;
0 M( ~5 c( E8 H, u. F, _1 ^; y int Bit0, Bit1, Key_Space; 0 o A3 A6 } c' ]5 y0 O9 c- E
& @2 _9 V( s. B8 i FileName = L"NewFile.txt";
% h0 ~- s: e' s1 w& r Space_Key = L" ";
: W' u1 e' o/ c: p3 L$ ]* v! P# s 9 t+ h" a+ v! R4 o \ h- P+ s
BufferA = L"ABCD";7 m) O2 I2 r2 U- e c( p2 T
BufferB = L"EFGH";
6 ^) A. Q' Y' p6 L6 M0 S2 C& q: q BufferC = L"IJKL";
w2 [8 A' v& u/ R) a# S4 R- ?, {- g" h
Bit0 = 0xff; y: v- F' {) O. a+ Y# ]4 ^8 @' Q
Bit1 = 0xfe;' H% S5 `; G3 s- |' |) ^. n0 H
0 f, W( @5 a( w: l/ m" T& Y Key_Space = 0x0020;
" c( B. a3 y4 } - P% i2 j2 t' i/ g( ?5 v
InitializeLib (ImageHandle, SystemTable);
. y1 G) J3 M3 o. |0 Q) W* Y! Y9 H; R/ R. \2 O& c! R6 L
DestAddr = AllocatePool (BufferSize); ( J8 B5 E, J& z# V
( e: {2 `) l* T4 W& ^7 M Print (L"Value of Bit0 is %x\n", Bit0);. \- \# U, r, s9 H
Print (L"Value of Bit1 is %x\n", Bit1); * X' Q, O4 {, R) \
, i2 W7 T6 m# q V6 B5 z
2 e3 e1 D5 J* y" \1 S+ s2 W- b# X- Z Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);& u. ^: h7 x( g; m. H' B# j
if (EFI_ERROR(Status)) {2 p, l1 W: f' k; _
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);: X( H3 R0 B/ N
return EFI_LOAD_ERROR;
+ ^3 s4 g5 y6 N; u" }7 ~ }
+ s& a4 H0 t5 Y8 y
! F* k2 W8 F( U- o! ~+ c Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
9 N1 P8 R( P; y: ^: R8 c if (EFI_ERROR(Status)) {8 \! L* ]' H& {4 h" F: U
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);1 ], l% D- A) F/ F8 T: U7 S
return EFI_LOAD_ERROR;
' B7 D; c9 r( P% A( B5 Y } * y3 Y0 {6 C# Z* C5 q4 Z/ v
9 s8 U6 \5 ^3 P: ~( r Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);8 y1 t5 ]& u: _- P P5 H3 G
if (EFI_ERROR(Status)) {
* q2 k; H: e+ X. Y' e3 N Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
. @' E9 Q5 S/ w' O+ r; `" ^ return EFI_LOAD_ERROR;1 H- d# W4 N) S# b& _3 O0 w. M
}
( i4 t B% H0 }7 g" H# m* q6 |. u3 {7 h
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
+ ]& X+ @+ k( X8 C4 B7 N if (EFI_ERROR(Status)) {4 z( F) W; }9 }6 D8 n2 E
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);/ B. T& _1 q$ z. {
return EFI_LOAD_ERROR;
3 R, |3 H, G# A9 m( Z0 X, q+ c) R" J }
3 t5 P; w% }5 u( l" O. P 7 J/ W9 y7 s- {& Q
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);3 W; d& U( P/ H2 v
if (EFI_ERROR(Status)) {- `: p% w4 _. }2 D) p- j* `- O1 U
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);6 H' ~" z) e* U# o
return EFI_LOAD_ERROR;
" O$ B/ D# [1 k" E2 r" e }
5 Z2 w) r* G; c- b8 K3 `2 j0 |* ?! V $ ^3 ?! T- {; J4 D( Z
Status = FileHandle05->SetPosition(FileHandle05, 0); 0 I, ~ }" l8 E" A9 Y e& y9 B
if (EFI_ERROR(Status)) {
X8 }9 l+ ~) L" J) _; N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 s( F- Z. M( n
return EFI_SUCCESS;
?. c8 T" x# b4 o6 x5 N }& E; J$ u+ F+ l( [5 M1 ?$ W: R
4 {; e! c+ V( E! ~2 P
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);8 J; _. A6 k# p, t8 d* N6 v3 Q: e
Print (L"File Buffersize is %x\n\n", BufferSize);/ ?* Q+ ?+ w n! w7 O
if (EFI_ERROR(Status)) {4 k; B/ y- }3 T- T$ V
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);- @$ U0 H; A6 ^3 W) U
return EFI_SUCCESS;
( I* J7 B9 b4 I } 5 j3 ^# K7 L! k" c3 u- S0 L
. Q5 K. R) I- g Status = FileHandle05->SetPosition(FileHandle05, 1); 7 O' f" ^5 C7 k
if (EFI_ERROR(Status)) {
8 q8 E5 p2 k! a/ F. e0 E0 H5 s+ k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 l/ b* f, ^: [5 O6 m return EFI_SUCCESS;
/ m3 o6 r) E9 L* V% f$ `+ ^! P6 ?* l }
: A. Y4 d1 o* S/ H3 N& {
' n9 C5 V3 u0 c, h5 ~+ e Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
% V( N; _5 F; Y3 U2 f/ J Print (L"File Buffersize is %x\n\n", BufferSize);7 b5 M$ c" w+ K0 h
if (EFI_ERROR(Status)) {) v) }9 p# f# z3 X5 @+ w9 C
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
+ o; {6 z [) V5 @; V2 t# {5 u return EFI_SUCCESS;% Z6 R8 i, p8 Q0 Y8 c
}
+ P3 i! m9 q: T I- @3 |* B$ L 4 f, x3 J* ^; f+ `3 P+ W7 Z
Status = FileHandle05->SetPosition(FileHandle05, 2); . ~: G L0 H; _" y/ B5 ?4 s$ M9 x, W9 M
if (EFI_ERROR(Status)) {
) S( P* e& A* L6 r# ] Y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& ]) J: j4 k* G, ^ x return EFI_SUCCESS;# W, H% B' \4 j# t
}
9 o R4 m, p. k" E7 x 5 d, W. G: h X4 g
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
/ b5 H. _/ B# I7 n( }+ r" g x" R; O Print (L"File Buffersize is %x\n\n", BufferSize);0 u! \ \% y0 w. K- H4 @( S; C
if (EFI_ERROR(Status)) {
A& g0 f3 d b$ w# I3 A Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);# P$ \. ]( y6 k
return EFI_SUCCESS;( |. m4 B4 a7 Z. z6 L8 y
}
* J- T. o& H3 e$ B1 n- V; `: s. ~: O
7 q! ?: x, r" s9 W4 d- }/ f7 X//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>" |" ?& { e% T
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
. V- X; ?( ~/ O n' f Status = FileHandle05->SetPosition(FileHandle05, 2);
: q* o( M- l0 |4 D; j3 M6 P if (EFI_ERROR(Status)) {
/ `/ n; W( _! Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
g8 d7 L6 }9 _$ B( g8 A& F; o return EFI_SUCCESS;
' H) ?4 B" O1 r5 _# {0 a+ d4 | }5 F* Z) I, I1 |, W1 p
+ k. E- N& M. A0 i d' G; Z8 @
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);- C( r- i' u- S' B; C# z2 e
if (EFI_ERROR(Status)) {: x0 u4 ^$ I# h) R5 ?8 L
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);' k9 _$ S0 d+ l. @3 S( Z
return EFI_SUCCESS;; D0 a0 n) x/ u) q
}
2 S" H" g" t7 ~1 [, {3 M" K: M Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);% [0 f0 a5 g1 E, C7 B
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<: ?* m2 O$ p T+ v$ [/ Y9 @: I) S! Q9 q
; [( H' W6 @; y" J& Q4 I$ r" ]
Status = FileHandle05->SetPosition(FileHandle05, 10);
) Z( A- @( m7 R+ t. f E if (EFI_ERROR(Status)) {
" M6 N2 z: q8 z* ]! S+ |+ ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# O: D5 v$ n' r' T! {( x5 U# l
return EFI_SUCCESS;! F, I: _' O3 r4 q$ ? P
}( }3 Q" i7 ]* O( c- q
K" {+ e/ V4 _" A- m- ]7 ~ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ I# X6 O1 `) N9 {/ m e if (EFI_ERROR(Status)) {& ]: j9 ?' ?2 t
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 ~5 @, R7 D" b+ z% O, R
return EFI_SUCCESS;
& [2 N( C+ w x7 |" T9 o$ Y9 M }$ g: X8 N) k4 C" t' ^% _
) e3 W; A; u6 W9 M
Status = FileHandle05->SetPosition(FileHandle05, 12);
5 u8 P' R4 y: X, H- U if (EFI_ERROR(Status)) {, R0 J* G& _4 o
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( i G& w2 w; I- h9 ?) L return EFI_SUCCESS;; ^: ~3 I }; l& R6 k! o
}+ Q& @, ?& Z# i b. ~- ]
, e5 j+ j7 l, j2 i) j2 F
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. R( H! X# {7 t- {
if (EFI_ERROR(Status)) {
4 z4 R/ m O5 ?1 B$ F* t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 s) H5 j6 S4 v, v
return EFI_SUCCESS;
! T: R( P: E4 e. \) } }
0 z4 l3 k) ^8 G$ K( s: @+ q; [3 k/ C8 E
Status = FileHandle05->SetPosition(FileHandle05, 14); 6 a- L7 m' G/ E% @- d: I
if (EFI_ERROR(Status)) {, b; B; n3 U# r/ `5 P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 }) u$ k4 `9 d$ X
return EFI_SUCCESS;. D# u2 t: y: S
}1 B% t7 u) [. L" R% @
9 x2 |) l- v" O5 a7 y3 A6 E
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 j/ X5 a) C# w6 y6 g: w if (EFI_ERROR(Status)) {+ l9 W( u) K$ _+ \. R" e5 Z9 }- t
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! S$ \( I/ |" A9 k
return EFI_SUCCESS;" ~: R8 e c- Z
}
" E: K) g+ X: |5 n8 `2 T8 l
1 Q+ d% B% [, u4 f8 Q. E Status = FileHandle05->SetPosition(FileHandle05, 16);
" g h+ g' |# d8 z0 o% k if (EFI_ERROR(Status)) {
$ M& U1 B. O+ v4 o7 m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ ? O$ B/ B: y" O9 Z8 w1 o
return EFI_SUCCESS;
5 p- \8 P: m, N- r- Z2 s, C }
6 H; _0 l9 B( h, I4 t9 T& C
L( U* W& k; | Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); e( ]" |6 G2 v1 U5 m
if (EFI_ERROR(Status)) {! W! G+ {* d" H( Q$ e3 E
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 D5 T0 {# a) X return EFI_SUCCESS;7 t6 I7 |, h$ f: F
} ! I% K, {& D6 K6 S6 {
1 a# \ D" p3 Q) c3 ]0 g- G//---------------------------------------------------------------------------------------------------------------------------------------------------------------
# E( W+ ^% ]0 ]; |4 C: d: d
6 C) U; U1 x+ X- Z5 I: P2 l Status = FileHandle05->SetPosition(FileHandle05, 18); 1 p+ N6 i* u/ e$ d4 d
if (EFI_ERROR(Status)) {
8 }# v- ~8 `+ q% \. V- M1 ] Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' G. _2 K( w: H& W% e A return EFI_SUCCESS;( J8 ?- d! H9 }/ U5 Z0 q5 H6 K z
}
+ [" u. _$ W { ; S. U$ Q- _$ |' X, s! h2 A* Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);: { G4 H4 @" S' g+ W f
if (EFI_ERROR(Status)) {4 S3 {7 E5 K7 W/ q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 Q9 g5 |/ x. h" Y5 O% _ return EFI_SUCCESS;
+ R* n+ V! t0 g' f b! Y+ P6 q } 7 z5 J2 g0 Z3 A) M
$ Z0 d2 V! K2 G! d" ?0 E a
FreePool(DestAddr);
2 a# H: Q! q4 G3 Y: Q
9 k1 R, Z/ \8 Y& h1 W3 n Status = FileHandle05->Close(FileHandle05);. I8 w4 f# T: f3 o8 ]& W$ |6 ~" m
if (EFI_ERROR(Status)) {5 i2 b) e& { C* Z
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);8 U4 [$ t$ ~9 j. S$ W
return EFI_SUCCESS;
, L5 X: A- I( y- U) z8 ^ }
4 R9 J' \9 B* P1 ]
6 u: A$ X; E+ P1 @# H' a5 ` Print(L"File Name = %s has already been created.\n", FileName);
' [; y2 i' [9 m2 {& ~4 }% u
* g5 n, e6 l6 [8 v# i. ~, B( x0 h Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");( `" n, Z- h4 t ~( i" g; p, s' U
WaitForKeyOrReset();, \7 a* t$ P) O2 r8 |8 A
5 c+ r4 G$ u4 i z) u+ q' e
return Status;: p. n" m+ X G+ Y
} |
|