|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
8 h: f1 j" j! S* d s- d6 J0 s * myfileio.c, s: r- b) M" H" r$ p! K: _2 \" A
* Apps7 F) b" I( \, P. e& M: c7 E
*/
4 h5 L- V; m7 q; P& v' L) p" a) g( ]$ x9 k2 X ~
#include "efi.h"
2 N& F7 a) E. H# [+ @& c3 o#include "efilib.h"
2 `1 }/ d$ R8 A
; D, ?/ @- _+ Q( G% m#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE# k) |* |; y3 n, E- _
; A+ x* }7 d) @& I1 }: v
static EFI_STATUS WaitForKeyOrReset(VOID)0 ?# M" D* y0 G6 L: E/ _0 \
{
3 k" J0 n' D* Z3 X! R7 @' c4 _ EFI_STATUS Status;& f% J4 z: K) M" _; t. o
EFI_INPUT_KEY key;; q+ v, k: L/ B
UINTN index;, ] i7 S' x4 M" j$ ^
8 w' ~ I5 Q+ C; ^4 |7 t
for(;;) {
( d1 Y; z( X3 I% g4 x/ s Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
, E$ E& q, G; { if (Status == EFI_NOT_READY)
5 p1 f! ], }! `# m$ Y BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);' K; F, f0 h/ y9 D. Q/ }5 O
else- O8 a" c+ b: I) P
break;2 v% g% N& f' a2 z; Q# M. H
}
0 ^& \) \8 N3 o$ F" L: X! w6 ? if (!EFI_ERROR(Status)) {3 L% u6 D6 K4 k9 A' D+ v
if (key.ScanCode == SCAN_ESC)! y o9 ?* c* t) p9 @
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);2 m+ r+ v, e4 A$ _
}
+ z" _* R+ U8 N! Q5 Y6 G5 z/ {" _ ) X4 f V5 E* X, D( K
return Status;6 V( K3 H5 K& C4 A( M: g6 h
}5 j! y6 B' |+ C5 z/ `/ h! x9 P8 z
1 O2 N6 o( [$ j! J% l; BEFI_STATUS
0 d6 V5 q* r; ^) G# q+ d" m2 W+ EEFIAPI P/ L) N$ F# v3 B' P
MyfileioMain (IN EFI_HANDLE ImageHandle,: M& P# r, B, `6 ^0 \
IN EFI_SYSTEM_TABLE *SystemTable)/ Z3 l5 m& F' p# N# c. ?8 @
{
2 e, [: _. j* }! f: o* D EFI_STATUS Status;
: R( r9 y; x$ v2 u) P* b EFI_HANDLE *DestAddr; : y3 E2 W8 _. W H) |+ B
EFI_LOADED_IMAGE *FileHandle01;
& A0 I: c, c. \% s5 n EFI_DEVICE_PATH *FileHandle02; 0 m" c7 s0 b8 _* Y6 z7 G& e
EFI_FILE_IO_INTERFACE *FileHandle03;
0 }1 n7 `) s( D9 ?& `7 L5 B EFI_FILE *FileHandle04;
+ z4 `2 y# L3 P3 x# y4 b2 s# _ EFI_FILE *FileHandle05;
+ x1 e2 I! ?3 K CHAR16 *FileName;3 { O! ~# B @# U4 }8 G1 M% b
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
+ H1 H7 l5 A* y0 g0 ^- p8 q UINTN BufferSize = 8, BufferSizeKeySpace = 2;: |) M; m! C6 o3 e6 s1 N/ K1 x7 ^
int Bit0, Bit1, Key_Space;
& v8 M+ P& b% q: t, S$ K: i3 k0 e C# ^( J: ]/ \
FileName = L"NewFile.txt"; H5 d! b2 D' b p' y
Space_Key = L" ";4 D4 H+ m* z2 @) L* W+ U8 n: o1 I) ~* O
$ e( N* C7 y Y1 B, |: i; V
BufferA = L"ABCD";
4 W2 a0 v) @& i+ m* i7 s BufferB = L"EFGH";0 x& D3 `- X, U$ N+ }4 z
BufferC = L"IJKL";9 Y6 A) ~( l! P8 Q' Q! _5 e& F
w, |9 Q( Z; Y5 m t* W' O
Bit0 = 0xff;$ B( e; _( w0 W8 [4 s' }
Bit1 = 0xfe;
0 ` l, Y2 g9 I$ g! _5 \" G # t6 f& Z0 [% q
Key_Space = 0x0020;
6 T$ y# i3 c' K/ d& A 9 Q( o# Q6 o: ?
InitializeLib (ImageHandle, SystemTable); " t Q+ c% p/ g
4 J3 W; z1 L) S/ t( t) s DestAddr = AllocatePool (BufferSize); & p! x$ M) w7 m* Y* q: i
$ D: G$ G$ a* c1 d- ~; _, W
Print (L"Value of Bit0 is %x\n", Bit0);
9 u9 e4 N0 y8 ? Print (L"Value of Bit1 is %x\n", Bit1); ' b6 _3 t5 |1 Z& M: ]
, {- {& N& Y. Z3 l: u1 }& o
; e- L! z% V) }2 ~- e9 _
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
9 h2 P: V1 `% Z' ^- _5 L% _( Y+ t if (EFI_ERROR(Status)) {
% M# A. C( s8 v( U Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);( K9 K7 n$ k& h& X
return EFI_LOAD_ERROR;2 H/ p; P- b: l |
}
: u8 O% P g7 Q2 A9 ]9 g0 l7 @& h
0 k4 O1 E, x/ v. F5 h% `5 J, f; H0 D* J Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);* w% r5 p2 M) a" \2 ?0 [
if (EFI_ERROR(Status)) {3 N/ V2 M0 i2 R, X) F
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
, f+ Q$ [4 d8 A+ | return EFI_LOAD_ERROR;( F2 l( }% n6 m! f+ Z
} ) n" j3 K8 E9 O1 K; n. i$ V5 U
0 s2 X" ~6 H q9 i6 `, u
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
% S( F0 [ Q6 d: N* p if (EFI_ERROR(Status)) {% D* R1 O/ [7 H8 T/ d- [
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
H U! [$ m( P$ n3 t, ^% d% j return EFI_LOAD_ERROR;
4 \% S1 |; [5 ~* U4 C' d0 D9 n }' i) w) y& V8 c
. c' ~# K/ s! _4 I: u
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
8 q! G0 k. i- c }$ V( q if (EFI_ERROR(Status)) {
# e7 w, R# z2 N5 k8 `4 d Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);: f2 y3 J% k1 p$ f9 R* Q: {
return EFI_LOAD_ERROR;& B# o b6 C7 P- N L
} $ c; D0 Z, o' |: C
8 a3 C" l6 `6 F) Z, X+ g Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);8 S+ m- a" ^9 ^- {- v0 \5 w3 a
if (EFI_ERROR(Status)) {, B; H/ O5 w- U* y9 X L
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
& h, L; F8 x/ B6 J9 h: E return EFI_LOAD_ERROR;* p: F: V7 k- X$ F: N
}, Z8 Q, ?" L8 L) _5 P' P
9 F. a6 G8 A6 H+ L" n$ Z Status = FileHandle05->SetPosition(FileHandle05, 0);
" l- N/ E5 f1 n/ I/ [ y1 k, l if (EFI_ERROR(Status)) {' S: ]3 s& ]" j; T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ T. f/ `: v9 ~( u return EFI_SUCCESS; j$ F0 v/ V+ W8 c1 O, B
}/ N# m' V9 c* b& v: X" }# [
: B' Y1 X5 `& Z2 x [7 m m
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);2 K; G& q1 d/ ^* s i
Print (L"File Buffersize is %x\n\n", BufferSize);
- x" j& X+ ^. V9 Q* \7 @2 F( R if (EFI_ERROR(Status)) {
, f0 i k! q# h0 A$ d! N Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
g1 N' z& E3 D; m return EFI_SUCCESS;- }: z- O, m& {, {
}
( c% [' p$ O! V5 \0 Z _7 [. E" F2 x9 R3 L
Status = FileHandle05->SetPosition(FileHandle05, 1);
0 ?6 G. e5 Y& ~+ Z3 W9 a" J if (EFI_ERROR(Status)) {8 C, G }( z& A' v' }
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 j1 w% M/ [2 L/ ^
return EFI_SUCCESS;
" n( A7 E9 F( [! a }
f, c; @ R, f( Q% Y % c2 p& K; ~, | R" j; s, y' E
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);& f2 P3 i! P% Q# g. M
Print (L"File Buffersize is %x\n\n", BufferSize);
( P5 O# H1 }; g6 ~ if (EFI_ERROR(Status)) {
! h5 n: J% N& K T: x3 Q K Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# {! B- s# M% v( @) G' | return EFI_SUCCESS;. Y' O! r5 z7 f) K9 d) c5 k' G
} # l: e( o6 I( s. \! N2 q
9 {0 t$ T' A: D8 _2 }1 B5 i( ^ Status = FileHandle05->SetPosition(FileHandle05, 2);
% r4 W% i' B9 o* s% X) { if (EFI_ERROR(Status)) {( }4 Z7 s) T! ?( x2 ]" p+ a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 \$ _9 M- A+ g/ l return EFI_SUCCESS;/ R$ }6 _* r# T' S! G7 L
}
7 D2 p/ E2 q5 H5 A( H( u1 m; m. J - _+ R# }% i7 O+ n3 m' G2 y
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);8 Y8 f! q" a# b/ ~
Print (L"File Buffersize is %x\n\n", BufferSize);
6 R1 g a1 d/ H4 Y6 r if (EFI_ERROR(Status)) {
- V8 L! G- p! j% M Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
- `& e6 w/ J, ~1 m6 G; }/ }. U0 A return EFI_SUCCESS;2 t( K9 K3 }1 V1 v3 Y& c" e' Z
}
6 R2 q$ ^. W5 e7 [! p+ ^
1 C' o$ W: I. r//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
g( f H0 @3 ?. e. Y4 t+ W2 h7 c Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);/ V2 [9 R1 b# Q. K, L
Status = FileHandle05->SetPosition(FileHandle05, 2);
' |( `2 B+ g, B6 {6 i* H) g if (EFI_ERROR(Status)) {
5 x) I1 r$ \. v" M, ?! c0 z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 O9 a! X) @9 l. h/ ?6 A3 K2 g
return EFI_SUCCESS;
! b$ ~6 [9 P* G6 `) E O, N }
7 ^5 a+ w" ^2 I$ `( O; U1 z2 N Y3 o: V' I0 h- l
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
2 q# [! }- ]# h5 V3 f I, i if (EFI_ERROR(Status)) {. L$ X5 }8 \8 I6 R9 k. v
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);4 y) S ^* d- D
return EFI_SUCCESS;, X% L$ c6 W9 N! L% B2 J* {0 J
}
1 p* `5 E3 g% g0 ~# D Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);, A! p7 x# q% V
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<! W6 x* I4 n0 j' z( b; L
( Y9 B& ?, B2 k/ M1 {
Status = FileHandle05->SetPosition(FileHandle05, 10); ( F4 \1 t) x, M7 T: i
if (EFI_ERROR(Status)) {
5 v1 p! W% Q4 D9 a& { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" ]. G0 f4 e% D$ M0 X- x; R- [/ A9 U return EFI_SUCCESS;
7 A! T" V) [+ Z8 Z; b/ K2 Q. M }) k. e) A S7 x
# X+ r8 s ]) j& V( E' h( S; `: K5 U Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( m5 _' A$ v. ^1 n
if (EFI_ERROR(Status)) {
]; L1 ]5 J1 p( W0 D) E; l* J Print (L"Could not write file, FileHandle05 err code is %x\n",Status);+ b7 N) K) j* i- [- N& R, X
return EFI_SUCCESS;! w- B% N3 F0 \; i" ~9 d
}
6 A& {6 G9 p! }2 ^% G5 N D" g \$ A9 X$ @& f
Status = FileHandle05->SetPosition(FileHandle05, 12); + M: a7 n# y* [ F: X p. b
if (EFI_ERROR(Status)) {$ c* m4 F- c& Y' E7 I$ W7 \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# R$ g1 a; R; t6 W* w/ X
return EFI_SUCCESS;9 X* q. w/ F2 E2 H" n
}
3 X. h! f7 _2 I6 N; A$ u " X4 d2 c7 m, T
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);6 a6 O% I$ n" A2 X! b; s) |2 S( m
if (EFI_ERROR(Status)) {
! r% i% v }' |7 x+ F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 I" m" N' G" s/ m5 d return EFI_SUCCESS;
# y) U8 P" C) Y) V5 N4 y; b }
5 g4 U* P: G0 ^% K6 w6 |. c1 Y
Status = FileHandle05->SetPosition(FileHandle05, 14); % j9 {7 Z7 s9 G, p3 W9 ?
if (EFI_ERROR(Status)) {
( I0 m% Y4 X2 V, K/ T" s. T/ h% l Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 G/ v+ @8 Z* h; a: B/ {: {. O
return EFI_SUCCESS;
. Q; ]* K$ y3 P/ _! U* { }
1 y7 E' E+ Q# J , |8 J4 F+ H; a0 e9 K* H2 x7 I
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
D7 r8 h/ k/ r J+ ~4 S if (EFI_ERROR(Status)) {, \/ y/ y8 n; H5 o2 w; q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' D0 L& V% Q2 R. g1 Z1 C6 y8 g3 H- Z return EFI_SUCCESS;% s5 F: B" Y; y l, D
}
1 G# U5 X1 Y% U7 @8 r2 v
) H2 [ A( K% d% p7 w( a Status = FileHandle05->SetPosition(FileHandle05, 16); ; Q! a( f7 {. Q2 B3 |5 k. h
if (EFI_ERROR(Status)) {
" b+ U* q/ l% z g4 J' K: h Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 z1 ~ p6 B" p" M# k8 Q return EFI_SUCCESS;+ |2 I/ s8 L& c
}
# v& a" Y# @3 F b' W; W, \) _ 4 g$ ]0 z& @* i' G+ i; M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
" ~5 t5 y, F# H8 y! `: m if (EFI_ERROR(Status)) {
[$ _0 k+ W0 f I% Q" b& N: a Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% e0 Y( T( d- O0 g% ~! y return EFI_SUCCESS;
$ }4 @0 Q) a6 D6 h) {1 r }
+ t/ { O( h% S8 [) ^1 _- g) d6 f6 Y s" x5 `7 T4 j: \
//---------------------------------------------------------------------------------------------------------------------------------------------------------------; M! j; ^" f" U3 k6 n$ U8 s7 n
, S ]0 Q$ K" \' Y
Status = FileHandle05->SetPosition(FileHandle05, 18);
0 }- P, N# {, E2 J if (EFI_ERROR(Status)) {
) [- C3 R( {0 T+ Y+ w7 i4 N0 z7 ] Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 A3 U) x; `% n( l9 I R; u return EFI_SUCCESS;
D& c ~5 g6 z" m- e6 B }% l5 v- s& f% j' p
. \1 u: l' Z6 j1 p3 Z- J5 F Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
1 Q O1 P. D- y9 X3 Z if (EFI_ERROR(Status)) {
* `1 @; \+ a( R$ A5 r Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- x" R# \6 I' c$ a0 b return EFI_SUCCESS;
5 j( q7 A- j% D; \3 C# D( r } + o, V- a) [ ~7 d/ ~" i. q; O& {
/ M6 J* d5 y2 T9 b6 { FreePool(DestAddr);
* z! q8 T( d& Y' D8 t$ y0 i3 P3 p& F' L7 ~( ~0 n0 k& m# D/ U
Status = FileHandle05->Close(FileHandle05);
: ^5 ]: o8 m; Y/ ~+ ` if (EFI_ERROR(Status)) {
% R0 p5 k. c4 b Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
+ Z' u1 I! h. C% k1 r return EFI_SUCCESS;
/ j5 ]+ F5 M5 |& r! c }
' Y8 W+ \' B# Y
. i7 Q* j# D* Q( `) B1 C Print(L"File Name = %s has already been created.\n", FileName);
9 G3 [* h/ t- U( h& ?5 V+ C# L9 A3 |* r0 d
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n"); G: x: u B# w9 Z
WaitForKeyOrReset();
- V* B( v% T, `# g
+ f) z7 L& A9 U, j& B$ V0 C return Status;1 ^2 w p, T/ e; @/ j; Q8 r& f' E4 u7 u
} |
|