|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*# d0 l* `1 T$ M- V
* myfileio.c
3 _$ d3 {# i3 W * Apps& @# C. ] O8 a4 ~: g
*// G5 E6 {: m4 K9 B" [
( g6 q1 M6 f0 g0 [* [8 F- u
#include "efi.h"4 ?) G8 c r5 V) P- V) F
#include "efilib.h"
1 b9 I. X I2 X1 b5 k. W4 M0 J4 M) W$ I
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE5 Z% N& Z! K, u7 N& x% F
7 s6 y6 G# V6 X/ ^+ Fstatic EFI_STATUS WaitForKeyOrReset(VOID)/ w& o2 h5 z- l; v) t2 ]
{
" }' \+ l* L, d, e4 p( e. N EFI_STATUS Status;+ `) h- W' \0 |# ]( {; u& X) s2 l4 `
EFI_INPUT_KEY key;# Z! i F& Y$ k! s% j9 M K) y
UINTN index;
# A4 C# j1 p! V4 G4 U) K0 G
7 u" w* f T, ^% k H for(;;) {
$ B+ K" N/ I, K, y u7 S Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
" V: J D9 k& K1 n1 ~ if (Status == EFI_NOT_READY)5 C% u2 S1 l) g3 d$ u
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
( F, M% z6 z {' P% e else
" P* Y: w$ C* m break;
: G+ c$ z! E, M9 {. S! ? }
$ p, C k P- k) G! O6 z if (!EFI_ERROR(Status)) {
& C) X3 S; k; b% }& ? if (key.ScanCode == SCAN_ESC)
' q2 T% V3 e4 }" F) ` RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
; m* y; v5 t9 t$ z( v }1 J! O! Q5 D- q
& f8 U" }/ {6 t, {/ y
return Status;3 I1 q# n5 x. R% M) X" u
}
/ S. ]7 a9 h. t+ R7 Z- w9 y+ G, ^" F5 F
EFI_STATUS x0 k$ X4 O& \. C6 i0 F z2 w
EFIAPI
5 ]' |0 a" E8 x! E1 iMyfileioMain (IN EFI_HANDLE ImageHandle,
7 a% Y3 f6 q X: O# S IN EFI_SYSTEM_TABLE *SystemTable)
' ^( F% `$ {8 f$ G, F. N' Q- y# K{4 |3 a$ T1 k. b' |
EFI_STATUS Status;
7 l- A+ `8 ]+ Q3 ~0 G' v+ o% q EFI_HANDLE *DestAddr; ' V5 c% }3 z1 X; b' k
EFI_LOADED_IMAGE *FileHandle01;$ T1 N& e! i& I4 ?* d
EFI_DEVICE_PATH *FileHandle02;
8 _' Z- S( o1 f' H% y EFI_FILE_IO_INTERFACE *FileHandle03;
* W7 S* Q; @9 J4 a$ |$ u) K. I EFI_FILE *FileHandle04;
/ Y' v5 l2 ^1 Q; m* I EFI_FILE *FileHandle05;' r- i6 ^, z% K% u/ F
CHAR16 *FileName;
# D" y# g; L+ C4 o) W5 H! x CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;# R% Q. k0 I# l+ c7 |
UINTN BufferSize = 8, BufferSizeKeySpace = 2;9 }0 A Y, N, s! s6 v+ s4 V
int Bit0, Bit1, Key_Space;
0 b1 w x9 h9 n( K/ A+ `1 H3 E- j# j; x R( r
FileName = L"NewFile.txt";
8 X& v) v3 ^" W* p Space_Key = L" ";
5 ^$ `; I# o% Z9 m( H
$ E( r/ b( G1 c6 B2 \9 H BufferA = L"ABCD";7 \7 P3 y f) X: ]1 h' h5 f4 J! V
BufferB = L"EFGH";- [7 N e7 R3 u1 a
BufferC = L"IJKL";
1 \" |& n0 g" c1 i* a# @; G" G* h! F/ F- X4 a; S1 Z
Bit0 = 0xff;: v; U( ~6 w3 W+ @1 I; Y
Bit1 = 0xfe;
% W+ n3 C( {2 f& j: b0 G( n / l) c& P2 q C) K" q3 Q
Key_Space = 0x0020;
: t5 B/ f5 U& L0 o7 V# u; @ 2 Q6 F7 P* e5 _. ]3 x+ T, A) q8 T
InitializeLib (ImageHandle, SystemTable); # S g- w$ p+ [
}" a1 q$ [) A DestAddr = AllocatePool (BufferSize); & u5 y0 e L# v' T/ P2 l
J/ [7 G' ~$ ~# _
Print (L"Value of Bit0 is %x\n", Bit0);
4 J& ^3 g8 A4 Q7 \' c Print (L"Value of Bit1 is %x\n", Bit1); 1 q4 @* @; o6 R# I) w" ~0 V
) C9 [2 u* P1 A3 ?8 Q; J / Y: V: L) ]- ~ L/ I& M
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
% v/ i) G% n8 @5 b! x9 l( N if (EFI_ERROR(Status)) {
' ]* T/ T& r8 g: x$ J, C Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
2 |9 ^$ G: h8 L return EFI_LOAD_ERROR;7 i! z- f s7 B' X
}8 `. C: B) J4 Z+ M
/ s$ e6 \3 Z/ y; s
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
! K6 C2 ~, z/ ^, N5 q0 v1 d if (EFI_ERROR(Status)) {
O! u) Y$ t' `# I% I Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);" e; m. u# R. ~+ o; A& f
return EFI_LOAD_ERROR;
; V+ f; V7 s% |. j) f }
/ V7 _' G1 r9 C- t' x: i5 J- T: f
& k% a) P) P, q2 i, C Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
" Q- _+ k2 I [8 B3 R R3 V0 ] if (EFI_ERROR(Status)) {
1 Z; O7 k1 _& p: n Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
3 z* S& e6 R' y& g3 @4 w% t2 U U return EFI_LOAD_ERROR;
- ?3 k, k5 {. }$ i6 W }$ N+ S& H6 I! n3 s- }
+ R; H4 k/ u9 L" p5 {) C, W4 g* ~ Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);' Q7 l+ h( z, t! i
if (EFI_ERROR(Status)) {
( z" Y8 Z T R, Z( }3 Q Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);- y K; r2 r0 `" C" x
return EFI_LOAD_ERROR;/ m6 ~3 R+ D$ v, s/ s
} ! G( ?" X7 P8 Z- H# h' E
6 Z2 m" [/ i* J4 j- Z& |& [ t ~
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);, K8 x. o4 L" d3 A7 z- @5 A
if (EFI_ERROR(Status)) {& s- w8 Z1 O6 L) \ s$ N# b: G
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);6 p+ U- ]1 w1 Z% t# K
return EFI_LOAD_ERROR;$ n% r% {, O9 w
}
3 R9 ~% N, i1 N8 q
' X. C/ n+ c, E Status = FileHandle05->SetPosition(FileHandle05, 0); & ^: n1 w( S2 m7 `
if (EFI_ERROR(Status)) {
) g; j3 M5 [1 X% C1 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. D& O: M# p7 F5 z7 ], B
return EFI_SUCCESS;
% |: H/ S, ]' O0 r8 ? }) O" W. F0 t" Q' i) k. I
" [8 }/ a2 t4 K* ]: z' b9 s, M' C5 z Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);: |% p7 \; C: H9 S ?' P. E
Print (L"File Buffersize is %x\n\n", BufferSize);
9 Q n! g3 ~# X* ]$ w9 R! W' w. T% G if (EFI_ERROR(Status)) {
, A4 p! K) X: r) ^ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);7 J; p, G! l( Q( Q6 v: a
return EFI_SUCCESS;; j4 F+ t1 }5 k7 |: _% u$ ]& Q
} ! O; [$ {4 Q6 a) y2 `/ a6 }; X
; H+ n O' Q5 v7 Q& Y3 S
Status = FileHandle05->SetPosition(FileHandle05, 1);
* i! v. {% w7 W8 g if (EFI_ERROR(Status)) {
$ j" \; K! m! j! L" L Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);/ n) i: w. f5 c/ x3 R/ L6 ?
return EFI_SUCCESS;: E, v8 E9 n: M5 O6 b0 o2 L
}
) y& Q# m2 ?- j4 y# z
* }( A0 t0 @0 i) u& H6 B Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);: |* y* V' I8 j" c
Print (L"File Buffersize is %x\n\n", BufferSize);
4 V; H8 Y4 s8 D: L; B if (EFI_ERROR(Status)) {
( q8 r$ @* k2 a: R4 e2 J+ U Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
; [: i+ ~8 S- \7 @& `9 J) ]! Q return EFI_SUCCESS;, E' z# f6 [# u; w& [ c
} 3 j- D0 P, l. p/ |' b6 K! Z
8 Q5 y1 M1 V1 t: A0 Q
Status = FileHandle05->SetPosition(FileHandle05, 2); K) U( {7 }$ }( q# d
if (EFI_ERROR(Status)) {8 ~* K) d n. R: ?+ c* U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 J! {. o' ?" B$ F1 b& S return EFI_SUCCESS;
) e3 h. h) t3 `# t( v7 o }
5 G& S Z: y* t; y& R + b8 \& K B5 u
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);' _- k; a2 X! \- |1 q% w
Print (L"File Buffersize is %x\n\n", BufferSize);
: l( ?. K9 d( [( o q. I if (EFI_ERROR(Status)) {
' |- b+ r2 \% w) h+ O" X Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);$ `; C1 e5 p" ~% i8 d l
return EFI_SUCCESS;
* e& T) b, Y* J) K9 v" { }
) X& n1 A# }) t( L& W6 f) T
, {2 G2 T5 l# V# m3 p) |//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
$ a1 A' V1 w6 R Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);) Q' o# M3 \9 S8 {/ c: Z, X! I
Status = FileHandle05->SetPosition(FileHandle05, 2); 3 n8 C& f1 ]; ^0 V2 E: |
if (EFI_ERROR(Status)) {9 X% p: \; e& S* x
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ ?' o3 t5 G- j0 f return EFI_SUCCESS;+ v8 X+ r* P, ?+ f' V
}! k: j3 S! m7 M; K" M
' F( k$ P! Q; G# i: y1 ?# z Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);3 ^6 J- S( T8 f+ u8 n# Y) G& Q
if (EFI_ERROR(Status)) {
# Q t* s2 Y4 F9 z- Q! L, j) u Print (L"Could not read file, FileHandle05 err code is %x\n",Status);, N0 B( Q( o2 v5 u
return EFI_SUCCESS; _$ _% c) ^- S2 d- Z
}
1 t* A0 k2 ` ^; P) [ Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
/ M, E; f1 ]# M' l//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<& I" D2 t+ s0 E; J) \0 D
/ Q( a- {3 k9 @& t0 V: n @ Status = FileHandle05->SetPosition(FileHandle05, 10); ' f! E) x/ y8 P; p& d2 U
if (EFI_ERROR(Status)) {
- C" U" o4 E( p4 D( J Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. ^1 [: n n5 t/ e: K4 r% p3 J
return EFI_SUCCESS;" G( y$ A& ^. F+ Q
}
7 K/ Z# }0 h7 y/ h, [# j6 j% v 8 m+ e0 g N& ~2 y- @5 n
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);' t( \. ?/ P6 v- e
if (EFI_ERROR(Status)) {1 i0 H1 L8 Q$ r1 u1 Q+ `! i
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 e; u& k7 P6 A0 j1 s4 H$ E
return EFI_SUCCESS;4 W/ e! L" s% Z/ t, K" h; c
}
1 _/ N9 \ `$ b! n5 l, i; l* m& b+ ]9 q. u5 ?) f
Status = FileHandle05->SetPosition(FileHandle05, 12); ' o( C, Q4 R' y, P
if (EFI_ERROR(Status)) {# L: D4 u& d7 q
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
9 I, g% F6 H* m; L/ r return EFI_SUCCESS;- e8 t) `& b. G& v m: q$ M; z2 A
}' u( Y6 T7 s: q" h9 \+ n
. v) x7 n: m; r" ~* X# R Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% x' r& v. S, S* b# N if (EFI_ERROR(Status)) {: w: m: X3 r! F) _7 J7 |
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
* o0 a) `7 I! n# x return EFI_SUCCESS;# O5 N' w; E) ?0 J& Q3 \
}
/ s8 s4 {9 w2 [- q" t- ]" g
: ^0 `$ Z' q. j, u0 @, X Status = FileHandle05->SetPosition(FileHandle05, 14);
5 g1 v6 E5 ]7 y3 H/ g! w if (EFI_ERROR(Status)) {
' r! l8 n4 B& Z6 G Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! p4 t" W' k H# Y6 ^- t return EFI_SUCCESS;
( x2 D) q, v+ O/ x }
1 l; o7 \: [: q* F0 ?: W7 V : d" x! v) L, Q7 Z
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
` S2 `$ T8 E4 q5 u7 k' S if (EFI_ERROR(Status)) {
) H! p; G: X- Z, h4 p; F Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' g$ X, l% H8 P+ l
return EFI_SUCCESS;
4 @/ s9 Q8 y% e" E/ [ }; d: M3 N; v( H. {" j% \
$ m ^' Q9 r1 l/ I" |3 N
Status = FileHandle05->SetPosition(FileHandle05, 16);
$ L' Y5 H; ~2 Z2 G if (EFI_ERROR(Status)) {
* \' A) o4 k+ [$ q% S Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 {1 M U4 ]9 Y9 [5 j. ^ U9 Q' d
return EFI_SUCCESS;5 _4 ~. [1 k( F, L1 v8 q: J
}% [+ h$ m# @2 ~3 b" [
j8 e5 b' j8 [3 t Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
3 f4 L2 M9 A0 }9 u( ~" M4 n if (EFI_ERROR(Status)) {/ V. C8 t( Z. u; J7 E
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);/ V! I. b! u t$ s! m/ D( x
return EFI_SUCCESS;
' E8 l: y. \4 t! O1 f% E$ ~ }
- s. ]7 J4 t- R( U- ~( `, g2 Z
! r: g9 z2 h- t4 E//---------------------------------------------------------------------------------------------------------------------------------------------------------------; g f* L2 j# `4 E8 a# e# |/ }7 l
, [+ d( N2 h( j; |- }& P \( ^ Status = FileHandle05->SetPosition(FileHandle05, 18); " ^8 r5 Z1 a4 i7 a/ a7 x) a
if (EFI_ERROR(Status)) {' m: A f: D+ v) r
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: y6 m, H' z, ^& G( F
return EFI_SUCCESS;
9 R8 P" d. D4 `: x }
* s3 A; Z4 k5 R4 `) p0 ~( k
$ E! Q, p Q% S" S: I8 p Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
9 ]% j4 A0 y/ B3 B if (EFI_ERROR(Status)) {3 Y0 l& R% A& a, L
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);1 L* ?; S$ |# w# T2 e* C: |
return EFI_SUCCESS;8 `. f6 Z8 U; _9 b* ^# H% L8 g2 n
} - z* q2 v3 k* w
- q# B" R* |# e7 V FreePool(DestAddr); # `" [ Q1 L7 \
# n3 `/ K/ h t1 @ Status = FileHandle05->Close(FileHandle05);+ ^7 u0 |/ T9 W/ o
if (EFI_ERROR(Status)) {8 w& q* D+ X+ ^) \: E: V4 g \+ C& g
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);2 r [9 Y9 `8 H1 [
return EFI_SUCCESS;: ?2 z" o/ a9 U( T, s
}
& ~! J' J. f" C, q; U
/ ^2 n3 Z$ n, x' _0 m5 l Print(L"File Name = %s has already been created.\n", FileName);
9 d* f! F% r) h" N& |7 j0 h3 U, b* R: K0 |$ I4 Z4 m
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");9 `5 d2 l5 h5 E: M& a
WaitForKeyOrReset();
3 n+ P) I. \; Q# G3 ^6 t
o# f5 n7 D2 x( i return Status;
! f# V! A6 f6 ~* i- [} |
|