|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*1 W& P" }7 j0 u$ _5 C
* myfileio.c
5 K9 n7 G" ]' Z. W1 q3 d0 {) n+ e u# s * Apps/ J4 r# x2 X( W
*/. G' ~7 _' p( Y6 y
$ j4 x" l- ]. G& _$ e6 [% ^5 ^0 x
#include "efi.h"" }: T& H- J0 k8 K' [& Z! {
#include "efilib.h"% t( R5 x' I0 S H% W0 T
( \" G! V3 T/ e! G) @& j9 w
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
& B5 j* y6 h+ l* F- U* m) s) N: Z% G2 p/ [& w0 J
static EFI_STATUS WaitForKeyOrReset(VOID)
" y" ~8 y+ }7 ?4 p{
Y5 f6 j7 Y6 p2 e EFI_STATUS Status;
; N. @3 S7 q* L7 y( Q& c EFI_INPUT_KEY key;' o! G7 J3 b# K$ |2 a1 G
UINTN index;. b- A5 p. Q/ ^2 W* y7 {, T
6 d ?6 m' G0 v
for(;;) {! N- t; z: R) t8 d- {; n2 U& |
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
3 L/ ^8 z" y& T4 q, }! ?1 I0 \ if (Status == EFI_NOT_READY)( D h! v. c$ ]: G# {5 u" v& c( b
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
9 j8 l6 b) e: u/ Z- _+ q else
8 Z8 Q/ g2 @2 L, i- V break;
& I' E% S& k& Z6 x- z }
# H# F& [2 @) m% L if (!EFI_ERROR(Status)) {
r" i% S7 K7 B% ]9 V if (key.ScanCode == SCAN_ESC); ?: X% x+ }$ C( o( |* ^
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);% w2 J8 `/ @ a9 K& e) y
}, L! ^; d2 n, e) y" ^* |! b: `: d
; m% ~0 M3 l: H return Status;
& Y/ l5 P5 W# F}* f! R1 g e$ H1 Q
3 I: K, Y4 P8 i6 \ yEFI_STATUS j0 t$ ?: E& F- O% G% t
EFIAPI) x: v- G6 j7 ^
MyfileioMain (IN EFI_HANDLE ImageHandle,
, j+ {$ k7 d2 W IN EFI_SYSTEM_TABLE *SystemTable)
, T( b5 Y8 n5 n/ |+ b3 a2 H0 z{
4 I. H7 d' D5 o3 F) M EFI_STATUS Status;* ^1 m( G& N% u5 }2 f) ^
EFI_HANDLE *DestAddr;
6 [& d3 n: r' t1 V% T EFI_LOADED_IMAGE *FileHandle01;
4 o7 X0 H+ t j$ n EFI_DEVICE_PATH *FileHandle02; + c( R1 T* ~. S6 D. y, A
EFI_FILE_IO_INTERFACE *FileHandle03;7 }- g! Z: |1 @3 x
EFI_FILE *FileHandle04;% Q* k6 ]4 ~( P. @; j4 {
EFI_FILE *FileHandle05;
+ M% U2 U9 g- T CHAR16 *FileName;
- V; V* X# d& S2 `& D6 S CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;* B" O0 ]2 ?: i6 T+ U8 x. \
UINTN BufferSize = 8, BufferSizeKeySpace = 2;) \' Y6 Y/ h, K" d
int Bit0, Bit1, Key_Space; 3 O6 s l" ]3 T& F2 v
/ k) R& v! J, ~ FileName = L"NewFile.txt";, k9 K( q, M8 ~* D5 Z, H
Space_Key = L" ";
: Q7 D% ]8 ` W7 |0 h 1 i; B* j& |' {6 A1 D6 \
BufferA = L"ABCD";$ a" ~0 K6 G/ ^" X+ C9 |
BufferB = L"EFGH";8 o5 @( ^/ }) x3 t& Q; F/ [& b' W
BufferC = L"IJKL";
. b5 }& Z' a7 X7 [: @; Q8 q: t
9 R. E) n O9 N/ z" b4 R Bit0 = 0xff;
B: P, s! J9 A/ Z+ ^- _ Bit1 = 0xfe;9 ^$ M# o+ {/ n$ j& \! x
5 \2 t7 H* Z9 r" p Key_Space = 0x0020;; R4 V0 _$ N3 s$ m6 `8 N
$ C0 \0 b2 o9 v+ ?1 ] InitializeLib (ImageHandle, SystemTable); 4 Y8 @) a `( k
7 I* m" U3 `% D+ o DestAddr = AllocatePool (BufferSize);
# R9 ?1 v0 j P; S/ f. i o$ T# r! t4 s" `. S& I6 Q
Print (L"Value of Bit0 is %x\n", Bit0);6 s, I* r) T/ |2 V9 @( N0 C- S
Print (L"Value of Bit1 is %x\n", Bit1);
' V" M A9 k7 o9 X7 e% ?+ K: U4 W& N& Q O% j4 B. J7 m# K2 n! W
5 x* |# t" q" T Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
" x* U4 h) e5 s5 G/ K if (EFI_ERROR(Status)) {
' ?; T" v* B2 p4 p: ^6 A Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
1 H( v- i* e- | return EFI_LOAD_ERROR;7 G6 K/ a7 g# Q# b4 F* ~: q4 s2 B
}8 ^: s }! _: E2 E/ c7 l% o
+ ]: W9 ]2 m- O3 {) j0 c2 H! [* u* E Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);# X% Q4 w7 T* h- m5 x( Q. W3 e
if (EFI_ERROR(Status)) {+ S6 u! O6 B8 `
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
7 r6 T4 S. g4 F% X( G return EFI_LOAD_ERROR;" b4 ^5 E" _4 s6 J$ V
} 9 t- B9 ?5 L1 L( g
+ ^+ i6 \2 {$ R% X& s4 `
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
. ~+ g0 }% e( R4 j if (EFI_ERROR(Status)) {5 E9 Z* l2 L4 q. v2 G) }
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);' C/ E0 Z5 W6 \& l$ s) j
return EFI_LOAD_ERROR;. E) K; [* Y- G T! u) b' s0 M
}
# u5 s" `) l( L/ z4 H2 ?" p1 c% Z- ?. Z9 x' X
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);2 h: {! k/ f, P8 c- N! D% a* A5 i
if (EFI_ERROR(Status)) {
# y& @* v2 o: Z Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
! t) o# l1 y4 E. \! ?$ ~ return EFI_LOAD_ERROR;
( l3 V" N- X! Z$ l. B' ]# V" } } - A5 t6 F$ T2 i( P$ I) A! W
+ U) h% n1 K3 Y) ^3 S; h. h
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);, i# P$ o$ Z- {5 ]1 y
if (EFI_ERROR(Status)) {& X4 Y# E3 R; O, H( j
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
P% F5 `! U f3 J( w return EFI_LOAD_ERROR;
8 O+ ?" H0 i5 @1 G5 K9 ? }
! c% @2 d# U" Y" z, s $ B( x; P9 P& P' c
Status = FileHandle05->SetPosition(FileHandle05, 0); 3 @/ |3 d5 j8 q& [0 T
if (EFI_ERROR(Status)) {6 e1 X1 u9 Y2 k8 F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! U8 y' g0 d: H2 y return EFI_SUCCESS;
! ~3 {1 V* w( a$ A# v, s0 K* u }8 A+ g/ ~- C/ U: P+ j# ?
/ z5 e5 k4 Q7 E& e' \( O& _9 s
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
9 ?2 t8 t* X8 {: i Print (L"File Buffersize is %x\n\n", BufferSize);
* ^& @) o2 ]; v& U. h9 Q if (EFI_ERROR(Status)) {
: R9 D, h ~3 W" f0 ^1 b' ~ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
- o' @; m9 f3 `' h return EFI_SUCCESS;: ?6 |: O0 Y' _' ]2 O4 w
}
0 r" E; b4 ], C3 L6 J& n3 E, |4 F
) b5 G* t1 ^* |! [* n, x) H Status = FileHandle05->SetPosition(FileHandle05, 1);
/ l; l2 V: A5 Y% O5 E if (EFI_ERROR(Status)) {
- q- I, ~, h3 I* ?/ e9 r3 U }, a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) e- z" Y/ S+ b& t
return EFI_SUCCESS;
! ^. X# t: q5 ]" o; o4 Y }
. q" k& C, p: |3 F1 g, \. c0 b' m * Z# D# i+ X$ [
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);/ T0 R0 {) |8 F5 t( p6 I9 c5 }. H
Print (L"File Buffersize is %x\n\n", BufferSize);
3 X5 f( M. f# B$ S if (EFI_ERROR(Status)) { |' M% z6 Q' H* q: ?
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
% p( p; A! J. n+ n; ~' W! d- @ return EFI_SUCCESS;# f4 `# [+ i' M% P9 A( ?
}
0 z# n! c: k9 e( S/ w
. ]3 @6 }$ ?7 I4 C$ L: x( }; i Status = FileHandle05->SetPosition(FileHandle05, 2);
) S H6 F$ B% m' D4 q+ o" g H- t if (EFI_ERROR(Status)) {5 M$ i6 @ a/ @- D1 V1 v
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 z: e- n$ X# h3 E return EFI_SUCCESS;. q3 x9 \3 `* u' m3 S9 _( X
}3 ~- w! J* Z3 Y7 R
8 B0 N) p; j" `& x) w. r
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);0 z" H' H$ A/ z
Print (L"File Buffersize is %x\n\n", BufferSize);3 J R. j; C# l0 y* x& f* H
if (EFI_ERROR(Status)) {1 D" t, G9 R/ y6 t# G
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
4 z' p: Q9 t' R5 t+ p return EFI_SUCCESS;* w% b1 j0 W3 h: q: q! P) }
}
' ~3 g' ?0 S6 I9 S / c3 [; e2 O+ F' v }$ |0 I! [! u' s
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
; w x" a* j; K3 t0 T5 F Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
: o' @1 w, \. }, J( L* | Status = FileHandle05->SetPosition(FileHandle05, 2);
7 L5 L3 B, f6 ^$ i% S if (EFI_ERROR(Status)) {) M# E+ f1 \6 k; l; E! s, \, O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); y" u& a* ~( X7 A0 f2 T# I2 O; d, z' ~
return EFI_SUCCESS;
' p7 w5 [! F. P) I }5 q( J' M& X* \* o
. q O: }0 P, m" a- U Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);6 o- O5 B1 B1 C" |" l) E# U
if (EFI_ERROR(Status)) {
( W$ g- J9 n0 b$ f) {2 S3 C Print (L"Could not read file, FileHandle05 err code is %x\n",Status); [, `/ P9 R+ {: S
return EFI_SUCCESS;# g0 I' t8 C" o" S* F
}
4 \0 y' s# \6 S/ `/ A* Y+ v0 _ Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
6 E8 P3 M; E9 |) u/ u( W//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<< b i0 M( u7 I6 ?/ x
! D( v% e0 T! m4 U/ I; H
Status = FileHandle05->SetPosition(FileHandle05, 10); / E- o. m. E3 W8 A$ y3 K( c
if (EFI_ERROR(Status)) {
9 U% c; j2 ?" f* V# j% M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% {4 k& ~2 j$ a# p3 X4 b
return EFI_SUCCESS;5 H2 t7 _+ I( q/ D! l1 q3 `
}
# r6 G% h% D# m2 K, v, |2 E9 W3 N
0 ^" c( W# z& _3 W, n4 {& v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. I6 H0 g( J3 J! y if (EFI_ERROR(Status)) {
6 W9 g; T! L+ t$ C ^7 k- b" Q Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: h# L/ J, G! i
return EFI_SUCCESS;2 g; _5 A0 w$ H( B! J
}
) M+ C0 ^( t8 V4 x! x& P6 r
- x9 O, s/ ~' x0 W L9 B/ |; z Status = FileHandle05->SetPosition(FileHandle05, 12); 3 F6 h/ }5 d2 X" [
if (EFI_ERROR(Status)) {
% R& r8 `% o& K2 m, F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; }; E( y( B) F/ U) k7 C c return EFI_SUCCESS;; L! D, H( d0 t0 s8 s$ A
} {/ a+ Z" s* }) I/ G; H
5 R, e2 a0 I) J' e) J4 f% D
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ |$ z2 E- m; L; q* P if (EFI_ERROR(Status)) {, `# m U2 s" ~; m) O1 u$ q* k
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- ^. V( M9 z% I return EFI_SUCCESS;& ]# |3 |) v( S- b ^8 t
}
7 v" S C; f( g# \8 g. d; c$ U# P6 x" I& |) s
Status = FileHandle05->SetPosition(FileHandle05, 14);
x3 c0 j# ]+ _. ~8 g) _ if (EFI_ERROR(Status)) {/ V# o" I' [5 c+ w( G0 S! q' S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 ?( x/ f& c: n2 U+ c return EFI_SUCCESS;
5 S+ X% Z% _* [( V! Z }# A P0 e* f/ d9 f6 D
! b2 I N8 j; v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ Z+ X" ^+ Q& | O' S# P4 }6 i if (EFI_ERROR(Status)) {, N* Q5 X4 ?& c# N
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: q% V2 W; I5 x9 [
return EFI_SUCCESS;
' i2 M6 S7 N6 {3 i7 ? }3 }( f* E/ T" j4 q/ H1 o* @/ m
# B5 f2 n: ?& F+ ~, y. [ Status = FileHandle05->SetPosition(FileHandle05, 16); ' j6 \9 O. X2 P' p# V
if (EFI_ERROR(Status)) {, T8 q; k6 z9 q' N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 K6 N" X& q( f6 ]
return EFI_SUCCESS;
. } k( Z8 i5 m! Z" y! e }
' B" f( x5 E1 w+ B4 T: Y
/ C' I* O: y! n* J; ]+ f! o Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( |6 k$ ~1 d" m if (EFI_ERROR(Status)) {
4 T, Q# F! W. j0 X' X- E$ C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 e/ _- H* B9 b
return EFI_SUCCESS;( V- E6 s U) U. f
}
" f' \1 V% o2 |, o) U# p5 j/ H* V. F7 k u$ e( F
//---------------------------------------------------------------------------------------------------------------------------------------------------------------; N+ r: J. h d9 z! T
5 h5 f! d' w% V- i/ E, _: y" ~
Status = FileHandle05->SetPosition(FileHandle05, 18); 6 y( o$ H6 G9 G" Y% O2 c" J
if (EFI_ERROR(Status)) {4 x0 e! l1 S3 `# B+ z( o
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 f% t/ m1 P0 V) L% k* T7 M
return EFI_SUCCESS;
0 L; R+ R3 i: z0 `8 R9 J }
F7 x$ Y Q7 ?. k4 w4 ?& k
" K4 V1 P. J# T Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);" k5 ^: f; J9 _5 d3 p
if (EFI_ERROR(Status)) {
& h- i! Y+ N. e. X Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
7 q% d Q. }! x1 a/ A return EFI_SUCCESS;# ~* ? A( a2 `2 }& y0 U
} / T) g$ @( f* \; w# Z
' W- _2 I8 Z, F3 ~/ f
FreePool(DestAddr);
$ c7 y' y/ L4 ~* ?6 k+ r
/ @ L# h2 U. e: X Status = FileHandle05->Close(FileHandle05);
5 W7 S$ b( b9 ?* d9 b if (EFI_ERROR(Status)) {8 L) @0 s+ M- B6 N) T* s5 k
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);( I# V7 z8 W9 \$ ^) p2 P& i
return EFI_SUCCESS;
% K; H& R g ^% k6 O6 J3 H; F2 h, l }% x$ b+ d9 ?. K/ P% `
. ~, u: e: u" c9 M
Print(L"File Name = %s has already been created.\n", FileName);7 B+ R% D2 P5 j4 S; |, `+ p
6 i V* e v8 u- M$ J" D5 q" [
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
/ N* u/ e. Q6 C" D. C WaitForKeyOrReset();
1 `+ r% A7 Q8 ? G
5 i2 }, ^: V' q0 {/ X# H5 _3 q return Status;% w! t: F" G* b' d
} |
|