|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
$ e! r# z2 ^. s * myfileio.c
$ W9 o& t5 X/ ]2 t; I * Apps
$ f& a: [/ H1 H */
( f+ ?! h8 I& Z& j" J6 d$ y) G) |' l. }
#include "efi.h"+ X0 ?( Z2 s6 @
#include "efilib.h"- T& j* M! i3 W; l5 f# b4 O6 Z3 G$ @
3 c3 s9 y, T3 ^#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE9 P% {. Q8 x0 Q7 }
$ ?2 { O0 P' W$ M" {, I- Pstatic EFI_STATUS WaitForKeyOrReset(VOID)) K2 Q* ^6 P9 _5 X! y, k
{5 G8 |7 ^) m0 p/ f- k" ^% K
EFI_STATUS Status;
* H' r' p; ^9 [' F& G EFI_INPUT_KEY key;
0 y* ], _, h' b8 M ^1 U UINTN index;0 r8 u N) J* t8 _& R- d2 E5 H
, n1 R2 m7 C$ [6 A2 f! G
for(;;) {1 V+ v0 R. ~7 w' o4 h
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);# _1 \8 N( {" }8 h- Z+ g V
if (Status == EFI_NOT_READY)- w6 ^$ z: z/ a; n# S- T; {+ a
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);, W6 q' t+ W* y8 V9 k6 f, Q/ n
else, S( f7 E* m) ~+ W' Q& z& _
break;
# A- h7 T! _) {6 e2 h }
! F8 D5 s3 L* @$ E6 H4 d if (!EFI_ERROR(Status)) {
* Z/ Y/ i/ @ D2 m1 W$ n if (key.ScanCode == SCAN_ESC)7 e. F1 F2 y) `, M. X0 ]1 u
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);1 F2 a2 t) c: k& r8 s3 H" s
}# c, h& J3 J- I. ^8 d! g
! ~+ t1 Y `6 B/ v. k% P6 W# t( A
return Status;
. N$ j5 d _; @. ]8 m Z8 _ n}6 d$ ]* R1 N( F+ ]2 ^8 @
% U' [2 s' M/ I' _, ], jEFI_STATUS y8 @$ ~( T5 p) q4 {
EFIAPI, p! V' w: C) }3 @1 t7 b |
MyfileioMain (IN EFI_HANDLE ImageHandle,
# h3 I/ z M/ Z+ G1 A IN EFI_SYSTEM_TABLE *SystemTable)
6 D5 z! |! g. J* ]- T{4 t$ z' ?5 w7 T* D" D6 K- X, x
EFI_STATUS Status;; z7 `6 T% X& u6 H, F
EFI_HANDLE *DestAddr;
/ ^3 w8 U3 z; m+ \: M EFI_LOADED_IMAGE *FileHandle01;
9 ^! K4 w+ V! }( X EFI_DEVICE_PATH *FileHandle02;
4 i; Y; X" @/ a1 E8 u EFI_FILE_IO_INTERFACE *FileHandle03;% Z# x C5 `6 h
EFI_FILE *FileHandle04;' U3 O. Y: c, g; v5 \" \3 ~
EFI_FILE *FileHandle05;% w7 s# b% O* H- Q
CHAR16 *FileName;
6 o/ N% O3 W d4 ~0 p CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
( ? m- f m: E$ L UINTN BufferSize = 8, BufferSizeKeySpace = 2;$ o, Y) D4 Y& `- B# Z, V
int Bit0, Bit1, Key_Space;
' X5 e7 A# O5 Y
( ~9 ^5 H/ V$ G& v, ? FileName = L"NewFile.txt";7 X' j3 s L* x* x4 o) q; u+ K
Space_Key = L" ";# O* r( A8 ]+ V5 |. |) ^' a' K
( \' T3 Y' ?+ m" ? BufferA = L"ABCD";
& o0 V$ G1 J: ^0 f1 T O BufferB = L"EFGH";9 ?* W* u# Z" K- F3 w
BufferC = L"IJKL";
5 S) ]9 ~. T$ W. L8 D* @, z# T4 {* w9 A* F: q+ Q3 F
Bit0 = 0xff;
, m) o9 @6 l; N" S Bit1 = 0xfe;
# g5 C7 ^+ K7 K' [7 A % \1 v e4 A; n. e+ l
Key_Space = 0x0020;
; H' ?% p \3 f7 H0 b+ L+ M5 e 3 w) \% o" p! l9 E% ]" O
InitializeLib (ImageHandle, SystemTable);
" |0 I: S. T8 `& }
$ P) \- B7 ?: N! P9 I DestAddr = AllocatePool (BufferSize); & g. ^* ~5 q4 @' g8 U5 N- D# W
1 r( c2 b* j: \- }2 Q Print (L"Value of Bit0 is %x\n", Bit0);
- H8 ]: T# k$ ~& Q# m; o/ C& \2 V Print (L"Value of Bit1 is %x\n", Bit1);
1 ~; ~2 Q7 E. Y8 f$ H3 [% f/ L9 b. F% {/ e, q
. \" M* V* M. F2 t: Q* ^* A s Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
) F1 A% l& J/ M! m+ R6 N if (EFI_ERROR(Status)) {, ^1 `0 ?$ h0 _7 n1 X% v1 z/ m
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
3 ?4 K- F0 ^7 P+ A4 r6 @ return EFI_LOAD_ERROR;
m7 S! `. c/ g1 M9 [- c. C; Z }6 p: h3 e' @% K' I' l
2 _) e. g/ i) W8 l
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);: V# { l1 S# j+ e% s8 h+ Y
if (EFI_ERROR(Status)) {( H7 W3 i7 V, e0 q8 j7 d
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);2 r# J; h4 P* r9 A! u
return EFI_LOAD_ERROR;
& ]2 k$ K6 s* y9 [$ {) X } 3 p' T. C; i7 w" H: I
- R. R! o+ a- A. k' o. r2 M. G6 o
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
- C$ l' {7 t) f* E3 h" x* a+ M if (EFI_ERROR(Status)) {3 D) Z3 B. w% x! F* [
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);3 `6 a5 S% @, T5 D
return EFI_LOAD_ERROR;
, K/ M& }8 k8 u, x( @. O# ~1 Y }6 @' x1 z/ S" \2 k2 M
0 u' [5 U6 X1 E Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
& l) c/ s# c5 x- f' f if (EFI_ERROR(Status)) { h8 v! T( t7 S' T7 \0 {' E7 C
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);% w4 W# d/ b. Y, M: s0 ~8 m* F
return EFI_LOAD_ERROR;) l" |- y, u3 i4 _7 B! V+ y3 H
} , o" U/ Q5 r$ @9 \$ @( d- J
) T; r. l7 n- x
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);7 v% T9 D% G) Z% { v: l
if (EFI_ERROR(Status)) {
( |6 o: z+ X& D3 l/ ]! a Print (L"Could not open file, FileHandle05 err code is %x\n",Status);- O* r7 _: I: {* ~. h
return EFI_LOAD_ERROR;
6 {' w _% \- {( {. W: V$ w$ A }5 a" @, K8 y) G; B0 p
/ ]: m' {- ^: e- k6 y9 q7 U6 Z Status = FileHandle05->SetPosition(FileHandle05, 0);
" _: n+ @9 B! u3 M h if (EFI_ERROR(Status)) {
% w! u5 z4 R q' K1 H0 I/ k+ U7 | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 |. v( v8 X) R( {6 a
return EFI_SUCCESS;
2 P2 X2 x& h% v4 q! O1 R; g1 ~ }1 ^ {$ X- {, o$ l
4 F$ P: P( g5 e( w" } Q$ S Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);) i% g7 V7 F) c f$ B9 c [" Y
Print (L"File Buffersize is %x\n\n", BufferSize); K5 Z1 z* V: J+ n3 _
if (EFI_ERROR(Status)) {" ], O u! Y9 ^+ Z; F* p0 I
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
" c' G7 N" q; j; ~; J$ [ return EFI_SUCCESS;$ e/ v; `3 j5 a, r* i: D( ]
} ! I ?: \5 c& {# @# L$ {
$ G& B- {1 ]) c
Status = FileHandle05->SetPosition(FileHandle05, 1); ! r- O2 Z8 W7 s: @3 M
if (EFI_ERROR(Status)) {$ N b& P6 D* k8 J) X
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ V( T x- x v0 H9 F; T return EFI_SUCCESS;
) n6 m9 L) O$ I& r }
# t! n# d3 O' b+ S
3 t6 R3 X! x! i r1 Q+ l* f Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);) j/ ~. n* b/ \! O
Print (L"File Buffersize is %x\n\n", BufferSize);$ \+ |+ S; L3 a& u: ^/ L+ n
if (EFI_ERROR(Status)) {
0 m' [* J* M. P/ r! P- W% { Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
* C& h# B/ z" S( G" S+ S return EFI_SUCCESS;
$ e% K/ U# I0 z* r- K( M5 E6 R } . E% J( N: a5 d5 F3 f( i9 \
1 u/ P' F% }) r( O2 W! H% d Status = FileHandle05->SetPosition(FileHandle05, 2); 7 W5 M% D4 V0 b1 I! i( p# _
if (EFI_ERROR(Status)) {
6 \, y1 c, u9 S* L/ R! I Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ r/ t$ j t) _+ [% Y return EFI_SUCCESS;! j* a" F" D) F$ X% @/ T P" b4 y
}" {/ x" y* P. F7 k# o/ t7 M
5 \+ K( s& R6 ]
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
0 R2 k) H0 U7 t8 \" V; L Print (L"File Buffersize is %x\n\n", BufferSize);
) e' z3 I1 M& p1 R! N if (EFI_ERROR(Status)) {
# h3 ?3 N+ o' q5 A Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);5 I! H/ @4 `: }: d& r% r
return EFI_SUCCESS;+ D3 v n/ q7 I2 x. e; n
} / n5 b, [6 J' y: W
0 P& @- p/ W; ^* V. r. S4 r//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
1 }2 L- G/ U8 _& U Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
$ y1 C* y$ Q* @& D9 g5 N Status = FileHandle05->SetPosition(FileHandle05, 2); . t c A4 @8 P( E! U$ C) `
if (EFI_ERROR(Status)) {
1 q7 a* N5 @& S+ M3 \( o; g- R7 a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. W/ i" w; t5 [( i' | return EFI_SUCCESS;
8 e: ^. h7 W2 w& W }
! D# D) a! X7 y: |+ c/ h5 q# M A& M0 I4 f: x, c0 H
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);3 e# ^& ` n5 ]# ~7 ~$ }, s% C
if (EFI_ERROR(Status)) {
/ E( h9 `" S. e) a% K0 o/ w1 Z Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
" P3 F) h2 m. S& y8 v+ w; l1 E/ I return EFI_SUCCESS;1 }5 V& S$ ]5 q0 d$ C8 w1 L/ H
}
8 s5 {* A$ Y$ b! r7 l+ o$ q Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);% _2 T' I1 W; {% S& |/ ?+ ?
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<9 E3 n9 p w/ X% Y2 B
8 {/ H/ ]& _- U8 A& j Status = FileHandle05->SetPosition(FileHandle05, 10); - q% y) ` }/ x! v9 k6 G [" x2 ^8 O
if (EFI_ERROR(Status)) {; x& O& J. h* F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 E1 z8 _6 P. H' E0 V. ^' m9 {4 v return EFI_SUCCESS;- c- }- L- L( A, H) m
}: W8 W! F" R; _5 W/ S' M% K8 J% V
: Q) a! x: p8 ?5 [ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);7 u: ]2 Y$ L1 U
if (EFI_ERROR(Status)) {
6 N# R7 V7 I# k3 V- _) u: p Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
7 L0 A& o/ O8 G; M return EFI_SUCCESS;" b* S+ g# j! O6 v
}
. d' s. J7 \( Q' S! G) q/ p$ n- U
Status = FileHandle05->SetPosition(FileHandle05, 12);
" ?, B1 X) F1 ` if (EFI_ERROR(Status)) {
1 h4 g" b/ k8 g4 ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 w9 w2 r3 Z, l( E
return EFI_SUCCESS;
8 y7 a% l) R6 N/ {: `9 L: x }
6 Z1 G) N: m+ P9 }# b0 w+ d- | j$ A0 n8 e # g, ?$ N& W) ~/ K9 M5 a
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# v |/ W2 j& R1 G4 `/ C. ? if (EFI_ERROR(Status)) {
6 G) o: ]8 d4 A2 g9 y- s3 \ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: a/ R9 @2 o& q- P* e return EFI_SUCCESS;5 W1 ]; Q$ x# m4 J& D
}
* X! J3 L7 J- L4 N. L7 n; T# ^4 F% P/ T
Status = FileHandle05->SetPosition(FileHandle05, 14);
2 J+ U- {- q) B if (EFI_ERROR(Status)) {
4 @$ H. P6 S; o. A2 v+ T5 p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# h- {" @; w0 b2 p return EFI_SUCCESS; A( Q' E$ Z m3 K
}
' V+ d3 P/ C6 w8 v ' B" u1 V: F' O. S% ]: f
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( f0 L D) ^0 p0 F
if (EFI_ERROR(Status)) {
( Q+ a; _/ ^0 z0 D5 e Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 s% I/ H# F, @# }% ^
return EFI_SUCCESS;8 e. Z: a: N- k E0 H/ e5 m
}8 w8 h; J- N) l' P" ~
* m! T6 |, b2 b; ?4 G, l
Status = FileHandle05->SetPosition(FileHandle05, 16); ' A+ i; K; ^# t
if (EFI_ERROR(Status)) {: a# t g0 T( \2 [5 a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) _, ?+ ~6 t8 X
return EFI_SUCCESS;4 X+ c( x' @1 e0 Z: c9 m4 ^
}/ b6 ~2 ?- {9 K) n
" O( C0 Q8 \3 Y: K( f Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 o/ f) w8 b2 [/ M# o# r if (EFI_ERROR(Status)) {
* ~9 m4 ?- j8 P- I2 Q8 ?2 ?0 h6 K Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: Z( R1 D4 P! |* E+ z) |0 K return EFI_SUCCESS;8 c l6 \4 p3 h, b, x2 \+ V( c
}
# |: ^% G ]0 D
4 F( Q2 `) W8 k//---------------------------------------------------------------------------------------------------------------------------------------------------------------7 j; }: i C/ H& ~! y, N; S
3 _, l) Q: q; n5 @6 G! Q8 w
Status = FileHandle05->SetPosition(FileHandle05, 18); # ]; V% W" H# g! t/ e5 ~& B2 R: f, p5 C
if (EFI_ERROR(Status)) {
0 D/ q( ]% q; Q6 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# g/ C3 |' K6 z7 c+ m5 Y K6 {
return EFI_SUCCESS;
: O9 v0 {0 o5 e) d1 o( O6 C+ n }# `3 u5 p& r* ?2 \
/ J, d- m0 q/ ?5 a6 u Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);+ t9 W+ f6 r% w3 W, S
if (EFI_ERROR(Status)) {6 k1 t' e& @! {2 |' v7 W) |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& g9 I3 L) @8 z2 \1 P |
return EFI_SUCCESS;6 N& }# D3 B' t- s4 u0 F! @7 _3 i3 o
} * A8 H/ @) M' h$ [" C
! T/ H" N4 ]/ `3 W) w, R( { FreePool(DestAddr);
( Y% H6 {9 H6 j
8 B" j& ]0 A) T ^9 D* Z: e Status = FileHandle05->Close(FileHandle05);# J) T/ O# c/ h! e) L
if (EFI_ERROR(Status)) {
, B; }7 M! H1 c: Y Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
8 T8 l8 `' I3 b return EFI_SUCCESS;# Z6 {' p% l" _; N
}2 Y2 {9 Z; K$ ?
6 ]3 [1 B5 n6 K Print(L"File Name = %s has already been created.\n", FileName);
$ k1 k6 p: \4 Q# D# q9 Z
9 S3 b4 f: n1 ?+ e4 Z% q M2 K7 X Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");1 S0 a; a. }: M4 [$ f
WaitForKeyOrReset();) L' h4 A! i4 J' S
2 z5 N( L! \, L) i/ v9 Z return Status;0 C1 o# s3 z& Y. e' v, _" | O+ [6 D
} |
|