|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*& Q2 E6 F' \8 w: w7 \
* myfileio.c; ?1 T i7 u+ K: V8 W& e
* Apps9 D/ z j- @6 X$ o+ k+ e y5 `! L
*/
# p: @# y$ F' w& W2 U8 ~' O0 v" G
#include "efi.h"( W. i' m' q9 |- `7 T$ @: g; {
#include "efilib.h"* w2 J) H b. \! z- z% T' ]
! w) U8 \, a& i" @5 r8 @2 ?" V) A0 y
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE. v6 b% p( [+ X
9 X4 C( M5 E2 @1 V
static EFI_STATUS WaitForKeyOrReset(VOID)
* v9 _. ?1 o g6 `' Y" M' |{9 L* ^4 u, A9 {; k. w" o
EFI_STATUS Status;
# j; T* F. f! i& C% D+ j' r EFI_INPUT_KEY key;
8 ^2 {2 m, d" C! j9 ~ UINTN index;: i! v1 @7 K0 ` M% J$ @! W
6 |. j; ?: _, A( k: s* p for(;;) {4 @0 R' b5 d u, c# G* |7 {- m
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);& L& h2 i& h8 m, b/ }5 x1 ^2 @
if (Status == EFI_NOT_READY)7 t! B2 C/ d$ K6 D+ k6 L7 g
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
3 f7 Q. f7 H P2 @5 v else
( H& }$ `; H6 H! F+ L. m% k break;
) g8 V* t7 z) t# Y }# D, q ?- I( j8 ~% } k2 W: x! t0 {
if (!EFI_ERROR(Status)) {. Z2 f& n& x$ w) Y+ |. l
if (key.ScanCode == SCAN_ESC)5 k3 ^; [, q5 k+ p$ v- i
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);9 R0 l; t) m9 V: P/ E) a$ k
}
; F* s$ s# M+ j. E t9 A 1 e( f+ C' P! q. \; F3 f3 t# f
return Status;; B6 J1 T0 s; [3 V* Q7 x) U7 F! W( C
}/ v. W K3 x) h6 ]
( w1 _( w+ B uEFI_STATUS% V; j( d& ?1 \( d! T! M
EFIAPI0 j, s' V9 J+ ]7 b; ^, M
MyfileioMain (IN EFI_HANDLE ImageHandle,
- O7 J" Y' T/ l1 M6 k# F IN EFI_SYSTEM_TABLE *SystemTable)6 S6 q, \: T5 S
{
& g2 V, y/ h, v! y1 y8 ] EFI_STATUS Status;
) d/ N+ _5 y" Y& x1 @4 Y EFI_HANDLE *DestAddr;
' ~$ z5 w' A6 z% e- C) F9 v% f+ L5 T EFI_LOADED_IMAGE *FileHandle01;% `1 z0 f$ }! q) ?) |
EFI_DEVICE_PATH *FileHandle02;
0 c. a& Q- L& d o2 \% @- r& N/ N EFI_FILE_IO_INTERFACE *FileHandle03;2 w9 I* E( e, Y4 A
EFI_FILE *FileHandle04;6 i, M. o1 Q- }* N7 ~0 i
EFI_FILE *FileHandle05;: L- P+ u8 ^( E# T( {- x; r+ T
CHAR16 *FileName;! @+ D3 N/ z4 y2 m. d+ y" ?: o+ Y4 `
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
$ `7 P0 Z4 O2 @5 `0 V UINTN BufferSize = 8, BufferSizeKeySpace = 2;
3 D$ m( O' _0 n1 w int Bit0, Bit1, Key_Space; $ h" O- X" X5 D* R1 ~
2 q! e* ^: ?6 s" S FileName = L"NewFile.txt";1 F# m' A. H& v* x; K) M2 Z E
Space_Key = L" ";
9 Y+ C: h/ F7 W 8 \) p9 g6 H+ l1 K6 g
BufferA = L"ABCD";
* s/ n: E' I. @$ Y9 n BufferB = L"EFGH";7 c" a2 ^; |5 h
BufferC = L"IJKL";
2 q( H8 C6 s4 `, u
; r% C! H$ F/ n' i Bit0 = 0xff;
$ o% X F0 b* H, M% W# d1 e+ L Bit1 = 0xfe;. C- g' T) a1 w3 L) y, a
; V2 Q( O1 G/ A( w1 j Key_Space = 0x0020;
5 _2 X- [4 M- n# R/ x' f# x$ ~* t
+ \5 t3 \2 G1 q7 {- f4 C5 T% p InitializeLib (ImageHandle, SystemTable); 3 `' Q8 ^! _8 \7 E
) q s9 J6 T8 m. W DestAddr = AllocatePool (BufferSize); 4 I$ h# u5 g8 [4 x. b
0 ~% n4 ^) q" W
Print (L"Value of Bit0 is %x\n", Bit0);) U' g( G9 K" N* _
Print (L"Value of Bit1 is %x\n", Bit1);
* {7 n' k0 w4 ^3 y9 H' a m. E8 G. r
9 |- q/ j: A3 L9 x: `% s! v
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
c% O$ z' N6 X2 m8 H' F if (EFI_ERROR(Status)) {
) l- U/ g# G* v3 T$ F Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
# D- O! b; z6 m9 N return EFI_LOAD_ERROR;
0 n: Z G$ S" f. v% @2 x }
6 r6 O8 b7 R; r/ W$ J' b' r5 I% Q3 p7 J$ G) e7 W( ]0 d- a
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);0 S, [/ o7 W a1 Z7 p3 C% s0 g
if (EFI_ERROR(Status)) {
0 u3 S/ g9 A1 S2 Z0 F7 f4 m K Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
7 y* S3 c0 \ \0 O6 c% B return EFI_LOAD_ERROR;
8 a, i9 G# @" L6 t" | } ( F2 z4 i' ]. I0 G8 y. N6 R
) y7 Y4 @0 w0 G; w Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
a6 K) e( D) L4 y3 O8 y if (EFI_ERROR(Status)) {+ w7 w7 Q4 z5 w, T( Z7 ~
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
% j4 W' e4 J! d% ^- s- } \8 w return EFI_LOAD_ERROR;& M/ i" M: }$ [ H- o3 D7 N
}
; x, u. k6 @# C- ?: E5 {- Y
0 K3 g6 e& v) F& M# w8 g8 V# w. Y Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);; `" h6 W; A6 s# w' I( `- c
if (EFI_ERROR(Status)) {- J- V( W% d4 b1 F% c& e9 z) j
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);4 |/ B! y. P- f% |# B' e
return EFI_LOAD_ERROR;/ s( n% [9 v* @8 x3 C- d/ j( B) v; X
} * J9 `( `, ~6 j: R" l
4 a. c5 S7 o" X9 f; k* _% Q
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
" }$ Z) J7 }# g5 v* q5 Y if (EFI_ERROR(Status)) {
) V1 x9 h& c+ H, ^' r0 J Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
) A+ U* Q. ? v- F* N return EFI_LOAD_ERROR;
! L+ `" \: N6 l% s3 @- t- B }- i( {) R# s2 ~$ A0 G3 ?+ V
9 K2 j n8 [0 g( e9 x* @* b Status = FileHandle05->SetPosition(FileHandle05, 0); 0 U: }, b3 ]3 S' _9 H7 S+ [
if (EFI_ERROR(Status)) {, `2 O0 a8 u/ f; a6 I4 _/ f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 f) g: B K# @
return EFI_SUCCESS;
, F# M& _- r5 v3 R1 | }
2 R3 j$ W5 E. p ' X3 k' Q' Z0 E& u$ Q
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);/ y" a! `3 \& G! R" v: W% _
Print (L"File Buffersize is %x\n\n", BufferSize);
' T8 g1 _7 u2 `6 L; ^- ? if (EFI_ERROR(Status)) {5 h4 D( g* A* g( q3 e
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' w: D3 K9 Z; R: C+ F/ [' Y3 E% C2 A
return EFI_SUCCESS;
. N6 N: G: v# v- J7 g } % b6 `6 g# h0 l7 a
! G7 C+ c% _: w0 E! @. P. h% f Status = FileHandle05->SetPosition(FileHandle05, 1);
# j/ a+ s6 u$ J4 o( [. Q& B if (EFI_ERROR(Status)) {( ~) h" Y \! S& ?/ @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
W+ X% L4 J+ W return EFI_SUCCESS;4 r9 z% y3 P8 i+ }$ q1 E3 o
}$ r% r* J$ c4 t
9 {2 K( p5 D t+ d$ p, N4 q Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
! C) q8 T2 W0 `6 _& ]% x9 U2 G Print (L"File Buffersize is %x\n\n", BufferSize);
& S2 A- Y8 {0 t7 w2 v2 { if (EFI_ERROR(Status)) {- r" K: W: U" l% n( N
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 o/ m$ O$ O7 i! O: b w5 M2 s) J. B return EFI_SUCCESS;
2 s6 K @; e6 A4 m( z2 D }
9 d4 C8 e. Z- d: c# a+ R 5 q& i) `6 G* ?) O+ Y7 `, @; i
Status = FileHandle05->SetPosition(FileHandle05, 2); : }% {) I/ K. t: |2 s- w- {1 T2 [
if (EFI_ERROR(Status)) {
. e( p8 Q Z9 U: U. { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; s3 |( n8 l" ]( r; W1 ^( c
return EFI_SUCCESS;' x6 |/ |/ Y8 \) t% H$ l9 U
}6 B5 O! W6 C3 h8 s: B- I/ q" i7 p
/ @' ?. D/ X7 }8 e) [! G& o
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);0 O# b5 Z0 u$ }3 N% ~/ G4 F; t
Print (L"File Buffersize is %x\n\n", BufferSize);
. S% N# Q, |2 {% q& x/ c. ~5 ` if (EFI_ERROR(Status)) {
J, D0 ^; ]8 W- ]1 P: i0 ?) d Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);# ~" Y0 i A) U# s5 m4 W2 M- J8 p/ n
return EFI_SUCCESS;
, s/ f- _ J$ l( G. ` }
4 y% b0 x) R1 O$ m$ `9 H* |/ ` + g& P3 P P& U! O7 \9 s! u
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>& A$ N6 M2 V) T& d
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
1 J9 H9 H; D1 z$ i$ p Status = FileHandle05->SetPosition(FileHandle05, 2); & t% e3 |* I" A# H
if (EFI_ERROR(Status)) {
( [# B z) d; C& G0 s- x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ ?- m1 I. a: E
return EFI_SUCCESS;& L. Q3 Y# Z' L2 A) Q
}2 G& v: I N/ ]; }
5 x! J' [9 j- r9 w# g) d Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);7 k5 g% L$ B( t7 N, G
if (EFI_ERROR(Status)) {
/ x; z' C& b1 o5 x1 ^. s8 f" i Print (L"Could not read file, FileHandle05 err code is %x\n",Status);2 [# E: e4 M5 B, Z! _" O' o' ?6 D; y
return EFI_SUCCESS;
7 m) j2 S+ H$ A. W, s4 p p } ' r$ T6 [ k# _/ C
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);* u. |' \2 \3 S- s1 c9 t$ o) D0 ]/ Y
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
& _7 R) b! e& N1 f3 |* e
" I; n- P' I9 f3 o4 f' f Status = FileHandle05->SetPosition(FileHandle05, 10);
C; Q$ ~" x! n! I) y1 n9 x4 \. W if (EFI_ERROR(Status)) {; e) X+ r/ q. I2 h& o3 e4 _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! i# M- Q1 g; [! m return EFI_SUCCESS;
4 M( h" K4 u0 Q; Y6 M+ k: L1 a }- D' b5 n9 q- j6 r( E7 f
4 A Y: F$ V3 P6 \ t7 ^0 g
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% q, W( |9 q; H$ K4 }& Q if (EFI_ERROR(Status)) {8 }/ m p! \( I5 C& u
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
; V: D( Z% Y, q: h. {/ b( a& J return EFI_SUCCESS;
) G0 L" s# Z8 S }
2 T1 d3 s7 N% ]* R
V S+ E3 E+ Q) T Status = FileHandle05->SetPosition(FileHandle05, 12);
( f2 a) y/ b' S- ]" [& r, T if (EFI_ERROR(Status)) {9 A, U6 l* @! H* h2 I3 x. \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" y0 N1 y5 J) l0 S5 Q N3 ~: f return EFI_SUCCESS;: y0 w& I$ y. v% r- N) e$ j
}: T. a' y/ u o# D
( \6 E$ ~: K9 N) i7 U4 X8 H Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);9 c% N1 O% B$ b1 q' p6 o
if (EFI_ERROR(Status)) {
; w, \: V. J8 t: L' [9 C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 |5 S1 q4 e" ^* F
return EFI_SUCCESS;
" B6 R* I+ E' l3 W, i }
# l3 N+ U/ ]( t- x: W# f" e- r E/ ]2 E
Status = FileHandle05->SetPosition(FileHandle05, 14);
3 h5 }: N* w4 K" q: K: J if (EFI_ERROR(Status)) {
/ k, E. c) F8 |3 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; ?' n) F+ ^9 J return EFI_SUCCESS;+ G' G/ l1 W5 K J2 |/ F
}1 u( m& u1 ]$ G( R; ~
( @2 P5 A3 ^3 a2 a6 d% G( e Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; R0 w# }: e F, n, E+ L- z9 q1 N& z if (EFI_ERROR(Status)) {
# l. G3 ]2 ?4 o) G5 r Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 R) p6 J5 q5 y0 N return EFI_SUCCESS;( g3 ^' T- D+ V
}* A3 r8 A1 t* K+ n! }% _+ S
; j6 {- `! K! P8 e3 w7 L- z
Status = FileHandle05->SetPosition(FileHandle05, 16); # Y- C, m. g* ^! w" A) _) I6 }2 u+ p
if (EFI_ERROR(Status)) {
9 m( `4 s# E' ]8 f Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, j$ k0 e# |5 T4 x1 b0 M
return EFI_SUCCESS;6 i+ Y# K- K ^& d8 I* Y! U' u
}1 f7 k# p$ Z) v* X3 l+ U; X
) y5 h5 E V1 z L; c9 Z3 _ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; y5 k' a9 \9 `3 r7 a9 D% e; N& M if (EFI_ERROR(Status)) {' [4 E" R/ X* s h
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: ?: e# {) `+ p2 D" U
return EFI_SUCCESS;
0 M, J" L2 ?3 j5 C# @- V4 N } 2 U( ~6 A" t8 {5 N9 _/ z
& k0 @; l7 d9 Z+ }9 u& d8 Y' Z//---------------------------------------------------------------------------------------------------------------------------------------------------------------
' n7 [7 b) R. f' d/ z* {4 J9 y. b8 s3 E' h
Status = FileHandle05->SetPosition(FileHandle05, 18);
2 ?! R- J+ K B" J& f if (EFI_ERROR(Status)) {
* w* E2 f' _1 A% y1 Y! { Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 K/ a8 ^3 v/ p0 r" Q2 V return EFI_SUCCESS;. B5 V8 i3 s) {' s
}5 U! @, S- F4 [6 T& r K( c$ H
# U# p- k5 ^3 q. p* k Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
* n1 [+ N: z! ?$ p: X if (EFI_ERROR(Status)) {9 n- r0 h# n' n4 T* M Z# f
Print (L"Could not write file, FileHandle05 err code is %x\n",Status); h1 W" ]+ }* B/ u8 T4 i Y
return EFI_SUCCESS;* a# j% f; ]8 I5 r0 s6 O' w. M, C
} & o4 `9 g& J) |# F) G2 ?5 q1 o7 ?2 z
' a# c: ~/ ]! Y# q
FreePool(DestAddr); - s/ {* g- j/ a5 @
7 ~8 h0 M; {: O0 T: @) T3 w Status = FileHandle05->Close(FileHandle05);
3 F0 u/ o; i: `( V1 Y if (EFI_ERROR(Status)) {
9 A- {" b( r; b% ^ Print (L"Could not close file, FileHandle05 err code is %x\n",Status);% E/ y" A2 E: [$ {: d+ A, W$ Y: R4 E
return EFI_SUCCESS;
' [' f) }* I) n }' b2 V9 `% l+ x: g
# p8 T7 {; R) ?" @- H Print(L"File Name = %s has already been created.\n", FileName);
# g9 |# z( A* @, Z* ?' n* u% e( L. h1 S" i2 u7 c. n% x
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
* F* D) W( ~ i WaitForKeyOrReset(); U* R8 t0 l, j( H$ Z
. O0 Q9 L5 D6 B4 c& b
return Status;$ w0 |7 `# B8 a9 k' B5 H
} |
|