|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*( c# e5 I6 G% p+ K/ D+ m
* myfileio.c' R+ r1 x( m1 ?" P1 X
* Apps
$ y f3 K! S* M; ~$ I6 N */
" Z- k" Q3 i0 I7 t
+ y: j% W, W, {* n4 Y9 x0 j/ t#include "efi.h"5 `% k) l1 W' X$ l5 z4 D, u
#include "efilib.h"0 Y6 C5 A* h& n4 r V! @+ v
' x, K9 M% O8 k* P2 M
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
+ D) u+ S6 x3 E3 @+ I4 R
~1 n) ~+ D/ o8 F5 _- P- R4 s" j( v9 H; wstatic EFI_STATUS WaitForKeyOrReset(VOID). z8 m/ i; n8 e; J \2 p
{1 b* ~$ S% g0 q
EFI_STATUS Status;
: ^" x P' u) H9 ?3 r6 k EFI_INPUT_KEY key;$ H6 R7 s; Y5 B ]
UINTN index;0 l. ]6 Q' F" A+ m
8 |9 C% w9 F9 _
for(;;) { j( A6 d3 b/ v6 O6 ^; b [
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);4 W4 Z4 U' u3 ~5 n& l
if (Status == EFI_NOT_READY)8 T! ?) ^/ l8 N1 @
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);! v6 ?6 [3 v) K
else
5 p) ~7 W6 s5 t; n3 O3 `$ w break;5 ]6 U8 C6 {6 S8 Q
}; s; R0 f9 z* P& I1 \/ U
if (!EFI_ERROR(Status)) {* s2 r5 i" p9 z6 o' b
if (key.ScanCode == SCAN_ESC)
; m; x; p5 X9 I" b) [ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);. r' a/ i, ~5 \. o# u
}
& R( N7 R" L+ E% Q & r+ m; A6 v" K( }7 ~- K, N5 N
return Status;
% w% T+ B J6 q3 [ Z! k; i}/ \) G, V8 ?8 ^1 h+ w' \
' Y$ m J; `( X, J; h
EFI_STATUS
6 }" r$ b8 ]/ O& |EFIAPI4 s1 v1 `# X& J' A9 |& v1 P: d4 G
MyfileioMain (IN EFI_HANDLE ImageHandle,
+ Z& X2 |2 l7 C+ R& g IN EFI_SYSTEM_TABLE *SystemTable)* q1 M' n; ?0 J& w/ O0 `" n+ o# ^
{
% w4 `1 v3 @% P6 j0 o8 B& a0 x EFI_STATUS Status;
3 _% _" i( N( H x8 _: u% Z. D EFI_HANDLE *DestAddr;
0 {7 z( R3 r! ~* ^+ u EFI_LOADED_IMAGE *FileHandle01;, k) A, c( j9 r0 x0 h
EFI_DEVICE_PATH *FileHandle02;
- M5 Z; w8 b1 G T! w- C4 P- O EFI_FILE_IO_INTERFACE *FileHandle03;
' @/ }6 _6 E# X0 C" L2 I' r EFI_FILE *FileHandle04;
/ X7 R: e; K" o. ]. j EFI_FILE *FileHandle05;" O- ^9 } b ~+ A7 N" Y1 ]3 U0 f X
CHAR16 *FileName;
9 y- ?9 X& _( g9 O1 c) o" j CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;4 \0 A: _* N1 w4 b6 B
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
4 D8 F! c$ X+ U$ X6 P int Bit0, Bit1, Key_Space; 3 F! W$ ]2 v1 g. O3 p$ I3 j
- ]* Y1 R( l1 P; \2 ^" [
FileName = L"NewFile.txt";
# V/ Q7 u0 q, S& f6 N0 ? Space_Key = L" ";
: K" e5 K8 p0 B6 O' z" G
- n) p' p1 s% Z; f$ K ` BufferA = L"ABCD";! k k: @8 g0 L3 D/ K: A
BufferB = L"EFGH";
0 S# t. k, L, P: F BufferC = L"IJKL";9 ^) u# J) _8 p4 r& s+ L; h
7 ~$ E% g2 K; }+ K' R Bit0 = 0xff;5 r, h. g, q3 P1 r" a9 z
Bit1 = 0xfe;& Y3 U3 ?) p% u! R) ^$ W
6 H+ {0 w! S/ h6 G& Q0 F. I Key_Space = 0x0020;+ Z' n" q" Y) k, v" Q$ m; o* q L
4 m4 K: ^" z/ T4 b+ w! r$ }% Z& Z( g InitializeLib (ImageHandle, SystemTable);
$ S( `% ]" y& \0 j. @$ N
. n2 D6 i# }2 I: F; p9 L( \ DestAddr = AllocatePool (BufferSize); " L4 a, r- K/ j" r
% b3 E- @3 U/ k3 k2 q5 r Print (L"Value of Bit0 is %x\n", Bit0);- ]3 f( R; A1 B( A* s. T) i& Z& @
Print (L"Value of Bit1 is %x\n", Bit1); 5 }/ d3 @2 M: G: Q! {
5 N; \+ \- Q/ G |! G
! ]# a7 w! K4 C' T; P Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);9 f- v$ V2 R l1 p/ [: T1 n
if (EFI_ERROR(Status)) {0 q1 f6 O* L% `" f. E9 e! i
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
3 m! L* ^ |8 l0 h. G) p4 L2 h5 Q# o return EFI_LOAD_ERROR;
9 D% q0 ^0 m* f' h S }" R/ S7 ?9 o: A; y+ G Y* J
I6 S/ H* N3 k- X
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);; Z" S) M8 M2 z, `1 f8 D( P
if (EFI_ERROR(Status)) {
/ K' [3 q8 ^! H$ ]) r Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
+ k0 {. S5 v! t$ ]) a2 A return EFI_LOAD_ERROR; W5 K0 W* o5 k4 z# ~
}
1 N3 Y& o* @ ?5 p/ a; a
$ o7 x: Y: Y# Q9 S1 e; ~ Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
( D6 U# M8 h Y8 u9 O3 | if (EFI_ERROR(Status)) {/ f0 |! w! \, J- \% v
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);% b! x, x; i& d# L
return EFI_LOAD_ERROR;" ?+ y8 k' @! v4 i' {* a9 A
}1 x% W J4 @% V/ E$ |, Q4 B. l
8 p$ y6 g a( m6 R1 k5 Z8 C, x
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
- q! d4 n& q1 Z% u if (EFI_ERROR(Status)) {5 X+ s+ F7 n) |4 [
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);- t/ v7 p4 @# d
return EFI_LOAD_ERROR;2 S3 C2 k% Y8 K0 T: n- @. H7 w8 y* C
} ) y3 U6 { n3 x
5 L3 u8 R$ u* t# M
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
* Y- ~, @4 C: o M" |& c l: T if (EFI_ERROR(Status)) {
+ W L% ^2 H" G, W7 s! P Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
5 X2 t1 U) q/ d- W% L; A9 z return EFI_LOAD_ERROR;" L$ F+ }) b4 g# K% f7 w- v
}- c% j# Q1 u& @" x$ p Y0 ]1 }
$ v. L. n6 J0 ]% x% O; b9 d4 k, x- w
Status = FileHandle05->SetPosition(FileHandle05, 0);
/ ]! E: b/ X1 r& Z' H if (EFI_ERROR(Status)) {
5 f r ?' b% r) G Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% D6 H$ R0 A9 [$ ?$ \! j
return EFI_SUCCESS;
) i! R2 K6 j8 E/ ] }4 u; d B4 @3 s7 @# N8 ]
* {4 A) }9 c' [& y' h# T
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);5 Y5 u; ~6 |* i* Y
Print (L"File Buffersize is %x\n\n", BufferSize);
+ ~' p* q; a) x8 l0 f( S1 T if (EFI_ERROR(Status)) {- J& o o1 C* h7 f7 Q
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);7 x: m, e/ e2 d2 g
return EFI_SUCCESS;
$ J3 P& Y! l7 ^- O) \0 \1 G# w* b& y! ` } ( Q" N7 x8 \6 }& m" v8 n
# _9 C0 D: Z& x% D! w4 N# d3 ^ Status = FileHandle05->SetPosition(FileHandle05, 1);
0 a5 k) m: f2 P0 p if (EFI_ERROR(Status)) {
; B- k' W* b k! ~/ z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; m [" x! B: \0 Z2 r
return EFI_SUCCESS;) H3 D( _* q2 h( e/ K
}
. z, q/ L- J3 _, s
0 K& U6 M* F. l- u1 D Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);' f/ h M. h3 ~, k9 g0 A: o
Print (L"File Buffersize is %x\n\n", BufferSize);, ~+ ]$ Y8 F" b3 `4 P
if (EFI_ERROR(Status)) {5 t; \' {: z H# q) g% o
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
! z- F9 X9 ?) U$ I% m return EFI_SUCCESS;# }" `) {% v5 P
}
8 q: x# q; {) e2 `1 I" ^/ L
9 }8 X& b/ r% h) j' a# [' W, a Status = FileHandle05->SetPosition(FileHandle05, 2);
% h" F. T; O) y4 Y if (EFI_ERROR(Status)) {- P- V( \ a+ s0 w+ X0 h
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
- M( @, Y/ f6 X# B& J& l& [ return EFI_SUCCESS;
" {+ B; ~: L0 z4 v+ P! i; K }
3 @! f e/ s$ C: q- w
3 p- l, U" E& z1 h: v4 | Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
" A/ X( U) l3 ]% r. ~7 B1 p Print (L"File Buffersize is %x\n\n", BufferSize);
% s# i! n# n3 t if (EFI_ERROR(Status)) {
6 @8 R( e# ]' f Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
$ D7 z5 k. @/ ` return EFI_SUCCESS;
/ F( [( P3 z. u9 S& q4 h/ _ } ( p, N% L. A: }0 W/ e/ e
2 H3 D; l5 C% n1 B6 x4 y2 N
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
$ o/ C" t, P. F" g4 J Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);; w: ~ r- X0 V
Status = FileHandle05->SetPosition(FileHandle05, 2); ! ?$ U' F$ i3 |
if (EFI_ERROR(Status)) {
2 u( m4 T* ^' B3 T4 l Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# p. M' w5 R! r& d( W( U
return EFI_SUCCESS;! X2 Y4 B# k* E! C! c
}5 y6 S2 Y4 ` X; L
# ^4 }7 [# e. c( |9 [: r% ~( h Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);+ D M, J$ \, y M9 @
if (EFI_ERROR(Status)) {
& t" j4 H( J; L Print (L"Could not read file, FileHandle05 err code is %x\n",Status);( k) v6 g; T+ i' R+ \& ~
return EFI_SUCCESS;
0 f1 g" f5 I, O" t9 W0 k } - e5 S9 R p7 g8 h
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);' Q$ {. g2 m2 G- K; r2 i& e
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<, d4 n; `7 v' i+ v
0 W6 y( [( C8 }: ?. ~6 @ j) z0 @
Status = FileHandle05->SetPosition(FileHandle05, 10);
6 Z6 f, r! J% P A/ f8 _ if (EFI_ERROR(Status)) {& f( T# u6 }7 d2 n! D% A, W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 H' c9 `" J6 }1 [- e* P5 z2 V return EFI_SUCCESS;
8 M6 @, I( T$ A6 \) V2 a }
% B2 B) J6 L* P
+ g; J8 R- k; p9 a2 a4 |$ a9 L Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& b2 `4 g4 r- t+ Q2 s if (EFI_ERROR(Status)) {- h' l. `% L) m
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 l: ?4 `, t# u3 G ~+ G+ S return EFI_SUCCESS;
1 O) T' I: n" ?( x% Y+ h3 D9 L- ~0 S }4 h$ G9 c' b2 L K& Q- [1 ^
: i( F+ P: t+ |: s; p
Status = FileHandle05->SetPosition(FileHandle05, 12); " U4 l2 z' Z$ d. p, E
if (EFI_ERROR(Status)) {
+ \ g/ j' C1 H1 g. V v5 N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; P! y- ~% }9 K! `) e5 y P return EFI_SUCCESS;
& ~- _/ q% e" w8 ^+ X }* z5 u9 \' N* }
7 u" Q& \; k+ k1 L9 z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 ~+ r+ p( J v) ]* T" {9 G+ X if (EFI_ERROR(Status)) {
$ B: s' j2 q3 s5 Y/ K) l Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% U4 I3 a) O! `. }: I7 Q
return EFI_SUCCESS;
7 f2 w7 V N! H" R2 r7 F }7 I8 j) w' n6 Q7 e3 B) w) E
* q+ z. D( T( M6 @! U' M T+ s; i Status = FileHandle05->SetPosition(FileHandle05, 14);
7 P) P0 T+ h7 c+ z if (EFI_ERROR(Status)) {
[0 _* s* a; b2 O# a5 W Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 b3 U2 L6 m- E/ }( d3 t
return EFI_SUCCESS;
. o9 p+ a4 {+ q( ^4 l1 ` }( W9 Y$ ^% Z4 i4 k# I1 t/ W7 g
8 v# F" N+ K8 k% N( W
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. d v# @3 u4 q7 e4 n if (EFI_ERROR(Status)) {: u+ r7 _" `$ m! K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ O8 T1 n$ E( {( k# e* l7 L0 ~ return EFI_SUCCESS;3 J# a, [- F! l3 Q' {
}0 P6 j- [, B) }
" E5 t. Z) Y% ]/ N5 ?: b% O
Status = FileHandle05->SetPosition(FileHandle05, 16); ; }! E ?3 D/ |9 z4 J. R
if (EFI_ERROR(Status)) {
4 F# E, X0 s5 L% V; }. ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& G1 {6 u5 z+ f3 x return EFI_SUCCESS;
9 N2 l0 I! B( a% s; P; H1 w }
/ ]( W, {# t6 Z. I6 [4 |
: A/ _% t8 ?- Z4 W5 x Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ L6 G, G m8 Y
if (EFI_ERROR(Status)) {
, t5 P1 g# t, I$ K3 F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);% J" F. m8 R2 n: E# T. s8 f3 J
return EFI_SUCCESS;9 r- q1 c3 f1 r$ x/ a7 B2 x, G% o1 ^
} ( O4 ^- k4 m1 _* C6 w. M* S
: X- |0 @4 c: h//---------------------------------------------------------------------------------------------------------------------------------------------------------------
# K# T& y/ l2 D! v: h
& m7 B; }( \. D: u i0 l Status = FileHandle05->SetPosition(FileHandle05, 18);
1 a6 ^$ \( ?3 Y7 H1 P if (EFI_ERROR(Status)) {
6 Q4 s% e( L# |" p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) _: v2 [0 ~; b/ |
return EFI_SUCCESS;
# F$ h0 r7 m3 h+ } }. m( Q- E) X: p2 _3 C7 L9 z: |
3 k2 Y: ~8 m- \" h% H
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);6 s! S* x9 R% }
if (EFI_ERROR(Status)) {5 U) P, Y& b2 H4 K5 q* [
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' y; c( O4 c2 |/ p+ j* b
return EFI_SUCCESS;
! r% [6 X& d; j+ ^! M. Z5 X; b. M }
5 B/ L% X% G- W) {! \* |( ~
" j* E8 E' V9 M! N FreePool(DestAddr);
# l! b2 f- P; n9 W$ ^
' ?; m2 j+ X& f8 @$ `& r" w Status = FileHandle05->Close(FileHandle05);7 W! }) g2 ?" e4 S
if (EFI_ERROR(Status)) {, w, x- y5 }+ }) v' q4 T
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
- ]* Z- G0 k: y+ I, L return EFI_SUCCESS;
8 t4 x* ?4 Z8 l" t }: o& b g1 x& @1 d' \7 z
I& k" u0 t# ]6 l+ i1 B9 [
Print(L"File Name = %s has already been created.\n", FileName);* K. U' q, z/ Q' Q
+ y2 S6 T1 F o. z Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");- w# A1 j* i$ ?
WaitForKeyOrReset();
- M6 n' L( O+ Y3 j; e4 O; T N5 Z , o3 e" C9 H1 Y# a
return Status;
% C3 p* R7 o/ d* c W+ D7 F( n+ k} |
|