|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*' N3 T: q+ {" t- r. z! }$ g4 T
* myfileio.c
- l$ R: M& h3 W' Z * Apps) X% E; A. X9 a
*/) @. G. H, W. ]: `' b# N
; i% ^1 | v4 ?0 C7 b#include "efi.h". s0 H% Z; h$ F) r
#include "efilib.h"6 v5 T+ m9 } J6 X, e6 k8 {- R3 {
' G: A+ m+ Z8 }) O
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
% ^- d8 V6 w" A$ Q6 @
6 M" f( P" P* d$ O2 I: b% u# T0 N* ~static EFI_STATUS WaitForKeyOrReset(VOID); x$ W( j9 d$ }, c, \
{
. `4 P' Y, N5 r. L5 t% Q/ E. _! ? EFI_STATUS Status;
$ s' ]9 ?( m' P" @; w9 c% Y EFI_INPUT_KEY key;$ L. }9 @! B. Q$ C6 @/ q8 J
UINTN index;
( l: x# E7 W F$ X% l' c " m u: p& s& e7 L* @
for(;;) {
9 P$ o0 n/ z! r4 V' S# B8 r Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
( ?) O$ J) t: H' }# @9 g if (Status == EFI_NOT_READY)( ^9 p: T3 k' u! Z' [
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
/ `7 `0 u. x4 D; q0 n3 i4 V else
. r- V9 D7 Y2 z3 x- X& w break;& a) }! d9 ~) \, @3 P
}* z( l0 f" {9 k9 {) }
if (!EFI_ERROR(Status)) {
$ b6 V4 A, @; Y! J+ @; w. o( s if (key.ScanCode == SCAN_ESC); _4 ^7 Z% w# k* d2 w
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
5 d1 p" ~! G/ u4 T9 ]- Z3 H) i8 O }' {# V6 Y6 Z- G
! s+ {% v& j: }- }1 m return Status;
. K/ C( x! O8 t$ ?3 y4 v) q}
6 J1 o& H" A0 X3 b9 `8 C5 N; r0 a) {' U% b% S" s% N
EFI_STATUS
4 b6 t5 i' x8 ^- h8 e: a/ i# \EFIAPI1 v9 g2 h; {- ?+ U5 ~4 I9 g
MyfileioMain (IN EFI_HANDLE ImageHandle,
: F4 ^' r6 s4 U6 l0 R" f- E% G IN EFI_SYSTEM_TABLE *SystemTable)& C5 p5 L5 q/ W( W* ^3 F
{
6 m4 c( h) `# k F EFI_STATUS Status;6 Z8 g0 t: F$ g0 G6 @
EFI_HANDLE *DestAddr; / c/ X4 g {4 t! s
EFI_LOADED_IMAGE *FileHandle01;( _# V) u' ~+ Z
EFI_DEVICE_PATH *FileHandle02;
% D" ~$ {: F: x* v EFI_FILE_IO_INTERFACE *FileHandle03;5 c! |4 E1 J6 y6 R
EFI_FILE *FileHandle04;
, ^! ]. j6 K0 ` EFI_FILE *FileHandle05;
U( O/ I# O) Z CHAR16 *FileName;* M, { W6 B# X. m+ Q
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;6 v' l" ?7 D$ D4 {; U
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
6 g( T+ {2 y3 D& s, _. u int Bit0, Bit1, Key_Space;
7 Z3 y2 i6 l# l2 ?# X- s3 J# c* Y+ t( ?# j( {+ |
FileName = L"NewFile.txt";
5 `4 a' G4 }8 D- t- @ {& g { Space_Key = L" ";
1 _9 R& T0 G6 A( |0 K; k
$ f: d7 P6 b3 G BufferA = L"ABCD";
8 H" Z& d+ z. t3 U; Q% ^3 g" x- e BufferB = L"EFGH";2 |! W3 s+ L* a
BufferC = L"IJKL";
" ^" u/ l0 X5 \* b" |
' y+ Z9 \3 b+ r% K* q6 o* v Bit0 = 0xff;
. n* b' `+ M8 ^# O Bit1 = 0xfe;8 p& ?' M$ k) l8 j% K0 B
' g/ T3 E% C: F$ N" L, h1 n4 J
Key_Space = 0x0020;! T' H9 o& O/ h( E5 K$ G/ m$ u
! T" ~, x" E! _6 p7 X7 k InitializeLib (ImageHandle, SystemTable); & |2 b/ W/ N& R+ A
; k0 h: y& |& ? DestAddr = AllocatePool (BufferSize);
0 ~0 s4 Z, L! b' C: G8 x! K2 N7 f; g% y
Print (L"Value of Bit0 is %x\n", Bit0);
C$ d' o' Z- t$ m Print (L"Value of Bit1 is %x\n", Bit1); 0 z7 ~" l. F7 ^% v4 b" _
/ r+ ^' m: \. j: m! K, W
k5 W1 W$ O- C. Y7 [) `/ M/ A Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
6 E+ }4 F/ M- x( K( O if (EFI_ERROR(Status)) { F' S! @( W# M+ O- @7 \3 b! ^
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
* ~; D0 w! I- d ^ return EFI_LOAD_ERROR;" _/ P. i/ y6 Y' d5 r
}6 I+ M' ]) ?+ p* S8 e3 f$ R8 M
. L: @, f( D+ D! O
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
8 R* G% ?( y: P# @( h' |+ R- f1 c) ` if (EFI_ERROR(Status)) {
5 v1 b7 F, T3 l3 ~0 U Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
7 `5 I& k& G% W" z6 d- {/ p return EFI_LOAD_ERROR;! q; X6 }) l9 Y3 }
} : V c4 x& H/ ?" ]3 l
1 y! q/ t: U; a0 \2 {: _) |7 c; E9 S8 @ Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
/ Z. ]& m1 |) n/ [* |# W if (EFI_ERROR(Status)) {7 l( i: q5 [4 [6 f4 y0 X
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);) Q, L( F# T2 h+ _8 I7 ^5 b
return EFI_LOAD_ERROR;
, v1 l; O. @! ]$ ?$ E& ? }
* J4 |" m8 ]2 B
: T$ E3 j. G8 K Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);* X9 T( P3 h! g, w; U
if (EFI_ERROR(Status)) {: D" ~- q" o5 k0 l' c S
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
6 u% z& {, S+ J3 B8 D$ S return EFI_LOAD_ERROR;
6 {% A: k8 J) v1 [$ x } ; r5 r Y0 V0 D$ F3 B" o
7 R) I; N- }% N9 A% ^2 A+ W8 `* @ Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20); _8 J X3 {. D( E& k8 p! C
if (EFI_ERROR(Status)) {
% l2 W) D) a* g v Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
7 l% K; P8 Z+ G) P$ u( j1 n return EFI_LOAD_ERROR;
& N. @5 c3 `3 M, m; u }; G( \+ J9 j# F! {( R# h7 u8 S. S j
7 M$ F0 |+ l" w/ ^6 E/ W& Q Status = FileHandle05->SetPosition(FileHandle05, 0); 8 x* G! M: w# ]' p. C, s
if (EFI_ERROR(Status)) {3 i7 s! o9 \9 r' P
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. _* q2 T5 O, o return EFI_SUCCESS;
1 |% M# x. Q5 F4 g7 P+ l9 W$ y }+ t1 R9 [) p0 N6 y
# k. C; j/ n+ T& r! Z5 H5 V8 J4 p `
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
/ L: w- B0 U, ]- i- e Print (L"File Buffersize is %x\n\n", BufferSize);
' q/ L9 L/ i% R% j) `6 w- u7 F$ y if (EFI_ERROR(Status)) {/ p# D, L6 G; Y* d% p% @
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
- r: c0 m2 N& V" K5 g" V& e% C return EFI_SUCCESS;% w! Y, o1 f+ ?
} ' v% ^6 ~+ T+ M9 o! X
; y: p" b8 v4 ~' a! l) r$ y Status = FileHandle05->SetPosition(FileHandle05, 1); . @0 n, a6 D4 z* U: u- V
if (EFI_ERROR(Status)) {8 `' N6 z8 v/ ^5 N2 P6 B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
8 z1 k* I# L8 e! _% c0 B# x return EFI_SUCCESS;
' V5 S- b* a6 ~# _8 \- J }1 E% b# J8 @ ^: K I- q5 A4 m
0 ^6 M$ E- b$ [$ g0 f8 C+ h& X
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);+ n& @" R0 f6 Q* Y
Print (L"File Buffersize is %x\n\n", BufferSize);
9 ~# J, p% N7 q D9 w if (EFI_ERROR(Status)) {1 m/ i* e3 Y4 Y2 s' u
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
, s" Z+ d! a) k M8 C2 C; e return EFI_SUCCESS;
6 g* r) V A4 K! ^ }
1 V: n9 s+ c& B7 \; t8 e5 b- W * n' v% o2 A) `5 u+ n' k& |( _
Status = FileHandle05->SetPosition(FileHandle05, 2);
6 z) G( E: n2 _/ x# d/ S3 t/ N if (EFI_ERROR(Status)) {- S4 I! y# e( o I( }9 g- q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 d# E, v, U( b. R
return EFI_SUCCESS;7 _4 F2 _: a1 ]7 E% [: I) X m
}; T0 C) @' J" Q+ E5 ^( R" H
! n# J# J/ P- }5 ~) o
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
% H5 v6 |+ Z$ _4 f& N; D" V3 v Print (L"File Buffersize is %x\n\n", BufferSize);
I: I! E4 n; j- Y; ? if (EFI_ERROR(Status)) {2 U2 [( z; H. C+ U
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);0 |7 d* [) N: n1 H
return EFI_SUCCESS;
" g8 F( Y/ z5 U1 M } - ?/ T. L4 s& c0 Z
+ T" g I+ i" u, o4 m4 l//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
% ~* `1 F+ ~, @4 \ W3 X) {) r Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);% M: |/ X0 f$ E3 N* _
Status = FileHandle05->SetPosition(FileHandle05, 2); 8 u" ?1 M. F* E: }' y' a0 k+ c/ R
if (EFI_ERROR(Status)) {3 V% y, `/ L5 g, K P) z3 H. Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);, B9 V# f0 W- N6 i! O
return EFI_SUCCESS;
K; w3 q h3 P }
5 h/ k9 z. ~$ t! s# F, x2 N- k, p
4 N6 v. y* s* I) H Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
) ]4 u8 D% f9 R9 b B# J3 ` if (EFI_ERROR(Status)) {
( `7 m, q" B+ M+ G Print (L"Could not read file, FileHandle05 err code is %x\n",Status);/ o5 Z- K& R% Q4 x6 q
return EFI_SUCCESS;9 ] s& H8 z {/ b7 t0 e
}
+ i" c2 A% `& R9 Z! v7 D Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);8 O% s6 M" W1 i4 e7 l9 L! R& h5 v6 K
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
8 V4 o% T# a! _: F
$ a6 W9 M" e9 M- Q Status = FileHandle05->SetPosition(FileHandle05, 10);
+ M4 T x/ n5 i/ T% E# m, L if (EFI_ERROR(Status)) {- B% Q8 z6 ^4 p4 ~
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 q2 I. U3 j3 x5 n) Z- Q
return EFI_SUCCESS;$ X: K# r# p6 X" L X
}
8 K% Z" c% J3 t- F + D6 k: _4 ?8 W2 P2 I! C8 m
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& h5 s! h- }$ @% q4 v* s if (EFI_ERROR(Status)) {% d+ g# ?2 H1 ^ S# p+ g
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! P. T2 |% _2 I) O7 b/ s* w return EFI_SUCCESS;+ r2 \7 a2 y8 e4 S5 y
}
$ a* Z$ o6 D/ M9 g! V) Y4 W3 c% I8 j* h. W. _
Status = FileHandle05->SetPosition(FileHandle05, 12);
9 T' }. n2 Z- ]; d; [6 D if (EFI_ERROR(Status)) {2 j. f( C. {/ Z7 x! b5 m B C9 k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( d! ]8 A6 z! }) y! V return EFI_SUCCESS; N' L" e& R4 `; C
}
+ b0 m# @! r/ Q3 _8 s' x
* f9 Y7 F- e& d0 W7 Q& c/ K Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);$ s% Z' B/ M/ Z* c
if (EFI_ERROR(Status)) {6 R" D4 _. D/ K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 ?1 X* k) o2 T- f
return EFI_SUCCESS;9 L, p- f! Z( \
}
( m% t- H4 s0 ^& H* _' _/ b- z; W4 l1 a8 u$ J8 c5 f
Status = FileHandle05->SetPosition(FileHandle05, 14); 3 W% e7 N& w3 C! ^
if (EFI_ERROR(Status)) {. _+ v! ~' q8 O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);! L' P3 X) V! O' j$ q: y! o' D
return EFI_SUCCESS;% B7 |8 k0 I X+ t0 z7 \3 l( d' `
}$ W o @8 ^! Z# \+ f+ ]! _
$ ~- y6 M8 O! b" U' ]5 `$ }0 c/ c Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
' \- D) F0 l) u if (EFI_ERROR(Status)) {
7 a% T- m5 W- Z* Y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 h% Q8 T% O5 m4 r4 c' K% z( V/ _* [7 Z
return EFI_SUCCESS;
! {# ^. b; n7 G9 \ }
1 I! n% J$ h6 y+ \! S% `. q% L" Z3 {0 P+ i' e' [0 G, e
Status = FileHandle05->SetPosition(FileHandle05, 16); `$ w" p2 C9 q7 `7 `
if (EFI_ERROR(Status)) {. G: `& S; z( H! s# [
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 {2 t" Y4 u( ?7 ]' E* v1 N
return EFI_SUCCESS;* \9 x7 I. c2 D& B5 F( p7 Y" O
}, g* U0 v% D& v* r
! j$ J2 A( \. ~9 B1 v# T# m Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& C. h: `4 l8 v/ u if (EFI_ERROR(Status)) {; g) R4 \& ?2 k( P6 R
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 s" j, y% A U6 C" |9 R return EFI_SUCCESS;
: N* J/ M r' R# z& F; `. E }
' Q/ l: U; i1 r- V& ]) j9 T
% S: Z: ?. g6 K$ K) Q# s//---------------------------------------------------------------------------------------------------------------------------------------------------------------
0 g9 G- D0 }) `4 L2 h, H q
; {, _( ]" U& V% M- h Status = FileHandle05->SetPosition(FileHandle05, 18); 6 y8 G9 ~ o' }7 Z2 Z9 j( E
if (EFI_ERROR(Status)) {1 ^, Q2 j1 H+ G, c! N7 }, Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 W; d1 m3 j$ o8 v$ f return EFI_SUCCESS;
0 Z5 b; M5 i7 ?7 s; I8 }' \* Z }
6 }* A% g5 P$ I. V3 y0 A
1 K# B9 m5 S# Z Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);; `5 W0 a; A& S5 I+ M8 @
if (EFI_ERROR(Status)) {8 y2 M I/ v, Q& }' B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 k% Y1 F7 C1 g, l& f, f return EFI_SUCCESS;
4 d( |% r! { k3 u$ u }
: f R6 ~# e2 m # B# l9 \0 {3 p5 f, C& \6 J
FreePool(DestAddr);
; v( R6 d6 v Q3 D/ H' c
$ u8 F8 p8 S' l' D7 Y1 @; ?* i Status = FileHandle05->Close(FileHandle05);. ?& v2 Z, @: ~
if (EFI_ERROR(Status)) { W0 @' N$ @$ D: j- q
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
) `/ ~ e( a7 p/ f) p, r2 v5 y return EFI_SUCCESS;5 R+ y% C4 A# O0 Y" M9 E& m
}- I) C" c$ T$ x# J$ i& e
y1 ^$ x/ E5 z1 t2 ~ Print(L"File Name = %s has already been created.\n", FileName);! s, D2 [/ h. V( F
9 [ a8 l* B2 K0 v& {. M Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
( Q( a3 K. L9 O, ?: T& r7 C WaitForKeyOrReset();
! `, V7 ^% q8 j6 h0 L
8 b- G/ B& x/ ]7 E' m1 f return Status;3 w+ J& w4 N# l; i1 f
} |
|