|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
5 m/ A: N, ^% i0 O) T * myfileio.c
1 s$ ^& _4 S0 W2 P * Apps
! U" V# ~9 L* `- G9 Y1 u5 Q */( Y- n9 W3 X5 B: A& A$ J
& @, @; L. f" \) J: O+ F3 s* q( ]1 N( Y#include "efi.h"& {$ |7 J$ m) g* F% W; h' ]
#include "efilib.h"
8 A0 i, _9 l: a3 c D* R/ B$ J! O+ A+ x+ f* c" o. V$ Y
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE" l1 L: {) `0 R, I& _. y2 ~
7 x1 T! D- o k/ p5 {static EFI_STATUS WaitForKeyOrReset(VOID). K; H, Q" \" I
{
; v" Q8 i' t) l' m/ @8 H% ]$ C EFI_STATUS Status;
, K H! O, |8 t7 g9 t EFI_INPUT_KEY key;, J h+ m5 [) I0 Q. Q2 e% T
UINTN index;( G" C3 i! b+ m# Y7 g) a$ g$ {1 ]0 @
( o' @% p, \2 |4 M' G7 x" Y7 t5 G for(;;) {
/ n3 g7 j) Z- q, Y# M) A3 \ Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);$ H7 T( Y; A6 n- ~6 R; Q
if (Status == EFI_NOT_READY)
5 G! `2 o% G# O; |# I, N BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
6 e) m% }6 {! K5 P) y, R- f9 J else
2 i# D0 {, D |6 g break;
% e5 ~: L- `$ s7 j }7 G. t* V" e# F7 w5 b- c) ^/ E
if (!EFI_ERROR(Status)) {
. b- m) |+ d& i! B4 d2 B( { if (key.ScanCode == SCAN_ESC). R1 V3 l+ f, @; {( T" N0 o
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);; D7 |) {9 p. K4 T
}
- z# p4 m5 g4 W) ?# `. b' @( `0 E 8 y: u7 i+ ]" D- v
return Status;
$ L( n$ b" B9 o8 E L3 I}
) J1 w5 k+ F0 S
5 f+ l9 R! G$ e- pEFI_STATUS
S: S6 ]' t; D! wEFIAPI2 I. {* K; K3 V( X! I0 L `& v
MyfileioMain (IN EFI_HANDLE ImageHandle,
1 Y7 Q# {$ m8 j0 `, G- \/ l IN EFI_SYSTEM_TABLE *SystemTable)& X4 ^3 v0 d& ]' J% Q- S7 u
{1 }, C2 g R1 e3 d
EFI_STATUS Status;
( Z, M- T. S# d n) A EFI_HANDLE *DestAddr;
' u3 V' Q0 i/ x; d9 r2 Q0 ]% ] EFI_LOADED_IMAGE *FileHandle01;; u0 X6 i* h' S8 M# h& { Y* W
EFI_DEVICE_PATH *FileHandle02; 1 ?* n! ~# I( q; f7 }+ I
EFI_FILE_IO_INTERFACE *FileHandle03;
$ [ L, {2 |* j EFI_FILE *FileHandle04;8 B# v$ _% m% X- D$ e, B) u
EFI_FILE *FileHandle05;
% a6 l# h% B8 p7 c( l CHAR16 *FileName; R8 D7 l! e0 E, o: h5 l; S9 M
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
; c- ], t2 d* f0 H: f UINTN BufferSize = 8, BufferSizeKeySpace = 2;
" i2 i& r$ U* o# U! x, h6 ] int Bit0, Bit1, Key_Space; ( p0 {' u+ _4 e4 N% O
- [9 z* |9 ~+ \: ^3 S) z) ^$ _8 q
FileName = L"NewFile.txt";; \+ b. x- P5 F4 Y; o5 ^- O
Space_Key = L" ";
" l' y+ A$ j) x$ [, f/ {* m
* Z; S6 h* p5 r8 v- U- H BufferA = L"ABCD";' E" a2 m2 E3 ?4 p! v r; ^
BufferB = L"EFGH";
: _) h j6 H9 p BufferC = L"IJKL";
1 z( F2 W: q- O- \# p$ n% [* ~) v4 x- l' v0 s! u* }0 A7 }
Bit0 = 0xff;
# k' ~* o* v! \1 H% u* ]( ? Bit1 = 0xfe;
9 ^8 N2 ?6 K, }! W4 ]
- C! S8 _% J! X+ p; p p1 L Key_Space = 0x0020;
H) k" Q6 \6 `/ T- _' F7 f . s% ~- Y: @; ]# I: B. j
InitializeLib (ImageHandle, SystemTable);
6 n6 V- J% u% _! ~3 ^+ ?
- Q& d1 W9 d$ V( Q DestAddr = AllocatePool (BufferSize); \5 U l/ S- G! {' Q8 s
3 f6 Q+ U( }8 z7 @( Z0 A5 ^1 Q Print (L"Value of Bit0 is %x\n", Bit0);* Q3 U: x* k) A) n# m& y7 q/ f
Print (L"Value of Bit1 is %x\n", Bit1);
& }' B& N2 ]2 d7 J ^9 O2 n; E0 f% C
( ^9 Z' {4 F2 E: b4 C* V2 I2 m * M/ e; o* A2 L* N1 l5 a9 `4 }
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
* S( O% C$ u8 n# \0 w8 `! t+ K if (EFI_ERROR(Status)) {4 y) d* c) O+ M, O) y2 T
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);( K$ F" V, @$ k1 L4 l8 P( P8 x
return EFI_LOAD_ERROR;+ B) e- T5 |$ Z# L
}5 \5 ~9 f, m! o3 W4 n p. B: X' Y4 C
; X8 y; N9 q8 N: F0 }( w5 I# X5 @4 l4 } Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);7 @3 ?- l+ _% c" j
if (EFI_ERROR(Status)) {: L& q, g! g1 B+ A
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status); W: {4 n4 i. }' N, ?4 E
return EFI_LOAD_ERROR;: _8 J! W/ P: [5 c$ [# Z7 G
} 0 s- R B. n, {# p' q2 m
4 p* V) c B" j$ B1 E; S5 ]1 @
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
# w7 n) P& C3 H4 I; _/ F0 v0 D, ?1 W if (EFI_ERROR(Status)) {
5 b0 w7 O4 D. L7 }! | Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
* v- D7 m- t! D+ a! b# [ return EFI_LOAD_ERROR;
8 b6 }7 w; @# O/ Y/ o" l }* z" Z! P3 e( a. |6 K$ X1 F) t) a
: ?9 A* L! ~7 y- q9 j Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);. }$ Y2 V" ?) S: B1 ^: y' w5 O
if (EFI_ERROR(Status)) {
5 Y3 w3 D& |3 l# q) b Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);- u2 r! c" i& [+ ~" A# _2 M
return EFI_LOAD_ERROR;
' S9 Q" l% x5 [7 q } 7 v( G! n5 [" A1 Q: G
) ?( O: i5 @: f, \1 ]; u Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
0 |. D. ` M( g% p- Q$ _ ?* \ if (EFI_ERROR(Status)) {, x! V6 o% B& n, K( M8 {
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
! z; `' _/ F) N3 y' d9 ~3 B return EFI_LOAD_ERROR;
6 y! |6 `1 k4 ~. | }
8 b H8 k, o5 d6 q' s0 j# L
# S: r' j+ x8 n) b5 { Status = FileHandle05->SetPosition(FileHandle05, 0);
2 @3 o: r& @* j' U if (EFI_ERROR(Status)) {3 P1 F4 s, |8 W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 G: _, j% A- a7 I
return EFI_SUCCESS;
5 N2 @8 z {* z7 n2 c0 M }
( g z/ ]6 I0 V$ X$ H* l5 y7 l1 b7 V 8 C/ [9 Z1 E0 g5 u P+ G9 i
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
$ l. O0 \4 C# _5 Y Print (L"File Buffersize is %x\n\n", BufferSize);4 |% }( d" O& Z7 Q9 K8 t
if (EFI_ERROR(Status)) {
& S& ^" `, I0 j/ |% ~ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);0 s3 B: R2 @5 r$ x t3 o- U
return EFI_SUCCESS;7 ^( B7 d0 X; c- N- r, c8 c [' b A
} H" w! k/ {( f% @, X+ b
+ [ C3 l& ~% K0 A
Status = FileHandle05->SetPosition(FileHandle05, 1);
, [, r& f$ W, Y4 R8 k' y8 p! ] if (EFI_ERROR(Status)) {2 R5 t. [& l8 B, n6 i2 v% `1 E- L
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! U! H& Y3 N$ H* |/ Q4 f return EFI_SUCCESS;* E- q+ z& U8 L" c/ ] R1 G
}
* b# U9 z: A: i' B* T
3 {2 i- q; n: K' B7 v' y) Y Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
% p, o1 P5 V2 T Print (L"File Buffersize is %x\n\n", BufferSize);7 X& O/ U2 N, @" v0 e6 i P
if (EFI_ERROR(Status)) {
Z8 f% d& j! Y- W* F8 {, m5 S Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' c# T% S* X$ e& c8 ?
return EFI_SUCCESS;
! O2 Z6 |! _4 Y7 k } # `. z/ t' D0 u
" o. F5 A% y2 b- Y( ?; {
Status = FileHandle05->SetPosition(FileHandle05, 2); 5 I2 X; [5 e, [- i
if (EFI_ERROR(Status)) {
; [% u; V" S3 l. N1 Q, ]7 B7 f% D Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 C1 v, M! Q' Q9 b6 W" b return EFI_SUCCESS;
8 H# \& A9 U1 S6 u( Z/ r, C }
* F7 ?( ^- o( G! w: G
7 z3 g- T5 Z* M M. P, F0 t( o. V Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);8 h) I2 O' U4 ^, B3 p2 n
Print (L"File Buffersize is %x\n\n", BufferSize);( U& Z/ L; y, V M( C0 H
if (EFI_ERROR(Status)) {
6 h8 x, o: a# Z0 v- t/ T Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);9 V( r0 g' R6 {# n( j2 @! g
return EFI_SUCCESS;6 v7 N0 @2 V. Z2 S
}
9 R+ Z, F* h4 v2 G0 o+ r4 B* } & X) ~' D3 O) a- h! ~4 {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>3 A* N# \- c7 B' q. w) G1 Q
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
/ i" q7 q$ B7 Z/ \ Status = FileHandle05->SetPosition(FileHandle05, 2); $ x5 }( L& y( a" g6 [5 }
if (EFI_ERROR(Status)) {
9 o# g9 ?$ b8 ~& A, @4 }0 o u, [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" o3 n X) T2 [1 M! S return EFI_SUCCESS;
) Y- H2 A7 Q7 @: _ }
+ _+ f$ H# _3 s/ X0 t
4 Q& L4 d! o/ S# H4 P Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
; t- D% `) Y7 k& F- N7 @* k if (EFI_ERROR(Status)) {. K- {+ A" c! e; _6 e
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
- r L; z3 U4 [$ ` return EFI_SUCCESS; V2 n7 W* p, v
} $ Z1 Z& O4 t8 W4 t
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
; L5 k( m# g- }; I' Y; v//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
. B7 N: I6 a5 W4 }7 k) O$ x" T O2 o6 s
Status = FileHandle05->SetPosition(FileHandle05, 10);
- q2 {* N9 v3 W7 o& J2 j, P5 @% @ if (EFI_ERROR(Status)) {, s8 r$ [) j4 R! Q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: F. \( Q1 z* K/ l! G
return EFI_SUCCESS;
9 \8 r+ |- G: u6 E7 r) c }; n3 C7 t! I1 F4 u
- [* }% A& \9 j( n" @! ^ W7 O0 f; y Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. K6 }# ]0 L! K& O: C6 C
if (EFI_ERROR(Status)) {, V! R: k. v. k3 f3 O6 J" n
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% s$ l1 { t5 {$ K3 u2 N; O( a return EFI_SUCCESS;
) q" h P) |- r8 f }
+ M( |# i. q8 c1 a9 _8 m
' {. O1 o6 [( A, O1 u Status = FileHandle05->SetPosition(FileHandle05, 12);
2 P* E- j- F" s6 q& i O" O if (EFI_ERROR(Status)) {
0 G! t; n& j& q0 y( v( c( | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ e1 R; r: h1 }6 ~
return EFI_SUCCESS;: j6 {. h" t; G( C& e
}0 G n" D: _, M( R- w9 q
0 r! B/ D1 r9 B6 ^6 v4 H' ?5 `
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& q1 P |& R* Z& _, p9 E. Q
if (EFI_ERROR(Status)) {7 q% V4 \% }% a9 U
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. U# l4 M7 U9 m/ @
return EFI_SUCCESS;
% q) `; H+ m* F. _+ C" u }2 L0 z$ V8 c1 A# \+ Q# k
! ]8 v. ~1 B9 W% ~* G
Status = FileHandle05->SetPosition(FileHandle05, 14); ) m# s1 `6 M; a1 x; r. P' P( y9 i
if (EFI_ERROR(Status)) {
2 k4 ^$ P S: n1 {2 X- n5 q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" m/ e/ S" Z- L8 C j
return EFI_SUCCESS;
+ @) m. p/ G% \, F( @ }
( b4 G& O# ^ Y( h' s) K% s
5 }4 Q+ a7 r/ |" C8 Y# o, X$ z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);% G& @% h! U7 d, a" a! u* ^" l
if (EFI_ERROR(Status)) {2 `6 F* P/ ^8 h1 K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);3 L* Z( {+ m' h, Z* g8 |
return EFI_SUCCESS;, U3 {: W7 J# \1 D d; \4 q
}% k: ]% T j' v1 ^: }% v+ ^
" y n; Z( ~; c
Status = FileHandle05->SetPosition(FileHandle05, 16); 3 h4 `1 B( e6 |7 ?+ a
if (EFI_ERROR(Status)) {7 [) ~; h/ O' J! N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ O/ b. |% G& D! j! l8 H. ^) z3 U return EFI_SUCCESS;
9 M( i( u* D" G7 F+ I7 u$ ? }
( L8 R c4 w& J; I/ R6 j& x k 9 @/ B8 A/ o, L& g
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. z1 I+ c, A+ @9 L/ v9 ^" ]
if (EFI_ERROR(Status)) {, V: x. l+ y4 h0 a9 m
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' M& }( B" @2 b5 A
return EFI_SUCCESS;
( k! G1 [ L" k- }7 i$ S0 j4 f* O }
& b+ W" O9 {3 D/ P" B0 t) ]* X# B2 E& F" P' f [
//---------------------------------------------------------------------------------------------------------------------------------------------------------------8 b8 w+ R0 S+ E) z7 C
T% u- {6 w. I @8 H0 e3 \! h0 p
Status = FileHandle05->SetPosition(FileHandle05, 18);
+ ^8 Y; F1 v$ w1 F, v if (EFI_ERROR(Status)) {
0 D1 L( G' o; `9 h6 `7 ~ v Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 ]: g8 @/ D" D0 o; g2 z0 B
return EFI_SUCCESS;# t! K! Z( [ p8 M! |7 @
}, w( p6 j/ r' ~4 e8 X$ g* M' L+ v$ o
) n9 x8 y9 s) F$ z+ _( S' l N3 R Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
( y7 \/ d$ j; x: ` if (EFI_ERROR(Status)) {" n) N" t; t) n6 g# }+ E, @% Y
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
& k& {2 [" }, ~4 o+ {$ t u return EFI_SUCCESS;
; J4 e) x3 v4 a* x" w- D; Z }
6 t+ X# m+ E" q- j5 d' X 5 V3 I! b7 k( }
FreePool(DestAddr);
# T3 `1 s% }& _' Z( A# \/ C2 x5 Y! T* l1 J$ X* B
Status = FileHandle05->Close(FileHandle05);* l K [, b7 s$ H- y9 k
if (EFI_ERROR(Status)) {- D/ u3 a$ I9 a- q8 `! \
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
8 p; k% W& T9 Q% K4 o+ f* k return EFI_SUCCESS;: g: B: H1 y: z0 W1 F( }
}* l% n: `2 M$ W8 n1 l/ n
# {$ Q- Z* v( T- Z6 W8 N. ], n J
Print(L"File Name = %s has already been created.\n", FileName);, \7 c" f5 z; n# X8 x6 |# ~
& P! `' B3 q' w Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
, m0 {5 p# W1 Z+ r' x/ U! h7 M WaitForKeyOrReset();; y6 h2 [$ X& f& q+ D
& `, U: G$ Y/ |0 C) _1 Z
return Status;
( Z+ k! ]; G# w4 C9 s3 x' o3 O' }} |
|