|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*& ~0 C' D- p1 g
* myfileio.c) p4 D/ g, |8 j; L/ L
* Apps' w- e2 e& I& o8 O4 v
*/) A. N- X% i3 ]2 X3 k
5 O! `8 D9 _5 Z#include "efi.h"
) q& b- }3 O' Q+ c- c* ?#include "efilib.h"
" V# T4 e& D2 z+ b
- }# N( n/ \! V& J: f" X4 c#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE2 s# b+ ?, o7 G1 y
9 S' H( N+ S l# _) Bstatic EFI_STATUS WaitForKeyOrReset(VOID)6 p+ V* r# H$ x6 M$ W
{
3 }( G; s4 c& c& ^- I EFI_STATUS Status;
" E0 h9 P+ C- k! f) o EFI_INPUT_KEY key;
6 a t' }8 w" @) X! {# A/ b UINTN index;0 p6 q& w' z5 |
: W* u& L5 A6 p. f1 Z6 C& A5 v/ V3 E8 { for(;;) {
1 \& q; O3 L+ x8 Y8 p, v0 Q Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
- O G6 c6 c3 Q9 U" `" m# K if (Status == EFI_NOT_READY)
- V/ t+ ^# v2 o0 z p BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
) o7 s' r7 J! ^! G) Q; n/ e% m else7 t4 R3 ?# @& R/ Z% R
break;
2 R5 o1 ?% t& a+ b; f }
9 _* V w8 |" D' h1 S if (!EFI_ERROR(Status)) {
% {+ M7 f( Y7 H/ y7 c if (key.ScanCode == SCAN_ESC)
0 f! U. l5 _. Q# E RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
# V, v0 {3 b3 Q* H' ? }% r$ y. H/ }7 {/ [# w0 @
; [; A$ I1 L- z; E7 Y; C5 L
return Status;
& z* ?7 z+ g! r I$ |4 s}
8 h$ m& f1 h& p5 n5 R0 y6 n. V, j6 L( P
EFI_STATUS8 g: m$ y0 o& s9 B
EFIAPI
. _: o% w& K) ? D+ N$ W3 {MyfileioMain (IN EFI_HANDLE ImageHandle,
. G6 K( X7 j+ f6 P% g7 { IN EFI_SYSTEM_TABLE *SystemTable)
3 F! M" U. e8 k5 ^{
8 `/ @0 ]0 F8 o: N6 X EFI_STATUS Status;. n( M0 A' q* a) |; m$ i
EFI_HANDLE *DestAddr;
3 b+ Y6 z. g8 }9 `" O; m ?( x. x EFI_LOADED_IMAGE *FileHandle01;
0 s: c- M* R, z" d EFI_DEVICE_PATH *FileHandle02; % Z9 |1 S+ g1 M1 L
EFI_FILE_IO_INTERFACE *FileHandle03;
7 H" ]; W, Q1 K+ U! x EFI_FILE *FileHandle04;) f- z& n. a7 |
EFI_FILE *FileHandle05;5 }' d# y$ k6 M* @+ y+ m
CHAR16 *FileName;& R9 y3 s4 D8 A. x' @" S7 Q5 I5 R* f
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;" V0 q+ @" m9 j- d6 Q
UINTN BufferSize = 8, BufferSizeKeySpace = 2;! i7 X" P: a) D, H0 p
int Bit0, Bit1, Key_Space;
7 v4 ?0 I% i7 X& _% Q' A7 t" B* ?7 C ^7 s. k
FileName = L"NewFile.txt";
* b! ^5 D: w( m+ o. W5 d6 Y. u Space_Key = L" ";3 z! s. v0 C; a& l9 N' d* b8 o# u
- [7 U. O6 A8 P1 _! V* U BufferA = L"ABCD";& E6 U% C, X, L/ t* P. M8 U
BufferB = L"EFGH"; {0 [' b( Q" p( |
BufferC = L"IJKL";
* F, M6 ?' V: Q9 M$ h: l4 S/ h; g+ ?' m9 _
Bit0 = 0xff;
4 v" U4 @. k9 t4 \ Bit1 = 0xfe;2 K- D' V" p7 W# C, p
. u4 k* `6 \8 ?9 s9 h
Key_Space = 0x0020;5 P0 B0 B9 \0 P/ f
; K1 ?* e% s+ T( S( V6 N! x InitializeLib (ImageHandle, SystemTable);
: D5 h0 Q" g0 V
* s+ X6 S6 R7 E: `( y DestAddr = AllocatePool (BufferSize);
$ E# p6 P* V/ j) d& ]' B
" O# a: h% l" E1 B& I5 E0 o Print (L"Value of Bit0 is %x\n", Bit0);
! [" l3 x' a/ [' o! m Print (L"Value of Bit1 is %x\n", Bit1); * _# X: r# y7 m d: M
3 u6 ?. y$ C1 T2 V) ]2 O
7 h9 s1 e, E$ f Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);$ A1 l; A# {: c- a6 x) G Y$ |( d
if (EFI_ERROR(Status)) {
, e" l* ^# K8 H: J1 L% x( D4 t6 m Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);2 S% G7 i# W; E+ h9 R# y
return EFI_LOAD_ERROR;0 z0 k6 m# y# [
}' o) H+ @9 Z! Y9 e
9 a4 C( G# {) _; v, m Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
! ]2 ~' z: z* \/ [1 g6 N if (EFI_ERROR(Status)) {
9 A& a* e6 Z# ]) ^' @4 ? Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);) R) K( {' F2 k6 f7 G' ]
return EFI_LOAD_ERROR;& f; G+ x, Q$ ^ V
} ; I6 K6 ?) E3 f d; W* |" x4 v1 L
3 b8 I+ J* f; O: v2 x, z Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);% N3 J8 L9 w# g, h& S
if (EFI_ERROR(Status)) {7 M% I- P* v* ]4 }8 B; w
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
$ A& P0 }: w, N8 J/ M& k return EFI_LOAD_ERROR;
+ u' |3 S+ ?' o, H& b8 R }, a) ^/ V; D: _, W+ n! J
2 Z/ s4 Q3 G9 P8 P
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);" N( R7 O' C' }6 e- u2 n9 l7 m
if (EFI_ERROR(Status)) {
2 m( d8 Q) g. ?8 g Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);" z# Q# O* U( d" W
return EFI_LOAD_ERROR;
$ i, m* t" B" D- D: N }
) |* x, X- o. W& K- G! b, r& D 5 k7 b6 l1 m6 X# q! G
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);( ^: k1 A0 V# y& m: l
if (EFI_ERROR(Status)) { G4 J' Z, U) ^" Y# C1 T2 a) Q- u
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);1 F V' {) B+ r
return EFI_LOAD_ERROR;
5 Z. o( Z# H7 n; o/ t }/ L: b4 y* P+ q9 Z* @* i+ P
$ z P7 F* u* a9 ]$ t6 [ Status = FileHandle05->SetPosition(FileHandle05, 0);
% s/ ~% P* V" b! A if (EFI_ERROR(Status)) {
" _1 x& l, E; Q3 O( [ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* P: Z# e9 E, ?( @. v- \9 @
return EFI_SUCCESS;
. x" C, D4 M) f9 { M9 Y, z4 D }
! {8 T+ F( A2 j7 V7 g! t) i/ [$ a & [$ g5 F! x0 S9 H5 o* Q9 @1 T
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);" P% y( L1 k' b4 }/ |
Print (L"File Buffersize is %x\n\n", BufferSize);
1 ~. z" P) m9 G- w' o& x* i if (EFI_ERROR(Status)) {* P/ y( u; _4 t% n% V! }
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
6 h& V" _1 \( }# R1 B2 @ return EFI_SUCCESS;
1 A! q/ m! B9 S: }% \4 t8 Y }
& c. J; O8 V4 O: U. X) z& c7 P5 C4 _9 P: M
Status = FileHandle05->SetPosition(FileHandle05, 1);
* v6 o* X* C0 R' m1 k* w if (EFI_ERROR(Status)) {4 j5 m8 `) ?8 o+ G. _" p) l
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! S( `2 M, H- f. u, b
return EFI_SUCCESS;0 d1 W7 `7 z) G) k
}. v* R7 o4 W4 b: j
7 z. Y$ J' ^/ |1 |
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);' q N/ e, H- M1 j
Print (L"File Buffersize is %x\n\n", BufferSize);4 q$ p/ H3 p! [% V( j
if (EFI_ERROR(Status)) {! m e. r6 u2 t: T
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);9 f) ^- v# N% F: ^; L! M
return EFI_SUCCESS;1 b/ b: H8 P! k. I2 f+ o
}
% `8 y3 W: a8 I- W! K0 h
' t K$ e' v- w9 E* p Status = FileHandle05->SetPosition(FileHandle05, 2);
|. O$ c, f6 h# g. y if (EFI_ERROR(Status)) {
; ^ n2 ]) _" F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# o$ b. N5 e& o; e1 G$ N# m return EFI_SUCCESS;3 }" Q' u Z6 G
}
. f- U( K/ J- g) X9 N2 o2 P. o/ u
/ X3 K" V0 I5 @4 q/ G- z" D# ?4 L Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);: G9 r! ]" L+ f4 Z2 E) q6 X/ R( b
Print (L"File Buffersize is %x\n\n", BufferSize);# \1 e3 e( W6 A3 {- |6 K6 L" e4 h
if (EFI_ERROR(Status)) {" ]. m; u, b1 @, }' f, E: @/ U& P
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
) v% z: Q9 ?5 C return EFI_SUCCESS; v2 v8 a% Q9 h) S
}
* e3 {% ]& |- K8 S: O, P0 f
9 R* P; q8 p* U \, E$ X y' J//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
9 X3 b5 y s( C Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
+ b/ a, }; E, j4 k2 x Status = FileHandle05->SetPosition(FileHandle05, 2);
8 u% v: [0 Y: W( I if (EFI_ERROR(Status)) {
0 h. J& ?) W6 r2 ~ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; B/ f* }6 y; u7 Q return EFI_SUCCESS;+ @6 a5 n' @. {8 Y4 R+ h
}
7 B J: R9 h2 T# c4 a4 v; T
+ Q# P4 B( _# l/ a) U: C5 l& g Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
# `/ N8 E: u7 p+ @ if (EFI_ERROR(Status)) {
" ^/ u' D, ^; K0 L Print (L"Could not read file, FileHandle05 err code is %x\n",Status);5 o# [+ E% t* [+ C7 ?4 n; D3 t
return EFI_SUCCESS;
$ r, O% k* Q1 C8 i0 K }
+ A! v O" n: f3 k- W Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);6 l# i3 O% F/ r! \" Q" n
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<* U( e0 h" x; @0 j
: [9 s: E$ K" M6 x8 f. r2 o& H
Status = FileHandle05->SetPosition(FileHandle05, 10); ' x$ u! _% w+ R1 D8 S2 K1 L
if (EFI_ERROR(Status)) {
4 ^% |* u. V2 E) I5 U& y8 w Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# B4 {! z4 Q) X0 H, k
return EFI_SUCCESS;
W# w1 e/ N. n5 }! V; m( a }' b0 P. Q& p m+ m. R
9 ~$ g& s! B& V- y8 F Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 P3 f0 X# P9 _; R if (EFI_ERROR(Status)) {9 `& n! [/ i: {$ t8 K* `* b2 h
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 f; m8 s& c0 }7 X2 g$ R
return EFI_SUCCESS;3 y1 h% A( @( G- C6 \5 v2 H/ G
}
/ w( ~) X) T* a$ U% j/ S- J* O* V3 R1 k
Status = FileHandle05->SetPosition(FileHandle05, 12); 5 u8 ^6 T9 j+ ?5 a
if (EFI_ERROR(Status)) {$ K, ?' f( e' F6 D
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 z E2 j" K" I L# ~
return EFI_SUCCESS;* e. U6 C, b7 r0 k: j
}5 A2 @0 j6 Z8 O, Y" d. O* k; p
% c- K9 ~5 n/ Q" D; U Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
- E+ j/ X7 f/ p0 C if (EFI_ERROR(Status)) {
( N8 g) i. V, B$ f% ^5 n) h Print (L"Could not write file, FileHandle05 err code is %x\n",Status);- K7 d" s4 I" K
return EFI_SUCCESS;
) d- D$ d5 i$ U1 s7 v6 ` }
" m' d1 B' A& `+ n' ~9 n6 o& b" Y/ ~6 U# D( v, F" G
Status = FileHandle05->SetPosition(FileHandle05, 14); - K: o& i& U9 z" w7 G! h. A
if (EFI_ERROR(Status)) {. G G* N7 x8 |5 `8 b' f
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( _* W1 X% r( e
return EFI_SUCCESS;1 X v+ N% ^8 R1 P; M% ^
}* F5 y% v2 O- S& z9 h7 e
( \" k5 B, X$ d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 U4 c" J9 Y2 Z9 b8 V if (EFI_ERROR(Status)) {
+ n* J5 ?5 c. Y D Print (L"Could not write file, FileHandle05 err code is %x\n",Status);+ v5 C4 {8 J( I4 K) X4 X# L
return EFI_SUCCESS;
: k3 \( A% Z$ ^: m }, T- G5 Z6 U( \' C
, n& I. c" o, Y0 o" a1 n" l) r: f' s- _
Status = FileHandle05->SetPosition(FileHandle05, 16);
4 B- v; R* G$ e' u/ A6 M( v if (EFI_ERROR(Status)) {) [4 N/ I% T5 U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" E/ h! ^" Y+ L
return EFI_SUCCESS;" K3 f3 M$ S7 f. }% ?" N; r, I I
} B1 O z- x$ B3 ^9 n$ V+ N
G3 _. k% X& F4 S Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; R1 @. H- Y# s if (EFI_ERROR(Status)) {. R4 w, v; ?- V& e9 {
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
5 W# _# q# |$ Q- u8 t return EFI_SUCCESS;1 P" i8 Q$ }# g/ J' K/ r1 E+ x
}
8 f* r; J+ d$ l1 ?
* p: O$ a4 X, s& }: i2 R//---------------------------------------------------------------------------------------------------------------------------------------------------------------
Y2 N* j& K; o
8 E' T2 R2 P- x' n4 N Status = FileHandle05->SetPosition(FileHandle05, 18); ) c& h H" T9 a! H& {8 r0 s
if (EFI_ERROR(Status)) {
6 z" F3 V( \* y* w5 N) g8 ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( g( ?, r) g2 B- b. _ G) ^4 ?' Z# t
return EFI_SUCCESS;
+ E- v# _$ ]3 \1 Q$ a, A* X }( j, N8 s8 d$ b3 q% j( u0 g; ?
n& K7 x- l1 h8 z3 u {! [8 | Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);6 j$ w& W. |! q8 x
if (EFI_ERROR(Status)) {
9 e. T* |8 h5 H! g8 v. h* f6 @! M Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) N* a% K% i) g# x! O/ k return EFI_SUCCESS;. G+ @! h3 H4 U R) K( f0 V. P( A
} % ~; N l6 y4 I( p, `
% u0 M( j# g' n) ]& u2 D FreePool(DestAddr); " Q9 F# V Q7 |
' _2 q# A0 A: ~
Status = FileHandle05->Close(FileHandle05);
6 p. w" _/ T E3 ^- T% X7 ~ if (EFI_ERROR(Status)) {
5 {4 |% }: U) F/ _; K Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
$ o- q7 y1 K$ O+ |) ?# p# u ?/ Q return EFI_SUCCESS;
) L, |5 q: ]5 {9 W* p2 l }% O9 ^- F8 O, [4 {* ?7 i& d( c
* U% _5 \- B: h3 G8 w# K& {0 w" m Print(L"File Name = %s has already been created.\n", FileName);
5 Z1 h3 }6 {0 h. h1 Y5 d3 W) D. [" Q) A3 ~* n* f' L( G
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");9 }: ~7 g) z3 U, `, \, }+ _
WaitForKeyOrReset();
; o, y8 `0 e8 y& l' K , Z) L$ a& y/ i7 a% N$ B5 X# ~2 N
return Status;- x) K# h" Y) [5 V. K9 Y/ s
} |
|