|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*& G0 b( e0 y" y
* myfileio.c
* H! | R4 B- ?: F * Apps
: G8 V0 u1 }6 n */
! |' }, }0 _& [/ v+ V. _: u9 ] e0 r# w" f( o! V
#include "efi.h"
2 I% q: L; D+ C# i, B5 b9 O- H6 X#include "efilib.h"
" [6 I* C5 ^: Y: d, ~( I
+ M6 W, A$ @' K1 V3 A#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
& L/ [9 w7 W6 i7 C/ H
) D G0 r$ l* x* |8 ^* c0 L* Hstatic EFI_STATUS WaitForKeyOrReset(VOID)$ G! g- S/ m. O* m* s
{
7 x) Y$ ~7 S6 e EFI_STATUS Status;
7 S% X# O; @4 w& @1 g# _5 B6 t% {# F EFI_INPUT_KEY key;
' t4 H, M Y" o7 D2 d/ X UINTN index;, l; N t. m* |- A6 U2 D
: Y3 X; D& }" B; w+ Q m for(;;) {
2 {( D, z! P. j, I" n; M Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
' C- T2 }! t; l1 X0 v6 Y8 v if (Status == EFI_NOT_READY)
" p7 p9 T9 ]; G, _) b BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);) S @; H; T7 Q; p! i: {6 {
else! V' d) E' ~( o% z
break;, C8 w Y/ _7 p. ~% i0 C3 W, H6 A
}
$ f' J# J1 I& U( U5 p$ u5 S if (!EFI_ERROR(Status)) {
$ u9 z: C4 Z# }# U if (key.ScanCode == SCAN_ESC)
# M x* L& ]! J RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);! P7 m _ w3 w2 K" y) u
}2 `4 @ _2 P, e5 s6 P
+ d2 _# w; A" o# W. m# Y return Status;
7 j d+ f& i' ~1 s}6 n0 R2 m1 @, c/ e
5 B6 Y, ~: L6 g; q# SEFI_STATUS
8 n9 R- a/ q, j$ JEFIAPI6 a/ x2 `9 z' s6 s# a3 b
MyfileioMain (IN EFI_HANDLE ImageHandle,
: W( T0 ` Q* l2 V IN EFI_SYSTEM_TABLE *SystemTable)( N1 u! [) U" y e+ C% a- J# D
{
9 f. x' ~; N1 R1 x* O! j+ d7 C' f EFI_STATUS Status;9 h' C# U- J* a Q# J7 O
EFI_HANDLE *DestAddr; 5 ~8 C7 V5 u5 g* E! {
EFI_LOADED_IMAGE *FileHandle01;
2 j5 s. D! v- z" [) V- }& L EFI_DEVICE_PATH *FileHandle02; ' T! Y2 }) r' H' f, D+ W/ j% O
EFI_FILE_IO_INTERFACE *FileHandle03;; C1 C, N5 j+ o; n* s Y
EFI_FILE *FileHandle04;
, \/ H: o% z" j) T4 E5 \ EFI_FILE *FileHandle05;
+ B3 Z% i- ^9 y5 B \( v4 v6 c% J CHAR16 *FileName;
' W+ ~8 {" o! U4 b CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;- p4 m' N; U7 D' }2 g8 |
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
( T f( A2 n( D: y int Bit0, Bit1, Key_Space;
& C- N! s! E! ~/ Q* X3 x
" K; g, _, x' [6 ^ FileName = L"NewFile.txt";% f0 x' z1 T D6 Z. w. U
Space_Key = L" ";& z. p! ?: W3 q/ C8 ^
* K. ]& b" {4 S
BufferA = L"ABCD";
5 j# I8 M% U9 A5 f! r BufferB = L"EFGH";8 ?9 B2 a/ Q" w0 ^
BufferC = L"IJKL";
$ s6 f/ N# |! y# d8 \/ v5 N( `* O; i0 f( k8 f; r9 k$ ]1 a
Bit0 = 0xff;% i; D8 H" V3 u2 o# a J( b! ]
Bit1 = 0xfe;
. M, Q" n/ J& h t4 j) Y0 h0 H" a
& H& W: d, C& w/ ^ Key_Space = 0x0020;; y2 Z G$ V" y2 b; x2 X; B4 u, L
/ q* M4 t( Q# U! L) y) Y6 [$ ? InitializeLib (ImageHandle, SystemTable);
) V6 J) z# Q( N5 k4 X
: [5 W2 x3 h7 U DestAddr = AllocatePool (BufferSize); . ?6 {% ^" g' S9 a& o
" i" J/ y# r. z Print (L"Value of Bit0 is %x\n", Bit0);8 Z9 r4 _# X" z8 m) r4 c$ H
Print (L"Value of Bit1 is %x\n", Bit1); ! r. I( G0 } I
* H* [ C. A( h: j y. w+ @( f * Q$ n$ G- Q+ P
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
/ y- I0 D( A4 m. p if (EFI_ERROR(Status)) {
! L; n$ C6 Z8 {* U& p Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);8 a% _5 ~" Q6 O$ g5 ]
return EFI_LOAD_ERROR;& ~% L3 I' f: Y8 o( X" Z
}$ m6 W( q3 o. e9 \) F
2 j. R( s$ q, e3 H9 u' c( m. j8 l
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);/ y( ~# v) _4 S B
if (EFI_ERROR(Status)) {2 I5 L$ a( _" R; c$ m- r$ \
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);! X. {# ~4 p/ x! \4 a
return EFI_LOAD_ERROR;" O4 e. W* G+ a- W1 Q
}
, R* [! P7 i! Z+ z
' `5 ?' S1 n( S. B/ v0 g Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);( C( k+ B% K6 m z: X6 N0 z
if (EFI_ERROR(Status)) {6 P8 n" `+ H5 G
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
. R- a) h" p" [+ q4 l3 e7 c return EFI_LOAD_ERROR;
7 n6 p' ?: Z4 r$ @+ M" r } D8 ~+ v& @; C: K/ R1 ^; R9 \
- Y" L* x s' d. x Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);- m( p1 V- e% b& V. H* _
if (EFI_ERROR(Status)) {% o3 [: G7 L% o
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
1 d, |" g" S Q return EFI_LOAD_ERROR;9 }! f7 l; C. A5 m5 T/ {
} + N+ V! c6 [5 R) V' w
1 E8 i" H0 e9 a7 N! c Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);& g T$ G% ]; I, H
if (EFI_ERROR(Status)) {
9 f }1 U- P" h- U2 V+ ^2 R Print (L"Could not open file, FileHandle05 err code is %x\n",Status);+ m+ i( h) t, \+ D a3 M2 j
return EFI_LOAD_ERROR;
$ K& }( ?- d9 ^+ _0 R; f3 i }8 e0 i1 }) D. R0 H# ?% `6 A* I5 h
D3 Z+ s4 F1 B) Q8 x R; K( ^
Status = FileHandle05->SetPosition(FileHandle05, 0);
7 W. x8 M4 M7 J: A- g8 R' o& N if (EFI_ERROR(Status)) {0 m8 l2 D" B- ~
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 {- L9 M, T7 r+ c8 h
return EFI_SUCCESS;
. u/ a$ i. [ p# w }! I% B5 s# _- P! j5 M# d
9 ^, _$ R6 \1 s( W Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
% X' R1 z# e; ~ Print (L"File Buffersize is %x\n\n", BufferSize);
: ~. N2 d' X3 ~( w! y1 m if (EFI_ERROR(Status)) {
2 y, s) a( d& t& B; z. k Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ F; }/ P7 K# ~% d# X$ J3 \2 g" T7 [
return EFI_SUCCESS;
4 w% ]/ _% J& V! A, [1 I* g } 6 N; L: Q1 L0 j( s
* B5 G" Y) V( t% A& J& R6 f Status = FileHandle05->SetPosition(FileHandle05, 1);
& I3 a; ^: B0 l if (EFI_ERROR(Status)) {
! L2 N0 ]' q: o9 c; S6 M% c" [% Y( y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 Z+ J- E7 X | {! `# d
return EFI_SUCCESS;8 ^2 b0 i4 \' l3 s& }
}
( K7 [- i- }( o1 l. ?* Q " H& a5 X: R9 e+ V0 g! [% D; o1 Q
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
0 z. [* q5 N) e2 h Print (L"File Buffersize is %x\n\n", BufferSize);
0 o, j8 `% M6 U7 i, Q8 F6 N4 a if (EFI_ERROR(Status)) {
3 Q; h. a' U9 f0 Y% _ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
3 y4 p8 G$ W) y6 d$ H5 q6 J' Q6 ~ return EFI_SUCCESS;
" j1 P i( o0 @' V) I } / ?( T5 ?9 M, x; U* p2 i; R
: y. X8 ~- j: R4 w' q! c$ {9 W0 A Status = FileHandle05->SetPosition(FileHandle05, 2); & c$ x! N: U! h; U! G) D
if (EFI_ERROR(Status)) {
# T( z% K+ T! y# v& H" ?+ ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) M! ?' v; d( a6 `( {! d2 q" | return EFI_SUCCESS;6 ~+ s6 i+ S/ J4 u% M
}" V' L+ v) c6 s6 v: @2 W
+ N" L- R) Z h
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
; n( m% W! G+ |3 ?1 V Print (L"File Buffersize is %x\n\n", BufferSize);2 J" ?$ t& s8 r2 s
if (EFI_ERROR(Status)) {$ C4 s @. G. |0 L
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
9 r+ O5 Y9 D4 Y) n) n# d( z; ? return EFI_SUCCESS;; ] V( P! O9 ^# u+ B; F
} 7 Z7 d! v: V! O+ Y4 s
9 e$ K( b$ E3 D! W/ ? Z7 f. J//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>: G3 M( y$ P" T$ ? _! J1 s _4 A
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);1 a# F7 Y+ w/ I+ A( W% ?$ e+ T& P
Status = FileHandle05->SetPosition(FileHandle05, 2); 5 p' d8 Y/ [& b: E# j
if (EFI_ERROR(Status)) {! K V7 Z/ R- I# t! b% u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# [ N0 n7 Q! J# L( {9 e7 R' M
return EFI_SUCCESS;% I7 C. m9 t, S: x5 Y
}" ^$ j8 T! m. T
+ ~6 j1 q1 i5 _4 v, |8 X$ e Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
- l& T/ ^; D2 d# P8 X9 W8 n if (EFI_ERROR(Status)) {$ b0 C# c% j a5 o( p0 W' ]
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);% I. g$ j' J0 [2 U: G9 Z% O. b6 T& D0 [
return EFI_SUCCESS;7 y. l& I3 `& H; M% w
} 2 r& L0 Z6 f; a% c
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
9 I4 p8 g1 ]& {//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<9 o9 r9 F" R/ [
/ |0 R9 M. J3 |, G3 P' W+ s
Status = FileHandle05->SetPosition(FileHandle05, 10); 4 W4 x; P3 m6 @$ R
if (EFI_ERROR(Status)) {
, k+ [" J! D! Q& Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ s! P6 L) V9 F/ C3 Y2 A4 x
return EFI_SUCCESS;$ E8 k6 Q9 ^8 W- x1 P1 |& E
}; @/ b2 X1 s; F% V1 U" `
. Z0 m3 } x/ v8 h) [ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);/ G# k- U. r. ~2 i- p2 s
if (EFI_ERROR(Status)) {; ?9 R% H* a7 y5 r0 q
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 g$ E! ^, m( A! D0 A, {: N! { return EFI_SUCCESS;, H1 [, Y9 z- ~% N* P
}4 z9 [ K. \0 b( A4 l; @: r
, ` i5 k1 j X) l/ s' [; | Status = FileHandle05->SetPosition(FileHandle05, 12);
2 q6 \, ^! t; w; }% K if (EFI_ERROR(Status)) {
' \# W& P& i6 U: @- c+ Y* ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 G; b+ _2 U8 B; H$ j
return EFI_SUCCESS;
; c* b8 ?$ U2 u2 @( X* B/ h }
7 R1 y+ b- Z6 J4 W 0 T+ U3 r- ^, p. c/ s* d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);4 [* A9 m: Y3 I
if (EFI_ERROR(Status)) {# A8 D. Z# ^$ A" i x4 |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 W2 i) `6 F% h6 G7 a) n return EFI_SUCCESS;' Q! y8 g8 C1 j* B8 c0 g+ L1 s7 q
}+ i0 F( w( R1 m" Z y, H
/ ?4 f$ i2 ~$ J/ I
Status = FileHandle05->SetPosition(FileHandle05, 14);
! v3 Z a; L# a$ \6 |+ L" [ L if (EFI_ERROR(Status)) {
% ], V4 D- v8 ` {1 S' ~8 V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! W# \6 t5 a) x- M* V: _8 p return EFI_SUCCESS;
/ O& S1 b0 i1 e6 `+ Z& W }; t) i+ o1 z/ T* G" |
& |1 U: t! ]/ i& k/ [
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);2 Z0 V* @! T H1 Z: T- o2 _6 G% _% z- [
if (EFI_ERROR(Status)) {
* t M* t6 b" m6 {) t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; t+ y _' q# | u8 u* F( D2 F) Y
return EFI_SUCCESS;/ g9 b( @* |' t5 r$ ~# g5 m. z4 y
}
9 J( z. F% P2 M \% M! n: a5 Z! T! s/ O$ W7 V2 V
Status = FileHandle05->SetPosition(FileHandle05, 16);
% ~3 ~# p6 F5 } if (EFI_ERROR(Status)) {
- D" t+ {# n. |/ i5 X [1 } Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 }9 }7 ]: Q# s8 o return EFI_SUCCESS;
' |8 ` v6 w9 s3 o$ k }
% R, y3 b# L# f, ^. G * |. V g, F3 n0 Y4 G2 W
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 F( ~6 Y5 \1 G# E if (EFI_ERROR(Status)) {+ i* e9 p5 E& d3 N: p
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% l. e& G& Q& Q# T return EFI_SUCCESS;4 C$ D9 K y- v9 w9 {8 l, l/ Z
} , S5 z, a% f- L/ Z5 f, W
+ o" J; K0 E3 S) Q) p1 [) h//---------------------------------------------------------------------------------------------------------------------------------------------------------------
$ Y4 z; E) p; x! j7 R8 D3 k5 y
" a$ q0 a5 F! f1 f8 q% n/ v Status = FileHandle05->SetPosition(FileHandle05, 18);
# [! N# Y# M. {2 @2 @ if (EFI_ERROR(Status)) {* `' J+ Y5 h6 C5 d n# {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 C6 t T$ G) w2 Q5 n6 R0 c
return EFI_SUCCESS;0 k( \3 r; F6 @! {0 s* |
}* w) ]( t0 o5 Q2 m9 b6 ?: i
( k' K! w0 k3 ^) c Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
' W; j/ i) h' y( K5 m' \ if (EFI_ERROR(Status)) {
4 V \# J! x. {/ c1 r' {3 D \; V- i" f Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: {8 j- w) X4 n+ C/ r R. l* ^, ^: x0 l
return EFI_SUCCESS;
& C, i; Q: i( X2 N4 R }
0 d! A: Y& K' @: M% w 4 h; s* W0 \) a5 e- M
FreePool(DestAddr);
0 K$ P7 I) ?7 w; t$ X+ `: `/ @ Z7 v8 c' q+ j! S+ W
Status = FileHandle05->Close(FileHandle05);6 E( z3 b0 W9 ~, V R7 D. M
if (EFI_ERROR(Status)) {
2 C6 @$ E8 I q; F Print (L"Could not close file, FileHandle05 err code is %x\n",Status);6 h6 m( D8 O9 i \- D; v
return EFI_SUCCESS;
$ @ G7 _; F v' @ }6 o, h9 Y( j: R) P$ F9 @! k/ q
5 e1 d ~; b" \& N Print(L"File Name = %s has already been created.\n", FileName);
3 C+ ?* ~. Y" {5 I) v5 C
& C; E: d+ [. R5 z. y. B Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");5 Y0 Q8 ?8 k# O1 T0 @4 V
WaitForKeyOrReset();" |7 }& c& H0 T2 P5 R
2 ~" Y) u( S5 g return Status;3 g @3 W/ C, t1 r: U; k
} |
|