|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
- t; q7 @- O( k& M * myfileio.c1 A# B: t! y2 X- W) m( I
* Apps! W: o) v$ H# x# S! S
*/8 }% I. X$ j @- C/ ]( ]
2 L+ D+ }; _9 F$ D
#include "efi.h": h- m7 }9 T/ J% l
#include "efilib.h"# y6 t; M/ ]+ r! S" }; Q2 m8 I
3 t5 C. I* u1 c5 x. g' y9 m
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
, S( Q( q9 y* V2 J
8 Q: K+ |1 H) k; R; Tstatic EFI_STATUS WaitForKeyOrReset(VOID) e$ j" I9 s3 v! O
{
4 c- i/ T" T: K$ }- G EFI_STATUS Status;2 |. K! ^" |3 [' { k; g
EFI_INPUT_KEY key;
; A9 @' Q3 H" s- ^ UINTN index;
$ p/ Z6 f( D% _) G; h ! w; x m/ v& Q* m
for(;;) {
/ K; Z9 [- m0 x/ L- C8 e Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);$ T; _: S# G* _: _& ?* Y) G
if (Status == EFI_NOT_READY)
4 ~& F# m, U" i: {1 w2 f BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);4 y1 b" ]/ I. d" F8 J/ I1 q+ x F
else! F* t8 ]% z9 @7 [' _
break;
1 y* V0 n$ Y, c1 Z; v( [0 s0 K }; l$ D$ }; s3 t% b+ s
if (!EFI_ERROR(Status)) {, a6 E$ X4 X* B0 F8 }+ }6 o
if (key.ScanCode == SCAN_ESC). c" c, q3 d5 X2 h7 D( f
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);0 m* [0 B( N4 q! o1 J$ p) ~; I& w
}
' r/ g" a- z) n9 ?1 K" f3 `0 n" A
, D& J0 ~6 I f- C7 B return Status;
( \8 R. k: Y" M}
+ D1 D& y# w7 M! {9 a
$ y0 Z" \6 b# Z- v3 M: h) N4 c8 O4 W* ZEFI_STATUS0 {. U/ ^! y- D" n9 S
EFIAPI
0 Y0 I% B0 W1 g: l5 h! Z! }MyfileioMain (IN EFI_HANDLE ImageHandle,+ a s0 n; n7 r* c( C3 a2 b
IN EFI_SYSTEM_TABLE *SystemTable)
# q1 F7 w# P( B8 z$ G o0 k7 t, u{" G0 a+ _. B* N2 z; d) S
EFI_STATUS Status;
7 b4 x' j$ K! q+ R y! Z EFI_HANDLE *DestAddr; 7 C& f4 U2 l1 p
EFI_LOADED_IMAGE *FileHandle01;! ^) w: o' q0 S e( Q
EFI_DEVICE_PATH *FileHandle02; - j: V& G$ _- z6 \$ M
EFI_FILE_IO_INTERFACE *FileHandle03;( v+ \' f/ ~4 {0 j* h; z
EFI_FILE *FileHandle04;
2 l, C6 q# V0 W' w* ^4 d EFI_FILE *FileHandle05;% [; E/ F9 |3 a5 |2 r1 c
CHAR16 *FileName;# o6 U/ U( ]) g H' A
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
- B( k& r' Z4 Q4 v$ z4 U UINTN BufferSize = 8, BufferSizeKeySpace = 2;
9 Z. E3 S# M9 _% `8 `& I int Bit0, Bit1, Key_Space;
9 D% S, e! R. E& _& N0 u4 M- e* W* q% a1 \1 O) Y
FileName = L"NewFile.txt";, }3 M4 U& }( C
Space_Key = L" ";9 z" O) v4 z' N2 g7 V7 ^- \: C
6 I5 f+ T4 M& k% v. p% i BufferA = L"ABCD";8 |" V6 u% R; v4 b
BufferB = L"EFGH";( e# g4 D# G: j( G/ _1 n/ _* s/ ?, f/ a
BufferC = L"IJKL";8 N- ]" n: B. S+ j% T
5 P" q( ~" a$ x4 \/ G+ s Bit0 = 0xff;$ D0 {; M2 o" S/ P# |/ B
Bit1 = 0xfe;9 A& A( W5 t! \0 a2 w
% [! _; C# e6 c! u2 l0 n: G- T
Key_Space = 0x0020;
$ W ]7 Q# q7 j e; A9 U, o - |. J" [$ Z, d- |7 g, y( M0 l) a
InitializeLib (ImageHandle, SystemTable); & X* z/ K0 n- o, P5 L3 L1 T4 W
- g0 _. Z* F2 s DestAddr = AllocatePool (BufferSize);
. T, Q/ F" e0 l$ R8 l; w T2 g& _9 ^) X
Print (L"Value of Bit0 is %x\n", Bit0);
" s2 W! G$ b# B* L' ] Print (L"Value of Bit1 is %x\n", Bit1); ; P$ M7 I& e1 f# r1 a) |) D8 Z
! [& B+ G2 L+ \# }6 U6 {4 q$ g
3 G( h, U6 P w5 O- Z
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
# g y+ w6 m' ~ if (EFI_ERROR(Status)) {
" }- O( X8 ]! P8 {! x Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);" F2 z7 r/ o4 \8 I
return EFI_LOAD_ERROR;
, m7 J4 s. K# n- m8 {7 P }5 F, o2 |7 r& r; M
" \9 c/ D' S; P; k4 ? Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);5 U; O' T: R5 d% l" I
if (EFI_ERROR(Status)) {5 Z. ~ \, N" X" F, X6 h4 C
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);: S1 R5 Z z9 M8 ^2 t/ z$ k% C
return EFI_LOAD_ERROR;- Q! @3 X% Z3 n e$ `
}
# M& l# M2 z$ t8 x5 |' m: e) K , c* J* n3 u% z. ?) B3 H
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);6 ]: i5 k9 p7 y& W# u
if (EFI_ERROR(Status)) {
( y+ l, X9 h( v6 H Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
+ T9 m- N$ O' n! N5 x2 p# E( y9 n return EFI_LOAD_ERROR;2 N" i! R* w3 r! P, R
}, a6 a, g/ r L( I2 c9 E1 U
! e3 x) P/ ~6 o2 h* h6 j8 L6 W! j
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);6 }6 }7 F( Y- |8 p# L
if (EFI_ERROR(Status)) {
$ a. v$ h8 ]' H6 F2 ?; U' M Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
9 v+ j+ H) e# g' G& H* e0 y% f0 l return EFI_LOAD_ERROR;
7 b, Y2 a9 B0 E1 u- I } 0 e. c9 Y, v3 E- [- [$ L
& A4 h6 o8 c, ^2 Z9 Y Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
7 d+ }+ s X" w4 N6 V4 F if (EFI_ERROR(Status)) {
0 v8 ]) Y% t' W6 q6 Q Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
+ R! L K6 ^: l9 x4 [0 p' t9 i return EFI_LOAD_ERROR;' i4 X, P3 a7 B) V" V+ r0 C; A
}5 K z9 n2 m( ~: Y
) Q- Z# J% j! ?$ a, _5 n6 F
Status = FileHandle05->SetPosition(FileHandle05, 0); + M n( F1 A& Y, f4 b6 _$ f
if (EFI_ERROR(Status)) {
% x5 f: g6 J) X6 ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! r; m) c |" X8 R return EFI_SUCCESS;+ K2 P) e$ u9 M* X- } c
}' s" o$ B0 `8 V: _
# x9 C. s: h7 A7 r+ J' }
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
3 {: p4 o6 l2 s& W9 e- a+ P- k& n& D Print (L"File Buffersize is %x\n\n", BufferSize);
9 J$ A% \' r# Z) p' t- O) f if (EFI_ERROR(Status)) {" H/ W+ B+ v' A/ z3 N
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);5 K5 \$ M1 x9 R" R- E9 ~
return EFI_SUCCESS; r; S5 x$ P$ F$ Z
} 8 s* S3 [- U" Q5 Y: H8 l' @
* F$ x9 z6 ?3 b, J- x
Status = FileHandle05->SetPosition(FileHandle05, 1);
5 v5 }! d! l ~ C2 t6 ^- f if (EFI_ERROR(Status)) {# W8 x5 x( |2 D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) }+ ]- X7 q- P6 x/ X. V/ h
return EFI_SUCCESS;
g5 r% [- X9 i- M4 i5 d9 J6 S- W }5 u O( w b8 P, R. _/ }: J& c' A, k
0 i# F" e) z6 X( B* b( p
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);: H2 z/ _" E: N$ W7 ?
Print (L"File Buffersize is %x\n\n", BufferSize);+ M$ o, `) D: z9 c. _3 f; H: N& D
if (EFI_ERROR(Status)) {+ K; }4 `* C j6 R8 W8 O, A
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' ]2 l4 z. J$ _9 S, q5 x2 u6 I
return EFI_SUCCESS;
# v4 k- l: ^5 E* X } 2 B3 j6 U! w, z
/ W3 y0 @. z) V. F; f$ h; L8 o Status = FileHandle05->SetPosition(FileHandle05, 2); 0 u% z4 f& I. J; B3 ^9 S
if (EFI_ERROR(Status)) {' ?, y, a9 C/ e& F; b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 w9 k. k W: d return EFI_SUCCESS;
4 k/ N( r! l5 {' S, A# J. }+ H }
4 W7 e# [0 J& L; ?* k
! b* a! G" L1 Z Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);; L' {' Y' c! B9 V
Print (L"File Buffersize is %x\n\n", BufferSize);" h& q" V0 g! k
if (EFI_ERROR(Status)) {7 u( O, G" {2 P6 I$ }; E" T
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);( D" p4 w2 p# h
return EFI_SUCCESS;+ r5 j- F, H* k" H& s; Y4 s
} 7 e& B3 G) m1 J' g
A; a, X0 V: m7 K( f, j, i% U//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
5 I: G" Q8 t! K `+ L Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
3 _! F0 s( x9 J/ L Status = FileHandle05->SetPosition(FileHandle05, 2);
* w5 q: S# b: h+ g& c4 H! ^; l if (EFI_ERROR(Status)) {0 ]: I3 a, U4 A* @% N' u5 P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 N- b0 C3 s: j9 W! b. u return EFI_SUCCESS;
% g& V0 {; @8 W" u1 C }6 r0 X+ H& B* R/ R8 d
/ l# d: e' R9 R0 B4 R, e. p% v Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
2 }8 B/ z) L" Q3 m if (EFI_ERROR(Status)) {
, e; A' V e9 `$ ^4 _0 }5 V* K Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
, v) ?! u' k U/ N' i" F4 @4 I return EFI_SUCCESS;2 r% X s8 q, n* {! D. t
}
1 P# E# E9 S# `. M# i) L. T4 w Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);0 S2 S( n. b* h( Q4 |
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<+ X6 j) Y" O. L6 |
n4 S$ T p; e
Status = FileHandle05->SetPosition(FileHandle05, 10); + z( s/ f$ a# W" H
if (EFI_ERROR(Status)) {$ Q H6 ~3 Z6 p+ z* c$ M. u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 a+ f# U( a3 [2 Z" g& N return EFI_SUCCESS;
, S. O/ t7 @/ S+ f }4 }" ?6 [2 [' E( D7 T- d" ?5 w
' b5 }" A {0 C
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& B" p Z0 v: l/ S4 @6 _
if (EFI_ERROR(Status)) {+ @% L: u1 p# X/ ?8 L0 J9 D5 R
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 A0 K: W# e9 C q
return EFI_SUCCESS;
/ ?8 j! o* O( w* r, u }1 C! S0 m: Y7 v1 U0 W
5 d1 i# m) `/ J9 c* `( N Status = FileHandle05->SetPosition(FileHandle05, 12); 5 f7 Z8 D7 I+ v4 O
if (EFI_ERROR(Status)) {7 j8 [6 e+ \( c F7 O5 |8 W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! _5 z. T. J. S k2 e$ |, S% ]
return EFI_SUCCESS;! p9 M4 D# T5 n1 m- W
}
6 R" ?9 P r# h' M. G7 r/ k 6 s6 _" a3 O- ? V+ d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
- p' }2 r. M( ~1 B3 h8 `5 n4 T if (EFI_ERROR(Status)) {
% m2 B3 B5 I0 a+ Q7 O Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ B" q1 M }1 Q4 c# } return EFI_SUCCESS;1 S! G, |) ~+ r% ?) F) y% V# U) A
}5 V& A, a. f0 b7 E& E; m
. K1 M2 f3 c0 K Status = FileHandle05->SetPosition(FileHandle05, 14); 5 m8 M: M5 f! A Q
if (EFI_ERROR(Status)) {
. s& K* u- r, a2 L) ~$ j/ E c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 k3 E+ t w; ^* j7 D0 i
return EFI_SUCCESS;8 E: i8 X& q5 v8 U
}) c0 Z5 A, h; f+ T& F7 [8 x" c+ y( ^7 E
2 v; c3 E7 y5 n2 K1 T8 V) f; j Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);# k3 a( P& p; Y6 o5 F
if (EFI_ERROR(Status)) {
3 ?7 K: \; X m h9 K$ l Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
( l2 L$ w/ B9 S7 V2 B& N return EFI_SUCCESS;
( u9 ~/ O% s7 j9 f/ k" j+ e }
9 ~# y3 Z0 g7 ~; M O; I
3 _# Z; u: \+ o8 h Status = FileHandle05->SetPosition(FileHandle05, 16); " e9 j. B" Q! x+ [) ]* A
if (EFI_ERROR(Status)) {" h w: C H% Y, d4 q m/ R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ z" s- c0 X* v$ E0 f8 x return EFI_SUCCESS;
# U7 c$ f8 d3 _ }
3 N. o& M s+ w! ? ; Q3 Q+ e1 t) K
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);& U( T# @; }) h/ J! ^1 H) L
if (EFI_ERROR(Status)) {: V- p8 v7 s9 b/ K1 M' F" M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) I e( x$ H: G4 c( u6 l+ z return EFI_SUCCESS;
, g, V s8 D2 |! i1 s) t; v } 1 t, h! J: D4 @) ?) j. o. V, R& W
( W/ D2 ] k4 D3 a# X- g//---------------------------------------------------------------------------------------------------------------------------------------------------------------9 ]( G3 \( m& \1 O" a+ u) ~( t% r" e
% i+ U& l- w. v$ a, d' {9 [
Status = FileHandle05->SetPosition(FileHandle05, 18); 4 T- d8 K; s4 R% p! G2 b
if (EFI_ERROR(Status)) {
$ t1 ~7 Y \( ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 `) y, n4 F, z* g, u
return EFI_SUCCESS;. T8 j1 R9 e F. x; ?' K# F# o
}4 B) M! b @6 O/ B: n D
4 f4 \' `& _, u& X7 G. U Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);& R" \* G. X8 \% b* a C# R U) C
if (EFI_ERROR(Status)) {
+ |5 T& u2 w9 n/ Z. ~3 ], l Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' C x# ?) Y, N( |5 H) A% c2 c) P
return EFI_SUCCESS;
E0 m6 Y4 \2 P1 j% p' W( v, f }
Q9 G/ M; s+ B$ l: d6 M - _/ _7 m1 o3 h/ G3 z
FreePool(DestAddr);
. R1 l4 Y9 }: P: `/ e0 t# `% _8 p" C5 q: K
Status = FileHandle05->Close(FileHandle05);
7 P* i, S. R8 S7 V' O! [4 P0 u if (EFI_ERROR(Status)) {
) C% ^0 G% P7 A Print (L"Could not close file, FileHandle05 err code is %x\n",Status);: h( L% p4 Z) y+ K8 m
return EFI_SUCCESS;
/ W; e! A+ f5 A/ e; i( R }
- \, o/ E+ d( Y7 G " D8 q8 V; z7 \& d
Print(L"File Name = %s has already been created.\n", FileName);- x) c* n* v2 e) x5 g
! e" @8 M7 Z* g/ X _' P9 S3 ] Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
; y' B* ?. f% k& I! N' t WaitForKeyOrReset();
$ g+ G2 \" B, P1 m$ s $ r$ Q5 W* h/ q# i2 K8 ^# C9 I
return Status;/ g: O/ ?7 M3 C+ c; a3 F
} |
|