|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
9 \6 F# d" _+ Y: u. }9 _; } * myfileio.c
. `& W8 j* `5 G% c! j! R2 t7 o7 x1 \ * Apps
- F" \4 ^' @7 T3 ~% M3 s. @* X */
9 m; t/ t; d/ ?5 R' k2 I# O/ ~! z3 A) ? x
#include "efi.h"* G' |4 h5 Q! X) K; y$ `5 [
#include "efilib.h"
& d" K$ [5 J( W+ Y& j3 s
" m7 R5 c, E9 X; j* J5 Y#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
: o+ r2 Y7 H% D! W/ B6 R2 @* d
# g- g! \5 V9 vstatic EFI_STATUS WaitForKeyOrReset(VOID)) U. |1 B. u8 l
{
/ y0 z* U) a; |6 {9 Q* C6 ^- ~3 b EFI_STATUS Status;2 |* m. D6 O( r1 M% j
EFI_INPUT_KEY key;* d/ C4 ^3 w& E! e% Y+ m/ t& g: ?
UINTN index;
% n/ X( j" ]# \. r+ l% q : H T& q' G: t# G- a
for(;;) {+ f9 \( w: S4 u7 X; k. H
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
8 a( O$ `# @& ^6 G7 a; u if (Status == EFI_NOT_READY)
8 ~6 `( |5 f# g3 G+ c; ] BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);* [0 L. U1 n( U5 C$ o
else
v& g" o6 a0 s- B% |1 u( s break;
4 _5 P4 @. y5 K3 m$ j6 c* Z8 n8 c }
8 i2 C G- u: M' b) Z if (!EFI_ERROR(Status)) {# y8 [2 a% f9 T# I. I
if (key.ScanCode == SCAN_ESC)( c7 C0 d( K* r, p5 M$ N; r
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
( @% \; N( e0 Y+ `4 }: q& X }! o! V5 K/ h* L/ e+ j* b+ h( E
; l. p2 A1 A% b B: h return Status;
8 Q, J8 K4 F8 P" t0 i}4 ?( ^' [* a0 }5 p5 Y8 H
. p% Z6 X8 h% [" H; g: K% s& U6 K
EFI_STATUS6 j) K; q1 s2 u" p
EFIAPI+ e! Q) |* u) s& [: n4 C# ^/ b
MyfileioMain (IN EFI_HANDLE ImageHandle,
+ g' [" j( J& n IN EFI_SYSTEM_TABLE *SystemTable)
4 V0 x8 x, J1 S{# b/ \( `' X; H, h; z
EFI_STATUS Status;" [, k: w9 Z3 p; I- g `6 K" y
EFI_HANDLE *DestAddr; ; B' p2 c, T8 w& L P" P2 N5 r$ R. r- }
EFI_LOADED_IMAGE *FileHandle01;; j: E6 v, F# h2 F- B& }7 _. Z: }
EFI_DEVICE_PATH *FileHandle02;
7 |# Z3 m! t5 Z7 H: k/ s EFI_FILE_IO_INTERFACE *FileHandle03;8 F! Q/ S; `* n( Y I' m' x. O
EFI_FILE *FileHandle04;5 c* I& `( c* {
EFI_FILE *FileHandle05;
& e* O2 A# L" q4 M" [, R0 h+ Y6 E8 } CHAR16 *FileName;
9 x* Q% r1 w( K; B4 R( p2 }9 {/ h/ S CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
% `6 @2 R# E# m6 W UINTN BufferSize = 8, BufferSizeKeySpace = 2;* e8 y6 o" w- s
int Bit0, Bit1, Key_Space; 5 U9 |! \+ s3 c4 _5 C: Q4 t R9 x) ]
: Q+ |1 u7 G6 t: I& P8 N FileName = L"NewFile.txt";
4 n# q; b+ g4 r# F# @4 Q Space_Key = L" ";
: N+ V. _8 E2 H: d0 x9 G4 w4 g1 d 2 L& F+ _+ `9 g7 k0 w% a0 S5 u% E
BufferA = L"ABCD";4 O3 B" J- r* J- l
BufferB = L"EFGH";% O& G2 G( c. [9 o
BufferC = L"IJKL";
( J$ A/ Z% F. x5 p
5 Q' d# M* R! g8 o Bit0 = 0xff;
4 o: C3 [9 h( z Bit1 = 0xfe;
& D5 J+ X; J% y# t 7 K. L) Q( F2 N6 Y5 y
Key_Space = 0x0020;
8 o, B3 @" |& r/ ]5 S* m2 R 3 p. q+ x6 D' i, X6 g4 L v5 X
InitializeLib (ImageHandle, SystemTable);
* y: ?# Q$ L1 }0 b
1 j. G8 D- J$ ^+ _# r: y DestAddr = AllocatePool (BufferSize); : c. c% A7 [: Q! B* m: Y, i+ {
; e6 C: R& f' ^ Print (L"Value of Bit0 is %x\n", Bit0);# ?9 Z m) s5 r; M6 R7 x7 p: D
Print (L"Value of Bit1 is %x\n", Bit1);
1 i" ?* X" c4 `* e |/ l) Y, v E; b6 c- b
% V( B) [, @. _4 V
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);$ u' m' s" p! B" V* z k
if (EFI_ERROR(Status)) { b& R, G' d% l/ G
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
0 h9 U7 `" Z3 ^# d: k* [5 b: s return EFI_LOAD_ERROR;
2 s9 I! E( r: a }
8 ~5 H9 R8 D& Z& k* ?: Y6 P
8 T9 n: t2 s+ C8 ^6 [ Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
( u- H- J" x: @9 x4 Q if (EFI_ERROR(Status)) {- w7 o) v) g! l0 {8 T# y( g
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
( |% Y# K m7 }- U) j( U return EFI_LOAD_ERROR;
5 t1 k3 _8 E5 A* Q } 1 u5 ]+ i) y* f# B7 l
; w/ H6 ]$ H0 P! c. e
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
7 y0 p( Q% {' @ if (EFI_ERROR(Status)) {
4 n) T5 M: U4 _2 A Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
3 \- f$ J5 S# k2 z. A return EFI_LOAD_ERROR;
( u! T! E+ X+ z8 m/ M; B }8 Y( V# A/ c) i
$ W+ D; n. F# Q. J9 b( m
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);2 K6 w# M9 Q" P) P
if (EFI_ERROR(Status)) {
" m3 N. V( l( J" I Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);/ G2 V: C& Z/ r0 L Y, E
return EFI_LOAD_ERROR;3 C) a# C7 _0 p1 _$ m2 L
}
( l) _" {0 o6 H& m8 r) Z6 t
5 ]# j+ q8 p% X5 a4 Z6 ~. q3 G Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
4 N& _+ A5 H. ? if (EFI_ERROR(Status)) {/ i$ u- F8 e" b5 X( R C7 O/ i/ O
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);5 \! e" j& t2 {3 U, j, S
return EFI_LOAD_ERROR;
- B# \- o/ Q" W: p7 t b$ ^* b }
+ F. s% @& i1 Y* Z& L9 V4 w, D( |3 A 2 d( k1 y. ]6 Z3 b, v4 B
Status = FileHandle05->SetPosition(FileHandle05, 0); ; W# J8 k; C7 p! q- o, i- S
if (EFI_ERROR(Status)) {& k; u) B0 \7 |- u. F: E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: N: M6 {# l- m- m( A1 M return EFI_SUCCESS;' w& u2 W- A+ Y$ e9 h
}8 q" e! b9 C9 N+ Y8 S) T
2 ~' B- |6 J& t$ F' ` Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
2 p5 A/ F* Y8 k( q% \ Print (L"File Buffersize is %x\n\n", BufferSize);
2 n& z+ Y6 a5 V$ `' I& { if (EFI_ERROR(Status)) {
7 M# S- S" l# _0 w! z Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);0 T* l( V* ?/ C7 ^2 z
return EFI_SUCCESS;+ u2 l# Q Z3 _5 a
} ! H: w5 \* V: g5 o+ I, {) s' }% I
9 Z- Y9 y& u" a' k
Status = FileHandle05->SetPosition(FileHandle05, 1);
( I U% v8 x, c1 Z) A5 [ m5 g% Q5 S if (EFI_ERROR(Status)) {3 B) S7 |+ \" F* J9 _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 G8 J# ~. x4 Z2 S
return EFI_SUCCESS;
2 ]+ `9 Z! U( i4 A }
; U1 w, X( C4 w 4 s: n4 H6 s" c* H
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
* f6 C! y! o6 k2 P Print (L"File Buffersize is %x\n\n", BufferSize);- Z# ~7 z5 h, ^5 s
if (EFI_ERROR(Status)) {0 a& a0 C! q! T- m7 a
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);* s/ O0 L3 G- u* ^/ o
return EFI_SUCCESS;
7 a7 }4 k7 T ]; k$ {' D& c } 4 d5 ?; a! R- w1 _( Y
; R _8 S6 W+ _, Z Status = FileHandle05->SetPosition(FileHandle05, 2);
" {* M) _7 j! x% L if (EFI_ERROR(Status)) {
, N6 C* z8 i# u' k( o p' c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 T& [) q2 G6 }8 X+ {0 J1 _ return EFI_SUCCESS;
( d. L/ J9 a$ p6 e# e! h/ S- M }/ V; v" K1 `: i! ^; O& _8 r
* D D+ m, j, k( c
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
+ {5 a3 A& G" s K* T0 ? Print (L"File Buffersize is %x\n\n", BufferSize);4 ^4 T7 v9 t/ |( i7 t( v: r
if (EFI_ERROR(Status)) {
1 w {) T1 V* M0 {! d Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
3 p1 U1 X& M: w9 s9 C) c return EFI_SUCCESS;
1 Z' Q I. A1 W: W6 Z } ( y9 ^0 P) N( A* B1 e
P7 b+ s$ a% b! a `! M! h//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>) d, n' G' S* D2 [: `7 h2 K" m: |
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
, K+ [* z3 ? p! X; b Status = FileHandle05->SetPosition(FileHandle05, 2); 9 g( h2 @5 i; j0 u+ r; W
if (EFI_ERROR(Status)) {
$ Z5 h! d1 X' l" P1 J: Q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 N% b# l) J( E( e, V return EFI_SUCCESS;4 F3 A' F+ Y0 [
}$ E2 O3 F/ s5 f- O* p
8 {1 ], S _1 _ Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
' {& Z% c8 L% n( v6 |3 C if (EFI_ERROR(Status)) {: q1 Z* I8 V% Z% F3 i1 s. c
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
6 g* D; D& k p6 G `7 B return EFI_SUCCESS;) E! q. Y- t# x0 h1 _# F
} ( q/ j& ~- \2 K, q6 s
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);* {; z- j" ?; Z3 c0 s$ M2 }
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
4 M1 g" M% z! [3 [: q
8 B( ~3 B8 }; p7 r7 | Status = FileHandle05->SetPosition(FileHandle05, 10); & P3 P# w# V' F
if (EFI_ERROR(Status)) {# q: u' ?' A1 K+ k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 ?8 X. O$ V! r return EFI_SUCCESS;- ^# E$ V: b9 v! f
}* A0 m7 F; s0 ?$ K
; _9 p' U4 B. C' c7 J- ?' u Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( M0 N( M% x6 Y4 H0 e
if (EFI_ERROR(Status)) {
6 g" \) y, v5 V Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
& |1 ?! d9 i; f: \ return EFI_SUCCESS;
8 k6 \- b; s- u+ W) b }7 m. p3 p7 u: K+ L; [# K7 B
7 Z9 j8 ? w$ z# `; ]( [' ~ Status = FileHandle05->SetPosition(FileHandle05, 12); 7 _, B; J0 [* ]8 C/ E! z! h
if (EFI_ERROR(Status)) {
% m, H/ k/ O( c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ x8 i* R5 z$ I$ A return EFI_SUCCESS;! F8 d: V$ H" O# a) _ n! c
}
, y; Y+ W! k2 l# S2 w , O5 |5 C& z; g0 H3 H/ @, P: m
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); ~* j- E6 ^0 l- Q/ R9 u
if (EFI_ERROR(Status)) {
9 q5 l: ?# \2 z) l$ T/ E3 ^ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 _+ ~. x# [6 W+ @ return EFI_SUCCESS;
5 {; }$ }* N& r0 l- t7 A, U }
# S8 n$ h8 s* b @* i! Y' b) x0 i) `; |, b+ W: T, \9 [
Status = FileHandle05->SetPosition(FileHandle05, 14); & d6 H) T0 t) ?
if (EFI_ERROR(Status)) {% ~% l. O. c+ R1 Z! K) j+ V
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 J8 N# [ F9 N1 \
return EFI_SUCCESS;1 @ v) U- j1 }4 i- w7 K: n
}1 T0 C6 B: Y# y) c0 T
, Z" K6 z/ d7 v0 l5 Z# ]* q3 P Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);5 d( I- l/ _) \+ A0 ~1 B
if (EFI_ERROR(Status)) {
! N- K, c' B9 [0 d& | Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. T5 R# l! d2 Q8 X, u1 L3 v
return EFI_SUCCESS;
! s3 b* d* I9 W5 k. e6 @* i }6 i- C* N9 N# }' x: M: T& I
8 J Y, V& m: D$ c/ R9 B Status = FileHandle05->SetPosition(FileHandle05, 16); ( [$ H0 f) n; h4 x- i3 R
if (EFI_ERROR(Status)) {
" _7 |* Y( T+ {2 D X' `1 V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ H0 _- i4 |$ Q, R8 `3 ^. f
return EFI_SUCCESS;
0 I& r% R1 g( ^/ M0 S }! U$ U4 h$ y, `8 q# M
, n, d4 V$ d0 g; D Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key); u' C5 F F! P' e6 L
if (EFI_ERROR(Status)) {
2 T! u9 V3 R$ O: `" I Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 ^/ ]9 Q& T X; F3 I
return EFI_SUCCESS;
' V" [$ C9 s1 s! _1 y } + v5 D! h" h+ k
. _0 T8 M9 E% c0 f
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
$ n* [. H0 N ?, K; ^0 \. Y: ^+ @0 E @- V
Status = FileHandle05->SetPosition(FileHandle05, 18); n0 F' h( t5 t/ b0 D# Z2 ^5 @" y
if (EFI_ERROR(Status)) {
; |% {3 S5 H/ s8 e Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 k5 h5 J& ~* L) r/ K1 R! t4 w5 U
return EFI_SUCCESS;
$ ~* Z% j0 k) q, K: o }% a! D8 G, I( r" E A
1 ?- h# f6 u, B, j" d Z Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
- P9 J$ `$ ^' Z& d. L if (EFI_ERROR(Status)) {, Y5 O4 X* b! u% u) o& y9 R. n- C t
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ k! G) m& } G ]8 T; t return EFI_SUCCESS;
3 H3 A- O. [. I1 J/ q }
- n \- t" _- w0 C4 e 9 y' q' Y" A8 Q$ Q8 S ~- N
FreePool(DestAddr);
% Y: N; R5 s" F/ g# l% x5 n- t; E" Q4 m% o2 Y$ I% T
Status = FileHandle05->Close(FileHandle05);
3 g7 W' I7 p) {" @" z if (EFI_ERROR(Status)) {6 w" z ^1 D ]' U# ^8 ?2 o
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
9 G) V# [8 m/ n0 n+ o) L return EFI_SUCCESS;
/ }9 t5 u* m5 D7 N/ T" D }
; Y2 g* |% P; o0 J. y ; ~0 H/ w6 n; h. P2 Z
Print(L"File Name = %s has already been created.\n", FileName);
[& A/ e1 t, O, ]+ a2 h6 f7 s, B4 X8 S9 j
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");" e) y# Q/ l* y1 U- B
WaitForKeyOrReset();
, X- O" ]$ I3 j. Q5 m 2 |* ?$ f/ L2 t- ~" k. d
return Status;
9 D. G; v- d2 m2 v% H$ e} |
|