|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
% ~9 z7 W7 a7 O7 T9 y0 T5 Y' W4 U/ x * myfileio.c
8 v/ b. d, D5 f% H V9 p9 b0 _ * Apps
- m8 x( z1 u& `, s1 N+ F% E3 m */4 Z* \! c; U3 Z9 J: }
1 e v: D+ C2 O! P A! O% S% _) X
#include "efi.h"
`1 V, e; U- C' ]$ m1 i#include "efilib.h"
1 c! Z- K7 S% {
- j. f: i/ x% ?/ ~3 c#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE9 L. J. k, J/ z: m2 ^
& j& ~. S- a+ K" [2 j2 y+ [: gstatic EFI_STATUS WaitForKeyOrReset(VOID)6 E" B2 x( y. u4 M; ~
{8 r7 v: n2 s% A) l+ i2 Q
EFI_STATUS Status;# m& a% v9 d) N. u
EFI_INPUT_KEY key;7 I# W+ t' Y u" y$ @ b9 p$ U
UINTN index;, [8 R1 q* ?# k% x+ n
! e. _2 a7 S) x for(;;) {
7 `0 t: C; }: a0 s Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);0 D x+ V2 y( ? u% b3 k/ I" u
if (Status == EFI_NOT_READY)# v0 G# G% I( T0 c
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);' l# h9 X7 D( t# |/ s+ g
else
7 }$ s' X4 J) p- Y$ y4 l4 V' Z break;1 {1 E: {# A. A: @7 {2 Z! ^
}
8 @* ]5 J# Y1 i% ?9 f2 K+ T& Y if (!EFI_ERROR(Status)) { {1 P( |& p! D8 t0 E% H
if (key.ScanCode == SCAN_ESC) U3 S% Q: O0 y& V( x6 ?2 b
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
9 R. j7 C; \1 W7 K }* h5 K6 N. e0 b! y
' \0 }) ^2 O' [/ X, ]+ K6 h: N* i return Status;
( R4 I+ e/ G& S5 t' H+ { H}$ F" ^, g- O2 a$ x
; ~, i; `6 T% G% HEFI_STATUS
& y! N1 R* O& PEFIAPI3 K6 W- P3 T$ E0 \* E {
MyfileioMain (IN EFI_HANDLE ImageHandle,7 I8 D j4 V7 z& k
IN EFI_SYSTEM_TABLE *SystemTable)
6 x6 i' Q* N0 c) r; u& k( K{* [; v& V# y! n3 {2 P9 A
EFI_STATUS Status;
1 B3 I- \* b& }) v! U6 Q* C EFI_HANDLE *DestAddr;
- q# F% F% q- H5 \0 V EFI_LOADED_IMAGE *FileHandle01;
' A. j# h& j' F+ r EFI_DEVICE_PATH *FileHandle02; ( _8 l' n/ x! [ c! I; M
EFI_FILE_IO_INTERFACE *FileHandle03;+ I' o' v2 @& o8 n: y" b8 f
EFI_FILE *FileHandle04;" o1 B9 D+ I, W0 X0 o" Z
EFI_FILE *FileHandle05;
]( W- }+ X* q( @/ A CHAR16 *FileName;9 b# o+ Q/ U6 _/ |, c- D' D6 y* e
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
* B% [6 A. b, l* p" r. a UINTN BufferSize = 8, BufferSizeKeySpace = 2;
7 Z7 e( }4 x+ K Z" x int Bit0, Bit1, Key_Space;
4 M- x* C4 I- N
; c' R0 N. r' f FileName = L"NewFile.txt";
9 N/ J) _0 X W" H( ~ Space_Key = L" ";: S* C4 _0 i8 W; H, Z
- F* y- O- t, y, e! h BufferA = L"ABCD";
0 Q+ b; i% r* ?+ M9 o) r3 J7 j BufferB = L"EFGH";8 H% R+ F* b! N+ R
BufferC = L"IJKL";
" g8 c" e; f, U2 n. G' }, u7 p3 ?( z4 }! t6 ^4 ?( \1 {
Bit0 = 0xff;) p1 C. o( G% b: R3 _' x& h
Bit1 = 0xfe;
7 z$ ~/ N% c8 f( Y8 x& Y - ^' _2 L* H# A* Z: ^/ c
Key_Space = 0x0020;
6 @* M( _2 @, ~ 2 R a! |( x* h& Y
InitializeLib (ImageHandle, SystemTable);
) d8 v. ~, }" U1 T& y( V; ]. b0 C' T! ~
DestAddr = AllocatePool (BufferSize);
/ n9 N v, Z+ I+ R0 \) z# G
% `/ G/ k& I8 g4 N+ g+ b: i Print (L"Value of Bit0 is %x\n", Bit0);
$ [- g# e1 G2 u; e& g Print (L"Value of Bit1 is %x\n", Bit1); 6 \( N* M. D+ e R1 l
7 d3 }9 m2 X7 n7 |+ ]
5 O" [" C8 x0 v2 c Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
" m' t# t9 k* A( P! ^% H2 x if (EFI_ERROR(Status)) {. z) z7 {. p/ ]2 p! I
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);* N0 F/ q4 P9 S' j* U
return EFI_LOAD_ERROR;" h6 e* U r* q. m0 {0 [$ d( M, H
}4 e$ k, O2 s( g/ M$ F. x' j
5 Y5 O# N1 l- `( |7 H% n% @, U, f Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);7 t$ s3 t" r( r; p7 V
if (EFI_ERROR(Status)) {7 \+ m( J& w L$ W! y) @$ g0 C" {
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status); O- t- ]. c; Q2 t
return EFI_LOAD_ERROR;. S! }. _8 q. l2 h$ r
} % p+ p. R7 G5 Z
7 |6 {4 s* t8 c! z" h Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);: V. S5 W; k1 Q
if (EFI_ERROR(Status)) {6 V; w5 J# l% u; X- H; U I8 C9 Z
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status); K$ U4 X/ a4 P
return EFI_LOAD_ERROR;. L- @- t7 O; W0 f* W
}6 y6 ? ~$ }& X; q" q0 P: F6 W
( a) u+ w7 |( P! b* q+ c Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
, ^: E0 K! x; c$ I; L. g" z if (EFI_ERROR(Status)) {
% {; C8 d9 q# L. Z: Q7 J Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);* [! g0 q6 t" d( v% w7 K
return EFI_LOAD_ERROR;" U& T8 {+ k+ T
} $ \, _; ^8 M! t; ~7 N) w% O
0 U* A" e- H- A' i% p- ?$ }
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
3 ?+ f: h! e% b if (EFI_ERROR(Status)) {, H" j; a. m+ Z6 v/ U
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
+ K" @" J7 X% s0 t" B return EFI_LOAD_ERROR;* i) Z$ c" d/ |& U7 j1 G/ e3 \' G
}$ S, l5 M5 |! h
: F, c3 e p3 K- m+ f
Status = FileHandle05->SetPosition(FileHandle05, 0); & b$ ` h9 E- l8 ~# q9 i1 T e$ e
if (EFI_ERROR(Status)) {2 `+ D3 Z0 K6 q' Z2 t
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 b1 ?+ l+ Z) d' ]/ l
return EFI_SUCCESS;% i: z( ~: u8 X
} P6 }, _# M& b$ ?9 k( i
( g% {: w' y$ F" C+ c
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);; j( O4 V7 u" N# Q
Print (L"File Buffersize is %x\n\n", BufferSize);
c1 n; `( f0 O* @ if (EFI_ERROR(Status)) {3 d2 b+ }. z. V& E
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);0 L6 O @' ~7 W
return EFI_SUCCESS;# R# G3 ]6 d0 f. L
}
! R- p" ?: C/ ?8 m! A( p/ ~& e. m1 G0 a5 C3 i9 M2 ~
Status = FileHandle05->SetPosition(FileHandle05, 1);
5 P: o# w+ z5 B' c5 k if (EFI_ERROR(Status)) {
$ Z7 @/ J# v# i, N2 ~ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 [* k2 [3 u$ }1 O+ m
return EFI_SUCCESS;& o$ h% N5 D" z" j# `
}6 `& s" J2 Y: \/ n( B: U6 B8 ?
6 C) I2 J! P' N) u$ Y# A Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);1 X) Q1 |3 @ D# b$ r W
Print (L"File Buffersize is %x\n\n", BufferSize);8 k7 a4 Q* z9 E' Z
if (EFI_ERROR(Status)) {
J1 E5 _$ P q% c7 D Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 m2 Z' w. ~; l/ s# m7 f return EFI_SUCCESS;+ q0 f( h D Y, J' F
} + A3 p5 u$ h7 E
- C" U$ _# N& V, b" K
Status = FileHandle05->SetPosition(FileHandle05, 2);
! ]$ r" ^: g9 s' ^, N0 F: { if (EFI_ERROR(Status)) {) x, M; n/ p2 }) E7 r6 ?6 s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 @0 n9 c5 \, t$ u5 ~ return EFI_SUCCESS;7 W# P0 Q- I1 Q& ]
}, h& }" |7 I$ o/ W8 G
' Y% R2 S, W1 A$ ?, n" D
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
" D9 p' Q! r$ ^; A/ f Print (L"File Buffersize is %x\n\n", BufferSize);
1 p8 D c$ h7 Y+ S0 M' { if (EFI_ERROR(Status)) {
' c; `: t4 F1 x# B5 u. A( \ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 L& J4 z, ~: O3 t8 f return EFI_SUCCESS;
( A r7 u Z( ?" W x+ P) A. l& T' ~ } & v; U+ E( W# r) ~$ u, ?
- N( o& \ P0 z* ?3 ?# K( K. Y//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>: [/ Q: S5 O5 O- Q3 A2 k- E
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);" l3 B7 e7 l. ~9 {2 C
Status = FileHandle05->SetPosition(FileHandle05, 2);
D& N! J/ K* \9 i if (EFI_ERROR(Status)) {
5 E8 K1 c3 M2 o0 X8 O Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 ~, a7 H, P% i& |0 H' ^' W% E return EFI_SUCCESS;
- c9 D! ~# t9 K- v5 s( t }3 q& A1 e. f# M: Y# B* R
; w2 j1 v# @6 N' O Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);9 n. x8 y; }+ t- ~
if (EFI_ERROR(Status)) {7 ^+ r% T# T$ \# L9 k2 g* I
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);# n! S+ a, O6 ~3 Z7 ^4 W: m% u8 M
return EFI_SUCCESS;
6 l" ~7 }9 Q, K) g }
* @: H# e1 |8 J' B% x# D+ p# p Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
& {* A% B1 @7 D2 {$ t( E9 t, Y//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<' T: L$ S. j! u( W) o" z
7 g1 I8 L; J* D) }; i& Y+ }
Status = FileHandle05->SetPosition(FileHandle05, 10);
4 O0 j0 Q ^% T9 B if (EFI_ERROR(Status)) {
/ M4 G. m" X y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 P/ L+ L+ f% K( V) [; r8 ` return EFI_SUCCESS;8 E, j) e0 k& k1 b0 x8 _
}
9 q5 |1 O& C6 H E" o, ~
% i% W+ o) v5 Z3 m& L t: X( V Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 Q2 F- C# V/ I. {5 D# P; Z3 P$ W0 S# M
if (EFI_ERROR(Status)) {
1 v" C% F, L; B7 w Print (L"Could not write file, FileHandle05 err code is %x\n",Status);( t0 n1 L8 V( Z7 R0 `! r w
return EFI_SUCCESS;% x1 A8 Y" c9 [# K
}
i, S$ j+ u! l+ R5 ^8 X. f3 M. p) H* q) `9 D, g& X
Status = FileHandle05->SetPosition(FileHandle05, 12);
: q" S2 `* a: U8 E9 F6 u1 g- v1 | if (EFI_ERROR(Status)) {. }1 M/ M( p; C L, O3 N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ M7 u9 M, B4 t* K6 R return EFI_SUCCESS;; s# a n3 x* v8 I8 k- u
}
e# A' z- f4 S
/ @, Y. ]' u/ I' H3 S' @8 D Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, V2 \; U5 m2 ~7 G: g0 K7 ^) D if (EFI_ERROR(Status)) {8 `* Y$ S2 P& J o9 d, |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' x/ [1 F8 R: e! T6 Q return EFI_SUCCESS;
+ a8 T+ H" m1 V& u! x }
* \+ O' U. P0 z+ H' l
, t8 N* b2 T% z6 s0 w Status = FileHandle05->SetPosition(FileHandle05, 14);
+ [( Q1 I) u8 J4 f2 M if (EFI_ERROR(Status)) {( [3 V: J% G& ~4 m0 @1 z4 U7 G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ Z, }& R5 X4 G
return EFI_SUCCESS;
2 ?' p( X3 k0 X0 @/ i }
+ t) T* X# D, A; A8 u
' u* Y8 Y$ \2 K& u7 A% O2 }" a Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 H$ i& b7 r( i6 a, y' @& D+ s a if (EFI_ERROR(Status)) {
0 p$ a+ C$ v- A4 b! H( Q' g: o Print (L"Could not write file, FileHandle05 err code is %x\n",Status); T, K \) [5 I& R; a
return EFI_SUCCESS;6 w3 J) x' t6 ]) |( _
}
) I2 H; Q! V, v5 ]( y6 f
# u2 d7 N% b; p) M7 E Status = FileHandle05->SetPosition(FileHandle05, 16); ! D: o6 b( i: a7 A6 L0 m1 V& r/ i6 J
if (EFI_ERROR(Status)) {
2 q. S" F+ U# E Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, Y9 x/ C: C) d) \+ J
return EFI_SUCCESS;% B- r3 B" B" l: n5 |
}! d/ Q2 c2 t5 r" R/ `+ a
3 v! e/ Y# v+ I; B4 a2 ~) | Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, s- y8 y+ m$ T2 H3 \7 T/ g if (EFI_ERROR(Status)) {5 j: M7 {; U/ F; b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: r# r8 u) f1 t2 _
return EFI_SUCCESS;
' w2 Y2 ~; P G } 0 }& Y; U% X6 g. b5 e3 v
- Z4 H/ O( |' t6 G
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
5 g/ w: \$ ]5 [) w6 c' D8 ]4 s
% D3 B" ~5 t0 B" B6 Q1 x' x. B, g Status = FileHandle05->SetPosition(FileHandle05, 18); ; k6 Q' ^) j" b2 L1 t) C) T
if (EFI_ERROR(Status)) {4 M& O; @4 h) p' g
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* `7 h Y" e' } return EFI_SUCCESS;+ k! r# ], k) N; y
}2 X! X7 h3 R2 p
$ E7 _* |# @9 @* z1 V' a: P1 Q Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);+ h* d; G. ]2 K+ m
if (EFI_ERROR(Status)) {- L. M- }8 V( |7 ` ^- r) ^: `
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 L5 k. _8 J( R: n
return EFI_SUCCESS;5 C2 d2 o2 S I! v/ I( B
}
+ C+ y$ a9 a# W * x% A+ [: q' f. e0 t1 P
FreePool(DestAddr); ! G' ]1 T# z5 \) q
: r9 f7 F* ]" L9 w2 e Status = FileHandle05->Close(FileHandle05);
4 ~# E9 N. E- w T" Y( I if (EFI_ERROR(Status)) {! h+ m# K! z9 I4 M* s
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
2 g/ ~0 c/ L7 b$ Q, [" k) t return EFI_SUCCESS;
5 h' G1 X) ` S; ~6 }. z }
8 l, {- n* R6 l ' E/ y2 F0 s( Z7 ~: m$ Z7 j$ b: P0 y. N
Print(L"File Name = %s has already been created.\n", FileName);. P( G( K( a3 j* E: Z5 @
. o b9 H, X* ~8 G e/ z% V Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");% }0 E! _8 p1 S! O9 h, U/ y- l
WaitForKeyOrReset();0 r3 d ]+ j; v/ M8 i1 y
4 G+ [! l8 q9 Y! D+ z( R4 y+ a
return Status;
5 B! @3 A# m: F1 c$ l1 K} |
|