|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*+ Q2 C6 ^/ R. h- W6 w
* myfileio.c. a0 p& o- ]' c* r
* Apps
- U4 e/ Y5 N, h1 w% k) | */
) c% A; G& W) b$ `; m) I0 p2 T/ _0 i9 K1 d$ b0 k
#include "efi.h"
v' {& W' l! |2 Z1 m1 \+ A; ^1 J#include "efilib.h") N% P1 }2 Z7 k& N) x- ^4 r
8 L, X% k7 G5 ^, `& k9 ~4 |) [
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE$ v" u- c, U" e3 A' \4 }4 P, l
" @/ p* U- a. o
static EFI_STATUS WaitForKeyOrReset(VOID)8 }; S+ V2 ]; o% h; b7 J' Y9 k* Q
{
5 c) ?/ H- F2 Q6 l EFI_STATUS Status;
" V- }3 ~4 d4 n EFI_INPUT_KEY key;
& c; D: u; w% c# ~3 A UINTN index;
0 D! R* u! Z. M- R. [
) K- l# _2 I1 h( `% k+ { for(;;) {4 ?. R" e) O5 u4 f
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
. F, j. v9 y- } if (Status == EFI_NOT_READY). f- L' Z I. @ `
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);& o$ S+ v& Q. b! r/ l4 w+ r
else9 ]* Y* j! T) M
break;3 y B- O0 [" I* n8 I2 w
}
4 x/ e! M" R$ P+ ? if (!EFI_ERROR(Status)) {( I+ \) S- H6 \; l- L1 U% d
if (key.ScanCode == SCAN_ESC)6 U+ m; \9 I. v
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); {! J4 c( l; @+ V- r2 G
}
! G7 X9 S# @7 l& O4 M 6 z# W6 B: q7 O1 i
return Status;
: b" P& {1 V" h3 w( I! T* f3 _}* T+ M' j! p# X) o4 `
* O4 k; n: m: d% B* r5 ]
EFI_STATUS
" d" o( H6 ]; u# j9 V* bEFIAPI. u% g7 M# l# n
MyfileioMain (IN EFI_HANDLE ImageHandle,
2 B1 c, y& s5 R! W5 q; n5 U z$ M IN EFI_SYSTEM_TABLE *SystemTable)
: [2 u* L* V* p m4 g% k* w6 c{* x, J9 X3 E5 t- c7 O
EFI_STATUS Status;
6 c [5 Y, n1 v0 t. H EFI_HANDLE *DestAddr;
. a8 P4 \ Y/ o7 w EFI_LOADED_IMAGE *FileHandle01;
% _* u1 G0 o C" y5 I' R) [) X& Q EFI_DEVICE_PATH *FileHandle02; ( r B+ v; A' T: l$ m
EFI_FILE_IO_INTERFACE *FileHandle03;- P5 ?) _! A; O9 \/ N' @# k" @
EFI_FILE *FileHandle04;
2 p. r7 s7 U6 ? EFI_FILE *FileHandle05;8 w+ x+ Y( I0 I9 C: `2 L
CHAR16 *FileName;- \/ m A) K$ f6 ~
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
0 j( K/ [/ A- P+ e UINTN BufferSize = 8, BufferSizeKeySpace = 2;! e& g6 z' M5 X& q# u+ R1 W
int Bit0, Bit1, Key_Space; + D+ c. ?( R+ d2 l q
; V8 M( Y {' E4 Q* e" P2 j( V
FileName = L"NewFile.txt";
% ]) B, l# J' D: O6 ]+ j Space_Key = L" ";! W c- A- q4 ^2 D( D' A4 V5 R% V
( h8 \' U; f: N* |6 a- S2 @% c7 |
BufferA = L"ABCD";
" _ ~# `. B3 e' j* ~ BufferB = L"EFGH";
8 A; K0 H2 x, z+ o; i: ]5 ` BufferC = L"IJKL";$ R1 [- v% j- G# e9 ^7 }# O
( R3 O, G$ }7 }
Bit0 = 0xff;
. d+ u& |5 Q; f8 t ^9 r& C3 I2 r Bit1 = 0xfe;* _3 G5 L3 u8 j x, [4 Q; H0 m# ]
; O& c0 }* z! S) c+ q Key_Space = 0x0020;
8 O0 o' s2 Q* V G j3 S6 u
! _- x. L+ w2 e) j InitializeLib (ImageHandle, SystemTable); $ k* p, X6 G# ^) ]1 Y; h; t1 D
4 o8 [2 z. u j* Y' _* J DestAddr = AllocatePool (BufferSize); 1 D) s4 D; P% Z" u; q
4 U! U% Q, `% P. i/ f
Print (L"Value of Bit0 is %x\n", Bit0);! R( H( \0 d3 g5 }+ S R$ S! M
Print (L"Value of Bit1 is %x\n", Bit1);
3 b! l6 } E$ H1 z# _' ~: v& z5 G& [! z7 j
0 C/ @" S" T B S- }. r
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
) Z d- `4 }% _, M/ X5 l9 i2 p$ u if (EFI_ERROR(Status)) {6 w. e' ^1 o: ^7 K, N0 Z1 X
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);* n8 s; L! t0 s
return EFI_LOAD_ERROR;
' d' p, } ~% ~* r( ~; F! j }& _- y8 v4 W1 M
; a' H8 Y4 u; I$ I ]0 n! T Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);5 t0 @/ j. |$ u: d+ Z
if (EFI_ERROR(Status)) {4 u. C) ^7 b$ r2 \" {5 T: b
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);3 d4 G, \; D% r( l) C9 s
return EFI_LOAD_ERROR;) S! Q4 f7 Y, n; h$ v" b) [* N* ^
} . V8 i' m9 k- p# }. `% U
8 e+ D0 d6 v/ ^; q, O' c1 c Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);$ I, S1 h; I2 J
if (EFI_ERROR(Status)) {3 [, @+ @: W1 o
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
) ]% ]9 l9 |0 q8 ~ j return EFI_LOAD_ERROR;
5 l1 @) d) G1 x; j2 F }" [: _& q% |8 L% Y
7 x. c, _+ Y) Y5 E+ q, a8 r, i Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04); }& r( M. i( | J+ O! z
if (EFI_ERROR(Status)) {- T1 D8 L u% Y
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);9 H: b) V* M( x/ Z- m
return EFI_LOAD_ERROR;
* T. n/ z) Z. t2 I8 w } ; {& K6 o) s. p- f& x$ {' F* R& L
6 H0 ]1 z% ~- E5 J1 w Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
- P5 b: m0 R: `8 e) l! q if (EFI_ERROR(Status)) {
- ]% S+ O+ E1 o9 Q+ Y% C Print (L"Could not open file, FileHandle05 err code is %x\n",Status);) c6 ^2 w3 a! x l. m' s
return EFI_LOAD_ERROR;) E9 J: K4 O: a- @
}0 N; l. u' Q: Z+ E
6 e7 t# u& I6 q# t% W6 O1 B0 w' I7 a Status = FileHandle05->SetPosition(FileHandle05, 0);
6 u) U& g P* b5 I. M if (EFI_ERROR(Status)) {4 v6 m! f# k1 @1 t: b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 n% d/ m$ ?; v% Q return EFI_SUCCESS;
" @- B' @. \# i( c }
# S. y. l* h' Y: _9 l 5 q. h7 p! L: N0 }) g( g8 D5 V; Z
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);% I* @( f, h, ? X
Print (L"File Buffersize is %x\n\n", BufferSize);, p/ d) b; g" k/ {
if (EFI_ERROR(Status)) {: F( e1 Q5 b$ k8 F
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, `8 |7 j% I" P6 X9 ^
return EFI_SUCCESS;
! D7 J, Q5 w& n# R. ~: D& b+ N } ; ]. Q* p% G# g6 N
7 M E2 E- s$ O+ F* H1 [$ c
Status = FileHandle05->SetPosition(FileHandle05, 1); ( l8 V4 l5 r7 M' [. s% e# G
if (EFI_ERROR(Status)) {
4 }* W9 g1 ? @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; }' x5 b8 V2 f" {9 l
return EFI_SUCCESS;
$ i+ r! ~9 P- ?0 p* m }
1 z. g; p z& z. A
! e# V/ ]0 v% d Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
j: A0 B: [! Z# B Print (L"File Buffersize is %x\n\n", BufferSize);
8 N3 ~! I/ [; a if (EFI_ERROR(Status)) {
2 a9 Z' u% r: Z) u Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);% B0 D1 `! ?' v" D
return EFI_SUCCESS; o7 c2 q* X5 E7 x+ J( D% }
}
2 e7 H0 n* X0 U- [2 ]7 w' H
1 v8 ]# R% ~+ r4 ] L6 K# C) N Status = FileHandle05->SetPosition(FileHandle05, 2);
0 Y5 k; {3 c! g0 L# r if (EFI_ERROR(Status)) {
; n" [9 q9 v+ {) P+ ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 I# U7 w7 l4 D9 }
return EFI_SUCCESS;! }" g' n6 i" P5 I+ l
}
: t3 f O* ]8 r) x4 u" ], N
# ^$ n% N/ |9 r( N) O- e6 U6 ^1 { Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);) S, c5 `7 k, O8 P! G: N: M
Print (L"File Buffersize is %x\n\n", BufferSize);
, _$ E" ?, |0 e# V if (EFI_ERROR(Status)) {
. |2 @2 t- m6 @: B$ J; h Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);- L' Z- W# f4 X; Q
return EFI_SUCCESS;
6 G2 v' i0 l9 K5 ~% v5 U/ C }
! r- |- X( B9 {
6 G3 D; ~1 y8 W& [: f$ ]2 c* H//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
7 I- J3 D1 ?. n+ Z7 S Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);" d. R- h$ ?; w. }$ j4 T9 t( u
Status = FileHandle05->SetPosition(FileHandle05, 2);
: M8 x% v4 S' l I& U% j if (EFI_ERROR(Status)) {. F5 }* J8 X# V! n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& g4 l7 R/ P$ j9 x! ` X9 D1 B return EFI_SUCCESS;3 a% M4 u& G+ p8 {% O/ Q
}. H9 q4 A) |6 S7 X. T' O
A' _6 U! ~) {0 @+ ]6 b! V; f& Z& p
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
# b$ ?% ^( S/ o( A% X2 u if (EFI_ERROR(Status)) { y: u: P, c' W9 ~; O5 {( p3 ]( e
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
. |! O) h" F1 d6 b: w return EFI_SUCCESS;
; Y' |+ c0 H* L& w* D7 A( h4 ` }
0 ~9 r3 O+ ^# n9 ]) v) c8 `4 U Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);4 P9 s* k, o2 F! c
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
5 s: r' i0 u3 A4 L( g6 ~# S
8 A( X" o& i5 Y0 Z& H Status = FileHandle05->SetPosition(FileHandle05, 10); 4 g+ m. s4 O- ?" H
if (EFI_ERROR(Status)) {
) C# `2 x* ]7 x9 u( I/ h. l2 q Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: x" j. Y) D+ l7 E$ b) C
return EFI_SUCCESS;
& _# D$ H" N1 w A$ ] }7 k0 S4 |2 O" d3 k: {$ u( x9 v
+ ]* [! I4 Z0 f% [ q S
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
; \7 a7 `1 ?' f8 g6 L if (EFI_ERROR(Status)) {
$ N3 W8 u: ^" |/ k0 I Y3 i Print (L"Could not write file, FileHandle05 err code is %x\n",Status);6 Y, e& c4 } V$ r
return EFI_SUCCESS;
9 p9 [" G9 S! U9 H }
0 i7 a9 o% [% U/ a7 _3 M a* S
+ {5 ]: Q4 J) ^8 i7 S& M, M Status = FileHandle05->SetPosition(FileHandle05, 12); - d) r# o: r& |6 Z3 w2 `: ~" ^1 e
if (EFI_ERROR(Status)) {
3 ]0 g5 k$ U' I, c% _, y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) s, ^' V3 X: Y% W, D" T return EFI_SUCCESS;
& r4 m. y7 W5 {" V3 | S1 q }$ x4 D( j% \( D8 `9 j# T9 k/ Q
3 B7 D, @( v3 \; n0 Z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. x" t. U" w& R- f: F/ b if (EFI_ERROR(Status)) {, J+ o, b* ~3 P* {) C" ?5 C. n7 b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);. j2 D6 f5 g2 l
return EFI_SUCCESS;
( ?- v9 I+ \! C7 L) }$ [1 t0 [' s2 d }
/ `+ V/ q6 n. Z! @0 E8 u" U7 y% u6 h, i$ q% ]9 ?/ W1 L
Status = FileHandle05->SetPosition(FileHandle05, 14);
! R* o2 s$ J+ _4 X K if (EFI_ERROR(Status)) {
7 o5 L/ m5 T1 R! q; ~8 z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. x2 Q, R. t. Y$ o! I2 V- m9 n return EFI_SUCCESS;
( K( P4 h9 G- Y+ r d" y9 F8 G }7 Q; t0 Y& @4 F
7 A6 n6 T3 U: a' a! v Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
" z8 @) s: d$ B, t. R if (EFI_ERROR(Status)) {
! O! h7 P1 p/ q9 J( G. ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ @3 [9 ^7 v0 [# b* |
return EFI_SUCCESS;$ o! x3 w# k! `2 ]
}
& o! |. l# S( S# z6 l
% f5 ]. _% t8 t5 S Status = FileHandle05->SetPosition(FileHandle05, 16); , x& p' k8 \- T4 V$ v5 U
if (EFI_ERROR(Status)) {
* K( X4 D7 P, \+ O$ Y) G Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- v$ X4 a: b/ t7 a/ }
return EFI_SUCCESS;% ]+ \. Q* y- x6 v4 E( g
}
& p r% M( o1 s( q3 [
: u9 O2 [( m" A( h4 }" l, |! ? Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( n0 Z/ a5 S8 W2 v$ l9 q
if (EFI_ERROR(Status)) {9 O9 b$ H1 ^* B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% x4 A9 d7 I% j" U, I return EFI_SUCCESS;1 c( B! N1 N. p7 i- L
}
" E( {7 o2 O y1 \/ R( @9 F& x
3 Y; x, F- x. ]//---------------------------------------------------------------------------------------------------------------------------------------------------------------
b8 L! j# @/ s4 t# x9 M
7 t. b' E9 W+ G' F0 x* O' K# U9 y; p Status = FileHandle05->SetPosition(FileHandle05, 18); ) @1 R& q. ?! m4 G7 \2 c' t2 r! L
if (EFI_ERROR(Status)) {
9 h# N z6 d$ O1 ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& U9 L; i! z7 H" o* \
return EFI_SUCCESS;& D- v' F# \, T. ~- X+ ]
}# N0 I8 E1 o3 L" F& I9 Q. D6 r
) u. L/ d/ f) Q Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);% _7 m+ \6 A- m, B% b
if (EFI_ERROR(Status)) {- O9 L: `+ t" V* h9 u! b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 V! [/ L$ m) G. V( L2 u
return EFI_SUCCESS;. O/ o9 N" f( |( F, t0 V
} % e9 t% T9 p- G+ ?! a- O
1 M4 z; }1 X( r2 Y9 r
FreePool(DestAddr); 8 f8 g& ~! o$ b3 Z, t
5 y5 {, g4 \% V z ^, {4 m
Status = FileHandle05->Close(FileHandle05);9 a( L+ M9 r/ D+ {. {" D: l2 _) M. u
if (EFI_ERROR(Status)) {
: v: h, Q! t! B$ g. s Print (L"Could not close file, FileHandle05 err code is %x\n",Status);1 ^ f* Y. m$ p
return EFI_SUCCESS;
# G! J9 k9 I9 A$ V0 @# u/ Y- n }- l- e& k2 n( `" R) E
0 w" c' W! C3 C7 n* ^8 s0 M# W) D
Print(L"File Name = %s has already been created.\n", FileName);4 z: N$ B* G1 b) I9 S+ U% ^
V! n& T" p! o) S# c$ m& z3 p- y+ ^ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
" U) g! P, ~7 v$ x WaitForKeyOrReset();# b. z; ?3 Y2 @1 D
( W. d, s4 U K4 Q* i
return Status;7 u/ `% d2 ^/ b. G/ c+ [9 f
} |
|