|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
7 q& Q7 i1 e, x6 V7 ^1 O9 A * myfileio.c
' g8 l, e- I" X. } q# @# o * Apps7 Q7 Y1 {* b1 J( R$ X N% ~, l
*/
# j; o' K0 N4 g# ~
( ]! J4 @; g) ]#include "efi.h"
) f+ o- X& T' _5 Y; b#include "efilib.h"
5 j9 r3 M2 K9 H% l/ T3 g& h+ x. N7 V/ U
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
, x, T0 y7 d) j+ `+ `" R Q8 `; d3 g: B4 Q- y
static EFI_STATUS WaitForKeyOrReset(VOID)9 C4 m; Y( {' L7 U; R6 f8 B* h
{) y! j4 {" e- Z( [
EFI_STATUS Status;
3 M9 A: b# T9 S6 p- D' y6 h( `( q EFI_INPUT_KEY key;; N* |, A+ n2 M$ `
UINTN index;0 Q* f/ S* _( ~
, J$ f# ?. H" _( L8 b for(;;) {" Y+ ?: i) R: w ~7 i0 H- k" d9 d
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
: l2 q4 p/ b- v if (Status == EFI_NOT_READY)
' V8 o: t9 `) s8 X BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
% d- W* p. L- k) H7 t! E else
' \6 q4 V# n" A: E. i S8 W break;& b! U4 E3 h! u' q
}8 n# Y6 b T \2 J. W
if (!EFI_ERROR(Status)) {
$ O& x6 M9 ~$ R3 o! Y1 }( h+ q if (key.ScanCode == SCAN_ESC). {1 p F0 J" I6 ?
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);& X% \& f* R9 a; V
}: c$ ]6 C, [' p2 H
- j5 j0 p' Z) K9 i: v" j. a. X b
return Status;
3 P4 x5 r5 t: j}8 R1 r8 v- P1 a/ w
* P; A8 B) ^- ^; rEFI_STATUS: f* w& z# w8 |
EFIAPI% b5 R" Z; j1 I1 L" p- e' Q
MyfileioMain (IN EFI_HANDLE ImageHandle,) Q4 X: G& |( \: g- K
IN EFI_SYSTEM_TABLE *SystemTable)( u D$ q; L `. Y4 A$ _
{
5 y1 r% q' }9 O' S# O4 f" e EFI_STATUS Status;6 X5 j, K6 L7 ~" C3 i. @
EFI_HANDLE *DestAddr;
6 b6 x- W, Y6 ]2 T7 ^ EFI_LOADED_IMAGE *FileHandle01;
/ ?$ C: U" E- U5 k3 ]& a4 l EFI_DEVICE_PATH *FileHandle02; ( j4 ?+ |/ N* B
EFI_FILE_IO_INTERFACE *FileHandle03;
" z1 Z* ?3 I( {9 ~/ { Z5 t4 C! V EFI_FILE *FileHandle04;6 C1 q0 Y* F) i# r# ~" E" J
EFI_FILE *FileHandle05;4 C) Q+ E- b1 K/ t2 x6 u
CHAR16 *FileName;
" Q# v% v/ U6 U: r+ n J1 Y, b CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key; x1 C- @; c( v2 l0 J8 p
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
9 I# E' ]% H) a- n6 ^9 d int Bit0, Bit1, Key_Space; 4 X' E+ `, K7 H+ _
) T% V d: u& Q' k W7 c5 K% T
FileName = L"NewFile.txt";
& P5 d6 S' V* K- w. T. [ ? Space_Key = L" ";' r2 ?1 j. j, I% b# N$ \
n- g P( K, P9 f
BufferA = L"ABCD";. J: v- H% X7 k# A% ~' j6 N5 g
BufferB = L"EFGH";
& s: y$ s3 W0 H2 @/ A' L4 _4 G5 W BufferC = L"IJKL";
C7 b h+ _- X" [7 U
; `# f+ ]! {' k* ~4 O) z Bit0 = 0xff;, z R* H5 [ Y6 ~7 Y, P+ C9 O9 I4 M
Bit1 = 0xfe;
* R! p3 M0 _' O; Z " T( Q9 r( R" c# f8 n5 I
Key_Space = 0x0020;
9 n" [6 l# P( ?; N4 [; U6 |; K9 L " \, i+ V; s" A- X
InitializeLib (ImageHandle, SystemTable);
, Z1 P* I/ c9 p5 R' u6 |7 }# m: `, w
DestAddr = AllocatePool (BufferSize);
; w( g" M& v; M8 }/ y% \8 R0 J& g& i% b5 e- g3 c, l0 W- _6 B
Print (L"Value of Bit0 is %x\n", Bit0);
* g5 R k# s. Q& T Print (L"Value of Bit1 is %x\n", Bit1);
1 B8 @( h7 t. U! H6 D; C* ? ?# Q' \/ N/ j
2 f$ H1 C i8 S. {
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
; l% b& v2 X( g1 i7 i if (EFI_ERROR(Status)) {* s6 ^: x5 I% t: Z: p
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
1 E' [" u9 B- Z return EFI_LOAD_ERROR;% ~9 O; p( U& g1 Z- F
}# u' | \! k4 J. S; W+ ]9 Y5 j
* l4 p# J& {, f- ^" z Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);) Z! X5 @8 j4 ^+ `) w
if (EFI_ERROR(Status)) {9 ~+ l1 Q, A m) J1 m1 n1 Y/ \% }
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
0 g: o2 \5 |! D5 t/ q7 d1 Q' X) r return EFI_LOAD_ERROR;
, M; q7 {; V( H& A: p: B } + k+ g, }6 f6 A' j6 }$ y3 t' H
9 f# d H; J0 h Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);3 A1 y4 c% `6 }3 A/ g% L, p
if (EFI_ERROR(Status)) {
4 w+ m. [3 L, K! h4 `$ t- Y6 Y Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);( p- @$ h) b$ @
return EFI_LOAD_ERROR;
. Z: j5 X3 }7 T& B6 S" Q0 w }
: [" J, `7 X4 R- o2 E8 o# E
, Q. Q9 { `/ b+ c Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
* m& M5 a9 n0 J7 z if (EFI_ERROR(Status)) {7 V) ~) e0 B$ z6 D
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
2 C+ A- l# _% W/ Z return EFI_LOAD_ERROR;2 e; X9 A1 {$ B# E5 ^
} 0 f" d; c0 X( I2 ]* [/ u- [
/ I4 ]/ i- c# b5 q Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);5 w" @# ]- k, z. `
if (EFI_ERROR(Status)) {
7 M2 j7 {( Q4 U- I5 B% i Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
) c: V) d* Z, P3 a return EFI_LOAD_ERROR;
# n9 @! i, p1 B& g& M/ v }' t7 n" l5 f5 }1 J9 p- V) Y' C) t
3 L: j" B9 d2 M Status = FileHandle05->SetPosition(FileHandle05, 0); " l1 n! r: O( e& W% ?7 D4 k
if (EFI_ERROR(Status)) {
" U: J/ U0 u. x! K' @9 p; n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 C3 }8 r0 f: y: N. y, O, n
return EFI_SUCCESS;) h* y# _. Q4 u k9 u+ w4 h
}
0 ]3 e: R8 c- h9 T. P
$ J( T* \7 a H$ y2 v8 T& g" | Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
: g2 u+ ]- Z9 | Print (L"File Buffersize is %x\n\n", BufferSize);# A1 Z) _: X5 ?2 q4 }. K
if (EFI_ERROR(Status)) {
1 ~6 q3 ?: ~, n- } Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
+ N9 z# {& b4 d8 I+ q5 r, G: G2 S* @ return EFI_SUCCESS;' `; C, O( E# R, E
} ' b6 Y, @, f+ @- Q
" F+ W8 }: z: X8 {# i+ d
Status = FileHandle05->SetPosition(FileHandle05, 1);
% |( U2 D m; j if (EFI_ERROR(Status)) {" z/ Y! u& w6 }, N& {; q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: |7 s: q. a% q. o3 W return EFI_SUCCESS;
; ?) F: u) a. U% G) x }# X; G" r% v8 U/ H7 j
, `- ^# W; u9 T2 r! L Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);/ W/ G5 r4 ^3 r
Print (L"File Buffersize is %x\n\n", BufferSize);: ?0 j S* I8 X, ~, y
if (EFI_ERROR(Status)) {
: Y+ }5 p$ l. L. T2 E, s Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); @9 w5 M$ F, N A; {3 L
return EFI_SUCCESS;7 f/ e# g! a2 p' J
}
* x6 z4 l4 L" M1 _6 }! [; }
* K9 I9 Z* |( D v: ` Status = FileHandle05->SetPosition(FileHandle05, 2);
2 G0 d1 [; g, [7 f4 O4 _ if (EFI_ERROR(Status)) {5 i4 K9 m# }- {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( y0 s/ r8 B' ]9 S' J9 x return EFI_SUCCESS;
' h; M- l0 V: q b( v }1 _6 t6 h! U% _- f6 E0 d
* K- O5 k# j& ?* T
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);5 Z8 s* p$ Z3 A9 u7 p' I* Q
Print (L"File Buffersize is %x\n\n", BufferSize);
6 ]( o9 D M" s; w if (EFI_ERROR(Status)) {) ~/ d: o# Q0 ^" v& M7 G, b" r1 F
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 B0 u4 V! X% M return EFI_SUCCESS;6 j% v1 o) F: h, P4 m) B: C
} 5 Z4 Y7 w( m ], L1 D& _
4 E5 Y( N. U% {/ G//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>& }, Q [; W/ P3 b4 W" a$ Q6 p" K o0 c
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
! j$ P: C) V$ ^7 |2 Q6 s# Z Status = FileHandle05->SetPosition(FileHandle05, 2);
* b, s: b6 T) j' \( w if (EFI_ERROR(Status)) { g: r O% j3 ?
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 ~9 N0 Z+ |4 s6 j! A return EFI_SUCCESS;/ u" G& x6 o/ ~! l. O
}; J. Y7 f/ V# F) ?6 n* g
8 w# m' G% ~' [6 ]. ?5 w' ~$ P( G Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
$ m4 _$ n6 V* B8 _- N if (EFI_ERROR(Status)) {
) o% f% p; o9 {; ~ Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
9 E4 p6 p6 J# `' ]* k8 w5 m y return EFI_SUCCESS;# ?- ?: }/ `% e0 h; p4 q
} ' d6 L# E) x& G4 a4 U8 x
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB); z4 y- G, ~# ^$ Z
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<8 L4 j; [: p$ G5 _7 v+ y
, N7 I2 r, d2 r. x* L" T' m Status = FileHandle05->SetPosition(FileHandle05, 10);
! {7 G1 ]3 T3 [, X4 q0 G% v if (EFI_ERROR(Status)) {, u& X( F( @$ D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" V( p( t0 e' p* @* D& G4 C
return EFI_SUCCESS;( `9 `0 f# l( [( G1 W1 e$ X6 x5 n
}
2 s# l+ v2 F- ?( b. m* C
4 S4 k& c3 I" S' `, e# c2 U Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 N* {, h+ g- U) t" r8 g
if (EFI_ERROR(Status)) {
) e. [7 f4 }3 O9 d* v B' e5 C Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: o# `: ?7 h7 u3 C" A6 R3 Z9 M return EFI_SUCCESS;; k. G$ y0 q$ e% `/ I& V H
}
T7 E) d7 S0 B# _& I& f
2 l) k, t3 A; ~ Status = FileHandle05->SetPosition(FileHandle05, 12);
- l8 O% d/ g% A& h+ R if (EFI_ERROR(Status)) {
4 H: r( o8 f% v e4 ~ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ G5 D/ z2 e0 A& T return EFI_SUCCESS;
+ U) D* J; d4 f: y7 G/ v4 Z }
) ^- n" F& Z6 v; A$ A/ M) q, K9 g + D' j" n* h6 T" `
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);" h, ]5 I" q9 X1 d3 @# N
if (EFI_ERROR(Status)) {
1 v! u) w: T9 M; T) ?& k Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' Z; M, S% m% r7 k* _; Q1 c( S; Q# \
return EFI_SUCCESS;
0 S' f5 j( M$ `. {( W: Z4 ^$ |" n }
- @1 L- o& T, d# q3 n5 C& E) T$ ]8 ?( G. K, d0 d, R
Status = FileHandle05->SetPosition(FileHandle05, 14); ; a) Z& s& H* j
if (EFI_ERROR(Status)) {/ J. _! @7 R: j% t! G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, l" l+ q9 u& C) Z) ? return EFI_SUCCESS;1 v3 K1 z* E, ]
}. E' I$ C) A, j! r/ J/ s
3 R1 w1 s. ]$ \% Z/ y" ^9 E Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);8 |" F& I+ Z" B1 y
if (EFI_ERROR(Status)) {
9 S) ~. q' k. H, Q2 e Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 V. g! h( F1 K+ v9 { return EFI_SUCCESS;
% t: k, \& P! E8 | }
+ b9 O4 y. i5 C/ Y4 B
& o. T$ D8 E: C3 m$ S3 c! y Status = FileHandle05->SetPosition(FileHandle05, 16); 5 D% `9 A2 a+ F. Y% d1 S
if (EFI_ERROR(Status)) {
* Z+ |) v; H* N( @, O$ u$ \9 R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 I# k9 \$ Y2 o! g9 F+ [ return EFI_SUCCESS;$ H" O+ Y6 s9 A9 ?* `. W, d: Y& L
}
, C! L5 n4 p3 { v' c) e
/ D4 ^8 d( Q- }" z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
3 }2 C7 f6 a* D' { if (EFI_ERROR(Status)) {7 Q1 q/ `3 c/ f2 U; @$ E
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: S/ m$ E) r, V0 [/ e return EFI_SUCCESS;
, O8 c% x" ?4 B9 E8 h3 u } " S* w! ]- [' z- K0 W6 \' P
. p# h4 B9 ?) M. Y6 V0 F I8 P//---------------------------------------------------------------------------------------------------------------------------------------------------------------' n. W5 b. p- d# _4 u0 z$ y
O- E. V+ U: L* |. |/ y. h4 g Status = FileHandle05->SetPosition(FileHandle05, 18); 0 `: Z2 e0 F: S+ Z
if (EFI_ERROR(Status)) {
- ~9 Q6 m% q; ?( ^! O& i/ M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 }+ T9 w1 U b# o
return EFI_SUCCESS;
9 B5 K& t$ A4 T+ p$ b: m0 Q }
: X3 l: E, Q1 l! x
+ d. n/ a5 P, ~# W" Z; U# e Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);3 A) D8 q, ]" ^/ ^
if (EFI_ERROR(Status)) {$ ]+ ` A Y; w1 ]1 P( D
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: o% ^7 y$ \' E$ ?7 b+ s9 J. ^ return EFI_SUCCESS;
2 Q9 A- s- l# L" ^4 \0 G2 z }
. k* g/ P" @0 e( T% c0 Y- \4 v
+ g- q. A ~- V3 T6 K5 v7 f FreePool(DestAddr); 0 I" i; M7 n2 c- x3 p: ^' \" z
! b: c8 N( w9 G% F# U2 a
Status = FileHandle05->Close(FileHandle05);. I* e6 x/ ]+ y9 a
if (EFI_ERROR(Status)) {
% `8 z: J1 G# n Print (L"Could not close file, FileHandle05 err code is %x\n",Status);) s' x- U+ t1 {
return EFI_SUCCESS;
: ?, v. |: s- a7 P: x' v1 }1 h }' @- Z( F. P$ @: i& |
0 O* E: j3 G0 l2 f: P# T Print(L"File Name = %s has already been created.\n", FileName);
3 X* T' j( r2 s. ~8 k: K+ K+ w
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");4 G6 d/ u- n: S. P! p
WaitForKeyOrReset();
5 c9 g6 X I3 x, x" W7 J) Q7 f9 @. J7 V
2 i0 v, Y3 ]: D return Status;* ?2 u: n1 n) q" j
} |
|