|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*, i" F, }! s; x+ x0 o* c$ Y
* myfileio.c
' b7 |; j; {6 P8 g) Q2 |& \ * Apps! M- W' K# E& W" Q
*/
3 ^7 m$ e% L, A0 _; E! U1 l" F3 Q4 b8 f/ Y& a$ N
#include "efi.h", t' M0 {7 c% H' g1 F. b1 H
#include "efilib.h"
! ?9 Q! E0 l3 P9 d- o) T7 A6 F" O/ D1 k5 ?
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE& ?; j- D" _5 Y2 ?. W
8 N" Y4 l* _0 g' S; g6 A bstatic EFI_STATUS WaitForKeyOrReset(VOID)
1 ^: c) H l7 B6 \+ b+ G, T{
- v6 Q1 C7 A2 @4 R" |" s EFI_STATUS Status;# a0 b! n7 q& T! H/ E
EFI_INPUT_KEY key;0 Y/ N0 ], t8 N0 Y r _
UINTN index;" k" @; i3 h& A# f! M& x+ U
$ m5 ` O, u5 E* T B# {
for(;;) {
+ D/ e; J% j! i6 _( s0 j% ] P1 i Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
8 j9 S( l: M3 e% U5 C% u5 I if (Status == EFI_NOT_READY)+ J/ O! W7 ]. t& ~7 n
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
+ P( d1 o8 w9 m. O else
- l( O) ?8 y- v1 [. i+ } break;5 L7 ^ |) e/ C
}. D- a c2 {5 E$ Q, ~7 \
if (!EFI_ERROR(Status)) {
7 k0 h$ B, o. c/ ?* o, N' d if (key.ScanCode == SCAN_ESC); D1 _8 z! ^/ F- o% O
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);5 g: h# V. a4 R5 M& s
}
3 [9 S+ U) c9 l8 X4 h1 `
# }( U) V# e% h# k return Status; |6 Q, U; K+ z' k& {. x9 Q2 X1 H0 R
}
0 A# K9 F5 d& J
9 \2 U7 E9 T' O& REFI_STATUS
$ a$ x/ U/ O4 }4 R1 j- H [& Q5 \EFIAPI
* v8 z. F' _) H- g. b$ oMyfileioMain (IN EFI_HANDLE ImageHandle,$ {* G7 b2 I3 k. n, D+ `# Y
IN EFI_SYSTEM_TABLE *SystemTable)9 @0 X) l4 n; k6 M* q3 A4 V# s
{
7 w g2 N* e: n" {+ S+ R EFI_STATUS Status;
1 {! l4 y+ b l( X' Q9 S6 ~2 V EFI_HANDLE *DestAddr;
|( f( }0 a6 R: d5 q& P EFI_LOADED_IMAGE *FileHandle01;: F6 n, h% i: e# S
EFI_DEVICE_PATH *FileHandle02;
" R& L4 O' D5 w2 m EFI_FILE_IO_INTERFACE *FileHandle03;3 W6 V" m+ M7 H) o7 R3 s
EFI_FILE *FileHandle04;; W* N: J; {, t6 k* b1 I
EFI_FILE *FileHandle05;: ?5 t6 k( h( C1 l5 C. O, H
CHAR16 *FileName;: k" a3 Q! o+ k# x9 g$ {* R
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
2 w+ j) \5 @# a: C5 q, i. y UINTN BufferSize = 8, BufferSizeKeySpace = 2;
7 |% B, h. v w5 u- A int Bit0, Bit1, Key_Space;
1 S- V3 ]2 F4 K0 U, M
% L, s6 P8 f n" |, w3 r FileName = L"NewFile.txt";
0 c8 S+ O& I! b' ^ Space_Key = L" ";( N) i" _* I' y5 Y* y! V
* s: J$ R N3 a9 L% }3 ?5 b BufferA = L"ABCD";0 Q+ m# E% f3 e& y$ l
BufferB = L"EFGH";" x& j3 e+ r0 r
BufferC = L"IJKL";
! }! \2 p" J0 U* ~5 e
! B; ]7 F7 A8 s) K9 [, l Bit0 = 0xff;6 l2 a7 C9 Y! p. ?( w$ A0 L
Bit1 = 0xfe;0 g' C' s# r% r: b1 m& m
0 _; E# o6 G5 i, P% ]5 u$ U( R
Key_Space = 0x0020;
# M8 p: y1 V- V7 h8 T( B: A 8 K) U$ n6 n0 C2 j
InitializeLib (ImageHandle, SystemTable); ~# o& C e0 r, o& c( {! q
4 k* l c/ m+ z5 o5 l) Y DestAddr = AllocatePool (BufferSize);
+ [0 C- ^. G( O {: X& H
- `6 i, r" s% s, _0 B+ O' b Print (L"Value of Bit0 is %x\n", Bit0);
; X% v) K8 i6 ] Print (L"Value of Bit1 is %x\n", Bit1); ' r1 R/ P0 J c- u- R2 X
( \( G6 P6 Z% ^/ d
! m; O3 v3 |: _8 h
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);) U: f8 X9 ~8 v! J# P, [( p
if (EFI_ERROR(Status)) {
0 ]% o7 {# M2 C6 U& [* w' L2 q Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);4 I# j3 ]4 L5 [
return EFI_LOAD_ERROR;
; K8 r* g8 j0 A' a6 B6 \ }3 p8 g# l1 S9 C# }/ E3 A H
5 d! H% f, Z- `+ j2 }7 H
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
( Z* y4 [$ }; R* W( u X+ ^ if (EFI_ERROR(Status)) {) \! G( k( p7 Q- _0 e) R
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);" y1 s/ L& L7 `6 w) g) g! m
return EFI_LOAD_ERROR;7 E" }* u! j I1 ]) I: H
}
3 g) i$ F/ J6 R( M& @
, r9 u/ Z |; @# c. n5 q Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);1 i; l& q5 q/ C y W* u
if (EFI_ERROR(Status)) {
1 a1 g: D5 g: G3 w Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
2 ?! `3 l: u/ W' f- X return EFI_LOAD_ERROR;
7 ^; }& u7 p! q1 m: L* Y* | }
2 U. g0 b5 Z% @1 T w; o, C& J
$ t: X A' {2 L5 f Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
0 ?0 @7 F: u8 d- L if (EFI_ERROR(Status)) {
0 D8 @, l+ k1 H7 g1 o, s& [ Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);: v+ A" n+ l6 U* N. [5 a
return EFI_LOAD_ERROR;7 `1 U+ p$ l+ W
}
. H% W$ N8 w5 R # \, {# G7 N4 ^5 D
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);6 C% ^: b7 l6 V' ]0 x) L
if (EFI_ERROR(Status)) {( O [$ X) W1 B5 ?* c
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);2 n2 }% u, }, w; T; h4 E
return EFI_LOAD_ERROR;2 ]3 i- m7 l+ g+ Y8 g( j) P: @! {
}
" H y8 b# p+ u1 c' G
( V& n2 x' W* Y' t" m! Y Status = FileHandle05->SetPosition(FileHandle05, 0);
8 o7 e) [6 f; j- P7 G3 q# N if (EFI_ERROR(Status)) {
: J4 _; B# K4 ]) ^% v Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; z a3 |( J& h; h& w return EFI_SUCCESS;4 y5 y% E1 x5 w" a
}/ K& |8 N. J: [6 Z9 V
8 u* K7 m: n( s1 }
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
" \2 ~, M) U$ e. P/ ~ Print (L"File Buffersize is %x\n\n", BufferSize);
& C5 o5 C" G3 N+ b; f if (EFI_ERROR(Status)) {- z/ H& M4 }' n# o5 L
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);4 D$ ]) G3 O; M- R- v( T0 j8 A
return EFI_SUCCESS;
0 D9 ]9 O& f6 n' J! R2 m }
2 a% C; t4 S S) ?! Q* h' _8 N6 z( ]
Status = FileHandle05->SetPosition(FileHandle05, 1); 4 q6 U6 r" p5 h E b
if (EFI_ERROR(Status)) {
3 y' ~) e) R6 Y3 o Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& G$ u* c7 K1 t9 G" H7 } return EFI_SUCCESS;
# Q! H) P! K# K6 h7 `3 F" x' D }
9 ?5 x- Q- d8 k y7 X* {2 l" W4 y3 a
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);2 r( R2 _" B; R& A
Print (L"File Buffersize is %x\n\n", BufferSize);9 u2 d z( `1 h. i) m
if (EFI_ERROR(Status)) {
9 _4 l1 R7 c6 s9 ?9 Z Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
* ^1 S$ c3 o6 q$ @$ I return EFI_SUCCESS;3 x/ C8 m6 L, Z' x2 B
}
3 b8 e7 L X$ \, ~% _& q8 \+ k
2 P- Z3 I( O9 f0 @8 E0 \4 s- A Status = FileHandle05->SetPosition(FileHandle05, 2); $ |8 ~; i) r5 d' I
if (EFI_ERROR(Status)) {
- W0 c8 a2 S/ w* x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* }6 y2 i5 j# ]; s; P% ?$ D/ m* E# g) w
return EFI_SUCCESS;/ X3 u0 E: e' H3 L2 ~- F
}3 ^- o: ~! r- q+ v2 e* x
+ l- L/ B' K3 u$ Q4 e Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);1 c& u* w. q( v
Print (L"File Buffersize is %x\n\n", BufferSize);; _1 ~) n. y S D6 j
if (EFI_ERROR(Status)) {+ ~$ N/ \8 g( c5 g) X3 T& i
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# i. k) K6 `4 Q) U& j; n' P( P return EFI_SUCCESS;
/ E. S2 y5 @, i# V* P$ C/ @: u } 5 b$ a- X8 d# O: ~
6 w8 I" }8 G6 H# X- l, R//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>( `2 A% m$ T, O1 R @0 w& R
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);! Y* ~# {4 D" k0 w R) ]$ z! F, ]
Status = FileHandle05->SetPosition(FileHandle05, 2); 3 x0 Z/ f! W0 b! k7 l
if (EFI_ERROR(Status)) {
4 h# N2 M7 k9 l4 k7 \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ O/ l7 h+ Y4 I& y, E' l1 u- a
return EFI_SUCCESS;* B7 K* f Z# ~8 h# U3 Y% c& Z7 |8 e
}
8 D0 Z |; m; P* f4 C* H% W" T# G) g( @# l' p
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);% C: Y2 T. U0 |* t7 P6 P
if (EFI_ERROR(Status)) {
/ Q9 y4 J3 i) a3 L" {( V6 D Print (L"Could not read file, FileHandle05 err code is %x\n",Status);+ f) V1 w; H5 ?
return EFI_SUCCESS;4 o' I- o( e( L* h7 s2 }
} ' }2 w& G2 a* B' D5 C
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);. O' E- p7 t, d- P! i4 P
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
8 v! w" P5 O/ @" H
$ y7 G! f- X4 ~# J7 n Status = FileHandle05->SetPosition(FileHandle05, 10);
* @$ t, h; Z& `' x9 l* i if (EFI_ERROR(Status)) {0 D& w* [. g G% J+ R; `
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 H& i- y3 \' j9 L
return EFI_SUCCESS;. z8 B( i" S' ~6 V7 c
}
& E b! @' W6 i) x; G( A8 G: H% J 9 G' _8 \& J# X6 W! K2 m u2 M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);$ s5 H% s1 \. n* p \
if (EFI_ERROR(Status)) {( G$ y. f8 p# `) ^
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) ?, r6 L: @6 m, t. M- m
return EFI_SUCCESS;% G6 z& M! [: l9 }5 G' G. W2 x" `3 N
}3 Q3 T! U5 H% n1 v
4 X4 c+ S F R; h' Z+ E2 ] Status = FileHandle05->SetPosition(FileHandle05, 12); " S! G. o+ s) Y( g# g, O* c$ y
if (EFI_ERROR(Status)) {7 C( E6 |: j+ o- ~, @% b! J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: U% h- ]' Q! n$ {$ Y
return EFI_SUCCESS;0 \' u r! H- T# Q# a( U+ D \
}5 {4 L5 ?3 N- C8 ^
5 ~5 i" A/ y! N1 X0 o
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' l; O8 K3 z/ @8 ^ g
if (EFI_ERROR(Status)) {! E8 V! \. m# D3 e/ D' ?. z( A; }" I2 o7 Z
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) f) @; s8 g$ |
return EFI_SUCCESS;# }' b$ D+ t& G a: S" R6 \
}
2 `1 b# O1 b1 j. b4 k$ d6 v" X& E0 o, t3 ], o0 O- y' F
Status = FileHandle05->SetPosition(FileHandle05, 14); + G3 a) g- f8 r6 u+ @& m
if (EFI_ERROR(Status)) {6 V& d, M; k& c
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( ? @$ W" u0 _! ? @
return EFI_SUCCESS;
3 u: t! F; `$ N. M9 C8 e- m @ }
( r7 A( B3 K1 b% _
T# B V* f! s Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);1 o2 Y# V# ], y$ |
if (EFI_ERROR(Status)) {2 J% ]1 k$ z* `7 N+ J
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 w. e R& ?, Z5 e; F0 ~7 w0 O
return EFI_SUCCESS; v9 U6 A9 O" g3 S7 N6 _# z2 L
}5 M0 y$ e. ]9 K) H& i) t
' Q/ j3 K7 U* ]$ i! X4 R, A Status = FileHandle05->SetPosition(FileHandle05, 16); , T& s1 v" A: m4 I/ t
if (EFI_ERROR(Status)) {
" |; Q+ a6 H1 ?' Q [/ q: \! M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 L; z7 T) O- P8 ?; ^2 H return EFI_SUCCESS;
+ l0 }" W7 g% M) {. K! g* f }
) W5 l" W, m& Q3 _ - J. V! q7 G6 F: k; u( K1 h
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);8 u# b4 x$ N6 D" W/ f" n7 j$ H
if (EFI_ERROR(Status)) {7 i% D# s2 h# Q& \. B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' n/ i) u3 W3 U* ]( E5 `' A return EFI_SUCCESS;
/ ]$ \5 ]7 s0 X( g% Z1 Q3 c. R! r4 D } & p: ~. J" J& I4 E
0 S O5 n$ ~4 L$ U V4 b, |//---------------------------------------------------------------------------------------------------------------------------------------------------------------; [& c+ t) S% h9 t1 ?0 t
) ~4 P8 x5 p8 N2 K( r
Status = FileHandle05->SetPosition(FileHandle05, 18);
7 o' F& Q, |! f! ] if (EFI_ERROR(Status)) {6 G( P: _7 A5 a# p) y: S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 q ?9 o; @; n1 u d, J
return EFI_SUCCESS;
7 }+ v0 v3 J1 n+ U/ f ` }+ j( V0 S" d$ y. u+ _. b: l6 \
5 O+ o' e6 [0 `+ j Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
% W- d. K6 C, l5 d if (EFI_ERROR(Status)) {
) d6 b3 T c. ~ G9 ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! x m* r: m, \' S' d1 e2 r return EFI_SUCCESS;
/ U" p$ B: Y9 s% n N# y } " h! }2 t+ A" a: W+ ~. B" y, h/ W
1 `: F" x4 Y/ s' w0 s4 `( g
FreePool(DestAddr); * u5 Y0 L6 i# ^ ~4 N
- \: L$ b3 n* [ Status = FileHandle05->Close(FileHandle05);
7 x3 x; @% m! }# {0 ~) c8 ^9 m/ k0 K if (EFI_ERROR(Status)) {
" S% }/ O. a7 @6 G/ I0 O! ~ Print (L"Could not close file, FileHandle05 err code is %x\n",Status);) I6 M$ `9 i& q1 [ R; W
return EFI_SUCCESS;3 A! m; p% ~& h! L2 Q/ W# J
}
9 Q# D! L; l- m: [ 0 a% r0 Z2 a+ ` j# o" `! y2 E0 s
Print(L"File Name = %s has already been created.\n", FileName);& {* o" _7 s/ c3 U2 z1 D
5 a# ]# z5 E4 m) w- a
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
& M. R! s( V4 C9 C WaitForKeyOrReset();9 @! E w3 j+ N1 T' S, `
U, R# E! `2 q) R6 ~ return Status;3 h! E. X( P$ X9 [
} |
|