|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*$ R5 L) t' l7 X8 Q
* myfileio.c
2 x4 A/ n" ]$ B, C * Apps
% h$ ?0 i0 g% ^0 w, ` */
) R$ Q' ?2 S, u- v c' D
' o1 Q r% v2 V1 f6 {9 S+ h7 c: Y#include "efi.h"
) R( q* u% X+ t: i8 u#include "efilib.h"
4 F0 @( w/ _5 P$ j7 D& `
8 p( X* u- _9 D% ~& u#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE9 ^. V: e) G( [/ ~, K. L; M2 P
) ~$ N1 f0 @9 S) lstatic EFI_STATUS WaitForKeyOrReset(VOID)
3 F1 e0 d5 r. S8 o3 Q7 g{5 Q, |8 d" ~5 x- k( Y/ l# Q+ k* ]
EFI_STATUS Status;
/ x# @4 K2 d2 J6 U0 P EFI_INPUT_KEY key;( n7 E3 F e+ h: M( y. ? N( T
UINTN index;3 S& e1 s* E1 ~5 Z+ X+ Z
; k1 a5 s0 Z. O% X: ~
for(;;) {1 L, g/ o) b9 m" r0 b
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);2 z( O5 G; k/ Z' r3 W) X) v
if (Status == EFI_NOT_READY)5 K; l+ ]& G" f1 R7 x7 F) o
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);) ~# n' o' x4 A/ \6 r' e7 K9 }
else
" R+ v! E* A; q% }) N, G' G6 z. U break;& y- n1 N6 M1 ?) W. B
}
s& w: \+ Y* J* Z7 S+ O, C if (!EFI_ERROR(Status)) {$ H. i3 S# y, R. o- _1 W
if (key.ScanCode == SCAN_ESC)
, b0 k) ^4 j6 v% ~ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
: f; {% J9 V1 J* d/ R0 { }. ]. d" ?9 r8 H% e4 A1 o) d
- N* l U$ ~4 m. X3 \+ C
return Status;
_5 [# P( M# i4 R. Z, B}
) y- c0 a: v' [4 O; [; v. b1 Y
* E/ O; G0 l! q# G3 H% Y e) k0 ^# _EFI_STATUS9 K- a5 {8 q' w" T; Q9 T" T1 v' w
EFIAPI/ ~' D1 q7 |& U, f! S9 w7 L( ]" k5 X
MyfileioMain (IN EFI_HANDLE ImageHandle," @" P1 F7 H8 t# Z: @6 D, b6 S4 r
IN EFI_SYSTEM_TABLE *SystemTable)7 }8 h6 r- Y9 ^% z, [# J! l
{: S! X5 X, D" f) {7 X l$ B
EFI_STATUS Status;
" f/ x6 J0 C7 E2 G+ Q: m EFI_HANDLE *DestAddr;
, K- S, B$ K1 j+ R/ j# U EFI_LOADED_IMAGE *FileHandle01;/ i9 X/ v' ?: Y" H: K- K2 E/ q9 q
EFI_DEVICE_PATH *FileHandle02;
7 J; B( d$ c3 H& D6 m EFI_FILE_IO_INTERFACE *FileHandle03;/ ]- p( `) d- \; f/ {
EFI_FILE *FileHandle04;" G( S g7 N' l) r' H6 V! Q
EFI_FILE *FileHandle05;4 B# X- x I- T
CHAR16 *FileName;/ X0 o3 e$ V' |/ }
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
# U5 G+ B( H) O: x \4 f& `! c3 W UINTN BufferSize = 8, BufferSizeKeySpace = 2;/ K5 {$ w: t7 O: S' O2 ?. m: l
int Bit0, Bit1, Key_Space; / F. u) S2 U- w: E- X9 F& o
6 e2 U) u" h0 R9 ~
FileName = L"NewFile.txt";1 g. w1 W, a) C4 f2 D
Space_Key = L" ";1 k9 {/ ] ]: V p d+ Q
* M' l) E8 }/ j' N
BufferA = L"ABCD";
6 n9 c d5 L: s" K+ j2 h BufferB = L"EFGH";
% f$ `5 x# N* n3 B. G$ L) G; T( J BufferC = L"IJKL";
! W+ y. H/ R: {' {: x: }( e. f
Bit0 = 0xff;
2 X3 v! S- z* h$ o+ J Bit1 = 0xfe;
9 V" ^1 K3 p% a; F+ M( ~- g- B 5 ?0 w; R1 U* ~9 e" O, Z
Key_Space = 0x0020; t k$ X# E9 b) ?
, W( h3 c* Z# x5 i( H) k" u InitializeLib (ImageHandle, SystemTable);
7 M( n1 v$ G! ?: S1 T- C1 u; e- J2 j' D3 k$ w
DestAddr = AllocatePool (BufferSize); . Y/ I- X4 b, j6 s5 Z& y3 O2 F
* T. c0 k: t: f' ~2 D7 A4 Q
Print (L"Value of Bit0 is %x\n", Bit0);
4 m2 T* o2 N- T7 Q) [ Print (L"Value of Bit1 is %x\n", Bit1);
2 W" j! L( M# Q/ G6 Y1 }$ R ~ C' e
# E: h( Z! e3 h8 @. ~/ _ " E- U3 c( r$ ~1 g3 @! E: m
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
% W% |# }7 t9 W. u0 c7 }% `) T if (EFI_ERROR(Status)) {
' Q2 n6 w2 |. C% F0 |. g; g, V Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
( N- a+ _2 r4 H$ _/ @ return EFI_LOAD_ERROR;& g1 @$ }. H9 u0 t5 m& P
}0 F3 y& ]. r. ^: }8 G; r0 `# E r, J5 {
% q$ {4 p/ r. J0 l, b, c9 g3 ]
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
8 U" I/ ~: L: A9 ^" f; m3 i) P* ~' Y if (EFI_ERROR(Status)) {
, s0 u7 p Q* d' a) t Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
' D/ Q! z/ c R0 t" {' m! U7 ? return EFI_LOAD_ERROR;& w# m* [9 n. ], R2 z, i2 X
} * M: g; A2 Y, K- ^. _
9 g0 M" H) c# S) ]. `& G) {/ G Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);& m" b0 i( d- U9 F. y: x, T
if (EFI_ERROR(Status)) {( e; \8 {/ \. [- ~% K, u8 _
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);6 N8 `* B$ M$ b& R4 o
return EFI_LOAD_ERROR;
" |5 k' l6 K2 |% Z% ]) F }
3 _' A8 i3 x% ?- P! P, Z9 z
' i0 Q! O2 ?# N6 Y* E Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);! a4 {6 O6 T' Q' Y- h2 I
if (EFI_ERROR(Status)) {4 O! y0 a, ^& g
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
3 n2 d9 x* c5 e$ r) g return EFI_LOAD_ERROR;, k- a& \# ?. F5 R- G; B7 A. v& r
}
, M( q. J5 _. L0 A& Q, g1 | ( @0 H8 |, W' d% R
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);* m* b4 c6 ^" o, D
if (EFI_ERROR(Status)) {3 {- R& J9 A) A* G
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);# Q5 \0 g) r' W* L6 ?
return EFI_LOAD_ERROR;
$ u% Q' O/ y6 U }
7 i' z+ R6 n1 I& p. ~& {
{5 v: ]+ \1 H$ w9 m4 f Status = FileHandle05->SetPosition(FileHandle05, 0);
. g- x/ C, n8 `2 H if (EFI_ERROR(Status)) {
8 c$ g6 }* [& W# F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# o/ L/ N! v2 J2 \ return EFI_SUCCESS;7 r0 J/ N6 [ \$ Q: E* H
}
0 C, Z8 `& B1 t! P2 A. o ( C/ L/ P2 k/ }3 q# N
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
* h5 J$ |2 B7 s% d; m P Print (L"File Buffersize is %x\n\n", BufferSize);' O6 A1 R3 x2 P$ {
if (EFI_ERROR(Status)) {
: C5 K `; Z) b: A# W! T; N2 l Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
8 { Q3 B' U: R$ d" j0 | return EFI_SUCCESS;
% g" ` k2 b+ x3 S! s7 e" d8 W } 0 n6 u, v4 G* n/ Q( F
# S2 {& D( j( K Status = FileHandle05->SetPosition(FileHandle05, 1);
N$ B3 E7 g/ m; { if (EFI_ERROR(Status)) {5 ~8 O) P2 m/ p' t) x _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 P! j. K) Z3 a& `4 k/ r return EFI_SUCCESS;, H( l# Z0 s5 |) E( F! G
}
2 c; ?8 J8 u8 f e' B: M0 h
0 w$ ?: z! P- x1 L) u) R9 }0 t* i5 f Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
2 V# z c7 N6 \ Print (L"File Buffersize is %x\n\n", BufferSize);
N3 u: n9 f$ o. i, ~ if (EFI_ERROR(Status)) {
5 ~0 l3 W) S u Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
' @5 g1 V+ { @ return EFI_SUCCESS;
. W8 j ~9 c9 A. R% [" ~ }
- N" h, v* @4 }# r! W: d
( q% K8 k' `1 D, K Status = FileHandle05->SetPosition(FileHandle05, 2); , B- s. L2 g& d9 g7 {) J3 v1 h
if (EFI_ERROR(Status)) {
$ W9 x1 Z+ {% P9 m8 O5 c Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ ?4 d# f1 |" ~; g
return EFI_SUCCESS;4 C, K& N" j) c" i8 K; Y# ~
}
. X- y5 g. Q9 m
7 Z$ x" X& K% f% {' o3 p! Z Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
# V/ l/ e3 X8 b" ~ Print (L"File Buffersize is %x\n\n", BufferSize);
4 c+ b9 B1 c8 T" u if (EFI_ERROR(Status)) {
& ?! d# X; T- v- @/ W& ~3 S Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);2 D$ Q% C, s3 J8 h- ^# k. U$ |% R. D8 }
return EFI_SUCCESS;$ U# U2 V9 y! ~8 r$ a
} + Y6 u, M/ H U/ k+ v
/ H& I, z4 [) D8 U& }! F( r
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>( R; k9 _8 U( T/ C2 I9 [/ `" {
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);+ _" X- g7 p$ v
Status = FileHandle05->SetPosition(FileHandle05, 2); 2 S* Z) s- n$ g
if (EFI_ERROR(Status)) {5 X) {* Y* m# Y( h- S! N; n2 @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& b, W5 B% ~' h# A% D7 Z. j9 z. a
return EFI_SUCCESS;
8 f7 G& `% L8 x/ B& f }
1 y2 o# U& j* h2 a( W$ s& h/ Q% P
c3 y6 S% E' b$ Z Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);. W+ S8 R6 O8 b
if (EFI_ERROR(Status)) {
8 @+ S7 f% L2 n8 P Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
) }7 C$ k4 n. d2 p7 `+ n' l return EFI_SUCCESS;) t J, j2 [7 A, }; y, @" t4 f
} $ M* @" y" \+ L) V
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
; A, h, j6 g9 n6 u) D' O. e$ A//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<" ]. F( V% s3 H6 ]8 K1 D6 Z
6 w/ x, }; j, E3 Q" a7 n Status = FileHandle05->SetPosition(FileHandle05, 10); ( Q9 p W% Q! j/ ^7 p8 j+ x
if (EFI_ERROR(Status)) {, A; y% U& E$ y/ I @4 p2 m. ~
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ k, R$ R; M% u3 A return EFI_SUCCESS;
9 Z2 F2 S, Q0 S }
* K8 y% c4 q, A0 x8 D- y* x
B' v" S& B4 A Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);; t0 g* s' ~) F4 U
if (EFI_ERROR(Status)) {3 p, p5 p/ ]$ ~0 k: R
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);9 r4 b F, F! A0 _+ ]
return EFI_SUCCESS;+ }" s8 S+ w, R9 s/ W9 ?5 N
}: A* i/ j7 @- f0 D2 a0 M8 z
; D$ ]4 z' K0 O; @8 Y
Status = FileHandle05->SetPosition(FileHandle05, 12);
1 V7 i0 U1 g. `7 D6 p$ @& u" F if (EFI_ERROR(Status)) {8 v5 G8 ?0 T. O/ U9 F
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) S3 c: n7 j9 R return EFI_SUCCESS;
; k; K6 {2 }9 J% M' F8 b# b: q; |; X" f }- V9 Q' X2 Q4 E' _0 N2 h
; e" O6 y; _2 v3 H
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ ~2 y( l& h: ^/ C; {, [, c: P) B if (EFI_ERROR(Status)) {
g( A! H/ S3 Y' O Print (L"Could not write file, FileHandle05 err code is %x\n",Status);0 Q7 k* k2 w; p6 U& @1 k* F
return EFI_SUCCESS;
: Q4 b v4 W" B' m3 M }; Y5 G' @* K: z& g t
! ^, n. f o/ q& N9 h% ^
Status = FileHandle05->SetPosition(FileHandle05, 14); 0 V' I# n3 G& p+ F
if (EFI_ERROR(Status)) {0 B3 T# I5 r# e# O
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 A( w( @( O3 T: ] return EFI_SUCCESS;
$ Q- V3 O( J' W) U4 ~4 Q }
% [* F1 J2 m) J' o C
+ v- I8 n: s# p+ P/ h; h9 C. [* \ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
0 C: Q# K. @$ s" }( { if (EFI_ERROR(Status)) {
/ Q6 U# y; e* \' a4 ]; [2 B Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. a8 w+ u0 B8 L4 t return EFI_SUCCESS;
8 q" S% L: S8 a6 |. ]4 _7 b; G0 [ }0 c p/ N! B6 |" s6 }# q# a
6 e! o3 b8 V% Z% ] Status = FileHandle05->SetPosition(FileHandle05, 16);
7 w( ?# x! ^2 H6 Q/ O+ K if (EFI_ERROR(Status)) {4 D/ w( N: r' a/ W! k+ J5 A+ S
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' p. t& E3 k" Q4 b7 q/ g
return EFI_SUCCESS;
* t$ T* | n4 f5 M- |8 \6 ~ }# O/ e8 i M( [* r
( u- K0 k' w+ \* x+ n$ E
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);4 Y9 n: B$ F" K7 ]
if (EFI_ERROR(Status)) {
' }8 U' u+ E0 D4 L5 x; o0 ] Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
. ?! {* g* t) i return EFI_SUCCESS; t9 H J; p( |3 m& q, d( T0 G
} 0 S8 ~# O# @# _4 z. H+ T/ a
1 F7 _; c2 d* f
//---------------------------------------------------------------------------------------------------------------------------------------------------------------* J- K& k/ D/ n: f
+ w$ Q+ E- x( W" i- c' e6 ^1 i# s Status = FileHandle05->SetPosition(FileHandle05, 18); ' F% P+ g1 C& p7 T
if (EFI_ERROR(Status)) {
& w6 L1 W" z8 i0 T, H* G Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' _$ n# U W' H9 V! m
return EFI_SUCCESS;8 I4 G# q! L1 W8 d( ~
}' k6 r6 H/ n. [- Y1 F' W
, r K! I: |$ M3 z: V' W
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);0 P+ v. \6 w5 c3 O1 a$ O
if (EFI_ERROR(Status)) { k, {$ M! A. I: ]2 H
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
& I3 j5 U! d: U& L8 v return EFI_SUCCESS;
$ r! a* V, W# P/ c0 s" U# M }
4 u2 [! I4 F) O& j C% q
1 g! M0 L$ j/ t1 v# b4 H FreePool(DestAddr);
$ K. ^3 |! A1 W4 Q5 d6 @( R( |. m5 \- t3 N
Status = FileHandle05->Close(FileHandle05);; g0 G7 M! T# Z& Z) r5 b
if (EFI_ERROR(Status)) {
3 ]4 c" w- p. T Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
; N( k5 p* H7 ?- Y0 E6 Z- H4 G return EFI_SUCCESS;
8 F) l7 \( {1 x: `: X }; v* p5 Y, [ D% ~5 V* h
# _0 K1 j Q& G3 I
Print(L"File Name = %s has already been created.\n", FileName);0 b; I- P2 a7 H% \+ c# z
: p. j7 U: {% s3 m2 g w Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");% |. I6 f+ _+ B1 r( z* b& I( {
WaitForKeyOrReset();2 }2 w* I' @7 a- E/ O& k5 J, y0 s7 q
+ z& j- O1 a; z3 ^! I return Status;4 v$ J" L9 ^" L2 h+ } S- @
} |
|