|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
- r! G3 v$ a& u( ] * myfileio.c) e9 ]9 q- F! b0 s7 `$ e8 _
* Apps P6 C$ }3 ^% L
*/
; ]6 M1 Z6 t5 J+ q' B/ j( K
+ V: ~1 ^6 u" P/ ]% J+ N1 h#include "efi.h"9 T* F% a+ N6 C- \1 z4 q
#include "efilib.h"
j# D5 |& i4 J$ t5 h. z4 Y% G0 l0 N
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE3 W$ n+ \0 b& N" s% t+ M
; d& |0 Q- {2 @$ J/ C( kstatic EFI_STATUS WaitForKeyOrReset(VOID)
" t( i, v7 d3 b& t2 j9 p{
; B# k W3 N6 S$ P& O EFI_STATUS Status;* W c* m' e/ t6 d# P6 p
EFI_INPUT_KEY key;
5 d5 a% }$ `3 z1 w3 _, @ UINTN index;! i7 W1 P; w7 f& N. s: _2 f3 O
% D6 e% K0 p' z' o; \ for(;;) {% V) F: H$ b7 i! k/ e# |
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
+ K5 e# d" p0 |1 e if (Status == EFI_NOT_READY), w% s, \7 x/ t) y% |
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
6 N. C {5 `8 n3 g& d! i else
4 P3 Z. U6 Q" C$ ^+ Y4 x break;
( q( T/ k% W5 o% N) t }1 u% M$ ~2 N% V) V' I8 q3 j- z
if (!EFI_ERROR(Status)) {
) C% @# a3 j' C4 o- X7 p' i* @# J if (key.ScanCode == SCAN_ESC)
( }: m8 @3 J4 Q$ G RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);% z& F% p# ~4 I
}9 B% @2 S# l: J) U# m! s
1 Q) \3 W7 {5 m6 B* |& ]0 s. \( F
return Status;
: d+ s+ k5 k9 Q6 t- x. p}
* v! r( m4 a3 C3 r- q
. u! c! \7 ^/ N+ n( \EFI_STATUS
5 P( w. W; }& G% P. f/ xEFIAPI) S) e& F7 k) s, [
MyfileioMain (IN EFI_HANDLE ImageHandle,8 M5 M' H. ~3 o. l0 z5 y
IN EFI_SYSTEM_TABLE *SystemTable)
, q% ]. S3 I9 i$ I% F) e& F{
' f. z: D+ K4 ~6 T# n6 s8 } EFI_STATUS Status;* B% e- ^4 w4 o& w7 K
EFI_HANDLE *DestAddr;
' O" \9 j/ T+ e+ m; F' h" y EFI_LOADED_IMAGE *FileHandle01;# L5 ^5 V4 J$ e$ _, u
EFI_DEVICE_PATH *FileHandle02;
& k M2 y3 e$ b1 P EFI_FILE_IO_INTERFACE *FileHandle03;
3 Z) y. u6 D5 o EFI_FILE *FileHandle04;) q0 `: A0 t) D+ |' V4 T. G9 x
EFI_FILE *FileHandle05;' `) E: X/ t, U4 k7 l0 R8 V
CHAR16 *FileName;
5 f& P! H% ]8 H; F CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
1 _+ W5 Y7 f9 @# }" R/ K; {& v UINTN BufferSize = 8, BufferSizeKeySpace = 2;
5 [! g) e& l% H. r. [/ n int Bit0, Bit1, Key_Space; " }4 _. H) |2 s! [* m4 S5 u
1 X1 ~; ^3 r# y1 ^# M' y
FileName = L"NewFile.txt";; ]3 l/ v8 L" R5 o) l1 ?# y/ D0 a
Space_Key = L" ";
) ?% r/ f9 |7 ]% b$ k( K0 I' w8 ~
0 Z8 P# n; M S s BufferA = L"ABCD";5 N0 G# R& Z8 J; Q
BufferB = L"EFGH";
z0 E% J; a1 s' m5 N# d* F8 ` BufferC = L"IJKL";! Z1 P+ l3 r. S( \
7 e2 N4 B, { }1 ~) d
Bit0 = 0xff;. L. r. d4 f) D1 f# ~3 p& u
Bit1 = 0xfe;
+ B/ g- D F# C& v: Q
7 Z4 b6 q0 j1 N( Q- S Key_Space = 0x0020;
# `2 c2 K) O8 D8 n
1 j6 F* b( i1 t; p1 N InitializeLib (ImageHandle, SystemTable);
2 @+ ?0 b2 N6 o, b: C' O9 j8 ]: V4 A" U! z# U }( E7 j
DestAddr = AllocatePool (BufferSize);
0 T1 N' w/ a% `* B9 t( g; d. \0 @4 |8 W! m3 e# n {
Print (L"Value of Bit0 is %x\n", Bit0); X0 f: \: y6 ^1 T0 G
Print (L"Value of Bit1 is %x\n", Bit1); ) ?5 N" B6 d/ D5 X. }: O
7 u& g& z! M" Y' d
) [& o7 `& @5 Y2 t Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);: T- M5 s4 f8 Z# T- B1 G! w% m
if (EFI_ERROR(Status)) {
" M) r' Y# n) k Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);9 Q f$ F6 t5 o( G) N. p
return EFI_LOAD_ERROR;
$ `0 B K) N% D, j }% `) J; Z+ [) C* `1 d1 s, c1 x- r
8 b, ~3 c- ?0 T- @
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
1 O9 Q+ p. |9 F' C if (EFI_ERROR(Status)) {. ~- q7 M' }/ P4 V+ G. l$ J
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
6 _ R. e- T7 Z return EFI_LOAD_ERROR;! [+ M, W+ S1 g& j1 @7 }
}
* \& W7 z! N" W. E / Z) T* p* s& @7 y' |6 [
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);9 W' S% P" o/ T2 { j8 j
if (EFI_ERROR(Status)) {3 \1 }9 m' m* Q; B' b$ P- b
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
( m; b& Q( F$ \8 b y8 r return EFI_LOAD_ERROR;
0 g* y7 ~/ q! S5 I) |" n }3 Y0 w* ^$ S( x: C( O/ ^5 E5 [, V1 v. C
* {- V! e# [: Y0 {+ f2 g Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
4 p" w9 L& p P) z if (EFI_ERROR(Status)) {
9 U" a3 N5 ]( ^( p Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
; T% H8 v7 e1 l6 U' u( s, n return EFI_LOAD_ERROR;( S( K5 C. B9 G/ t/ m2 t5 U/ l" Z
}
O, L& H$ G* {6 |$ \% ~: v $ o/ P2 ]3 J ~2 h/ g8 v7 f
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
3 {; A7 D& |/ U% | if (EFI_ERROR(Status)) {
) E% D5 Q6 g& F( p, Y x6 C! b4 L Print (L"Could not open file, FileHandle05 err code is %x\n",Status);6 P6 X/ S8 G! _3 [' I* @. q
return EFI_LOAD_ERROR;
+ l- `5 t8 x# B' L5 h/ a }9 \% C- ]2 Q- m, c. }+ x
; X7 h# s t: n$ v4 e Status = FileHandle05->SetPosition(FileHandle05, 0); 8 ~% a Y( A, t& R P# W7 G2 d
if (EFI_ERROR(Status)) {' P$ j3 @3 W" P' L4 \" H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ ^' B2 `+ I& ]5 G! k
return EFI_SUCCESS;! \1 t6 [ f7 r9 I% N
}- |3 _# l; q& B1 E5 T+ N
7 o0 e5 J& y0 \2 r* I* |! G3 o4 c
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);& q) w, y8 h: \
Print (L"File Buffersize is %x\n\n", BufferSize);
$ q# X& u+ q* ]) U if (EFI_ERROR(Status)) {
! {& U0 \. M1 j, a. ^- n Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);2 O+ s. I& S' u1 {
return EFI_SUCCESS;; {' Z e- ]" G3 Y2 E% X9 H' V. n
}
% p- |) H& y e s0 \) m6 u% Z, {# M' r
Status = FileHandle05->SetPosition(FileHandle05, 1); ; }8 L' \+ {6 j
if (EFI_ERROR(Status)) {
+ U% \$ ^( X a/ m- h* \( t7 `* } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' O) l" @: D% S/ w g
return EFI_SUCCESS;$ t5 ]8 F$ Y+ `/ {, e
} L. Y* j+ T' h# I- n
5 v# V6 m' I' S8 m% g) ] Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);" M' }* E x X9 l
Print (L"File Buffersize is %x\n\n", BufferSize);
7 o! Q9 l1 c/ \& R6 O if (EFI_ERROR(Status)) {% u2 D: U5 e0 k J; H$ l1 u, V
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
6 Y. ~- t/ x' k) M" q, q" t return EFI_SUCCESS;& d5 K- I& l, p' x& `0 A; Q
} 7 A3 }! r. i S, I5 }. E! c
" ], B8 g- @* a/ Z2 A' o* L
Status = FileHandle05->SetPosition(FileHandle05, 2); ' }# b( P; T6 G- L' ?
if (EFI_ERROR(Status)) {; j+ x1 r9 I `% Q# [+ x, `3 {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" n- y& x) O2 u M3 V4 x return EFI_SUCCESS;
% n7 n8 o) O2 w4 W4 W6 m }; c9 H' b: S' Q* |
! z1 a/ }! n; J8 p1 l0 ~
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
# A9 u5 H, @/ }5 |/ Z' D Print (L"File Buffersize is %x\n\n", BufferSize);
7 w8 P+ |: ^7 D8 I7 |; }" Z4 f if (EFI_ERROR(Status)) {. `7 s: ^: y8 r- C- ~' E
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 ~ ~+ ~, A) X& i& M return EFI_SUCCESS;1 |) C- _8 } d$ y
} ' e# v% d4 e. z1 F) \* M) [
, h. k& S; S4 N& [8 i//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
! h6 }% F7 L9 s" A$ E& o; \ Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB); x& s0 B& p: F( s1 q% `
Status = FileHandle05->SetPosition(FileHandle05, 2);
" I6 |( V* x/ A4 [$ u5 C if (EFI_ERROR(Status)) {3 F7 E7 p; N) Y: T$ e. F% c8 \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! P* G- q. Z. C5 F- z Q
return EFI_SUCCESS;, I( ~: }/ Y6 P% k' T {( B. O
}% l' A% _3 }; V* c- ~' w4 D: N: D: w5 S
* t9 w, b& W# s* ], [
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);& ^, Y7 F7 D7 w
if (EFI_ERROR(Status)) {
( G% S. m) F! w1 r Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
9 f9 o4 ^4 R7 e w return EFI_SUCCESS;7 {& Y& `" R; F/ N2 L
}
! U& j: j( g! W8 a! D4 s9 Z# s Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);- _+ U. q5 g0 X( ]( L9 L2 r1 m
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<& O3 \5 o5 @! @& R
5 H a# i4 @* o0 K, K Status = FileHandle05->SetPosition(FileHandle05, 10); + R ^+ u7 H$ ?$ e" Z4 O, G, ~
if (EFI_ERROR(Status)) {+ O# [& @* {( `4 F* w( l' W$ G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 C# r* X( W/ X' Q6 F return EFI_SUCCESS;, f( R$ z3 a5 x
}
9 ?2 d9 n2 ?0 E2 K# \ | 9 `3 E( H/ T t' _; _
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); ^! N( r: e, ]' ~
if (EFI_ERROR(Status)) {
# g! \8 `+ q6 w( h& M- p( G" }* u g Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) y2 X8 g! v# r: v2 a, G( @
return EFI_SUCCESS;8 V" A) W1 z% r
}
. ]! E( @1 z; e- T7 \
& h& @1 d* ~! P9 W7 y/ X Status = FileHandle05->SetPosition(FileHandle05, 12);
4 m- R3 b" }. v. e% x: C if (EFI_ERROR(Status)) {
5 t% F- G- ~! H: X: y2 i+ q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" G1 L1 R% W) Y9 o5 D" Y return EFI_SUCCESS;" |: }1 Y. U" r. r0 Y. z: c7 y
}) t5 U1 o7 I2 T, t" A8 s( S+ H
; ^! C, ?9 T( P% }( g* v5 R8 P Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 n4 G! ~, H$ v5 S( u5 g: e% u( g if (EFI_ERROR(Status)) {
3 L% |4 S$ P3 x7 i# ] Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 @4 p$ [. S) O
return EFI_SUCCESS;
2 S* `" f$ c& Q. P }8 m9 e& v9 z" y. ]: W
: d! G. B$ F& Z3 W6 i
Status = FileHandle05->SetPosition(FileHandle05, 14); 7 S. Q0 M3 |# |: C4 B
if (EFI_ERROR(Status)) {# `/ i! w" |: \% f5 W" M1 t
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. U$ t( V6 S( A. S+ q& u. } u return EFI_SUCCESS;; S9 r: {/ L" B: a, E
}
# `5 P& P) }" B! Q) n4 v' i6 ?$ b3 |
, I5 l" I! r. u, I6 q/ k: T8 b Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 V( Z& P1 v* j( m if (EFI_ERROR(Status)) {
6 l, g; ?+ [) o( r Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 }. Z9 U1 l- H6 k# E& D
return EFI_SUCCESS;% C8 `9 W* e4 E9 {$ O
}1 M7 U' g3 V7 S `3 I, _# d
+ p n F0 a" @
Status = FileHandle05->SetPosition(FileHandle05, 16);
7 }$ f4 U( q+ T! n* m' r: i4 u" S if (EFI_ERROR(Status)) {
R+ I* k2 u F. ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 ]% ?* G3 |# f# b$ C+ w return EFI_SUCCESS;: B+ F) ~3 p# Y
}
- ?3 h0 Y/ f: G0 x + _7 W1 Z" }, i, W& a7 z( Q
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 `& L( i+ a: R9 u5 ~& s
if (EFI_ERROR(Status)) {' v* E) t% i4 G- C+ z+ r w
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 X/ `% v$ O2 D V; j; ]% C m$ _
return EFI_SUCCESS;
5 P7 C9 N( X4 s8 b. l3 D } + q# b/ t r3 ~6 E. w/ |
) N$ L+ Z% I2 C X//---------------------------------------------------------------------------------------------------------------------------------------------------------------
- ?; U$ c( K+ q$ ?$ K {% s) ^; J! U/ q* e, F
Status = FileHandle05->SetPosition(FileHandle05, 18);
2 [8 A* Y/ O/ y2 a+ z( `* w$ K if (EFI_ERROR(Status)) {6 Q0 }* R" r0 m% T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( y6 W `. e' X( w$ }" ]
return EFI_SUCCESS;# y3 V% F: F/ Y" T. }- v
}1 s a% u8 x- q5 M* K
9 Q8 `3 \5 U- ^, w% L4 c
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);' V0 k5 e( z3 q) ^
if (EFI_ERROR(Status)) {
/ t @) ~( L* z/ ~$ D Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. t% M! e5 W) r% u
return EFI_SUCCESS;
- k& s# s: u1 `! ]/ q+ Q. u" B3 h } + R0 i8 q) r- i9 N5 k
- O' m# e0 F. z0 x! T2 Y FreePool(DestAddr);
0 C# T3 {7 } A0 U8 k* g8 h. _* k# L' u
Status = FileHandle05->Close(FileHandle05);( b, m! o7 K- k4 K7 i7 J7 U
if (EFI_ERROR(Status)) {9 L8 d' k/ M7 F* i8 ~! U/ s: C
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);4 | h+ H- G" M# r
return EFI_SUCCESS;
0 F4 f: r8 V; \' r* R, D4 y) {, f }0 d9 f0 ?1 @3 K
( |( {) i6 R& L) t( {* P Print(L"File Name = %s has already been created.\n", FileName);
; i. j9 f$ T- v' d9 I# U
; \- L! w2 V. D: e2 s% F* m1 i Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
+ v5 t' n1 i5 m2 E! s WaitForKeyOrReset();8 F+ x# x. `8 m* `9 i3 o7 c
( @# e/ C" ~* q# |* D1 q$ _, W# I
return Status;
" Z l" J; \1 d' G1 Q} |
|