|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
^3 N, |+ W; l, k( a1 _ * myfileio.c8 r! s% n1 Z) C4 N+ c5 w0 c) d9 u
* Apps
- [: H, e# B4 ]' u4 v' ^2 U6 { */: ^; ?3 }) s8 G! a2 s
" P/ q' m- o; ^. v8 E. R( c& N
#include "efi.h"/ Q) E, b/ d5 s& g0 M2 T' k( Z
#include "efilib.h"
7 r! ^! s5 j7 E. f& ~1 m6 W1 d4 _1 g& V) |* d5 p
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
) I6 D7 t2 q& J! U5 O% l
3 h" ?5 H" T# G7 G; d$ Cstatic EFI_STATUS WaitForKeyOrReset(VOID)6 P8 {+ B" N+ [ `4 v; c* L
{
6 ^, F4 g5 x! v7 R0 S! G& Q7 { EFI_STATUS Status;/ l+ a. y( X1 q% ~- L4 F- s' Z
EFI_INPUT_KEY key;- s5 M0 C7 M% `! v' A
UINTN index;9 |8 ~8 l- T3 p: r c6 H
) X9 i) C: W ^9 I( Q U, T; g
for(;;) {
& `9 f1 u* o) R Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);# m0 n# e: a; i( C3 G3 B/ s' U% z1 G+ N0 \
if (Status == EFI_NOT_READY)* N; j! ]) [8 ? _" P
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);3 h- W$ @+ A" |# l( i& Q
else: h# ^& Z$ D o, |* A
break;4 ~ x5 {( i1 [' z8 v8 w: D- x
}
6 e5 }) K+ ~4 [8 _* O' } if (!EFI_ERROR(Status)) {
& Q* P' u0 M' k if (key.ScanCode == SCAN_ESC)( F1 k+ x9 G# p/ B* l3 [. R5 j" M$ o
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);) c \5 @" u% P4 k' W! H# W
}+ O4 M' c; Y# j' l! ?3 A6 g9 A( d, O
* @* g# q& M1 e \* R return Status;
- m+ P( s9 k6 g! O) |. P}# j6 r: e2 b/ D& h& @
8 O2 ^# h0 a6 a3 W' H6 r3 c
EFI_STATUS' s: O7 I) t: l0 e3 R6 a' }# k5 x
EFIAPI
, F; [) m% B% I# m5 cMyfileioMain (IN EFI_HANDLE ImageHandle,
9 T: U4 l" ^( } IN EFI_SYSTEM_TABLE *SystemTable)) A6 `! x( E2 v9 ^' V
{
6 p( q9 x0 n7 n' q EFI_STATUS Status;
: G! c5 {1 W1 } EFI_HANDLE *DestAddr;
. g5 ]' I. E# k/ e EFI_LOADED_IMAGE *FileHandle01;
: L" J. A- p" q EFI_DEVICE_PATH *FileHandle02;
. J0 F- r# U. z l/ P9 [ EFI_FILE_IO_INTERFACE *FileHandle03;$ @5 V. F& M3 k
EFI_FILE *FileHandle04;, I8 O# V3 V3 M- g- V. c
EFI_FILE *FileHandle05;
/ S: `" R/ E% e CHAR16 *FileName;8 p$ p: k/ O( @# z1 i( z" m) ]
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;6 B) e( B& n7 i8 `2 Z
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
9 R) a, E5 U7 Y+ D1 [6 F int Bit0, Bit1, Key_Space; $ L) H4 J V7 \2 h) A! G
' w) D D+ S* z% g5 W FileName = L"NewFile.txt";
* j$ w8 ]! ]! b& [7 y1 m* v! c Space_Key = L" ";
* q/ L: @( T' w) m& |8 ~
* c8 e& t5 q! T1 G: P. c& ]. j BufferA = L"ABCD";) [6 O& w4 i4 O( { z' ]6 l
BufferB = L"EFGH";4 z6 {8 X8 P4 D- U) m
BufferC = L"IJKL";3 p+ e3 Q+ I5 f
* a0 H6 _+ ~) d% Y3 H1 P
Bit0 = 0xff;$ N- o/ h, ?' ^2 @* G
Bit1 = 0xfe;2 h2 q/ C7 S4 ^# h5 W$ G- b! _
( ` P) w$ q. k6 W
Key_Space = 0x0020;
( l( r1 L8 E8 E$ u% [ ; G! o3 W$ \. `( h2 S
InitializeLib (ImageHandle, SystemTable); & F; x# r5 l) G+ U$ s& c2 s
. c; K/ C) z* `4 n0 ?) v9 w DestAddr = AllocatePool (BufferSize);
, a8 U$ h6 \% {9 Y0 V( { p: ]: B3 ?/ j4 y/ ]; k' }
Print (L"Value of Bit0 is %x\n", Bit0);3 F/ O! X7 j1 u1 k. D7 ~1 ]
Print (L"Value of Bit1 is %x\n", Bit1); " j$ z0 _# K# x8 b. v0 J
* W; O9 J4 f; i" I' U6 i
o+ [/ v" w8 K9 M4 M y Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
# A7 [' |, E# B3 |, g if (EFI_ERROR(Status)) { x# d& W5 }- p, }$ Z; L& o E3 n
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);5 t* d; p. |7 |- u) h
return EFI_LOAD_ERROR;
9 P# M1 v& v1 X0 i9 p2 e }
/ z' D3 i* y# K$ t
( o7 B4 U) y# n9 h8 Y1 i Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
1 m$ F) d! m a" X6 c2 m4 L if (EFI_ERROR(Status)) {1 Q0 G2 e1 |8 N; r4 m3 {( X
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);( q7 A9 d8 y- r/ L$ X
return EFI_LOAD_ERROR;) p: e. I7 G/ K; G5 k# V% V
} * W3 F) z1 l4 h' }+ a* P
: K3 ^" k2 ^# G, O! r+ h, V3 `4 y" M
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
7 U9 q" ?- M5 w5 H5 X if (EFI_ERROR(Status)) {, ]2 m- f6 h. o+ m& R0 m
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);$ @" {% F8 P. m. d
return EFI_LOAD_ERROR;/ x. ?" M7 I5 Q; r! g9 ]7 ~: ]/ }6 p
}
7 o6 p& q' I8 a3 S
# O, w' G1 ^) O/ q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
) Z2 [" D5 D* X if (EFI_ERROR(Status)) {& k" C% V; e @/ ^ W" v
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
2 ~8 V- O' |8 `- r, t+ t1 Z return EFI_LOAD_ERROR;* K4 H! E! F( n6 y$ E' H, `% K4 z
}
' @$ F) q9 E3 K0 _2 S" L; {- ^ + c: [5 H) ^( J% y* o4 A% c7 w6 r8 P
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);# s+ s; A' x, X2 j" E* J& Z
if (EFI_ERROR(Status)) {) i7 C( G5 d1 B7 n' f: ?
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);8 w8 R6 f( d3 W; N. m1 [- a
return EFI_LOAD_ERROR;. }2 A( x0 h8 {) A* [
}8 Z+ @5 p. `, Q5 L! h% w7 U
* ^6 X! c' s" W- A% ~ b
Status = FileHandle05->SetPosition(FileHandle05, 0);
! m* O h3 F7 m# ~# y; W+ V- n if (EFI_ERROR(Status)) {
6 j, _$ m) ^ c' `$ ~+ S8 w Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; O" u* {. [3 Q7 N! { return EFI_SUCCESS;
. ?9 l6 g/ R+ f: K& M: |$ [8 T }
+ ?6 P: {! I* T / w+ i3 E9 t( v5 G2 p( T" N
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
# K0 v- U& m( \' I/ a) `+ i9 z Print (L"File Buffersize is %x\n\n", BufferSize);
4 w- H. O' n; V0 R5 w& r$ T. M if (EFI_ERROR(Status)) {8 j Q4 A7 I n9 S0 U
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); Y2 i) V' s+ B) O9 n1 R
return EFI_SUCCESS;0 C/ V6 L% o9 k, g4 O
} % U* f% b" v5 V9 q# s! i4 P
T. Z+ S9 ~& P# T, Y Status = FileHandle05->SetPosition(FileHandle05, 1);
m+ p0 o( \& h; V$ }: G+ ^+ i if (EFI_ERROR(Status)) {; z5 P5 K1 O! N0 ~
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" `5 D# F, s, S% _! W; l/ u
return EFI_SUCCESS;
8 n- i+ |! D5 ^( _+ e }! F1 x( Y: W, D) Z m8 X
1 b& \4 u- n H+ }
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);, P$ p6 D. B5 E4 s( K* E" A4 S K
Print (L"File Buffersize is %x\n\n", BufferSize);0 r6 T/ g6 }. R% E7 p% F
if (EFI_ERROR(Status)) {2 q9 J9 x I# `! V' D% s8 u
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
* G# \6 l# ?: e6 V; k$ u: q return EFI_SUCCESS;& S! ? o) L+ ^2 ~- t- h. z
}
8 w q9 I9 I+ p7 {7 o4 V2 k % W$ A! G d' J; }1 S. M( E9 l+ ~
Status = FileHandle05->SetPosition(FileHandle05, 2); ! V. N* {7 X8 s
if (EFI_ERROR(Status)) { i: b+ F6 U& e/ j! _# O% ?9 Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 G1 _# K# a4 u, r |7 v4 L8 ^
return EFI_SUCCESS;
9 L8 Q% ]/ n8 a9 {' {7 o }
3 z0 `0 {% W5 W( a
! a' z' v# J6 l' p8 L/ ?9 M9 l Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
$ m8 i5 S! k% p6 [1 Y Print (L"File Buffersize is %x\n\n", BufferSize);
0 a/ j# ` \% u. B if (EFI_ERROR(Status)) {
4 {3 d8 H- T0 j2 A+ e Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
* a4 q _; `4 ^2 ?/ I3 `2 M# J return EFI_SUCCESS;9 U* W# G* ]5 S o
} ! d6 T m1 p% z. E7 y
, i& a* _/ ^0 {- l+ F//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>/ l4 L8 E6 E* ?
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);1 u% b# {; b6 p e) h+ @; W g
Status = FileHandle05->SetPosition(FileHandle05, 2);
5 C, a& W: N% l( ~ D if (EFI_ERROR(Status)) {
+ H+ |6 B7 o6 ]0 E' F$ g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: O" F9 A! A, T U+ | return EFI_SUCCESS;
' G" l5 k8 m9 P8 E6 V/ f }
; f& B. l/ J) {' {* i8 }
. I2 r( N1 z9 T. ]* n# ?& Y Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
/ F. [1 b% E3 e5 p: m6 h if (EFI_ERROR(Status)) {: s$ g: ?# O3 [3 A% P. N
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
- C# }" K8 V/ i. Z/ T return EFI_SUCCESS;( f" G( o( Q0 ]/ G- f
}
2 Y! T8 N/ O L6 h Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);& B, [; A9 `" z1 o6 \/ S7 _+ y' p
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
Z$ M% \7 k' @7 b) X
6 F4 e$ o! o; l0 \. l Status = FileHandle05->SetPosition(FileHandle05, 10);
$ p+ @* r! |8 c& m if (EFI_ERROR(Status)) {1 u7 \8 A& k0 d* Y, E# `. J6 J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 `0 b1 c7 `( d+ f! q
return EFI_SUCCESS;" Z& N- Z2 t2 ?
}& C7 v( x; ?3 n
8 l& ], K k: w1 H
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
Q; H; u7 F4 b% F8 {& z if (EFI_ERROR(Status)) {
5 Y% h2 {/ |0 l5 J, k" `( o: m Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
3 b2 v- f* v* E+ i% f return EFI_SUCCESS;
! u+ c0 z: U6 a" w }
2 X% [4 C: ]) t
' u" U |4 n4 b# ]! \! }* g Status = FileHandle05->SetPosition(FileHandle05, 12); # X9 J& W. z! p3 K
if (EFI_ERROR(Status)) {
) r6 z b" Y* ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. ~ Y& }2 E( V) R1 r
return EFI_SUCCESS;" B" B- L$ J3 F- l& Z4 Q6 ]
}; F2 q5 P! H$ K
8 ~" m6 [2 `, ]* K" I$ @0 s
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);/ u5 d& [$ B0 b4 ?/ X
if (EFI_ERROR(Status)) {. Q# \! k: |5 N( _& }& K i
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( V$ e8 K: e' N/ A return EFI_SUCCESS;
7 F6 }. b/ ~5 C1 f" o0 b }* w: s, |% M6 F* u. I/ _& u9 K
8 b. @$ c p U ^- h+ r; } Status = FileHandle05->SetPosition(FileHandle05, 14); 3 n3 h/ N; M* I$ v: o
if (EFI_ERROR(Status)) {. z" W) T, N! j* o. B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! c9 T+ o7 C# X) j- ]. L
return EFI_SUCCESS;: l- S' H& g& ]% X
}: {2 o+ E" F; h! p; S9 P
5 {' W4 p; ~# j Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 v. R* j9 N4 U4 @: P if (EFI_ERROR(Status)) {
2 R/ S/ m; g- b- o7 F; z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 p$ `9 k6 a# L return EFI_SUCCESS;
& a1 q M& Q6 h0 f/ ]: G- O }+ z; Y$ W# O' J+ {+ i4 H. k# [' @. _7 r
! I, i9 L5 u( Y, @$ e Status = FileHandle05->SetPosition(FileHandle05, 16); 1 c9 s; ~* t6 K# d$ B) }
if (EFI_ERROR(Status)) {- _7 j F/ k* R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 }0 k& E2 O, a2 Z0 K) ]! w return EFI_SUCCESS;
* `9 l& R* i, ? }
$ E8 j7 x% o d1 k # @- r k" k' Y- F. p
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);/ `% \ y. L1 i2 |. |
if (EFI_ERROR(Status)) {# F; w. f, S9 }5 B) A! Q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- @: z$ K* w6 a1 ?9 @! j, J; Y0 @* w return EFI_SUCCESS;
- g; T9 c# `/ [' k } T8 I# l, ?0 I! D* y2 z6 h
; X8 F( g* {' A, ~- O9 V//---------------------------------------------------------------------------------------------------------------------------------------------------------------8 a) Q1 @/ i3 o3 `4 ?
5 G/ @/ u) y# z- [6 | Status = FileHandle05->SetPosition(FileHandle05, 18); + ^. F9 m5 @; h1 F
if (EFI_ERROR(Status)) {! S; g7 d" Y c$ G7 X2 T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ B2 e7 P. P/ w, j2 \4 ^& Z return EFI_SUCCESS;
+ r _9 R3 K4 |0 U; L }( u% f% _( U$ U; q s7 g. }
; |8 l, W M# v Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);3 h7 q8 O, Q. k8 K+ D! S
if (EFI_ERROR(Status)) {
- a6 `4 k$ O" ?2 m9 I0 d Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 ]" ^6 r6 |9 w3 F+ F% e; x return EFI_SUCCESS;3 j0 u1 j" B8 N: a- _" B; n2 D6 l
}
0 ?1 k. o1 n+ ]; e, s! X 0 h$ W* R; y6 A6 O6 [
FreePool(DestAddr);
. P! t, Z5 G1 B
' }/ W2 e+ G5 {7 {! D Status = FileHandle05->Close(FileHandle05);
# F' U/ w5 L* P if (EFI_ERROR(Status)) {6 s- t0 _1 L; o( p
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
! U8 G( l6 H6 X2 a( t3 Y return EFI_SUCCESS;1 n) s9 A8 a' \$ Q; X( m
}& L' c: _0 W; I4 Y8 V# i
" z+ e( O" y) S$ I# Q5 F# M Print(L"File Name = %s has already been created.\n", FileName);
# C8 O8 I. ?3 ] x: b( Q8 {) S5 ]# E: h
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");) e6 R- N, ~) l4 h% |* @
WaitForKeyOrReset();9 N0 r3 }$ v5 Z
: j( z5 C% o' |8 H7 X2 H return Status;9 O# W& J7 o9 G; {8 X
} |
|