|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*% N2 ~+ {3 z( a, n, U, q
* myfileio.c
/ F P3 n( S6 d * Apps
/ l: `" `( J! Z0 H: t; R) e */ P" m) w5 j6 W/ ~: \+ N
* ?* E( b% L" [* ?#include "efi.h"
# A* T& o* {4 f3 D3 U" W$ q. q#include "efilib.h"( ^6 V. S P9 ~9 W* i$ c6 }
6 @( x! \3 f) D5 y#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE6 w6 W4 f5 L+ U% s& w6 L; [* j
* h" p2 A. R0 A4 ^8 o6 \( m8 _* b
static EFI_STATUS WaitForKeyOrReset(VOID), b+ i$ ~3 g, ]9 K. `1 B% Y
{ _! `9 v/ J: C; d
EFI_STATUS Status;8 i& ~, T( a; y/ w4 w! b
EFI_INPUT_KEY key; R1 h- I% C- j6 x
UINTN index;0 d, @) a3 N3 k& `
2 E' R; G& K5 x
for(;;) {( G9 o6 u6 Q# Q0 _
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
7 }/ _$ \1 p- }; H9 k if (Status == EFI_NOT_READY)) B8 z2 X1 V* a2 Z) p
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);; R2 Q) `% q4 o# I5 \# e
else& H% |9 }# X) o7 G3 n* R$ q# U
break;$ ~ m, e! j M" h& n
}. q8 N6 ^) h4 s, S
if (!EFI_ERROR(Status)) {
4 F/ Q: a2 { x" X# B if (key.ScanCode == SCAN_ESC)* F9 y: e5 m, g) X5 X
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);; V9 A" }& q$ S& }4 U0 B: n
}
2 O" P0 u# X; H" a" U3 s
* p$ ] n* w0 R+ g2 P return Status;0 _: `5 m/ m7 u; E* U R
}- R0 Q% F; q' |& }" X" ?- ~6 Z
6 X' w& ?" ~/ ], X
EFI_STATUS0 [# P/ [0 I- n' N( ?6 b
EFIAPI
/ B) {2 K' P1 Z$ ^5 WMyfileioMain (IN EFI_HANDLE ImageHandle,2 @5 p) A. ?8 A9 W# f2 {
IN EFI_SYSTEM_TABLE *SystemTable)
& W0 ~7 ]/ J5 j0 r) K{
* n, u2 r! ~( \/ _+ T" L" O EFI_STATUS Status;
4 E+ G0 q8 M* p& m8 k! F EFI_HANDLE *DestAddr; * S& k. X* x3 Q
EFI_LOADED_IMAGE *FileHandle01;. O1 `0 y5 ^: p3 g0 R1 r$ u
EFI_DEVICE_PATH *FileHandle02;
( j. E8 R& D% w( u EFI_FILE_IO_INTERFACE *FileHandle03;
* P) U- o0 K5 x; n0 g+ U EFI_FILE *FileHandle04;
1 n) @9 ~8 B( y2 l EFI_FILE *FileHandle05;
* [* S# x1 u" {# @7 b- j# X0 J; H8 \* u CHAR16 *FileName;
/ X, L% |" U ?0 \! B' L( M# j) |, \ CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;3 f( _2 A. R+ F; H
UINTN BufferSize = 8, BufferSizeKeySpace = 2;( A$ _) B' j5 R' `
int Bit0, Bit1, Key_Space;
/ t3 u; Q* A; X
5 w$ k: c1 B( F, N! s FileName = L"NewFile.txt";9 f1 U9 r4 e0 p" V/ S& v- E
Space_Key = L" ";7 ~/ P. R4 T; S8 S6 V. p# R
( w3 f2 z' ?: T# e' F BufferA = L"ABCD";- [5 v8 [8 Z- i$ O; J
BufferB = L"EFGH";
8 \# f7 l' e4 F b+ v BufferC = L"IJKL";, Y" H8 o4 l Z7 U7 L8 p, E# T
- H `0 A; s3 m5 z
Bit0 = 0xff;8 P! P1 S+ X9 G- l0 U& l. a. S# ~) U
Bit1 = 0xfe;
/ J6 p$ W4 k; r+ o0 L1 {
( Q! a5 _1 c. Y8 ]. y! y3 @1 V Key_Space = 0x0020;: H3 N1 E2 I6 v9 r9 ]
/ D6 k- A1 X/ H; J& H* T Y
InitializeLib (ImageHandle, SystemTable);
7 l4 J+ w4 l% H! ]( i# T& o
! u+ p& i/ P/ `3 j3 o9 j% | DestAddr = AllocatePool (BufferSize); , g, [* c$ ~* k1 x
5 @0 ?5 {( ~$ |& A6 i2 V" p Print (L"Value of Bit0 is %x\n", Bit0);8 c: T6 I: L/ a1 }' X
Print (L"Value of Bit1 is %x\n", Bit1);
9 ^5 ]2 B5 [ O) S- G- O1 @3 z U; p! u7 G
3 m/ B1 t, j% O Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
6 [ I& L4 @5 s7 U if (EFI_ERROR(Status)) {# y% |9 n" _" Q; f/ Z5 ~* D3 \* G
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);6 O0 f7 ~( }7 z, V8 x0 e, O
return EFI_LOAD_ERROR;
# {% B) W) s! K5 H+ u+ o% H }# j' Q) M5 p q0 ^6 q
0 e( m5 X4 V2 l
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
& m9 r0 ]1 v% N* O7 [; _ if (EFI_ERROR(Status)) {! U/ D( n" o. L. [6 F
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);; C1 s8 t5 L9 I! ?- f( ^0 o/ U
return EFI_LOAD_ERROR;
. Y: p; F+ ]( T! } }
- x! p" o) e; U7 g; B9 E; U4 R$ o- g 8 i: t* L, ?5 Y( f. C. n! X7 f: `& d
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);: [) g5 }' |, U3 Y7 l
if (EFI_ERROR(Status)) {* l! Z1 V4 e+ Z5 C: N
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);! N2 }# B" z# V% [; u
return EFI_LOAD_ERROR;
% U: S! F5 L$ a$ X }4 n! L: w' c4 \9 f
) j( U1 J" V% x0 e
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);* A2 u" H p0 d4 w
if (EFI_ERROR(Status)) {
/ M" G# r, p+ M: n1 }3 G Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
7 l; C8 {9 e7 w return EFI_LOAD_ERROR;
. ?+ G$ A; v" I- Y }
1 A' q. x6 n1 ^ & ?* K( F$ W8 a4 y
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
# F, m+ ]+ g: y9 n: ?; }3 o% L1 K! a if (EFI_ERROR(Status)) {
: L- ?) G- j, [8 W* A! P Print (L"Could not open file, FileHandle05 err code is %x\n",Status);# ?; D3 a! q" t8 L1 `5 r
return EFI_LOAD_ERROR;1 o0 _( S4 o9 f7 k2 e
}
. S. y1 y% P; B/ W0 G' A " V# n; K! Y' `* M
Status = FileHandle05->SetPosition(FileHandle05, 0);
$ l+ ] ?/ B" [6 O: ~ if (EFI_ERROR(Status)) {( s# s6 E$ K5 @* S' C; o3 Q" v
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 s+ a4 D( n! U' f, u
return EFI_SUCCESS;1 |4 K! u* R2 A
}
, F4 l, k( y4 k* a' A - C& j- N7 ^. m# I1 S8 n; D
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);3 l3 `0 S$ v4 C: h! W
Print (L"File Buffersize is %x\n\n", BufferSize);& d& B7 E: r- m. ~
if (EFI_ERROR(Status)) {) {4 J D* s9 Q5 }, W2 v; D! v
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);& {: f- O1 @% g9 L1 D. b
return EFI_SUCCESS;7 M$ K2 \! E$ q0 L& J
} / N9 E8 t. f) S+ ^
5 A, J& K" c2 V# D% V1 t
Status = FileHandle05->SetPosition(FileHandle05, 1);
8 Q$ @6 Q: e; p( ? if (EFI_ERROR(Status)) {
8 A# X% \2 _) U7 i Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: Q h; p; {# H- Z
return EFI_SUCCESS;# s, Z, h* ?' z5 n
}
) l* ? ]. l4 W5 W, |0 l2 n& {3 K
6 H5 k9 X/ g3 }9 x4 X1 d( h8 U8 V# w Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# U: N- S5 Q- }9 o Print (L"File Buffersize is %x\n\n", BufferSize);5 X! F( b' D; t5 ~, M c
if (EFI_ERROR(Status)) {
# I( w$ S" {! ^$ r3 j: J1 G Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 R$ g: z1 M7 h; S: ?. ?! f return EFI_SUCCESS;
# Q. l7 h2 D9 p& m; A' {. @4 T( @ }
( u& z$ Z; s$ {) ~' @
! {8 i1 r2 U$ X. \$ w; e Status = FileHandle05->SetPosition(FileHandle05, 2);
- g# _9 d1 c `7 G& C; i: j" I if (EFI_ERROR(Status)) {
3 C" K1 I. j% Z! Z/ B; I Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- D0 o% W9 f. p5 W2 b/ U* Z
return EFI_SUCCESS;" [7 g: q! H* O3 M
}; F7 ^/ z; a5 c
# j! R' M: B( ]' E" a Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);; g/ s- d; m* }3 L) C6 _
Print (L"File Buffersize is %x\n\n", BufferSize);
2 i3 T! [- p) m if (EFI_ERROR(Status)) {. h) w4 B6 x7 R1 i; R5 K" a
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
8 T3 [& D- L) H9 F return EFI_SUCCESS;0 [$ N, W4 e. U& U
} 3 u3 Q- P; \# I2 t0 }3 f
6 f2 f$ K2 }- n% ^$ v2 |//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
3 P: I5 L0 ^0 O7 e0 C: g Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
_# j* D' P+ |5 i# l# | Status = FileHandle05->SetPosition(FileHandle05, 2); 1 e" r. h* o. S. ~
if (EFI_ERROR(Status)) {% C! T5 K( f6 Z2 x! |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# @8 x5 |" w; I" c0 w! y return EFI_SUCCESS;
4 w7 C# k w! z- M. y8 u0 [ }. L% x7 Z3 l: N1 ]# t1 T; K
! ]8 l& f1 A" H( ]
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);3 S" q4 k9 @3 |, H; t. @6 |
if (EFI_ERROR(Status)) {) L; n4 J4 } r- f2 i' C
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
0 e. V& z1 `6 B return EFI_SUCCESS;1 C k! ?: F4 F& F9 g
} 0 A5 Z8 F5 U! f
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);) ^& l0 x% E$ ~
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
+ s; w1 [+ `) S! Z6 h0 T0 t2 `+ }( K4 T# V
Status = FileHandle05->SetPosition(FileHandle05, 10); / H: J6 @+ k4 y. {7 W2 ?8 [
if (EFI_ERROR(Status)) {
, J& R, k# h$ y2 z/ [: a Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); x+ C2 t2 b4 x. |0 M
return EFI_SUCCESS;
4 O! h* q o) m F3 e0 @ }- i f1 a7 i0 u9 f( ]
5 W! j. H0 D5 H4 T$ H9 V Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 c1 B) F' U# M; v3 H) s
if (EFI_ERROR(Status)) {
" Q$ ?. F; K9 D4 Q9 F( G6 E Print (L"Could not write file, FileHandle05 err code is %x\n",Status);# H: h9 M" }. r4 K- E, m
return EFI_SUCCESS;
* Y }" o) m$ [: ?& w }8 k8 Z) o' h$ I0 v7 Y
6 Y- D4 H+ V* P# H8 y7 F
Status = FileHandle05->SetPosition(FileHandle05, 12);
) v# O+ a6 S, u4 Q4 w. ^* ]4 |9 }2 E if (EFI_ERROR(Status)) {5 y4 o- N. {4 ^
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 O+ b8 j3 p7 G- X- ^6 K
return EFI_SUCCESS;
* _; d$ N5 T1 K! K8 d( z }9 x8 V; F. o$ G" i
i) L; N6 g6 M/ D$ f" w r Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 \' o5 S8 D8 [, s) B8 i: T if (EFI_ERROR(Status)) {! N0 e% P* @3 L- X0 y3 `* P
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. f+ e' b" f- G% J+ G. W+ F
return EFI_SUCCESS;
; k# \1 J! \( o }
/ Y3 Y% `0 O: O4 ~ M. H( g6 p6 A( T
Status = FileHandle05->SetPosition(FileHandle05, 14);
2 A5 s( r* G2 ~ if (EFI_ERROR(Status)) {
) Z4 G2 b' G+ D! {' X Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# O1 K/ v# \# r0 G+ n return EFI_SUCCESS;
$ g9 y" [; W8 B0 K }
" k2 b7 E' T# N! G! }* [ ' r" Y9 O* J4 S$ F! ?% U
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
- u; ~9 ?( r' S. Q) @ if (EFI_ERROR(Status)) {8 y& Q. }- o6 b5 B" R0 O
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" E" V) G: o+ I2 G" J return EFI_SUCCESS;; u, h& S+ e" h7 X9 C, [! g3 u0 y0 b
}
: l" @) j" f9 P7 o& x# C
& b! D! v% z0 o) ?! G Status = FileHandle05->SetPosition(FileHandle05, 16); 4 o3 g- |/ d2 H7 _0 i; V
if (EFI_ERROR(Status)) {
8 d* ]6 E$ b% N' t, O Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 o& ]) ~ a5 m# N5 Q4 D V return EFI_SUCCESS;' V4 F' R+ C# g
}' {6 o8 s8 q4 Y% a9 z
0 y& z/ ^; o+ K3 V4 ^# r; d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
6 `- C4 m" g3 ]. M+ c$ H if (EFI_ERROR(Status)) {
+ C' @& M( H( [5 j+ e) ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ q% @" @( P2 Y1 D
return EFI_SUCCESS;+ X5 [& M$ a9 M' n. v W' H0 T) c: G
}
/ f2 e+ \8 g+ n9 q: L4 ]+ @* M7 d% I4 E2 L
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
1 r0 K0 e5 v8 g6 e5 E
4 O! X+ h3 Y& f$ a% M Status = FileHandle05->SetPosition(FileHandle05, 18);
7 l, ~. i, ^2 ~8 y* s; ? if (EFI_ERROR(Status)) {
# Y+ j/ L7 t! @! k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 X f9 c4 [& X
return EFI_SUCCESS;
3 h3 i3 \$ W, y( N' Y8 z7 q$ E }
0 j1 ]4 o5 S$ @
2 z$ |9 R: B$ Q2 |4 d" I+ n, E Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
) H* r" S. F0 \& r! |* s if (EFI_ERROR(Status)) {8 J! C- i f3 O' A1 h( a$ C
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 G2 m+ [9 L9 k, p. x( e
return EFI_SUCCESS;* {$ c* I" e2 }3 a- d/ Z
} ' h& ?$ Z1 A3 W( U; w1 p. R# B0 d4 i
8 z, E. w0 j% M& t FreePool(DestAddr); # y0 k- A7 ]$ o+ O% o }
4 E" t+ a$ P" {* L; a
Status = FileHandle05->Close(FileHandle05);
, ^/ O P" x8 S& l if (EFI_ERROR(Status)) {3 x } m1 E" O. m# h+ \3 Q
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);, A9 E/ j4 q/ v3 H5 X0 R$ J
return EFI_SUCCESS;
% [% f2 t% W3 i K3 |' i3 A/ v }
/ e5 l: z6 [+ L0 B L; g/ a+ a
( Y' u: o/ {5 B( |: m Print(L"File Name = %s has already been created.\n", FileName);# k) ]; R. z$ E' H
0 t9 L# W2 f( p- i6 b& u Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");9 j @+ D4 N+ X4 ]% e. V) f
WaitForKeyOrReset();
+ O, Z' i4 h7 L; d* [7 d5 _* n
7 R! z1 T: b) K. c' D return Status;" I' D5 f7 @( J8 v: P
} |
|