|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*; ~+ w3 [7 g1 x, E6 W2 p. t( k" H
* myfileio.c
( A4 d. ~0 M5 @( `4 x * Apps" G. E+ }+ |* _# D5 T. m1 k
*/# J/ S5 f; D& Z; p V- i" x5 L
, w0 `/ h; Z" v# P
#include "efi.h" D- C, u( z$ p! X# v5 n0 h
#include "efilib.h"
* m9 O- h, i' T- O% o' _7 t8 g0 f! R' I, G# N1 x
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
" K5 F. I/ e2 B8 C- u1 T# `2 K$ ^/ y O! h( f. p
static EFI_STATUS WaitForKeyOrReset(VOID)
! m" h! i. u$ ~' {. |2 a{5 \, r) O( i. U
EFI_STATUS Status;
/ Q% C! o7 {5 [ EFI_INPUT_KEY key;2 j" Q: S9 B6 l9 z" h2 V, q
UINTN index;
- {3 D5 L0 h2 x% {- Y 9 r3 Y- F4 P$ |8 O5 u
for(;;) {9 h* V6 P2 \, {: L, z
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);$ D% M* s+ G. ~( a7 E1 Y
if (Status == EFI_NOT_READY)5 J4 e; ~1 B; K* S0 q
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);4 p& b7 I+ x; q. Y3 ?' X
else
: S t5 e' V) U7 K9 J3 v& a* S# d break;8 }+ y U" {4 G( N
}
' i$ J/ b$ l6 k if (!EFI_ERROR(Status)) {$ y3 M8 V9 f/ U: z7 e3 {
if (key.ScanCode == SCAN_ESC)
7 ~! Z( O: U; b' M RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);" U, C1 v; Y& x- U/ O5 I9 x8 d
}0 D% t& D P. [! R! S
! [1 [) u$ Y4 G% D4 i return Status;
7 t+ ]+ V8 C! ]3 Y+ ~ z4 R6 B} T+ q- M5 B% u X+ y
6 t7 V' |5 f* ~1 d5 c% G
EFI_STATUS
( S& K/ D% H( M* c; ?: @$ IEFIAPI
: O( \9 U9 n9 l9 oMyfileioMain (IN EFI_HANDLE ImageHandle,$ B9 `* R5 R/ z
IN EFI_SYSTEM_TABLE *SystemTable). O' G3 ^. \! P9 h1 ]& I; y" Z
{3 [7 T. I4 W A7 P
EFI_STATUS Status;! k+ i0 C) K+ ~$ Y6 |2 X* \
EFI_HANDLE *DestAddr; & l+ q1 _: u9 u
EFI_LOADED_IMAGE *FileHandle01;
6 k6 z* o1 k3 S4 i; l# l, Y EFI_DEVICE_PATH *FileHandle02;
9 _7 G9 F- ?4 J7 Z( y; C! H% o1 z EFI_FILE_IO_INTERFACE *FileHandle03;8 c5 c8 c1 w8 Y. d
EFI_FILE *FileHandle04;5 d0 n+ C' W7 C) @
EFI_FILE *FileHandle05;9 {- E- _& |( A$ W9 {) U& R
CHAR16 *FileName;
/ m4 ]" S! a |* v0 B CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
) L, K5 B6 X6 `5 V9 i7 F UINTN BufferSize = 8, BufferSizeKeySpace = 2;
: `. a6 K" t$ i4 v! i int Bit0, Bit1, Key_Space; 9 p0 @+ D4 a8 t, [" r: m
" m0 {8 e+ v" T+ R) Z
FileName = L"NewFile.txt";) d) s q; _! ~' w9 d4 `
Space_Key = L" ";8 X# P8 O4 D/ v6 }6 W& z
- s5 I$ s3 S. I2 |
BufferA = L"ABCD";2 i+ g; J7 p. Z" p
BufferB = L"EFGH";4 I+ y/ V9 C& m1 m' k$ M5 G# F
BufferC = L"IJKL";! {9 P6 K- \ m L
8 l, A$ f. z9 |# N; r
Bit0 = 0xff;8 t2 _1 R! S/ N, j0 c6 k2 q
Bit1 = 0xfe;
# ?, z3 E6 \4 m/ e5 l6 C
' F- a! ~/ c9 l Key_Space = 0x0020;& ?. A7 Q8 {7 q
9 h" I' y2 `7 R* s
InitializeLib (ImageHandle, SystemTable); " f; e. _$ F) Y' Q; v, i# Y; B
4 p* T8 b4 m# U/ I k! U$ L5 \. e
DestAddr = AllocatePool (BufferSize);
* k' z/ h, f) O( ?+ J
. L. s& W' | w( n, h8 i1 | Print (L"Value of Bit0 is %x\n", Bit0);. G; q+ j* G% C) Z T0 g2 ~) R4 D+ N% D
Print (L"Value of Bit1 is %x\n", Bit1); - X9 [+ q1 a6 y
& w1 a' b) e/ \4 v( |
# v5 v$ V; m! m- t) m: t Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
1 v" i9 h+ R* H6 t if (EFI_ERROR(Status)) {/ e/ F$ |- h) X5 b, K! Z
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
i+ i% ^- q* W" N return EFI_LOAD_ERROR;
. b/ ^# y# ^- H! v6 n d }/ ~* k& {2 r- g& B
, r! B* k$ Y Y9 @ Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
K. k& ^9 `) ~# M% ~! m" @+ k* M# p3 R if (EFI_ERROR(Status)) {+ ]& M" Z9 W( h \; |
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);2 P* j2 s8 u) y/ h7 u8 g
return EFI_LOAD_ERROR;
+ C& A- E i1 y }
' Z: T5 _& F1 Q' A / I2 u2 Q* ?; B
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);1 z0 R7 f) X4 r" g9 m4 k! G
if (EFI_ERROR(Status)) {
6 _7 l+ T- x$ a Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
2 ?7 Z: M f5 W; }2 z+ R! } return EFI_LOAD_ERROR;
3 ^& L" Q% J6 |$ v; m: a$ E* ^; D9 i }5 E9 q5 a0 O P: m+ e
+ h A9 U" G9 W) Q3 g3 S* |
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
' x: [& l: `3 C# Z+ U. J if (EFI_ERROR(Status)) {
" v. S9 I. o" Z6 Z/ L# c/ O' d8 S8 ] Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);0 q% h, A, \% S6 q- Q n2 m% H
return EFI_LOAD_ERROR;
9 y' T# b, p5 j M5 a; u+ \" p }
! \1 b! h: H3 k8 k9 V
% Q1 I% B g/ ?* j Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);9 ~3 _6 p+ Y) c* |2 ]9 w, e! a
if (EFI_ERROR(Status)) {
1 T( Y# [" Y/ {: t1 Q* \0 M Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
4 R5 [1 D0 N7 V$ q return EFI_LOAD_ERROR;
- ^* \. ], I1 @0 M4 _! g }
; T' |8 [/ t, A R: X* A8 D . K0 Y2 ` w( O8 c$ K6 j* W6 Y* c
Status = FileHandle05->SetPosition(FileHandle05, 0);
/ f$ Z% \# u& `7 \ if (EFI_ERROR(Status)) {
1 j, ?% P7 ~" E3 e9 h! n! m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) O0 ?5 a; ]2 Y4 O return EFI_SUCCESS;- D# e; x7 O+ o# p5 j
}
# n* T/ H$ ^ A' b# m ) d( |! q' B, A4 z0 z7 [
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
& u3 @+ I& l9 |/ l Print (L"File Buffersize is %x\n\n", BufferSize);& s$ T5 ~* U" U$ k& a5 o
if (EFI_ERROR(Status)) {
! @' d! l' w$ U0 j. Y1 x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);1 V* Q2 Q \6 X- t3 s6 F
return EFI_SUCCESS;
0 W+ X6 P& T) |3 L* z/ G' R, y }
^8 y" R( S$ j# @: i N- K# d8 z+ W
Status = FileHandle05->SetPosition(FileHandle05, 1); 0 ]; \* K7 C% E0 j' K$ A% k- @( X
if (EFI_ERROR(Status)) {
- B. _' B% w5 J% Z9 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ X1 Y* b7 E% @
return EFI_SUCCESS;, P6 L( h, s! I/ V2 m; ~
}
) m N! J# v7 B C
0 v) B% [& b) o$ j, I Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
2 j4 ?6 `8 F% T u% f) Q# ? Print (L"File Buffersize is %x\n\n", BufferSize);
4 [) K1 r& h4 f) ]5 B* H0 F if (EFI_ERROR(Status)) {8 Q. ?/ E- f3 q$ z' e _+ _
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
3 g# Z; ~" c+ O1 q return EFI_SUCCESS;' y* \" ^" M% g) ? f1 h' Q9 y3 P
} + z7 J6 g2 ?1 D7 W
0 c3 u9 G Y7 i' E6 X6 N/ j+ s8 F Status = FileHandle05->SetPosition(FileHandle05, 2); 2 U& u; ~8 c3 U8 U/ H
if (EFI_ERROR(Status)) {
% H$ G& R( P. ?* g( C) A Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ e. U7 w7 Y9 ~* k n. w/ Q* W
return EFI_SUCCESS;. W- `3 \/ Z) ?
}. T8 Z C* x1 ?
' Z6 ]2 m& I! \4 `% d" w) I
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
/ l7 W: H( n+ ~" a" J; ?& y Print (L"File Buffersize is %x\n\n", BufferSize);
' U* \6 U( A/ K6 I if (EFI_ERROR(Status)) {
: k c! q/ z& h7 V; e: ~ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);/ z( i7 z% s) R8 u; R0 i( l6 [! k. a
return EFI_SUCCESS;' g9 N R, W, |
} ( @6 O$ ^, Y3 v4 |6 \
; L# E4 r4 J+ B//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
2 O/ h. x* X/ R Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
* K! [! y/ a5 J Status = FileHandle05->SetPosition(FileHandle05, 2); " B$ F$ |0 D7 ?/ A9 w: x
if (EFI_ERROR(Status)) {
" c8 g: ^1 [8 U Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. b' l9 ?. h# S$ o& t return EFI_SUCCESS;+ j g/ y& x; \$ s+ u9 v1 m
}
/ D( m3 S1 S9 g8 }3 K6 V* y+ ?' ? d$ w0 K( [# b! ~4 S
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
4 Y! ]( ]6 u2 }/ \4 [ if (EFI_ERROR(Status)) {
9 q# n* q6 ]; n: a* G2 O8 ~$ X4 m Print (L"Could not read file, FileHandle05 err code is %x\n",Status);3 ?: ~$ f* T# I( W7 C: N
return EFI_SUCCESS;& F/ k, z, C; |5 \2 b
} ! o6 ^: ^5 n( L" U2 f$ M
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
7 U8 j" |1 |+ q% s3 x% Y//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
! Z: a3 Y$ |4 r) k$ h
; C1 u2 l0 Y% e" u' F" d: w Status = FileHandle05->SetPosition(FileHandle05, 10);
, g% ]9 U+ A5 u6 f+ x if (EFI_ERROR(Status)) {! P; O; }" F* H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 E5 f. _% ~$ k& h+ ]$ Y }
return EFI_SUCCESS;/ E7 ]5 w1 r5 T* J3 ^4 E9 @
}, V3 E4 u1 x7 h+ M% H! l$ x
8 a0 r. f) Y( j! ?8 B) O9 Z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); x/ B' r: {8 n6 i6 \# i9 d. d
if (EFI_ERROR(Status)) {
! U9 l. O9 h4 N' A$ Q; P Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. _& G, l' M0 R6 |1 G! A3 O& v
return EFI_SUCCESS;
6 W. K' J1 |( \3 \ h }; L- ^6 R9 D% g1 Q6 G! v, m
9 x2 a1 r; q V2 T& p Status = FileHandle05->SetPosition(FileHandle05, 12);
: D' T1 n8 X: @$ N5 t2 S if (EFI_ERROR(Status)) {
/ }! c" l2 F$ C3 ~1 ?8 u Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 ]5 i& l1 o5 \4 L: a$ w5 N return EFI_SUCCESS;; [% Q- R9 O, S+ x% [
}3 ]5 G2 l d( X# _# u- G/ w
1 k! m/ r, f9 ^& G8 c5 }# ?! R$ H Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);% V4 k: X+ ^( }; C3 c1 {+ d
if (EFI_ERROR(Status)) {
/ n) Y9 U7 O$ W5 {( e Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. Y! A& {6 n K2 w2 j4 N return EFI_SUCCESS;
6 L @: z& Y5 ?& u }0 u+ s4 R4 _3 J3 ~; M( j, s
- d% |% b/ h F$ A" Y& d$ [' F/ ?: R Status = FileHandle05->SetPosition(FileHandle05, 14);
- o/ n% x1 i9 ~9 t& ?/ g: a if (EFI_ERROR(Status)) {
8 R# K1 a! i2 T1 O2 i4 q# s Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ w& I* G' D7 z( ?
return EFI_SUCCESS;
+ B/ q! T0 w- I$ { }
( q9 \) I G q" s% S0 |& E ( P. w# H: x5 ^) m2 ^6 t' E
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, r. p! N Q6 k2 [ }7 D2 d2 q if (EFI_ERROR(Status)) {
+ ]- x2 o) C2 n Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 H# j/ c; ~" @& c+ s- n return EFI_SUCCESS;, I9 H9 Y, l6 V( _; x! `2 }6 ?
}
% k. s! i* H; ]+ ~% u
6 }6 d: O" ?* o( n Status = FileHandle05->SetPosition(FileHandle05, 16);
) X/ A* O5 p6 Y8 {+ b if (EFI_ERROR(Status)) {
$ k! |/ D7 f5 M/ t7 v) {' g* m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# b! E" m9 n% j* n" t! a( _
return EFI_SUCCESS;- c1 G1 S5 F: d: B. m1 j# W
}
9 q7 l& l O" B4 R $ G# H% U9 f! J9 G$ l3 b# f/ M( [
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 c0 N$ r9 T; E6 b9 ~+ ] if (EFI_ERROR(Status)) {
, f1 J2 g2 y8 j. x: H8 ]0 c Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: p. H# ~( I1 j( i/ a9 ? return EFI_SUCCESS;
3 n2 C. z5 d& {$ z1 t8 i } t& g7 i m. p6 K; G! s) D
; I! U5 }: c/ X0 j- T( k) K* m, b* _1 b
//---------------------------------------------------------------------------------------------------------------------------------------------------------------. m- p% Z, S$ [! f
z- d3 [# L3 ^ D' Z, Y4 ? Status = FileHandle05->SetPosition(FileHandle05, 18); & e: y4 r6 E {) A8 O5 x
if (EFI_ERROR(Status)) {
% N7 } q% Z6 h) s3 E( `# ]# } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& b- w' }9 f& y; b. p return EFI_SUCCESS;. v% O3 W" j2 \5 _& z
}3 k$ n8 _6 {, o4 b- H
! }0 F9 o9 [. `# L8 _2 K4 { Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);6 N" t& @3 F+ B" x8 [$ e& h
if (EFI_ERROR(Status)) {
: `5 E' i7 e5 f. V7 K Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 ]1 e' S( s" Q) p
return EFI_SUCCESS;! B1 ^3 \& j+ m0 }
} % c! C& ]& q/ O. m0 v: H
' a# j# m6 N9 f; l FreePool(DestAddr); % _& P7 D. i2 _0 S, t0 z
5 ?, |8 n W9 y% [, h Status = FileHandle05->Close(FileHandle05);
, u: m, u7 Y K( l( f f) T1 n( r! o, F if (EFI_ERROR(Status)) {9 w; `4 f! `, [* p4 w: J
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);: U/ J' ]) ? W! Y
return EFI_SUCCESS;7 e5 }* @& ?( x8 r, K' \8 I$ \3 c
}
7 o- ^- Z# V9 ?/ Z' C # U5 h* _ z# F2 Z+ {
Print(L"File Name = %s has already been created.\n", FileName);( Y1 T1 n& G3 _9 x" \% } @: y+ L
9 [3 O6 B" `5 [% k+ E
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");, Y8 ^5 D8 K1 ?: K4 f
WaitForKeyOrReset();
6 m. g7 f. q, m1 {0 o
k% x* g5 ]0 m/ [4 V return Status;
3 g& p! O- @$ y6 u, f# ]( G& c} |
|