|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*- H, H- ^& x+ z, ]. [/ g# L
* myfileio.c6 j5 I* |; h; A
* Apps" O# _4 r. N2 [, }
*/& a' Y8 B j4 k+ j, q, ?* Y/ T7 x& W
, R6 @- D# Q9 J#include "efi.h"
) ~% q4 q9 J$ i1 w#include "efilib.h"
$ _: n9 C; h2 |0 H: W! F2 V/ O6 w$ T+ D
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE/ b! |9 K$ V2 H0 A2 |' m
# l- Q7 H, b0 R1 [+ u* d8 z! Tstatic EFI_STATUS WaitForKeyOrReset(VOID)7 R; |1 @: N) X7 [5 w; T
{+ j% ]" `. Q, u; h1 n; k5 Z
EFI_STATUS Status;
) A5 H- J- r$ R. m: Y; |) l& H EFI_INPUT_KEY key;
- d' i) d; |% T) e$ B" |8 { UINTN index;
, P w: N9 ~( z+ n- x
}8 q, {6 h- m+ W* y7 O for(;;) {3 B5 _$ }% S8 U' v. X
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);9 u8 s( w5 h5 e& Q
if (Status == EFI_NOT_READY)/ T0 u/ R& `6 |
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
/ e$ |5 ~% s1 D6 g# Q else( |9 i4 h* W! S$ T! {
break;* R( f# V- ]6 \
}* c5 ?0 n- J0 v2 l8 L
if (!EFI_ERROR(Status)) {
/ [3 Q: A5 }$ A7 V$ D! W if (key.ScanCode == SCAN_ESC)
' r6 X) Q0 n# s2 D4 l) q/ {) { RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
9 Q) g, J3 H' y2 J. e }% T% s" I% x3 w* Y6 ?
+ w; |& k& j2 ^( ]. M7 }9 l
return Status;
* }) w4 e. [7 Y( F* g}3 i: E4 u, M7 F9 M: K1 u) ?
8 {5 g8 @. A+ VEFI_STATUS9 c1 j& ]$ `4 Q
EFIAPI; Z! r5 L+ ~, J0 s
MyfileioMain (IN EFI_HANDLE ImageHandle,7 o9 y) C* P4 L! v8 G
IN EFI_SYSTEM_TABLE *SystemTable)
" s1 j" @2 l v9 v* N( U* D{
' p7 P5 U/ l4 L: ~ EFI_STATUS Status;. R7 x# \7 N. u' b# a
EFI_HANDLE *DestAddr; 0 j, ], I1 M6 ^/ y- \. I
EFI_LOADED_IMAGE *FileHandle01;
* b; h' c, @! ~, Z# G# N! o; h# ? EFI_DEVICE_PATH *FileHandle02; ' s; Z0 @; t7 p4 w
EFI_FILE_IO_INTERFACE *FileHandle03;( L: i' c& K- p8 E# U; m
EFI_FILE *FileHandle04;
; N5 v. Z# l- H" B S: J EFI_FILE *FileHandle05;$ K. \: Q, v; O: J. W
CHAR16 *FileName;& @+ f$ k# | _6 w- z* o
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;1 `4 U9 k: v; u4 j7 z
UINTN BufferSize = 8, BufferSizeKeySpace = 2;: C$ Q. a/ R& G3 z, Q
int Bit0, Bit1, Key_Space; 7 R6 k; D: r3 D1 d6 Z* f! J
$ M" b( ^# N; y2 N5 n% s
FileName = L"NewFile.txt";6 b9 \: s; w6 `' M& t! R; M
Space_Key = L" ";
$ X1 x9 L) O/ \1 Z 1 m2 ~* `. A6 k, v0 W# g' d( z8 f
BufferA = L"ABCD";' O7 p7 K& i; R6 _& ?
BufferB = L"EFGH";' T: z( O" v- I4 V
BufferC = L"IJKL";+ g" q( S4 G; @, }
# B- a" e0 Z& T/ P4 w
Bit0 = 0xff;
9 s, Y) v: b7 c3 j% W0 X# u/ b) x: j Bit1 = 0xfe;
1 z. ?0 V$ I+ z f
, p; |) H6 o8 D+ v6 x Key_Space = 0x0020;0 p) z5 |& `+ t1 `& o
5 L T7 ]9 C+ b, r
InitializeLib (ImageHandle, SystemTable);
5 t& c* m$ S: |% H
4 X" ]4 ?* @7 a$ ` DestAddr = AllocatePool (BufferSize);
" @ {1 Q% t4 T" c7 b' H
$ `( @( H( S) H% t, s# ~ Print (L"Value of Bit0 is %x\n", Bit0);
' @4 C* i' S/ }- ? c Print (L"Value of Bit1 is %x\n", Bit1);
- y; J, K! X* w+ w3 v. I) |
- A+ i8 r6 S9 w! f7 _9 L: U: i7 i
" F, _5 g6 c; N Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);, c c8 A* ` f' |
if (EFI_ERROR(Status)) {
& [* X; V- S$ F+ V* v0 k0 G Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
) c- J4 Z4 i4 [) O return EFI_LOAD_ERROR;
8 b4 r3 K' ~. L& H: U9 h }) { z* u6 ^$ E
, m( ~, b0 m$ l9 {2 w$ s, z
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);. U' G$ x0 j& y6 O
if (EFI_ERROR(Status)) {& O4 L6 O) F [( m( w) g
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);4 k' J( S) X: u- ]
return EFI_LOAD_ERROR;4 e+ p% P! s5 c7 x
} ( W, m8 C1 @& ]5 @& ~
- K9 Z; c; r7 R8 c Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03); o( f! k( ]$ j8 `% U
if (EFI_ERROR(Status)) {
* B+ J7 M2 l) a+ Q+ l Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);7 m3 e: Z1 p5 [$ y7 ?# k
return EFI_LOAD_ERROR;
3 \6 B8 b8 R# F9 F: q2 W/ V9 |* o }6 \1 b/ D$ Z/ h+ L0 M
' \1 i4 Y" ^% v- K4 s% q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
6 A+ t3 o$ [, Y+ Y: W if (EFI_ERROR(Status)) {7 j" ]1 a4 K. {8 X: ^
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);* {! b Y) [$ R1 U& P" [) G* i
return EFI_LOAD_ERROR;
* n) h) s. r$ i4 G } 3 E, Q& T7 Q6 J+ j
- e! w4 v, D6 C: K9 l Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);* K- {' ?2 [* p7 u' u/ ]; v
if (EFI_ERROR(Status)) {
! X* Z1 [# V- ?& Q/ c Print (L"Could not open file, FileHandle05 err code is %x\n",Status);: a+ M5 u7 J1 ^( l7 J, ~# c
return EFI_LOAD_ERROR;& T8 A- ^4 m L0 B t1 d
}. U4 ^! \0 m( I
2 h3 _; E2 G5 }
Status = FileHandle05->SetPosition(FileHandle05, 0); * z G. Z* D* l
if (EFI_ERROR(Status)) {% l. H7 e/ u7 h
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* i. h! G& a3 T/ o5 q
return EFI_SUCCESS;
! m0 T6 }* T8 j( g( k# t }7 _6 W3 j0 P# e" ^# Z, }
6 L z+ |+ L! I4 s# L
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
/ U5 x5 a, B2 t( h' H- O! y% [ Print (L"File Buffersize is %x\n\n", BufferSize);
. }, q1 b: ]" {( ]6 \ if (EFI_ERROR(Status)) {
" f8 Q: b6 @; c) Y' C Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);; z- f/ |- W/ t' H
return EFI_SUCCESS;! Y1 L S) O1 a0 }& M$ N, Z8 A
} 4 L" H" b% x( \1 n
+ j: \% G! H! _9 g% x/ u2 h" S
Status = FileHandle05->SetPosition(FileHandle05, 1); ) Q2 H! o0 c' `! j& i; G, W
if (EFI_ERROR(Status)) {3 O8 B: O( d; B' O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' i3 q; e- p {0 p( ?8 K7 D
return EFI_SUCCESS;2 h' u/ t6 e) a1 e1 g
}
3 }. G) A. u3 P. q 8 L1 d' f) b1 d7 g8 U8 |/ n4 j
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
" ^; l C3 J+ r Print (L"File Buffersize is %x\n\n", BufferSize);
4 H. ~$ A* x7 j9 I" Y* Y' b# n if (EFI_ERROR(Status)) { v4 a9 e; ~/ ]- a0 @
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);* f5 M$ c" @9 V9 ]
return EFI_SUCCESS;
9 p* p% P0 T7 Q7 h8 ] }
" ?$ ]3 e3 V4 v# H1 O
& d1 w# C4 w/ Y% R" i Status = FileHandle05->SetPosition(FileHandle05, 2);
/ V/ P6 P3 t* q9 ^5 g$ b9 k# A if (EFI_ERROR(Status)) {1 K g1 m/ h* B) N& v$ D4 F5 N
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 v" y3 W( G' _' T$ G9 D+ E return EFI_SUCCESS;
4 j3 _2 Q3 l7 y# k$ v }
- K' |3 i2 A+ X! {. h% f" m; F
, {$ t8 _4 g( ~% `+ w# F Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
! G, u7 r% ]: W4 S) E1 T Print (L"File Buffersize is %x\n\n", BufferSize);) u- @1 y+ W4 B0 k& A* ~" i
if (EFI_ERROR(Status)) {5 o$ y( C7 Y5 d0 \ o4 g/ c
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
( ~$ @5 e, p# P return EFI_SUCCESS;
0 u: r8 Z) o1 }3 ] }
( m, S P Q! m, r. i+ A; r7 E* E
: i5 W f9 B; ?% C: J F5 p//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>/ u% }" Q% A. E# z* u
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);# y* ` `0 p# n; p
Status = FileHandle05->SetPosition(FileHandle05, 2);
" G* b& m. k) V8 o6 J if (EFI_ERROR(Status)) { U- Y5 n5 Z3 M! k
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: d# e/ b6 T/ \: D1 v6 d2 F. G
return EFI_SUCCESS;6 F' B8 B+ {/ N/ M1 v6 V; a
}
, Z0 W+ x% O" A+ t) k X" j
# b" Z+ @ Y9 @# F1 L Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
$ t/ x8 U+ W# ^ if (EFI_ERROR(Status)) {
$ t- c) z. l6 e" h5 ? Print (L"Could not read file, FileHandle05 err code is %x\n",Status);1 K0 m8 L; P7 q6 N& W* @% F6 G5 X: w7 B' w
return EFI_SUCCESS;
+ A- z* ]9 N1 M' g }
/ v3 S7 n4 U1 l8 h" j Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);- V8 {; Q; ^0 D! [% K! N
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
2 e5 Y4 }0 g8 n4 A1 E0 Y% s8 C" o' |! h% I/ n$ v# s2 N3 g
Status = FileHandle05->SetPosition(FileHandle05, 10);
9 ]/ ~, M8 u4 Z if (EFI_ERROR(Status)) {( q& N$ o0 S. ^6 K* U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: P+ Z' A, H; _
return EFI_SUCCESS;) G, I, w& b; w. G! o! b
}, k3 K* H' N5 E! F
2 Q" I0 A0 H9 u! }, L* B& l Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);6 i( Z: \. u# V+ q2 u
if (EFI_ERROR(Status)) {) l% _9 q0 n! W. s7 [$ j( o2 K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);) [) E ?% V& A% @% T$ p) l; ^
return EFI_SUCCESS;
$ \, d3 {5 T* f: Z+ j }. Z; R9 c, s8 M1 ~
( j5 w" @* p+ p4 p( M
Status = FileHandle05->SetPosition(FileHandle05, 12); % b) |, D+ }- g7 Q! n+ E& |
if (EFI_ERROR(Status)) {
! f, U) o$ L% X) P) D; K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: U2 `, N1 W4 O+ ? return EFI_SUCCESS;
( ^$ p! q3 D& y( g) g# [ }
6 e8 n+ h; Z& g7 G8 q - t2 k( K8 N% }/ j. h) y
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);- ]/ v# K6 H M8 a9 F
if (EFI_ERROR(Status)) {
* k2 Q- o4 B- u7 p1 h9 b Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* s+ f* ~# x) ~- f
return EFI_SUCCESS;+ y- `' F- i2 y3 ^/ w1 U
}
* z4 W/ E p" |8 p! k3 q& \3 E- I& a; I( L; V7 V" k
Status = FileHandle05->SetPosition(FileHandle05, 14); # R R7 V6 L" o( v1 Y6 R7 ^
if (EFI_ERROR(Status)) {
) X5 r1 c8 u, T9 T9 x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! C9 \& f4 V, [/ i9 _ return EFI_SUCCESS;
9 [+ Y! z; Z7 T4 a }% Q" ?; w" A3 {. p7 o- w
3 s9 v& I0 [+ _# o/ h Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! V$ T( R- ^; _0 r0 L: g9 h
if (EFI_ERROR(Status)) {" r B( v/ W1 y
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 u: x7 i0 p0 U( ^$ Z
return EFI_SUCCESS;
% l8 t; \) k0 S. V" | } g0 F5 p) ?2 T- ^9 z: v
% J& p1 o2 Y5 z& X
Status = FileHandle05->SetPosition(FileHandle05, 16); % ]9 T8 l8 x' E! O, G
if (EFI_ERROR(Status)) {
3 M1 }/ d, o. e7 H Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ H" ~ _+ I5 s9 h4 Y; Z" Z return EFI_SUCCESS;0 X& _; z( ]) K& b
}9 c4 r# E1 ?- G p- n: p6 c' S3 C
& @2 E4 T# \+ Y& |9 B+ C; I- M8 W$ d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
- o. A1 ^6 b9 r1 G9 _9 |: U3 K3 b if (EFI_ERROR(Status)) {
2 e9 k' V, d9 K0 x/ F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);8 N7 h3 X) R' B- o
return EFI_SUCCESS;
2 c% v2 P& L* v% J/ L/ l$ U! i }
) J4 U: @$ R6 m( b- `" [2 }& g; \2 t
//---------------------------------------------------------------------------------------------------------------------------------------------------------------- j$ t; ^ y- |6 \1 S% l# x
3 h: d- {0 V( Y& g Status = FileHandle05->SetPosition(FileHandle05, 18);
8 ?* x" q8 h8 g& c* d/ @ if (EFI_ERROR(Status)) {2 j/ T5 x$ E+ G" y0 L5 x2 U0 E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);( l7 i; n6 C4 [ S% S
return EFI_SUCCESS;! ~- Z) a9 K* p( ^! i' h& D
}3 X& t- A$ s; U9 S7 O
" x. V+ f/ l, N+ J
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);$ ?. E: o3 N# N. m
if (EFI_ERROR(Status)) { [+ g, ~9 P0 @9 U9 [- |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. R) M/ ^- |. Y; W. M! y
return EFI_SUCCESS;; ?: @/ p, U% @5 A5 ~) @$ [
} : f* a0 a' G( S& I* _
! L5 i' W5 B1 Z* j0 S' L FreePool(DestAddr); & Z ?8 r8 w: m- c0 S
5 [, Y( O9 J7 B; y. @; U9 t6 c
Status = FileHandle05->Close(FileHandle05);, g, c8 Q6 }& [7 Z
if (EFI_ERROR(Status)) {
: _( N3 I) {1 E _% u, L5 [- N Print (L"Could not close file, FileHandle05 err code is %x\n",Status);- e- U- x% {4 j. p
return EFI_SUCCESS;8 A8 M6 s0 N9 I6 }" b0 j. H+ K4 E3 n
}. I' A# O# n8 M
& j8 @0 r* s2 `4 G. a
Print(L"File Name = %s has already been created.\n", FileName);
~3 t( `7 Y$ W, X: H. `4 P8 n5 K1 E" q2 Y( A
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
! [; _, c( U6 a$ X WaitForKeyOrReset();. c+ j! H. D6 G) t1 b% k% c. b$ N5 \
( Y1 f. c( f, p5 Q1 t: X6 O
return Status;: o; L) N- L# X; S, Z: V2 F/ A
} |
|