|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
# Z5 M! ~9 y1 M2 \( G; ~7 u * myfileio.c
& \: p8 E) W. _, i1 v * Apps
* i8 Q4 n+ \7 X% h) w' r */
& |# d+ P4 U! M |) T# c3 l4 p- d: g" E+ U5 F9 q
#include "efi.h"
5 i0 A% G3 K+ O2 S0 E#include "efilib.h"
& g/ c5 I1 r9 v) q/ a
) ]* x: G0 k0 Z6 S9 C/ K& s#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, L7 T) `- U9 Z9 V& @ E1 S& V+ o
& E6 g. t7 u. p* Ustatic EFI_STATUS WaitForKeyOrReset(VOID)
% V$ L9 f# F2 }7 Q{* N, V) h3 J* i' P0 d: k& x, U
EFI_STATUS Status;
4 D0 K: \! n5 C ^8 O+ B EFI_INPUT_KEY key;2 H! R6 e5 d8 m/ }! ^; q
UINTN index;" E% ?/ @% G7 Q1 L H% I i
( R- E; F2 K9 T! }
for(;;) {
2 ]6 P0 P, D2 r7 L! f# ] Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);$ J4 t/ n8 f0 F9 m3 Z2 q
if (Status == EFI_NOT_READY)
4 n) S, B- r4 L" O# K BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
G n% ^% v- k" R" j* z" o. R8 s$ t4 z else
- a2 \0 n! M f% D+ @/ A break;+ F* F& W0 V4 m; }* F
}
2 _ m' O: N# u1 b. I if (!EFI_ERROR(Status)) {* o/ F1 [0 b* \& t) H& |
if (key.ScanCode == SCAN_ESC)
: c+ W) |7 \) S RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
2 F. X- b: c& }1 i& J1 O7 T }4 ]8 `, p: v0 P( w, U
5 C' i4 i/ `1 w. Q9 J" H8 b/ p* z0 Y
return Status;
0 F. s* ?# H7 w# ]}7 |% d# O6 n' ]; m. d3 U: M5 @+ H, f' A+ R
+ Y* z3 O9 C9 z* \; [EFI_STATUS
" a: L; n$ ^, k7 X& }( M0 r* fEFIAPI$ W, e: N/ P6 @/ ^% ]9 n
MyfileioMain (IN EFI_HANDLE ImageHandle,! T/ _) `( H8 G
IN EFI_SYSTEM_TABLE *SystemTable), Y; a& W7 _4 U
{ n5 `% Z0 `6 B6 v# D
EFI_STATUS Status;
4 q% ^. f% D5 t+ \0 ^0 r EFI_HANDLE *DestAddr;
5 r# x* e8 b; k+ a+ r# C: G EFI_LOADED_IMAGE *FileHandle01;
4 c7 p3 @8 G) i, ~' `& } EFI_DEVICE_PATH *FileHandle02;
+ e/ r; K, i) h) z, ?5 ^( X EFI_FILE_IO_INTERFACE *FileHandle03; K& U* B" |/ U- d
EFI_FILE *FileHandle04;5 h- x( a) y+ V0 b }
EFI_FILE *FileHandle05;8 v3 D$ @- @6 j- D
CHAR16 *FileName;
+ x1 u+ F( e1 a8 @9 @5 D CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
0 g8 C( X7 g) z# _9 T3 ]- r5 I UINTN BufferSize = 8, BufferSizeKeySpace = 2;
) E. \. | B- ]0 W" g8 Y4 G int Bit0, Bit1, Key_Space; B* c) ^0 F: a( X4 N6 q5 c
% n6 p5 Q0 L% k( K$ P* j FileName = L"NewFile.txt"; @5 |3 m, |' U/ m1 ]! S# ]! x
Space_Key = L" ";% A+ X# G1 M. b
/ `- a, I2 B( ?: l BufferA = L"ABCD";% W* A, J* |6 F- G
BufferB = L"EFGH";
. P7 j) [; a( O* q8 _% m2 J BufferC = L"IJKL";3 o8 Q/ U- [# Q! }4 ^
9 I' g- Y3 c* ?
Bit0 = 0xff;
4 Q% O" K! ?& g/ N$ q% {% i+ { Bit1 = 0xfe;* W; j7 M" F* [0 l" T
! X; B; Q: N+ t9 A+ O0 Y$ O Key_Space = 0x0020;
* z4 V0 l( R7 y2 |0 U$ K9 ^4 r
2 a% t. S) P( p% R% B* m) x3 d InitializeLib (ImageHandle, SystemTable);
/ [; M7 Q! D* Z* c: g) s4 L
+ C. d3 K' k# M( R DestAddr = AllocatePool (BufferSize);
4 {' S% ]+ \: Z5 a% S) o& V- K; K: M B/ A" L( Y0 Q
Print (L"Value of Bit0 is %x\n", Bit0);* y" V. ^2 _7 ]2 t4 g) \& @3 N
Print (L"Value of Bit1 is %x\n", Bit1); 2 B0 K$ F3 Q" B* b; Q6 t
* Q1 f. G. U7 g% ^) b
5 ], j0 a: X6 W" L/ T% X0 { Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
' f' J) a( X* |: A0 A if (EFI_ERROR(Status)) {
0 t; s/ o8 J) d$ N2 C4 @ Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);' p! |. x0 N5 r8 D. [) s
return EFI_LOAD_ERROR;
" j5 R7 q% L& v8 N9 J! \$ q }4 X; B8 z% M& p) i& S( B4 U
p, m& U8 a3 s' m8 T% t
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
; \+ ~" ?6 ~! q$ g1 ]7 y* b& V if (EFI_ERROR(Status)) {
6 I6 H4 O( [$ M+ C! Q$ x Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);6 Q9 u. C/ Z2 h- ^% ?* Y$ t. J
return EFI_LOAD_ERROR;
/ y' F& n' K4 m% }2 l! _9 [% M } 3 c/ x2 `3 I# n, i0 N
. K& W* |2 _& l Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
4 T$ M$ ^& f9 [( \ if (EFI_ERROR(Status)) {( g- l3 W, \7 o7 o ^, O7 H
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
# o: k5 r, f) ^: L8 A; Y: |7 g" f! U return EFI_LOAD_ERROR;
9 G+ Z1 J" O: [" x$ e( N# [0 H1 U, y }. x1 q. T9 T9 [. t6 m% U
. l7 _! X$ P4 a
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);9 c3 e4 T1 D: H5 x ?: s
if (EFI_ERROR(Status)) {" L2 n. r4 n @- E
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status); q' k4 |+ L( N; m; `% ~3 {
return EFI_LOAD_ERROR;
8 F% M4 p* k* w/ M# \8 B }
5 k) {3 Y0 ~/ j- K
$ b( ~& L; J3 z Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
K ^7 K9 ]+ G k f6 S if (EFI_ERROR(Status)) {1 W% W6 A; \4 E: \2 G1 x! k
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
" e" M; W& J ?7 [$ E1 X, M( V return EFI_LOAD_ERROR;
2 @- [% ^0 e; m% }$ G" o. F \- _ }
8 p% C; P' `1 C5 {
0 r- K" C; n/ u Status = FileHandle05->SetPosition(FileHandle05, 0);
# K% m5 v3 H% L5 C# S7 x" } if (EFI_ERROR(Status)) {9 Y' k0 C$ H# B5 V% g, [; W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' ]9 E) M) \+ [, w: ?
return EFI_SUCCESS;& u$ m R3 N) d( H7 F
}
# \# q1 T1 ~: o ?
7 a& e. F: ^. O) W( Y' o' ]3 N Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
$ x: f' B% s6 b/ V2 w# S9 w2 h6 X Print (L"File Buffersize is %x\n\n", BufferSize);/ a2 @7 X( [! v2 G/ U
if (EFI_ERROR(Status)) {
, {& }& J* r) U$ s- L: p# y Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
9 I4 v- v0 ?" [, o! h- o3 o* O return EFI_SUCCESS;
; o) e {/ K X/ D2 \& U" S8 E8 K8 h }
+ @/ d% g! W, B/ C$ q" x
. F! {6 e q. \6 | Status = FileHandle05->SetPosition(FileHandle05, 1); 4 \& N @" i- g8 N
if (EFI_ERROR(Status)) {8 Z! F% L# D8 v5 ~# b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! N- a5 \: V, E* N
return EFI_SUCCESS;: j6 R5 `. H3 D) D/ N+ T
}2 b6 @* S2 M6 L. ~
( N5 K/ E# C: r& P! q, ^+ q Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
7 @/ i- h6 Q: m+ _, a Print (L"File Buffersize is %x\n\n", BufferSize);2 T% L3 A/ Q9 ]. E8 z
if (EFI_ERROR(Status)) {
/ X! O' W. N. p; l. ]+ g) F! a Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
! m: L& Z& x" U+ r# _ return EFI_SUCCESS;
4 @% d& z. Z, Q9 d* y1 y! |) Q; [ }
5 W" o3 a; |; L; \/ I9 V2 I% X" ~; K( E 6 H) v% J r2 z h
Status = FileHandle05->SetPosition(FileHandle05, 2);
9 B( W8 @! t; n+ ~& _ W if (EFI_ERROR(Status)) {
# {7 X" s$ L; H6 E Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( E. j1 |3 y0 @
return EFI_SUCCESS;
8 y$ W- w# W+ R$ A }7 [2 ~6 i B* A7 S
9 y8 M; o9 K9 x0 z0 g8 [
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
6 {& C* z$ z! y5 @, p7 A. R! E- v j Print (L"File Buffersize is %x\n\n", BufferSize); d) n' p2 y2 A3 H. \' `
if (EFI_ERROR(Status)) {
/ |3 i5 x* `2 \! o5 v7 s3 S Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' O% }- W% Q. l' p; r6 s
return EFI_SUCCESS;
3 `0 D8 D" ]$ w1 ]8 { }
5 J9 [, k+ R& c! C
9 k' M' D7 _# U* ~//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>4 m/ @8 G$ L. A2 o: _
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);+ ], b7 U& c7 ^, u" M: \. G3 ~- e
Status = FileHandle05->SetPosition(FileHandle05, 2);
" p7 q% Q5 z1 H, g3 M. ] if (EFI_ERROR(Status)) {
8 D: J( w. A% \2 {1 g4 K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' h4 Q: m0 s, ^- V/ H8 Q return EFI_SUCCESS;
" ?8 k# c, l3 W }- N# Y8 G# \0 w3 |5 c
) T5 l4 b& A: K! B! i4 G/ h2 k
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
& F/ U h! Y7 M- h if (EFI_ERROR(Status)) {6 Z2 C/ w, _6 M" U) X+ P( q
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);% c( E, w9 u$ k, ~
return EFI_SUCCESS;6 F i$ K( H, c
} 9 B) d5 V& @0 z
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);6 e3 Y" }2 |% _& z
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<) ?% f, v4 Q8 N5 O. t9 t; i! }1 _. A! X4 H
$ [! r% F" Z/ ^1 ?
Status = FileHandle05->SetPosition(FileHandle05, 10); ! i/ p6 L0 C; y
if (EFI_ERROR(Status)) {5 W5 o+ b" c R `5 e6 T8 n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
I5 u; |* W- i% }8 { { return EFI_SUCCESS;7 K/ x% K5 }, Q& d( y
}
2 F0 K _/ h# l% Z" B* o; o. b" R H
) b* o+ N9 G1 M7 ~6 Y0 { Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
+ j) ]+ ?9 A6 C6 m* m if (EFI_ERROR(Status)) {3 T& t- W; l; E7 j2 E& H
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 M- K1 W: @. g6 N, ~5 d
return EFI_SUCCESS;3 n' d; ~8 W' }0 W! G/ a
}# G0 p k, }$ \ F+ p M
7 N" D& J! Q$ L Status = FileHandle05->SetPosition(FileHandle05, 12); ' I T* N5 B( ? C3 S( N
if (EFI_ERROR(Status)) {
, F* O# T" Z( Z% r8 Q% U# f% n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. d; K/ ~# z1 G0 T0 `
return EFI_SUCCESS;" F4 f6 C3 g& t9 ?6 B
}
: W- ]' m. @6 c) N4 y& {; B7 G ) R9 G6 S3 D6 i* U1 ~
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, P. |9 C# M: R+ J: w- w if (EFI_ERROR(Status)) {
3 r- A& S5 w0 h5 f8 F& F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 J) @# F2 ` a5 Q return EFI_SUCCESS;
% M: g: v2 Q( |2 A e }+ a, `: r/ |+ {7 v) _6 a, c( H
+ h* h7 G* Z6 T/ o( w) ? A% C4 L Status = FileHandle05->SetPosition(FileHandle05, 14);
+ |8 h5 D! D1 y; p1 D- j/ @, M+ ?3 V if (EFI_ERROR(Status)) {
+ ^ k2 _) N& d9 V- g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) t4 x7 g8 u& G2 f+ H% @; k, C return EFI_SUCCESS;
8 g1 l& l2 v4 D' Q% q }8 V0 X8 t. Y+ H( ^0 R
$ O2 `" Z( c1 \( S; l Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: _3 m* O# g! q! i* Q
if (EFI_ERROR(Status)) {
! [" `$ K7 u9 f$ o( ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
# w7 T, t9 F5 \6 i- U return EFI_SUCCESS;7 S. I. Y4 y5 u: b6 d4 [* ?- z. l
}9 ?3 d# r+ U3 G% k& O) K; S( N
/ c0 }( Z( J; X9 d5 ~0 s2 ` Status = FileHandle05->SetPosition(FileHandle05, 16);
6 d6 i* }- ^" X9 y Y if (EFI_ERROR(Status)) {! i1 u( S9 S( n3 b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ r: t/ R+ ~: N+ P6 K) h return EFI_SUCCESS;
6 [7 C8 \' [& N7 W) T+ b7 k }
) o3 X) |2 X2 k# @9 d) c3 ]5 U* z
) |2 o& z6 c+ W Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: f6 U0 }$ B" `; B
if (EFI_ERROR(Status)) {$ O5 _6 h& A9 a
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 n q0 r4 {" W7 Q
return EFI_SUCCESS;) c5 W7 ]* c) i5 r4 }
}
! ~" o) I# C; m* z2 u* m3 k& \) L' J6 }9 i: `9 m4 [/ ` L
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
0 m3 f+ m& m# }* I) Z W; f2 h) f8 o9 u0 l" }4 \) E% s
Status = FileHandle05->SetPosition(FileHandle05, 18);
. p% f5 B$ Z+ a- E if (EFI_ERROR(Status)) {6 o/ D0 s! Y' d- I/ i, R. I
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( j" e) P5 F2 e0 ^- c9 F+ G
return EFI_SUCCESS;
7 X E* r3 i' b' o4 a1 m0 Y$ ]8 ]8 N }/ ?' C* |. ^$ l7 \: x* P
% y% N2 l7 p& ^' s* e$ |& Q
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
6 N @4 ] n# H$ v! i if (EFI_ERROR(Status)) {
3 P. a# {+ X2 g) n6 ]8 U" \ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ a( g# X1 U% ]; H3 Z" i return EFI_SUCCESS;
& i3 | t8 j* t. U }
" w5 e7 ]: K: `3 E8 | F+ V 2 [. c) b: e4 e4 w3 D0 H. s( |" n l
FreePool(DestAddr); , m/ v! c$ @) j* j
% p l1 V) `3 J Status = FileHandle05->Close(FileHandle05);
! x% I- v T7 k, q" S, }, L if (EFI_ERROR(Status)) {
) T+ m( A3 X) h/ m+ g Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
3 G- U' k5 o. z j3 @$ D return EFI_SUCCESS;
: S+ D k# g# _1 J# W }
4 `, W- E! A% i
$ i6 ^, v' u' }; X. v" b Print(L"File Name = %s has already been created.\n", FileName);
6 S+ w/ b: J9 I0 _. ]* t) Y: F5 `, \( C, x [9 w X% C6 j
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
7 @) P. c" z# ]9 \1 e WaitForKeyOrReset();1 r/ J: w; I K( I
& o9 j! I( v! @5 v5 f# G
return Status;- B9 J, X& c. M0 p7 V7 ]7 H
} |
|