|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
( k: Y5 }6 h$ t _( N) J * myfileio.c+ z7 O0 G- P( r% K. |+ Q0 Z, p8 H, n
* Apps
" V) ?# a* B6 U" e1 B( ~7 S8 Y */: l& G" i: Q, D4 }' N
; T) D/ A& P9 X8 y1 E
#include "efi.h"9 q; _4 ?* L- d2 U. F
#include "efilib.h"
- g! ]% l. S; G% f! x3 f0 r5 @! A/ P1 h% o+ Q9 p
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE: J; B$ u; r% e4 o( S# E3 \
9 S) T& s9 ^3 F
static EFI_STATUS WaitForKeyOrReset(VOID)
4 a1 z m. P' o8 r1 _) O E) d) g{
* m7 m2 \$ r' a2 l EFI_STATUS Status;! l0 V8 L1 e8 H0 N w4 ~
EFI_INPUT_KEY key;6 o/ G7 P8 h5 f7 V6 ]4 E; `
UINTN index;$ u7 N* F0 M1 y) ~# C& S
( v- ~# y1 F2 ` ~( J" `# h
for(;;) { P0 w" t$ B! z G- R. m! C
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
& e; h0 o" T! ~: g+ x- f$ A# s, w% @ if (Status == EFI_NOT_READY)
5 n' p0 @% F1 Q BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
. [: h5 m n2 q& Z3 y7 ] else
9 h2 Y) d- d" [# R* \ break;
- C+ {% l$ I4 J3 A& c6 w1 e }' _4 D2 c8 Z9 ?: \
if (!EFI_ERROR(Status)) {: a* @$ F- r* ?0 ~1 O% Z
if (key.ScanCode == SCAN_ESC)
' B+ F( v' D2 k: {* j RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);8 r+ q p: V9 y9 C1 d' n& p
}9 r$ `3 {1 E9 ~: ?
( R* h3 a# q1 m/ c return Status;& |# f9 [ `: L0 K- F+ S
}0 h3 T) H% D' x4 } `+ P, ^; _
+ G, q. Q4 ^0 Y) C# [: O, K' f- QEFI_STATUS- S2 I# h2 v- @6 d. [ K
EFIAPI' A* t" o6 A6 D7 U" U3 z* T
MyfileioMain (IN EFI_HANDLE ImageHandle,+ s# [* D" U6 l6 A! F5 @
IN EFI_SYSTEM_TABLE *SystemTable)
( o: u o/ K- _' Z0 `{
+ k: g# ~: M; u# Y O3 L EFI_STATUS Status;
& o+ B3 j7 X/ r: H C/ \+ L EFI_HANDLE *DestAddr; 5 E; g9 x: K* t1 p7 K( O
EFI_LOADED_IMAGE *FileHandle01;0 S- E7 n% x6 T) Z
EFI_DEVICE_PATH *FileHandle02; 0 Z' s2 ~, ~3 N" a. v, \
EFI_FILE_IO_INTERFACE *FileHandle03;
1 o; x2 P% U' H EFI_FILE *FileHandle04;
3 ~# h- u* ?. P1 k EFI_FILE *FileHandle05;, }: Y1 c- w. s- \3 }, S
CHAR16 *FileName;1 D4 L& @ b; z& \( D! N: k
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
# v$ ~- F! ~/ x UINTN BufferSize = 8, BufferSizeKeySpace = 2;. Q$ V" C; Q7 G0 J: D7 e7 \
int Bit0, Bit1, Key_Space;
5 d: j$ p$ N5 P8 {$ O1 _) F4 j: N6 z7 F" N# Y7 Y
FileName = L"NewFile.txt";
5 e0 V( s+ e# s; z- R* G# q3 z4 ] Space_Key = L" ";
. L# u* S% r: s5 A+ Y
6 p' b4 Q1 U# V+ |/ R3 { BufferA = L"ABCD";9 A: ^, q& o# l6 K
BufferB = L"EFGH";
: X0 k* o! b+ }, E BufferC = L"IJKL";
/ a; Z+ i* `: D7 F8 e7 y: X0 f6 e+ V: _! C# P/ x( g+ Y
Bit0 = 0xff;4 L" [; M7 } P+ q3 w& M0 E5 y7 D
Bit1 = 0xfe;
& C- J0 _- ]8 ~) X2 U / N* d4 A' h8 d
Key_Space = 0x0020;/ Z9 C; _# p, O- _8 w! K
/ n- I/ v7 c2 O+ @: n0 W
InitializeLib (ImageHandle, SystemTable);
F9 ^2 Y0 s. a7 n& g5 {
, N5 r' F: {% u1 V0 o DestAddr = AllocatePool (BufferSize);
( T+ U: F3 j7 ?6 j+ \0 j# {7 w" u
2 c5 Y, A: [9 e( w: N Print (L"Value of Bit0 is %x\n", Bit0);" i* L1 A9 B% Q. F$ |: W9 b
Print (L"Value of Bit1 is %x\n", Bit1); 1 z1 A) ]: D7 e: u
- j8 C% f- r& W& c
2 }/ X, K+ ]& R# x9 K: g7 R! L Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);. R% x5 {: d! [' S
if (EFI_ERROR(Status)) {% K' {! l1 W0 k8 t) r* A" {
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);" E/ t+ ~9 h' n A$ L& c" ~
return EFI_LOAD_ERROR;. `2 @5 `9 q# ?3 n0 a* q' l
}6 T1 k, U! D. n+ Z& n5 J. Z& }* @; C
5 \/ \- R9 U, x, E' X- l; C- ` Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
. W4 _, @3 @* e2 X7 @2 ]( `: ] if (EFI_ERROR(Status)) {- ~7 Q. P: a( W1 m( s7 e
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
9 O; W2 u# Q3 k$ A- ~% B7 G return EFI_LOAD_ERROR;% g! Q" i2 C+ ?/ Y. D
} 4 B4 ]# `8 p* h. M
- Q$ `$ J# r* j+ H2 ], Z* V
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
# X3 q+ `6 [$ x1 f if (EFI_ERROR(Status)) {7 h4 k- P9 t7 D8 l, j2 y
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);- ^! M9 p( D! o) L
return EFI_LOAD_ERROR;' C! C8 y! r V+ l/ B
}& p7 \# O8 r" [' _ ~* c
o5 \! l$ F% W, w' U4 \1 i
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);4 J: {/ Y' [0 i6 M
if (EFI_ERROR(Status)) {4 L% `# U/ p# [9 o5 G
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
3 I7 f& _- }: g) D0 r return EFI_LOAD_ERROR;/ T( c9 H6 _' m3 S" {& q
}
0 a% V ^) E7 d2 y% s& r( n * S& _1 W* _. Q) u6 O/ M. N# _& z8 }
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);( |0 R) e+ G: u' {: V) I4 t7 u
if (EFI_ERROR(Status)) {- A; Q0 _4 x+ R% W9 g8 B3 L
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);: {: e+ _3 I3 W
return EFI_LOAD_ERROR;
% h" f' X5 f- z }
3 b: @" Q* B( t q - m% Z; @4 H- ]& H$ H7 s
Status = FileHandle05->SetPosition(FileHandle05, 0);
/ ?# C v; x( d, B+ n) \5 p if (EFI_ERROR(Status)) {
* y; K$ c1 A) P) V# d4 @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 N5 I( U" R. P
return EFI_SUCCESS;, p6 Z. F# j% ~9 \4 W* x3 I
}
5 ?* y/ o6 _) V( u6 E3 I; o ; N2 o; I/ Q9 J6 _8 l/ a
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
! o! |. K r! J9 f6 a7 x Print (L"File Buffersize is %x\n\n", BufferSize);) Q1 U- B* z% q }( M* L# _
if (EFI_ERROR(Status)) {- H' J3 f0 |: o; V' x/ K" `% ]' G0 Q+ [
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
, m: ]% `) N8 F9 @# N0 C return EFI_SUCCESS;
' @: ~1 `! m I k }
+ q# M8 A1 i9 X* r( c5 ^
2 _% `+ V `7 S Status = FileHandle05->SetPosition(FileHandle05, 1); 3 Z9 `0 M& Y' j' Z
if (EFI_ERROR(Status)) {# N# _5 y2 S* d U& O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& Y5 K6 y8 t+ L" m
return EFI_SUCCESS;5 d d3 n3 l: ~, F2 j
}
3 b5 }# a3 p1 H | $ k+ ?6 x+ ~1 |7 T7 a
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
- \4 I( z% ?; w3 e( M& a: ^ Print (L"File Buffersize is %x\n\n", BufferSize);0 W- J V) H7 x* Q3 e
if (EFI_ERROR(Status)) { f( t* g2 h9 v) `8 y% O
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); [) ^& M4 o4 O: r, X, @8 O( a
return EFI_SUCCESS;
3 W5 I% {3 R; K5 X) N g } : i, G0 O, e5 U& | G/ u
# u/ a: A- P k Status = FileHandle05->SetPosition(FileHandle05, 2);
# [) w5 Q7 g- o5 a5 \2 p if (EFI_ERROR(Status)) {
* H$ X' ?% Z, |! Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 e! Y2 w8 ?2 u: ?& n# Y return EFI_SUCCESS;; k8 s9 y# g( _; I
}
8 f& u' w/ X# Q
\4 q0 N" }& I8 J3 }7 H) { Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);) B [6 G2 i8 [ h9 I) M
Print (L"File Buffersize is %x\n\n", BufferSize);6 k% s, [- m* P; q2 c! v" a
if (EFI_ERROR(Status)) { G. C5 x6 g$ ]% n4 k9 p
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 h1 y; \4 z' M' t* d2 i: _ return EFI_SUCCESS;9 ?3 b/ n5 s2 i# Z
} $ R: S& H' J' @7 B% d3 L% j2 S
, K1 n. C$ [, j y2 }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
' ?6 t2 r1 }, n! d+ e' E1 l0 X Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);: E1 O% R! w7 G1 y- \
Status = FileHandle05->SetPosition(FileHandle05, 2); l0 e0 `- e. k2 U
if (EFI_ERROR(Status)) {
8 n6 R }$ F& F. {4 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. h# N% X) c, ^& b0 g- y return EFI_SUCCESS;' e, j2 b$ b) w
}9 o1 G: q6 D" c4 U Z% X
2 s- V+ d# P/ j4 t4 n Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
! f! t- k% \$ k. Z+ s if (EFI_ERROR(Status)) {! z- H0 a: k2 g" d
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
2 A0 `, c( N) z& X: S) r return EFI_SUCCESS;; F( F. h" o9 {5 C" {9 E
} 8 [% s# k. W5 }! e' b
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
3 q( G$ _5 k9 l( R//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
1 C* d, K( E% h3 o
- ?% o: C3 o- g$ E Status = FileHandle05->SetPosition(FileHandle05, 10); . G2 N& t# q7 M( h6 A
if (EFI_ERROR(Status)) {3 W9 w+ w' n1 W- s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( _% `4 o3 @- M$ r return EFI_SUCCESS;
; Q+ I5 i& H! G5 O! E }
: _" Q6 o9 K; ?$ |+ o9 k" T
& I% U8 }5 b C$ e. g- N Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. f1 {2 ~% q7 _9 V if (EFI_ERROR(Status)) {0 S& @" P6 Y9 L8 ?9 h" |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& k: e2 ]: D2 [! f9 z: M
return EFI_SUCCESS;7 B; Z1 x+ [: K# B! z$ m
}
6 W% p" f9 C' k& b3 j( x3 }8 _
- t% T4 Z" b, h: p$ i5 @0 { Status = FileHandle05->SetPosition(FileHandle05, 12); % K @% X. C9 X" o5 X Z
if (EFI_ERROR(Status)) {
+ O* Y- }) q" e4 P3 v; I, Y! ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 z3 O, d6 ]! n: ~9 ?+ B
return EFI_SUCCESS;3 i; E- r( [7 i2 z7 W
}' X7 Q1 [8 x9 s- z
7 C& `' c6 u7 Q4 N
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
/ }* a) p% G) d! G" M if (EFI_ERROR(Status)) {
' l7 l0 N2 g' O7 r( ? @ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 T* _8 k+ G6 N return EFI_SUCCESS;" z' j$ P! d( O
}
3 r) C' y0 H/ H* y. u/ B0 C5 W& F+ j) ^: y
Status = FileHandle05->SetPosition(FileHandle05, 14); 1 d+ ]& O% Z N1 @' f. n7 t
if (EFI_ERROR(Status)) {5 j- N0 {6 W' r; g& B8 B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' s# z' w2 E1 B4 f
return EFI_SUCCESS;
y6 Y! @1 G' A$ ^( [, S* D6 p; r }8 P* ?; j% H! x
0 ^9 ?4 @- T: _5 W/ _) s6 E; x- Y$ W Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);5 g* |1 q$ u! A1 G
if (EFI_ERROR(Status)) {
; R- P B2 d" r8 j3 J& @0 {* H$ P; d Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; U# j+ Y2 a6 }+ D4 W3 }- v* u1 B
return EFI_SUCCESS;3 Z# f8 K9 g) F" E6 g* f
}2 j7 f- U0 K5 `. P9 i8 Y
p* a0 b8 G9 X
Status = FileHandle05->SetPosition(FileHandle05, 16); 1 T, e1 F! B2 `% M1 `0 w* x
if (EFI_ERROR(Status)) {
% b C& G. h/ P& z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! s& }) W2 }7 U; R- S( O9 O return EFI_SUCCESS;; y4 `% }# A; x, s
}
. J: g9 r P/ }+ x! A5 h- h- j V , Y8 Y- Q& z9 Y! e) \' B
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# C2 g; ~# Z& w. K% G if (EFI_ERROR(Status)) { N9 L% S; y5 d9 M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);( k/ n0 E8 _0 z5 R' Q4 @' W
return EFI_SUCCESS;; S) g% V4 W0 x. u3 B* a) s9 i& g
}
& s: u9 c! w/ ~/ \1 s4 B& K/ }
9 N% ~% f1 a+ t/ U1 `( h//---------------------------------------------------------------------------------------------------------------------------------------------------------------
3 ~7 H; F' d' E1 f5 D8 x( f
/ d% X$ d6 n' c! m4 c9 h Status = FileHandle05->SetPosition(FileHandle05, 18);
- r* q5 F: L8 S ?& [% J if (EFI_ERROR(Status)) {( V$ K) e: i) k! ~
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- x [4 }% P r$ W
return EFI_SUCCESS;
# k9 B1 A& [2 U8 R }
8 C7 g. l6 n5 \5 } 2 w4 g' t- w% J
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
- e& t, x) x6 U if (EFI_ERROR(Status)) {6 Q A/ @+ c3 M9 d
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% V+ s* X6 S$ G/ m# m, R7 k: C+ y
return EFI_SUCCESS;. Z- t$ d) k/ k$ d9 |# }) S
}
! p& g3 e, k& l: n 3 P' y% j6 O! }+ g! }. j# x
FreePool(DestAddr);
3 c m% [; R9 L+ C2 z3 i) t% L
' i; W8 _, p/ x* |$ A8 p Status = FileHandle05->Close(FileHandle05);( T4 A+ @ r# D/ R, ~
if (EFI_ERROR(Status)) {, u* a7 Q7 J/ q- d3 u4 G
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
! T- v" ] \0 c, r return EFI_SUCCESS;/ [4 a7 D9 O2 N/ b- o$ l9 K
}
1 n- t% y) e; h G% S5 Q+ @& [. O+ F! o 3 O! S3 j/ q7 i
Print(L"File Name = %s has already been created.\n", FileName);6 A" b5 [( V7 u3 z( L9 S
( c! k1 Q& x5 P Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
2 q& k7 g5 _+ r" f WaitForKeyOrReset();/ P- x3 V- F& x" G
2 h) z7 ?2 e( M7 @" S5 q return Status;1 C) d. G* l: s1 I5 ?
} |
|