|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
/ j* [' N" x7 s0 P8 r * myfileio.c1 ]1 a9 D z0 v Y7 a
* Apps
; H* c) k9 ~( v( r' | */
$ B6 Y; [2 x7 X5 |: n
3 s5 S/ h$ b# [# a! d2 q/ {! x#include "efi.h"0 w8 P7 R* c8 q, s( ~/ J) M. ~
#include "efilib.h"
1 M% q; L e5 e% Y v' B( z
+ A" c; H6 P6 O9 g#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
" K0 M- x K% Q- o
- l' k4 i4 V: L* I4 ustatic EFI_STATUS WaitForKeyOrReset(VOID)
# A% I0 E8 f7 g+ q' ^1 [/ B{
. G9 }- e" f( X0 q2 u# x3 ] EFI_STATUS Status;; h9 _; ^# u; |! J1 ~% Q
EFI_INPUT_KEY key;
& t. G2 B+ [- ^& z UINTN index;' l3 @/ g3 G; f: h& L8 ]0 b0 t
/ f4 n# r& x/ o. b* q! s! g1 Q for(;;) {2 w+ l$ y5 z6 |/ R, }0 C
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);8 w8 d0 M0 h: Q" F7 O e4 c2 w
if (Status == EFI_NOT_READY). i/ H( H, }& ^+ Y1 A
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
' g$ F5 H }( [ else6 ?: f/ {$ j; _: D J
break;
+ G D5 G( g8 P0 g( ]: @- r }) ^$ ~- S7 J% i2 l+ \# p
if (!EFI_ERROR(Status)) {
/ a8 o/ k4 g7 u I8 e if (key.ScanCode == SCAN_ESC)$ h$ s) m' F( f
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
1 }. R' u( j! s: J& N3 r& m }
# l+ C z V" P O( Y5 b " M2 P$ J; L0 k5 ?
return Status;
4 S5 l9 u$ t+ o; N2 J}
6 E* H# G& X. z, \
5 C# L/ U) D- S; ~$ A- KEFI_STATUS2 O8 ?3 x% d" x7 e3 h# z0 Q w
EFIAPI& i/ H% w1 X( W9 n# e0 ~8 ^. C8 |
MyfileioMain (IN EFI_HANDLE ImageHandle,, z& m4 i7 O/ _5 N" k
IN EFI_SYSTEM_TABLE *SystemTable), c0 U& d) H2 B4 j u4 t
{3 P3 H) `( `: D
EFI_STATUS Status;7 m+ h( a D: G! E+ F3 T7 A1 I
EFI_HANDLE *DestAddr; 8 v! n% C- H# Y. E' p# Q+ K8 o
EFI_LOADED_IMAGE *FileHandle01;
+ S$ f# j. W, J9 ?# ~$ w EFI_DEVICE_PATH *FileHandle02;
, ~+ I% ?& O0 L EFI_FILE_IO_INTERFACE *FileHandle03;
3 p$ N1 r) h; X9 D5 Q EFI_FILE *FileHandle04;: R; m& E# V/ S1 X5 x" N4 U5 |" [* c
EFI_FILE *FileHandle05;
# S8 G: l1 O. N0 S7 |, r CHAR16 *FileName;
+ D1 M/ x* b. b" {- P! t' P& _ CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;6 w2 m+ C: r; }; u4 ~
UINTN BufferSize = 8, BufferSizeKeySpace = 2;9 y; v. Q" R; M! g! B7 M. A
int Bit0, Bit1, Key_Space;
- }7 @5 L) H( U y3 L; [
( z5 P' g9 }) U7 d! U$ | FileName = L"NewFile.txt";
. n4 h, ^, d! R/ R" _ Space_Key = L" ";" J m; [' @' ]7 K
+ G" l+ }/ \6 b6 d- |& T- R
BufferA = L"ABCD";
! `" y* P/ D: r# Q2 a+ n BufferB = L"EFGH";. t# x L: x2 K" r5 b* `
BufferC = L"IJKL";! x, Y$ I- ]( {: m$ i* C7 {3 R. }
5 Y! P }1 ]! a( z* m. c$ h Bit0 = 0xff;
( k* w) T4 W3 r% _4 @ Bit1 = 0xfe;
* u) x# ^9 ? W " U, P5 |' [0 y
Key_Space = 0x0020;; k! d9 H4 V2 T0 k- ]& g2 K
* g" }+ b9 W$ E* m0 q; y6 E: y
InitializeLib (ImageHandle, SystemTable); 2 [1 K* z+ K- n1 M: ?
6 Y$ b/ g* J9 H( ^7 V7 ^3 u6 E DestAddr = AllocatePool (BufferSize);
( l: N" y7 s y4 | f; e, |; J; e Q7 g( @+ j
Print (L"Value of Bit0 is %x\n", Bit0);6 P) V# }7 r' O; A% d
Print (L"Value of Bit1 is %x\n", Bit1);
) w6 d, Y. Y# H, v
; H9 B: t6 ^3 R7 z- [% Y8 k* `3 y
5 n5 |# S9 Z4 J' N# ?4 H Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
2 C U. ^0 l0 F$ k1 d4 B3 S if (EFI_ERROR(Status)) {
, b$ d' O5 m w& v7 |- r Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
* J' c0 P/ I' P0 l. c return EFI_LOAD_ERROR;3 ~9 v% L' F7 ^: [3 {* f& C
}# ?* B& h! ?% ~5 G- g# X- b
$ j2 f8 q# m! e8 M) c
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);3 O. ~( z; u8 E. S5 C: k
if (EFI_ERROR(Status)) {6 P( s) o* n9 d% c
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
( ~6 O& |) h+ G# n" ?( Q return EFI_LOAD_ERROR;; s7 e$ c" U+ h1 Q* L
} ]* a. }/ [: F
6 z! W7 s y3 V W2 q# x
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
- }, |6 U+ l% N) Z" e if (EFI_ERROR(Status)) {
3 g& s2 y$ F. }/ s& c; L Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);2 n* ^$ x: ?8 o6 K1 B' x7 l
return EFI_LOAD_ERROR;
+ o8 X n3 H5 i }
, W& q8 J. R) a, c8 _
X6 B3 H" K0 B6 o# l. K" Q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
, {1 A! n' |7 ~ if (EFI_ERROR(Status)) {4 D8 ^) N& P' Z6 G
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
* L! U0 [( n6 J1 ^ return EFI_LOAD_ERROR;
/ \3 F6 I3 L H8 K, R- Z1 K } 8 x1 l4 G+ {+ t8 W6 N& ]6 i
# }8 Z% l( c; v& P$ f$ z
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);, _+ x& A$ ^% g( l! ?
if (EFI_ERROR(Status)) {
3 R' Y3 g$ y. X3 ^( H( w Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
; Z" v- p0 r; E! _" X9 t return EFI_LOAD_ERROR;
0 `! O# i- p ]; e! q }
8 t' m3 }% c+ }" X8 Q/ F7 h: a
3 B6 C7 Y( Q& P$ | Status = FileHandle05->SetPosition(FileHandle05, 0); 3 ?* @* }9 V3 D6 W% I% l
if (EFI_ERROR(Status)) {% h* c. B/ B) d! ?4 S; g9 L( k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 D0 `/ A x" I, ^; n. y$ ~
return EFI_SUCCESS;
& B! a: M% |, O# k- w! u: |3 M# A }
8 @: i1 Z$ B5 B0 E* i1 j / ~/ t3 T4 l3 q
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);+ o- z: }5 R* ]3 T
Print (L"File Buffersize is %x\n\n", BufferSize);: K+ d9 ]' q, w
if (EFI_ERROR(Status)) {
$ k* S4 Y0 T, d" X Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);. O1 I/ H2 K2 y: @9 Y" _
return EFI_SUCCESS;# e& W1 p' B, A
} 3 W! n: c7 f- d. [7 g3 M! ?" g& Q3 [
- l: S2 B+ S" g) J) x
Status = FileHandle05->SetPosition(FileHandle05, 1);
( R. s2 r( \3 c if (EFI_ERROR(Status)) {
^' ] {: ?- c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 \. l) |& a% c' v- F# F return EFI_SUCCESS;7 g$ n5 D+ F& }5 x
}" J I$ n( h" d* {8 J' j; Z1 q
% Y% m" W) r/ `5 O ~ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);0 o; X2 y& m+ L* V. H
Print (L"File Buffersize is %x\n\n", BufferSize);/ Y( U! L6 ]) l' n0 ]! ?# n7 c0 q2 g
if (EFI_ERROR(Status)) {0 v6 W6 D* T; z# w
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
. i |1 @6 [1 E/ U return EFI_SUCCESS;% ?2 ` g! d! K4 w( n9 O( C
} 5 l! L$ A7 u# R4 K
) N6 H( O/ L7 x8 a, C
Status = FileHandle05->SetPosition(FileHandle05, 2);
' x" [1 v4 d4 D3 Y1 \% z6 {% I if (EFI_ERROR(Status)) {
3 N$ j& |) U# _% R2 y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 j ], H) r2 P
return EFI_SUCCESS;% N1 T5 w; Y, w( r
}
6 u8 w. R! s9 F: [ ' k! ^3 b# f% X5 d, Y! J: }3 K
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
- ^: u" F I4 I1 }0 k5 H: a' H Print (L"File Buffersize is %x\n\n", BufferSize);
+ V: E$ ]/ B/ B; u) L' J; W if (EFI_ERROR(Status)) {, S+ r1 |) C- I, S4 I& H
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);: i+ P5 _6 `- a2 r
return EFI_SUCCESS;4 a- i! ]. ?+ U6 Z$ r
}
H9 R% B. u5 {/ G( L# \
4 B9 U' @8 T2 x0 M2 \- I//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
2 H" y7 g! y( {* y' s, i. y' O, d Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
, T2 X, b$ \5 ^2 O$ `8 v Status = FileHandle05->SetPosition(FileHandle05, 2); ; P6 z/ ]! A0 @/ b1 `
if (EFI_ERROR(Status)) {- O) Y/ s, c1 U3 A
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- ?, W7 h7 X, s' j9 W K
return EFI_SUCCESS;8 F1 x" h; E, Q+ o/ u+ L
}" T' _4 P9 ^! Z4 t( S
8 P& g* D# j0 d3 h( ]0 F
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);- W+ _6 [4 G# H% W% L2 p
if (EFI_ERROR(Status)) {$ _) z$ L9 |+ C6 {6 V
Print (L"Could not read file, FileHandle05 err code is %x\n",Status); q5 |6 M8 B( k \& r
return EFI_SUCCESS;7 J- }, a& j; r2 B9 b7 ^
}
5 I [( \+ o7 ^3 M( e0 h Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
# ^& ~6 f4 F0 `7 d3 h8 e//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<# y6 V) Z5 F9 {- ?8 d
, x& u6 m( Y8 J. Y4 ~
Status = FileHandle05->SetPosition(FileHandle05, 10);
! ]" R5 j0 @* b$ | if (EFI_ERROR(Status)) {) r; t# v. K0 Y; x" X
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ e2 v5 a4 l0 i6 \, j' O' p return EFI_SUCCESS;
, D# p! k8 w" h0 k# \$ F; P! Q }( b: X4 W3 k! n
) y0 u4 L/ U- p, Y! z
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& o$ }' d8 m* w- U
if (EFI_ERROR(Status)) {! y+ i C/ L( y* ?
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! v3 i4 @" t/ t% g return EFI_SUCCESS;
/ C& ~; h& ]% t. f1 b& h. U }
. U+ _- v& u' n! ]" q% M& y0 A% Q% @" {+ [4 G$ N6 c
Status = FileHandle05->SetPosition(FileHandle05, 12); j) Q0 E! r/ ^0 s V* `
if (EFI_ERROR(Status)) {/ L% J- h; H; H* K5 n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' n2 y; m8 T. E6 F: t6 p* s return EFI_SUCCESS;
6 {5 U4 H1 j }/ y }
J2 y( B X' n! _2 d , V0 q5 ^' w' X5 n! @6 M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);4 m4 j, X2 {" @. J0 F
if (EFI_ERROR(Status)) {
- R @1 \& G1 x% D ?: s, x Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& u1 l0 ~! S5 ?! {, J
return EFI_SUCCESS;
: Z! y( w& m, R5 S, B4 [ ^% h& Y }
$ L: _& g- f, R+ A9 K N+ h% V' [8 ^2 B5 f. G) B P
Status = FileHandle05->SetPosition(FileHandle05, 14);
1 U: b3 ?0 i" n% B; L1 b7 R if (EFI_ERROR(Status)) {( w5 g. t; k& D! x) w& O) Q7 k/ q+ j
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 E# `3 U% b f' U- L return EFI_SUCCESS;
. @- s, W' K; Q9 E* h' a3 @7 I9 y' K }
# Y3 k: c: X* j( W" n. z
, _; S+ ?7 {# Q L; n3 z2 ^4 Q Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
) h2 J3 d, m# X; y/ G' K if (EFI_ERROR(Status)) {
4 o- U- l# n, E% } }3 ?( j! Z( R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 v8 N5 R' n |; u+ \8 I# d
return EFI_SUCCESS;, O, w6 F6 h) Q, O9 a
}# Q8 X, d# @. L$ x+ i- B5 J( l4 {) `
( T( O) w, {' ^; [5 P Status = FileHandle05->SetPosition(FileHandle05, 16); ' I! H4 | ^& b! O, @1 ?
if (EFI_ERROR(Status)) {# U& U" H- o C5 U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 h( l# j5 g" m/ K9 m- @
return EFI_SUCCESS;
7 v) K1 }. U0 g3 r* G9 w5 X! z, w }6 T0 q- W7 ]1 l+ v6 Q' G* ~1 O
/ d& G9 N/ f! |! [+ f Z2 n0 D/ v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& ~8 q& z+ }1 T7 x0 o6 L) @) ]+ _% _/ H
if (EFI_ERROR(Status)) {
/ J/ F# ?; t, d0 m( X Print (L"Could not write file, FileHandle05 err code is %x\n",Status);, V! I3 [# a( {0 a: v
return EFI_SUCCESS;/ {& @ |' z9 ]9 z5 B+ d- r
} # P, b6 I+ }3 b9 Z/ K2 a" J2 l
8 G" `7 i3 h& o+ ^- E& m//---------------------------------------------------------------------------------------------------------------------------------------------------------------
$ E# \: m% d. V( F/ o& P: e" W* P& ~( @6 p6 C) `3 |
Status = FileHandle05->SetPosition(FileHandle05, 18); v) E3 E: B' ]7 Y
if (EFI_ERROR(Status)) {6 j0 L) Z6 `( J1 E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); T3 b3 j G+ T
return EFI_SUCCESS; [1 l8 X4 l3 s5 _$ ^
}
0 z; p M x3 F+ j ) C# ]: E8 f- z; ~2 Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
+ ^0 {" t" m* Q% ?# [ if (EFI_ERROR(Status)) {
1 B; ]) v0 K2 ^$ R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);3 A. q$ r0 _7 Y0 w& ^9 J
return EFI_SUCCESS;
2 i+ R0 _$ M! r$ T; @ } z9 W+ \- W1 h$ n
- t" {7 O0 {7 n9 ^: c- F1 S
FreePool(DestAddr); 4 }) J* ?0 e; r3 g9 f2 l2 ?
+ w. G* {! V3 D/ \ Status = FileHandle05->Close(FileHandle05);
3 ?, U1 C5 d" K if (EFI_ERROR(Status)) {
. p2 G* Y6 b; b5 B' g) p5 [ b6 F$ ?; l Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
* G9 p0 D: v: j5 ]) {/ Y7 n) { return EFI_SUCCESS;4 J) }' ? g Y, V. v
}2 R0 w* ~. E9 G: u y
}0 v4 a$ o( B: @/ d4 F. \ w Print(L"File Name = %s has already been created.\n", FileName);' U9 E1 K* s# _. M& W
. G: Y; e, I/ C
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");! x4 _" V; N0 F, n+ u* H( F: A2 _
WaitForKeyOrReset();! Q% P4 p/ B! W! ~
& j9 O, D/ e: a: O- W" N1 }7 h
return Status;
1 E: h% l' H5 z# }0 J G% u, Z} |
|