|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*% f8 o9 u6 `8 y; V' t
* myfileio.c
9 i9 c: t. S1 p- U * Apps
& s0 B2 Y- V' z8 _% M */
9 `- P5 _' r7 a
1 H- P) E0 H4 }* }" |) Y6 L#include "efi.h" X; N9 C& N2 H8 h1 v, ]4 e
#include "efilib.h"( h: k/ a5 G0 Z! @
$ X! q$ r8 N: O6 D0 c1 D) N! E+ d
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE0 D- B4 e) q3 T' `1 ]9 r
( L: T% l8 ]2 u6 D9 O
static EFI_STATUS WaitForKeyOrReset(VOID)
. ~8 N, i& h" o{
- K# c X9 f7 D2 }& {$ o5 L" ^ EFI_STATUS Status;" b" q) P0 ^$ |9 r. [9 |( `: M1 p% G2 _
EFI_INPUT_KEY key;# G5 u- T7 R2 P5 ?7 l4 W
UINTN index;
5 N% g; ?$ ~" h( _5 l0 \# Y. _ 9 h6 P0 w7 \% C5 A+ z
for(;;) {
6 |; e" g+ m8 H% m( y- C Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);2 d+ p5 G- _& x9 S7 X$ L" K& u
if (Status == EFI_NOT_READY)/ b. K& j5 B5 o! x- d6 X9 n* `. m o
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);5 ?- s' {0 L6 N
else7 F6 Z: i# {% s8 j0 m" _
break;3 t1 N8 e( |% K; ^- p* U# a
}
& {0 O0 r6 D6 I3 p3 F! |+ D( t3 i. L/ V if (!EFI_ERROR(Status)) {
4 L2 `/ \( j* E, D8 r3 S if (key.ScanCode == SCAN_ESC)# X( e' J- V# _$ O5 j% R" }( n
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
9 x. B& E4 Y1 b' A4 W }
6 E1 n8 K, ?# \$ O " g8 G; |, w5 ^) O9 {
return Status;, H) |6 S: M2 j4 }; l% `5 x3 c! y/ }
}
7 ~5 {% @2 A1 o" S
6 |* K6 h3 j" r6 x) I- n1 |EFI_STATUS
3 u" a# ?2 V( L# w% J6 P+ HEFIAPI
* O9 E% |' l: `, R' E3 j5 LMyfileioMain (IN EFI_HANDLE ImageHandle,+ H: \2 E5 Q. ~; `% i
IN EFI_SYSTEM_TABLE *SystemTable). J' o% k/ p+ ]' P; v- }& s
{
7 Y) P1 d2 d S: z7 p9 H4 D- i) g: H EFI_STATUS Status;7 h6 _) B' f( C, L
EFI_HANDLE *DestAddr;
3 B4 X# x) ~! S) h EFI_LOADED_IMAGE *FileHandle01;
: {* o, _$ s9 h, o% A5 m" V EFI_DEVICE_PATH *FileHandle02; , I5 E+ k" I0 P9 j# @: i3 G( J' U
EFI_FILE_IO_INTERFACE *FileHandle03;. [( i- @! t1 p. R" g
EFI_FILE *FileHandle04;0 W+ p( r: F! W ]+ b ^+ y2 F9 B
EFI_FILE *FileHandle05;- K6 t: o$ F8 _7 D2 @& x- j
CHAR16 *FileName;7 V6 z: y% i! Z
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
3 ^3 v' _/ p- f* y# N: ^ UINTN BufferSize = 8, BufferSizeKeySpace = 2;* o$ P# ?3 P# Z3 p+ ] M
int Bit0, Bit1, Key_Space;
0 q; f8 o, x g) v. ~+ d3 u4 w4 U ?4 H/ m
FileName = L"NewFile.txt";
/ j; o" u1 `+ b% [2 | Space_Key = L" ";
, \5 k2 P }- ]# k) Z+ u
7 e; r3 D; N5 `; f& b BufferA = L"ABCD";
5 k( U `. i( m BufferB = L"EFGH";; W: |5 a+ F [( s7 i1 i
BufferC = L"IJKL";
# v9 t4 A8 b; F" q( c" b: j: x( {2 f( N
Bit0 = 0xff;8 p, q7 S) W. s& A: V
Bit1 = 0xfe;+ U; |# R7 ^( D+ B9 b0 N* |
+ ~- y- x. B/ X9 J Key_Space = 0x0020;
; z3 [) b5 g7 p. _5 U
- ~, F5 H) t, @7 ]$ } InitializeLib (ImageHandle, SystemTable);
9 w; R( o6 `0 @6 S
% x) M) n* [; ]& H# f- j DestAddr = AllocatePool (BufferSize);
1 q! w7 F8 y1 [6 b; c+ G$ V
5 I% g* E- t) p; T: H2 r Print (L"Value of Bit0 is %x\n", Bit0);- h: N+ J" X2 G# B0 m+ h- ~! e
Print (L"Value of Bit1 is %x\n", Bit1); 4 s% d$ b S& P( X* B' m
) R1 n3 ]" c6 l8 H1 l' F , t$ T6 P- m- T8 P+ c W
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);! {4 N& b$ R/ t% n- r% Q
if (EFI_ERROR(Status)) {
. I& p+ E1 L: S( S" D Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
: }3 h+ \9 n* M return EFI_LOAD_ERROR;; c+ f" q" a. B' M" Q/ B# x- @
}0 ?" m8 w1 U( O0 H9 ]
8 M6 Y' A+ H; C" i, U- S# m& M
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);) R" u. K* p3 }( J. A
if (EFI_ERROR(Status)) {8 b* Q5 K( R: P$ a9 ?& l
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);$ \( @+ ^0 T" Z( O" t& Y
return EFI_LOAD_ERROR;
5 ]7 j7 Z# V% r) w* T } 0 [9 j' \- h& X9 a2 D
4 R7 q: o" o5 F2 V9 F Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);* e* C! f, q% {+ z8 ^- ^! e
if (EFI_ERROR(Status)) {
; B' P6 M% T/ ^! A9 ?: }) I- x- [ Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);. @" j/ M& o& M/ X" o7 V( S' Y
return EFI_LOAD_ERROR;
0 f% b( [% ~3 J$ X2 o }. ^5 V5 {* N& |+ |9 A! K3 L, K
/ |1 T: B& d# { Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);- n ?" E7 R' J1 R# K6 o2 {
if (EFI_ERROR(Status)) {. R. J5 K- }, q7 Q
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
/ t5 G: u s' h! H- h return EFI_LOAD_ERROR;/ f& x# O1 h; R+ h% {( p+ m
}
: P0 A: [& e. v4 q" `+ @% P5 _ 2 `: G4 p: _. R
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);5 l: [* ^: K/ E0 u1 r- C
if (EFI_ERROR(Status)) {# H1 D# o8 }, U# \% L L
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
8 Y) E1 _% u' A. g: D, X return EFI_LOAD_ERROR;
$ z' e$ x% Y5 q S) h6 m' G }3 I0 T7 Y& z* y2 y
3 J( F% R1 ^) ~" a; z5 ? Status = FileHandle05->SetPosition(FileHandle05, 0);
. M- N! R n' ]9 J. {1 n2 q& W& D if (EFI_ERROR(Status)) {2 d$ X, O; r6 P& V" @9 I! u7 a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
- }4 {& N6 X2 D# Z return EFI_SUCCESS;
2 | V1 v; P# L: B }
9 P* l6 D; M& y8 Q
/ p) ^: f: l6 S5 `% k Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);" r/ k" u& E/ m, ]
Print (L"File Buffersize is %x\n\n", BufferSize);) z& B6 L0 o5 P5 @# M" G
if (EFI_ERROR(Status)) {" _- ^* ]1 p8 _& B1 S1 U2 N; q# {
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& F. i8 T/ f3 _$ l, z return EFI_SUCCESS;$ W1 p3 ^) y9 c" o1 ]
} & t3 f5 D0 b: B' h6 ^- ~- A6 l
. k. _; j. \( Z! S/ e+ ?
Status = FileHandle05->SetPosition(FileHandle05, 1);
/ V2 t' B' j8 H1 q; D" ?$ T; w0 q if (EFI_ERROR(Status)) {: e; k0 k) a+ r" p, B/ L) T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# g" f! x* n% A, E6 i return EFI_SUCCESS;
1 F$ J4 p" q, j) c* b+ L& s }2 [: K' d: X* r% {- x: C/ a
7 v6 x2 i( L8 a8 z: P0 @
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);6 R! [% \/ k A/ D, P: ~/ K
Print (L"File Buffersize is %x\n\n", BufferSize);
' z4 J' ~) m3 y' [, `, H if (EFI_ERROR(Status)) {
3 @1 }6 Z+ }/ o Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
- b$ X0 S( m% p% o$ F& N+ i return EFI_SUCCESS;# d# R2 x8 p' ^/ ]& f
}
: V& R, h2 k9 D5 i - C f8 V' u1 N$ |) s- }
Status = FileHandle05->SetPosition(FileHandle05, 2); ( ?+ i s7 F2 W: m
if (EFI_ERROR(Status)) {
1 k2 f, @( A0 w4 t& ^0 _1 N! U3 @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 f, r8 b. J& D- b8 M3 m return EFI_SUCCESS;
* w6 S4 K {; d" @' s; T( { }) x8 q# {1 v; F) V
9 Z# t6 T/ a: \0 L Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);" J4 O! h1 F- Y& `* d8 o; k
Print (L"File Buffersize is %x\n\n", BufferSize);9 M+ G+ G9 z) p4 X
if (EFI_ERROR(Status)) { J% c0 K. M" G/ `2 c
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 [3 b" w. f9 {. s* _* l4 u return EFI_SUCCESS;' G+ h- F$ p+ C8 D* ~/ ^
}
3 R8 p {1 k# U8 O, @
4 c9 |' _/ N* M) p//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
7 \! j5 ]" F& J/ W9 p) P! p! X+ i3 S Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
( s# Y, d" F+ c( }% a l6 Q/ p( b Status = FileHandle05->SetPosition(FileHandle05, 2);
: G) n% x9 J: W q7 N, V2 O if (EFI_ERROR(Status)) {
+ ^, A8 f1 h4 P6 D Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: Y5 ]1 d9 `( j: {. l return EFI_SUCCESS;
B) O' X: L: C' S }
: [8 l8 |: F5 g$ u- ]2 M
0 [; v% A9 e/ S3 w Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);$ T" y, b8 h/ Z, c) h- M% y
if (EFI_ERROR(Status)) {! s( o: K' @' p( \
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
( s. O9 C) z/ n return EFI_SUCCESS;3 u7 V1 u$ y8 |, c2 B* K/ F9 h
} 5 ]# b! M p6 d: @$ f" y* Y
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
& h6 N) d1 i8 f/ R" @) T; B( T& W//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<- U3 x( g. w" j3 E* \: s& ?1 N& g' _: n6 A
" a. ~ C6 @$ U" O7 f0 ] Status = FileHandle05->SetPosition(FileHandle05, 10); - U7 z& H. U0 y$ |/ O
if (EFI_ERROR(Status)) {0 `* @4 x) D l8 Y E! u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 t; |/ ^/ J- ^) U: b9 s
return EFI_SUCCESS;: S" v$ o% |( d! l
}; ^9 K# X2 }* f1 o J
3 z9 n1 e, @4 t! {# M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);6 }) X" i, O' X4 D6 v
if (EFI_ERROR(Status)) {' X% ?3 X% f% O$ B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! F" R2 ~6 }. ?; u" y- G! h
return EFI_SUCCESS;
+ o$ z. f) ~7 [, j2 E }$ j, V# j1 t2 A, Q+ a
4 }9 S% ]2 c& e: L! J) t. n
Status = FileHandle05->SetPosition(FileHandle05, 12);
7 I$ o. I$ H, Z1 X& B4 g/ e if (EFI_ERROR(Status)) {
8 f+ C) p8 U; ~4 s. s% [/ | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! W" H# |: V+ z1 K; D1 I8 Q return EFI_SUCCESS;
# A4 ^/ C( X+ H* T+ o }
) a) B1 H7 o% g ) @7 @# A+ e% J6 d7 U1 x `( t
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);) s \/ G1 m$ _8 q
if (EFI_ERROR(Status)) {! r$ O$ s# ?2 h
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);3 z- |/ Y$ S! Q% R) O# P
return EFI_SUCCESS;
& g; c( e8 i% y7 y8 U1 E }
0 F! Y% l; e; P4 g O' g) R
! J+ J* R4 v6 `; |0 s, x7 W$ e Status = FileHandle05->SetPosition(FileHandle05, 14); ) ^. j5 g! B6 {
if (EFI_ERROR(Status)) {( I' N) m% _( K6 n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 ]6 U E( D5 p* P, z% h
return EFI_SUCCESS;
, `8 i. T/ j2 q/ A" R | }
: P) l' f' `. u6 Z9 F
- G0 t# ]: \4 l Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( R" e/ J7 d! k if (EFI_ERROR(Status)) {8 f: R9 v2 c& f) Z8 a
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! h: J1 Y' t# E
return EFI_SUCCESS;. @* F+ _- O, N+ O. P" {
}
6 k& `7 q2 S& ^! O4 b
! P; T9 P6 ~6 D7 `, p \6 P Status = FileHandle05->SetPosition(FileHandle05, 16);
) P% |3 f3 r( s8 h4 L if (EFI_ERROR(Status)) {
4 E6 `7 |: t# X2 l; C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 v- p! r5 X0 U# o ^0 v" {
return EFI_SUCCESS;& S. a& z M" v. g
}
; q/ B* T2 Q5 H/ h. o
9 d' ~" `* o% s* r$ P6 m Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: ]* K& |; z% E
if (EFI_ERROR(Status)) {* K. v0 u) K& f. E* C' q: s# ]7 Q: X1 {
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);! w3 c" T6 }1 y- `5 H Y
return EFI_SUCCESS;8 ^9 k6 B$ T& c. Q, W
} 8 q# M) j0 _9 K9 O+ y: S3 h
8 D' t& b$ n) ^% r4 D+ W# _
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
6 T; g/ f" w) G: v" ?; s; z, ?0 T% C. H, |# Q& j& H. h0 K& _6 O
Status = FileHandle05->SetPosition(FileHandle05, 18); 1 _: t( A! e9 o4 G, I, S4 ~# z' C
if (EFI_ERROR(Status)) {
/ I | `3 `( R- {* [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; }# i- e4 a, Z5 i7 i
return EFI_SUCCESS;
A! j9 T- V; c. w5 R$ z1 e5 Q }
6 T% s. w5 O) H5 \7 _ : a5 r; v/ r1 @( J) c
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);3 J, V" y: x4 N' O0 Z& ?0 O
if (EFI_ERROR(Status)) {$ B" n1 H: r0 a+ g
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* g( C( k/ ^+ x. h) M
return EFI_SUCCESS;% z2 _1 a5 p* \( z4 a! y1 d* O
} , m, B7 X, A% q# d
& f- d4 W3 L/ q FreePool(DestAddr); 7 g& ~/ O0 S% @9 {/ O
& ^% T, J; d. v7 x Status = FileHandle05->Close(FileHandle05);8 T& W R. ^/ |! r% i0 U- ?1 V! t4 E
if (EFI_ERROR(Status)) {/ u: k. w+ r6 k' V8 \
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
1 w* F# A2 b% h6 A! h. C return EFI_SUCCESS;
/ f/ F- O/ E7 T; g ?4 ]1 j }
: f# p: x6 E, D& @8 X; G! r# P
2 C# X/ b+ N% Y3 G1 Y5 }3 ^ Print(L"File Name = %s has already been created.\n", FileName);
* x/ z- O, Y! O- }% m2 i, P5 N8 X! {# K U6 L
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
/ c9 r( S# z2 t- }% D. e WaitForKeyOrReset();
: X+ a' Q& D E L
, Y' y( t7 Q* c7 ~# }0 {: l) t3 \ return Status;
; _# }& v; k) S J% n' Q} |
|