|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
8 t+ d# k% k, b- Y6 ~. \* ?$ Y# Q* t * myfileio.c
! k9 `( H% Z: d/ O+ b4 ` * Apps6 y6 D0 v0 T8 |
*/ o8 F6 x( h$ Q; x
w9 o' L/ }% x" x$ _6 k- m' i#include "efi.h"
) O2 z& m7 k2 M; u: M k. j#include "efilib.h"8 v9 |. Z E7 I, a8 R+ S+ `6 n j
' W8 `1 d% @$ E4 A#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE" T$ U% Q4 ?2 }: D/ B% ]
& j* J! x% p- b+ X: I2 u
static EFI_STATUS WaitForKeyOrReset(VOID)1 n2 [% c8 \* J0 m
{
5 y* \7 _/ ^, [/ N EFI_STATUS Status;
( w5 |$ A0 |/ q: z# s* y* k+ J EFI_INPUT_KEY key;
9 n/ ~( g: F! D' O3 x" \ UINTN index;( |- H% X- g1 L2 X
1 r- T! d5 G/ S+ n for(;;) {6 n! f+ @; E: u9 X
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
. p9 A3 Z( K% l- Y8 @ if (Status == EFI_NOT_READY)
4 ?8 ~" T1 Z) K6 i* f o BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);+ r: n: I# w! }+ W* t
else( `% j# r% _8 n& E4 m3 O7 B
break;
, x8 y" T# U; s' F# X }) n9 }! V3 O0 t- e5 E( y! R
if (!EFI_ERROR(Status)) {
4 d T8 _; ^6 i! }9 \+ U$ R6 |8 b if (key.ScanCode == SCAN_ESC)& t& q6 u, A3 m
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
: X: Y( A& c7 b }, J2 W# n* S: W; D- O
$ K4 E$ K9 d/ h* n3 B
return Status;
* S# u( v$ H6 p7 G- Y% p}
1 \ o$ C! q) ?1 ^4 O7 q/ K; v1 ^
EFI_STATUS% ^2 X' L& e4 m, J% Q
EFIAPI
) W% s- p# I# |2 RMyfileioMain (IN EFI_HANDLE ImageHandle,
/ w* m. d* y9 _) ~ IN EFI_SYSTEM_TABLE *SystemTable)
3 h, M# y2 R& l# b# q# l6 B7 y{
8 H* ]4 D2 S; h EFI_STATUS Status;
, f/ s4 v( Z% x/ K( D0 ]9 g EFI_HANDLE *DestAddr;
. Z3 ~( b# L4 ] EFI_LOADED_IMAGE *FileHandle01;
% v K/ _+ O2 ^9 J, S. a EFI_DEVICE_PATH *FileHandle02;
0 y$ I4 \' i' K8 L% h# M EFI_FILE_IO_INTERFACE *FileHandle03;
) N$ R8 Y) X# `+ ` A8 D EFI_FILE *FileHandle04;
0 u, a% d @1 j3 S P EFI_FILE *FileHandle05;5 k. _3 I/ O4 b8 a; c) g
CHAR16 *FileName;
! r8 E a3 H. @( r& H e7 v% r CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
5 y0 D2 b# v: u UINTN BufferSize = 8, BufferSizeKeySpace = 2;
4 a- B: C7 h K+ B- F int Bit0, Bit1, Key_Space; 0 u1 B& u9 F( t& ?. {5 l
( Q; d/ \0 v/ ~# o4 ~; Y
FileName = L"NewFile.txt";
S' L, |2 t+ f9 F+ ~( H* x Space_Key = L" ";
1 G' S0 H8 Y/ D9 _6 N+ n
. i. t. l% X% P$ y/ e BufferA = L"ABCD";; [4 H% Y7 n2 R! s4 r+ M& D
BufferB = L"EFGH";4 n2 E, |( S# m, q, e( _9 S+ ?/ E5 U
BufferC = L"IJKL";
M1 y3 S, Y1 p# M: l1 ^ U, Z' U' J1 s, s) C# e
Bit0 = 0xff;. K4 t4 g6 k) A9 o
Bit1 = 0xfe;" r- b( T! O, Y/ m9 p
0 B' ^' u. `; P4 C+ N- \( D Key_Space = 0x0020;
; _ s% y. z3 I& ^* n m7 h, L - r+ `6 w5 T) X
InitializeLib (ImageHandle, SystemTable); + L( B8 A, G# P3 w' r- i5 l
' I6 t3 l4 g$ ]6 X DestAddr = AllocatePool (BufferSize);
! H- }, x+ v5 E; g; l+ B0 {2 ]# s2 q* h# f" X
Print (L"Value of Bit0 is %x\n", Bit0);! ]4 d7 {6 W0 F7 Y _) v& z; P4 c
Print (L"Value of Bit1 is %x\n", Bit1);
8 f& ?. r/ {' G5 q& {4 f& d# T. E8 y, z+ c& g, c6 G2 z6 u0 A* Z) [
6 R2 I y' V4 q0 C$ e
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
! x: r7 c m$ }" m. I if (EFI_ERROR(Status)) {) I; _+ V" _5 |! E
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
$ n+ g. O5 j8 p2 c# Z: b return EFI_LOAD_ERROR;, o/ w% x* z+ W; j, s, V
}
; r; K5 `3 y, }1 W2 B8 k L7 u) w
Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);' f/ L, q+ [, I1 d* b5 o
if (EFI_ERROR(Status)) {( w/ Y6 h3 g8 M$ Y% S1 r: c: `2 S
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
( Y; T8 X2 [/ ]1 G4 g2 e return EFI_LOAD_ERROR;3 X# f6 v U; _4 ^1 J/ |" C- l
}
0 I; X2 V$ E( `2 n4 J ( q8 E4 v b; _( D: a4 s2 g! h+ N
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
: I; O. M: a8 _6 R# ]9 V( \, Z0 n if (EFI_ERROR(Status)) {
" S; f* N) r' T$ ], Q- T$ g1 z3 X Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);8 Z' u$ B' [, Z0 j
return EFI_LOAD_ERROR;
, O, r3 N5 N- D7 M }
2 r. m1 i' `" C8 w4 T j4 N7 g3 B; ~% l
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);) {8 E/ b1 Q8 o$ B n3 ^) l% u8 W
if (EFI_ERROR(Status)) {
; u, n& Q$ J$ U6 z Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);, V0 i' Z4 W2 ^3 u- u: t$ }
return EFI_LOAD_ERROR;
+ s- Z* C" ?; O3 K; o2 V } , z2 H$ y6 M3 D- M$ W p! ?: ^
. x e) r9 O2 K( l& d; { Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);* Z9 g( j1 |8 ^$ s* O5 G5 x
if (EFI_ERROR(Status)) {
6 _$ _1 V! n: S Print (L"Could not open file, FileHandle05 err code is %x\n",Status);5 X& v, ~/ P1 I& u2 G) y
return EFI_LOAD_ERROR;
; I l! d R3 m& P+ ~ }
4 }* m# \ R% s% H b5 t 7 F* V3 g9 n e, a( h* R
Status = FileHandle05->SetPosition(FileHandle05, 0);
( O+ m, E! r- v& w if (EFI_ERROR(Status)) {
& X0 \) g3 {: b* z: A Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' G9 F9 E; ]! L
return EFI_SUCCESS;% s) h4 x1 x2 z9 `' r6 e( D
}
$ Y* h# i7 {8 V- O
& U* M! e" }: u" P Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);0 y7 ]8 y+ `! N; p4 C* m
Print (L"File Buffersize is %x\n\n", BufferSize);
0 E8 Z5 Z# y8 e$ h6 ~ if (EFI_ERROR(Status)) {
6 u$ Z; Y- C0 i, \ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);" j% W9 U2 d$ k! u6 p
return EFI_SUCCESS;* j$ _9 ~- R4 l. Y
} ) u% q8 J2 H5 s1 M
% ?' S& C( }5 D6 L8 h/ |6 B2 G. h
Status = FileHandle05->SetPosition(FileHandle05, 1);
0 ?+ t; `; g9 J6 C( ^, G% @9 I if (EFI_ERROR(Status)) {0 p- u& n& P8 `
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 c/ Z" b( {8 |8 t" W) C$ H return EFI_SUCCESS;; A2 l" r- Y& q3 U4 _. y# |
}
5 t; f6 E8 R0 K+ Y! e$ J
* ^( t& G% B& ~ Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
) `; T/ o' ]. T) `7 {. n8 t t Print (L"File Buffersize is %x\n\n", BufferSize);7 U6 K+ w# s/ ^" ^9 H W1 z
if (EFI_ERROR(Status)) {
& |' W" l, }9 F Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
8 J/ [# X# N# G6 p2 u return EFI_SUCCESS;/ \; R, q0 u, f4 j
} 8 Q9 i/ k3 c. t* m4 u) O! l
$ l) x5 u; k9 F
Status = FileHandle05->SetPosition(FileHandle05, 2); ( w/ ^7 k1 N2 [, z) m( _8 H
if (EFI_ERROR(Status)) {
1 a _9 k& Y) j2 g, M# s F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* e3 p* W* {7 B0 N/ j) y return EFI_SUCCESS;3 w$ p" z2 K" L) ~- i( Z1 H( ~& f- M
}1 Q2 K) n2 }9 X& V# }
' C) y5 C/ c6 L% U) C4 C
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);7 [) ?' H7 M# K8 F
Print (L"File Buffersize is %x\n\n", BufferSize);
& y7 a' r& {, B& h9 N if (EFI_ERROR(Status)) {& E* s Y/ p9 q( z# y C, i# H
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);7 H a [1 l- j: Y+ E j
return EFI_SUCCESS;
: W F& f) {+ H( m4 M, G7 e+ p# V }
6 p3 Y5 k3 o2 A: g2 J5 g 3 E$ u) i6 E8 ?7 P
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>. b3 s, n2 Y% r7 V
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
U% E9 j* W, i* e$ W0 D Status = FileHandle05->SetPosition(FileHandle05, 2);
% ^& H! A8 p5 U if (EFI_ERROR(Status)) {! Y) x. u/ B2 [) Q( I
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: @+ l9 ?* d( m2 k" }; p$ B5 h. u return EFI_SUCCESS;' R3 ^- z' G- q. Y' W7 x' ]
}
$ l) ~# S& D( g% k+ ^0 G! D- t$ H7 A/ l1 v R& x
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
# e1 D. l; c" x4 y4 Q& W$ y( l if (EFI_ERROR(Status)) {: b) ?$ n u! L( x, L
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
4 T+ |7 S% r( E$ X# I4 {* }; R return EFI_SUCCESS;8 @" Y9 |4 b+ C2 t8 A3 P' d
}
8 N2 n1 i! J7 [2 h Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);. ~6 x8 D) ]5 h* _* C# ]2 r6 ]
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
* Z8 s) `9 K; A/ O4 `/ P E+ U! C) D
Status = FileHandle05->SetPosition(FileHandle05, 10);
% u; V! i- _" Y3 F9 F; }/ [1 K) I if (EFI_ERROR(Status)) {" `+ `1 e: Y9 v( ]' u2 X, }3 r
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" ^ _/ ^7 Y& H( }$ ?' d! X+ ]
return EFI_SUCCESS;
1 t' k& f1 [9 h; U9 E4 a } \% J/ z- n9 K
/ J% P2 Y: S! S; h: X( D
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ w8 t& |9 ]) u- E/ @ J
if (EFI_ERROR(Status)) {
2 l% g9 `/ Y+ a2 S% h9 [* @8 H$ [ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 u: i. Z7 e2 ?. V/ |! Y; J$ \
return EFI_SUCCESS;0 L* P* ^4 |' q; ]& Q
}0 c) k! A* t; \" q8 S! Y
7 I) u+ g/ x R9 B ?$ f* u Status = FileHandle05->SetPosition(FileHandle05, 12);
0 I8 g/ Z! d9 ?) g7 \) H0 a5 f if (EFI_ERROR(Status)) {
. [4 _: c' o/ c( y& T$ a+ R& G2 E0 o Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); w: u4 ]8 A. T& x- p/ x& J' ~) u+ O
return EFI_SUCCESS;
7 v- ~& H- J! S8 [ }
2 H# E) P- O: q" ?$ { N 7 Y) m6 v. E. I
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);- Q. Z* V/ {" \
if (EFI_ERROR(Status)) {5 _' Y0 `' m- P; W, O
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ M6 U6 O4 m o) t7 ^, K6 U return EFI_SUCCESS;2 k& l/ T! t, k* Z6 J" r8 E- d. ]
}! d8 n1 i( e$ x, u7 U
6 i: S* R. Q% P2 u
Status = FileHandle05->SetPosition(FileHandle05, 14); 9 Q4 N! T4 i; B& e2 W
if (EFI_ERROR(Status)) {
. j+ ? V- Q3 P J& F' I Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 L" o0 N9 T: p# W
return EFI_SUCCESS;4 j. ~/ S0 g5 o0 P
}. h' L/ z8 G# D6 o( A
4 [/ G D! A" H! ?! w0 N
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
% d% o' ~/ S9 I& F) x! } if (EFI_ERROR(Status)) {
6 @. S# y* f7 J& j% q. Z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 l& b8 b6 k; `5 K3 G4 q# J( E9 c
return EFI_SUCCESS;
+ m3 B: D$ A6 V$ k9 O) I/ q* O+ \ }
: n8 {/ e" S7 w8 G; {, p! N2 ?* e2 x) ^) H: L/ W1 k
Status = FileHandle05->SetPosition(FileHandle05, 16); * e) `) t* s/ J5 I3 C
if (EFI_ERROR(Status)) {
5 J* q4 V8 \7 e& K- u Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% [5 z% ^/ c. S. D
return EFI_SUCCESS;: H( C. ^0 B, R
}. y6 A2 u+ s2 Z8 d4 K& f
. j' \& z! M- O2 F# n( D' Z
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);4 z+ O+ b" P' A6 l* [2 j
if (EFI_ERROR(Status)) {/ L9 C$ L" ?% O4 S: B8 z! q: {- ]
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
+ I8 {/ O/ y% L$ H7 l4 L return EFI_SUCCESS;/ F' |7 {, ~5 S
} ' Y: i0 ?, G$ Z8 F( @
5 I/ B/ m( z5 V//---------------------------------------------------------------------------------------------------------------------------------------------------------------/ } T+ {/ `- D
1 e- x- Y# [% x! @- ?/ x
Status = FileHandle05->SetPosition(FileHandle05, 18); / B N" T0 L t/ o& Y7 B
if (EFI_ERROR(Status)) {0 z3 L1 N# s+ \2 e( m7 C! H6 \
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: }3 P; z* Y6 p, ~ return EFI_SUCCESS;
& |0 J1 |$ F, Z/ m5 ^ }
! U! i( `; e0 ^6 e2 l5 r
% @3 j. F/ T ]& m5 `5 d: ?7 s Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
# ~6 q/ p. u" b if (EFI_ERROR(Status)) {: C$ ?9 i6 w V7 y
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
* c5 R+ c( q4 k2 l p4 y return EFI_SUCCESS;
7 f8 V: v, i! j+ N } $ `" }5 F5 m; p: K2 S- ?5 d$ ~/ H
7 x5 s$ a9 K- l FreePool(DestAddr);
9 P3 Y4 ^4 ^$ F& |6 k, W. x) Z$ {5 r F3 Z6 o' d+ G& z: Y* B) t
Status = FileHandle05->Close(FileHandle05);
: |' k; o7 l9 p" O! K if (EFI_ERROR(Status)) {
4 `& y: o' z. y; v4 Q+ t6 a Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
# t. a8 y9 |. l5 J7 \) Z9 c7 Q! G return EFI_SUCCESS;
- m4 `* u4 U# c7 c+ t }$ d# F, i! {; V2 G, X3 V/ }
+ G- H# ?$ ^' M$ |
Print(L"File Name = %s has already been created.\n", FileName);3 w4 Q2 O5 n; \% W6 n
" E& S' x Q1 `2 m
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
: p2 T! n$ f2 z" D* J) R9 v WaitForKeyOrReset();7 N! D. k* }5 M) B! O* u% e; ]5 S
- z- B Z' H* b! S( B" {$ X
return Status;
' J& G: D) t! i' ?( r0 @} |
|