|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*! I, P" |+ p6 t4 _( o4 y' |
* myfileio.c
' P/ V- m8 U, u$ b$ E * Apps8 r# Y; {7 o0 y5 s3 i8 r/ N& Y9 y
*/
& S9 z& b( U* ?# w( C, t1 @, u! A
f+ [/ B) d8 y; o: ~#include "efi.h"
, {3 @' O- a- R" J4 Q( e#include "efilib.h"* ?9 X( H( I Y! \% ?6 k L
: c( }- _: E0 C' _2 T* A
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
% r3 P) I) q3 J* M/ \% q9 o' P4 [* c- R! a! d
static EFI_STATUS WaitForKeyOrReset(VOID)( h- I6 N' _/ L) Q) u+ Q7 @0 T
{4 O- s( S5 U+ b5 i" h- {
EFI_STATUS Status;
& c4 `. t* l r, r9 Q/ A9 W/ J8 d8 t EFI_INPUT_KEY key;4 H9 E1 ~8 C5 u
UINTN index;/ ^3 C6 F" Z" D" a9 M4 u" s
5 c# C; B1 s4 O for(;;) {8 t- k" {+ @: H3 r7 |
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
' N& e: a. h0 a if (Status == EFI_NOT_READY)1 z8 m1 X J7 M& E( Q0 _: k
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);2 v5 z" X. z# v# ~; R: S/ a
else
2 B& t, D- F) I) k( T break;% {" T/ [* H: ?, f- h
}
' W* ?6 ^% @7 `9 c$ V4 h if (!EFI_ERROR(Status)) {6 T- Z1 [. c3 l: q& s- U( x
if (key.ScanCode == SCAN_ESC)) O/ e' `4 V. [$ L& v, f. M* c
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);9 x9 N5 l4 p) e& x" {+ k! @
}0 e) U% C, j, V( T/ _
) a% R7 [+ m; D1 \, \
return Status;8 P( l. m$ ~) A' @! f
}
5 y. D% ?7 l2 T; r2 K
) K" t- b; y! E9 e9 T' b, xEFI_STATUS+ _. @9 V1 L L5 b
EFIAPI5 K" V! E' E* B" \- h% ?8 F5 e& Z
MyfileioMain (IN EFI_HANDLE ImageHandle,$ \( [, A V! |
IN EFI_SYSTEM_TABLE *SystemTable)
& A* E" q Q& D- g8 y% [1 \% E{
Z* ?1 T/ k: M EFI_STATUS Status;
& E2 |" J% R* G4 J EFI_HANDLE *DestAddr;
, ]9 ?0 P6 Q5 z. O+ p, R EFI_LOADED_IMAGE *FileHandle01;
% f' q: V8 q) r3 T" v EFI_DEVICE_PATH *FileHandle02;
9 c; {, J" s1 \ Z5 t% [/ Q EFI_FILE_IO_INTERFACE *FileHandle03;
1 d+ Q- \/ X9 o& ^! b EFI_FILE *FileHandle04;0 w) w; h& {: P: `" f& ^
EFI_FILE *FileHandle05;$ h2 V" N# N( N* f
CHAR16 *FileName;
7 w8 |6 E, b6 Q' h. a6 ^0 z4 ` CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
! {9 A9 g# N, b9 [6 U# u UINTN BufferSize = 8, BufferSizeKeySpace = 2;( p6 q; e: S' ~4 N# L
int Bit0, Bit1, Key_Space; $ i0 F4 G- U2 x
( G3 C( O( H0 ?' ^+ O9 [* O FileName = L"NewFile.txt";: Z2 n3 b3 z% ~8 V N& C* u: F( a7 H& O
Space_Key = L" ";
5 e" |" h6 e/ r2 _6 w, f. Z $ U0 G8 [1 U3 `
BufferA = L"ABCD";6 u0 |3 V* D, V- i- t4 d* j
BufferB = L"EFGH";
& T+ Y. x- W& ^" F! t5 j8 ~ BufferC = L"IJKL";* E- c2 d0 s( s, r( P6 I A
* G$ n4 w" R5 G# N# c5 N- `
Bit0 = 0xff;( @4 b5 ?0 V& F: h1 G
Bit1 = 0xfe;3 |9 Y+ \# e7 e- M. l
, c4 h6 I( I! J2 _# q
Key_Space = 0x0020;
- Z" Z7 O) ]2 m9 Z X 5 O( C$ T2 I9 d* E
InitializeLib (ImageHandle, SystemTable);
# g' ^: u. I# Z. F
; A7 q1 M" }0 r$ o$ V8 q( r, K DestAddr = AllocatePool (BufferSize);
5 n3 k5 p6 g# i6 i9 C2 P. Y G" W* @" ]- _" {
Print (L"Value of Bit0 is %x\n", Bit0);
5 P" G& u6 i& Z1 X Print (L"Value of Bit1 is %x\n", Bit1); + F+ r' `) @$ e z0 r1 w4 i
8 a/ V3 L5 U/ D/ B, g7 J4 }
; w% B9 b) H; }+ @5 R1 a! [ Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
0 T. @' Z0 _- m. |+ Y/ _% L if (EFI_ERROR(Status)) {- d% ~3 f! c# S# Y) \2 `' {
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
/ o) f O, r$ J2 l& ~# P return EFI_LOAD_ERROR;$ a* g5 _7 v3 K( V8 P0 \6 z
}
' ^) @( ~0 P# E, I; G6 D5 L9 d! q& S4 _% G2 g9 ^
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
, A. J3 H. y5 ?7 U6 t- B" @5 @ if (EFI_ERROR(Status)) {. [' E+ E. ^* {( Y0 f0 j9 g$ a; T
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
+ f$ z7 `7 {1 N return EFI_LOAD_ERROR;+ L: R) O, M% ^. q4 q! s
}
- Z$ X# p* _$ `" A7 I( F + P% h8 i9 M# \1 {* I7 v. W
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);7 j. p% w( ]7 N2 O! p; S
if (EFI_ERROR(Status)) {
# K6 K- Y; Z: T1 o: E Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
' S! `* B3 C4 C# H q return EFI_LOAD_ERROR;' n8 M. k# T q, P3 n- U4 }
}
) H. \! e% \3 @5 c# @6 J9 f6 x( o% z
' S- l; Q0 m# }0 W1 X6 A! @ Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);4 b2 \/ A. V8 a
if (EFI_ERROR(Status)) {
6 N7 ?' J; [+ M E Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
2 A5 U9 Z$ s7 V! O1 T& { return EFI_LOAD_ERROR;
- R1 i. H5 L1 Q3 ?5 V } 2 N5 ^8 o& r5 \! Z6 {
5 z/ M$ H: R& C6 o7 t
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);+ l+ C6 x- D# K+ c$ f/ U
if (EFI_ERROR(Status)) {3 W3 E5 u' S0 s4 N4 f, k
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
+ M( f8 A; {9 V( D; F* |7 E! J! A5 M return EFI_LOAD_ERROR;7 ^+ [& o9 w7 T; B" O
}
9 h& S$ I' Z: Z& r 4 _6 ?0 F3 @+ `- N
Status = FileHandle05->SetPosition(FileHandle05, 0); ' T$ e7 t: n b; c4 U1 g
if (EFI_ERROR(Status)) {
7 T, O, _- w; t! P( ?8 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
% Z# A. C$ C+ q* _# y( j$ I return EFI_SUCCESS;, f& ^6 y/ ?1 t2 i9 z
}
: P S7 c3 ^1 U& G1 n/ U 7 `/ w) P0 I8 M3 S
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
& M- U% L% T' `. i- v( R+ A Print (L"File Buffersize is %x\n\n", BufferSize);
; q7 ~$ j9 U. ~- G9 a/ ?! s+ H1 ? if (EFI_ERROR(Status)) {
c4 u' \) K+ R8 \/ Z Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ Y6 f9 p2 \' E- L
return EFI_SUCCESS;
5 d; p6 f1 O# j }
% f; x0 Q M& q
+ _& D. C/ @+ y, \1 Z4 d% j. C Status = FileHandle05->SetPosition(FileHandle05, 1); ; C1 ~! s- _; |5 H" l( d
if (EFI_ERROR(Status)) {
1 ~2 L0 K/ Q6 K" y) e( S+ C4 o Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ n3 G5 {% J! R
return EFI_SUCCESS;
) n/ t& Q0 F: ]- ?# F! j6 e, g }
! b) j* l7 \" v' ~ - S' m) |. [# X1 {9 g
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);8 c- m5 u! R ^) d; u! [1 W1 V
Print (L"File Buffersize is %x\n\n", BufferSize);
, @" B- N, Y8 z5 l* A7 s) G if (EFI_ERROR(Status)) {
1 o9 V! ?' a8 d! W2 _5 o9 T Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
, O! o2 X0 ]/ b/ P6 y/ s B1 A% q return EFI_SUCCESS;
$ t* d: B3 G+ w9 B$ ^ } : z4 t7 s1 ^% k
; g1 S- ~4 n( K" ^; V
Status = FileHandle05->SetPosition(FileHandle05, 2);
/ m) `- G' p4 a! w' C3 A if (EFI_ERROR(Status)) {$ ?2 [7 {$ _, h2 t% G0 P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" z0 T3 N5 Z. w8 P0 P return EFI_SUCCESS;* Q% g/ n' ?. F3 B' u" i1 l# x: ~9 J, H
}1 M- y8 Y, p- s/ S" e$ E+ U( f6 I5 [0 W
) Q- W! h5 I7 K7 y/ {8 ]. D Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
+ l0 N0 h# Q& ?9 p E1 `" O4 t Print (L"File Buffersize is %x\n\n", BufferSize);
- O) k+ g8 \+ G+ Q2 c+ U. k if (EFI_ERROR(Status)) {" z% ]2 {5 g8 @
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
. S9 I6 F6 D' S: Z3 C return EFI_SUCCESS;6 ~# `% `8 R3 T( D$ C$ N
}
/ c" K+ o' l% c- P6 Y6 `6 a) }3 H 2 Q2 g4 G4 a; `9 e$ @) |% M
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>, z. c0 N6 {: S6 y7 X
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
+ g3 z9 v, C- c( V/ t' ` Status = FileHandle05->SetPosition(FileHandle05, 2);
/ C# a+ Z0 J% A$ Q7 x& Y( H* o1 Y if (EFI_ERROR(Status)) {
. d0 H4 U4 M$ q: i+ b/ u- p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 A1 K) }& P! n, u) R0 r return EFI_SUCCESS;! ?+ c' J6 Q( S
}
2 @, h" w: _/ i5 H8 M' c
% B- s% [% x. F0 ?; \6 B/ N Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);, [7 m6 W% R. B! v) G# v8 F$ Z
if (EFI_ERROR(Status)) {* M3 k: v% G/ u6 o
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
& d" V0 D: V5 o return EFI_SUCCESS;: r4 H" U3 Z2 ]3 ]
}
% `+ W0 g- ~: a s( F; b8 K Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
U2 V( R6 J* v! r, O//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
" ]& S6 b; L9 ~ T; ?6 a5 K1 G' g# F" O4 ^% V! a
Status = FileHandle05->SetPosition(FileHandle05, 10);
( F" |" A6 z6 K if (EFI_ERROR(Status)) {
; G. Y0 a% }7 Z; O* Q7 f Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
% N3 x: }* x+ n return EFI_SUCCESS;
: J8 R9 s( q0 b& ]3 m8 E5 {0 _6 p }* A# @8 c( f8 ?+ X' A% L
& \% M* ~$ ^ b& b2 Z+ X4 n% M Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);* q; Z& Q% Q, d
if (EFI_ERROR(Status)) {0 H: y& n- |" y$ \; \
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) I6 j2 U# |' \1 Q9 d8 p return EFI_SUCCESS;, e9 e% O5 {8 C) Z. t( U
}
1 A5 Y5 S, ^' J: y+ E
4 l: e5 J: g8 ?; S( U. T6 o Status = FileHandle05->SetPosition(FileHandle05, 12); + u! o* d! Y0 T) V2 }3 j* W2 u+ Y
if (EFI_ERROR(Status)) {
- k8 Z/ ?1 H# ^" Y) { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 n F) ?, X4 p/ F d- y" G1 b
return EFI_SUCCESS;2 y6 e0 K& H Y% ]
}
& k# b5 m% p; s u0 ]- P4 _
' t/ w- m9 @; H# _ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 Y% p+ s6 Q( C5 n6 u if (EFI_ERROR(Status)) {
& d8 f, E! o7 G: Y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' [2 O$ d* q+ r* o
return EFI_SUCCESS;
" m) }# L. j% x- z" B }0 l' k" L4 q& ~1 m
! c; ~6 }9 L0 p2 ~& ^4 l4 i Status = FileHandle05->SetPosition(FileHandle05, 14); : `+ t( v1 R# H7 Y9 R: W8 @- @+ L" {& _
if (EFI_ERROR(Status)) {
# O, f6 H' M' A5 f+ G7 E1 b7 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( q! [% F' @2 L return EFI_SUCCESS;
T# }5 Q) z: @ }; u2 p }
, C4 j: Q" C; `9 Z% ?, x9 s/ c
O- J% l' x- Q1 G& s4 P7 b Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);9 X3 d) z3 L7 q% C
if (EFI_ERROR(Status)) {
* I- r& z9 D6 E; Q1 l6 L. V5 i Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 Z' B% c3 }% `3 k" V return EFI_SUCCESS; H& h9 ?. y7 z
}1 m' C, B6 X( h- t
' ~6 P. Q+ L; K% P
Status = FileHandle05->SetPosition(FileHandle05, 16);
) ?9 _7 D# q( g if (EFI_ERROR(Status)) {
9 O( f/ I6 @' D' ^4 P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ v4 [+ N, N @8 b( ]! o! p- B return EFI_SUCCESS;. T7 e6 R' D! ]* g7 S. F5 `* N$ \" `) P
}
1 ^: m3 ?7 K! i' I L8 O" ?% m7 I ; q4 f) `. ~+ {1 m3 J- l
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( T: B2 }) }% e3 y# H
if (EFI_ERROR(Status)) {4 C/ n) w& y4 ^3 ]
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. o" `5 c! ? p# s) Q6 h' \1 @ return EFI_SUCCESS;
! N$ U+ d7 [9 S* W4 e }
) a, q9 N4 q' K# J! n9 _! ^% J/ Y3 M" {* z0 N8 K
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
! p6 P/ i$ D; {& x n7 T
$ G3 `2 F) f1 B; c2 S9 C Status = FileHandle05->SetPosition(FileHandle05, 18); ! I9 }% R P. |6 m! H
if (EFI_ERROR(Status)) {
) S2 Z, H) g, c! n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
G. o# {% e) E6 ~+ N3 U- Z return EFI_SUCCESS;
$ z- v \# N$ g }8 X* b7 W' O# p6 X2 M$ q
: p6 x0 H4 a) S4 ^) }' |9 J Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);' ^0 y) t' t% I6 `% w1 N0 \7 k w }
if (EFI_ERROR(Status)) {
1 ^' w* { n1 E& A' h Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 u2 Z: m' P1 M2 e# A$ C! A' }
return EFI_SUCCESS;
" W$ R8 E7 M/ B- c }
0 A( P7 r) M. Z$ P' ? $ p; a) @/ s; ^6 r P: v) ]
FreePool(DestAddr);
: U: p- w& x9 `& Q
% ?8 E+ y5 m+ T% J Status = FileHandle05->Close(FileHandle05);; N9 T8 r( v- E2 T, h- P( p6 Y
if (EFI_ERROR(Status)) {
/ s S7 b- i0 o% Q: a. E- a Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
p H" a3 w1 F. L% o# a return EFI_SUCCESS;# z( ^5 D5 P$ b4 L* W3 T
}
6 u) O1 G# f7 ?9 a+ _6 M- t Z ; |2 d( U4 m0 }5 P
Print(L"File Name = %s has already been created.\n", FileName);8 C9 p! c. Q( E! B' Q$ O
! ~* ?# L. ]+ U6 |5 }8 C J! u Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
) S4 r! _6 j. D3 O3 F) G WaitForKeyOrReset();
0 y& n( q# `5 W" V$ [ 3 Y( Z5 |0 B, m" n( H( o: K
return Status;
7 b, X: M. X: }} |
|