|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
1 E8 P' Y0 u6 o, W2 E * myfileio.c3 |$ }2 ]/ S! s l' S; }
* Apps+ R% g! C0 \1 x! d3 e9 X
*/3 _7 y9 E) O2 T3 [0 x/ l f# Y
8 x. L; R5 d2 G0 S1 n#include "efi.h"
, V- s G3 l g# J, Y3 h#include "efilib.h"% z6 l7 z/ | G3 M
' o2 }2 @" d: ]! O
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE. y: \, k+ G) [+ ^) R/ u
/ I' B6 c' F# `! @$ R( G6 n
static EFI_STATUS WaitForKeyOrReset(VOID)7 W! u) N/ J9 @6 Z1 E1 z( W( p
{
& C* E# F- r7 E: d* x; a3 p1 N EFI_STATUS Status;2 Q" o* }8 F4 W! J- r
EFI_INPUT_KEY key;& n" F- g S& _. n* C. U. Z8 W
UINTN index;# \) Q- k& X# R
6 d. E0 V- {5 T$ q5 H$ \/ r7 U0 r* ] for(;;) {
1 n. [3 U; y7 C& v. n) v Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);5 @" T1 N' ]7 v
if (Status == EFI_NOT_READY)* D) o) ]; k! f5 p/ T% p2 B
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
# v+ |2 ^! Y r4 w1 d else
+ @& M; q% K1 v break;
+ R5 U& O$ Z* Z, L( d' C6 r }
! h* V, P G' k4 h if (!EFI_ERROR(Status)) {
# S) C+ X+ C" b! z9 Z4 n( l { if (key.ScanCode == SCAN_ESC)
7 r9 ~6 a1 ^2 U% V/ {" X' `! L0 I/ A RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);! F( S4 M# _7 K6 y- i1 Z" {1 X
}- R- H9 h& w5 S3 p p
! e) F, S+ n* z6 O6 F/ `' n X& S& s return Status;" x6 d" N9 _+ G7 h( x% o2 r% G
}: `1 W. h) Q( H, o
7 |+ ^/ g0 t; `EFI_STATUS
, v! c* ]8 V1 `3 c" s( p4 nEFIAPI0 j9 r* O$ b: A/ @7 T) U/ x
MyfileioMain (IN EFI_HANDLE ImageHandle,
+ X9 ^7 C h" ]5 m3 M IN EFI_SYSTEM_TABLE *SystemTable)
- s2 X9 ?3 E' `: z. {7 ]2 I$ r6 v6 Z% s& j{! {6 E5 ]* z7 v6 q: a6 C
EFI_STATUS Status;
' _( } z: C$ ~& Y d EFI_HANDLE *DestAddr; # [ O3 P1 j$ @$ f9 K
EFI_LOADED_IMAGE *FileHandle01;
& z$ C4 e+ F% F EFI_DEVICE_PATH *FileHandle02; * `7 X b$ S! a7 w& [
EFI_FILE_IO_INTERFACE *FileHandle03;
- E7 {) V+ D g3 N; O" J4 y. I EFI_FILE *FileHandle04;3 b3 i$ B: n+ n% Q9 ^
EFI_FILE *FileHandle05;
; n" X% _# E1 \& z0 F3 W, H5 } CHAR16 *FileName;
/ T$ r! K( L1 `* |( k" C5 ?# M$ r CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
c) N! W( p: b UINTN BufferSize = 8, BufferSizeKeySpace = 2;3 l, o6 k; ~0 {- j
int Bit0, Bit1, Key_Space;
6 j; c5 b: z7 s; Y w- M* [# E) d% G c. @ V
FileName = L"NewFile.txt";
7 V. ]; h5 \9 W4 q Space_Key = L" ";; B! @( q, y6 J$ B( j$ y
4 H( J" o3 P& P j$ e2 d
BufferA = L"ABCD";
/ |+ t* ]5 @9 ` BufferB = L"EFGH";
. R# N; a- Q+ N$ i6 t1 P" C3 W BufferC = L"IJKL";
( p+ Q8 v+ B0 V7 O" m- U+ ^! T! S6 w
Bit0 = 0xff;) j- @+ l j# m2 S& `
Bit1 = 0xfe;# U- n, N3 l4 c( F+ Q H6 U
m: `: S. k* b# B7 P3 _
Key_Space = 0x0020;( K3 C( B4 F) C' j7 b1 p
y1 x) ~: Q; R! Z8 @ InitializeLib (ImageHandle, SystemTable);
, V5 Y6 N/ Q1 C" `( n& `- q; {! M3 S9 G/ |; U2 s3 D/ v/ |
DestAddr = AllocatePool (BufferSize); 8 j H8 N4 b0 q: Q3 b
! ^* \6 Z2 L7 ~+ z8 ~
Print (L"Value of Bit0 is %x\n", Bit0);
" {9 R( g ], ^, Q4 h8 c" z4 J Print (L"Value of Bit1 is %x\n", Bit1);
3 ]1 D, \3 m4 E$ H5 }, f0 ~7 g* T+ J; Q
% M6 @* L! F; s j
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
! C/ @1 `2 L- G1 d, u0 a- C& h/ \2 J if (EFI_ERROR(Status)) { p: [' U/ \# y1 ~$ G& B. G/ k. m
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
$ ` r' [ d" h! Z) S return EFI_LOAD_ERROR;/ o0 V: b: k, B# n
}% W. g) d! E+ o7 R+ e
: |0 {4 o/ d, x/ H* M Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);$ C! ]$ a3 Z2 p6 h
if (EFI_ERROR(Status)) {% z: y" m5 d( G; p
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);. ?4 _9 k' }* P+ v/ E
return EFI_LOAD_ERROR;
# s/ I% R" g" P7 ?. p }
( F$ M3 b. L3 e ^. `2 P8 `* c; M6 h* W. ]
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);( U9 q4 v: c; j/ r1 j9 K2 L+ d1 k* V
if (EFI_ERROR(Status)) {! O. H" [% Z& _, s! Z
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
% e2 Q( {3 W2 ]* R9 e7 {& F1 ^, c return EFI_LOAD_ERROR;
( M! S: P: V0 g7 U; R }5 f O* c, w) G& I& w5 J! X
/ o+ b$ u4 Q6 d; [
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);0 `! n2 R# ?9 V' j5 @, l* }
if (EFI_ERROR(Status)) {: u! k7 t2 k% c' V4 L6 H
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);1 @. g1 N1 n9 m, ~
return EFI_LOAD_ERROR;/ v/ q& b* E! A" ?. s
} ) H3 A; o% E3 A$ A, ~0 [
. G% S9 {! T) n: d. u. g$ e
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);) [& O7 n- _' F! f6 Q
if (EFI_ERROR(Status)) {
8 J9 P4 d8 W' I' a/ \7 h1 F7 `/ c Print (L"Could not open file, FileHandle05 err code is %x\n",Status);( E6 a; Z$ ]+ }1 r. k
return EFI_LOAD_ERROR;& a; f3 o8 |. G! i- W5 @1 j1 p" ^' X
}; I+ b- R, p9 ~! a+ V. c
; y0 @' w; G+ e. i1 [; y( a, C Status = FileHandle05->SetPosition(FileHandle05, 0); * p% p+ U. }0 \
if (EFI_ERROR(Status)) {) @( S8 u( R3 T' X I
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! `* x9 O+ G, }- L2 n; ^4 d return EFI_SUCCESS;
/ Q7 j: I) U3 k5 n5 X: F }) t- _* i+ E/ I
0 R- M2 P: V. `5 u0 P7 G: C
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);8 N" p# p- Y4 k& z' @ i
Print (L"File Buffersize is %x\n\n", BufferSize);' {1 L4 u2 `% ?2 K- E
if (EFI_ERROR(Status)) {5 j% m- a* o2 ]+ V# g' k' Q
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);& x3 v/ S/ @6 ]" `( {: F
return EFI_SUCCESS;
4 L9 H3 k8 E0 R+ F8 ^ X$ _ }
6 L3 i; S( I" W: M# C( ^0 V [% X/ {5 H
Status = FileHandle05->SetPosition(FileHandle05, 1); # B& H- r; Z$ ^: V
if (EFI_ERROR(Status)) {
2 t( q- P% L" r2 U3 a, S( C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) t& j: B2 w. l8 \8 ]/ v4 z
return EFI_SUCCESS;- P( c( {/ p# d& ]! u o
}
0 |: H% R" D" {& _& l
) R; C/ i% u& |% Q- @( s5 ~5 o Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);7 A; h, Y0 }) j+ K: `
Print (L"File Buffersize is %x\n\n", BufferSize);1 k8 v+ \* t' G9 g' f
if (EFI_ERROR(Status)) {
+ i5 G# z" X! S, C* F Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# h: a5 b/ D# _' m& c9 N8 _1 V. A return EFI_SUCCESS;' n1 H8 [0 b# Q9 @, q6 A
} / J4 i# ^* l' c' s3 @; i3 |: w) w
" d( y6 f% D3 _ Status = FileHandle05->SetPosition(FileHandle05, 2);
0 ?, w; s, I- T/ k if (EFI_ERROR(Status)) {
% F# G; V( D2 E# ]- I; b1 A; r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: S( T5 Y9 A) Z. r }0 z+ L return EFI_SUCCESS;3 ^& _; W, Z# u: L
}; O) u/ J7 y! {8 A: U( k; D9 ^9 Z
% K g0 i1 h1 V3 L( E0 }
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);9 t, I8 ^; I, M
Print (L"File Buffersize is %x\n\n", BufferSize);
% O' A# g, O4 e8 u. s1 S# U$ M4 @* b if (EFI_ERROR(Status)) {
' r" q7 \+ Q6 E$ O j8 f/ h4 m Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
* H/ m2 F, b. j. P. `5 h return EFI_SUCCESS;
: V* V# B; X5 [! i m" J } 0 H) ]# y" C; m! h
- x% [# F. o9 o" Y; `. p7 z0 m
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
6 f( z, [9 Q* j# q* s Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);& f: w C7 S }. K/ {
Status = FileHandle05->SetPosition(FileHandle05, 2);
+ p1 @! L: E( c% L if (EFI_ERROR(Status)) {4 V8 x8 I8 o' Z M2 {9 Q' f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* r/ k: v1 X2 l" S return EFI_SUCCESS;
7 k' ]6 j9 p$ H3 B- ]9 M7 u }8 t7 O+ x4 h1 v
# m* I9 g, T+ D4 [* M. X1 _/ G _
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);9 j6 I. Q1 }& V$ B" Z, K" `. p
if (EFI_ERROR(Status)) {
' Z+ f) y' j V' r0 v Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
9 _' k9 M; u: O5 o! E/ p, @ return EFI_SUCCESS;, D& E7 H7 K+ R" J. s: k
} 1 [- T5 O1 n5 a* n
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);( y2 F- F. M" {: [7 f+ p5 f
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
2 r6 M1 H% h( s3 Q$ F2 P2 [% E
5 `/ C& L/ Z& @0 W. U Status = FileHandle05->SetPosition(FileHandle05, 10); # N9 J/ k, z, v; e
if (EFI_ERROR(Status)) {5 _0 T0 @7 K/ G
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* ]4 Z) H1 `0 a3 Z return EFI_SUCCESS;! b2 i/ D, R3 i C$ a
}
0 M' l" ]4 L, K) _7 Q6 ^ . }6 l1 T3 Q3 T' X" R: T
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
3 `# y7 G5 @$ h- F; q0 Q) ^ if (EFI_ERROR(Status)) {
# _* X4 Y( w8 f, K6 |. `/ ? Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
# J4 n0 w$ q9 t9 r7 L. k# X return EFI_SUCCESS;8 O' |( S% I$ R/ t: E$ T7 ~
}
1 }/ A; N2 h1 p3 P7 G+ R) K
6 G4 G# ~) {3 Y9 j Status = FileHandle05->SetPosition(FileHandle05, 12);
! ]) O! ]& \1 D5 g if (EFI_ERROR(Status)) {
$ e* p) a. Q2 g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 O0 Z% [$ i7 C& @) `7 D+ x return EFI_SUCCESS;
1 X& t) D1 J& Z }2 x1 j; E7 P: S; Q
2 _ e. U$ u$ E5 P; S" |+ e5 j: w
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);1 B! j& O# Q, E; V. A) d7 \( O
if (EFI_ERROR(Status)) {3 |, g8 @3 n8 X
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 v. S9 U' B, `8 Y- x! h8 U
return EFI_SUCCESS;
! ~& u q$ `3 t( G' a }' r' {. ^3 f$ v% I
4 a. l# s3 E8 _# V X+ Q: [) G; z Status = FileHandle05->SetPosition(FileHandle05, 14); % E: F& B! {8 |( V9 K
if (EFI_ERROR(Status)) {
' u' m" f; J- p# z: x9 S3 t/ z0 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, \& E& E' a2 E* D% K
return EFI_SUCCESS;+ c* d- _& @& H) i5 m0 j8 q; K ~( W6 }
}
- A7 P$ ~9 P3 b* y7 s6 }& U8 [$ a + J: ^2 c; W6 p
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);; t q+ B g$ L
if (EFI_ERROR(Status)) {; i1 ^/ ^$ f$ \: i. y6 P& [) u: c7 U
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ R% X1 V1 w# C
return EFI_SUCCESS;5 }# t7 l8 y% \ t8 }
}
$ e5 E5 v. M/ x; U3 E3 W& T/ `$ _ X% }
- p$ h& ~0 X8 d! r r- n Status = FileHandle05->SetPosition(FileHandle05, 16); 6 g; H2 i4 h# n" g8 j( Z
if (EFI_ERROR(Status)) {2 u. Z3 q* w: l. j& z( z# N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" K4 x. r1 N1 Q return EFI_SUCCESS;
0 K! H$ i. M3 k+ A } c3 i+ L% q+ E1 X* }( }* E9 v6 ~
+ @& V" K3 h( U3 ^9 ]! E
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);. e0 h( Y" N& ^. g* q9 u
if (EFI_ERROR(Status)) {
+ i4 O- B1 i! I- e; d+ Z1 U) X Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% N) d' U' c- v5 F
return EFI_SUCCESS;
8 J3 r- _, c% T( B }
+ ^+ h# V6 `; r n. c% ~' V! f- e: x- M- a
//---------------------------------------------------------------------------------------------------------------------------------------------------------------0 C# c; f9 M+ w T$ H8 \2 f4 W; \
' }2 e& V' [0 ^5 c6 Z- z9 I Status = FileHandle05->SetPosition(FileHandle05, 18);
$ _4 z, [% R, q( v* Y. A if (EFI_ERROR(Status)) {
' W3 [. X, g2 @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& r2 a: q$ j1 K& h1 E return EFI_SUCCESS; [: P0 {/ D* s1 }* M; }, m' ~2 s
}
" L. X; b* R" ~9 x* h6 Z6 Z+ [ . r" g" s- }! ]- P, N1 C8 ~/ I
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
4 n$ `' x; q$ y if (EFI_ERROR(Status)) {
; ^; f; }3 Q5 K% f- c! `) t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
# `. s' v* I7 g return EFI_SUCCESS;1 ~6 j& p( ^5 d9 R1 i: g0 A6 Y
}
) P8 t3 U5 m! ]+ G! G @* {1 H8 r
9 e( f+ e" h, a- H FreePool(DestAddr); % U; l! f1 n& @( @% h, r
0 c6 j* t* G& O6 I Status = FileHandle05->Close(FileHandle05);) z; ?" Z" K( t v
if (EFI_ERROR(Status)) {
9 w; @$ H- k$ `* N" t% u! Y6 q Print (L"Could not close file, FileHandle05 err code is %x\n",Status);/ e& O# ]+ h! z/ E
return EFI_SUCCESS;% V3 |) Z3 R9 C: l
}2 H `$ | r% g
$ y% z! Q) K7 C# q Print(L"File Name = %s has already been created.\n", FileName);7 C" P6 K8 d8 ~" s t' k
1 T' z! ?8 u* l6 Z: o; U
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n"); u3 F# \' C* k8 h/ R0 ^ n
WaitForKeyOrReset();& o/ X8 g, W0 t( F
8 p6 l5 Q6 d1 t3 q
return Status;& Q+ T2 T' C# J" m
} |
|