|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
* C$ K: z. H! ?! O * myfileio.c$ i U8 C9 v1 c0 o
* Apps
3 P" ?. S( h! p# n */, E, |0 `+ d% H& ?3 \
x8 w0 Y; K5 Y3 k. o; L" A
#include "efi.h"
% V% F1 i( x. d2 g5 R7 X#include "efilib.h"
9 R' R, b8 b1 J# G7 {6 m
! ^4 T9 I. f( H. w3 j5 m( ^#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE# }8 {3 q# ^( c6 f) F/ a
( n5 {* |5 h- W% d! S$ O* M( f- y
static EFI_STATUS WaitForKeyOrReset(VOID)8 \- {8 i6 ]9 v
{, j" c* L" B1 Q
EFI_STATUS Status;
$ Y8 @: a2 F9 U1 l7 M) ?6 u EFI_INPUT_KEY key;- w0 b' `6 j6 Z0 I2 v# u- [
UINTN index;
* V; k. k& E5 z3 `9 b 4 V: e- N7 Q0 b1 E5 G' G$ M, p3 ?
for(;;) {# _' p% |9 {$ L
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key); }4 J4 a( [' r3 {
if (Status == EFI_NOT_READY)7 g$ |1 E/ |3 P# |
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
+ [& Z- w; P0 g9 D! a else
7 C: V ~- U1 }: j( e0 U$ C% u- } break;1 }* ~& j' C) H, ]% M+ A
}* B3 T& ~% y4 k0 ]& d+ V
if (!EFI_ERROR(Status)) {
9 Q7 o5 o W) z! K* R! ~/ t2 y if (key.ScanCode == SCAN_ESC)
5 m/ z* g9 Z# u! c. x/ B- I RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);9 S3 ^" ~# y- B- e6 d7 s
}
0 W- X( T/ x2 z$ X( {
3 X. M' g+ R, } return Status;& [, w& f% j/ w7 ?
}; u* P% b3 x1 m! a- o! P
- \. a* [# c1 a. t( A
EFI_STATUS' e. p/ i2 @3 n; b( v0 \, ?0 H4 ^
EFIAPI7 r4 k, d2 q$ S4 E. Y
MyfileioMain (IN EFI_HANDLE ImageHandle,8 [& r4 e& G- g7 R7 w
IN EFI_SYSTEM_TABLE *SystemTable)3 \/ V9 E6 ^' b G9 M
{
3 `1 `, R. h: m4 ] ~. N: q EFI_STATUS Status;
4 L) O0 }3 `5 C& a" ]4 x EFI_HANDLE *DestAddr; ) _% s/ ^5 `! b3 l$ p: K
EFI_LOADED_IMAGE *FileHandle01;9 k8 X- u- i' p" Y
EFI_DEVICE_PATH *FileHandle02; ! r5 g; b$ }) J7 |# d
EFI_FILE_IO_INTERFACE *FileHandle03;
# _0 {% ?4 w( i0 W2 a% E/ f EFI_FILE *FileHandle04;
. C7 C, g5 v, r `7 k EFI_FILE *FileHandle05;! T1 O6 L) l0 z8 E# X" k/ d& z
CHAR16 *FileName;, T% m0 [7 ?. J- |- a
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;* E& ] f9 _0 w- ]" N7 j1 X
UINTN BufferSize = 8, BufferSizeKeySpace = 2;2 x5 `9 O- Q" {! L# P- j( J
int Bit0, Bit1, Key_Space; # F3 N7 Y* U4 n: N
A- ^8 C" M& R% |; h: m% z7 E
FileName = L"NewFile.txt";
8 ~; M3 ^& \( @, h7 S* e Space_Key = L" ";' K6 v" c4 e: y8 _: \
" O. f$ C1 k" _$ [ BufferA = L"ABCD";
- n Q/ F! `. C( D1 S2 T# w# l" R BufferB = L"EFGH";" a4 o6 d/ a1 K
BufferC = L"IJKL";
4 G8 m$ a+ _) b: C5 W' P' C( B
/ x: L, s/ V$ h# [3 ]7 l) x' I Bit0 = 0xff;
7 I5 z# u3 B3 b" L! k( g Bit1 = 0xfe;9 }' Z; z0 L: B; N3 M* ?4 X
8 J0 b) {2 d9 ^3 d* y) v! x Key_Space = 0x0020;8 J9 Y. {- ^1 [* t3 ^* V n; H
4 M7 l1 X- ^9 W/ n0 G, F! t% I- D3 { InitializeLib (ImageHandle, SystemTable);
0 q( g J8 \4 e8 V' |: ? z1 P# i* W8 m+ Q* i& r2 Z
DestAddr = AllocatePool (BufferSize);
( W% l; v9 D5 H8 ]0 j! d+ q
) ^( n/ `5 z/ P Print (L"Value of Bit0 is %x\n", Bit0);
0 O* j% |4 u, F' W Print (L"Value of Bit1 is %x\n", Bit1); / p) v% p. m i1 z- o8 H" I
; ~2 W" N9 f2 ^
$ ], @& K% ]- j( A Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);6 J) A3 E# L' w, _ a
if (EFI_ERROR(Status)) {
0 ~: I& q$ P* c% f( v Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
9 h$ U |, g" q! _3 v return EFI_LOAD_ERROR;, o1 h: L3 @9 p! g, p% K, ?- f& t
}! `1 J: l0 y* h O% R5 t, [: `& ]: F9 S, m
- q+ R$ f0 z: t) u Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);0 n3 {7 W3 V2 I' o N4 Q. U4 U
if (EFI_ERROR(Status)) {% a1 Z6 f2 Z. `( k
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);/ B K( C; l( `1 m
return EFI_LOAD_ERROR;
6 I& ^! G* Q, W/ E/ T8 l8 |$ F3 v } / ` \+ [ {4 h! H
5 W0 U) B+ K0 ?# V8 X" K% P2 w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
0 I4 D" o2 L/ E' q3 A; J ~7 u if (EFI_ERROR(Status)) {
8 ~" k5 x& X7 L; v* i' m( a Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
2 @$ O& e% _% n/ N+ I" V return EFI_LOAD_ERROR;
6 N7 [& P9 B( r) T. x+ l }
8 q8 U# q: x' {4 e% N; |& \' d# k; ^& ]
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);; L8 a1 n2 R9 c' I/ a) g- R. w
if (EFI_ERROR(Status)) {
+ i7 n3 g, r$ C" r* n2 o# Z Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
8 P7 ^$ V# r; m0 ^; r' u+ S0 f: ? return EFI_LOAD_ERROR;$ D0 o1 |9 S1 p9 t1 ?$ r
}
3 N' H4 n% Y0 m+ b 8 a1 K8 S: f. R& h1 c n8 k
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);; w r# R8 h8 D
if (EFI_ERROR(Status)) {
# B0 }" k' z% }, V Print (L"Could not open file, FileHandle05 err code is %x\n",Status);$ x: g% l! w+ ^! v2 r% P
return EFI_LOAD_ERROR;
6 m# V; ?) V9 \8 u }; T' }+ L/ |9 p5 f
% _3 n! K( h$ C Status = FileHandle05->SetPosition(FileHandle05, 0); e0 ~0 r+ L( G8 U
if (EFI_ERROR(Status)) {
# {" N; [ `9 K6 O# N8 M/ X# P8 x: ] Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! D' A5 v( V2 W* h8 Z4 ? return EFI_SUCCESS;/ c$ O% z" r& f C: Y* {
}5 z8 l, @6 o- f, \
2 s+ l! f( m; k$ q. b3 j Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
- B1 x7 G9 E @ Print (L"File Buffersize is %x\n\n", BufferSize);1 h# E- P1 m/ D3 X9 \
if (EFI_ERROR(Status)) {
3 E# }* Z- m+ m6 L/ q8 Z4 B& ^6 ?% E Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);8 M, |/ j' |/ b# @5 q+ `' R
return EFI_SUCCESS;- ?& F( B' R! d* e
} " n- G5 ?$ U% Q, _- X+ \- w
# k) E) m8 H; @' O8 O! |" N$ t/ C2 g Status = FileHandle05->SetPosition(FileHandle05, 1); , A. ]1 z z, K
if (EFI_ERROR(Status)) {
! w& @/ u. p4 _# O' V7 c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) a0 R; f; E( ]
return EFI_SUCCESS;
b8 v# l) o4 Z3 o h, s6 r }
: ^ n0 Q7 b4 N0 S. N' Y
" C) F0 ^& R% J* P! a" s, d Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
' A9 m0 d4 {4 d) ? Print (L"File Buffersize is %x\n\n", BufferSize);3 {) \+ Y; F# W% |6 o
if (EFI_ERROR(Status)) {3 Z6 N8 O0 t$ o" e; q2 e, K
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' Q8 l9 ?8 T: w$ s: C) {
return EFI_SUCCESS;! I% ]& p5 S- A/ u2 u; h
} ( @# J, P, q7 r: t# f
4 o2 q. S J N+ ]# X) u Status = FileHandle05->SetPosition(FileHandle05, 2);
! X; p( z3 |, W! g6 e [" q5 J, e if (EFI_ERROR(Status)) {
4 ~2 ]2 ?* g7 j- Z6 P( G; Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% i4 s( K5 |! T1 D& r
return EFI_SUCCESS;
" r; y, n8 s# f }
! u Y0 w. J$ E1 d, m , z" {! y3 {4 C) O; Y& e/ {. V1 L
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
! u' r- f$ a5 z, l- O Print (L"File Buffersize is %x\n\n", BufferSize);+ H5 n& P) Q0 w* A, K( A& h# R9 ?; ]
if (EFI_ERROR(Status)) {
7 `. O# R8 X1 f2 d7 f) g5 c Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
/ h) p5 s3 z* _$ u; N: ^3 S return EFI_SUCCESS;
# k8 G A4 X* d3 K/ L0 m9 C }
7 p, M( s* V3 Z8 z4 x1 |. E
$ [3 F9 b: a: @. u5 I8 s6 t//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>) B X# j; P# `: s4 f9 Y
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);$ O+ R, Y* ^' N: j
Status = FileHandle05->SetPosition(FileHandle05, 2); # O! C" z0 d4 U5 S3 ?
if (EFI_ERROR(Status)) {3 G9 d; o! t2 F* v
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 w5 D' o! I8 f return EFI_SUCCESS;
+ E/ x. m( t9 Q3 W) v! n }' O9 [+ _. M" P3 d/ h% }
! W/ }6 `2 ]5 \' ?- C% i; E
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);: _2 d( J' {+ W2 k+ x% F; n
if (EFI_ERROR(Status)) {
$ e. d$ t; z: P3 D# ]& d Print (L"Could not read file, FileHandle05 err code is %x\n",Status);; Z3 \& _% C# [; m
return EFI_SUCCESS;
3 e, X% p3 U) V' }! v M }
( c) k9 d7 N% M w g0 @2 q& { Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);* Z, P3 A& u. Q( l& z
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
. v" Y% C7 N- M; J' ?% I5 d& z6 j3 m& A( W$ T6 e; S! e
Status = FileHandle05->SetPosition(FileHandle05, 10);
0 T( ]) I. L1 ]- p if (EFI_ERROR(Status)) {# Z; a: _' P8 N2 R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 r* R7 c& E! ]$ o h
return EFI_SUCCESS;
: G p' ~. ]/ S' {2 b }
# n# [. S# a% {6 j: M. K' J
* r0 X- {* T- z# A2 \" U) S, {: v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);- L- ~4 |4 {6 d% Z
if (EFI_ERROR(Status)) {9 }3 J9 q6 x0 w+ {6 K/ r q: W* O
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& ?8 x: I# d$ N: c c& Q# Z$ e% n" P* A7 d
return EFI_SUCCESS;
1 O S( p3 S0 m6 C/ k* g }1 x; j& K8 v% _) e6 _2 a
- m3 P t: M {: R, ?# o Status = FileHandle05->SetPosition(FileHandle05, 12);
6 d# W5 X; R% L& V2 y* @ if (EFI_ERROR(Status)) {
8 e; N# s/ R2 M# _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 e9 E. y) s; I! R2 q/ \
return EFI_SUCCESS;" N* K" o* g: s: Y b3 @
}% Q* c) x9 R3 l+ L. X
+ Z3 n( y: ^" A0 O/ G! ~ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 b& E: Z* c; _" @# j! G0 Y+ c if (EFI_ERROR(Status)) {
- n, X, y5 O1 ?9 z7 n" ? Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% \' X. M l. A" d# Z return EFI_SUCCESS;' [: V5 G9 f% V/ @; N+ U' O
}* l* x& V/ }* S
8 ~& `& E* Z# g/ @- ?6 I# b+ G+ J Status = FileHandle05->SetPosition(FileHandle05, 14); # k, h* [- g& F9 N T/ u& ]) D
if (EFI_ERROR(Status)) {. m. T; C0 l0 Z6 N* `* Q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 c4 D- W8 v& C- w6 I
return EFI_SUCCESS;
8 s& C, \. y) g7 ~; J% L }" _' \6 d, r* i/ C2 O' f2 l( Z
% Y/ u0 X# ~6 k \8 e/ X5 e Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( t, [$ d. I* C! m3 O6 \% W. ?2 C if (EFI_ERROR(Status)) {" u* E+ l0 N% F: K1 }8 L, ]
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, y; I5 @+ f# T' ~0 x return EFI_SUCCESS;
6 I' h; T# N+ q2 l! S$ { z1 q }
; A7 k( k/ P: ?; |8 b2 c9 E; D$ O+ f4 l& J* A; ]) E
Status = FileHandle05->SetPosition(FileHandle05, 16); " H( ?" N& p8 ]5 z
if (EFI_ERROR(Status)) {- C" q5 _. {0 `; J% a1 v3 Z! ]
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 O8 a F+ t+ b* X% S# w$ p7 P- n, V return EFI_SUCCESS;
6 X) r' s7 M; L X }
$ W/ v6 i6 T \. m1 r- G 8 A k& j9 B" c
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
) x; z8 d* I% o& _. C if (EFI_ERROR(Status)) {& i% l- k3 o- s3 b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ r4 C3 t( J; D2 `
return EFI_SUCCESS;
: C' y0 x, C' m6 u4 k } 1 h0 w9 @5 }3 M }! z
; Q% d. ?2 F A5 P. P- h//---------------------------------------------------------------------------------------------------------------------------------------------------------------( ]- x' ?* m. P2 p3 x
0 W% K, s2 v! ~ Status = FileHandle05->SetPosition(FileHandle05, 18);
6 y4 \. S+ w+ U; c if (EFI_ERROR(Status)) {
' D" M7 S) r; ~ | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ P# Q+ F/ |# Z6 d6 ?' U D
return EFI_SUCCESS;
1 E" ^" g) _- h | }# [) V8 a( I5 Q. y* k
. T F' G; {2 l Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
6 w8 _3 }7 J: `. K4 g if (EFI_ERROR(Status)) {
- l9 d8 h5 a$ j. _ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% P) [# Q' w' O0 q. u return EFI_SUCCESS;* t& u+ ~& W' O: A, R) b) l
}
1 a0 I' P; l) s4 c6 G; ` $ H: I C: r9 }' G( j8 K4 ^1 s* h$ P
FreePool(DestAddr); ' W% N. x5 O7 W* o$ Q
! f; h* ?* v9 w! C7 W
Status = FileHandle05->Close(FileHandle05);5 q I! X3 C6 A6 {( }4 R
if (EFI_ERROR(Status)) {
$ H. ?5 }1 Q: d |6 l% q3 @( s+ ^7 w3 q Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
; ]/ R6 u" C9 @; N- z: X return EFI_SUCCESS;) g5 y2 A5 j) ~; U5 s9 P- O
}
9 L- C7 @' x5 `4 H- d* R: y0 J 1 k \9 [, D" R4 e; ^0 d) H6 ~
Print(L"File Name = %s has already been created.\n", FileName);0 A& i4 Z0 p5 q# A
+ L7 M6 ^, @+ c! i
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
( [* q8 x! ^1 [3 }: w WaitForKeyOrReset();% n9 L) H7 O4 Y, o
/ ~( s3 W; y' l5 i return Status;0 b6 \ m9 |8 v1 i1 j
} |
|