|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
: B9 b% P1 t9 i! ` * myfileio.c, ~8 w; {2 k* }; J$ {+ q+ g) A
* Apps; _* A1 P6 M4 m% T- j& `- H/ D% L
*/
& X6 Y4 {; i8 \& W! ?6 d$ e% }0 H1 M4 s: v$ M
#include "efi.h"2 b. d, E3 W# @7 C: X) c# c
#include "efilib.h"
( x1 J. H1 A& X; ^8 K$ F
]$ `, ^( Y% V5 o- j#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
% D: ^2 d% r$ A+ Q( L4 D t% H: W8 j8 d2 i# Q7 i/ Y
static EFI_STATUS WaitForKeyOrReset(VOID)
+ X$ B6 z9 p: e; K0 ~{
9 j3 J. e2 s" | EFI_STATUS Status;
% E6 v* c/ b' P8 L EFI_INPUT_KEY key;0 {$ \& S( w/ M1 r5 N: H
UINTN index;
7 ]. X# s8 h0 S1 i% I9 {
/ N( p, G, Q# l. f' z$ C2 ? for(;;) {' l% {; j) B; M9 c" C7 }; Y
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
9 {* d" q9 i7 ?/ @4 l( n if (Status == EFI_NOT_READY)
2 n( W/ L; S/ t- B BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
6 r3 g7 }' m! i! v _ else; Y$ p' v5 T9 D7 g. n3 j$ h
break;
2 @ g% U$ n: q: R3 F1 a* F1 g }' q2 \2 Z+ [, x# G$ \; u- U9 V
if (!EFI_ERROR(Status)) {& \$ X' D' i/ p! R0 P
if (key.ScanCode == SCAN_ESC)
$ T: J& W5 J: R# } RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);, R! N6 ~% s! @# e7 e
}, j' |) g" T& |7 u2 |( Y: T! B
4 y% x% I! X, \4 V( U+ |4 c return Status;8 R, \' K. l7 Q8 p, R+ m
}! p- Q# B4 I% Y: n9 r
% X; ?' }) i [$ e; yEFI_STATUS- N9 Q0 o; u- p4 `
EFIAPI& d1 q! @- ~. X% K/ U$ H1 ]
MyfileioMain (IN EFI_HANDLE ImageHandle,$ `& T5 @7 r( P, A W
IN EFI_SYSTEM_TABLE *SystemTable)3 e, a7 R% Q& y
{
: E- @& r' k2 }& ?- a6 k( m! g EFI_STATUS Status;
3 A& P; ]; L) D EFI_HANDLE *DestAddr; 1 r, Y* a8 r- `
EFI_LOADED_IMAGE *FileHandle01;' P/ n, U5 h% z, L6 u
EFI_DEVICE_PATH *FileHandle02; 2 ~+ M5 S& o. M& x
EFI_FILE_IO_INTERFACE *FileHandle03;$ g8 M. T! Y4 P+ q) k! v& {+ D
EFI_FILE *FileHandle04;6 b7 _* H+ D. _
EFI_FILE *FileHandle05;3 j2 d/ `! [ E
CHAR16 *FileName;8 ~) | Q2 [$ x0 L
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;% ]6 T# ]9 C; {$ a0 a& z% `
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
$ M+ i3 j0 n4 Z: I4 w; M; r int Bit0, Bit1, Key_Space; # V3 N+ m5 `. B, `/ N5 f; k l
' N2 [5 C0 d/ O5 L. z, I
FileName = L"NewFile.txt";
! x: [( P2 R" j2 _' B, N# V Space_Key = L" ";
5 \% | a, J* v' Z- R8 W: H$ ^( k
3 S5 z+ w9 b2 k2 h/ v BufferA = L"ABCD";( j( q; Z* C0 f, ]: N' q: v8 D
BufferB = L"EFGH";/ O. B9 y4 O% {$ U) v. s
BufferC = L"IJKL";2 B* \7 r5 B7 a
0 { {$ ]2 I6 x5 o- X* S# h/ I
Bit0 = 0xff;
0 s& k2 l$ [1 G5 z2 Z" p4 n# F/ ? Bit1 = 0xfe;
]7 s* @5 H$ |7 }
" o) Q$ I7 F1 ?' o3 l( t Key_Space = 0x0020;
s) I6 f( m$ \- p) h) _4 {) { 9 b: P* x5 j" ?; k( e/ U1 r
InitializeLib (ImageHandle, SystemTable);
, W2 D1 [7 `# i3 k+ b) z' H* T3 A- ^. R4 j1 B
DestAddr = AllocatePool (BufferSize);
6 e; A6 {& O& U& x4 a' x" t3 o$ T. E+ \
Print (L"Value of Bit0 is %x\n", Bit0);
, J& s. W% E @8 H! G b Print (L"Value of Bit1 is %x\n", Bit1);
' N& S- Z$ X3 m# R1 A& N. h! ?9 C. L0 r5 ~ ]7 C
+ ?* P3 F- F# O2 a* ~2 M- e
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);. Z9 j1 R/ m: W
if (EFI_ERROR(Status)) {) Y4 L7 \* X' i+ \. D% b/ t
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);- F' Q1 j, o" I# Z
return EFI_LOAD_ERROR;
& [, F2 l9 E z& s _ }4 j3 ~# K' ]) k5 D
/ R! C( |! }* g+ x Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);5 z2 s8 ~* ^+ H0 Z: t
if (EFI_ERROR(Status)) {' N5 A! K. J& ^/ X; G2 Y
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);8 ?6 n# \5 t4 F, @5 j E
return EFI_LOAD_ERROR;* k% x: {: N! d5 N7 k. Y' G
} + p* y# b( F4 L. c) O
$ r, d6 D/ I0 @, }' \ Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);$ h9 f, O% n! `% _2 J
if (EFI_ERROR(Status)) {; [8 p5 W; J+ B
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
: E& y7 ^) T* K6 V/ ` return EFI_LOAD_ERROR;
" F' p8 s- R. x T }" i9 x' Q& K6 n/ k
" C, f: h8 m: \; H' q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
1 ~! {' y9 e" ? if (EFI_ERROR(Status)) {( b( P( U0 W& U3 [7 T# g
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);. d* Q* r; P8 j+ G2 Y( n' l
return EFI_LOAD_ERROR;
( K: r* g- S/ D7 p' o, g }
1 ]+ Z& I9 d9 u7 c
: G! \5 ?1 \5 K0 j Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);: @ U8 B' ]& P9 b/ f( b1 J
if (EFI_ERROR(Status)) {
: D1 c" F; o3 j/ F9 T Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
) u' F8 \8 ?7 x. O8 m. _ return EFI_LOAD_ERROR;( G% r* D' B2 B2 [
}
% }* Q- z) b9 c3 r. {
, s. y: |" m) O5 ?$ ~+ i Status = FileHandle05->SetPosition(FileHandle05, 0);
u3 N7 ]2 U( g ^$ L if (EFI_ERROR(Status)) {
: A: `& R; Z9 P+ ~" d, i Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 ^4 X( m5 A* k# Y3 K* S9 j
return EFI_SUCCESS;
* W/ p* c5 p y$ ` }7 Y9 ?5 n' T! [# F
+ `! J0 y. A7 v7 r4 @7 A Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
4 a# Q. _4 @4 p Print (L"File Buffersize is %x\n\n", BufferSize);' G+ c; G3 P* x9 k& @! Q, Z" V
if (EFI_ERROR(Status)) {: n" j) [0 n& ^& u* E# H0 ~8 c; c
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
' s6 y+ \& ?8 k y+ J return EFI_SUCCESS;9 l1 S0 m0 p5 I$ a5 C: q
} . Y( f) U- ^( ~( x6 q2 G: j
1 r4 q1 G% U6 K' o Status = FileHandle05->SetPosition(FileHandle05, 1);
8 v! o4 W0 t( c! A+ j if (EFI_ERROR(Status)) {
/ F3 U( P# L) |4 U Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- H7 L" f, [! E, \0 l
return EFI_SUCCESS;
' G; W: ^! `9 w3 o: s }& k. Q2 x' I. ]) p* g+ n1 G6 i& @4 i
5 r8 ^; y5 `6 @0 D
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
7 \1 o6 j+ L9 X$ n Print (L"File Buffersize is %x\n\n", BufferSize);
5 \* \& v, ?# o- t' x& ?6 e if (EFI_ERROR(Status)) {- C6 q. w0 W9 ~% G+ d
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 ~% o+ `5 c b8 Y return EFI_SUCCESS;, m+ {- B6 R6 q* e$ S; [& [3 ]
} $ p5 y+ D5 @6 i5 a
" |4 T- P! m7 T/ A. B( k" N
Status = FileHandle05->SetPosition(FileHandle05, 2);
8 I& I9 Z6 N6 Q( r7 G0 \0 S if (EFI_ERROR(Status)) {
" Q T" T0 T2 q' G- k& I/ k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 U" ^' o# w u( c
return EFI_SUCCESS;
7 H- d) K" n0 M+ j9 j }2 a% U* g# J9 i
5 _) U: A0 U4 d4 V: V2 Y: P7 ? Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);4 n# `" h& K3 C; }' J
Print (L"File Buffersize is %x\n\n", BufferSize);) z% Q! J$ ^1 d+ o% b
if (EFI_ERROR(Status)) {
8 O- \, | D3 m% C8 W( F. \ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
/ k/ K1 x+ A" X) u a8 ^# O return EFI_SUCCESS;& `5 b$ T7 y3 R% Y' f+ w; W% {
} , B0 b! r4 f: R9 v; x$ t0 f- \
! R5 i8 j: M4 c/ u3 [. f) G/ ~
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>( \5 n( G6 S/ u$ }
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);2 S; O+ J. G3 A/ E) R$ P0 L
Status = FileHandle05->SetPosition(FileHandle05, 2);
- c4 r. [3 K) e6 a- ~3 X if (EFI_ERROR(Status)) {
& z, e; R J0 { q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ B1 i2 I% Z# ~2 P( m
return EFI_SUCCESS;/ a0 F" u% t# @) V; e/ r0 N3 j
}" r) k( ?1 ?3 R
A( W) T/ j2 l Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);- h6 f9 l: C4 X9 i6 ^/ L' X# e
if (EFI_ERROR(Status)) {
) \! I" q% G6 B) T Print (L"Could not read file, FileHandle05 err code is %x\n",Status);- K0 \% h& U s B
return EFI_SUCCESS;
$ f7 [* V1 h# p: o* G } ' t! h4 \, |7 U, r3 m; h
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);* _: n1 Z$ a5 l2 s i7 e
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<) p3 s4 N$ |* d! K: Z+ t
: ^4 N' J( }5 w S6 z' ]
Status = FileHandle05->SetPosition(FileHandle05, 10);
- j$ |0 U5 D+ J# m if (EFI_ERROR(Status)) {$ s' c' k0 ]. B* S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. s F" r% |, l7 ?* v
return EFI_SUCCESS;+ x. A- P, l3 L# F
}* r" U G* y r* a. H
# P9 j6 q/ c4 \3 J
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
W# v' y0 U2 k/ R1 ]/ A if (EFI_ERROR(Status)) {
I6 u( B1 w' C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 a& h0 ?7 J. l- u7 ^, X5 T return EFI_SUCCESS;
5 o5 V5 m9 u! N, [0 f+ y& p- `9 C }: Q$ y6 k6 u& O: a0 T7 C( ^- e" F
$ K& d; d; ]" E& H
Status = FileHandle05->SetPosition(FileHandle05, 12); , G8 T7 Y* d3 @& N) K
if (EFI_ERROR(Status)) {
9 p$ ]& w4 c! J, C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. Q) K6 n i9 k/ ]- `. ^/ b0 a return EFI_SUCCESS;
; I1 y6 I7 v4 i2 F6 i& B$ U. `! ~ }
# a K! ]% j, R+ ]* h
$ w! k6 E! R. @1 e+ D0 X Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);% r, s, B+ @6 ~3 G; O Z
if (EFI_ERROR(Status)) {; _% s9 b6 F, t6 L0 \! L8 J* y- v
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. U6 @$ M# c/ A return EFI_SUCCESS;9 S, r& n' J% O2 a" \
}
3 X$ f& I- s$ e$ D5 R% g0 |2 i6 k+ `1 P. F, t# V9 a: @; f
Status = FileHandle05->SetPosition(FileHandle05, 14); : p; V# ~9 c7 ]) ^
if (EFI_ERROR(Status)) {
- M, q' _( u* p7 n. B Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 `2 X2 n% F# _' `1 Z
return EFI_SUCCESS;
. H5 F" K9 f2 ?- K* ]0 u }7 T% a3 r6 G; t0 T. ?
9 Q& l/ V# @/ U
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);5 W- S4 d" }) e% D% J
if (EFI_ERROR(Status)) {
|6 O% w$ e% S Print (L"Could not write file, FileHandle05 err code is %x\n",Status); k+ Z5 a8 B3 G3 N+ j* [; ?4 @+ R
return EFI_SUCCESS;* [ N) x! P5 @, a( L/ @8 w
}
# f+ J# u) t2 \0 Y, O! G) ~, k0 o, a: z) P
Status = FileHandle05->SetPosition(FileHandle05, 16); 1 i/ B& x$ K# o3 U# C8 i U0 i3 i' L( ^
if (EFI_ERROR(Status)) {+ b& }' w S% u9 |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' b; I/ b8 Z8 b4 v8 @
return EFI_SUCCESS;2 c6 f. D7 O. J
}$ ^1 Q6 ~( m% Y+ F
! l4 \0 i, E$ G
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);2 L8 A* W* I8 [5 v, \( q
if (EFI_ERROR(Status)) {7 R( C: K& E' S2 W! x0 G
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 C9 y3 l2 Y- E6 B# _" q# ?/ }/ a
return EFI_SUCCESS;
1 C( K/ Q0 }+ `2 t1 j% `4 z }
* k& \& Z8 X: t% t
: G' @# [+ X! v$ x( U//---------------------------------------------------------------------------------------------------------------------------------------------------------------
0 {4 e/ a/ o7 ~: R+ p/ r: K7 s' o) r, O8 N2 P
Status = FileHandle05->SetPosition(FileHandle05, 18); , j. _, d! W- \" v( o! l
if (EFI_ERROR(Status)) {
$ i' u8 S; A8 Q7 Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, s' C" Y' U7 X return EFI_SUCCESS;
0 [$ o& Z. l* d }% o2 J5 J% n4 H% L2 w$ q+ k
" u3 _3 p( C$ C5 D! q" H) x6 u
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);. q& a& _/ u4 `- s, Y& o
if (EFI_ERROR(Status)) {
# [3 f( w2 w' r" G, } Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 O* D; ]0 V( x* b$ C+ W$ H4 Y5 b return EFI_SUCCESS;
( ^% E* v+ h( b9 [$ h; X$ F: @- \) H }
_% T2 t ~- j7 x' W/ W J
; B) _6 i# ?7 Q* n FreePool(DestAddr); 2 @# n! G Q9 t5 X8 b" r, F" U* Y
/ C+ g5 p8 C: ~1 Z0 ?% }5 P Status = FileHandle05->Close(FileHandle05);) U7 ]5 e; t: P" b
if (EFI_ERROR(Status)) {+ A, g" ^. q; m6 b+ L3 d
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);( T f, V6 G! w/ Z! R1 F9 p
return EFI_SUCCESS;, }/ c* T K) ~! Y3 D- V! v, ^& q
}
" D& b$ m& t& c9 `/ G, p % l1 V8 E/ Z" T7 [
Print(L"File Name = %s has already been created.\n", FileName);. T$ n8 H; v M; @* Q+ X
& y4 U& F2 c& S( D6 z, Q' o
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
5 y5 k+ s/ ^2 U' c WaitForKeyOrReset();
2 F1 q$ m& C( l9 o, A$ U 5 h& w9 K( r: d3 i
return Status;
' m0 v( R+ }7 {2 A} |
|