|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
& ]( v7 n2 H/ Q7 ~ * myfileio.c! n# ~% y5 l, k. s, F5 D* e* r
* Apps
( y, f' {7 t: ?4 q/ w( [9 W */
( y. T7 _+ d6 ]1 y4 k2 E) Y- r3 T5 l$ o6 P
#include "efi.h"1 }. C2 z$ S1 t2 O" o: |1 [
#include "efilib.h"; i2 w* Y" ~7 r
+ n8 a% s- W0 Y9 G) X
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE( f& d. b" s" w3 B
r* ]" C1 A0 Y% e
static EFI_STATUS WaitForKeyOrReset(VOID)1 a! I3 s, \ L
{
; }9 S6 l# H2 q0 O EFI_STATUS Status;
. X2 u3 r7 Y$ ]! w% s) d' B% a EFI_INPUT_KEY key;/ v2 B2 t9 u+ p }; @1 l
UINTN index;
* M7 E6 D) {$ ^% d1 c * D6 c7 v) d' U$ r1 n- |
for(;;) {& c" E9 q' i" d5 y1 b
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);' D( s5 p8 c6 q9 l
if (Status == EFI_NOT_READY)
" |( o+ f: y- o$ Z8 z BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index); X0 e/ x" x' o6 C, d9 J" f
else
, ~# g3 _3 o2 i2 M7 P+ g break;* F0 b9 n0 w! s* K$ t
}
, @/ l- [/ t6 v, q' F if (!EFI_ERROR(Status)) {
' T1 M! F j0 e5 J if (key.ScanCode == SCAN_ESC)% B: t: K8 L% q' j# N
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
( q) n; ~0 f, ?6 V8 _ }& j% f$ J* a: i) ^' m
+ q% Y$ X+ M3 P6 B return Status;/ M" e) n7 J. F# N5 `$ m$ L
}1 F' L" q5 E7 x! n
$ r! ^2 z; T1 p4 E7 W: [# C% [EFI_STATUS
& Y, g' m1 ^3 ~3 P2 S( W" e! zEFIAPI6 W+ g! c4 T! @3 U2 o B
MyfileioMain (IN EFI_HANDLE ImageHandle,6 H* B9 d1 y5 I
IN EFI_SYSTEM_TABLE *SystemTable)
; _- G' A% w2 I, K. c8 I0 f* J2 P{4 E. p0 p8 M. X6 K) ^" x
EFI_STATUS Status;
; i* `7 v7 M, T! K* U5 [, \5 E. f EFI_HANDLE *DestAddr;
" w4 H0 L8 {' K. u EFI_LOADED_IMAGE *FileHandle01;
' i5 s9 O$ p( X& K' ^& d& d EFI_DEVICE_PATH *FileHandle02;
9 ]9 n Z# q9 ?. d( M$ k EFI_FILE_IO_INTERFACE *FileHandle03;; d" r4 w) W4 E* S# y3 h" {
EFI_FILE *FileHandle04;
9 E7 n3 L5 {) s# \ EFI_FILE *FileHandle05;
) f6 p% a. a# f' A l CHAR16 *FileName;9 a# ]0 N: X$ n0 K# _% m
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;$ h0 A1 O. \, Y: R8 e" J+ a
UINTN BufferSize = 8, BufferSizeKeySpace = 2; f9 B9 F/ Z6 J1 E, ^" i
int Bit0, Bit1, Key_Space; & q7 J( G. N6 B& ]+ |4 q
7 b( x3 d% |6 e0 g FileName = L"NewFile.txt";
$ f x" F1 H# t, Z9 V( C% R Space_Key = L" ";
! K$ N# }9 f* `- R$ ?* E, d
% b/ {+ [* |8 O3 J! |) Y8 w BufferA = L"ABCD";' x; n# E2 x1 I- _0 @& e
BufferB = L"EFGH";
& p: x ~9 K& P, @ BufferC = L"IJKL";5 A7 R) ]1 c* i) u Z. N6 \
- i+ d. o2 S w, @ v* `/ \# p6 D
Bit0 = 0xff;
- P' j5 o* h0 ? Bit1 = 0xfe; `( m# i+ d1 U1 u. d+ a
/ C# D; Z4 Y& Q# i$ A/ Z
Key_Space = 0x0020;8 w- x1 @1 O; A+ Z( T$ l
6 u1 g! F, ^; J, D2 P" |) P/ S InitializeLib (ImageHandle, SystemTable); 7 J L. U* n: k& O- ~
5 N, @6 Y( \& ?. p DestAddr = AllocatePool (BufferSize); ( _' E& V6 |( G& A
# T1 M1 D7 ~( k% x4 V$ Q% |
Print (L"Value of Bit0 is %x\n", Bit0);
9 N9 d/ p- F/ H( Z Print (L"Value of Bit1 is %x\n", Bit1); . _9 T( o& Y' f
- p) a! r5 P7 t) P% U" H5 [. e" t+ A* |
2 s" ^( d) ~( R2 ~% y
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
/ a) C) M1 s9 i5 B if (EFI_ERROR(Status)) {
g- U# H6 U0 I k Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);% E) F# ~$ s$ c' \( P7 p$ e, |) }
return EFI_LOAD_ERROR;. x% z9 k8 f& P/ i
}( p F, r! S/ e2 V
- O; u6 c2 u# U U9 _9 @* } Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);6 }3 L) m5 |/ I, e( L; J( v- F
if (EFI_ERROR(Status)) {
' }( d; a0 c: u' }2 x$ e Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
$ ~, @, j/ r) Y6 k. [/ `0 e- x return EFI_LOAD_ERROR;
X" d' ~/ c e* o2 l o }
$ J7 ^7 w8 ?+ p5 }
: O3 T8 H8 `! ` Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
" }+ @- N. `/ v if (EFI_ERROR(Status)) {- I4 M7 Z0 g9 j. h
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);- \( v$ ]$ Z0 o+ U/ G" {: M
return EFI_LOAD_ERROR;3 w8 y2 L% E) ^. u
}
$ X( ~7 W/ f& M( N9 C
. k% n, l. z; O! {, |' L: u% [$ f Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);& W3 o" q& P Q
if (EFI_ERROR(Status)) {
$ m/ l4 b' o1 }* k: }. o$ @ y( ^ Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
# D6 H8 o" w; { return EFI_LOAD_ERROR;6 H- Z& T& z) O1 ^9 w; f
}
+ Q- } |8 Y d# R" U; o8 `9 X
0 @8 @0 d2 G2 }, D1 V1 t7 E ?- G Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
# w" w" K( e; k& L8 l! R& ] if (EFI_ERROR(Status)) {
+ F# J0 L: z* @- R3 N. o Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
0 u( p' }: T" x return EFI_LOAD_ERROR;
/ U" _& r8 m3 E5 o }/ Q; H9 a7 N7 m6 t
( O4 ?8 w! G& E1 W" O8 I( S7 X
Status = FileHandle05->SetPosition(FileHandle05, 0); 9 q& f: Z$ O5 Z/ j
if (EFI_ERROR(Status)) {# x8 o7 c; |, j9 \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ v) L( u5 _2 l* ]; I
return EFI_SUCCESS;
0 J% s2 F0 K& j8 v5 F }% ]& n: {0 J/ k3 d% ^8 {, ]" W
3 O. ^" W& q( @! p- d. ]$ o! K Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);3 _& \' [' n! a. @; C$ K( Y0 j
Print (L"File Buffersize is %x\n\n", BufferSize);& s8 J0 k) j$ k- f& W
if (EFI_ERROR(Status)) {
' ]- P) A. c/ Z9 v5 Y Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, a+ t# S1 \1 G1 Y# f4 n
return EFI_SUCCESS;0 d) j6 W1 Y4 x1 q5 Q* }
}
* e; o/ ~3 C6 i/ _% W! l9 t) W" V! [7 o$ Y( V, S
Status = FileHandle05->SetPosition(FileHandle05, 1);
" c) @8 J z9 a3 S if (EFI_ERROR(Status)) {) \# ]7 K4 c# n) b) I% M/ x
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, |2 M5 {" n; _0 ]2 i% I0 z$ F return EFI_SUCCESS;
" c7 P; q# [3 v/ F }
; ^! H# S8 Q- D4 u, q, k" q$ V ( ~0 [% M$ Q s, K4 h
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);7 G" k# q! Y1 m( |
Print (L"File Buffersize is %x\n\n", BufferSize);& `! _$ N; Z6 D- U
if (EFI_ERROR(Status)) {
# i3 I6 J2 r; u Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 V! u3 Q. Y; e7 s) Q return EFI_SUCCESS;# c( }+ d* N) w/ d. z
}
8 v9 p" d% p" K0 g$ W2 \ % D6 |! E% a, V2 ]4 }# ?7 Q3 I4 b
Status = FileHandle05->SetPosition(FileHandle05, 2); 0 K7 @' B( w5 \8 Z
if (EFI_ERROR(Status)) {7 g1 e0 v z$ `
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' d J9 s* L0 {2 A( a
return EFI_SUCCESS;
4 `" u2 Y! Z- W0 i. X }
q# ?5 U! k& j. U4 p * Z0 l3 z, h$ ?2 ?; s
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
3 p* v9 S$ q6 [! T% W. S9 [. \8 o Print (L"File Buffersize is %x\n\n", BufferSize);
1 M$ J3 g9 k$ d0 R if (EFI_ERROR(Status)) {
6 }& O: k# K8 j0 G/ N K' H Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ L& N* U& E% U. n3 K, X
return EFI_SUCCESS;8 [! [% w3 N' |$ m* p
} 5 _- ~, V; \# R8 j
0 T; w* L6 U& V" a0 T9 G- m9 A
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>># s7 W' ~! f, h- a7 Y: Q7 J2 m
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
+ G# _1 U3 B9 ~9 Q- i Status = FileHandle05->SetPosition(FileHandle05, 2); $ ^0 I6 j( W, f& g: c
if (EFI_ERROR(Status)) {' O2 s" T4 P6 T! h
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; u$ p6 c9 e% S* q return EFI_SUCCESS;
% _7 r2 V3 K$ P6 P9 } }2 Q! h. Q9 P3 V
8 i8 H& H; z1 `$ ~" [% A) P9 x
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);4 X5 {& q# f" T1 W V) u
if (EFI_ERROR(Status)) {7 t% Q2 x+ w% b- J9 [ T; Z% R F% M
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
1 t5 J6 Y+ N9 Q; w2 s return EFI_SUCCESS;
0 M$ f9 F" j& q, b) G } - f- u2 i- f# u1 }& m2 Q3 r
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);1 E1 ^8 A, H: g& r3 Q, p
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<5 S3 B# e S0 ]4 d
/ E0 i) s4 F6 b% S- f
Status = FileHandle05->SetPosition(FileHandle05, 10);
7 q d) f3 D u; @' P if (EFI_ERROR(Status)) {
+ g5 Q2 g2 D# X( g6 y% { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 n+ N& a8 u; t! x, ]& k2 ^0 Z return EFI_SUCCESS;7 e; \5 ]" H/ ?
}
0 i( @. X0 T5 i: Q, w ) t9 o8 t' g3 H' N A0 u2 v
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);8 t8 |' t* V, N! w
if (EFI_ERROR(Status)) { E2 K5 l0 F& P, |5 j
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ s" b2 \3 J, E$ g% C/ A& Q* N" T3 t return EFI_SUCCESS;
. V9 H! G& O5 z& G c }
. P, {. P7 y0 z/ n/ F/ O# ~ I! X. q
Status = FileHandle05->SetPosition(FileHandle05, 12); ! F$ I0 o( H9 Q
if (EFI_ERROR(Status)) {9 Z1 {) L; W# V, _" n/ W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! e+ F/ F& M ^/ ?5 D; C
return EFI_SUCCESS;
; l7 w O" c# B; A T }$ k" u" ^) L: }, J
! g% a" P0 W3 E6 S9 i* w
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
8 R/ M4 K6 L( x1 f$ O if (EFI_ERROR(Status)) {
1 N* m4 \' m8 G& ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 d' C3 \5 W3 F; y return EFI_SUCCESS;0 K; n0 T: Z5 `6 N" |9 t
}
3 V7 A0 t5 n( y9 m- S& F! X' z& [5 o; @; O9 ~2 X2 w
Status = FileHandle05->SetPosition(FileHandle05, 14); ' u$ K5 n+ U+ Z
if (EFI_ERROR(Status)) {
! O: W* X+ G- c U# a8 Y: i) y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- c* X- r& g1 v, ]1 h# v3 o
return EFI_SUCCESS;& c2 p1 |9 l+ |6 }: S2 N6 e: l c
}: p7 V6 [$ \! t* j" t' c7 W
4 b1 c7 c$ h; R" D Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 I8 @* I, n$ _ if (EFI_ERROR(Status)) {# m* k3 P3 j' r* x7 f$ V2 D
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. ~+ Z8 d& G* J/ \
return EFI_SUCCESS;
% |& a1 t2 P6 M4 L _# K! w }. j! i" D7 S+ U- [. {- j
1 O( e2 Y# i- {8 {: ?$ D4 G Status = FileHandle05->SetPosition(FileHandle05, 16); / A3 Z3 P0 r7 E% d" h [2 @! g4 p: K
if (EFI_ERROR(Status)) {
0 W% o. s( t, a8 } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( F. I. w+ [' L8 R return EFI_SUCCESS;
7 a" ^. L1 W+ @2 H2 Y6 O }
1 b. q \. Y, f2 v
; C( h; ?7 ^& j4 ^: W/ t5 U: ~; O8 { Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);) O) s. m' X2 _- k0 w/ ~4 @
if (EFI_ERROR(Status)) {
# y8 M1 m9 ~; z5 s/ { Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
K3 y7 m9 x% g8 u& R0 A' c$ [ return EFI_SUCCESS;
$ M# H) _* x; j) ]/ x1 i( i- v- q } / |7 m) A$ x+ S T! b+ Z
, b7 ]2 p7 Y' |+ D, q//---------------------------------------------------------------------------------------------------------------------------------------------------------------
2 q- z$ s1 i. l
) e0 t1 s V0 c9 X/ Q0 p7 E Status = FileHandle05->SetPosition(FileHandle05, 18);
+ a/ N" Z$ E9 I if (EFI_ERROR(Status)) {- Q, w4 M9 z- U" H, F. @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 c/ i! h. T H
return EFI_SUCCESS;/ h5 Y3 f1 Q& ~- O9 v2 _9 J
}5 ~5 \5 Q$ y8 C: T: _, d/ L2 S
1 Y0 F. t' D( a- E+ _3 a Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
5 o2 j3 r& a6 ]2 s( v7 b4 w# I if (EFI_ERROR(Status)) {9 H' _8 f. J' J
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);# F) j( s% X$ Z% E
return EFI_SUCCESS;% ?9 L! g C% e3 c& s
} ; ^, H. O% e, h# r( Y. |% k/ k
+ }8 q) u3 ^# t2 x, u& T
FreePool(DestAddr); # J8 V! _$ }. ^2 |! c5 m
2 P* p2 G6 `- ], e+ N Status = FileHandle05->Close(FileHandle05);
9 k+ Q6 q: v: Y% O( }2 f+ \ if (EFI_ERROR(Status)) {2 I0 J- O2 B& P5 \) u
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
# W, t. E" B9 j$ k- c! w( U return EFI_SUCCESS; @& ~7 |: h. U" M g
}' D, H# o- B/ J, o) o9 j5 I
& u2 o4 ?" g4 ]1 ]' T Print(L"File Name = %s has already been created.\n", FileName);( l* h# a& n- o0 C
- B6 H/ x* V7 E0 A6 ]' o; }
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");2 E; |. W9 V. T, q: `
WaitForKeyOrReset(); l7 N) v4 D, J# S5 w8 J
2 x7 c! A+ P" d7 G7 r return Status;; `, E% ?' B6 c3 a4 Z
} |
|