|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
7 C' C' W6 \ V/ Q4 x * myfileio.c
$ F# \2 V: @) A$ C9 e o3 G$ R * Apps
3 p8 g& H. O' ~0 j- Q+ \% w */
Z: @( `. \! y1 F8 P- ]
9 ~4 `0 ~: l q H# Y#include "efi.h"$ s: n7 V( ~' l1 m9 f) Q
#include "efilib.h"
& z( c* _7 K2 F! Z- X$ `, r( x; ]! e. M# u& b
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE4 W7 N! l4 U! H
0 |6 c% U4 B- v6 G5 E5 Dstatic EFI_STATUS WaitForKeyOrReset(VOID)" _; x" K: J# w+ o, S0 u1 ^: V
{
( e( t! L+ s4 D& ] EFI_STATUS Status;
/ A1 h( b& a1 Y EFI_INPUT_KEY key;. f0 Q7 B, x( X- I
UINTN index;# D o0 e6 x, C
8 I8 z+ T4 u- M, g1 I
for(;;) {
3 y' b/ m# d; i5 ~ Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);0 t; {, V6 D; g
if (Status == EFI_NOT_READY)! b- ]- |) d* u% H" m! d5 M/ U
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
) G: B, `' ]0 s% U) _ else5 q! X; i# {& m0 p0 e/ V
break;; C0 R; ~2 y7 |4 u- ~: k' a6 u
}$ H, Z3 i; o7 Z5 ?8 P) l' i5 {* G
if (!EFI_ERROR(Status)) {
9 F9 c7 n# I, z$ A/ `( A if (key.ScanCode == SCAN_ESC)1 ?9 x6 ]% `7 h; ]+ x8 h6 }5 [
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
# q. \8 w2 S4 ?* s9 ?% d7 t6 g }
* x5 P. M% f% F, A3 J- a
3 s* x" V4 S0 P$ b0 x4 u return Status;
5 G9 s- @) ]3 X Q}
- _6 B* x9 M" o
) i; ?/ O! [$ V# W0 v' @! mEFI_STATUS
* ?7 w; Q: @% c4 U+ B: @# }EFIAPI
0 l# i; H4 c: p$ V1 Q2 rMyfileioMain (IN EFI_HANDLE ImageHandle,( W& C# W& i A: @, |+ E+ P
IN EFI_SYSTEM_TABLE *SystemTable)
, R# F6 @7 A: d: j4 u) F, Z6 @{8 h: j# ~2 {0 r- _. s- }
EFI_STATUS Status;4 M5 B$ R1 V2 ~* |; N* Z
EFI_HANDLE *DestAddr;
0 G6 u4 ^' v" h# S& A EFI_LOADED_IMAGE *FileHandle01;0 |7 V, i, w9 f+ o! u! ~
EFI_DEVICE_PATH *FileHandle02;
. j0 q3 D7 }, N% l- X" _5 Y EFI_FILE_IO_INTERFACE *FileHandle03;, q- N( x4 `/ F, G
EFI_FILE *FileHandle04;7 P/ T/ W n5 _: L8 x
EFI_FILE *FileHandle05;% I0 h4 _" [% J }* H
CHAR16 *FileName;# R# {- @, T! z
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
) q7 i4 l9 Z: s6 v4 A6 L UINTN BufferSize = 8, BufferSizeKeySpace = 2;
% ~5 L" l. d u7 v/ _ int Bit0, Bit1, Key_Space; 2 ?, m1 h/ v* H) f) e! Q2 r
% [. M( M. l4 ^) x" T: D FileName = L"NewFile.txt";' _0 E# ?: d; J7 e/ R
Space_Key = L" ";
% r0 j( O: V2 Z9 `) @3 F
, d5 U! E% {1 W" U$ I BufferA = L"ABCD";
" T6 X% w& Q2 z6 |9 F BufferB = L"EFGH";
' E1 M" c1 W$ W# p* H2 e! @4 W BufferC = L"IJKL";
7 T* F* O; k% J) Y- P+ I# ?( L/ C' n" z) T. _
Bit0 = 0xff;1 s0 U+ I% p v
Bit1 = 0xfe;
9 l& A' ~ W) n4 U0 v0 r4 } 6 T: {( \3 }- G% a2 M! ?% e
Key_Space = 0x0020;. i, q; J2 ~; p$ g4 c1 L" |7 j w
* R/ F O) I/ S5 y1 [, O
InitializeLib (ImageHandle, SystemTable); / t* G5 Z4 [$ |( l# F
5 H' e1 j W( h b* z H m
DestAddr = AllocatePool (BufferSize); 4 E6 n: `9 ^6 |! x8 ~6 G1 D
* `' n' A( Q5 C; f. S; K% | Print (L"Value of Bit0 is %x\n", Bit0);
: ~( ~( N2 h0 e P& {, {3 M v Print (L"Value of Bit1 is %x\n", Bit1);
* m$ N8 N+ u+ V2 _
7 j8 I" m. \5 { 6 x; f b0 y- {4 t
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);' n' x9 \4 h) N' t
if (EFI_ERROR(Status)) {/ j- v7 E% M9 }- A5 G) M) ]
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);% x8 u2 [7 d" X% B
return EFI_LOAD_ERROR;
& l, B L4 D+ D7 g) J" b( l' S' ~ }$ }, \$ e5 U: U" O. W2 ]( r) _
: I' o# N1 @6 g2 ? {: L2 g
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
3 C3 f7 [# R2 Y8 U3 l( a- F- N. J/ ~ if (EFI_ERROR(Status)) {: _0 s) B' S% G( \( X/ V
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);; s6 E$ H$ h" T* l; t
return EFI_LOAD_ERROR;
L' p4 F6 |3 O6 C } + t2 r: A6 e! I7 L/ T( p; [
U+ g# e# g) O+ f4 e2 N+ | Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);0 i/ h2 n+ p% g3 ^$ A
if (EFI_ERROR(Status)) {
) t- X! \) W' ]6 P7 E6 X$ I7 h Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
, |8 x y5 g" L* u$ P return EFI_LOAD_ERROR;
1 Q% N8 E& H R2 v }
8 o( p, Z6 ?2 H0 b
( w) r! w1 o0 a q% L Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
, C( n6 a" r& i% j( _ if (EFI_ERROR(Status)) {
% Q, L5 i/ O3 d2 i2 t ^ Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
0 @( D& R0 r m( h, Z0 h8 k return EFI_LOAD_ERROR;6 q* ~& s9 d9 D: s
}
, R4 w# s/ u- X% Y0 ~& y
: E5 U( x2 c. [0 n6 P3 G& m$ i Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);* e5 @- U2 a5 G J- s6 E. q
if (EFI_ERROR(Status)) {" X1 g; G2 V% _2 S* S
Print (L"Could not open file, FileHandle05 err code is %x\n",Status); P, E+ |. j' w" f
return EFI_LOAD_ERROR;
w$ e6 j2 W8 O5 \ v, c }/ O2 B7 y4 G+ x) m2 N
: j* B/ Y. k* j8 M' z+ T
Status = FileHandle05->SetPosition(FileHandle05, 0);
5 J" [& s( |, e; X if (EFI_ERROR(Status)) {6 \1 I. U2 [% [
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! o# M. j) m) {7 f! l
return EFI_SUCCESS;
/ y7 i! i+ p0 I, T6 L' z }+ C2 z% D& v- r) Y; k9 O3 B8 c
# @$ R& P, V2 n% x4 M& f2 l( ] Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);" z. ]' f1 A& m/ l" e
Print (L"File Buffersize is %x\n\n", BufferSize);5 [ Y6 @9 ?. X. ^9 t8 q
if (EFI_ERROR(Status)) {5 B, `7 L" O7 I# `3 ^
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);& c5 q' I2 ]1 k+ X B& Q
return EFI_SUCCESS;
8 v# e. B" c" q! w } ' G( L- X; G0 q P/ x
5 V& ^* g9 _( ^! ~ Status = FileHandle05->SetPosition(FileHandle05, 1); & P/ o4 N6 c2 O3 \6 S3 E
if (EFI_ERROR(Status)) {( C$ G9 j6 A! X4 a; ~% K W; O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ x, B# z/ F8 j k- w return EFI_SUCCESS;
/ n8 Q" a5 o: K3 d: E }
! t) Q6 u) ^0 W! H- ]# [6 H0 I/ P
; E' n& V0 f# K Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);4 z2 b" [% q- [& ]8 E
Print (L"File Buffersize is %x\n\n", BufferSize);/ { l2 J1 i, ?
if (EFI_ERROR(Status)) {: F& ~2 {! z+ Q! L( {
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);) [. d8 v8 U# g- C" N4 y; {
return EFI_SUCCESS;
; A" l1 b; @; i: b7 c" `$ ~ }
% y! e/ a1 |4 Z S5 [ . ]% j p P$ \# S1 \$ D
Status = FileHandle05->SetPosition(FileHandle05, 2);
5 X1 [! L, e: ^& k if (EFI_ERROR(Status)) {
' \- `+ K8 s/ a8 d, q( o/ |5 H Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 m) {4 B. n2 \
return EFI_SUCCESS;+ v4 u9 s( F' L
}
+ y! Y. Q+ n2 H : E3 `- i5 F2 R7 ]( L* s) g
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);! @2 _2 f5 l: n J* g
Print (L"File Buffersize is %x\n\n", BufferSize);
* e$ c6 D; r, Y3 W' |/ R if (EFI_ERROR(Status)) {
, A0 ~) Q7 Y% E _, G7 l# C3 x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 T) p% W( p+ b5 q9 P' v return EFI_SUCCESS;5 s0 E! X5 t. m9 B
}
# @3 z: m. h, I$ U8 ~ ; \( I4 t) }. H0 u# d
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
P9 l$ F' v% u2 ]9 S# D Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);1 b& e9 U: _& U
Status = FileHandle05->SetPosition(FileHandle05, 2); % }9 n8 T9 g* l; _+ W
if (EFI_ERROR(Status)) {' R+ e: y: ?( x- u% W' d
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ f0 T6 o( L r! F' \/ f3 N: e return EFI_SUCCESS;9 d I1 f( g" }( S' ]8 Q+ p* \
}
7 j7 d/ X; k+ x7 t7 L: ^2 p6 k4 L& {( F7 C) D3 w
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);2 R1 P- t) z" {1 N& w1 p; W
if (EFI_ERROR(Status)) {+ t' |( p3 A3 _! u( b g6 Y
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
/ O4 O! k) A; w2 w return EFI_SUCCESS;
2 S9 C, W/ }) D* K1 l }
. Q2 x9 F0 q1 t) G" V- v Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);2 Z& y" i- T, H
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<; p( n; Z6 f) d# S
& c" q, I4 r1 `, z2 M
Status = FileHandle05->SetPosition(FileHandle05, 10);
$ Y- B: ?8 e7 j0 Q: x# u if (EFI_ERROR(Status)) {9 c6 A9 w, y% @0 Z
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# d; O2 t( [+ U1 d7 e! M! _ return EFI_SUCCESS;
% D' [$ j) u# E9 v+ @: d7 ^ }
0 ~" h% U$ M# a4 v' N7 o2 @
. p1 {' V( I. D3 O' b Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);) X6 @% i3 D! r" D
if (EFI_ERROR(Status)) {" ]% m( c T& `% O2 s
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: t! t2 W7 Q0 J! A
return EFI_SUCCESS;, |, Q4 r* e8 Y% j. m! O
}0 h; J3 X5 ~. s, z+ w
1 v$ S7 Y2 j5 Y/ ^/ R Status = FileHandle05->SetPosition(FileHandle05, 12); 8 L0 _* K! B1 n1 g. D3 V; F) j
if (EFI_ERROR(Status)) {
7 A$ T" d! S y4 t% C& |) s Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ o) E5 H. {3 N+ \! r return EFI_SUCCESS;1 Y. y' m( ?2 y7 Q5 V( }
}' A/ Z. A5 E! Z, p, \
1 S+ O7 x0 |$ I( ]7 x
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 e* w* z( p3 t G! k h3 E if (EFI_ERROR(Status)) {
1 _4 t& i7 W* C" D- m+ w8 M% Q Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! |# _* I# {) z7 |* U1 S return EFI_SUCCESS;
3 l) M/ o" o1 D& M- Z9 P: w }
9 a& \! ] b7 A7 G4 `3 q2 i% t: z, E: \, K- `
Status = FileHandle05->SetPosition(FileHandle05, 14);
& i1 |" s' x( x, m if (EFI_ERROR(Status)) {
x5 ~, q+ F; _% A& _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 p7 n0 F/ q6 x8 R; @: \! N return EFI_SUCCESS;; y) \2 r( F) I5 q+ Y4 c# J0 x0 c
}; W" h- S& a! U! Q5 x$ O
# W1 Q& ]8 y) \+ d7 ?
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ s8 T$ S5 d8 J! g if (EFI_ERROR(Status)) {+ l! d$ g3 v0 l Y) L' T1 u0 m, K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
n5 f: ?/ Z: A3 P& ~% M return EFI_SUCCESS;
9 A/ Z" W; ^4 r3 v1 [5 w5 s }
6 H- R8 W$ e' V
8 E5 o7 |* j2 U/ h' T6 E Status = FileHandle05->SetPosition(FileHandle05, 16);
) K* ~9 H' [2 W6 O$ x" [ if (EFI_ERROR(Status)) {- j/ c) r; M; o1 ~; c5 e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ Y/ ~3 F, }3 G; { F7 U9 W
return EFI_SUCCESS;7 G* V0 Y( b6 S; V" x' o
}
8 q8 b- v! y. ?+ q8 n& h6 R' }
( D# ~- E- G" M0 E; Z8 \6 W0 [ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 P& T9 l( d- l$ d+ Y' x' n if (EFI_ERROR(Status)) {: P" v( [# g) t0 g
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* R$ g3 K8 w( x
return EFI_SUCCESS;
" T% q, s- W' |0 [+ u& @ }
0 z) L) G+ o4 E" v$ t) ~; L3 B+ ?9 M \# |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------' [/ F2 }' A( i; g7 }6 Z1 l
2 v) H! k" }" F) s q4 M7 m/ ]; ^6 q4 M
Status = FileHandle05->SetPosition(FileHandle05, 18);
4 W: x$ V1 s2 z if (EFI_ERROR(Status)) {8 |! c& P; o4 t5 v) a5 c( f& J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); n/ ~- D* q# D, d" x( a
return EFI_SUCCESS;
+ z9 G# X4 Y$ A b6 g( F- w% O5 d# g0 B }2 U% w2 r n. {8 `+ g- y. A
. Y$ S7 b0 |: M( _7 H+ ~
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
5 I% \& l8 y! y( b, O* Z if (EFI_ERROR(Status)) {
0 P5 K5 o+ C7 ^' C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
* ~* p- \" e- g3 @ return EFI_SUCCESS;1 D2 l4 Q/ d* a5 q- W% v
}
: \% ^2 B8 A" B; n' r4 u 5 Y8 m) P" @ F7 ]. {
FreePool(DestAddr); . ^! f5 N& m% u1 x+ f3 }) C
1 p0 n+ X" { T6 N. W6 J6 E6 V
Status = FileHandle05->Close(FileHandle05);
: U% v+ K* F8 Z; I6 `+ ? if (EFI_ERROR(Status)) {
$ R8 j1 _5 b4 T/ H) j Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
0 K% @; n) U6 T0 W4 [ return EFI_SUCCESS;
. w6 r; J3 t# l$ `+ P/ f; N }
: i! O3 T9 J2 @ p
4 o/ |7 ~" ~' X4 G# `: O' Q6 F Print(L"File Name = %s has already been created.\n", FileName);
" V7 b5 ^' M \# J
: h; H0 \9 {1 I. ~( W: ^& i Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");7 ^5 G: V' D, Q% ]/ G. {; b
WaitForKeyOrReset();: G) F5 m7 t0 b h: ?6 d6 V
, T `& x5 Y, i3 y
return Status;. l- {5 e6 v: X$ o8 y
} |
|