|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
9 w+ S: _4 u5 G; v1 i * myfileio.c- X8 f! Y. `! ?/ K* m- U
* Apps
6 Y" d. A5 ]' t( d6 Q$ p) H8 A; Z */
6 P. I: ^1 ?- R+ b7 x0 T+ x: J: O3 \- M. z; {! \# C8 j
#include "efi.h"
! p( m) F- n5 Z#include "efilib.h"5 n4 M% a" A, `& s
( Y5 n5 a/ D7 f) P
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
; M K! z7 x1 b# f/ G7 ?' y/ \4 u" I: C) C, w, F) p& T+ F( m
static EFI_STATUS WaitForKeyOrReset(VOID)
, J j1 Z! E; q4 [1 V{
: Y* J, C2 e: A2 o/ f EFI_STATUS Status;
6 D: ^1 p3 j. ?$ G" B+ B EFI_INPUT_KEY key;; E, }. y" `* W8 T
UINTN index;8 b4 C8 `, B5 j
4 R% B' @# Y" C& s1 d9 \ for(;;) {
- {. M2 C, S3 G7 G& D Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
! `5 _- I6 C- a& H x if (Status == EFI_NOT_READY)
1 H% X" o" R& Y BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
% w* l7 v1 s& Q p else$ _- E& N& O/ j9 b, [
break;
& i0 E" K+ x* t2 F9 V+ P" X6 N# d }
. l& h$ G O& S. R- _ if (!EFI_ERROR(Status)) {
/ K! C% B2 U; J0 L! u2 h. R if (key.ScanCode == SCAN_ESC)
3 A! w4 G2 Z7 Y/ W9 k/ t8 D RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);- J- h" ^* S E5 E5 X
} i4 e9 P {, i$ X2 w. o
/ D* s8 e# H1 k return Status;
" |/ H! l# H) w( i a( I r+ K}; j1 p" W& j2 z. f6 M( Y7 Y2 r$ G' i
- G8 Z! |7 D) G! j8 p" }0 s5 B
EFI_STATUS
4 Z& q1 f! l4 ^& _* cEFIAPI
) n, b7 L" v* d/ y' R( U4 M5 UMyfileioMain (IN EFI_HANDLE ImageHandle,' N( l, n. y" f2 x
IN EFI_SYSTEM_TABLE *SystemTable)
f4 X, S2 N" \; p{' ]2 o" @# J8 ]' I
EFI_STATUS Status;$ o/ Q, u7 |! d; @
EFI_HANDLE *DestAddr; 7 ~- b% W. L! _$ N5 {
EFI_LOADED_IMAGE *FileHandle01;9 {2 W/ R! C9 D3 Y% v6 a1 L" O2 w
EFI_DEVICE_PATH *FileHandle02; & r# A# y6 E n& W- P% ~4 e% w/ x
EFI_FILE_IO_INTERFACE *FileHandle03;
* w7 K2 `- {0 J EFI_FILE *FileHandle04;, y5 J+ B5 u+ u% \
EFI_FILE *FileHandle05;
) i9 m6 O/ V- n; d0 ^3 O CHAR16 *FileName;+ T" |: N' \7 C2 o, @5 ]
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;$ h/ ^: z) w7 ]$ m F. w/ }
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
) q' K8 d; ]7 ]2 U- d int Bit0, Bit1, Key_Space;
( s% A0 K& r, H3 l- n5 b0 p* t8 J
7 ]( {( `" @. \ FileName = L"NewFile.txt";
2 ^& M, |5 S. r1 s) }$ p Space_Key = L" ";
) r' S; z4 ~4 y/ L' E8 t% y9 M
) t4 T* D( {. H* X8 [% q BufferA = L"ABCD";) J c9 t5 s- `# S: `( r1 H- A6 ^
BufferB = L"EFGH"; K) M: b9 G; _5 v& w) g6 s( q8 Z
BufferC = L"IJKL";5 N7 ?- D, c- Q' v- g8 T
+ V$ g& J$ `& I/ k2 }9 D' l$ D Bit0 = 0xff;
4 C; j% T2 N2 `& m Bit1 = 0xfe;) \; _) l4 O. C5 G# d( W7 U5 t" W
% ~! i, F% Z7 H! D8 q Key_Space = 0x0020;
$ H; Z% e$ P1 M* F % I2 ?) _1 @2 `
InitializeLib (ImageHandle, SystemTable);
. j; n3 H2 _0 A3 _0 Y
2 w" q, d; G' n1 @# j DestAddr = AllocatePool (BufferSize); ( s2 `! b. v, ^7 x: O
! a6 v2 {5 i( `1 V: `4 l8 v Print (L"Value of Bit0 is %x\n", Bit0);
8 m- M+ M0 _. V* E' o: e Print (L"Value of Bit1 is %x\n", Bit1);
$ A$ v# |& s: k) F# _/ W! {7 G0 M7 i& C* ?4 i
3 A6 p: @$ g1 R' O3 Z1 ^3 ~ Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
: g- V+ Q7 Y. a1 M if (EFI_ERROR(Status)) {
' I& |) m% U6 a$ n Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
" a7 P2 v5 h% G& t4 H return EFI_LOAD_ERROR;4 u y6 O( W$ Y' r
}
' `+ v" P3 D% U' b2 y- R* l. y. |0 B
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);) K$ R+ w: X$ W$ ?
if (EFI_ERROR(Status)) {
2 c3 ^) I3 k$ `) S, H Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
/ e) t* |$ A; g- G3 r; e" j return EFI_LOAD_ERROR;5 y* ], k$ b- e I7 p
} $ ~4 O2 O8 R4 {
, L; q/ l2 n( C h! ]
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
: c8 R _" G2 }. S3 J8 `: I if (EFI_ERROR(Status)) {
' [1 Y) `) ]. t2 y5 g Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);5 s6 J3 a7 o! r/ S$ d
return EFI_LOAD_ERROR; X' o, U! u# S* E0 n0 w& M
}0 I) E0 D4 N$ v8 [6 l* M. `# x
6 H9 ?$ G1 s& [. k) r9 r Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
4 ^$ K0 `; t W/ C if (EFI_ERROR(Status)) {6 U2 H. d- k8 B9 t1 W7 m- w- G' w
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);% B( X( C+ s& [
return EFI_LOAD_ERROR;
+ F2 M t+ @8 c, V7 o( M }
3 M" |6 E. A8 _+ }% ^* j
/ Q. k, @' B- r Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
, S8 j, L' ?. V2 c* k$ q/ U5 e if (EFI_ERROR(Status)) {
$ k- D+ y8 m7 j& U Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
6 [' E2 m+ e% Y1 B7 g7 R+ o$ h0 ] return EFI_LOAD_ERROR;
3 \& O" [- y; v' u0 C$ K }
7 _* f) {5 i) Q' _; q% _4 W+ V: E 2 `" g) x, o5 M( `. z2 J8 V
Status = FileHandle05->SetPosition(FileHandle05, 0); : C. i6 Z, V7 Y. P9 w; b) T; P2 U
if (EFI_ERROR(Status)) {, _8 p: ]2 K4 x' B1 H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' }- r$ i3 h7 H, j, F: G
return EFI_SUCCESS;
2 |/ j; i/ M4 p) h0 g) }/ k. x }
: t: b* Z* L9 j. d
1 |2 ]" g. C, y/ v2 I) ?, ^ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
. r! W! ^; `% i$ z8 c Print (L"File Buffersize is %x\n\n", BufferSize);) {1 Q# R! k0 @4 [8 i+ X
if (EFI_ERROR(Status)) {
0 _' R* k, g/ Z1 h! E; p Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); W( ~ n% ~. m% H; s" ?7 G
return EFI_SUCCESS;
# ?" I+ w: T- o k/ V$ |7 i3 f }
8 u- D' S" P" R7 C7 J% X6 G! l/ [) c1 T m4 D" j/ J
Status = FileHandle05->SetPosition(FileHandle05, 1);
$ S% W' z% G$ d0 t6 C( M9 U, p if (EFI_ERROR(Status)) {
* y ]; V. e8 z; r* w Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' _1 t% x' s$ ]6 i- x0 A7 r return EFI_SUCCESS;
! l$ Y) `+ K6 n; q; t5 Z# v }
+ c1 \0 }0 I; }6 N8 V9 P+ F) C 2 u* J3 D P/ v8 w- g9 j4 c
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
' C; Q& Q7 c% d3 Z6 } Print (L"File Buffersize is %x\n\n", BufferSize);
: N5 |2 \+ @' A) }& D j+ _# S* x if (EFI_ERROR(Status)) {. F9 u1 D8 k+ v w
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& M; k0 j B6 k5 D( W return EFI_SUCCESS;6 H, q# e/ \, ]1 N
} ! w r- ]0 }) [8 S. N
" I% e8 W- W8 \- I) d
Status = FileHandle05->SetPosition(FileHandle05, 2); & n5 D0 ]- d5 O1 s8 L9 j% p
if (EFI_ERROR(Status)) {
: J* P, C" y5 m- x: w Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 B! T: M, R% [. v. T/ _# {; H
return EFI_SUCCESS; E# T7 Z, T5 b) T- g; u5 j+ K
}8 @6 h/ {; S# q) W0 G% |
4 |# R& \2 F7 X% B3 u, n
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);- D* M0 x5 Z$ g+ p, y7 R
Print (L"File Buffersize is %x\n\n", BufferSize);
9 Y, D3 X) L; x. [4 g1 L if (EFI_ERROR(Status)) {
' M" j7 \) X+ h6 B7 i Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);4 y" k( G {; m6 W; v
return EFI_SUCCESS;
X7 w4 }$ m+ A/ M3 ` }
/ Q- |6 e& d6 D+ a4 h3 q8 h
" o' Y0 k; }* u$ K+ d O//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>1 Q8 C- g S! k
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
* q. g% x" d: l7 |7 K Status = FileHandle05->SetPosition(FileHandle05, 2);
+ l% p( U' ~. Y if (EFI_ERROR(Status)) {
4 |% H* n* |) l5 Z% P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; B4 C/ `3 `. F5 Y2 i& e4 @ return EFI_SUCCESS;
3 Y; {1 H" w, G1 O }( S3 j r' h2 _( Q' ^$ ^! a6 u; f
# E( D0 x: Q# K$ F | Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);) _& @, I% t. L/ G; Q1 u" M5 ]
if (EFI_ERROR(Status)) {/ w; K9 i, F$ \) s( u
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);4 w5 l/ a2 o3 V9 i* r
return EFI_SUCCESS;9 k( B% [, W e- f- {
} 5 ]) z% h% v4 e* Q- n
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);6 `0 ^$ z+ s9 x2 D6 M: v; m
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<9 p) D0 S! {; e' _, f2 v% t
! I5 e. k5 K- p/ T Status = FileHandle05->SetPosition(FileHandle05, 10);
7 j, N) D- t, H) f% f8 N2 {5 { if (EFI_ERROR(Status)) {0 B( @% }, K! s' R4 q" h5 f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 ]5 \( w1 Q1 e/ N9 _) q return EFI_SUCCESS;; Y% g0 \# e8 n
}# L4 {: j! @7 L2 \4 O
3 O; F7 R) H, {
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);2 R# x% C m6 c p6 C
if (EFI_ERROR(Status)) {
2 W, H f+ {; J6 ?7 F! K Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; U% c5 @* L% n+ o
return EFI_SUCCESS;, n+ s: l9 {, C3 m! u
}% b6 x% k" x: ^6 R- N) y0 o
# f. b/ g: v% {
Status = FileHandle05->SetPosition(FileHandle05, 12);
- [, m3 d* h: {8 Q9 R% ], r) \ if (EFI_ERROR(Status)) {" y6 F% X0 P! ?
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 t0 h$ C" Y4 N return EFI_SUCCESS;3 t: u5 R/ F6 W F( _' c
}
' L- A: ^5 n& c1 p( S- r* v
! |2 e8 a" ^. d1 I% l Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 F, U6 d: a) k' K6 b" X if (EFI_ERROR(Status)) {. e0 m `( r7 O: g
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ n1 L' }; Q; D, i5 a return EFI_SUCCESS;
3 @; s7 G: c: ^/ I. S* [8 m0 u% ^ {" l8 z }
$ [% r# }; [# }. k( N: w' {6 j& c `1 ] b
Status = FileHandle05->SetPosition(FileHandle05, 14);
( T$ Q0 _8 q, a u if (EFI_ERROR(Status)) {
. K2 F0 i- n J6 V4 ^9 [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: _. x( y: j4 \, w, Q% U return EFI_SUCCESS;& Q! Z; x* K0 ?4 j$ j
}
2 F$ m& w3 B# r - P+ I$ I+ |+ A2 O0 A
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
) n7 p: _! @0 n2 ]+ R) y2 k2 f/ h if (EFI_ERROR(Status)) {. Z$ D: n8 o# A1 U
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! ^( r/ l" y6 D+ Y' C return EFI_SUCCESS;5 k7 K+ t& m5 @9 M) B
}8 M$ `9 }% t+ v! Q, m! ^
2 w/ |$ `8 |8 [0 Y6 s; @3 M7 c3 r
Status = FileHandle05->SetPosition(FileHandle05, 16); 4 _* M8 |+ |9 v1 R
if (EFI_ERROR(Status)) {$ j6 p, P3 a6 Q8 D( V: j4 e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% d0 z2 I, }5 p+ j3 r
return EFI_SUCCESS;
6 a" }8 x" p4 ~, \8 t" D, _& e }
: c2 p O6 ^; p8 {
; r% ~4 M! S- V9 N# a. z+ F Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. ~$ T' w. e& H5 v l if (EFI_ERROR(Status)) {# V5 n: Z) A5 I# N2 \1 Y$ ]9 n$ }
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" Y8 v, Y: C1 X/ X return EFI_SUCCESS;
, p9 ~0 P8 S( _4 y0 ~# B& B } 2 Q/ C: K3 X) F- `* e- w/ I
9 v9 ~! y. T P. W* X2 z1 T! r
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
& X2 u2 h- h" U- f( w1 i' k2 O; ], U" G. g9 O# ^" c3 N
Status = FileHandle05->SetPosition(FileHandle05, 18); 8 }& H2 \- e) {: B
if (EFI_ERROR(Status)) {3 a0 @1 e6 c5 N: Z, d) z. n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 H, b% s# j" q( o( } return EFI_SUCCESS;+ w7 x, E, l0 w4 P+ g( w
}
9 c4 P1 y- _7 ]' D! e: H6 u7 n! J* \ ' z- |, n2 x' s( d6 s
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);2 K5 j7 L Y% h3 Q" l
if (EFI_ERROR(Status)) {" K) t1 H# J8 e, a. o7 `9 ^
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% [) ]% y6 @& K5 o7 C
return EFI_SUCCESS;
/ L: [' X& {* K3 X- X7 n } 6 v" |3 m; ]. K2 v3 U
5 @8 B$ F/ L$ F( P- A4 `$ Y FreePool(DestAddr); 8 s' i5 m" B& M' D/ w6 O4 c
% p* ^" B4 k1 A8 |; A% H; L
Status = FileHandle05->Close(FileHandle05);/ q1 F+ }9 D7 i9 K7 `
if (EFI_ERROR(Status)) {
! ~' _* d' H9 v7 q: U Print (L"Could not close file, FileHandle05 err code is %x\n",Status);" i$ ~: N1 u; W
return EFI_SUCCESS;
1 E/ Y& _, D/ A& ~ }
$ m, k. U3 a$ K o4 o. M( C1 \: T# v) x5 ~, ^
Print(L"File Name = %s has already been created.\n", FileName);
* |. f/ w7 X ^: e' k- g/ l8 t- p# N3 m% q; W7 U# F: y, P
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");6 B7 ?& b2 ?% n, f p
WaitForKeyOrReset();
4 R# y- f$ F) [0 K3 D
3 m8 C! s* v: K/ r& G return Status;
, x8 b# D" S6 M2 H8 D1 V} |
|