|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/** M7 w* R r. i5 f, c
* myfileio.c
: g8 u5 } U9 f * Apps" [2 |' B" ~; \
*/4 W: W1 ]0 X* G
$ b6 z {' E5 [/ c: K
#include "efi.h"
9 J: o5 q9 w5 d+ M3 W5 O% v3 M#include "efilib.h"
$ X" A. q- U- P) J% F! B; l: O/ L
# {, v: L8 _2 F) D, }1 `& Q#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE6 p0 p, K) ]6 Z) {! v' }# B4 m
! E1 b( e" e" a, `0 {/ c7 g& Wstatic EFI_STATUS WaitForKeyOrReset(VOID)
; g% S/ H8 a6 v& P9 k: u8 e! L{ F# u- v9 l9 g) \' T* v
EFI_STATUS Status;1 o+ N' O/ n) O4 j
EFI_INPUT_KEY key;/ B4 k- O9 @( h, ^6 h9 N8 k+ }
UINTN index;
2 Y1 j* Y) E- Q& m. k" S, w( \
5 m$ H8 s! q, ], ~ for(;;) {$ s! J( Y/ R1 V# x- ^ k) b0 v, O
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
3 G" H% A/ k8 |( s0 F if (Status == EFI_NOT_READY); ]. Z% u: [* J: k8 W- e+ A+ ]
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);' t k/ v& N7 C: K
else9 z( v- |- _2 w) h& V: Z
break;
$ g7 |, m# ]/ Z1 b }
( H) v! n* M, G b0 D1 @ if (!EFI_ERROR(Status)) {
/ X% Z# \$ \# b# P" X" k/ @ if (key.ScanCode == SCAN_ESC)
- i( P% y; u. R) J- J RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
& _: d- ]# h% @9 P8 ^- g7 O }: Q, F% r% q" Y( W1 B6 M
7 f9 i4 }+ \; q0 B& a return Status;
- k% I# L2 J( m}2 v5 B. y: [! x
5 `- y# b, X% c: QEFI_STATUS& m" Z# y; O9 ^8 ?- o7 U# F$ _
EFIAPI
' N/ K0 `( I/ w/ z' {MyfileioMain (IN EFI_HANDLE ImageHandle,) ]) o O( i0 l. ~
IN EFI_SYSTEM_TABLE *SystemTable)
. A) z" P% _0 t7 o% \{2 H# I( O+ A6 h: X& Z* K( \; u7 e
EFI_STATUS Status;
& h7 W. G/ h: w/ Y5 l5 Y EFI_HANDLE *DestAddr;
4 L* ~0 N! I' \. r: c( Z EFI_LOADED_IMAGE *FileHandle01;( T7 r9 k: U' r3 x2 K6 j
EFI_DEVICE_PATH *FileHandle02; * y: I/ R! \2 i6 I
EFI_FILE_IO_INTERFACE *FileHandle03;0 m( v& t9 x: w' N6 r" k
EFI_FILE *FileHandle04;" E, g. P0 ^/ {. @
EFI_FILE *FileHandle05;+ z6 Y0 P! h6 \& P6 t% ^$ C7 I
CHAR16 *FileName;
' N3 p! o3 r# a CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
' g1 h# B9 L+ a" f( e UINTN BufferSize = 8, BufferSizeKeySpace = 2;
/ @( p7 [5 m. r# r' |8 x5 U) j int Bit0, Bit1, Key_Space;
" e; E4 M% y5 ]- l! r7 X6 c3 m; `7 R% C d+ `- P
FileName = L"NewFile.txt";( N; m m, D/ P* w& m" t
Space_Key = L" ";& @% Z3 |% H7 D, w" b$ p
" D3 I; M1 T+ h. Y: E2 M& B BufferA = L"ABCD";) K& ?6 D4 t: |# f. i
BufferB = L"EFGH"; J- p9 y* R% V# U5 }/ U
BufferC = L"IJKL";# |4 @! }/ R( @# y* D4 X" B
; e P$ c! f7 I% w. d. c4 \
Bit0 = 0xff;
4 Q1 e, U: I' a: M. s Bit1 = 0xfe;; \) p- V1 x4 c0 }! P
( b; u9 q& K3 D. m/ F
Key_Space = 0x0020;
* I" B( G; o4 K3 }. N( Q9 q7 O' j
) m* k/ ~" @" ~4 `! T0 b! Y: Q InitializeLib (ImageHandle, SystemTable);
! h* k, f+ `) x7 U. O" M/ H, U8 l8 I
' K T5 i& R2 G# }3 _ DestAddr = AllocatePool (BufferSize);
3 F' ?: e! e0 [! `+ s% i3 {& g! H/ @) }
Print (L"Value of Bit0 is %x\n", Bit0);* g0 L' T5 [" K0 E6 g
Print (L"Value of Bit1 is %x\n", Bit1); 6 h5 t8 f: `& B" x
/ d- q4 [% y, t. q% H/ M 5 J/ W3 O! J2 A: _# C6 e( I1 D2 p8 E
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);. e! B; d1 n) b5 L# m3 d& A7 l' c) m
if (EFI_ERROR(Status)) {
7 U0 Y/ x8 o" D6 F Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);$ j0 `" @% q8 ` M3 V" S" e
return EFI_LOAD_ERROR;
+ m. I2 ~8 x9 s5 x }
* C& `( w6 I- Z! O/ \( `! b! ?
) h& b- H5 d( q9 v& ^8 H Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
; y' c7 ~4 e$ j# Y! x _ if (EFI_ERROR(Status)) {
. [, c3 o$ ?. z) c( B Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);8 `+ `! @" _2 w5 h
return EFI_LOAD_ERROR;
) `% m/ }- K: s. {. C n } 3 ]# H8 @, o, R6 A6 p
9 A4 l5 B8 Q+ a2 U1 G' B" y# [ Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);. Q. e: F0 s% k- v
if (EFI_ERROR(Status)) {; O$ A! v2 Z ?1 o+ |
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);/ ~- A4 y9 n5 l: M1 d8 I& {
return EFI_LOAD_ERROR;
7 F# y+ D6 x* w/ s1 q }( d% T3 L4 k" z! c
# E8 D+ d% O$ I8 w8 D9 J
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
6 X+ ]+ t; W2 h) t3 _, C) I if (EFI_ERROR(Status)) {
3 [* {7 C6 k$ w, `* W Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);# K% x( w0 i9 ^$ B0 r
return EFI_LOAD_ERROR;0 e8 X" \/ h8 v' _# r
}
9 W- @& J) E7 j7 s' A" | [1 K. ]6 L5 q: E% s
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);- o" M9 r1 K1 T) h0 a
if (EFI_ERROR(Status)) {9 Y+ w0 y7 E9 |: r$ ~0 d
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);! Q1 x: D; O0 z0 u0 ?* e* Z
return EFI_LOAD_ERROR;3 G+ Y! r* V- X. j7 l
}+ P9 L7 K# C _0 Z, |1 D5 N
4 a% G6 n# V9 a; r- A
Status = FileHandle05->SetPosition(FileHandle05, 0);
^! C' g$ t# K4 g if (EFI_ERROR(Status)) {: l( D. K( o1 Q( G+ }
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 ^+ _$ W* n. z9 n0 l return EFI_SUCCESS;! U& a J) f9 f' Z) k: S
}
/ K" T# r _& L4 G& X e7 W* E : X; u" d. \ l9 Z; q i- d
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
7 i( |0 |1 K1 K. g9 [ Print (L"File Buffersize is %x\n\n", BufferSize);8 \" I4 i6 B; N' d* n5 D- i
if (EFI_ERROR(Status)) { x& E* Y, R4 n7 l C8 u
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# i2 b( X h$ r+ m" n4 o, s return EFI_SUCCESS;" S# z* `. b* z) C/ B/ q! X
} & ?! ~/ K- K6 C
+ K/ _3 @& @+ \4 t+ c
Status = FileHandle05->SetPosition(FileHandle05, 1);
; Q/ f1 {. W; @2 R: x if (EFI_ERROR(Status)) {
3 i3 L7 \6 @2 g4 s4 E5 }+ a' k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* k5 x) S% c6 h return EFI_SUCCESS;
7 P% R1 ~1 X1 w- q) Q }
; i, f: q; v- E$ n 4 X- e; i: Q0 ?7 H0 Y" o+ r& V
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);6 [! K# m4 P* r. H& a
Print (L"File Buffersize is %x\n\n", BufferSize);0 Y6 ?& v# z C W
if (EFI_ERROR(Status)) {5 p P P/ [, d9 z! K( s% N
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);: Y7 \. P, z, O6 G6 c( H
return EFI_SUCCESS;- ^/ P4 l- U. b; O# h
} ; }" U9 m3 F& H7 Z4 ?7 D
! e+ q! p6 L; o2 y5 b9 d
Status = FileHandle05->SetPosition(FileHandle05, 2); 9 @& ~* Z6 M' Z
if (EFI_ERROR(Status)) {
' F- Y. c& _2 u7 e Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. ]# f, R2 v/ f
return EFI_SUCCESS;
4 s3 D3 \# T9 R5 @8 Q1 t$ x }9 F; T4 M, P6 G* X3 d8 z
6 t5 P) `) }: [" v5 D
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
$ I- a0 Q3 {; R Print (L"File Buffersize is %x\n\n", BufferSize);
( i( \' ~# d3 I if (EFI_ERROR(Status)) {; ~# ?1 z6 Y8 P' N: w2 P7 N) @
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);& ^, ^) k* _+ \* \/ X
return EFI_SUCCESS;
* z: s- }: ~6 f5 F1 S% I6 o }
7 N% L: c; o/ |9 |) R$ ?- O3 [6 v 7 L- l8 O- v' C0 Z# g5 V
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
3 d9 S$ Q+ q, P0 C i Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);0 z: a8 } a% N' { q
Status = FileHandle05->SetPosition(FileHandle05, 2);
( M. g. l- v$ \4 j if (EFI_ERROR(Status)) {- q4 h1 I' H- P* k% I& k) T! w
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" w$ y& l& o9 [2 t, e M return EFI_SUCCESS;
4 B5 Q5 F5 t7 y* Y } |3 o( d, }% T7 [. P% [
2 K& R5 F9 u' F9 m, L
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
, M) R2 E- \ ~5 E if (EFI_ERROR(Status)) {
# @9 h9 l& A# y1 ` Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
# k5 ]4 j X% d/ l! Y) T' J2 f return EFI_SUCCESS;
9 z7 O- X9 p+ z- \- u( t/ g } 5 L2 W$ D+ L5 d8 t
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
/ s2 N/ }" G" V }//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
4 ^/ W0 f' H9 _/ G
, {6 c8 A4 m. ?9 J Status = FileHandle05->SetPosition(FileHandle05, 10);
3 y6 B ^: B7 u& l+ N0 l) b if (EFI_ERROR(Status)) {. b7 p3 X. y9 x/ T: O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 }- F/ i: i+ Z5 b0 a2 r: Z: L return EFI_SUCCESS;$ a7 d- D; a' V- k- C8 ^1 v+ H
}
3 W! k# S& j1 \. b) ], N
2 E# g) x% y) ` Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);" x5 `. E& R% g+ d4 b
if (EFI_ERROR(Status)) {
x7 E" \" M' K6 x, ?, ?/ z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 j4 M' L; J, g1 i- E. j
return EFI_SUCCESS;8 g L: s# J& C) l
}5 q; m- |2 {6 n7 f
% \9 F2 B2 X/ O Status = FileHandle05->SetPosition(FileHandle05, 12); 1 h+ D3 U0 S4 E j/ W0 v" k0 A* a
if (EFI_ERROR(Status)) {2 |! D. \0 r! p9 b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 E- \: T9 U/ E: u4 }2 \" Z return EFI_SUCCESS;9 o0 T8 G$ o. H3 H! s1 X* V
}5 ^* S( G' Q4 J4 i3 m
9 j4 w/ `( I5 r1 @8 [ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);4 s/ ]& t" P7 l9 q8 ~/ O/ w: X
if (EFI_ERROR(Status)) {- A8 |- Z5 C+ r( |- d2 s
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);8 S. Z/ o) C: s
return EFI_SUCCESS;$ ]4 r% A7 {, P& |- _0 N2 m
}
7 V7 ]/ X- w4 A* J
. W! O6 S4 ^. m3 }' ]; G' h Status = FileHandle05->SetPosition(FileHandle05, 14);
R, Y+ C& B2 [" W) P if (EFI_ERROR(Status)) {
& F4 K1 n. m a) l Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" B! R U9 W; Q+ m" B return EFI_SUCCESS;
5 H+ v( H; Y" ?/ Y! ` }
8 n1 r3 P# B% k/ {7 u6 \, K
1 T) ^. m& ?' j O Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* d8 c6 k! X* G0 w3 E if (EFI_ERROR(Status)) {( ?/ _4 X0 v1 m* b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& Z3 I# Q8 y0 k6 U
return EFI_SUCCESS;
. f" |. ?+ {; F$ d }
+ w/ S" X% a7 c: R. Z) S% b h1 R5 P H, J, k# V/ C1 p6 r
Status = FileHandle05->SetPosition(FileHandle05, 16);
4 v2 z9 S7 f% Z" p4 {' m7 v if (EFI_ERROR(Status)) {4 Y; G$ f! i, ~: a. t& y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" q2 T$ W& N( H; M8 o return EFI_SUCCESS; |1 }1 F8 _" m
}" m: ]5 b+ z1 C% f$ }! e) T8 M
2 u' G. h+ O$ F. ^& w, O$ t: ^6 t
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 C, X1 E" F* Q" {3 \/ O* Z+ x7 A if (EFI_ERROR(Status)) {
) ]8 y4 a! n+ n$ H) H. ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 o7 g+ C) u+ D! F return EFI_SUCCESS;& Q8 \4 n; ^7 A3 e" E2 H" L
} 4 D: j8 a3 G9 |& B- U3 B6 {6 e+ W0 z5 Z
" z( ?) _0 f3 D( I1 Q//---------------------------------------------------------------------------------------------------------------------------------------------------------------# u% O; Y6 r% e M
6 {) R7 a! @# R Status = FileHandle05->SetPosition(FileHandle05, 18); 2 [" G: {, Y- @' K8 q! b% g7 \
if (EFI_ERROR(Status)) {. |% V) w$ Z; P3 a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; X& V4 F4 [& |7 V% k' v return EFI_SUCCESS;+ R: ?6 @: U" J& l% U
}! ~4 u: h% A ^3 e1 N
* Q+ i+ Q: z. m# H Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
7 B9 Z+ P& B* R" h if (EFI_ERROR(Status)) {( x4 n$ k' {4 L1 F7 u
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
P, _* F6 y' B8 Y return EFI_SUCCESS;. [! r8 S4 _# n0 h6 [# m. D; Y0 p
} 5 p4 a! z, q+ T2 f+ Q% ~" U6 q- Y
6 D8 A9 v! {% o
FreePool(DestAddr);
% | Q: b( J! X s7 J3 t& v/ c7 [9 U; F+ E
Status = FileHandle05->Close(FileHandle05);
. i5 ]+ d3 \$ o' m* l: s2 w6 ^ if (EFI_ERROR(Status)) {
* f% a3 e! {3 I0 R Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
( }. \9 Y; {8 z# N return EFI_SUCCESS;
! Q' |+ Q5 \0 |$ W* {- z4 v Z3 f }
Y+ y& T( O0 L; B; q. u% _* g ' q% |0 B, e% L, O
Print(L"File Name = %s has already been created.\n", FileName);' v! Z5 A4 E5 T& [
* x& e4 E y2 c& n, e! t- y/ s6 E Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
; ~; s! ?$ }7 H6 i WaitForKeyOrReset();
7 \5 F2 T9 R8 a R
1 d; \9 D- N( o) K1 \. Z7 R return Status;$ [* _/ |" l0 x" ?
} |
|