|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
1 k" y7 }8 o6 H( m7 ]% ` * myfileio.c
6 P! |! f; }* M' k, r; q5 A( u * Apps4 L% p/ O# Q) N' j5 ?
*/
9 E% O* V7 |0 U# g$ W0 r
0 u8 c5 u( K$ {7 A, ^#include "efi.h"
/ `$ p1 {2 s, b% z! D/ O# @' S) Y#include "efilib.h", P4 @: m$ p5 U% |' R0 P
/ f* g9 G: _! I. x
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE* L: x+ u7 ~6 i1 j1 A) Z* j. f
! c; I* v7 l! M8 I$ s Hstatic EFI_STATUS WaitForKeyOrReset(VOID)
+ U, t7 v- N; L @) N{
$ ?9 y9 f6 W5 J' w+ @* s. ^ EFI_STATUS Status;
; ^" }% e! B g( v% b EFI_INPUT_KEY key;
" u b( E( s$ I7 c3 n UINTN index;2 m+ D& F n+ G0 r( i5 {! V
( a8 [, O' @. k3 W, |+ D for(;;) {
: @3 m8 ^, E4 e9 V! J. Q( {3 w) m Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
* c( X! O3 y: m8 O$ M if (Status == EFI_NOT_READY)9 Z5 n# H" g* |. O* T1 @% L; v
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
4 a, i4 f+ A) k! F! @, z( t else8 S( P- j# a4 S' g
break;
* a! m2 R7 d5 ^* M }6 S. T, i4 @/ ]1 { F. V
if (!EFI_ERROR(Status)) {
* _9 L8 Y) r' H) Y) x if (key.ScanCode == SCAN_ESC)7 v$ R3 w. D. f) S# E
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);/ L& L7 `- C' W& w& O! G/ t' |
}+ D0 B- U* g L8 j: u2 S
5 L9 I% F8 x! ~4 B! I$ `# D
return Status;
8 b8 ]& N: R; Q5 S V R( G. p}
9 c+ `3 {$ l: K
0 f8 i. y1 e! K- ~% GEFI_STATUS5 E/ J; @% C1 @5 i" f
EFIAPI
& O* ] T3 x7 xMyfileioMain (IN EFI_HANDLE ImageHandle,$ _4 ]( ^- R* ~. K" j% k
IN EFI_SYSTEM_TABLE *SystemTable)! X$ T- L; Y- i2 A3 |+ @3 Z
{
: O% j8 e1 ~( t7 |' S EFI_STATUS Status;
3 A4 z4 }: p1 f8 u% h EFI_HANDLE *DestAddr;
; N3 ~1 s# C" j! ~0 e( p( ^# e EFI_LOADED_IMAGE *FileHandle01;0 S# \8 [1 f# ^- q
EFI_DEVICE_PATH *FileHandle02;
9 Q# N0 w1 Y# l. k4 T EFI_FILE_IO_INTERFACE *FileHandle03;# }: w" D6 w f: L
EFI_FILE *FileHandle04;
+ x4 f }, u+ v6 q0 e/ z" D EFI_FILE *FileHandle05;
9 {2 f, m# m$ h& _% k+ E/ a5 k) U' C6 ` CHAR16 *FileName;% H$ o8 I) ^2 J R( }
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;) T& F' Z I1 a7 y2 U5 D
UINTN BufferSize = 8, BufferSizeKeySpace = 2;4 ^; \: O+ h- e. v5 p7 w
int Bit0, Bit1, Key_Space; 2 V1 a, W! ~$ P& s1 z& s
0 N3 e' D& h& C FileName = L"NewFile.txt";+ D" M, T: g c* q* j' {
Space_Key = L" ";6 }9 ~% }! F7 q
d4 x2 P- f$ w+ v) Q* @% k BufferA = L"ABCD";
. [9 o5 J6 m: A) l BufferB = L"EFGH";3 _' _9 T2 @* m) H# G3 [7 l* G
BufferC = L"IJKL";
4 K: U+ l% Q2 l0 w! L9 ]8 ^+ A
" a t0 \0 B% W Bit0 = 0xff;& v5 L' m/ a2 {2 d# X9 `8 r
Bit1 = 0xfe;
, V+ X# r$ y, q9 N/ d
4 R6 K: j& @+ x) c Key_Space = 0x0020;
. @" l' u/ q& G! O0 i ; z% ~. z: m2 a- q$ p9 Q# E2 N& q' @
InitializeLib (ImageHandle, SystemTable);
1 k/ E+ _& L6 h$ }$ y& R- d, x6 w: X5 `& F
DestAddr = AllocatePool (BufferSize); ; s2 m3 ?7 Z' y6 k
4 i3 z! X! v. ^ f0 ]" w
Print (L"Value of Bit0 is %x\n", Bit0);
v1 I' f) T1 i Print (L"Value of Bit1 is %x\n", Bit1);
6 k* h2 Y6 j2 D, U& v+ `
( }3 p+ D% `5 G; o R9 h G4 B) U
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);' o4 h$ y, w& H
if (EFI_ERROR(Status)) {# s) B8 W" _ ^- h
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
4 e. u" A/ k3 s" e! c return EFI_LOAD_ERROR;6 i! _& Z3 Z: m+ b9 T
}
+ [, ^. J8 A/ S: E2 D5 r( u$ A n# j' }9 e+ V# P4 X! X
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);/ o, F8 c- @% T. L& Y' @7 H$ H/ m
if (EFI_ERROR(Status)) {7 b9 y, z5 ] E" o& z, F
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
; ^) g4 h* |1 M0 U0 s; @ return EFI_LOAD_ERROR;
7 n8 N# X& M4 S' v0 `3 O } , z; ~3 t) i& w. ^$ X4 u0 o# L% A
& f3 Y3 o% I7 e2 E3 x& [, C' b6 F Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
Z( f ?- h& y5 `, _ if (EFI_ERROR(Status)) {+ u. d4 b/ z4 ^0 ?- ~
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);( l6 f8 M3 s L5 u" f" _9 r$ ~" |" w
return EFI_LOAD_ERROR;3 F4 e- _2 H2 Y2 v3 P
}
7 _" b; g7 b0 | b' ~9 K/ ]$ [! v
9 T) T1 m0 r m8 {# M. F$ F Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
* G2 g4 e8 ^- b/ ]9 u if (EFI_ERROR(Status)) {& p" v6 m7 `6 ]
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);5 F9 d* w( |$ ^) {2 S
return EFI_LOAD_ERROR;2 p: D5 }8 L% W1 O b, f- |
}
. s9 c ]( x: ^4 C3 i* \" b: } * q0 G7 Z6 m' @$ ^
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);6 _; ^5 d7 @8 P2 J( x
if (EFI_ERROR(Status)) {
4 C; L1 e% Q# g) f Print (L"Could not open file, FileHandle05 err code is %x\n",Status);! f' @3 p' g4 K$ U5 p
return EFI_LOAD_ERROR;
: c& R- ~& J+ W( v8 v }
6 D- G: [# K) W, g3 t5 m% k" t8 ^ 6 F7 o. \7 M3 X# Q ?
Status = FileHandle05->SetPosition(FileHandle05, 0);
0 J# Y6 x" G6 {" G( n if (EFI_ERROR(Status)) {
+ S5 i6 T& r' r5 m% }- | q5 P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 p+ Y( m3 J9 D7 C* B& p return EFI_SUCCESS;! u, H4 Y( C( j' t/ Y
}' W. c" x0 \" @) O. ]& n
. q" {; Y; T' L$ Z1 l Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);$ z% L, o2 ~+ r( N+ E' L
Print (L"File Buffersize is %x\n\n", BufferSize); @7 I2 a. L9 Z! Q, z8 M2 E- F
if (EFI_ERROR(Status)) {
: f2 ]4 ]/ [+ C Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, w+ E8 k4 M* f8 t/ \( \4 G
return EFI_SUCCESS;, l, t! |1 Z+ I
}
0 Q8 t1 p; I/ a9 H3 i! u
! O/ Z) [8 @7 F } Status = FileHandle05->SetPosition(FileHandle05, 1);
1 e" R6 m) `# X) O% ?, v C k if (EFI_ERROR(Status)) {" \% O; W: { Y# H& i! v1 k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( F/ x% n( g# O/ O; U
return EFI_SUCCESS;
1 h, p* a. ]* q5 G6 ? }
/ @9 Q# l( h8 d V * e# }+ M- [/ T9 Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
! |; c" |; \+ R& f Print (L"File Buffersize is %x\n\n", BufferSize);" Z7 H. t Z. L$ ?& m8 E
if (EFI_ERROR(Status)) {
! v8 [. O- q3 ~" s) H$ { Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);" E3 B( W( u% y- ]& a* f+ Q- k- o5 f
return EFI_SUCCESS;
' E+ w. H' @6 w8 t6 z } 0 [" U, |% G4 x4 @ B
* `0 D" ^ L' t& i2 o# g Status = FileHandle05->SetPosition(FileHandle05, 2);
3 w' S- P( [1 Q J+ p if (EFI_ERROR(Status)) {
! R- r+ e% U5 G Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ M* H1 m8 M$ Z5 t' @ return EFI_SUCCESS;2 ~ c& z$ [* m w# H
} N3 a7 x7 v x1 A6 R
% C& ]9 z L; @! n8 S" a. q9 s Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);0 @! D: f0 h1 S( y4 F9 Z
Print (L"File Buffersize is %x\n\n", BufferSize);3 W0 j3 e( C9 `1 c& h! b0 e* y
if (EFI_ERROR(Status)) {/ r/ E! c5 e2 X# d
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 O$ |$ t) H1 o1 u: T return EFI_SUCCESS;
0 x5 ?' X, q8 _- L6 e' E } " j4 d: \; l$ p/ K
8 j/ C/ q$ @) {1 |6 S1 d, v/ B//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
) t4 I. [: U. f& b( u! } Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);# T0 F+ I3 \4 z, b( V! D% [
Status = FileHandle05->SetPosition(FileHandle05, 2);
, K3 j5 z& F, z0 d" @5 z if (EFI_ERROR(Status)) {
8 B6 |8 @/ ~6 n, ~* y6 j Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 C; }$ b+ }0 B! ]( _4 A ]5 }
return EFI_SUCCESS;2 G Y. u- b- c2 I
}
3 B! ~8 A: n( K$ ]7 g: k7 ?' i( G% [% v3 z. T
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);/ Q2 g% X- G1 _( _: N
if (EFI_ERROR(Status)) {! G% r, y( k( c
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);8 e2 i" n: g8 C4 S8 Z- Z0 ^
return EFI_SUCCESS;
7 i, K6 |% e9 A# n } - J2 K; s1 u; N" |9 b# j/ V
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
* u, W1 i3 ?2 A9 r5 T7 P8 @! x6 g8 o//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<$ d; X" D- I2 d/ M
* J* L- j# w. |" Z# |6 x$ q
Status = FileHandle05->SetPosition(FileHandle05, 10);
3 y4 K$ ~! t+ h& [2 M if (EFI_ERROR(Status)) {# C/ {4 n% a4 L
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ M$ V6 L$ |8 A% f# p0 L B return EFI_SUCCESS;9 S1 P- i% y- O8 b, q6 @
}
/ S1 l, a" f2 Z
9 F0 F. s9 O8 I' |# D* ~) f1 ~" M Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);8 S/ L% i& K% l; ~9 o
if (EFI_ERROR(Status)) {7 q* S2 p: K1 P, q7 D
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: r- j& _7 i( o% s; p3 @) R return EFI_SUCCESS;
3 @+ n$ U) w# f( I, H }2 X8 E: R% a2 y$ x, ^2 j
. V% \" c6 Y2 c) m( v9 M8 a
Status = FileHandle05->SetPosition(FileHandle05, 12); 2 f# L! M% H, U6 p0 A
if (EFI_ERROR(Status)) {
3 T2 I+ E" `0 g' N( h" Z2 P9 b( q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( \; I) s1 z! }, @3 z return EFI_SUCCESS;! A, Q- R2 N n1 A4 i; M4 y
}
# A) F% Z9 T( z
6 g. R' I' h/ `& y7 t% G Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 m; p; y2 {/ C2 d. j/ G if (EFI_ERROR(Status)) {
8 n* e( F! ? ]7 `9 H# @ ]& i Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: S% z A% L7 } Q
return EFI_SUCCESS;
' W( N4 q4 G7 z* y& s. L }
% M3 e1 D5 q( V# f6 \) M: L* u0 A$ v0 X C$ s; a4 F& y( h: t+ k
Status = FileHandle05->SetPosition(FileHandle05, 14); . J/ S! u3 b- w8 f! P- V% x% X$ _
if (EFI_ERROR(Status)) {" c9 o) K' P2 g* K
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 w+ R* _; v/ V, Q& Q; L' g return EFI_SUCCESS;
7 B7 t& _4 ], @/ s/ J0 O* ~ }
% |" Z( \" N& ~
- N( I) o& [3 | Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);, R# A. k7 v& U, ?
if (EFI_ERROR(Status)) {
" V7 {) M2 l9 @* y; k3 j Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, g& x9 g7 l2 J' q0 _- U return EFI_SUCCESS;$ `. N8 P9 v# g: n: e# M0 k
}
; z& J4 T5 r, |6 w" G4 [: `3 `0 L! q" s ^' s k) G
Status = FileHandle05->SetPosition(FileHandle05, 16); . a2 z2 K, l3 Q
if (EFI_ERROR(Status)) {
6 a& C5 m" {& i Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! G9 E, a, N4 m: b return EFI_SUCCESS;
8 Q$ R4 q4 Q- X, l* L }
$ e# A* K( l' k! R) z+ D , _9 F* {% O3 t& H# |
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);# q* U9 @* z( g6 r. [* z
if (EFI_ERROR(Status)) {
$ J& _$ s) [+ `$ {) [8 W Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
7 I# `$ W* ]& ^ return EFI_SUCCESS;
' g0 L- a7 R. r* a* w( {9 L } & q& _- V) e$ H" U: e
+ I0 x* d0 }0 V0 d3 b4 e3 i
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
9 M- h c4 `4 f# e; q# S1 u7 A+ V! d
Status = FileHandle05->SetPosition(FileHandle05, 18); + b6 |9 {+ g1 H. ~
if (EFI_ERROR(Status)) {5 X% Q! l/ k6 i
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 Z, Q8 w$ q- W! E0 [ R$ \ return EFI_SUCCESS;
- a: z6 [' i, l# B }
6 o! U! b9 j- n * N( ?: n; l& n' [+ `
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);9 J. u6 h7 D7 c* c, h
if (EFI_ERROR(Status)) {5 `2 a5 M* R( l* r) C
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, L$ F- a% @' i7 f9 R1 z' G' Z1 Q( L return EFI_SUCCESS;
# t% T2 O- r' r/ h% M% b. I }
" {8 d( y% |' f7 T
' I5 l( t5 @8 T& D FreePool(DestAddr);
4 }/ `% G( B) W: D6 i
' w9 m3 g% S' T+ M Status = FileHandle05->Close(FileHandle05);8 X0 Z5 g/ t) ^ x$ t; T
if (EFI_ERROR(Status)) {2 Y$ D% a2 i g" T
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
9 {$ S5 s% j3 k( L) e. i) c return EFI_SUCCESS;# F, {1 H9 E! y) T2 w6 `
}
5 V' s( l) h: P' l: x
/ R* Q- W: Z: b- J Print(L"File Name = %s has already been created.\n", FileName);$ L$ O- P: X3 j! W6 F
; h ?$ y0 m T" i8 @ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");* p9 |: h# `9 C0 o' {
WaitForKeyOrReset();6 u# M7 y8 h4 C
$ M0 j) u8 l/ d) w" u return Status;
D9 G9 @5 q' G} |
|