|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
4 q5 T c" d) V# T" L * myfileio.c7 Z! W2 \* w% |' E
* Apps
7 x# f( Y; `: @5 s1 b7 Q */
! c# w0 G* r( y g+ u9 T
0 ]0 ], s1 k9 H( l, N#include "efi.h". d5 F; Y& o) ?7 n3 B
#include "efilib.h"& d# M7 n4 F: Z0 e- \9 s( W; x0 D1 F
# l! v1 ^# d9 ~
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
! A- e$ @% D" D9 |- `$ }
6 Y6 |5 h! i" W+ Y! ~" O6 Vstatic EFI_STATUS WaitForKeyOrReset(VOID); v3 Y9 N+ W2 j) v
{- Y' n0 P2 C. t* j: k6 a0 L
EFI_STATUS Status; f: p3 Z. A& ?5 o
EFI_INPUT_KEY key;
* n. B( J0 b) T+ H2 i UINTN index;+ d; {) ^& Q% J5 k& p$ ]/ u
0 B9 V2 s$ P+ T6 ~! y3 P
for(;;) {; O c h W9 }* g( ?0 E2 T, b
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
0 T5 L; k; S2 k0 @5 I+ i# K if (Status == EFI_NOT_READY)
8 ?+ Q5 e& |. ` ^3 _% p' E. \ BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
& V9 q. ~6 p a% k& C" i1 q3 m else4 Q# U9 A, I: q% O9 _8 O
break;+ f, b& O( k5 q. ~
}
0 P$ W3 }( @7 N& ] if (!EFI_ERROR(Status)) {
& `4 q$ `- m6 I1 m7 C if (key.ScanCode == SCAN_ESC)
! p! e& s' C& e' V% o' y+ { RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
1 d! R* m0 R3 i! E% _3 ]8 y }
! ]. X5 a: F* x6 o
7 R% u) k6 ?5 a2 `$ T return Status;5 r. \! i0 d* C. e$ `/ @6 f( C, [
}( _# I7 l) G6 i! G* [# p/ h
, G, [$ ^. [# wEFI_STATUS; E5 |, z( b5 Q9 E" h4 }
EFIAPI+ D" v- d! w. u, j, G
MyfileioMain (IN EFI_HANDLE ImageHandle,- G5 G/ ]$ z) w3 V1 r8 _
IN EFI_SYSTEM_TABLE *SystemTable)
8 |3 N( \, j% U* v( a3 p# q{
/ g) I+ M- d9 b* `& N EFI_STATUS Status;5 ?! ^# P" p% t/ V- c! W
EFI_HANDLE *DestAddr; - z* W1 I" n* m8 r& b7 I
EFI_LOADED_IMAGE *FileHandle01;7 ~) K0 U ]6 v( Y
EFI_DEVICE_PATH *FileHandle02; 8 |4 _$ o2 f% h) S& o5 @, h7 y
EFI_FILE_IO_INTERFACE *FileHandle03;, v$ |! R8 }' o: V# L$ t- y
EFI_FILE *FileHandle04;
" K# \, G' F d: } EFI_FILE *FileHandle05;
' X2 u$ v$ h. Q" S" o, \ CHAR16 *FileName;- G- v- S# d. s: _) ]0 G* B
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;/ _* c r6 c; e) z# o
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
% L; c/ @) L4 U. O" L int Bit0, Bit1, Key_Space;
: B) W% z3 l" g1 {7 s0 m# C
$ f- N2 r/ t1 z: g1 R1 i FileName = L"NewFile.txt";
" `, j, U2 n2 Y Space_Key = L" ";
- C B! Q0 B% o6 ~' s - w1 J* D, X, z5 Y
BufferA = L"ABCD";1 x9 N x: V2 Y' O
BufferB = L"EFGH";
/ u, s& h8 v) M( u5 P BufferC = L"IJKL";
! ]8 q4 e! G9 s3 ]* ^5 p" D/ m9 {; O' { v& L
Bit0 = 0xff;0 L) T; w& m' V! F& c* o1 ~1 S
Bit1 = 0xfe;
. i* I9 {$ E. T
7 J% X4 K* J8 s; J+ H6 L- V Key_Space = 0x0020;
9 _7 V; N ?( u: _$ _, P
* K [- x- E0 M9 L# A: B1 Y InitializeLib (ImageHandle, SystemTable); 4 W% \' r. O3 U1 E9 A9 i4 B
/ [. `1 ]) ~' @1 Q) w* F3 V& ]: K$ Y
DestAddr = AllocatePool (BufferSize); 2 J1 F" v# K, }. }' J/ N ]
5 } E% V- \* Z" v$ t8 f* [4 { Print (L"Value of Bit0 is %x\n", Bit0);
5 G8 Y# n$ ?- B4 @. d Print (L"Value of Bit1 is %x\n", Bit1); 4 o4 E: k9 v* T- Y( a3 K8 e
; e' y: s+ J9 }, b; |: W # j& q- V2 t8 y1 ~( @
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
4 \* [1 b8 [* |( q) u if (EFI_ERROR(Status)) {' e1 w* |" }/ K4 ^3 C7 D% [
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
2 y6 O+ a& G7 ?7 L return EFI_LOAD_ERROR;
9 @5 W4 X6 d# U5 L) u, _) ^ }
) ]& ?3 `, I9 u6 q$ C! U4 E* N3 _0 L$ M3 m
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
; r2 T. {5 P. g/ ]( g if (EFI_ERROR(Status)) {, s f% C( ?+ _4 h) z/ t
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);8 ?2 v" L7 H0 S( Z3 O
return EFI_LOAD_ERROR;0 f, X$ A' |2 _8 q5 r/ f
} ! B1 z' V* T R, W
- ?# x% K: q5 B1 ] Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);- Q$ E0 p: O* N& N( D
if (EFI_ERROR(Status)) {8 _2 @6 _& X" O" B2 ?# ?4 A6 y
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
/ \, S" U) b' @ return EFI_LOAD_ERROR;
# ^( ^5 \) K2 q- h( n8 E3 [5 f }
9 J# ^ C$ ]8 c/ t+ p( }. f S9 F9 ]' f1 r; D9 ?! Q& Q
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);+ l% F7 F' _1 F/ A6 I
if (EFI_ERROR(Status)) {
6 p( B) K% c" ~9 e. @- U Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);) N/ f% H: M! F. T, C
return EFI_LOAD_ERROR;
l: u) @/ i0 x2 h& g6 X7 ^ } ; V* I2 u. k7 K
7 L$ Z0 Z8 [+ u6 {% |3 `
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);0 ^; {& I# L1 p6 l
if (EFI_ERROR(Status)) {( U+ D `7 ?0 R/ g$ Q
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);0 X+ C0 B r. Z9 u1 V: [0 J; [
return EFI_LOAD_ERROR;. \9 B. J7 W$ L6 d
}
; j, t! T3 d8 |' f8 I8 @7 Z : O: @8 ^6 c2 ]( R! ~
Status = FileHandle05->SetPosition(FileHandle05, 0);
' g9 o R( K- [5 F, y5 X if (EFI_ERROR(Status)) {$ y y( F% k- o
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 l( s/ T# [. ^: z
return EFI_SUCCESS;' @6 ~' l$ y( g$ k' G
}
1 c2 ~0 [& ?- B2 q! o ( P1 G& R! `! I# h4 [4 a2 [7 Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);2 D: E7 _1 i2 w
Print (L"File Buffersize is %x\n\n", BufferSize);$ C' q/ p+ _7 x3 J* x9 h
if (EFI_ERROR(Status)) {5 ^8 {% `9 ~& J+ s: [0 T
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); [9 s- \. c. b/ i
return EFI_SUCCESS;. N; N# G z7 N* J7 u, A: ~6 c
}
: B8 C# d" m$ J* E) G3 |
) O3 G3 A1 e; J1 c+ b Status = FileHandle05->SetPosition(FileHandle05, 1); . @. b) S3 @$ g( w7 |1 I
if (EFI_ERROR(Status)) {
! g, @; ^4 o, H1 _/ ?8 B" ?( v4 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 x* p0 r8 p5 E) a
return EFI_SUCCESS;
5 r+ A8 p* Z: }) [ }
; y$ f9 m, }; w H3 ~3 j
4 B/ ^5 Q7 }* m3 t0 R c8 f6 ] Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);) f, M' \, Q, n' f
Print (L"File Buffersize is %x\n\n", BufferSize);
; h9 B- d2 R3 I' L if (EFI_ERROR(Status)) {
) t2 S# k7 ~$ _: S Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); C0 h- R# N( n" s7 Y8 b8 Z% J
return EFI_SUCCESS;) _& \! Y- {6 C: o
}
1 Q8 g; [* e* V
, ^# e* @9 r" A" {9 I Status = FileHandle05->SetPosition(FileHandle05, 2); # w/ f2 \& z6 M+ e' \8 ^# [* ]
if (EFI_ERROR(Status)) {
" m+ i! G; u0 x' m. _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' x7 O: |( K/ R4 @, I8 u
return EFI_SUCCESS;1 ]3 v7 h$ F+ k
}
4 R8 j z5 m5 ?& F0 e! M 5 S/ `6 @. j v* D8 e
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
3 \" I4 a* x! [! g' K# h' m( [* C Print (L"File Buffersize is %x\n\n", BufferSize);( b1 ~. Z1 `2 [5 ]6 Y. i
if (EFI_ERROR(Status)) {5 S" r( g) \) n: [6 A
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
9 @1 m2 n3 ` R2 j6 V; |$ s return EFI_SUCCESS;9 S y8 E4 u: i
}
: S7 O3 \0 d: X% q1 H1 t3 w4 z
! T9 h6 Q8 L/ c( i//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>, i+ \& O. z5 Q
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);1 j0 c9 b" l3 i1 ?$ i' q+ d; _0 o
Status = FileHandle05->SetPosition(FileHandle05, 2);
7 o" j v' \' M, r: U" d. @ if (EFI_ERROR(Status)) {4 ^; w$ `% \& s
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ W; Z$ Q0 j' A3 t6 _ return EFI_SUCCESS;7 F1 k- B" N! X* r
}# h( a; _5 q) p+ Z6 [3 I6 M
8 l: R1 F; q5 i7 z, e3 H7 \- a
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
( I1 F4 T1 Z) w7 M if (EFI_ERROR(Status)) {; a4 f- ~9 u& N2 V+ G
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
* r( ]1 I a w3 D; _8 E1 h return EFI_SUCCESS;
0 Q0 E4 J. _# h& w9 Y0 e' M } % B% V0 c/ a' h0 w: \
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
* W) a! s6 l6 o//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
( H2 ?. j5 G, s8 ]9 A" l7 _/ k% B7 v$ u& j! z
Status = FileHandle05->SetPosition(FileHandle05, 10);
2 N" B3 \& Q9 @3 z5 D& u. u if (EFI_ERROR(Status)) {
& C7 ]* Y4 S+ B. |4 o# q% V! K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 T& [ P9 _8 V7 P2 p return EFI_SUCCESS;& ~2 K4 }8 k- E# g6 s
}
?7 c0 Y" K; Y3 ?' n6 w
5 g9 P- S1 {5 x) G/ H$ {; } Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);, I7 i ?: Y0 o: c# D h& b; E* v
if (EFI_ERROR(Status)) {
/ W, u5 h% v; E% f0 {3 n g8 x8 o Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ ]' L: T9 { |7 Z. q( l return EFI_SUCCESS;" r, h; |+ T, d1 E9 Y
}
7 @" y% p C: D, ^6 j* O; J1 R: u* @" u5 _! `9 i( o" A7 j
Status = FileHandle05->SetPosition(FileHandle05, 12); - |$ e/ j2 T3 }, i0 \7 {1 Y
if (EFI_ERROR(Status)) {
$ b0 h5 k' x* k Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( @8 I) L7 ]5 E7 @' P
return EFI_SUCCESS;" p, j; ]1 o) f7 w8 C: O
}3 v4 S6 V# a9 R- e4 s
3 N4 V- ]; C8 C3 j, y ]! m Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);6 ?) x* m% r$ g/ F% p" p1 j, N
if (EFI_ERROR(Status)) {
. r+ L; q% U- F1 w+ R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) d/ J( B' V" }1 p return EFI_SUCCESS;' z# e. Q+ N2 Z8 C3 i k% Y
}) k- f, B0 }% n. e6 I
* `. S1 u8 o" x3 C; y Status = FileHandle05->SetPosition(FileHandle05, 14); 5 n3 x: {9 o" y4 }+ J
if (EFI_ERROR(Status)) {
9 k: m3 t! ?; G; V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 G `* \2 S7 M9 p return EFI_SUCCESS;
: e) o2 m9 Y Q) S/ s% r }, [1 O! ^, H- c$ t$ ?% Y- K
V, J i+ D" B( K5 }# [: F Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! f( h" R* C. L' i# @" u
if (EFI_ERROR(Status)) {
T. q, p5 q% D/ q# I; `7 J- {! z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& z0 P5 m1 ?0 ~( v5 h O" k1 N
return EFI_SUCCESS;- {( e* Q" u* d3 B' g
}
7 v A z5 N$ I4 r
! o f0 z4 J% o# a1 L8 p& H: D- J Status = FileHandle05->SetPosition(FileHandle05, 16);
. h7 o. k1 L; ~0 }6 [ if (EFI_ERROR(Status)) {$ P7 E) z* N9 r5 e5 ]$ l/ c
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 ~9 @! p5 T) v2 i1 |
return EFI_SUCCESS;
$ {! n# ], r! E% f6 C9 m( `* \# G, r }) A4 A/ k* |* @- `
6 k" J4 ~4 b- B( [" a& g% }
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# u) F7 Z3 g% `9 i" F6 e5 U, n if (EFI_ERROR(Status)) {
$ y+ N) F0 E4 Q* D4 d Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) k3 l2 R$ P/ _% v2 ^# J ?; c return EFI_SUCCESS;) M; [/ u, z9 ^
}
5 \" v9 C, \" I u5 b+ q) k5 c3 F$ b
+ j6 f* N3 C4 y l( Y% {# a: Z//---------------------------------------------------------------------------------------------------------------------------------------------------------------
; y+ t, X' h+ i- z# X w) G$ I
( W5 S; g% s4 O G( _: |3 s% d Status = FileHandle05->SetPosition(FileHandle05, 18);
4 ~3 Z4 B; l7 a5 M5 Z4 e if (EFI_ERROR(Status)) {4 I9 b4 Y1 B+ M! R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" F1 ]- h3 c8 u9 t0 e, V2 y
return EFI_SUCCESS;
?, n, E8 D) H }
2 }; M0 g& F) f8 N* @# P+ x1 A
8 v: M8 S$ l: r" r' X" W Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);" f: e0 B- h* c
if (EFI_ERROR(Status)) {
7 P' |" b' f. J! `5 o. h Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ N% {3 D/ ?! I1 n; C, B; m* w3 a
return EFI_SUCCESS;* B) A- s6 |+ Q. [' _
}
# W) F) Z! M9 O1 S
; N+ X# R: }: F' c7 R/ W FreePool(DestAddr); ! i7 A3 @; G. ?4 r3 U. Y W
" {6 T) F# C2 L; {% P! _, T Status = FileHandle05->Close(FileHandle05);
% l$ j* B$ A+ q! l9 B if (EFI_ERROR(Status)) {# q. w7 J1 }% W$ l& P4 K& y# W
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
% h. c: c8 F8 {0 o7 k return EFI_SUCCESS;+ i/ f$ Y- K/ }$ Z: ?: Y
}
# K, Q) v @! {7 T$ \% R/ V; D
! w9 J4 W, D M3 \/ B+ b Print(L"File Name = %s has already been created.\n", FileName);8 W/ `8 B; y9 r) s2 M* p3 o7 y# W
; d5 x+ [0 ]7 Q, j1 ]0 j
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");2 p# `4 f0 Q+ |% I. J$ Y: t6 S; R
WaitForKeyOrReset();
0 A: n1 T. s# q$ N& L% I7 I8 G5 h
4 l6 O2 y( p0 L return Status;
; R" y/ P$ \" e! r/ ]$ d} |
|