|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*( w* b) M- G* r3 s, _" }
* myfileio.c1 h$ [0 G5 ?# h0 j7 ]# L
* Apps
- ]. c$ u/ `6 x4 r */9 V9 d! R% X0 K! _
# o# L9 `: r' e j1 q7 A8 g4 T+ {+ s
#include "efi.h"
6 L( i( Y7 |+ R+ _0 _8 o: y0 j#include "efilib.h"
/ [8 ] u/ H3 O, z* |/ O' ]/ M; ~3 y, p4 y! \
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
: X( y! @0 I5 u1 h9 f/ |9 F6 i! d# H- \
static EFI_STATUS WaitForKeyOrReset(VOID)
$ Q4 x4 t% Q& L# b1 y{
: D2 m f+ E8 e- A! [ EFI_STATUS Status;
- Z; l5 [) {" @ EFI_INPUT_KEY key;
0 Z& c: G; N# f! _' k4 ]* A# ^ UINTN index;5 R& O6 M# t, f$ r2 J
) [8 k" ~. J. P! z | for(;;) {4 N$ x$ J8 w) p4 p5 V- A( X' e
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);4 `/ q! `9 f& p' e) K; c- K
if (Status == EFI_NOT_READY)
0 o% N+ V1 H0 s; p8 \9 i, f BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
& T% b1 @: K+ W; D6 r9 I+ S, c else5 x$ V/ I, R$ S5 v0 [/ p
break;/ x& j4 m. |% }$ v- L' w t
}
2 M+ r# ?2 F, n; }7 W( C if (!EFI_ERROR(Status)) {
3 Q& @) o. }& V% w6 i1 ? if (key.ScanCode == SCAN_ESC)
- {. Y* O+ E' z RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);1 g; q- n) ^) I% N$ L
}
+ T6 l% I1 Q6 S0 }9 y
9 t# D, g7 S- B' W return Status;- }; _; I8 o& F0 a+ J6 @
}
; I4 f( V6 A6 R' c$ c: o& A$ i
EFI_STATUS7 V8 x1 k4 J0 m3 a& V
EFIAPI( i: u4 x P$ y4 ]% E* k6 S3 h5 z
MyfileioMain (IN EFI_HANDLE ImageHandle,; ^) z3 V x( s3 j( M: C0 p2 r
IN EFI_SYSTEM_TABLE *SystemTable)
% `9 K" F1 H0 a" l3 L& |: z{
" N) D1 K5 d. c& W! a EFI_STATUS Status;7 ?2 Y, G' e- `0 W1 K- n
EFI_HANDLE *DestAddr;
( l4 Q/ H+ B( a: I" @ EFI_LOADED_IMAGE *FileHandle01;/ H B$ o! s0 l- r: u
EFI_DEVICE_PATH *FileHandle02;
/ b8 ?. k* N' f* d8 A* X; m5 L EFI_FILE_IO_INTERFACE *FileHandle03;
|: Z5 r. @9 l. d6 X EFI_FILE *FileHandle04;5 R) I# t5 C1 { j% N) K
EFI_FILE *FileHandle05;7 O1 P' t8 Z- r5 I7 M u
CHAR16 *FileName;9 Q* Y$ Q, u- D
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
" V0 H' [' |) A. p3 t9 F# n- e8 J UINTN BufferSize = 8, BufferSizeKeySpace = 2;0 r# |& K/ H7 i O
int Bit0, Bit1, Key_Space;
4 E* U8 ?+ x9 Q1 C; O' \7 b: t. C; u- T. d/ o- W1 H' a7 z
FileName = L"NewFile.txt";+ r' n$ E- V. j+ v
Space_Key = L" ";! V) W/ q2 q4 N4 K. g- B
) R( J3 L2 ]! f* Z7 h. U4 Y" C) u9 }5 g
BufferA = L"ABCD";7 w- \) X6 \6 Y
BufferB = L"EFGH";: D5 }/ X+ U. n; i7 R$ P! B3 B7 w! c
BufferC = L"IJKL";
+ u" `; C" V" N0 N, ^# d8 T, H% _
2 r0 o, c+ S* E8 j; w Bit0 = 0xff;1 v& D* x3 A4 ^
Bit1 = 0xfe;( J* e. r/ F* n! u8 `9 m$ J% U
% d# j! `6 P) u6 l4 Z7 d; g/ y Key_Space = 0x0020;
$ U) l/ j$ a6 h$ S- K# r# `' }
" Z* U9 }" w" ?! k U8 ^6 \ InitializeLib (ImageHandle, SystemTable); 8 _/ \+ d) I7 [9 j) x, k
+ l: e4 }2 f, _! t9 @
DestAddr = AllocatePool (BufferSize);
: v. J$ d" D$ ]5 R+ g" e+ a. q6 \/ G: _- f; D4 ]$ W9 P
Print (L"Value of Bit0 is %x\n", Bit0);
5 K" ]4 N1 A& \) `, M6 n Print (L"Value of Bit1 is %x\n", Bit1);
/ v3 `- n/ X7 p1 X* G! U$ y
2 z& ], J8 x: k- r ?+ C/ A
# `% J7 v1 U/ ^. ` Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
3 i! [1 I& Z3 a; Y/ [ if (EFI_ERROR(Status)) {
4 p1 W8 I: ^+ P s1 \ Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
1 |9 J6 F- H% C8 V; G# x6 P! y return EFI_LOAD_ERROR;, b/ h0 Q5 @ g. M. v3 ~4 X! V
}
- l' d* k k6 B* B1 p, e# k/ K# G6 J- B2 \* A: [4 p
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
; y: Z9 x& a$ e6 O$ @. V if (EFI_ERROR(Status)) {/ U7 V( E) p; U5 r
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);, o/ Z$ z% Q, ^) k
return EFI_LOAD_ERROR;# P" P# k6 ]; h4 r+ _, T
}
1 Y& D9 n5 i+ H6 t8 d2 w& J* Z 6 |1 q) b" M! w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);0 r0 M4 O% `7 i, q) V9 {
if (EFI_ERROR(Status)) {
5 T: I! z$ x6 E% e% v' Z5 S Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
' h5 D9 |8 R- W return EFI_LOAD_ERROR;; x8 [$ S3 x P0 e# f
}
W: Y4 V- ^. k) W' X
9 r$ J6 @) H5 P3 a6 a7 H j Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
2 B) F; z3 J# @! o( Z if (EFI_ERROR(Status)) {" E/ ~* ^7 y6 k- z$ N
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
; O- D& s0 V! B+ J- t1 y0 [9 ]8 m return EFI_LOAD_ERROR;
8 b, p, e0 R: P( B; ` } ! I1 V1 d4 e0 V. }* o8 ~1 v
4 s; R9 k; b4 i- |/ w8 Q! d Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);, t7 c4 u: b! Z& x1 l
if (EFI_ERROR(Status)) {
: _, [+ z7 j# e; d8 O, d Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
& B6 V! u' u" r7 ~ return EFI_LOAD_ERROR;
" ?' F3 L& ?; w# |- y3 X1 M; w# c }
' _7 c1 [; v) M! U( q $ s; B3 u- [1 v, B
Status = FileHandle05->SetPosition(FileHandle05, 0); ! ?0 n9 G9 D6 a, w2 Q* R
if (EFI_ERROR(Status)) {8 {4 n' M3 f5 `* X
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& E+ t3 E( u) g- j: j- ~; } return EFI_SUCCESS;
+ ~/ j5 F- g) } }
8 [. I( n$ q* U9 w' M! r8 u / j+ Y' r0 d2 [- ~2 o- ^
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);8 ?, n5 e8 | f: Q7 B! y
Print (L"File Buffersize is %x\n\n", BufferSize);
. J% c! e0 V* K$ n' _9 g if (EFI_ERROR(Status)) {
1 _+ h' D$ H2 f5 m2 w3 [7 Z Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
" \- t7 }) P" r/ I/ b$ C return EFI_SUCCESS;4 {- \( b ]8 Z1 Z) R8 [
} - C D! E* d# M
; H& z" U* w) A5 g
Status = FileHandle05->SetPosition(FileHandle05, 1);
* c ~ ?+ @$ J6 p# S4 R& n if (EFI_ERROR(Status)) {
5 i3 L4 ?1 z4 M: H Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 p1 j2 P7 E5 R/ g8 g3 P2 q. B
return EFI_SUCCESS;' p% T+ a7 q7 D/ s( d+ n
}: \" |! v7 u6 Q
d. z* f/ _$ j' Z5 s3 @* R7 ~8 {
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);- X. x- X3 B# j% Y I
Print (L"File Buffersize is %x\n\n", BufferSize);+ Q+ C1 u0 c$ r( X+ e" R$ J
if (EFI_ERROR(Status)) {) |* I8 I5 S! |$ w, G
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
) U& M( i* X# b# M return EFI_SUCCESS;
$ C) Q0 u3 a5 C: P2 k } 1 D1 d+ {" g) g; W7 ]9 W
* ?7 J8 R% ^: j. I) l$ B2 s: ]
Status = FileHandle05->SetPosition(FileHandle05, 2);
$ |2 ^! P/ r, }/ S if (EFI_ERROR(Status)) {
1 K9 n* O) [; e# ?7 T& X- S/ [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 [/ \# q+ b# V return EFI_SUCCESS;
; h; `; F: Y8 x4 n }' v; j) C# D% f: ~6 X! l
4 W1 _0 s3 T5 A" i% y Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
5 J$ ]% E U9 @: e Print (L"File Buffersize is %x\n\n", BufferSize);/ E: n1 T" s3 n) B* s( g* U8 X
if (EFI_ERROR(Status)) {
8 a& n: e# c a) k; W( k& J- f Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);4 x( x* V5 r% ?
return EFI_SUCCESS;
' B% g+ h& B6 Z/ c# Q+ d } ( j: D. K! E w
. O& E; n: t j$ e# y, [
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>' t, X6 Q4 X' r3 I5 g# K1 Y
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);1 ?5 H$ S- {: [! k; g, l! C: ]
Status = FileHandle05->SetPosition(FileHandle05, 2); ; {; m V$ m' m9 N
if (EFI_ERROR(Status)) {
4 q+ _% w6 R, s0 G% t- u0 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- z& ?6 R$ l6 ^/ i8 I4 |
return EFI_SUCCESS;/ ^2 L1 X3 @* U! O1 e8 p
}
! P) E# s9 \9 C) X
: J5 R+ R+ Z* w Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
Z4 S! q, K: X if (EFI_ERROR(Status)) {5 W; n6 i- \: `: r5 k" y( _
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);! K: p7 o3 w9 @* B+ O0 k
return EFI_SUCCESS;
' g' _( z6 X1 [( _7 p, b9 V } & L |& k1 Q+ y3 z; A/ ~" ~
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);- m( z2 Z& X# s$ E+ a
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
6 P7 E: ?$ M! L. F, _
7 }8 W* q& h; P+ ~6 c, j Status = FileHandle05->SetPosition(FileHandle05, 10); 0 H& r) D& ~9 V3 x6 T
if (EFI_ERROR(Status)) {. K* X7 e# i5 P' C2 j% E9 _5 e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 q1 K, U' T" c3 E5 J. X
return EFI_SUCCESS;% W6 {# b7 {. u5 n
}/ n' R+ J1 Q9 y3 a
# Z' g% `2 e. D# Y8 ?
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, s3 B1 }$ Y$ ?' I if (EFI_ERROR(Status)) {
4 ~, q% A/ s E& [9 J9 f5 g Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 z S; {! {6 O) g+ Q M
return EFI_SUCCESS;4 m: S' x( L7 t- ~5 F
}* S; q! a" k6 ~8 |2 ~, O9 b
3 U i) V) z# Q6 O
Status = FileHandle05->SetPosition(FileHandle05, 12); * A. n0 c* F0 I2 e4 m$ `+ s. `6 f4 e
if (EFI_ERROR(Status)) {
* s' | B5 C4 e+ V, x3 E1 g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 s9 R& ^2 }, [4 e% f return EFI_SUCCESS;
# Y$ ~$ D6 @3 O x }
0 q3 i( Y6 o) m6 p
5 S) g) J6 }3 q. K4 p0 m Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
\* B0 |6 p8 U$ }! P- I+ k if (EFI_ERROR(Status)) {4 A X4 Q3 d/ w; ~& D* a+ c! T$ @$ _
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: u; w8 |# c! ^+ r/ K return EFI_SUCCESS;9 }' t% N' y# F5 k! ~
}2 `' N1 _1 {5 c7 A- W
8 |3 n9 V4 B6 h6 [& f Status = FileHandle05->SetPosition(FileHandle05, 14); q, T# o* f B8 H, V
if (EFI_ERROR(Status)) {( r9 h/ I. y2 R% B' r. U6 Z. ^6 c$ ^
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 O- @. q! P7 N4 z$ E( z) A& Y; `7 F P
return EFI_SUCCESS;
7 G6 Z0 S" o! y" `6 }* i( r }) i9 ?) k! f D" y- E. j
/ m2 n" C" `6 i1 |
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ [9 U0 g: P3 F2 y# L6 C7 V4 U if (EFI_ERROR(Status)) {, m1 p# u2 q7 v7 g: s
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* z4 ], o8 Q8 }6 g2 v& N
return EFI_SUCCESS;
) i2 {" r/ z F- x9 u5 B1 A }9 ~, B$ D6 k' u3 j
6 c" M" I/ ]: n, y6 ~, V/ n5 _
Status = FileHandle05->SetPosition(FileHandle05, 16); ; u0 ~5 T3 H3 I* d
if (EFI_ERROR(Status)) {
- F8 s8 Z/ l o/ P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, _$ E2 A9 v h return EFI_SUCCESS;' {- _ d' J8 b& g
}
) \, W: \0 J9 [- M4 e0 J 6 l) }- c2 S F+ J$ ~* R3 Q
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' e4 L- L2 g6 g2 T
if (EFI_ERROR(Status)) {
2 d+ P% j4 M) r+ A# s2 X5 g Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) x! S/ e7 b, M- ~. P! W# |
return EFI_SUCCESS;, |7 J$ r) j6 n2 a' _+ Y
} ' @6 A1 Z O& `2 q2 R$ E
+ p0 d4 ?; Z' y! U' Z//---------------------------------------------------------------------------------------------------------------------------------------------------------------$ |. v1 }+ Y7 O/ z
- ^. g" j0 P" n5 T/ y: _ Status = FileHandle05->SetPosition(FileHandle05, 18);
4 V5 A2 K5 `9 l9 A if (EFI_ERROR(Status)) {8 }6 z4 i( t7 `! L% W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 V. F& k5 M5 m; @7 Z. t return EFI_SUCCESS;
1 L: F$ j1 \7 P9 W9 n6 b* n }5 q6 s( g- O- a: K: ~ W
( u/ b" Y: Q$ K5 j Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC); ~& z4 x* S: ]7 p Q. i' u
if (EFI_ERROR(Status)) {
7 {6 B( Z. j2 n9 a Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
3 Y J& Z! Y6 J4 j$ ~/ i return EFI_SUCCESS;
0 O: G/ ^5 f9 I. [' x } 0 \' Y6 X5 x8 C+ G4 ]( `
" y% r/ Z( S5 [ FreePool(DestAddr); 6 Q4 f" ^! R. P! V, l
; R$ n. Z* m! {9 l A
Status = FileHandle05->Close(FileHandle05);
/ w5 {/ X7 p4 F4 u: ?5 ?3 `+ J if (EFI_ERROR(Status)) {5 S A3 N9 C1 O8 o9 c n6 E: f3 Y. T
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
/ z {. ?- @; C( w" N. P return EFI_SUCCESS;5 p! j" u b( @( a
}+ t8 j+ w7 L5 v; Y1 o
" `: E0 b6 d9 V" M, R& h Print(L"File Name = %s has already been created.\n", FileName);; }4 Y8 ^/ Z4 s1 a
9 G% r, }* u/ h4 O( b! U# A) I. E. @
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
& ^4 G. k, |9 O WaitForKeyOrReset();5 l3 f3 \/ P+ B. k$ F) Q
7 R3 L$ H+ ?( i1 x- a( S
return Status;. M: u# S0 Y7 k
} |
|