|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
; r- _" e0 ]8 `( c * myfileio.c/ C( w! X' E J8 D6 X. B; p# L
* Apps4 |7 H. s4 g1 L, s' Q) h3 a2 z. E, @
*/
6 X* r" i) u g5 i9 |9 [
) ^' J! D: e4 p+ g#include "efi.h"
+ E; r: _2 ], D#include "efilib.h". [, w7 t6 D) ]8 c4 J) V
/ N- [! e4 B( k2 P9 h+ E#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
' |4 K2 S. E/ Q: t5 y% |
( _* }/ n" ?" w+ R. rstatic EFI_STATUS WaitForKeyOrReset(VOID)8 ^9 p# ~7 H1 ?, l, ]
{
( l @1 S; h$ p* M( E EFI_STATUS Status;# h) B! T/ S0 }8 v+ h6 L7 X
EFI_INPUT_KEY key;* [& V( W5 M! ], a/ b
UINTN index;$ v c) O* _# ^& I# H8 d% [) _
: t) N# r. \7 E, G" } Z9 C for(;;) {$ `. H1 A/ m, V6 \. t' K
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
" M' S V; ?! j1 j if (Status == EFI_NOT_READY)
; J# }/ L: {6 S6 \' m BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);1 H j1 ~- A8 l% v
else; _2 }2 k9 V( ?) m4 G5 b" F
break;
. X2 D' ~: s: C$ h }/ k/ K" E+ Y: @4 d6 {$ m/ B
if (!EFI_ERROR(Status)) {: m- l: R: E% i& M
if (key.ScanCode == SCAN_ESC)
; X8 q+ n( }' o; I) n/ R4 ` RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
( G# b) v! {2 \- ^ }3 `/ ~- C& k, v
/ z# T9 \- A; a, Z$ Q
return Status;
$ k+ S2 @/ L9 \ z3 }}" i6 y/ S# I6 K/ j! d
4 h4 _/ K$ ]8 Z! G; }
EFI_STATUS1 Y2 @8 W, z; x, G8 f. t9 E) T
EFIAPI4 G! s2 r5 v2 z* q, f
MyfileioMain (IN EFI_HANDLE ImageHandle,
% x, l7 J4 H" R! `( Z6 u* O IN EFI_SYSTEM_TABLE *SystemTable)
, T! D+ d- J/ k; b: H; z1 X9 ^9 C{& D% s# H- k! u0 u/ D5 W, E
EFI_STATUS Status;
* [4 T6 g; y5 | EFI_HANDLE *DestAddr; & W% s: F) R5 p8 p& `
EFI_LOADED_IMAGE *FileHandle01;
6 t. a5 A, ?4 y* T5 l EFI_DEVICE_PATH *FileHandle02;
1 d; m" \' g3 T( ?8 t EFI_FILE_IO_INTERFACE *FileHandle03;( B. @ x" n- _5 r# b# l4 H4 S
EFI_FILE *FileHandle04;
" d) T# j) ~, {) W- S. s EFI_FILE *FileHandle05;
7 z; m! k2 c5 ]! q# l3 G' m CHAR16 *FileName;/ g$ c0 k6 M7 k3 |6 ]. ?
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;6 ^( N2 @- h8 J5 ]8 i- v6 M
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
$ Q5 g) F7 B6 p5 W8 j4 F int Bit0, Bit1, Key_Space; # r8 _7 X0 v) x8 Q& t3 w% J, P7 }
% L5 Z9 ^3 U& S+ J% r2 D4 ^# R2 F0 O
FileName = L"NewFile.txt";
. i' _% j2 M; y7 f- ^! h {4 \$ z Space_Key = L" ";' p( P5 O X9 L6 s
8 K( V& ]' m) J( L( Z" Z1 _
BufferA = L"ABCD";
# M @# ^+ X8 i3 { BufferB = L"EFGH";
. N; X3 E$ P8 c! _6 d( F BufferC = L"IJKL";# x9 O" E' E0 G- c1 V$ U
! x. |# D/ _3 m7 e! A8 J W Bit0 = 0xff;
7 X) O4 ^& M! r" c) x Bit1 = 0xfe;: r- E' g' U2 q. V4 T3 @1 g
+ T: p- \! `! g8 w+ A& q' R! Q Key_Space = 0x0020;
# b8 \/ o6 y2 F! G8 M. N
7 p4 Q {% B2 ^, J* { InitializeLib (ImageHandle, SystemTable); 8 q- h% Y# T! C
$ I4 ~; d; |7 e5 r DestAddr = AllocatePool (BufferSize); ; y/ E' n% ^. `! f Z. g s1 U
3 `$ l/ K. a% r* P( }( w, W Print (L"Value of Bit0 is %x\n", Bit0);
' Y' Q& d" t$ ], p" M! B; ] Print (L"Value of Bit1 is %x\n", Bit1); 4 H8 K! w2 ?* i6 ?8 ^: M, v( Y; L
8 e! m- [+ w, d/ {0 M! U 5 F0 T# g& _* y# |5 T
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);$ c. }1 T# l1 |5 ^. }* y
if (EFI_ERROR(Status)) {
) g& N8 _. m6 E. ^ Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);7 r$ y( o9 L. v! s
return EFI_LOAD_ERROR;
; v# \" U0 ~4 v; j }
& {0 e+ n/ _' b3 m
; w! x+ t% |# \% B$ l Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);& i1 n/ ~* W* p: _: O1 z2 s
if (EFI_ERROR(Status)) {
* b/ U5 u+ V# S( a& h v) N/ x Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
1 B6 D- e( d9 |" w6 Z return EFI_LOAD_ERROR;0 o3 ]% N! c9 Y# R
} : A# j5 Q; h) Q( k f) J
4 a; F/ ~2 i0 `: R Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
+ F7 H3 o$ Z# Q6 G! q( {& F if (EFI_ERROR(Status)) {5 u6 t+ f# t6 F- T5 D. X
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);4 D% B# t( d) K
return EFI_LOAD_ERROR;5 q& R1 i" c% w1 h+ K6 O7 L; L7 ^
}: b+ F, V7 O2 T
h. ^/ h3 O/ X/ O3 p: d; l2 l; [
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
& R& {- s. ^! d$ ] if (EFI_ERROR(Status)) {5 x/ i" x- K7 X$ \- L
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
+ s8 @' @3 p( v' a. t return EFI_LOAD_ERROR;
; ], d7 x0 N* c: N4 c$ U } / L& J4 J* e4 C
7 o- w( L* k U
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
/ R( n w5 ~! V4 ^+ O" t' O if (EFI_ERROR(Status)) {3 c4 y; L; t. m* s
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
6 w; ? J# y6 \6 W/ z return EFI_LOAD_ERROR;
; c) f, L' i- E/ i0 \( d }
- ?$ E. G# D. x5 \& a : n- }+ F Z( y
Status = FileHandle05->SetPosition(FileHandle05, 0); 1 ~8 ^) B/ g" _
if (EFI_ERROR(Status)) {
7 k) u( ~7 e6 h k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
K4 C. X# [5 h; y' G, b return EFI_SUCCESS;0 @, c* ]; o, d/ M
}
7 \" `. Q b1 R( ^: E* ]* f. H0 x& S 8 w& p' ^. M. e& E
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
% a& ?# q/ m4 b, W8 s0 S Print (L"File Buffersize is %x\n\n", BufferSize);
. L4 k _9 O1 l; `& O if (EFI_ERROR(Status)) {
/ Y: \, O0 K$ m" u5 { Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);0 R' _7 s! g: H, p$ G" F
return EFI_SUCCESS;
6 M, o, x* w" |2 y7 _ } 8 w7 v- b1 n" q% s
/ K9 R/ m* `6 A, _ Status = FileHandle05->SetPosition(FileHandle05, 1);
, o& Z$ ?1 f, V4 i4 T if (EFI_ERROR(Status)) {
2 w& \. z# D/ G: I& y2 Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 @; F, W/ s" e. M& F8 \- I. M return EFI_SUCCESS;* R; |. R& L. }+ f4 o l
}
7 g1 [& S) _( X( |+ b+ f: o9 t) k 4 z2 T% g! B+ t( D
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);. d" J9 K& e8 l( D4 Y0 O" f0 j
Print (L"File Buffersize is %x\n\n", BufferSize);
) k$ T. L7 Z J7 ?( J$ N if (EFI_ERROR(Status)) { k E: [5 T# j4 F
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& y* x: q2 [! b/ ~( p return EFI_SUCCESS;
3 K; y, x$ H: \+ l/ \) \ }
3 s! Y. P' J8 D2 a' _& M
* ]! F0 A. h$ w- `5 X$ u Status = FileHandle05->SetPosition(FileHandle05, 2);
, c2 |" v P" @; M if (EFI_ERROR(Status)) {$ |& S1 |& z0 M
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& n/ x' H8 u+ y* m6 ]0 p
return EFI_SUCCESS;0 L& ?" l/ j5 \ Y9 Q/ r0 ~
}1 _5 `; Y7 d+ f! q; ^8 f& I, Y9 X
7 g- v- ^9 X, |1 p- J- u7 @1 ^* E
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);5 ^6 w! @2 N) _5 h. L5 w
Print (L"File Buffersize is %x\n\n", BufferSize);
8 J1 g1 r* d# J" k$ h if (EFI_ERROR(Status)) {9 M: K$ }! M9 W
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 S& P7 b5 @1 F4 c [4 K. b% k return EFI_SUCCESS;7 n: Q1 f: g7 J6 T& `
} 4 x4 m8 x* k' v7 a/ y
3 ` I1 P' r0 G; l/ C" [6 n( F
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
2 f/ p4 D, u1 Q' y. q Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);9 Z b7 w3 O! B( Y8 ^
Status = FileHandle05->SetPosition(FileHandle05, 2); 1 W. u% F$ s7 ]- j" ]( C
if (EFI_ERROR(Status)) { h! F9 f/ N0 B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' `+ f) `0 G0 u9 V5 h/ g return EFI_SUCCESS;
) s# I! N2 a# q }3 ]; ]2 k! V) C% P- S
0 O9 M6 r) ^2 p# E
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB); e; j, U4 {; ^8 x, @8 P
if (EFI_ERROR(Status)) {
& J2 K: J% e+ `6 W3 Q Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
5 `- B: _5 j8 {* a9 Q% h return EFI_SUCCESS;: ?: T( c: G$ ]8 X: P
} $ b0 X% P: F- i/ n! X) T t! T
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB); P- D" U E% K: z
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
0 k1 t5 ~$ w4 r3 y% c/ `) h+ T' Y$ z
Status = FileHandle05->SetPosition(FileHandle05, 10); - _2 j2 K" h0 w) j
if (EFI_ERROR(Status)) {
! C+ }+ S8 h* L! s) b# @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& v$ c! I% H6 F+ g5 o! o7 g
return EFI_SUCCESS; u( D8 R5 V+ `4 c' ~/ F1 w
}1 g/ ]1 Z7 m# j4 T6 i1 N
' e0 O Z# v9 n" |1 q$ R) U
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% [. k( Y7 o4 m7 Q M5 `" o" B8 g, S if (EFI_ERROR(Status)) {
; [( a- d* _' a( G7 G. L Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 B/ d v y4 Z$ w return EFI_SUCCESS;
; r7 A* w; G: J6 V, k; W- @ }9 J$ N! y7 F+ ~/ ^, u/ N
0 q) ? B K. c6 W% ~( T Status = FileHandle05->SetPosition(FileHandle05, 12); . G# [. }9 {% M
if (EFI_ERROR(Status)) {
7 n0 C! H$ f! s/ z4 y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) e# Y. Y! d: D9 B: f0 U
return EFI_SUCCESS;
( ^) U1 i/ y# O) z2 K }% N' Z' N9 W- u, o' {& [# t$ \1 x
" c( p3 P0 P) O+ d9 r
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
7 p, ~) y! b# x ^: z, G1 I8 ^8 V) ] if (EFI_ERROR(Status)) {
! O5 ^1 @% [0 v$ a7 i& n. A5 f' z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. L' j/ i4 _. Y. C) W0 I- n |4 F
return EFI_SUCCESS;
. x3 }& y, ^5 C6 P$ z }
% Z- s4 w9 R R+ V3 h" n
- t& y4 u1 [# ?; l+ ]: `( {$ R Status = FileHandle05->SetPosition(FileHandle05, 14); 7 F8 \5 _, k" J, o* E
if (EFI_ERROR(Status)) {
& I T$ \: t, }2 K$ [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! o9 e/ ?. q9 M3 H& U
return EFI_SUCCESS;
3 f4 V1 T+ ?& C" D% E9 Z* s2 ~1 S }
* ]) O. M' M& B, P+ j; j# [' }
9 f' q5 `& P3 p- q Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; G z' L% l3 f if (EFI_ERROR(Status)) {/ K1 m1 X' V o- R& L' q! A8 W
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 [6 Q* s% P" z) j j8 I return EFI_SUCCESS;
) }& s0 v7 }+ g" F }5 O/ q& M; N5 K: S7 R
/ d" ]) B3 ?- t3 n, w% {2 c
Status = FileHandle05->SetPosition(FileHandle05, 16); ' c3 i! A8 a9 {8 k6 r" z
if (EFI_ERROR(Status)) {! r, |! s3 G. T) ~! c s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( @6 |: _) M1 Z2 d; x5 q return EFI_SUCCESS;& w' f' F: I- n3 _9 j# _: t' K; J
}
+ ]1 T5 f. m/ y# G2 W m# L 3 ?: v- k# D* \+ r/ I# J4 G
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
B- P' O# `4 y1 {" N- l if (EFI_ERROR(Status)) {9 H) E$ W2 Y& q/ H% y+ w9 Y9 u, ?" v
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);" m* \3 `# g& s; w* d/ B0 D6 P
return EFI_SUCCESS;
# g" F+ K+ H" S, @3 f8 ]6 T$ I& j) [ }
( L E4 T \, r( ]# k3 i. V) a# D9 `4 `$ h# V b: S
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
F; R" k8 M6 n3 `, w% U# U% m1 s M% L9 L; F
Status = FileHandle05->SetPosition(FileHandle05, 18); " s* o# O. m: m
if (EFI_ERROR(Status)) {
8 m% H( P" E+ `* v: d. z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) Y% w/ O6 t, n* n- _5 e
return EFI_SUCCESS;
# h1 |* Z2 K$ r2 K; K) q }, l W" z2 j. z+ I3 P# H
% Y" N! Y* E+ T* \ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);2 ~8 B% x" |8 Z; N1 _6 U
if (EFI_ERROR(Status)) {) C! O2 i: @+ `1 R" C% r) s. v
Print (L"Could not write file, FileHandle05 err code is %x\n",Status); ~" }) F* O& c6 b, L, p7 z; p7 X v
return EFI_SUCCESS;! V+ @; [5 Q* y3 M8 Z& n( x# l
} ! u# V3 G% ^/ D( a' l. |
# a; P: r; ]$ g. _, m k k% y
FreePool(DestAddr); 7 T1 M" a6 U$ M+ N" x3 i& ^8 M
2 N" T, V2 i( R2 ]2 m
Status = FileHandle05->Close(FileHandle05);
& S$ e3 F, S) j9 T( n7 s( z4 r8 T if (EFI_ERROR(Status)) {$ e' D U, L$ G) @3 S
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);0 P6 n, q0 l1 c& [$ `9 x- y
return EFI_SUCCESS;' L+ S h7 t( Q, ]& U4 P
}
' a' W: [2 t! t" U- ]6 K% j. ~
$ Z" d) r7 E$ u3 o: G Print(L"File Name = %s has already been created.\n", FileName);
8 n: c. T5 H" G8 w, V3 P; G% a; _$ O! ?- A: F
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
' S( B6 [' X& I/ a) W" d4 N WaitForKeyOrReset();
; L/ c: V, M8 q) D7 u! r- N+ g # j$ a& k1 x1 Z2 p
return Status;
7 O1 C! J8 v G0 ~ }} |
|