|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
! I0 ?* X U: P- M * myfileio.c8 C0 i! q: u% _* [0 {0 \$ n
* Apps
* F( O2 Y. i' Q+ a0 r3 x */
* Z+ E% l3 O9 N, p: p
6 e6 r9 O$ T* B0 n) o8 N#include "efi.h"' U k/ @# N W7 |% F" U2 n1 Z
#include "efilib.h"
9 d3 T* T: F6 s; w$ s" C0 R
. D0 [, H+ P) i# T6 \#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
: A0 q- D" V$ h C) w. C" L/ f* u/ ]
+ w3 f& ], H' s8 j% v+ nstatic EFI_STATUS WaitForKeyOrReset(VOID)
1 i V* N5 R5 R/ f0 t2 G# s{
$ ?. ?7 H1 o4 X9 B EFI_STATUS Status;
' F+ c k( W/ D1 o EFI_INPUT_KEY key;
# X$ W! @5 `7 w# c UINTN index;+ \1 Y( ]1 x$ |6 W+ {5 Q: ~, y
6 k$ O- Z1 Y/ {7 ~& [" Y
for(;;) {
2 Y/ D: n z# f% |7 ^" _ Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);3 P+ `; n/ ]. k+ J# \2 \
if (Status == EFI_NOT_READY)
H. T, A6 l; g G& v9 I BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
& E+ X$ V4 M* C else
' L( A" z8 j' T! Q9 f- I3 P; G break;
) \% k. q' h$ n, v5 V i& z }4 N8 X$ B% Y2 k
if (!EFI_ERROR(Status)) {, K0 p( {6 ^: h3 n0 ~2 B
if (key.ScanCode == SCAN_ESC)
" g. C. q' H, M, d0 n2 _ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);2 [$ x+ o! P& g+ {/ I& f) Z$ J. s, n
}; e+ {$ H0 `3 s! X b
6 @% ^$ G/ }, x* a$ ~" n8 J return Status;/ h' r5 h2 p8 L( ]5 G! D
}$ j0 {) a# a" f
( N4 X5 N5 Z* z l6 \4 W$ G
EFI_STATUS
, i! A" P, @6 h/ Q) T3 S( ^EFIAPI
! `6 A, O1 H, i5 iMyfileioMain (IN EFI_HANDLE ImageHandle,0 j3 {9 ~5 s# O3 N" K% w# T: \* ?
IN EFI_SYSTEM_TABLE *SystemTable)
) ]( `) h; ?0 ~, P6 x{
3 ~+ |+ R0 H: N% a8 f EFI_STATUS Status;0 V9 s) Z1 d- t( r) N& D; P4 i
EFI_HANDLE *DestAddr; 0 a5 D1 S& G+ f4 W1 {
EFI_LOADED_IMAGE *FileHandle01;
) v* ?% v8 I2 j% | EFI_DEVICE_PATH *FileHandle02;
8 C7 f- v; n H0 J% W' _+ c% j+ g I% Y EFI_FILE_IO_INTERFACE *FileHandle03;) M+ k4 P% K* D6 O" k' ]
EFI_FILE *FileHandle04;
/ i! k, ^# O L7 ~ EFI_FILE *FileHandle05;
3 K1 \. n& Q3 K. t CHAR16 *FileName;3 d" P* u# c; w3 B3 n) y- {
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;% m8 }6 ^$ N7 a# \6 Q
UINTN BufferSize = 8, BufferSizeKeySpace = 2;# X# M7 ^1 F$ f" W# C6 N" I5 {1 I
int Bit0, Bit1, Key_Space;
2 {, w4 |# N0 }% c5 {
- T4 c) k- w* _2 w6 x! z5 ? FileName = L"NewFile.txt";
" N$ C. {, u: D( h% j. _: v Space_Key = L" ";; x$ ?: m+ E& A( H, I
* m: h; q3 Y. T( p# r
BufferA = L"ABCD";& m, P. N: _' N
BufferB = L"EFGH";
8 [5 s/ a& F# w' M) B BufferC = L"IJKL";
7 P& {9 s( T8 ^1 x' ~9 R. ]8 }$ Q3 ^0 j5 U6 z
Bit0 = 0xff;
6 e- G3 U" w8 h+ y. u& ? Bit1 = 0xfe;8 R: j+ ~( w4 \4 t
6 g4 `7 C9 e& B- A Key_Space = 0x0020;
: H5 @1 ~1 S6 Q. Q9 Q/ [
2 @6 a# w* m6 r% g! ]- X7 k InitializeLib (ImageHandle, SystemTable); 2 h) Z5 k" P4 i5 Q& @% T, p
, `! b) w; Z0 E' g: \ K3 [% F* M DestAddr = AllocatePool (BufferSize); + O, U c; ]5 P+ v0 L& |& R: V/ T
% k, _! p# Y$ y
Print (L"Value of Bit0 is %x\n", Bit0);
0 b8 A% ~' j2 O Print (L"Value of Bit1 is %x\n", Bit1);
: A; Y4 f6 |9 |. K0 M; }" i% q' ]+ q2 u# ~$ T9 _! b' {( |
: g. h" I* T6 q9 @, f6 Z) p Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);( Z, I# g2 i6 w0 ~. I5 \
if (EFI_ERROR(Status)) {
3 J; X: L7 F) S. \6 @) R3 K Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);/ ]* t: _3 M; U( S, g
return EFI_LOAD_ERROR;
: ~* w3 s& h: [6 k, } }' G2 a+ i! o/ q* j1 j
* q) N8 S1 C. o Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
: a; n" }3 U8 g# p6 j8 l: g3 b& g if (EFI_ERROR(Status)) {
! D1 D- {5 A; e4 r! T$ l9 q% P" B7 a Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
G8 w3 L& _+ J8 i! i return EFI_LOAD_ERROR;
! q( @: C9 S' \( \: u5 o1 U }
0 ?) m2 n- ]) ^& N/ i/ l
& [/ E9 }# z* E1 U2 b; D Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);. ]4 t! Q- H* A2 N$ |+ Z0 _# l
if (EFI_ERROR(Status)) {
6 V" D3 J. z5 S2 c o- L Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
) _! [3 F4 P+ X return EFI_LOAD_ERROR;' z5 @% C# N/ e/ `4 e( c$ V
}
; M6 p5 t/ Q- }" V$ P1 l+ R& z, U. O2 Z/ q- R+ P$ S8 h* Y
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
3 k1 U- E: k0 n& N+ X) W if (EFI_ERROR(Status)) {4 W- n. i4 T3 A! y4 c
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);8 R7 ^7 ~0 y5 k, _
return EFI_LOAD_ERROR;
0 { h" e7 e+ Z# b: X }
; r! f" N. }0 J( k
- k: m. W3 ]9 F# P0 W: ?3 i Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
" X5 Z. T$ R, b if (EFI_ERROR(Status)) {
' u! u6 a& o# Q7 G/ { Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
- L$ L. y' B6 k/ r7 M+ N return EFI_LOAD_ERROR;
* q7 J) g a3 B" `0 j+ E }
, J* d/ A, i" s$ |$ X ?; i 2 \4 }/ ]6 _/ O' g7 e6 x
Status = FileHandle05->SetPosition(FileHandle05, 0); 2 ^ ^# u2 F: Z2 X
if (EFI_ERROR(Status)) {
) u+ V \: X& A- Y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- t% ?; Q k, W! t3 d8 D/ P
return EFI_SUCCESS;
& t* T4 K& M g% x# Z+ y }
& B3 e2 d! c6 w+ U1 ?3 Y7 P 5 V: Y I0 [, C7 E$ _2 f
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);% o' b+ ^/ V/ O h& B
Print (L"File Buffersize is %x\n\n", BufferSize);3 C) m2 \ ^. s* s9 a9 \
if (EFI_ERROR(Status)) {
M+ \ |( Q/ O0 n. _ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 h* q5 F" Q; E. A/ c return EFI_SUCCESS;
x# |. O4 J4 ^ }
+ x3 ~4 k9 n. u% m/ g0 \
/ d7 j& N$ x0 ?9 u, R6 e9 Z Status = FileHandle05->SetPosition(FileHandle05, 1);
4 ?& S: ]+ f* c/ O if (EFI_ERROR(Status)) {0 h2 T6 D [' s# {9 N: n/ p
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 T0 |7 X" A4 u6 h9 w" g3 J2 D' ^
return EFI_SUCCESS;2 `- ^( C0 y. z
}
8 a5 v5 b7 N; v" @* o, o) Q8 A
; J, d$ }' }! ?# S Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);* S. a9 F$ d7 b- r3 a @4 L
Print (L"File Buffersize is %x\n\n", BufferSize);! A* m- L# X) h' x% Y6 w2 Q! O3 I
if (EFI_ERROR(Status)) {
8 i* S6 B- \8 a8 h# K' d) k/ n Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);8 y4 k9 b, T* F3 p; P: V
return EFI_SUCCESS;
0 b7 F" e: [& n: h7 {4 c }
1 R5 m+ C- [8 |
1 N' j' v5 M- J; L1 Q/ m Status = FileHandle05->SetPosition(FileHandle05, 2); 2 h( r' y3 I9 y( J8 ]* s/ B
if (EFI_ERROR(Status)) {
) R! g; y; g1 |% W5 Q+ b1 y" w6 y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 l* ^1 Z% U, p return EFI_SUCCESS;2 u9 Z( R% N% I
}
& R, ~, l; U [4 H
8 X" y# i2 u/ g0 b Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);4 K' m6 D" Y% Z: U
Print (L"File Buffersize is %x\n\n", BufferSize);' }' \( A C$ x& L+ D# [2 W$ p% i
if (EFI_ERROR(Status)) {! u; c; O: q, h+ I+ p1 L
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 v/ ]3 V; w* e) k4 H return EFI_SUCCESS;
+ w( O3 v: D7 T1 m+ @5 [, D } 6 R& l" N; ]$ W/ ]4 ~/ g' w
6 f" P: z; p, m1 ~//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>+ n2 n' S8 z: K+ H% _/ h0 b8 f% @
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
: d* r( x( d: f: o# t Status = FileHandle05->SetPosition(FileHandle05, 2); y8 o/ i: S) |3 T6 p: I. L
if (EFI_ERROR(Status)) {: B7 l% B2 x7 c6 O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& L/ ^ z) R7 k s1 M
return EFI_SUCCESS;
3 O3 |! R. }5 J7 @/ e* {7 N }
" X, p8 U& j9 x. g1 Y g# N1 a z0 p/ T' w& K; P
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
( }# s3 G0 X4 m/ s* N3 K3 I if (EFI_ERROR(Status)) {# D1 R" u. L! ~4 u" |$ d
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
3 Z" | Y# S. ]1 f* E% P return EFI_SUCCESS;3 N. i) [) v. R7 n
} * v. ]1 ^7 y+ L! B; y2 P
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
* q |) [% d8 N//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<% Q- J( @( C& j3 y7 a" ]' v
& I' a, r( D/ E Status = FileHandle05->SetPosition(FileHandle05, 10);
! G( X8 J7 L) i- C. ^% @& c if (EFI_ERROR(Status)) {- E7 W+ n4 C( y7 C& i; p6 F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 J$ f. k, ~7 J/ L& `6 O
return EFI_SUCCESS;' x2 O& H n$ j
}
) F1 f$ H5 Z. x( n3 N
7 I' D: D' d! K+ f& h% J4 v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 ^' y7 H( |$ I4 l if (EFI_ERROR(Status)) {
: w ]( A: I+ l7 i! A; g! | Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: i$ u3 n4 { F/ g8 z! d0 l; ?5 i return EFI_SUCCESS;) [% [$ g# N& z* T
}$ q# K7 l9 R1 \$ I
& c0 r% F# t* ]" I Status = FileHandle05->SetPosition(FileHandle05, 12); : {3 M; m$ N1 t' Y- u
if (EFI_ERROR(Status)) {4 P; t- n+ X3 N" S( d9 O" \, A* L
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ A8 ~, q& t, `# _/ P2 T return EFI_SUCCESS;! E. m/ f l" U8 r) T
}
4 V9 C4 }2 s( u* ~2 V - ~, B# T+ S7 T3 K! S
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& J7 u( j) d \6 M* O7 w6 z
if (EFI_ERROR(Status)) {$ h( |4 e5 A2 G2 X. v
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);1 |0 j( T' i2 g b& g$ J/ w
return EFI_SUCCESS;
& r, Q, m& y l, o& h }3 a. _+ P/ U, I7 f a1 N" d
. p7 j7 {( F+ ~+ ^: ]' |- L$ l
Status = FileHandle05->SetPosition(FileHandle05, 14); : S' o' Y, ~9 I
if (EFI_ERROR(Status)) {
: j6 i2 A7 c3 ~! I8 h8 H8 m9 Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ T, y* f- f8 N. E% E" |6 n/ {' o- T return EFI_SUCCESS;
; C. U6 v4 v$ J5 h$ R5 p% \! E8 e! [ }1 @* N( e1 F6 `/ k M
/ w% d# i6 Z# \* |% m/ e Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ C( a4 [" ^3 F" ` if (EFI_ERROR(Status)) {1 Q R% o) S. y4 h1 Z: [
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 y) ~- u+ }2 v& |
return EFI_SUCCESS;( X. i) p4 D; G. F0 r, M- D: }; L' N
}
% x! b4 g. n% e& E& h* {: B8 r Y) A4 \" |' W) p% h
Status = FileHandle05->SetPosition(FileHandle05, 16); 1 C- r+ Z) K/ g/ m3 k( u- m( Q9 y- u
if (EFI_ERROR(Status)) {
3 U& ?) \/ @' {: Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ V0 S9 R- d; e% a9 y/ H9 ^" C
return EFI_SUCCESS;
% C' c( a$ Z9 Z8 s) L, ^- U }5 ]8 L' q0 O% s, N3 \) ?
* L4 w4 n) ]- z: ~; W
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 j( I! j6 _2 M9 C4 Q* r if (EFI_ERROR(Status)) {4 b# a; T# `& w& i6 b1 f; B+ a
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' [* F r: e, X4 ?4 A! l$ K5 X9 W
return EFI_SUCCESS;
/ x" U# d i# u5 @% d2 D } 0 N) z+ f2 C) `: e
/ {, h+ H) [: q2 V" A
//--------------------------------------------------------------------------------------------------------------------------------------------------------------- B. [% { F8 S+ X; k/ H7 L6 q; U9 q
3 ?& N4 U& x- r( P }; d! x& D
Status = FileHandle05->SetPosition(FileHandle05, 18);
2 i4 G8 N' e( L" ~% n5 b: j3 U# [ if (EFI_ERROR(Status)) {, h& ?$ R2 n. B6 _( T( U- i3 U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 Y0 d& U4 u7 u6 q F; c return EFI_SUCCESS;6 A# N' r. k: F- V( s
}
2 R* P' F1 v0 u$ Q, { : d$ n/ L" j |, ?; ~
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
- l8 ]4 B; f' h if (EFI_ERROR(Status)) {# e3 _1 ?' `6 `& b% p; U5 K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 j: g) e: X$ p% B/ N2 a
return EFI_SUCCESS;- m- @) s B3 ~& d9 E v
}
y) s0 g- m+ Q( ^5 ^ 4 h) L- B! Y$ m' f6 d. m. {
FreePool(DestAddr);
- `- k. n ~( \2 m, ~5 T9 K8 ~' o: ]6 E& f. s" m
Status = FileHandle05->Close(FileHandle05);
: Q! Q% @, A2 R if (EFI_ERROR(Status)) {
1 [- F3 m2 N7 _1 O( W5 M5 } Print (L"Could not close file, FileHandle05 err code is %x\n",Status);! {* p" E4 d0 z! B4 [
return EFI_SUCCESS;
- q) O: A% K7 R" { }0 R) ?% @2 P8 I) i2 b
$ w: H9 K' B8 p% t, Q4 j% c
Print(L"File Name = %s has already been created.\n", FileName);
7 l9 `( v" [8 P
/ U+ C. n. Q$ E" N Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");, T/ r, f: Q; x( V4 I% |# `
WaitForKeyOrReset();
8 y9 Z4 } K' [% v+ S5 P7 h5 V8 w - T( Q3 ~# g: T R& g
return Status;/ M4 A% x$ c; O1 o" i5 F2 Y
} |
|