|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
6 Y+ ~8 w' b1 r; }9 r+ E * myfileio.c/ N8 X! {; D1 c
* Apps" ]2 H" u' \; W
*/
' v* B- Y+ J. b+ ]4 y4 F9 R0 ]: R% |4 d" q5 i: T
#include "efi.h"2 k+ E( Y, a; C2 a: y* S
#include "efilib.h"; u o+ D8 r5 ?, S
$ M4 N. c4 D( k( f, V
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
3 h4 J3 Y( R/ ?. p( c7 Y. z t6 f9 B+ Q" N
static EFI_STATUS WaitForKeyOrReset(VOID)
% c9 C9 T" O# z4 {4 I9 m5 }+ R{
4 x5 v% O) B7 A9 Z0 m# L( }2 R: m EFI_STATUS Status;
h+ p m. j5 o% c EFI_INPUT_KEY key;
/ M! c$ |$ Y5 }/ [( o; p$ O UINTN index;9 n0 ^6 }% t2 Y
0 R" h! S: ^& B& s6 @9 s9 o
for(;;) {
$ r' W0 m. l- s. x$ S; G. i2 }+ F Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);: c2 H& }* C9 `
if (Status == EFI_NOT_READY)
: T+ Y3 ?2 E. b) G BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
$ V! b$ ]) z! e2 p else( ]2 M+ ?! j# q
break;
+ e3 _* H1 U* @. k" H+ y% q3 g }8 t/ m2 j' ^6 P& Q' O
if (!EFI_ERROR(Status)) {
) k/ p: x4 @; p, Y! a5 B if (key.ScanCode == SCAN_ESC)
- a2 K9 \4 y* v; g9 ` RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
( C5 `) ~, ^8 ~3 P* C! n4 D0 F5 U5 \ }
' E' ^6 F; a; j+ ?8 A% R1 j
* ~& Y& q; k2 u; J return Status;
/ y! [& i/ T4 ~}6 p" R5 J. _, F
# P; ]) i8 t1 Q! f: uEFI_STATUS
. W! r; t5 L6 R3 c+ ^EFIAPI$ H4 ~; i- V M. }4 w7 E
MyfileioMain (IN EFI_HANDLE ImageHandle," k8 d$ K' @) H* N8 W6 g" E
IN EFI_SYSTEM_TABLE *SystemTable)
3 z1 F% o: l5 E& U+ G{
L5 }0 G C' k; [ EFI_STATUS Status;) ?2 G0 q. p; d# Q! T# z* I
EFI_HANDLE *DestAddr;
9 ^: O. r, m3 E' S3 m' E EFI_LOADED_IMAGE *FileHandle01;
h4 }( f$ n$ A0 v; b, J2 w4 v0 w EFI_DEVICE_PATH *FileHandle02; " S1 ?8 ~8 Q& b3 g7 k P' T0 _
EFI_FILE_IO_INTERFACE *FileHandle03;+ p7 {& ?0 p I/ [+ I: k
EFI_FILE *FileHandle04;
$ G$ C' [1 I. |' q0 | EFI_FILE *FileHandle05;
3 ?4 y" t8 p; p4 o CHAR16 *FileName;- ^8 ?' Y6 r' j, C, p) k9 B
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;4 h6 | V) w+ D( ~8 s2 R4 V
UINTN BufferSize = 8, BufferSizeKeySpace = 2;
0 s8 x; q1 b6 c7 I: z int Bit0, Bit1, Key_Space; - Y2 ~. R: s) x+ r
/ Q# |' B! s& ?; N) I( j% h; ~
FileName = L"NewFile.txt";% O0 n+ `$ t% O
Space_Key = L" ";
) Y6 n, J' X6 L" E0 I7 U) }3 V + U0 u. ^# [) `/ w
BufferA = L"ABCD";
* A# V8 J% O: X9 P BufferB = L"EFGH";
* H' T! {6 N2 [# g0 K6 a. f BufferC = L"IJKL";
5 P) d) r# ]; V1 F% R
# U. V* B5 O# F3 T$ J1 q1 U Bit0 = 0xff;
. W5 |. |2 e0 i1 f* ~ Bit1 = 0xfe;
1 D& w w3 Y) F f1 e7 b# F3 q. w3 n
Key_Space = 0x0020;
8 Y2 j5 Q( F# B U' X # _1 o0 Q6 _4 g6 Z
InitializeLib (ImageHandle, SystemTable); . B) f( x2 C' q6 n* ?; t# `
! q: p8 z4 t8 z! L! G% g DestAddr = AllocatePool (BufferSize); ( w2 l' O7 |9 f8 M @) T
6 {1 P9 ]) W+ y2 l% Q1 _ Print (L"Value of Bit0 is %x\n", Bit0);
6 T1 x! ~6 v& H6 C% S* P6 C* T0 v, S1 ^ Print (L"Value of Bit1 is %x\n", Bit1); / |' ^( G+ ^7 b. m- R
5 K/ r+ P# ~+ Z
' k; J+ d9 T5 k6 [ Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);8 n5 s" r% w) O
if (EFI_ERROR(Status)) {
# H; l0 W# L0 l$ K) k8 m Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
4 S1 t$ w; B9 g- ^% G return EFI_LOAD_ERROR;0 r3 [/ e s/ Q0 T
}
% v; L. ^# e* [! S" Z" i' p0 B, L5 A) L
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);4 N& Z( ?4 F& ^8 g* M7 P* M j
if (EFI_ERROR(Status)) {8 t6 ^$ G& E$ X: f6 s4 n4 { x0 F
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
- ?4 c' W' r' L. A3 j& s3 a return EFI_LOAD_ERROR;2 X @: m" d# B { f9 R! B
}
4 V! W: Z* w& A+ C
: @! n1 |6 Y0 M7 X+ C Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
) [ p, }+ i# C- ] B- c7 _ if (EFI_ERROR(Status)) {/ \: T, H- I2 H2 D& c- G f
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
; P7 [5 j: @; h+ l s }4 q return EFI_LOAD_ERROR;7 x4 U1 J2 F) w7 G
}( T) t& [) s7 g6 j; a" V
1 _9 E$ Q* B; t' |3 Q Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);7 H R" M3 g& N, U v5 M! Q
if (EFI_ERROR(Status)) {
( g! p- ^7 w/ A Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);" ~" K! ~" ?4 b: |$ p
return EFI_LOAD_ERROR;
+ \3 z6 g* I0 [3 `( f, k/ _- [ } : G9 r3 |& i3 J/ V. }% v
+ E' [8 q* Y8 D5 \1 F# _ Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);5 x8 }' u9 }) c) z
if (EFI_ERROR(Status)) {
- W7 H% }8 m& L+ k! H7 D Print (L"Could not open file, FileHandle05 err code is %x\n",Status);% F$ Q/ |7 ]% R2 x* m: K
return EFI_LOAD_ERROR;& [9 B' I; u7 W$ M
}
7 Z8 v' B: W& n* f4 ? 6 W( x' S! p, m
Status = FileHandle05->SetPosition(FileHandle05, 0);
% I; B: T* P4 x6 y7 u if (EFI_ERROR(Status)) {0 Q( p' R* h& B" Z2 h4 X" |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- `8 V; O* v# h) @; v0 V& N8 ~
return EFI_SUCCESS;( E+ B" S8 P3 g, | }5 T: ]7 ~ I
}
3 F& K2 t X) D b$ v- y 2 {% l+ e/ [ Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
" g. J$ t" @( O) G: i) [ Print (L"File Buffersize is %x\n\n", BufferSize);
, l: B4 {1 V/ h: ~% h if (EFI_ERROR(Status)) {, x' ?; \) {3 e. N; J
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
\" [" W7 n2 c. x3 R return EFI_SUCCESS;
, R) B: P' X J& H4 L3 @7 u } 1 q& S2 @2 e& {' l
* s0 x0 g' N3 r' e. s! l; T! S" o Status = FileHandle05->SetPosition(FileHandle05, 1); 0 T. z! }8 K/ d: m* K0 Y! V6 D
if (EFI_ERROR(Status)) {
7 X/ `" O; ]! y' ?; Q1 M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 Q: H2 {+ [. l/ | return EFI_SUCCESS;6 t( R2 K0 O# z( u% C/ }' h
}
& \+ s# f9 q8 ^4 ~ `. i ( @6 A- u+ W" ~) `; u7 O
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);, o9 D, C( c) R: z
Print (L"File Buffersize is %x\n\n", BufferSize);
1 f- M8 I, P% M+ [+ H if (EFI_ERROR(Status)) {
1 [ f2 H9 t3 e; I- F# s Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);6 w; `3 ~" M0 b+ z# X1 |7 \5 o6 T! H
return EFI_SUCCESS;6 z. ?& Y# }# ?
} * d8 L1 k# [# W" `' g) C3 C) B5 [
2 j) U/ C% s( K Status = FileHandle05->SetPosition(FileHandle05, 2); , ?; |# _# a o
if (EFI_ERROR(Status)) {. V8 F6 k7 b: S' k7 c" p
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; t8 H+ ?0 V" V& q+ d0 m
return EFI_SUCCESS;
' u& I! K5 o* z }
: \; i$ W! ^5 Z
+ a( ~# u) ]+ L Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
8 U" J f$ T& T/ D5 d1 A Print (L"File Buffersize is %x\n\n", BufferSize);
% B) I7 A2 X1 N# @- g5 l if (EFI_ERROR(Status)) {, z9 H, W( d$ T6 J- z: |: p
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ y6 \7 L: H8 g/ I
return EFI_SUCCESS;, L+ m) U; }% D2 Q2 L0 c; Q3 W# J
} : M5 Z! }' y6 }# J6 q
% |- N! R' F7 B5 j8 [" j, j
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
" e2 J. S& b0 V8 ^. ?; m) w Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
/ e f7 _& p8 l: x: w Status = FileHandle05->SetPosition(FileHandle05, 2); * ^) P$ W7 ?3 ?
if (EFI_ERROR(Status)) { x" z' `7 n! c% Q% ~$ s m J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
b, ^0 y) P4 [: i5 p return EFI_SUCCESS;3 U `$ X/ s6 Y4 M z
}
3 p! u' v& p$ s: G* o! Z" N4 u( b: W) b$ U! H
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);" t) z. B: K# W; q
if (EFI_ERROR(Status)) { l; x, D$ i% w3 M. k
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
3 L! L4 q# P" v" o; m return EFI_SUCCESS;9 Q. e; b8 p! K# q0 ?* T2 k4 d: ~
}
7 [9 c, M c/ w$ K% e0 C0 h0 r Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
$ y% ^& P1 b" n& O" x4 a+ G//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
8 h' O' v4 P- l5 w1 z/ {
4 \2 M- j4 N" @3 k Status = FileHandle05->SetPosition(FileHandle05, 10);
- W( }# ]( ?0 r0 o, T% q if (EFI_ERROR(Status)) {
4 P/ H5 ~2 g) `! C) a# g) o. ]! F. _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 W4 k8 `/ T& S9 J
return EFI_SUCCESS;
+ {1 X* d; D+ {6 _! a0 s }5 \8 u9 x& e9 Z! G" `! i# G# }5 N8 p
$ g( O4 j7 m+ w( j F
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ L& R: W, O: D" l
if (EFI_ERROR(Status)) {3 d" y C' q5 C; P8 G$ q9 l+ K
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. s8 d+ l3 g) G return EFI_SUCCESS;
; N( @1 L- n7 B% W }' j4 z8 w0 ]7 w. L
) U: I! l/ x' A& M
Status = FileHandle05->SetPosition(FileHandle05, 12);
/ M* H$ x$ _! I. I if (EFI_ERROR(Status)) {
% _* z( O9 l4 k# u; O! U( r Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 |9 N3 a0 S. L6 f return EFI_SUCCESS;3 ~$ K- }: a5 N) }: }, D; y9 _6 g
}
' H% v4 c4 t% F2 W0 Q2 [ 2 x) |5 P* D3 I! f/ t3 c
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);7 P2 D D( K& \! A. [
if (EFI_ERROR(Status)) {
3 Z G' T. r+ u1 l. n& A# y Print (L"Could not write file, FileHandle05 err code is %x\n",Status);+ ^ \+ U/ | W
return EFI_SUCCESS;9 [1 z; N* y6 O3 g
}/ N/ k* X' c0 v; k+ y
+ ?* z! T( r- L8 i Status = FileHandle05->SetPosition(FileHandle05, 14); & N* }0 t4 H; m
if (EFI_ERROR(Status)) {
/ L/ o4 T3 v! b/ r. K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 U! ?/ e" a4 t2 a. Y return EFI_SUCCESS; Y/ F/ w% G& G* v
}0 e2 I, { E- F3 r& N, }
& }! u' K0 `/ c Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* c% m. B8 G2 b Q/ E- X if (EFI_ERROR(Status)) {7 }& X5 F" z) `/ {# z
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
) o; ^& [6 G3 V9 a1 A" P return EFI_SUCCESS;/ l: h/ ?) n1 c7 i5 b5 L; P& r5 @" h
}4 K5 ~6 t1 P* r% _1 E7 }4 @+ C% s2 ?( @
3 Y$ _2 A0 }( u$ D Status = FileHandle05->SetPosition(FileHandle05, 16);
6 ?+ q* F4 M3 H3 J6 Z8 Y if (EFI_ERROR(Status)) {! \. j! T5 ^- L8 `0 Z2 [7 \" U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ C" H" k- x" M: ~7 Y z
return EFI_SUCCESS;
% ?& J* j. J' ?7 _/ y! |" ]) f }
) p n% [( p/ {- \ f: U * m$ w4 v. K$ i9 L
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
9 \4 N4 }' ~2 p: c5 s if (EFI_ERROR(Status)) {
9 x4 l# U5 T0 T3 |3 C. U; z+ v Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: ?/ ~4 I7 ?+ ], d8 q4 Z$ T a
return EFI_SUCCESS;: z: Y: j& J# k- k/ M% Q! }
}
, D: n# n: S7 h1 s! P, b8 c, k! F0 J
; L7 i+ T3 P# f- k& P( r6 ^//---------------------------------------------------------------------------------------------------------------------------------------------------------------
- r# ?: N. V+ }6 M- o) i- |, L) I
Status = FileHandle05->SetPosition(FileHandle05, 18); 9 e, d7 @5 U" o' t
if (EFI_ERROR(Status)) {
" P# v; h9 J! \ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' D6 \5 m; ]) ~ return EFI_SUCCESS;
0 n3 |- _& i) {" [- u |" i/ O }
; @% x' l. r* W8 H 3 Q; F. i, k j' A# }
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
- {) G3 D) ?4 _ if (EFI_ERROR(Status)) {
# e8 u( u! [; H Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
# l4 p& ^: o, C6 e return EFI_SUCCESS;
4 L: k3 [4 j+ x7 X } + ] e. s S4 ]9 `& z
! m3 m% I7 Q& v FreePool(DestAddr);
0 }) q3 W, G3 A& A4 {
+ Y- j; Q- v. V) Z3 U: z: g Status = FileHandle05->Close(FileHandle05);8 z4 v) g8 N: n4 o
if (EFI_ERROR(Status)) {
6 R7 a- S/ h2 z. q. ?) M/ `0 L Print (L"Could not close file, FileHandle05 err code is %x\n",Status);: M0 A" H$ A. r( V+ ?' V# f# Z
return EFI_SUCCESS;% r9 v2 a5 J6 @8 L) S* @
}8 @* k) D3 {8 d* z6 ~) W2 q4 H/ _
. A# c- u; U" X: K# F8 o% v Print(L"File Name = %s has already been created.\n", FileName);
4 T. o2 p2 k" k8 T& |
& `# Z7 y* @+ T8 \2 f) k Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
! l1 m6 x6 {7 b" z! i5 s2 ~1 R WaitForKeyOrReset();
( Y# R" I$ K# o* R
2 n( [( g. M' e7 P return Status;! L g( B( s3 O7 K) o& A
} |
|