|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*0 I! }# @7 [, ~
* myfileio.c0 f5 N& c2 Q- n6 T
* Apps ? J8 c8 A7 C
*/- x0 W1 a, S5 I3 g7 Z0 h6 h% y
2 Q, y: m' S+ o( M, p0 p' Q
#include "efi.h"4 Q: [" D0 c. s) i
#include "efilib.h"
# n! ^$ }; x8 m! u7 ?
3 g& y% j1 y- X& ?! M# W( {#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
- u; }4 a$ p8 S( |0 s4 Z' \
) @5 n2 y# m9 g! | Sstatic EFI_STATUS WaitForKeyOrReset(VOID)2 G( E8 ^- ^/ c
{
1 [9 E/ E3 y. I8 u( C EFI_STATUS Status;2 G, }1 p9 X& ^4 u/ {8 y
EFI_INPUT_KEY key;
) K6 D4 T, V# Z4 l: Z0 [ UINTN index;! k8 A, r2 u& [; ]$ I
6 ^0 ~1 J5 z* M0 Z( U8 r
for(;;) {
% o( a- ]" X; d( v9 x* u2 i Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
( n! g) F" h" `4 n if (Status == EFI_NOT_READY)
6 f, `, Q* ]' Y/ K; m% M BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);9 x: m; D3 E. l7 w- n
else; L* M) h. O8 e; x
break;; w) z, P, m. u7 z/ ?. ~
}
" ?3 _3 [! r' [- ^; I" s5 m$ h# O if (!EFI_ERROR(Status)) {+ C4 A% w* I' r4 W1 T. _$ g
if (key.ScanCode == SCAN_ESC)8 ^ r6 ]4 B- U1 ~, ^% ^0 [ e
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
7 N- J- J2 V! i1 r* F6 j+ h% w; f }0 k y0 S- t, D
& T' e3 [& K! z0 b return Status; v9 X" ?6 G) |8 h3 {* u- z
}! ?4 v" O, D8 {2 F
; ?, u9 t4 g# Q& z. mEFI_STATUS
/ B* X8 Y, h$ _0 k+ \* ?: aEFIAPI
$ U* s! m. j: \& N% b& ?" ` g+ hMyfileioMain (IN EFI_HANDLE ImageHandle,- ?1 s/ C% P' `8 {5 F* z0 ?
IN EFI_SYSTEM_TABLE *SystemTable)
) W( N+ G) E; @1 r- f0 Y* |{( c9 d( E5 p$ C g0 q8 B" k
EFI_STATUS Status;
, y+ t% X! L6 ], X1 w7 a7 f EFI_HANDLE *DestAddr;
) s# P! w4 z) }2 ~0 p+ l EFI_LOADED_IMAGE *FileHandle01;
$ G, H+ P% F' l EFI_DEVICE_PATH *FileHandle02;
8 I! ~& j# Z* Q( E2 [% R/ M0 X" I' i" ^ EFI_FILE_IO_INTERFACE *FileHandle03;
& m! W& W; [8 c0 _& b) B% P EFI_FILE *FileHandle04;
2 U9 Y9 s) J- Z; |1 ?! v/ P H7 P EFI_FILE *FileHandle05;
: v7 c/ H! }, B7 i" v CHAR16 *FileName;. O" I7 ]& L0 s9 Y. D6 C& G9 P
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;+ A% }+ \. p0 J$ y6 z8 w
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
. }% g/ i$ B9 M! Y* l p- e int Bit0, Bit1, Key_Space; # O0 i4 V$ }9 G1 V$ _% D4 `
! ?- W d0 f: C' G! \ FileName = L"NewFile.txt";
0 S! t! w- O: h3 C Space_Key = L" ";
; {3 y8 g5 V1 V* Z, N/ q , w. m' f' N' r8 g5 L1 y
BufferA = L"ABCD";
+ |" _, Z, c: `; @; b) {, ~9 r BufferB = L"EFGH";
+ G8 v+ N1 ]9 K s7 m* r7 V BufferC = L"IJKL";: g% S+ C9 F, ~! `% U3 \/ f4 D
4 t. {$ P8 _1 f% ~ L! ]" r! ` Bit0 = 0xff;
/ I3 w! D% K5 _# i$ \3 _- `' E2 E' v Bit1 = 0xfe;' C* K' _0 r, |3 c
( P7 d& m2 d# W9 A" R" x% u# C2 k
Key_Space = 0x0020;
: |# k+ K2 f% } $ Q/ c5 d8 r/ f) Z* H' y
InitializeLib (ImageHandle, SystemTable);
3 e2 U3 W" I$ @2 L) F* ]6 f' G* R8 h
DestAddr = AllocatePool (BufferSize);
0 L) T3 \/ q' e# y6 c8 \
. P% G) q0 ~8 a; _6 W) O, A Print (L"Value of Bit0 is %x\n", Bit0);
" ?! b: i8 B# T. T! ?8 M* h' [( ^ Print (L"Value of Bit1 is %x\n", Bit1); w- Q, O" b& J" B- [8 \
7 @8 Z$ n1 z1 B: w0 u $ K2 d7 Z% C) r v1 z& l) B4 f9 Z
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
3 V4 D: x1 M' Z1 [ y if (EFI_ERROR(Status)) {
" }0 t/ ~) L8 e" p/ R4 g Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
0 v7 ?7 Y- n n# ` return EFI_LOAD_ERROR;
, F9 o7 F: ?1 a6 Q* |! k9 `9 a* U6 J }
6 q9 E5 w" l5 W1 ]0 O
0 A) ]. w1 C2 y2 }. x$ ~8 b Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
6 E, B0 D" X$ K if (EFI_ERROR(Status)) {
3 q, `/ ~. @1 h( _( I, p Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
9 q, B/ m3 C+ V return EFI_LOAD_ERROR;# n1 ~/ v: [- L/ d. _, j0 p2 J
} , Y/ e- D9 l* ^
. D; F9 Q2 U t# {8 g- R6 ]+ q Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
+ C: Z! S, Y; L' E9 Z: X. | if (EFI_ERROR(Status)) {$ R9 S, ^" [% l$ z3 X- i: h
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);/ c! I' ^. z# c+ P9 Y6 O3 ]
return EFI_LOAD_ERROR;
7 Y c- ]0 |' j8 V! J }
% t. p* h) T, u% U: L, V* s2 u- W7 u; X
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);) j# v4 j! B1 C! D+ [
if (EFI_ERROR(Status)) {! D) G# g1 S( i4 i0 W
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
" g' K6 v& g4 U; N7 F b# N: h" J return EFI_LOAD_ERROR;
/ \% v7 n6 \) L }
7 x3 G9 r) V% n D" D% r 0 ~8 ]9 ~5 R% C; ^
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);; g O8 j$ h+ s, W. f9 n3 O; o, J
if (EFI_ERROR(Status)) {
4 Z5 x% o: O8 `. [( |9 x+ A" ^ Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
3 ^7 L7 K3 E! M ^ W return EFI_LOAD_ERROR;: `) ~% P3 O2 @5 Z. P) J# [
}
6 N" N! H, k1 w1 [
& C3 K) k5 \: L7 @( p5 t Status = FileHandle05->SetPosition(FileHandle05, 0);
2 w9 l# G& @: a2 ^0 W if (EFI_ERROR(Status)) {
0 X: {' o; ~, G7 f0 H9 a7 c0 i& h Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! ]6 C k h9 k
return EFI_SUCCESS;
- v2 A; A {3 @ }$ r$ a9 I+ i$ }1 j" q, M
" d; I# f3 P* t2 @7 N8 `+ n! I
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);. l% P3 B5 v: b6 Z( Z* C
Print (L"File Buffersize is %x\n\n", BufferSize);
+ ?2 ?% y% k/ U# x if (EFI_ERROR(Status)) {' b L" \, u% b7 `1 R) ]2 _
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);9 P+ k0 Q" B. i
return EFI_SUCCESS;% j. r1 D( H. R8 T8 ^
} 2 @) b, Q0 r$ C; `) N! s* s
. X9 l/ |; U4 O
Status = FileHandle05->SetPosition(FileHandle05, 1); 4 b } ~0 B/ X9 L
if (EFI_ERROR(Status)) {
O& D4 f& ]5 `5 x( f* m8 W. j( V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 l. E4 B1 x8 s( J return EFI_SUCCESS;
# I. S1 [& {; b" l }
; h# [+ y! w3 B5 x" c( E( B ( R8 G( _6 b" |+ D; J
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);# @* x' A) ^2 s) w6 L
Print (L"File Buffersize is %x\n\n", BufferSize);
8 b. Y4 ?, b3 ?8 a if (EFI_ERROR(Status)) {
2 s1 y, T" {; z. `' x/ ?& R7 p5 D! M Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);5 R" Y' ?' m5 U4 [
return EFI_SUCCESS;
" t* ?1 v2 } i, i3 q1 M } " J3 t8 p6 F7 w4 W, C8 b9 w7 W
8 j6 G5 C& m% X Status = FileHandle05->SetPosition(FileHandle05, 2); 1 E& f+ G- U0 G& x: p+ m V4 e" }# D
if (EFI_ERROR(Status)) {
) d3 |9 l6 _ Z0 F! A8 r0 J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 `. r, B5 U4 I" C$ C
return EFI_SUCCESS;, w% I9 @- o/ g, c6 J
}
$ h5 e$ N) X' D
# A' s, u0 j0 ^" S! P9 D Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
* _5 O7 C( ^0 R Print (L"File Buffersize is %x\n\n", BufferSize);
" G H; b" `" m1 U if (EFI_ERROR(Status)) {
* T: c' p, x8 I( d: B Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);( u: g0 x, y& }/ n
return EFI_SUCCESS;
# n; V' S+ v! G. S- J! e$ t- l }
1 Y% k% V& N' H/ S: ^! `' L6 M ! \8 i& t( T2 G9 {6 N+ H7 X2 Z
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
9 c4 c' x. ?& N3 u Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);# D2 `4 H; Z* w/ P4 T- X
Status = FileHandle05->SetPosition(FileHandle05, 2);
! k) F* q5 _0 \) X( w9 ?- o if (EFI_ERROR(Status)) {
$ A; T! p+ f! L( |+ F# ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
- ^* d2 W8 ~+ {7 ~ return EFI_SUCCESS;
. k% L8 [0 p" V! a: Q }! H4 H9 X: X& u9 V: X$ s# A" |0 c
1 X! r. i$ t% a2 h3 B7 w9 V- w Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
5 b1 F1 G/ e+ E$ M if (EFI_ERROR(Status)) {
0 ?/ S; b. A. d- V1 @$ ^2 B t Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
( @) s0 b* z. Z5 x return EFI_SUCCESS;
' w, d& a& t& l; O9 c } " p+ Z; N% _6 P$ W' y. B* W: W
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);1 I/ o7 \& ?- m0 H3 l# U
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<% X* F$ y/ k% W5 L! ?
+ Z2 Y5 ~* e. C' q& \ Status = FileHandle05->SetPosition(FileHandle05, 10); 1 T1 Z( o; Y2 v& v% R. u
if (EFI_ERROR(Status)) {
" A' g$ ^ d1 [& A Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; W7 N, R& d `. `7 O+ J E return EFI_SUCCESS;2 v) ?( g# F0 P$ I7 ?; d3 `3 s1 M* K/ H
}
& x3 i" N; z& Q/ s5 w o
' z% A: h. N' v; F4 z% X# j5 { Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 n$ y# c5 n: r# T+ A! O; `! w if (EFI_ERROR(Status)) {
$ p! z/ g( L& s; N9 i0 p+ { Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
; Y, E! Z9 N4 H return EFI_SUCCESS;
" o V. h$ U, u }3 n/ A4 {7 H3 E
, q. Q6 [) P& ]. |) k% M( B$ E Status = FileHandle05->SetPosition(FileHandle05, 12);
& H9 L7 m. s" T& W- C9 a if (EFI_ERROR(Status)) {2 `4 j+ g: P6 z r8 S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) ]% G3 W- ^/ n! ~ return EFI_SUCCESS;
* x% N. y) p. O1 |$ g }
) P1 `) d9 _8 Y! w' ~8 x
, l/ Z: m' a5 z4 [. s Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);% p4 z% |3 |0 Y0 N. _6 B9 @% y. l
if (EFI_ERROR(Status)) {
$ G% [9 K5 t/ M! ^2 a8 @9 O" N Print (L"Could not write file, FileHandle05 err code is %x\n",Status);" C4 [5 x0 Z4 {$ R8 z
return EFI_SUCCESS;
" o' D) v( p& e% Y; |% s }6 ~0 v: ^ P* u- o a/ K3 l% g( ~' H
6 T0 s" ]' I/ `$ T; F% n Status = FileHandle05->SetPosition(FileHandle05, 14);
- W% E2 O. A7 B if (EFI_ERROR(Status)) {
' b& O5 I- W% o9 T Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) H% b7 g @2 A N9 b: L return EFI_SUCCESS;8 ~6 G6 x( H; \9 g
}
& k% t6 ?% o- y
! ~! S* y* T% }+ L0 I3 C Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);) f* P% A; f9 H3 o
if (EFI_ERROR(Status)) {
/ p4 Y4 \' w g5 v, u" ` Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& s6 I! `+ l/ T3 r7 ], g
return EFI_SUCCESS;
* Q2 N1 }; y* Y c* p0 ^ }
" [ Z9 t$ _2 P7 f' v; L" f/ h! U0 [% c D$ C2 _& i$ q4 O
Status = FileHandle05->SetPosition(FileHandle05, 16); / P! Y2 }% [* u3 o
if (EFI_ERROR(Status)) {* v( h$ v; i6 b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" Q. z; D: m7 }2 ]$ ^4 { return EFI_SUCCESS;# l- G( j+ K$ y' H! I
}6 |7 B4 J0 a* m( `5 ]0 W* p' u
- E( ^* U4 b! P6 D
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 ]% F( S! S- }6 N) q8 z1 q+ v- E0 _ if (EFI_ERROR(Status)) {
: S. L) S% m" J7 h/ T: P# y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. {, z' N: N6 Z. r0 Z- l
return EFI_SUCCESS;
; O5 R9 V2 M. I- m) D } " C+ _# D' h8 C# p D
0 Q$ y+ ]9 @% w l* t3 c, P' ?
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
8 [2 W+ H8 }1 [- p- J* c: d' ]1 q+ T0 d6 B- ]) g- { j4 r, b
Status = FileHandle05->SetPosition(FileHandle05, 18);
4 [# @% d# U3 U6 m0 a2 F. } if (EFI_ERROR(Status)) {8 `. N4 e% z2 s$ N" Z+ f# E8 @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); O: }9 U1 @3 T# L. u5 X; W f
return EFI_SUCCESS;
" P& K+ N& K' N3 A H }
6 a8 Z# a" m. Y# ?+ @& }: `9 `
! q+ z h' _; p# [$ L" d Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
6 {9 q7 G u: j9 j9 c if (EFI_ERROR(Status)) {
% M0 V0 j/ p- T Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ c) w2 t: c j; ]+ |: V$ P
return EFI_SUCCESS;) ?$ y2 M1 P* M6 r: h4 h
} 0 O- j% V# p+ ?) E; F
5 b) i: {6 j* P- @9 [. D2 _8 T FreePool(DestAddr);
g& b: B: U" L1 M2 V* D6 H z4 Q, d) J
Status = FileHandle05->Close(FileHandle05);
7 z. J6 X4 r4 G8 M) B+ M if (EFI_ERROR(Status)) {
3 a9 v" S3 m/ t4 p8 ?% H: k& I" ~ Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
9 }6 T$ W+ M& H- E9 o return EFI_SUCCESS;
) ^' j8 b z0 k* i& t }, ^/ | u/ `# n
7 Z% z: | R7 n5 T8 e& h Print(L"File Name = %s has already been created.\n", FileName);. E, P! w B8 C
9 J( }4 E* h- Q* A6 Q* W, E- | Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
2 G6 _7 u; }2 _) q* M. w WaitForKeyOrReset();' G% |2 E/ r$ }1 i# r- `" k$ M5 H
4 ` T6 i. w- \) Z$ j8 Y: y
return Status;
N2 S8 `# R. w# j7 }3 X- B( C} |
|