|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*. ?. m0 q$ }. [/ e4 P
* myfileio.c8 H8 B2 r- \0 O3 @6 z7 y5 f& z
* Apps2 d0 _5 D7 h) g& }7 r+ i
*/
/ j* M* S. r- U- R( j+ e6 J( O" N1 Y9 c9 D. |
#include "efi.h"" } Q0 A" w; K. [6 F
#include "efilib.h"6 P. l6 O* t* g% P
8 E. t0 o k" {3 A; k
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
+ k; G6 u# d4 B5 j6 e3 i: F
& T+ C2 n* M9 j; k# m/ d5 kstatic EFI_STATUS WaitForKeyOrReset(VOID)
9 z$ x+ A! h7 G3 S{
- J; m8 l4 _1 H1 n EFI_STATUS Status;
9 p w& e; W2 R6 ~! f EFI_INPUT_KEY key;3 r7 e5 M6 H7 |) M" B( S6 J2 w1 V
UINTN index;6 v8 f4 w1 h3 l# _% Y5 T
@. p2 L( \4 l% v, p: n& q* Y for(;;) {+ P( a+ m4 p( e
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);4 N1 w. I4 w, t/ _5 e) r
if (Status == EFI_NOT_READY)- `( J" Z+ R+ J) w e+ x
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
, C) v1 |. f: k, m else
. f! I. Z' \7 b4 j, L break;
) a' b3 j0 F. e- J1 R6 B+ i }
# t$ W7 p0 V) Z k p. Q/ p9 h if (!EFI_ERROR(Status)) {" F7 A; I# N/ z* {
if (key.ScanCode == SCAN_ESC)+ b8 N% j( s6 y+ s7 U# E' ~/ E
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);! t/ \; b3 \8 c* i& B/ D
}
6 g/ q: K: J n
4 [+ `. O9 f6 F1 G7 \1 e return Status;+ H4 `4 r- [9 g/ Z. s% _( |
}
: j7 B+ R* m) d8 e, t
* o& e# A3 |2 K2 hEFI_STATUS
% O8 B$ [6 {) [: {EFIAPI
9 `" v- j: ?: P6 V7 s- m( B# LMyfileioMain (IN EFI_HANDLE ImageHandle,) i. B8 |% @. o- G- o3 K. ^
IN EFI_SYSTEM_TABLE *SystemTable)
" R% M& C, O( D. S: a- E/ z{
$ V9 G" e" S5 W EFI_STATUS Status;
, N4 @" z4 A$ J5 V EFI_HANDLE *DestAddr;
; q* h/ m; u9 v4 J. _- | EFI_LOADED_IMAGE *FileHandle01;
) d! H3 }. }$ q1 q EFI_DEVICE_PATH *FileHandle02;
. ? T0 u; G8 C EFI_FILE_IO_INTERFACE *FileHandle03;
- U- w9 T# h3 B/ S$ e EFI_FILE *FileHandle04;
" i+ G4 w3 Q5 L- k EFI_FILE *FileHandle05;
L5 R; F6 ]" y8 f. g CHAR16 *FileName;
! K4 ~0 [2 {) |; U9 \. E CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;5 I( e X! m- b1 S9 u7 @
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
+ C! V4 B. _, N6 | int Bit0, Bit1, Key_Space; 8 E5 x8 u X8 [( o+ g
u6 u0 c8 Q; y FileName = L"NewFile.txt";
( ~' j4 V# G. w6 C( k Space_Key = L" ";
2 w `- y) D6 v) y" \4 X b: c1 ~
% A( d& T, c8 c4 l9 w3 N/ ]4 d" v5 G BufferA = L"ABCD";
: r, f7 j, P$ @% R BufferB = L"EFGH";
. a+ j# w4 X: V2 ]9 H* o BufferC = L"IJKL";6 a( I& w( l5 `' s& T! ?0 _3 M
5 F! l: C; d3 V2 C
Bit0 = 0xff;4 Q6 L6 g8 t3 E( F' A$ Z
Bit1 = 0xfe;
, j* S6 i6 K! k. b) F
, e) c- A. S' j, q Key_Space = 0x0020;
6 M0 {; m6 l( r5 s9 D* i : ], S/ X3 \$ L, n5 p! ~
InitializeLib (ImageHandle, SystemTable);
2 ]+ q7 n. O) g9 J: Y+ d4 P& q7 w: i: x* R0 K9 p0 I" E
DestAddr = AllocatePool (BufferSize);
( t Q6 k9 ?2 T2 J9 Y. n% w, S- y. V" J2 z2 r
Print (L"Value of Bit0 is %x\n", Bit0);
' b: V- o9 U z/ w' W Print (L"Value of Bit1 is %x\n", Bit1); " R' F& K' u& \- ]& ]6 p
: b3 I9 q! n+ v5 L" I) l
, t0 r) D4 S/ C+ S) H Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);' A( V" S7 a2 H1 H8 _4 l
if (EFI_ERROR(Status)) {
# [9 I! \( J/ a; D Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
" k0 }, Q" [( P% ^# p9 p return EFI_LOAD_ERROR; Y# U. x o8 D# B# P4 b6 G
}4 Z' c1 u$ }& X" P( c- V1 b8 U+ ?
9 B/ a" a7 y- X) v! T
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);# }+ c+ ~2 ~+ P. B2 q% [( Z
if (EFI_ERROR(Status)) {+ o, O) X8 J4 E- H' K8 F
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);5 R" ^* j) H/ x2 U7 H. c
return EFI_LOAD_ERROR;3 V' p3 M N4 d# H* \9 {
} ' i( P. x- _, m) A# o# A( H
- X. v6 V, \" \6 ]% b0 x Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);8 a* O& Z6 _) ]' p- y
if (EFI_ERROR(Status)) {
8 J. `7 A8 F- u$ O8 _ Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);+ {6 g# p# [& N7 S
return EFI_LOAD_ERROR;
: W: _! D' r/ c% }4 V6 v9 m$ A }
% |3 x9 J! a% ~. z! h
+ S! F, f) C/ d! m: } Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
+ C8 p8 P: |9 d: b2 k& y0 B) F/ V if (EFI_ERROR(Status)) {
/ z$ I4 {* R* \/ t: O Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);0 l Y, f2 c# `- n1 {4 |
return EFI_LOAD_ERROR;: S9 K' J" a/ }/ Y4 _
}
' Y" l/ i4 h5 @
' C# q: c& l( X. F9 ?7 F Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);! z; ~* F+ [# f/ M% x
if (EFI_ERROR(Status)) {
" q* q2 f6 \6 t) g; p9 o Print (L"Could not open file, FileHandle05 err code is %x\n",Status);( c) z- }' z% U& D1 z
return EFI_LOAD_ERROR;, g, x4 Z+ v$ I9 s V
}" j1 x% r: A: w' X; x
6 ~" S; ~2 \ n0 `0 v
Status = FileHandle05->SetPosition(FileHandle05, 0);
# L9 O+ Q- t" G. s if (EFI_ERROR(Status)) {
6 ^/ n) l2 j: D0 \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
% _1 z- E( P+ z) w( Y6 w8 r return EFI_SUCCESS;7 a e3 t ^ k$ c, w: ^; t9 v
}, A! T& X C1 a# V! R
/ ?( ]' D! K( {0 _7 ?) ~& f Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
& O% w( n0 `- w4 A; p$ l Print (L"File Buffersize is %x\n\n", BufferSize);( G9 }$ m* Y3 l$ q" @6 `( a! x
if (EFI_ERROR(Status)) {
: s4 l" }' t6 b/ C Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);( z# g' p1 x, q$ s
return EFI_SUCCESS;
+ m0 r- ~' a5 a7 e* C- b: L } % y% a5 F! y1 Z% ~) W
7 E: F6 U% o5 K2 p) } Status = FileHandle05->SetPosition(FileHandle05, 1);
3 n, P( b$ {: ~2 R. }7 T8 d if (EFI_ERROR(Status)) {
# {+ @8 i- ?0 q' R1 L. D Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; J$ v% M. o) q
return EFI_SUCCESS;3 {, h' W) p; Z
}
. R2 g6 ?3 {; ^; d9 V7 ?) y
0 U( x' F0 ~5 s$ _% _% j# T8 e Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# u& |* a! ~$ C6 {8 L& b Print (L"File Buffersize is %x\n\n", BufferSize);2 ^% a' S8 U) x! S
if (EFI_ERROR(Status)) {2 I ~+ Q* o# W$ \! k4 I& }
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
" m. \# V" X& z* F return EFI_SUCCESS;
7 m2 @0 t/ b& c0 \1 V }
* r$ D2 z' ^, V" y ' w% W7 o5 \) W( h6 q+ |- I% L
Status = FileHandle05->SetPosition(FileHandle05, 2);
- `% W( l5 e- b W if (EFI_ERROR(Status)) {6 ^' @% D3 M. W- U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ y6 _7 X6 t, y" ~ return EFI_SUCCESS;
! v5 T' R8 x2 [) X V+ T7 E }
+ `9 }, H/ G* r9 v6 O! `
$ ~9 V- @1 X: Y1 @ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);9 v/ R' Q" t% x+ m" v; p' g
Print (L"File Buffersize is %x\n\n", BufferSize);
0 d* L, R9 v3 U E( Q: W/ J- X% G if (EFI_ERROR(Status)) {) U9 u$ u3 s/ \5 x9 g
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 l' o% i, N2 Z- f# Y, [ return EFI_SUCCESS;
6 _; z. A# `) ?& |* ?8 N }
. @+ {5 H) b' B( u
3 _) o) E8 q/ E# g8 F7 w$ k//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>& h, b9 {) {' Y
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);3 F- K1 ]% F* p, t+ i7 I& E; d
Status = FileHandle05->SetPosition(FileHandle05, 2);
* g4 w z+ Y3 l& p% q if (EFI_ERROR(Status)) {7 i) J+ U- d0 ^& ^4 C2 ^8 R o# d4 U4 |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 {' n: P( o' v/ [' x
return EFI_SUCCESS;6 w- V6 A5 M3 }1 G
}$ d( W$ W# a* @, l$ b: e7 c
3 ~3 M( s3 I) u# q& _ Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);, ^7 ]- p7 Q2 w) S) y0 [6 F' ^
if (EFI_ERROR(Status)) {7 U" H1 ? Q3 m6 X j/ q2 {8 q
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);! b: Y1 s7 y8 V; _0 e% B
return EFI_SUCCESS;
5 K; D$ B7 k- S/ A3 E& K1 G }
S# m: B) O/ v& A Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);0 J# `7 o e5 z- K6 \# s
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<6 F; z, [" _: f* i
+ A1 @" w9 Y- S( m/ B) }
Status = FileHandle05->SetPosition(FileHandle05, 10);
4 a0 o: a: m! U% F- f+ F if (EFI_ERROR(Status)) {
) t$ I2 T/ I6 d, x7 } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ @3 q' u) q" L# z9 b, O
return EFI_SUCCESS;" m, A j |* L- G0 D6 T2 t. z
}
5 N! \" D% t; r/ X
" n, ~+ M* l2 R, N1 `: l. }8 @ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! f W/ z1 D# m/ ]0 m9 y! t2 @1 ?
if (EFI_ERROR(Status)) {
9 K- D3 U2 `- z& `2 A Print (L"Could not write file, FileHandle05 err code is %x\n",Status);8 q/ H. Y3 P% l2 Y! {( ?
return EFI_SUCCESS;
- h5 @+ s- Y6 N2 i, w$ h( [ }, u8 N# _* Q7 V. v: ^. [
2 q" q+ V* U' z4 U Status = FileHandle05->SetPosition(FileHandle05, 12);
& t$ n) l) K: d; p; h, g if (EFI_ERROR(Status)) {0 P+ I8 J! p7 o) ^# n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" o& k7 x! x9 y/ @0 v return EFI_SUCCESS; H2 Y! D9 Q% s4 A; Y8 ~- e- i/ a3 J
}
' O% [# Z! J; W' R+ m' R( D ) `7 F L' L: g# G
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 d# v! l' t3 E if (EFI_ERROR(Status)) {2 O. {) ~1 u4 X* S; G1 ~
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) O" g Y p ]) z5 R/ r: M return EFI_SUCCESS;
0 P7 V x2 l/ N! }) K- I8 N }
# m$ G2 \4 b# b. h j! M' I: ~, c, A# ]* L- K8 X* H3 r
Status = FileHandle05->SetPosition(FileHandle05, 14);
* H$ c7 }! b: B \! u6 H } if (EFI_ERROR(Status)) {
% p* L' B ?' ]: o1 L; ?1 ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ S- Y7 U9 S+ ^5 Y! Q/ E: k5 g0 W
return EFI_SUCCESS;& e" ^3 [$ R4 f7 l. i
}
( v0 _$ C) F4 F4 G( t8 M3 G
) F# F+ y' n4 E! ~6 @5 @ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. ?; G, n2 x: e# J$ m/ }
if (EFI_ERROR(Status)) {
; I/ E. Z: m p% k Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ t4 X& Y' Z$ ^8 M
return EFI_SUCCESS;! o, T* Q% P" l5 l6 z- n7 L$ N
}/ `1 {8 w- B6 h) O6 Z, u# R& i
* R8 \3 w+ \6 p, { Status = FileHandle05->SetPosition(FileHandle05, 16);
3 {5 w' S5 m4 H: _ if (EFI_ERROR(Status)) {
& r% {! a5 P& f* t4 U- g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; m! V) K1 x/ b- i4 d* N
return EFI_SUCCESS;9 @+ V- n, C8 c$ _0 E
}
z3 i7 ^" m3 I! d" a. M) U
2 Q" v0 J9 W/ x2 @) p3 o! N/ g Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ X9 y: U' g- F6 K) _: U4 ^" Z if (EFI_ERROR(Status)) {7 ^8 x: {' H7 L' |) X$ o
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- A1 Z4 C7 ~( @" i3 J# E return EFI_SUCCESS;9 `2 @- k' M( z9 t
} & X1 Y9 z& p9 W2 q. z
! t& w+ [5 t3 f
//---------------------------------------------------------------------------------------------------------------------------------------------------------------2 ?2 W% H8 I0 @; e; o1 @: u
9 O* Q( @1 F$ g, S7 w, P
Status = FileHandle05->SetPosition(FileHandle05, 18); + ~2 l# x; q+ D5 Q2 I0 j1 W
if (EFI_ERROR(Status)) {
6 T0 ]; ]# g8 @; l4 X3 ]) a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( {+ J+ {4 r7 v1 k5 d. B' r8 | return EFI_SUCCESS;
: Q, I: S& z1 ^( m) r' J: ~ }
# Q2 W2 _, N8 k' [- F! m* t. I
1 A" S; m' N0 L- ]. Z$ ^ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
+ Y1 Z3 Y3 O3 x& ]5 T if (EFI_ERROR(Status)) {
* O! A6 Z! A' X5 r Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! \0 y# T& Q7 ?0 Q) w
return EFI_SUCCESS;
( k/ u1 G. F+ g) V. k% M. T' t1 W }
- w3 ~# [3 Z2 l, z
( \, D6 Q& ?" h+ M FreePool(DestAddr);
$ |' e2 l2 j" T7 `$ Q3 p+ ~4 \' e5 R! e
Status = FileHandle05->Close(FileHandle05);% o/ E0 n# V& K# P5 R8 n
if (EFI_ERROR(Status)) {$ G1 v4 e% T! q6 q' R
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
( M b- W9 _. y& r2 d9 H# F% p return EFI_SUCCESS;
, A% X, @: l3 T! t8 n( B; Q }/ Z' b' G- F1 J$ t; Z( S6 ]6 ?
" y$ \; U% l$ y4 Z
Print(L"File Name = %s has already been created.\n", FileName);
: ?4 Q5 U r9 w4 o) r8 B: Z0 |0 q7 d# _+ C6 } Z2 F
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");* y# c0 P4 }2 Z e: S6 i$ `& A! L
WaitForKeyOrReset();5 P, W" ]9 M& x4 E) N8 a' B
# r2 u. z- h6 j return Status;
2 C& ]! t& _" G' t1 A1 Q} |
|