|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*8 _) _7 L* m8 S* p( j, s" P# j, l
* myfileio.c; H% k" A6 s* _1 V" M( {6 s
* Apps
, \7 [0 V" i1 m' E */2 ]2 p- Y/ E) u/ O1 \6 j( q# c! {
6 D% ^, J# M U7 f# w A1 ?2 g
#include "efi.h"
% e' q; w8 e6 F#include "efilib.h"& c( l$ [# f: O* U- S6 q: Y8 }
* I' ^5 z* g4 @
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
@+ T( O+ y; i+ |3 l2 L% t0 J7 v5 M9 |1 O: Y* S6 C3 O
static EFI_STATUS WaitForKeyOrReset(VOID)2 x4 i5 M: p2 w3 `4 Z& B
{
0 f( i [' Q5 Y EFI_STATUS Status;
3 ~6 ? L5 m6 s3 |2 k. S EFI_INPUT_KEY key;
7 m; S1 w4 U* v4 h UINTN index; M6 E& w7 `* E& x' C3 Z e7 S
: B K) `0 \! c$ T for(;;) {* B* c$ w5 p+ k, i& {8 h0 D
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);2 I/ t( q+ U! O( S
if (Status == EFI_NOT_READY)$ D. w6 g: v. E# y
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
% n5 H5 ^/ W4 E2 h. k$ G# I else8 X( Q& H* |$ I1 y- O4 y2 t! i8 p
break;0 I) v% y7 w9 w X0 o* c) F
}
' H" J0 a( _* [0 g if (!EFI_ERROR(Status)) {
: X0 |+ |' {* [+ Z) a% y8 D if (key.ScanCode == SCAN_ESC)
/ q1 F! Z! b1 }! ]& P' ~ RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
# L; n) q# \7 `$ U1 U' U }# G7 w a, i/ z W h. V
: ^2 K( b! Z L' M/ n) |* [3 Y
return Status;
. H$ W! g, }+ h" G2 g$ B/ B1 @ s( e}
- u1 J" Q0 ]9 M9 Z& k- {5 w4 x, Y) u+ N+ s g1 r
EFI_STATUS
, G9 m' K s; F2 e! _EFIAPI
5 K- Y' N2 @) u: GMyfileioMain (IN EFI_HANDLE ImageHandle,
6 r0 a8 [# B$ }) T3 P0 p! E) A IN EFI_SYSTEM_TABLE *SystemTable)
5 Y8 d5 k5 u3 q3 {8 i* X2 k{, k# z9 F+ m, Y7 y/ C) G) k/ F. W
EFI_STATUS Status;, _ `+ K7 f/ A- I N% |4 k/ w
EFI_HANDLE *DestAddr;
) M( v, q5 B, O, g) V. j4 T/ T& j EFI_LOADED_IMAGE *FileHandle01;
* ?# z. U) b8 u EFI_DEVICE_PATH *FileHandle02; # `5 m! y$ C! i9 \& h4 f6 w
EFI_FILE_IO_INTERFACE *FileHandle03;
1 U6 m" A0 s) u6 O$ m EFI_FILE *FileHandle04;
/ Y7 i( ^! V% Z- w u1 A7 ] EFI_FILE *FileHandle05;* j7 l5 p' L: i' D& K" r, ]
CHAR16 *FileName;
* o; r( A+ l3 T, e6 x# q# f+ T% x# x CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
1 B# I3 w" N+ B& Z3 r1 _ UINTN BufferSize = 8, BufferSizeKeySpace = 2;
9 n7 D; ?, M# n7 Z: L, g0 \ int Bit0, Bit1, Key_Space; , c2 _# y I8 D
, w! s2 f; `* N3 X4 j# i1 P FileName = L"NewFile.txt";+ ?3 M0 N" s5 M1 T2 U
Space_Key = L" ";. |# c# v; F' A; O' T
7 n: T( j/ e& \7 J" O7 S+ p BufferA = L"ABCD";* U# Z! i" D* i! E- h2 a( T1 l
BufferB = L"EFGH";
9 ?/ y, n, n# F+ l, {0 p7 Y BufferC = L"IJKL";' Z9 D1 ^. l+ m$ K% U
4 k9 G' L- {2 C+ c0 R9 B- @( M; y Bit0 = 0xff;
) r1 D2 G. e( g" W" @0 q. ?: T Bit1 = 0xfe;
" b! ?: v7 h9 n# `
- l/ w0 l. B. `$ y3 w' U Key_Space = 0x0020;( H& X9 I% _$ Q0 U K1 E
" c5 G: P6 V% U! o3 r# ~ InitializeLib (ImageHandle, SystemTable); ' r8 J: r# [/ U7 t7 P6 T8 ]" w+ d
, L6 q9 ~7 L, K1 W; W' s
DestAddr = AllocatePool (BufferSize); 9 X2 z& h+ f# t
, H$ [; }' K) U5 S Print (L"Value of Bit0 is %x\n", Bit0);( [, o1 b9 o/ f/ w
Print (L"Value of Bit1 is %x\n", Bit1); % n2 K: Q/ K. b2 N, E
2 g* i2 \9 c4 ^
/ L4 V( W2 d# |2 p1 i2 p Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);" K2 ?8 f: p& M1 ? ~
if (EFI_ERROR(Status)) {
; Q) m* h( n9 s! P+ G Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
& [/ y8 N0 E) p/ e return EFI_LOAD_ERROR;
, S$ ^2 J1 @ y; i- _ }8 s5 U6 `" b) w, h2 N0 W$ _ ?
5 I4 |' u7 f* g: j0 O Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);4 t4 o" I3 v( K5 x
if (EFI_ERROR(Status)) {+ h2 Q0 c+ Z3 `6 y$ g$ T
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
: [' i# l2 ]6 `& C, F return EFI_LOAD_ERROR;
" v/ l0 h3 L. g' ^. o+ q } 7 g' g$ x; S( G" Z% v
& L# `! @. ]7 R" F& q7 w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);8 S: C8 G& e/ g& F
if (EFI_ERROR(Status)) {
; m/ A3 b" L @6 |8 O" T) I7 U' _ Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);- U8 w _2 V2 _9 }/ Y2 _- H0 d2 A
return EFI_LOAD_ERROR;
! o; y5 U+ y# B }% j( n3 B1 G* z0 }( w- h' O. s
( O- j( N: g. c- a; W0 ~5 b
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);. ~7 C( o5 i- N3 n4 W
if (EFI_ERROR(Status)) {
5 d& q" d8 t0 ?$ W Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
( b5 ^* B+ w& m9 I( R return EFI_LOAD_ERROR;0 k( g M0 c% T( P) R
}
2 d9 A% L/ e- B' e- E* ~
+ x2 w" Q7 @1 F2 _4 K8 [0 I' M Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);! w) Z5 i' N( k" F4 `
if (EFI_ERROR(Status)) {0 \; H6 J- f! Q8 f
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);. F$ Y6 ^+ j& Y& e% @$ Z
return EFI_LOAD_ERROR;
2 r; n3 h/ {5 V+ l# A }1 c+ G; p8 D& b$ ]6 w
t1 X/ t! _" H
Status = FileHandle05->SetPosition(FileHandle05, 0); ( U+ [+ q# X/ K- z+ j9 G
if (EFI_ERROR(Status)) {4 d$ Y8 h+ V0 N( J: _8 J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
, C) c- N3 G; }$ Z/ c5 T; N; g return EFI_SUCCESS;
3 L* a; D. f: h- A* C1 e }
( j, d) d3 O0 g5 a ) s" f' Z: Y5 n9 ~7 M
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);2 h& e) a: G0 r6 K( Y1 @0 L
Print (L"File Buffersize is %x\n\n", BufferSize);4 T9 b R" q' B8 y: Q- u" J
if (EFI_ERROR(Status)) {
: |( d( n8 U: ^- O+ A! J Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);, t- H% B8 M4 M2 P
return EFI_SUCCESS;
1 X* V/ q4 i4 v: T7 T } ' H% o2 N+ G }" G2 ~
- A1 w& r8 i ]6 g/ l# C Status = FileHandle05->SetPosition(FileHandle05, 1);
' l5 j& C" l) A, X0 N! f/ f if (EFI_ERROR(Status)) {
% R, I6 [' M% z- P1 i; q' a& m Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" v. Z" |$ ^! P$ v( w: `
return EFI_SUCCESS;4 j& A& y5 |4 r: b+ [3 d/ |
}
+ v3 _8 Z, N' ~" W+ u1 }0 g h + |1 j8 N5 z [, w
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# ^2 ~5 \$ u4 W1 ~ Print (L"File Buffersize is %x\n\n", BufferSize);$ N7 V& B: |$ `! C3 `! e4 }( O
if (EFI_ERROR(Status)) {
$ y; q9 Z$ p, n! ~ \' m; s2 w Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);3 V$ _- R) d" p9 L
return EFI_SUCCESS;6 I- ~/ i. ]5 P* T
}
: G9 C1 [" y: M1 F' n# x P9 _; x9 G- h( k! P3 j& `$ j
Status = FileHandle05->SetPosition(FileHandle05, 2);
; g; [3 K1 v) m1 ^ if (EFI_ERROR(Status)) {
% P$ |2 [, T m! x4 i/ w; z& p Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& e* h3 ?: }' I+ p, R9 X- N
return EFI_SUCCESS;
* D. O9 ?' G' [4 A" V% U; C }4 t0 U5 X# A, ~# X8 `2 i' K0 b
# t" S+ {8 L' l: X1 U Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);4 K/ e4 X1 {* J) v6 j
Print (L"File Buffersize is %x\n\n", BufferSize);( B. a/ N& X- L% I6 k5 z
if (EFI_ERROR(Status)) {
z# B0 z+ ?2 h, o# @) I$ O0 W Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);. ^# b, a6 q3 Q6 J; k* B
return EFI_SUCCESS;) g) X ?% q! ]% v' Y/ D, N6 b
}
C- w2 ]- c3 }! P$ h9 g 1 k' t( [ [; a, b ]& {: d
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>$ f! R0 e) {1 i j+ f# l% N
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
8 \: k" t2 g) Y9 n" S Status = FileHandle05->SetPosition(FileHandle05, 2); 0 R: I J5 `& l
if (EFI_ERROR(Status)) {
# z. e) u3 q2 \! b9 w6 |! Z) ], z( i Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% O# r- d) \% E9 G6 `1 f2 w, Z
return EFI_SUCCESS;
& V1 _0 c& H0 ?# _! | C }1 T1 y7 T3 e* o1 O8 A- E
: l' e0 @) T, ? ~2 Q0 Z
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);# ^/ c, h) E* M/ S
if (EFI_ERROR(Status)) {+ ^9 J8 ?( S$ e. s) o
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
6 K0 B1 [/ U) ~6 n: O1 n return EFI_SUCCESS;
}# b0 P/ l- P0 U$ F } 3 ?' ~6 d* ]! |: m! g
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);9 n6 G% |8 d& X1 v. v7 Z$ ?6 s
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
q; {. Y ?# d' O+ S! \* A8 F# l, Q8 X2 n# Q/ W$ R% E
Status = FileHandle05->SetPosition(FileHandle05, 10); $ Q3 k" W( ]0 R2 R" a4 u
if (EFI_ERROR(Status)) {. T5 p5 a8 ?/ u7 t& _! p! {* |* k2 Z
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 ?. S3 T- _9 u- g" T% l return EFI_SUCCESS;
/ x- ^- U& A2 w. S. Z1 M }
( d1 ~+ q5 L4 ^, v6 ]5 U
3 j9 Q* W- P" _- z5 `% O; z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);6 w% @$ K( p+ }4 Q/ \
if (EFI_ERROR(Status)) {
! ?; ^- F u4 x; o$ I) o Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, k+ w2 o" Y( _. g( } return EFI_SUCCESS;
! k# G) L& { E7 c2 n9 P3 ^' u }; s0 u( b4 t! i! N% K6 u; I$ ~
2 x5 t, P! g; c! N
Status = FileHandle05->SetPosition(FileHandle05, 12); " Z- z( i$ r+ B, n* @ t
if (EFI_ERROR(Status)) {
9 ]- A% H, S+ M+ }. W4 J; b Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 g7 `( C1 ?: H1 y% x- p4 z5 j; A9 r return EFI_SUCCESS;
; T5 `0 u8 ~6 `" H }
: C% p' `( t' y9 J; _+ Q% {4 u
( Z1 {3 r6 U& X: T) ? s8 Z4 a& B Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 {) f& w6 G/ y9 s* C
if (EFI_ERROR(Status)) {( y/ U+ l' R. }: `) D+ k- _) b; c
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! E4 O6 G3 l& a# }8 Q; d" X return EFI_SUCCESS;
% k6 P; X. d4 @4 D# c2 D# @ }1 y3 N& V* {5 F8 {2 l/ j* f: x
0 O* g I8 u. I& z9 ?1 p( G
Status = FileHandle05->SetPosition(FileHandle05, 14); - R6 z- D4 t% G
if (EFI_ERROR(Status)) {! x+ ?" h6 Q/ ?' {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
3 H% ~7 {; i0 L! S9 o6 Z C return EFI_SUCCESS;
( ^" v- B' G; ]: M8 u }
& u: ?3 P7 C1 ^- h! S, ~0 e
9 r+ R. v3 b4 } Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
* J5 V: Z/ T, g& w/ i if (EFI_ERROR(Status)) {7 B/ {# [, O% A' t- b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ D9 u5 i# a/ {/ S return EFI_SUCCESS;
5 C8 B) w* L& X }
) z6 f k% l" K7 I6 O( g
( _+ ?; r/ l$ [/ ]1 |& A" o Status = FileHandle05->SetPosition(FileHandle05, 16); * `( j, r" [* y. u
if (EFI_ERROR(Status)) {( |% B: n J8 @1 E" ?; f; R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 R& x4 \6 G# c: W# U
return EFI_SUCCESS;
. t6 D( W7 f2 |1 o, B5 v& H }
0 s+ z- n3 |/ R( T; {' c
/ e( [0 Q4 a: Q- M: C; z Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! c4 N" T g- R8 ^2 M9 v" C
if (EFI_ERROR(Status)) {
+ E1 A0 h3 {6 h% Y7 i Print (L"Could not write file, FileHandle05 err code is %x\n",Status);' R$ X) `3 |) X& V W
return EFI_SUCCESS;3 s% m* \% {8 v0 b* b `! J5 I
}
V% H- @5 `0 o: V v$ x7 L7 x' O: Y# O8 F+ C, J2 b; W
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
- C, s2 c9 E, e
8 ]! f$ O+ _( X/ n* I, x% t7 C Status = FileHandle05->SetPosition(FileHandle05, 18);
$ T" s6 ^* M) |; [! C: `! K) O0 O if (EFI_ERROR(Status)) {: A/ G1 K& O! F% F% M c
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);3 T7 {1 w' O2 S
return EFI_SUCCESS;
: c7 m& R- ^) {& d* y+ ^+ k; f& t }
# j1 l d( j) F$ D' f
* M) Y# Y* {& o: R. e Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);# s0 O$ f' @! I# \9 v9 U
if (EFI_ERROR(Status)) {
8 m6 L; q# S; J3 b% L) b: @7 z2 t Print (L"Could not write file, FileHandle05 err code is %x\n",Status);; w' m& [4 Z) j a2 `! U
return EFI_SUCCESS;
. w3 n' g' s6 g: `! i+ w }
; m+ q+ F1 |# P) w7 d d
( X( \4 x3 g: d" e2 @# N) X$ j" P FreePool(DestAddr); ) b+ \) E6 t% y9 i
5 [. R5 P' X3 a0 h Status = FileHandle05->Close(FileHandle05);! Y/ Y+ r& L4 a& N* A
if (EFI_ERROR(Status)) {
3 G* d, n) e1 O3 V4 b6 Q! U Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
+ K1 S" a) P) t3 v5 `1 \) K return EFI_SUCCESS;
6 ^, t$ z& A4 Y) _: x }
% Y6 R8 @( P% s/ }* m$ Q 2 ?( x! H& R* L/ E `. J
Print(L"File Name = %s has already been created.\n", FileName);
& A/ E! n' \' g$ Z6 v2 K% }4 K6 \' j. ]" _5 G- N
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");4 c' C% ?$ h- [- V4 S( E. R9 _* q
WaitForKeyOrReset();1 N* S# |& F% P6 U. o
1 n k. l1 O! f9 ^' m2 s8 J
return Status;
0 d! n( j T9 h% t, C! R+ \} |
|