|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
4 M+ `8 P5 J x" P- K8 W * myfileio.c9 V$ Z1 m" X E: P% ~) X
* Apps
$ z4 L/ b% l- O. U */5 m# a, x1 b& g
& T, j6 ]' e/ e# f T#include "efi.h"/ M; M% m* A" e' b' O/ c
#include "efilib.h"
: o. J, s8 O/ Q+ c# e7 ]+ }! T" w6 e% v9 R$ W# I/ m+ J
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
8 ~* L& K! m1 Y+ h9 {6 G+ M5 z% l7 l% t# A! d. }
static EFI_STATUS WaitForKeyOrReset(VOID)+ v" _, S1 ]' ]
{
7 Y- Z% W& L& a EFI_STATUS Status;, s7 q5 u' Z) ]+ }2 a# D
EFI_INPUT_KEY key;
y+ q2 g* @' ]/ \ UINTN index;
k! m4 ~! R3 b% i
0 r9 @; e+ |) k( ^ for(;;) {
! S! e! O/ |4 Q; Z* O1 C! K Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
4 K/ P2 ^8 Z% X' n$ X% v% K( @; H+ F" r if (Status == EFI_NOT_READY)
7 ~' ?% v7 [% Y4 S BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
2 H4 C5 D: q7 M6 @% M# { else [& _& W' U. v6 t; R6 K( i
break;8 x8 U# \$ U% e
}
/ q% }' V3 z% i if (!EFI_ERROR(Status)) { o) @( K* Q) y7 K* m. ~ `
if (key.ScanCode == SCAN_ESC)
8 V' j/ o6 @! e: y RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
& W8 _8 c0 @3 Z) L }2 _5 C E/ M- {
4 }* n3 C8 z% S8 G$ N: R return Status;
0 o) F8 e/ J1 F3 a8 w4 ?* l$ t! i}- s8 O1 ~# X: O* X4 X* e5 R
& e9 C; K& j2 j5 kEFI_STATUS
% ?# D& ]6 |( W$ OEFIAPI4 r) m9 u8 [4 ]
MyfileioMain (IN EFI_HANDLE ImageHandle,5 ~, Y5 h9 q) x3 X, _
IN EFI_SYSTEM_TABLE *SystemTable)
5 M: K9 ?) C1 n/ ]! n5 I4 _+ M8 I{
2 V7 i! A0 E% x* r6 P! S. l EFI_STATUS Status;0 Q8 \7 W. g' t) `+ B- ], S
EFI_HANDLE *DestAddr;
. [5 C! e' o7 s% S- ~ P EFI_LOADED_IMAGE *FileHandle01;
+ |- u3 F$ v$ z0 f3 U. G EFI_DEVICE_PATH *FileHandle02;
: W y! p9 {# f% m$ w EFI_FILE_IO_INTERFACE *FileHandle03;3 h" n2 S& g7 {
EFI_FILE *FileHandle04;
& U: E9 o" V c3 J# e( v' p EFI_FILE *FileHandle05;8 {" M% a2 O* w* r) D& m t
CHAR16 *FileName;
5 ^ e( n. z$ c5 { CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
+ B. ?& K" }% z: ~4 n3 x UINTN BufferSize = 8, BufferSizeKeySpace = 2;
% f! g/ l' w B& z, J int Bit0, Bit1, Key_Space; - j8 I; k V/ r1 j S4 X9 q# f
. e/ k- M$ Q6 P: \+ S FileName = L"NewFile.txt";
1 {2 w8 h9 A4 n* y' a9 T+ i3 J Space_Key = L" ";7 z, o: E) R1 k
8 a7 K \7 s6 ` K# { BufferA = L"ABCD";
* j) P: b1 C. b, x BufferB = L"EFGH";
% ^) Z; y) S9 I6 T BufferC = L"IJKL";/ t- ^3 ^+ l' g0 E r% j; {3 P3 f' _, D
0 K# S; n: [7 w Bit0 = 0xff;+ K2 E* C/ W5 E' B% y+ x9 K
Bit1 = 0xfe;
1 s6 a# D, g4 e" B& s
/ t/ n0 R+ [4 k$ w: `* s/ y* R Key_Space = 0x0020;
. m4 L/ v4 u A$ v 6 I& m/ T( `7 w r8 N8 C* F) [, A
InitializeLib (ImageHandle, SystemTable); - Q; ?6 S# B0 g0 A, R! I+ |
$ Y* C3 q1 ] `) k: l+ u4 q7 f, n- L DestAddr = AllocatePool (BufferSize);
+ S* q4 z% Q9 {, @6 q2 Z" c
9 W3 F! }( O; l3 x) Z Print (L"Value of Bit0 is %x\n", Bit0);
$ _0 S: \$ H. ]! K Print (L"Value of Bit1 is %x\n", Bit1); & {% C, _" v; k
, w4 `( \- z7 ~1 Q
2 ^# ^, \; N* t4 x Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01); {- K' |) }+ r; i
if (EFI_ERROR(Status)) {7 s8 k/ S" ~3 H, |5 S6 @
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);, }4 q' h0 i( u& A/ d( {" w4 \4 W
return EFI_LOAD_ERROR;! x8 M! l" l* y
}4 d3 A4 O; h+ t$ e1 i
^6 s+ z* [6 }" S) ~5 e/ }: _& m Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
8 P+ t( Y' G0 l4 K# q$ W if (EFI_ERROR(Status)) {
- E" y, M2 Z5 t- d/ G; L- M- W7 V Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
& `$ H* U- R g) M- ? o0 X return EFI_LOAD_ERROR;
- V8 z% q5 p. I( M( ]8 T }
3 Y, c) ]; Y9 U
! x S. A/ ^1 n6 J Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);: k% g; B3 l9 t+ J, n, ]' Q6 g" {
if (EFI_ERROR(Status)) {
* H. [( z, B: ~3 a* Y9 A! Q5 @3 z Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
) N8 Y; H. g0 d9 k' S" t return EFI_LOAD_ERROR;: i/ c$ q5 K$ \5 i# f* ]
}1 l! t8 v {7 X* i
* ]0 `3 R, ^, W% S1 `! a" t! X
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
6 w' [6 }6 z! l1 m if (EFI_ERROR(Status)) {* Y t: D1 C8 r
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
! b# y3 t( X! p' r7 h return EFI_LOAD_ERROR;. N. ?- _! F5 N# L
}
8 q. \+ j- O. j" K6 X; Z, \8 W ! `% u7 ~0 I, D4 ~
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
* R7 _& i# q. x: h0 E7 e- F if (EFI_ERROR(Status)) {
6 K) H6 Z7 N: m w! r/ {3 \0 F Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
- o4 s& Z% l6 w$ g, \' {$ b3 C return EFI_LOAD_ERROR;1 |" b+ L7 @/ d9 D1 U% O, @
}5 k- n: }& P& O- U ^
7 D! d: w: w" d; ]
Status = FileHandle05->SetPosition(FileHandle05, 0); ( Z# v: @: j U. {: I- R
if (EFI_ERROR(Status)) {
: x: M5 k, X4 s' N9 J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! @- O# A9 Z- G; |7 ^ return EFI_SUCCESS;5 C: f; @/ K+ g b3 I* V* x: ]
}' D' Q! {$ E) R
; `9 L8 y6 e4 ?' l9 D5 c3 h6 V Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);& _. h0 [- a: `( n2 I
Print (L"File Buffersize is %x\n\n", BufferSize); g, Y3 l0 a3 R! c s6 F0 {. ]
if (EFI_ERROR(Status)) {$ D A, Z" n5 K' M3 a+ t
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
G! \" }, V$ T. s4 B, o return EFI_SUCCESS;
" K0 X4 K1 `( S5 b- x5 ] }
$ k4 b% K6 e0 f! w0 k! Q) h9 `$ M
- v q7 Y+ b2 q. G Status = FileHandle05->SetPosition(FileHandle05, 1); , `" v+ u/ v: j0 f
if (EFI_ERROR(Status)) {" _" [" }' H. ^- W& H( c0 @* F7 O0 _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 l, v- }0 Q* j return EFI_SUCCESS;- S+ h1 V3 T: o0 g* w
}
2 J$ h5 N) S3 Q . _2 J }7 H5 P5 K# p% u
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
% I/ ~% X& H( r/ f Print (L"File Buffersize is %x\n\n", BufferSize);
) }8 U/ ^% e# H, s$ j2 e) @ if (EFI_ERROR(Status)) {6 u* H9 k0 K* X" z
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' F. |3 K j5 x3 O% f
return EFI_SUCCESS;
2 _* a: N6 A/ t/ X }
4 S" f5 P1 s1 D' ? + P9 d! Z6 L7 \
Status = FileHandle05->SetPosition(FileHandle05, 2); : ~# r- b- q+ d& }3 u) S
if (EFI_ERROR(Status)) {
+ y- w. g$ F. C# A0 s8 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( V5 |! U0 J( S r6 I7 U$ V return EFI_SUCCESS;
( y. `& L5 d2 J; D$ y- ~4 R }
1 T+ `# U2 A2 k% _4 q4 K! g W* M. M4 S8 u2 b; P T
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);# M3 v& o6 r7 Y# O
Print (L"File Buffersize is %x\n\n", BufferSize);
4 s( w0 p! Q7 `" r; T if (EFI_ERROR(Status)) {/ Z# m' o4 K9 R% n5 E& U$ i; Q! A
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 T3 T+ N. h. ]' }2 T, U return EFI_SUCCESS;
: T9 k0 q' k3 s0 L0 K } % A8 O4 r1 S5 ~* X
4 I- ?9 _8 S& g7 Z" e6 E' |' R. | [
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
! R5 O2 D. M: m$ X$ a7 h Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
/ r* |* }( c; J- G7 Q$ o3 L* s Status = FileHandle05->SetPosition(FileHandle05, 2); ; L2 |5 g, f7 _5 N; j" |8 _
if (EFI_ERROR(Status)) {" d' g9 c7 U1 m5 M2 F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" f3 X; [+ k9 ~$ _# H return EFI_SUCCESS;
0 e0 | p0 [* D X/ G' R1 j }+ C2 |0 B! ^, y: W# _- e
6 P* M- c4 z8 p/ [; p4 R! b; s Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
1 f2 T. E& j! ~ if (EFI_ERROR(Status)) {6 U& ^' y: u, ? `
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);- L6 D8 d% i& }& W/ P4 v
return EFI_SUCCESS;
/ {7 o( u) B: Y# J6 J: R }
6 ^* s' B. U' V* }3 a. { Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB); M1 N" L) Y8 [7 P9 Y- S$ V# W- ^. F
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
- y( a- D4 W+ x! Z. N7 v
) {* a7 O2 S% s, W Status = FileHandle05->SetPosition(FileHandle05, 10); % P* Q+ x# M( }: z
if (EFI_ERROR(Status)) {
* E( q0 E0 A: K& S) }. d0 { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ Q! _2 ` q& {8 X6 Z3 U$ ^ return EFI_SUCCESS;6 `5 i! N0 q* u5 w2 i
}4 Q0 ]) h# N& q) b2 ~! E0 }8 b
# H1 V+ I. X- z9 j
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
7 U" o6 w! X) y* k if (EFI_ERROR(Status)) {
! I, p8 N; R, D; } Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! \+ T9 l- [0 s) s return EFI_SUCCESS;* t) b9 g1 i$ C1 k2 W, a* M9 J
}
7 s: ]) Z( T. e+ J9 _1 S! }! ?, B% M: p ^- L8 o5 J7 A
Status = FileHandle05->SetPosition(FileHandle05, 12); ! k9 B! k9 e! d% t" U5 Y) e- V
if (EFI_ERROR(Status)) {0 M4 L- L! u& c+ H; N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& x! |2 U" u; u1 a, P1 K
return EFI_SUCCESS;
0 b9 v. F1 F6 a X. Y) u: H$ T/ A }9 m9 b! y9 V2 y" x
# ^! }" _, Z2 Q* h2 P- v' r Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 u( U. P" X/ { if (EFI_ERROR(Status)) {
7 E3 ~) P" u8 n" t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. F; h0 g7 ]6 H return EFI_SUCCESS;
: a4 u; W8 q4 j }
4 ?0 \+ _* f& \1 s8 |) E( U9 o
: o5 L6 u: a8 T' t Status = FileHandle05->SetPosition(FileHandle05, 14);
_1 N+ z) _0 x0 ?8 x3 a/ }" O if (EFI_ERROR(Status)) {
9 k1 X3 a" Q: {8 O Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, `; j h, ?4 F/ [+ Y2 ?- E* q
return EFI_SUCCESS;
/ F, r+ S, m$ {" h }
# B% R7 l( h0 g7 p( ], I7 i8 | 1 b6 q X3 d1 ?$ G" F
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% |7 y) }0 @! w8 w ]$ ? if (EFI_ERROR(Status)) {* I& i7 R- |, B( J0 m( f7 p
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& W0 b5 u8 a: d" h, ~8 h( Z7 G
return EFI_SUCCESS;( h% V7 L: D y _
}
. J: t* t, K1 m7 d* }- c0 d5 E8 T
Status = FileHandle05->SetPosition(FileHandle05, 16); & a5 c' r1 j! r9 U0 y; e9 V* Y
if (EFI_ERROR(Status)) {# Y J9 @ P( |* _9 {% o, w
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 f1 _0 w3 I3 @, Z/ t2 W return EFI_SUCCESS;
6 l. R1 ~7 A# c8 j }2 D; n9 d6 ^0 q1 S; |
8 n" X8 j5 b" j& { Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
) K( U/ x9 r" {$ r# ] if (EFI_ERROR(Status)) {+ s N6 y$ v- X! k
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 R# b% X) M' R1 M, X return EFI_SUCCESS;
+ b4 k+ x, S6 Z0 a/ R; I S }
7 a0 E# |2 L( B3 ~4 o5 a. A" [
0 N% P3 m( I2 u1 [) g% M//---------------------------------------------------------------------------------------------------------------------------------------------------------------
5 n' w P7 b y* Q1 I" B3 J3 A3 u. a1 H
Status = FileHandle05->SetPosition(FileHandle05, 18);
) r" r% ]9 k) ?. |6 } if (EFI_ERROR(Status)) {) l4 `/ I5 T0 B1 }) i
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 K5 j K. t2 Y6 q
return EFI_SUCCESS;. o9 u8 |" I$ ^- x! _+ F) s# `! m
}! c1 O2 m B, M+ t/ @
0 J8 S, t% _" [# W8 v- I
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
4 x$ U1 O8 A3 C if (EFI_ERROR(Status)) {
( L' S1 H T9 X5 v( M) b Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
# v( ?1 g5 Z+ a/ z: | return EFI_SUCCESS;
]. V) c9 P4 |: {" G }
5 ~% B- p, o2 B4 K S% B8 @/ i
7 w/ U+ X8 b% o# ]9 z: C FreePool(DestAddr); 1 v$ @+ w2 L4 `9 c5 X4 J9 G" g7 z
A, n+ B6 k7 l& t: O- O2 h
Status = FileHandle05->Close(FileHandle05);+ [" P" S2 \/ P; a' C3 o$ o" X! s+ b4 @4 f
if (EFI_ERROR(Status)) {, ^/ {( w0 L, q; d8 r* M* H6 `
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);7 \5 `/ M6 X) N4 ~( p
return EFI_SUCCESS;) o$ ~: R3 n# H+ S& m
}1 k8 i0 ~# k, @. A4 i$ ?/ o/ Z
8 X/ N( }2 l) X$ q$ y Print(L"File Name = %s has already been created.\n", FileName);
4 D7 W( Y0 b# o/ J; v
0 g' J8 @/ L7 n Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
6 T7 W2 `% q- }# X) T+ o WaitForKeyOrReset();
+ w3 \' x% k% s! F( l U
9 h1 T( V$ `8 S% G& \ return Status;2 C y( t5 w' `) r( N, J5 ~
} |
|