|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*$ \" F; x; {8 v# X: e4 o
* myfileio.c" ?7 U8 ?/ F* o7 O. {
* Apps
& W7 ~# y6 B: V- l% o3 Z7 v# ? */
6 i3 | |1 I) z. j- i. z! z& l2 S" r% R& w r) V
#include "efi.h"1 L. |6 ~! h" v8 B" }0 w6 ]
#include "efilib.h"* c% f4 G, U5 x z& ~
O% d% y4 r7 k t; E" u#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
1 O4 W1 e& c; u8 z" [' j1 ~) m7 z# @
static EFI_STATUS WaitForKeyOrReset(VOID)
1 v# c3 }! d( G{
- n6 v) B- A0 r8 `. @; I4 Y/ o EFI_STATUS Status;
I. x1 M' @! l( D! [4 R! | EFI_INPUT_KEY key;
, r" X7 R6 P; z4 ^9 d3 K UINTN index;
7 s! d. Q$ x* U! [5 z. s6 ? $ B$ L% m0 ^( e ]; P' y
for(;;) {8 s, }9 l3 W2 n
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);7 h" ~; H% | v0 V! E* |
if (Status == EFI_NOT_READY)
% b- z2 F0 C a6 h. K' t BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);8 v1 L# ^. t, @6 W
else- ]5 ~, }1 L% p9 ~% [, s
break;
5 O% F9 a& K" a; {& ` }: O& E) d* W+ q7 W* S# ~7 N
if (!EFI_ERROR(Status)) {7 a0 s1 l. a3 [9 z8 f
if (key.ScanCode == SCAN_ESC)
( ?$ F/ t$ N/ D- N% i RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);6 k) v. R, K1 O( A! J8 v
}7 V3 B" l [6 Z, P! k+ W& B
, }7 q5 g9 e5 h return Status;. w! H6 \7 q! N5 P
}
. e5 v" c6 b) A: d2 M
. P- D9 a3 l& Q$ j( h( h* u( {EFI_STATUS
* _2 h6 A, P, A% j2 ZEFIAPI
- G* u7 {" [( T& {' O: L. n0 IMyfileioMain (IN EFI_HANDLE ImageHandle,) r: R! z( h6 [7 g" e: P7 f
IN EFI_SYSTEM_TABLE *SystemTable)
6 M2 h2 c4 `, t4 [+ N6 ^- u{& H4 j+ G1 Q6 K% W' [
EFI_STATUS Status;, g1 a4 L: P+ b( d& d! A7 B" f
EFI_HANDLE *DestAddr; ) M: {5 F2 q `; m9 x. C
EFI_LOADED_IMAGE *FileHandle01;! e5 P' x7 D8 t1 \2 K( ?: S7 A
EFI_DEVICE_PATH *FileHandle02; ' q+ e y2 l6 L( l8 J
EFI_FILE_IO_INTERFACE *FileHandle03;( q0 @; m& g n) N }
EFI_FILE *FileHandle04;3 r; B! q7 ]8 Y9 _5 z: J
EFI_FILE *FileHandle05;
& B) d8 }3 u/ c1 N/ R" F1 m CHAR16 *FileName;' u5 p& e& z4 H4 y6 b& B. y* f
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
* g, p0 g, f! {: R Q1 y+ b0 g$ S UINTN BufferSize = 8, BufferSizeKeySpace = 2;5 I6 Z1 }8 s( R# v
int Bit0, Bit1, Key_Space;
4 p, J' u! H8 E" N- c
* }# V% G( d: Y1 K FileName = L"NewFile.txt";
: ?* e# ~' j, m$ B Space_Key = L" ";
# s4 M$ O3 D' l( s$ x' E 1 G+ u- O! u) R9 i% U
BufferA = L"ABCD";
3 z6 f$ h& S+ v6 u BufferB = L"EFGH";! u3 [( @- A& R: y# m2 O$ f
BufferC = L"IJKL";
1 e2 c+ d3 l& C! J4 U$ u
( }! k9 c, p$ e0 x Bit0 = 0xff;
/ v* O1 k* L" R* ~ Bit1 = 0xfe;
" F: V# t) w1 ~1 K& K& a3 c+ I ( p2 Z, }" l$ o, Z5 m) m& i
Key_Space = 0x0020;
8 L* g8 }. k( {. l
1 ^6 f& _) D5 C/ a2 I InitializeLib (ImageHandle, SystemTable);
, C; u/ n8 _/ r, n# u" a; W
. {2 o3 H$ }4 @; x8 U0 ~4 f, s( p DestAddr = AllocatePool (BufferSize); % T; c; [/ U1 N d0 p/ Y
+ o7 x2 Q9 O& y8 y/ }* Z! S
Print (L"Value of Bit0 is %x\n", Bit0);
; ^/ ^$ \, j9 f& _ Print (L"Value of Bit1 is %x\n", Bit1); ! ]$ C! Y) }5 N
% P, O# B1 E3 E5 \ ! i+ c( n# W" _6 H- N z; M& e$ ]
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
& y ~% t: U- J5 K1 c if (EFI_ERROR(Status)) {
. {, _- o. o+ U R9 {# ` Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);+ B' b$ @- h; M% {8 U
return EFI_LOAD_ERROR;
. q- ]3 B& _/ K* ^9 S+ K }' A# f, Y1 M% E9 G: W
8 J: O2 T, j; i$ z4 ~# p6 g Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
" r# Y$ b2 Z( h+ ? @9 i, p if (EFI_ERROR(Status)) {
8 Y; Y/ {$ i" W Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
) k; _* v5 @% K2 b/ V! k3 [; p return EFI_LOAD_ERROR;
$ h* h# _ x1 v! l- A } ! }+ B( s0 T' [, A3 C% a% M3 n6 x
& P- \- s% |8 \- H/ W Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
% Y. G5 g v9 H' s, m; K* N if (EFI_ERROR(Status)) {
# c3 ~6 S. v9 H- J2 }, h( P3 S Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
# V) w, U; j# y7 x return EFI_LOAD_ERROR;
+ S# c d; S$ L1 E0 E }5 h4 P0 j! G9 _0 U, ~) c+ l
1 o9 K8 @0 x g. r n Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
0 a4 A( z* x X+ k if (EFI_ERROR(Status)) {, D, V( }6 K, I2 u3 M
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status); i6 y0 f+ M }) X0 Q6 |
return EFI_LOAD_ERROR;
: S! `- h0 D3 Z* F+ u! d2 V; q+ X }
& F- ] j3 d# A- ~ , u7 K2 l: W: w8 G) t$ S0 [6 Z) S3 d, [
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
5 `4 l/ z2 o" b if (EFI_ERROR(Status)) { E" c" H' O- j6 o
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);- n" ]* _# z/ ?3 }$ g2 X
return EFI_LOAD_ERROR;
& }$ m4 j0 q; ]7 w0 }- J' `1 H }
* j% }0 ]9 o7 N7 T# h$ x0 g ' _* Y1 V/ ~1 T. K+ z/ c1 x/ w5 W0 k6 |
Status = FileHandle05->SetPosition(FileHandle05, 0);
) x0 K v0 S3 ]3 O' ~/ C% p; T if (EFI_ERROR(Status)) {
6 c' x2 o7 d9 ^4 m9 [1 C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" P% Z! B2 p( ?; m) A) g
return EFI_SUCCESS;6 O2 q5 P* H3 h G. a, J) n- u
}
" d& Y. y$ e7 k+ N: L, a
9 N1 r7 P: {4 u3 a8 ^1 l Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
0 M* Q$ ^' G/ m+ M9 r, [2 H3 Q Print (L"File Buffersize is %x\n\n", BufferSize);; Q' O% t8 P c7 H3 h0 C' U( L8 _. C
if (EFI_ERROR(Status)) {
. n% x, `& G4 i Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);) L% r1 g1 _5 Z3 _
return EFI_SUCCESS;3 d/ e0 }; V! @5 b3 g C
}
( Y! Z @8 k3 W$ ?; [; r# z% U2 x% q* b8 T: ]7 T. U- q
Status = FileHandle05->SetPosition(FileHandle05, 1);
1 M3 @1 P. `3 |& ]1 X8 K7 I# ]" `- R5 D if (EFI_ERROR(Status)) {3 M3 Q4 m4 g& ]' |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 q9 W* x8 R1 _: l; L' { return EFI_SUCCESS;
+ F- L2 S4 z1 Q7 s, E j }* v) M" t; r8 m7 H, i, P
: c/ g, q4 X8 Q
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);4 b8 F8 X+ j" A* A; f
Print (L"File Buffersize is %x\n\n", BufferSize);4 W( s5 l+ j8 C: Z# |
if (EFI_ERROR(Status)) {9 O; k/ I- ]. ?+ L3 d
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& u+ p% G/ W+ f0 ~* y( b return EFI_SUCCESS;+ ^2 N2 n7 Q- ^6 U. ~! @! F
}
9 v4 \$ b0 i% {3 n & f* @8 h3 Y5 s5 V5 B' ?
Status = FileHandle05->SetPosition(FileHandle05, 2);
3 N9 H& {! C/ t if (EFI_ERROR(Status)) {8 t$ K8 M; ^# O7 W5 C9 m: {! z" D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
U3 @, \' ^- M. B return EFI_SUCCESS;
: a }# E' [! T9 q3 z7 k }
) l5 v' W' b* E% K : }/ f' d6 c% \0 L
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);# E8 s+ ]6 C Z; |
Print (L"File Buffersize is %x\n\n", BufferSize);# |+ y7 |, w, x3 ?
if (EFI_ERROR(Status)) {( P. P4 C0 |7 M! K( x& Q4 K
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 P( V' i/ o- m2 N" l6 R return EFI_SUCCESS;
/ \6 p' w8 r2 E) F- A }
9 U, D3 Z$ N$ k1 |9 H, b! o) f * |0 r6 A6 D+ z8 \; U) N$ h9 n
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>+ r9 J% K: O: E# A3 o% @; x
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);$ Z5 r! A! |2 S- F- ~9 c3 [8 I
Status = FileHandle05->SetPosition(FileHandle05, 2);
9 D8 M. |5 Q- r+ X" o8 q1 ~ if (EFI_ERROR(Status)) {
& @9 z) k7 |- o6 Q2 s: Z$ p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' d/ \9 j! f% w$ ^
return EFI_SUCCESS;
, z( t# Z, f3 W2 p- ` }
% [* @: Y2 J! y$ F/ A
. f( u' M+ l, ?9 S7 i Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);6 A# R2 a3 H& _; f: `
if (EFI_ERROR(Status)) {
9 r/ y& k7 w& r; C: u Print (L"Could not read file, FileHandle05 err code is %x\n",Status);& i, u* o8 h* n! Q- U9 ^8 _5 E- @
return EFI_SUCCESS;
! W) k" X( d/ W+ P1 j, A } - G# H8 ?( j# p. |* x# D
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
6 [' s8 W$ j) j8 W Q0 D//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
Y) E6 [! i, x6 Q2 p, K, ^
, R0 a: l n! K( w; u* t8 }3 x: p Status = FileHandle05->SetPosition(FileHandle05, 10); ! b8 m' F6 n: r( W9 V/ H
if (EFI_ERROR(Status)) {/ F8 \' B( j. i$ [. s* ?1 m
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ ^, s8 c. l: f2 M# P return EFI_SUCCESS;
( V& k7 u8 N# c4 O+ Y6 b4 F }7 N7 O: J; g, D, Z
. a9 Y: C4 t6 k& W' Y. ?
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; f, J: t& D7 H! ~, A+ _5 d if (EFI_ERROR(Status)) {
0 C5 d2 q4 d7 N9 c0 [, M' W. `( `+ y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
- g6 W% s: p* |; L return EFI_SUCCESS;- b( k# @0 a1 C! n' I
}, H: ^8 n! J4 k0 w% ^
" L4 i8 x8 o: M+ J |9 F2 ]3 b" h3 |
Status = FileHandle05->SetPosition(FileHandle05, 12); # t/ P) t& H/ J" c1 [+ t' ~
if (EFI_ERROR(Status)) {3 q, A2 @0 t0 e/ i5 J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 `& `8 n- H% ^( i7 j* D return EFI_SUCCESS;/ w) S: v0 b$ n0 k9 y0 O
}/ c* `- h% ]6 b2 W! S6 J( a* F
* I" L. ^% p U: ]1 p* v
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); {9 Z M) T- v% f S
if (EFI_ERROR(Status)) {
5 g3 K; T$ \& a Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 s/ y0 u( u* B& \6 ?
return EFI_SUCCESS;
# N {7 h- g* v% N }. H( q! y3 D2 }; b- M* I( a
1 k' d0 M* u& Q. r2 {, |' S Status = FileHandle05->SetPosition(FileHandle05, 14); + k, C" n8 ?1 u3 M; D
if (EFI_ERROR(Status)) {6 C. k6 L5 E5 B0 @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, _+ v& `. P; P- g
return EFI_SUCCESS;
' C, e" a+ A' f% N }
/ o0 ^1 W7 Q( w- x" h
7 D; u, H3 u2 s) M Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( ]. S5 Y) P. w y M if (EFI_ERROR(Status)) {2 R; p+ b# W9 W2 t; ?7 s
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 H* u8 S% H3 |. i/ m return EFI_SUCCESS;
2 b: S) q, f- N1 o1 n% K }
3 R' ~0 h7 U) _) _: \
2 n! F' k& E$ i Status = FileHandle05->SetPosition(FileHandle05, 16);
' A \& s) X3 w: r1 t8 D9 e5 k if (EFI_ERROR(Status)) {
* {. Y2 b9 S+ W. Q- T% E5 Y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; Q. X* u; w8 M. ^& v p$ r return EFI_SUCCESS;
* S6 p: K7 ~( _5 A/ a. K }
7 t4 H: n1 d7 k 2 ~/ o" k+ M, ~" A5 u
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 E7 k, v1 `2 l; O" @( [& g8 x" I if (EFI_ERROR(Status)) { _* v+ ?# s" [/ W7 s" Z; r) ^
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);# D" ]7 H1 q" n. g r9 k
return EFI_SUCCESS;
! Z& I0 L, G& c }
5 L1 F& w* ]7 r/ A4 _
% ]2 t0 ^" |+ N0 O) b//---------------------------------------------------------------------------------------------------------------------------------------------------------------
5 w# S: P! U. ?% v
X! w& {! [; l! i: d' z ` Status = FileHandle05->SetPosition(FileHandle05, 18);
# b6 b' _8 s. I if (EFI_ERROR(Status)) {4 f7 q8 \7 q! F. K+ H" w
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 f# t1 X4 k2 r& X% G
return EFI_SUCCESS;; r8 H+ j* @' y1 ^8 b
}
2 l' ^, C8 g( U5 Y- s
. F3 C5 ?* i5 C F" C1 K5 l Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
8 b+ W/ ?* D+ L( W9 _ if (EFI_ERROR(Status)) {
+ X( h; |# L8 f0 i Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 Q. A7 b( w4 r3 b8 t" }1 i5 j return EFI_SUCCESS;
2 G$ L+ E: W7 J( A" z o' G } 1 W! S0 t2 B6 v; J1 o8 K" f. w' H
# l' P9 I. z* f1 c. o, I/ a
FreePool(DestAddr);
* e7 f D5 q1 u* R% d( E* f |& k
: ^/ B& J2 ^0 L5 C. N Status = FileHandle05->Close(FileHandle05);' ? a+ w7 @- u: R, f
if (EFI_ERROR(Status)) {
- q# W- [/ ?& V+ @" b Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
! r# H9 }1 S: `+ m7 L, l1 H return EFI_SUCCESS;. A3 \1 h \( V
}% c1 D. ~2 z N- R t
% i! u! w( P/ F9 `% @ Print(L"File Name = %s has already been created.\n", FileName);
. E0 ~) B$ w" }! d, X J! E& G0 c u: X. Z D, D
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
% R+ p* T/ G, g& C# c" h" {( Y WaitForKeyOrReset();
( d! E/ F- R: A% f 3 H) z! b2 z+ W! t) V% D- {: D0 L A# S7 r
return Status;
7 [ }, q" }9 M: M. C) r} |
|