|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*. a- h2 I' K! l" h3 X, g3 t
* myfileio.c
1 ?3 T7 l9 e( R4 y# j. j$ i& F * Apps7 J4 c2 f' ~2 s. _8 Z
*/' u1 `7 ]( t, ~1 f! ~# K+ n9 ~% b
" R' a# M/ \' k#include "efi.h"
: v! J7 m2 V% ]#include "efilib.h"
* k3 ?* q( K6 V; [" @3 H' p! P! N& p" S' f! l, p) L. c3 g
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
1 |+ T) A- S1 [7 E h. S$ y7 t2 m
: R* P* [ o1 X8 b* H. `- Qstatic EFI_STATUS WaitForKeyOrReset(VOID)
' ~. T$ H% T* \: b0 p{
" f5 ~% z* Z. I1 a4 R EFI_STATUS Status;0 a6 X9 F# M! ?% N$ j: E2 U; x
EFI_INPUT_KEY key;: v; X' j- S% e H. {& X
UINTN index;
: w( H& i* U: ^" }% [$ e - e- z8 U0 Z. |$ ?% K2 }. f8 k9 a
for(;;) {
+ h, m2 P2 ^# p2 e; [8 c Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
' A+ w0 Y. z7 @) U! O if (Status == EFI_NOT_READY)
0 s( c2 K6 @9 w6 l BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);0 P/ w9 i1 U! A6 r
else; x/ r; c0 R. W9 V; i
break;& x% D# e4 O; n0 }0 g3 e7 n
}
% g# O- J' R9 z- B) ^ if (!EFI_ERROR(Status)) {8 N0 a" V# |: O& B3 |$ W% c, w4 K1 {
if (key.ScanCode == SCAN_ESC)
' s. o% Y6 H4 c- l! B RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);% @1 K6 D$ O( x+ g2 c# a, }3 F
}
% P; W7 e/ ^* k# I; w0 y7 J6 ^5 L 9 j5 F% Y# V% r- X, ^' F" t2 \
return Status;% b4 P5 I# d" v( }1 R/ c8 }
}5 c* }' u# X5 r9 O: C, y
# E1 d* |' w9 ^6 g# v2 u, j/ d6 GEFI_STATUS
0 `" c1 d s8 H4 AEFIAPI
1 d+ _; A* {+ w- zMyfileioMain (IN EFI_HANDLE ImageHandle,; p( j% ~- K0 r1 A5 Z1 _
IN EFI_SYSTEM_TABLE *SystemTable)3 w/ s+ s* C7 S4 q6 ^
{5 v) B$ t% D/ W+ y( T3 P" {
EFI_STATUS Status;
+ t; v, Z/ T$ N6 O EFI_HANDLE *DestAddr; - k& G" d9 s& ?/ R C' t# P
EFI_LOADED_IMAGE *FileHandle01;/ Y0 |8 N$ O0 g Z- `0 M
EFI_DEVICE_PATH *FileHandle02; - j. @8 S1 y0 K7 }' h c
EFI_FILE_IO_INTERFACE *FileHandle03;
3 Z/ N Z# c3 k EFI_FILE *FileHandle04;8 W0 g; g* t7 t- A+ v( r; q
EFI_FILE *FileHandle05;$ x. S" G. G- |, ^5 d5 u8 U
CHAR16 *FileName;
2 B7 k& b3 l0 q" E9 O CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
- K, @4 T$ Z. j9 X9 D: W UINTN BufferSize = 8, BufferSizeKeySpace = 2; P4 m7 K: J! B) a
int Bit0, Bit1, Key_Space; ( }* @7 t. f; A5 u$ S8 q; L: K' j4 D4 M
" X' U( H/ Q! L, Q) H FileName = L"NewFile.txt";
+ m! U! o" f1 K( l9 V' V5 | Space_Key = L" ";
, ?5 \$ S# B" g6 J2 j+ t0 Z# X% D ) a* L) ]8 s7 f, S% _0 L
BufferA = L"ABCD";
% d7 b- y% a$ J4 A BufferB = L"EFGH";+ B# ?6 g! I; K8 j- |5 y* g7 e7 b
BufferC = L"IJKL";
/ p# u: C' j* n8 F. {7 Q; W
# \1 j8 O' @8 c Bit0 = 0xff;$ e' g/ l, i$ w) J, ?: u4 d
Bit1 = 0xfe;
- E) T$ s' T: D8 W. G1 P. A ! ?3 n# X2 ~. g/ q
Key_Space = 0x0020;
/ W, C, b7 Y! V
/ F1 n) k; z# Z1 S7 L" ? InitializeLib (ImageHandle, SystemTable);
( Q! }/ k' D- W5 ^* J. r f8 t$ U8 o$ A- r" o7 }" c2 O
DestAddr = AllocatePool (BufferSize); / f' t1 Z3 r# N1 `4 V$ P
" [; ~; Y% c; c/ _! Q Print (L"Value of Bit0 is %x\n", Bit0);
' Z; D* p6 Q7 W0 r1 R* ]5 r Print (L"Value of Bit1 is %x\n", Bit1);
# Q2 b0 `6 I1 k! z5 h' A
& N+ d* _. Y) a& o( u5 I+ o G1 Z) Q0 I, _$ g" X
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);: v! J& W, X Z0 i. S8 c
if (EFI_ERROR(Status)) {
- v' ?- X& U" X! D/ J! }1 _& P Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
; ], W2 j2 p3 A) r% H0 r2 } return EFI_LOAD_ERROR;
) K, k( J1 P7 Y }* Y. x( {3 R3 A! p2 e5 ?
7 t' z6 _# D' O; R Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
! A# l( t9 o. [6 K: z. ?4 k if (EFI_ERROR(Status)) {
" w5 \3 ?1 S, Q9 P4 o9 [3 d Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
1 h! V% ]4 }6 x; [& ~ return EFI_LOAD_ERROR;
9 @3 e& M: d: K( h) @ } & C/ @0 |. e9 q
( {# x- b% Z4 s* ]8 n/ L
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
* {: a0 S _8 s if (EFI_ERROR(Status)) {8 m. f' L5 S* [7 Y4 B+ g+ \; p
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
; g2 K$ Z, @5 F( B# u+ g# R9 D return EFI_LOAD_ERROR;0 f: F; Y" C7 ?6 {" z; y
}
' V8 x7 e4 @) W ^: o- }/ B0 n$ f# O2 S. J7 ~0 {, Q! ~
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
& p- R! _. Y: w if (EFI_ERROR(Status)) {. |5 Y0 n: R* C4 r( e: I3 [
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
u- A4 m6 x( \# F0 R' } return EFI_LOAD_ERROR;6 }. B+ A2 k, k& c4 c
}
0 W8 w9 d" I7 m5 _: a; Q 2 F9 k" s$ F% ~% E- R
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
3 |/ C# g7 e% n) m. E G( R% o8 V if (EFI_ERROR(Status)) {
' X- U5 S4 ]1 N0 ^1 i0 ?" t Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
0 R& p: [9 f t5 p3 c return EFI_LOAD_ERROR;- ?4 ~4 X+ C$ r% ?" S
}1 O+ J# D1 C( t$ c
! l4 v0 q) m N3 k% }) n6 z Status = FileHandle05->SetPosition(FileHandle05, 0);
4 c/ ]0 H9 \" c+ Q7 P7 M* T# P x if (EFI_ERROR(Status)) {
) D8 @: Q1 y/ {" X- L0 r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) v, _( t1 g0 @) s8 A9 t return EFI_SUCCESS;
( j7 u) [2 ]+ f4 N! b }
9 P+ f/ I) ?" w, \
+ d/ J* `2 o- D Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
! S. e% |) \" `( ` Print (L"File Buffersize is %x\n\n", BufferSize);- X2 P7 [8 y. Y
if (EFI_ERROR(Status)) {
) f3 Q+ D2 o9 D( r. @# d+ U) C Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
$ V7 e. y) d4 x! C* i8 F& w5 D return EFI_SUCCESS;# e8 @0 L2 v* h% |% v C
} % | ^2 J. X' \) w5 m8 u
) G5 Z( D: ]& O l& n4 A8 H Status = FileHandle05->SetPosition(FileHandle05, 1);
4 ~% ~, k6 }. r- M }8 W2 W if (EFI_ERROR(Status)) {
" |0 l9 X5 u6 W Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ {9 Z* K& ^( q2 H" K7 K, S" z return EFI_SUCCESS;4 z+ V5 ]. _* ~8 \- X+ S( d
}
) e0 r" e& ~3 F, q* b 2 e- J5 W& C8 ^! t$ }+ B. I, X
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);' d! c4 B+ q+ F$ y9 {8 }+ K. V- e' s2 ~
Print (L"File Buffersize is %x\n\n", BufferSize);2 {( {8 o( L! q/ ~7 `5 J
if (EFI_ERROR(Status)) {
! p0 N! ?' }' K& V2 G Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
A5 \' j, m7 b: D1 V9 M return EFI_SUCCESS;( o& x( V; g. s, ^, a% X+ X
}
6 y" b- _7 w. J
. k; [$ \: K" V# c$ |% S5 b, D: q Status = FileHandle05->SetPosition(FileHandle05, 2); ) a- M1 F# Y4 N, h0 r' B# O
if (EFI_ERROR(Status)) {4 p& m. m; G& c5 q& f3 @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 S* n: [- L# ?: l5 f5 O1 y! l
return EFI_SUCCESS;& @; X3 v0 g) d! d: V( P# W
}
3 [$ o, `; N+ f* i
. M# }5 J" ~* K1 [ Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
8 T1 {+ E8 ?6 v; W0 I' @ U$ R2 L Print (L"File Buffersize is %x\n\n", BufferSize);
2 L- a! ~' \1 @ if (EFI_ERROR(Status)) {
( [. W6 F1 `& @ | Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
8 X R% C2 {+ `0 R6 j' ~- p return EFI_SUCCESS;3 I+ |3 K) e5 s6 V- T) U
} * M% A7 g9 G' x5 n7 K; J
$ }0 i* `1 T5 s q
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
$ G2 v* _! n3 C9 C: u Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);2 U% @6 Y0 T" ^' ~9 k
Status = FileHandle05->SetPosition(FileHandle05, 2);
x( @6 ?* y6 ?+ o% `7 | if (EFI_ERROR(Status)) {
4 |$ u; Q. z+ m$ T Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 }6 b& }. L! }
return EFI_SUCCESS;; _' i' Z' I) j; h/ J
}
% `7 C v Y* O6 ]: E% Y% ^3 B4 V0 ]
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);# b4 q4 P1 s4 t( n
if (EFI_ERROR(Status)) {0 ~" k- W/ H9 f: \( F6 A
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
. t1 ^* P& F* r( C0 G2 ~ return EFI_SUCCESS;. x# a2 H9 I; I& ~" t$ H H# u% _
} , Q9 {- K$ U6 V
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);% H* T. m6 W9 N0 J
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
7 b0 Q/ S. Q6 y( U. f/ N% e, Q/ F$ A* I8 J% i6 Z( M% p0 A
Status = FileHandle05->SetPosition(FileHandle05, 10);
/ |) a. W z# q- w- f6 b. e if (EFI_ERROR(Status)) {
. n6 Y) u8 e" t) }3 m! W n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; q7 F& Z* Z1 c; U8 r return EFI_SUCCESS;
- | l5 {7 Q3 x& l }
# X( O( {3 O; ~) Y- H & g0 F7 ?) c& k+ \, i1 m
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 ]9 z( H. E9 B; o/ E. H# e if (EFI_ERROR(Status)) {
& o/ D( X% I0 [: K; @ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);( n) S( ^. z) H9 Z
return EFI_SUCCESS;$ d- N5 W# `" ~ }
}
* I2 X7 |% r7 L* G+ d' }* t! o7 q
0 F/ R3 j6 i2 {& k. }1 C Status = FileHandle05->SetPosition(FileHandle05, 12);
6 ?9 t9 Z# ^' `$ F, e; h if (EFI_ERROR(Status)) {
0 u2 N% N* k; T Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 N+ \& e3 b% e) D* x" k1 i" ` return EFI_SUCCESS;& y, C" G* n6 s. p" f& i
}
' x, p' R; h% K* T) \
6 m1 I, R9 j' I& A, q Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ z7 \" C; L+ j8 O. F8 K# V0 h if (EFI_ERROR(Status)) {
# ?) c9 [0 X) A6 g1 B$ [2 A( H- c4 t4 s Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 d* C- J1 f$ \/ V return EFI_SUCCESS;
4 n" H$ R- y2 @1 _ }
/ P, J1 t5 l' K1 G+ e7 V9 @
7 E' u% N& ]- I/ ?& |( S& D" A% g3 ^ Status = FileHandle05->SetPosition(FileHandle05, 14); / O. I: J0 h/ f/ |, l4 D& P2 ^
if (EFI_ERROR(Status)) {
7 [$ B" b& r; X1 P. L( | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& k# Z% P# e) L: A7 E return EFI_SUCCESS;( W4 K8 H5 _0 U) H
}
2 h4 W2 F# ^: a9 p
1 O: O% q! U# R8 Q! H% k: l6 z- H' u% J Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 V' l) N7 ~ V2 p9 G- V" x! W3 T if (EFI_ERROR(Status)) {& R# v$ }5 w: u
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) G* k0 a; y& F7 p2 j
return EFI_SUCCESS;( }7 N3 g9 A; W# a+ g9 y
}4 r& T2 e) q% s# }6 a. `0 _( m$ I2 X1 w
" m; h; I; H$ Y/ ?6 V
Status = FileHandle05->SetPosition(FileHandle05, 16); 3 E0 f1 |, R# U- t9 d" X
if (EFI_ERROR(Status)) {
- W) J$ @2 T2 B+ u3 M1 F1 v Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 m3 i6 u) W3 e! ^" l) i3 K
return EFI_SUCCESS;* J1 Q- a* J4 s# Q' m. p* R7 T; e
}
. E4 ]5 t" ]- `- w + S; {6 Y& h; @& \$ u8 H
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% b, X9 x1 }) n' q if (EFI_ERROR(Status)) {' m4 L: m, K, ]3 u3 b: Q- U* V
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
7 S1 W" ` R( I- u9 n: K5 } return EFI_SUCCESS;
F1 @2 V. Q8 _/ {1 ^9 ` }
$ M# e1 m% A" A# n5 T& g& n
* [# ~: r& b, t& [" D//---------------------------------------------------------------------------------------------------------------------------------------------------------------* A) o8 _& M2 l' w
& X- T; a! ?: w7 H" x2 ~; r8 l
Status = FileHandle05->SetPosition(FileHandle05, 18); 2 ?# M+ G$ V" ]( _7 s0 t5 g0 b
if (EFI_ERROR(Status)) {
! C9 ^& B, G3 X/ O) P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 x ]+ H3 E+ e/ a. }9 K$ o
return EFI_SUCCESS;
# f+ z. D. v& H) Y2 E }
& |# {/ _8 k1 F " G+ R2 n* O" j8 x
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
) R; a4 b9 A, {8 C# N if (EFI_ERROR(Status)) {) k, \ Y! k. `8 r( \% s4 n% `
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% N9 ^% u$ `( o4 B$ x- B
return EFI_SUCCESS;
+ J0 u; d5 A: _# N8 g% G9 P2 D, T } 6 S" M l4 |& ]7 G. q9 d" s( m
9 g; w' e$ O! x+ ?, f H FreePool(DestAddr);
) H5 D1 {- L* T6 I
5 H8 R) p! B& J( o& G$ q Status = FileHandle05->Close(FileHandle05);
" ]) C- P" h' f- a3 D$ D; v6 @ if (EFI_ERROR(Status)) {
8 k' R0 q4 s, Y5 D2 h Print (L"Could not close file, FileHandle05 err code is %x\n",Status);& K3 c# T3 t/ N, V' F& o2 k
return EFI_SUCCESS; s( X% T. s# B4 {5 Z
}
1 p2 ]0 F* O" ~& i8 G ! m/ R9 _$ U# `( f
Print(L"File Name = %s has already been created.\n", FileName);
; ~8 w9 h$ f# u# s& [+ X$ g3 [& J; {1 \" o2 R! \
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
% e. Q+ p( `; K WaitForKeyOrReset();6 z) } O+ ]& ^) _
) f Y h' W: D- l% O( T" { return Status;
7 i5 X+ J0 @$ f1 \+ d} |
|