|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*, B8 Y6 T8 X8 b, f, n8 c, _) p
* myfileio.c
2 R- ^* _5 V7 E * Apps
' M7 r2 p# F8 u" q: V/ F+ m */
* m2 w# R/ T; ~2 W" D2 f* v$ |# i4 M7 r3 c
#include "efi.h"
) {& L. F1 {1 V' r, T& q#include "efilib.h"
- s# k3 ]+ J7 a _
' W. ` t7 e- F+ c! K#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE4 d& S N; t% o7 @2 f" y2 j& k
2 l7 h. V( ~% ]; z1 Z# {static EFI_STATUS WaitForKeyOrReset(VOID) V$ d# _9 f9 F N" ~/ x- D7 t- {
{8 S( o& [* h# Z( M. {& M; M
EFI_STATUS Status;" ], e9 z; I! x0 {2 i
EFI_INPUT_KEY key;8 u2 D2 f: H& A8 @6 b
UINTN index;
2 [' q; Q& T! b$ K& n2 ~' ~) V: u' a9 f; w
5 N( l: J# Q) }$ l) f# | for(;;) {; | ~" H) h o
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
3 O, g/ v4 O1 Q# B. z if (Status == EFI_NOT_READY)7 f6 h' Z8 ?! C
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);+ n3 p I- b/ [+ W0 F4 O
else
" _3 I1 T } N+ k6 x; @ break;8 _* y" Z8 P" `% t
}9 I+ P$ W: m& o- j) a+ C2 a3 h
if (!EFI_ERROR(Status)) {
2 w0 u2 t7 l6 N: h! ]! D if (key.ScanCode == SCAN_ESC)/ v+ @' o- t" X' W8 U' l. M, L
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);9 e* }$ H' }% I$ q- Q" r% x+ k) q% C
}
# V( x& o* h0 H- D1 N ( E8 _( v' P# P8 Q( G3 J, S
return Status;
2 n( }/ e1 C u0 w) A}
; N3 I5 ]8 i4 c' \% y8 @1 f T& D' f. q. _/ L5 n- K5 o+ l* E
EFI_STATUS
+ O/ [% t* P% i0 E; F/ ?; f' SEFIAPI
- H2 [0 M0 D0 L: @MyfileioMain (IN EFI_HANDLE ImageHandle,
D, e# [& S: d# p. A IN EFI_SYSTEM_TABLE *SystemTable)) U: ]% y/ W2 W
{
+ K$ n$ ^7 ^( c. t# N4 F8 [ EFI_STATUS Status;3 i* R+ S( @$ B, Y2 c: F6 F
EFI_HANDLE *DestAddr; % X$ F: h1 Y+ ^/ G1 ]
EFI_LOADED_IMAGE *FileHandle01;( G9 I$ e" `6 f+ l4 _6 e
EFI_DEVICE_PATH *FileHandle02;
. x* W, X1 M8 G* ^! A6 U& d EFI_FILE_IO_INTERFACE *FileHandle03;
. x7 v6 H8 o6 }7 o- ^ EFI_FILE *FileHandle04;
& B& H2 o4 P( f EFI_FILE *FileHandle05;9 e3 \. d( P0 e7 V I G
CHAR16 *FileName;
$ f9 f& z8 W1 ` A S CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;6 g4 w( E! g* b2 D5 R4 ^& Z
UINTN BufferSize = 8, BufferSizeKeySpace = 2;1 C* N. W- M0 U" I, s. g( q
int Bit0, Bit1, Key_Space;
. O5 s& i- X3 U) E2 ^4 b9 P+ A9 p- U+ v4 f
FileName = L"NewFile.txt";
! r+ m0 z9 R$ @' Z Space_Key = L" ";" M# A+ c- Y5 X
- E; `9 G' e J4 [ BufferA = L"ABCD";% Q# ^: b9 H7 i% p
BufferB = L"EFGH";0 ^ {+ a8 c% Z
BufferC = L"IJKL";4 V$ ?+ b; _* I8 y V3 a6 }
' m4 e. x* y6 [6 L( E
Bit0 = 0xff;
% p) d" g8 w* ~' ~0 a Bit1 = 0xfe;
. n! m, w4 L1 l8 {; @ 6 ~0 k) P4 a5 @! S2 j9 n9 B
Key_Space = 0x0020;6 v+ M2 W; k; V& ]1 o$ j+ n6 l
6 d; u/ ?" Q) b, R+ v
InitializeLib (ImageHandle, SystemTable); 4 [9 `# R f% S- c) H6 p
( S, c8 K: A! s: @( {# T
DestAddr = AllocatePool (BufferSize);
4 d; ?, Q2 c' ^2 r6 {" ~7 x; n0 e% x
Print (L"Value of Bit0 is %x\n", Bit0);
~( w' q, z9 U/ j. O Print (L"Value of Bit1 is %x\n", Bit1);
/ L9 b! z8 }6 z" [. z; y O
1 N. a" Q3 y S: C
" ~, G% y' Y2 C Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
+ E* `5 d# n: z& [% p0 Z' w if (EFI_ERROR(Status)) {4 u) w% }+ t. J& l
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
2 p0 _# ^+ i# C/ O) n7 V return EFI_LOAD_ERROR;' [+ }% s/ v( F. Y8 J, l: ~
}
# a6 H9 Q; V' M: ^/ O4 ^: c
3 b7 j' @0 q) { Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
# w% @2 E7 j1 f% g if (EFI_ERROR(Status)) {
, F, ^4 {6 \7 n' z( A Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
# f: \- E) p9 d' @0 }1 G* ^ return EFI_LOAD_ERROR;2 }2 n, F8 a1 _
}
5 J1 K" P* r* o8 F" z ) U9 |; D7 b5 a9 v- {
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
7 [% E: W/ c2 I9 I2 o* [ if (EFI_ERROR(Status)) {9 M' a; m* k3 ?0 ]( Q
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
% [% E" l9 m" r) ^+ h7 e0 o return EFI_LOAD_ERROR;
7 X% x# G; {& T! w0 c2 N6 }% m1 w2 J ` }& h. H+ ?$ a% b, q7 u( g6 a
1 _: n* o4 C9 c- m! V& n% x Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
3 y' Q4 c+ v! Z- ], p, ^+ o if (EFI_ERROR(Status)) {
) _4 W: B+ F- o0 l% p) d Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
" t& ^& r8 Z* y9 g5 L return EFI_LOAD_ERROR;0 X9 J( Z6 ]# G) t6 T' I( l
}
$ I! q" |, t/ a# K ~2 z0 O- b( D
! @+ a. j" d. q, p& y Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
* I8 x( R; G5 S0 U' N' E7 G# u0 V if (EFI_ERROR(Status)) { A( y: I- x1 D% i
Print (L"Could not open file, FileHandle05 err code is %x\n",Status); L% `; B6 }+ W& ^
return EFI_LOAD_ERROR;2 j) s7 F B$ ^
}
+ H. U+ c2 E( |4 S% J 8 O: ~7 O3 f& ]( e! W
Status = FileHandle05->SetPosition(FileHandle05, 0);
% ~3 X" [ b9 |: I0 N if (EFI_ERROR(Status)) {
: y- N9 U* d7 ^4 n# d Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ l6 T( \. _, Z' h$ M1 S return EFI_SUCCESS;! h9 ?6 D- t$ T9 t" G- [1 k
}0 }7 d* l2 Y9 p! s5 _+ `4 {
. j4 J4 _4 Q0 c
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
% O$ P: q3 |- E Print (L"File Buffersize is %x\n\n", BufferSize);
( ~8 l, M; W# K! K ~! Q if (EFI_ERROR(Status)) {
$ v" e9 S3 m( E- ` U5 t8 x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
3 Z: P) Y: |- ~ return EFI_SUCCESS;$ Q& N* `6 t5 ^: W+ ~4 q. j
}
1 C3 q; o* q/ C" H j& N
$ [ K6 u5 |- P9 R* Q( Y7 T Status = FileHandle05->SetPosition(FileHandle05, 1); - S8 c9 }0 P& I7 F! Q' c* O" ?# M
if (EFI_ERROR(Status)) { w' \ ?% L5 y9 i) o
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# T$ R; p8 n7 Y5 j1 P2 I return EFI_SUCCESS;
& c, V Y2 m, ~3 ?/ f$ ^$ q5 c6 ^ }
( G6 E# w" f* D5 a: ^& V2 ? . g( D. V8 v0 ]. H& i# M0 C6 U
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
' I+ e# k$ e- a Print (L"File Buffersize is %x\n\n", BufferSize);
! r/ A, M9 d% ^' L, R H O if (EFI_ERROR(Status)) {
6 `& r; `$ e+ I6 e5 \ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);; l- ?* R: N' H, Y
return EFI_SUCCESS;% z r1 l H/ R6 i3 L- f( F
}
/ U M% G: i4 b 3 z' z& L6 [7 G7 g$ H
Status = FileHandle05->SetPosition(FileHandle05, 2); ^2 t( X. z$ t
if (EFI_ERROR(Status)) {
2 T& m5 f% r8 ] Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( d0 d* @6 Q, q# F8 b return EFI_SUCCESS;: U1 s5 q9 W& Z# [2 u/ q O
}+ w, |& K4 G1 N3 U5 a/ V; I$ x
) a c; }# } f8 n/ U6 y5 y
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
, N3 j% G: U8 E( r7 u# |+ b$ D Print (L"File Buffersize is %x\n\n", BufferSize);* x3 K# o' h% m6 p# H2 ?6 h
if (EFI_ERROR(Status)) {5 Z: |: y2 y8 A
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 P7 N0 Q D2 m4 ^4 p return EFI_SUCCESS;8 |/ i2 o. d/ _6 U4 u
} 9 [9 l+ e u# |# T( U" ?
+ x$ U* ]9 k' i" b9 B& d- i' B) l//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
( i9 z9 s1 ]+ d+ V. y- x Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);' L8 m' F6 t* X! A
Status = FileHandle05->SetPosition(FileHandle05, 2); : u" E2 w6 U6 S! Z% T0 s2 {
if (EFI_ERROR(Status)) {% h8 D( l0 \# K4 P7 e; K' F0 j
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 Y5 U2 n" P% e! I! D$ W return EFI_SUCCESS;
. a& O6 U; `$ T" i }
0 }, R( s' m+ ?, d2 R* |$ @: ^! T! ~6 D
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);1 w; t; O C4 Q* l
if (EFI_ERROR(Status)) {. R7 p' j1 |. q& ^) ~* m2 ^( a
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
' w; c+ s! G3 } return EFI_SUCCESS;
; ~+ c7 A" V4 h" T } 1 ^6 u3 D9 G" Y v+ }1 E- w
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
( x+ O+ k( o! l/ v' N//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
6 U$ v! A, I4 s) P) H
/ N, u% s3 m" y, e, c6 D3 [, q' v Status = FileHandle05->SetPosition(FileHandle05, 10);
1 V& {7 K) Z- t% j/ |9 E if (EFI_ERROR(Status)) {; b& S! b5 \1 e) J6 R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 f- M7 F. p4 P1 H return EFI_SUCCESS;1 _' L& L& _& P( q$ Q& _2 a
}
! T N1 ?; f K4 F) ?+ V$ v 4 f. ^% ]4 e' N, ~& l- ]$ n
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& S* k# W9 f) @* I; V if (EFI_ERROR(Status)) {
& X' ]9 \4 Y: P' d- X5 W) t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 l9 f( @% s) m& C return EFI_SUCCESS;
+ r# M0 y. y; g! t4 q1 C }, M8 b _" ^$ T/ r
. x$ G+ H! D/ W* a Status = FileHandle05->SetPosition(FileHandle05, 12); $ N: m/ X/ K, W
if (EFI_ERROR(Status)) {
/ {, a/ s) n% |3 D4 x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' l# Y. |" U' a, m( e
return EFI_SUCCESS;" ]7 F0 {; B: i
}
$ K7 k2 t5 C/ `4 b4 o
' y8 g0 |( R3 G# ?6 `0 j Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: A1 d% M h, a' T+ L' n" k7 C/ o
if (EFI_ERROR(Status)) {
8 H# _7 Q( D. U Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( _9 ?5 Z' t3 {7 d8 K: J- t( o return EFI_SUCCESS;
1 _. A; O* d, K# B5 p0 O }2 t0 g$ n# s; z& m1 c
' r+ E6 `) U E7 }/ c Status = FileHandle05->SetPosition(FileHandle05, 14); 6 @% X& O1 B# r1 `& h
if (EFI_ERROR(Status)) {
k1 x$ ]# ~+ F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 y1 F; r" U) \- [. V7 } w: i( u
return EFI_SUCCESS;+ V. ^8 |9 D# T( X3 G6 M0 Q
}$ f- v4 g3 _% ^6 H% x4 f0 r* U1 L4 j
) M! I; {! F3 {" a* V
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 }9 W- |9 H! j7 t# I. L* p if (EFI_ERROR(Status)) {9 n; U; \0 b! _
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ Q' T; H$ Y( B1 u1 S
return EFI_SUCCESS;, J4 Q6 z% R; r0 J' m2 i& D
}6 b/ l" V1 m7 B6 i$ F* r7 D
; q& o- L& c) g9 n; v* k1 J2 S Status = FileHandle05->SetPosition(FileHandle05, 16);
7 x/ a( E V8 \, h if (EFI_ERROR(Status)) {& T) F8 R; u, ?- O9 C: |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" ], o6 ^+ j% ^+ J5 N return EFI_SUCCESS;! f2 ^6 x, B9 e6 T: K! N: |9 p
}
: _/ k' n' c0 _; m. \ % M" o( K) T+ R" R8 B4 K
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
3 K; ^( x+ p, p C, g2 \ T if (EFI_ERROR(Status)) {
& z- o- S; T# f! t) R1 E Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ y. d6 ~1 b" Z1 T9 Y" o, b return EFI_SUCCESS;* L9 G* x( M% M0 |- x
} 4 Q7 J- A; t! K% M Y/ S4 Z
2 y" j9 |3 [/ P- e/ @//---------------------------------------------------------------------------------------------------------------------------------------------------------------
5 B3 A5 h# C+ {. s" ^/ X
+ E7 C5 Q, ]3 H% I Status = FileHandle05->SetPosition(FileHandle05, 18); 2 A! k) I% c" D0 e% J
if (EFI_ERROR(Status)) {
& H5 i! @* A, N0 ?" r9 g4 b! C1 X Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: i- n7 w( d V/ O& x9 f return EFI_SUCCESS;
0 l" Y5 f2 [8 [/ h& Q- W; [0 \) l }. j* j; ^$ Q2 [6 t6 v! u. ^
2 }2 U' V( Q7 @! v; f6 E# \ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);4 M: s( [1 Q2 N" C4 u! E) [- ~
if (EFI_ERROR(Status)) {# M7 F: J0 A; f: B4 e! Z9 q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. d% N/ s7 e0 k7 z! e) I
return EFI_SUCCESS;
: c$ H! w% C! Z+ q/ A }
5 x, N2 o) A- G) [- V" b" G 8 }5 A. T$ X* U7 C* L2 X% ?$ Z
FreePool(DestAddr); . S( t' j+ Z7 ]/ I
2 G% O A- a' L4 Z2 q4 | Status = FileHandle05->Close(FileHandle05);7 v5 S8 K5 E" |( s3 @7 K* Q
if (EFI_ERROR(Status)) {% Q. {5 R& \3 {' b) I
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);( N* y; ^" g1 D; p
return EFI_SUCCESS;
4 C% Z8 a2 w3 S }
' k% s& [' D+ @# l- x _) m
" c/ M; P3 `; {6 q0 f! r Print(L"File Name = %s has already been created.\n", FileName);- Y: Y5 K0 D0 U
- T# Y7 |* N- i3 f
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");7 Z1 M) u2 K% { L2 h" G7 c$ ~
WaitForKeyOrReset();
5 a- g/ K8 [ y/ ?; c5 _
* _9 o" h* P+ J/ P; A return Status;5 u8 s8 I" Q1 [4 Z
} |
|