|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
, `+ \& X3 @5 _# i6 H) q# q * myfileio.c
3 B' N% \. r# Q * Apps
5 Y: m3 B: t/ p# \; K */- F# j, @; g2 G4 n) q3 ~
) R" k# q _3 V3 d$ ~" Q2 @#include "efi.h"
7 s' X( {( h! X$ O#include "efilib.h"
( X% W4 H2 i- b+ O# G4 I h7 ?0 @( T$ a& a& {& [
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE" }' X4 @! V$ |
! M1 L" h* b& `
static EFI_STATUS WaitForKeyOrReset(VOID)
8 g+ ]1 t4 A: U7 j% G) c{% d/ l5 h* L; b2 J" T
EFI_STATUS Status;
]6 ]* _: [- U4 ?8 n) x EFI_INPUT_KEY key;
7 ?$ Q6 B& x" p UINTN index;4 s% \4 m+ e* i
! ~4 A7 L! q1 d% @ H+ f& Y for(;;) {
" J* i1 f4 g; @( V Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);& {3 i1 h: c/ N' m; b
if (Status == EFI_NOT_READY)
4 @3 b2 R& }+ P9 b. g5 a6 h BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);! l% F0 r# D) ]
else" e8 i8 V' A1 m4 O
break;
1 r$ D; B) w2 F" E& ?: P }
9 V, a4 y& j! s5 o5 `) s if (!EFI_ERROR(Status)) {
' n) Q' l+ X2 m& _' Y; q if (key.ScanCode == SCAN_ESC)
4 m" v& b6 C- @( N; J8 w$ `6 F6 T RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
+ Q! F3 l7 y- d. o }
' \! R' N4 c9 H0 x7 D ! v4 P6 C4 Y4 C' k
return Status;4 z3 {$ C1 A ` |, p9 i, |4 n
}
' n$ U" C% E3 B
$ L. C6 l: z5 U9 Q- bEFI_STATUS
" Z. }/ o! W! p% ]% Q& ^( B3 SEFIAPI
9 \2 X, I0 f) j; G4 _' I2 GMyfileioMain (IN EFI_HANDLE ImageHandle,3 g' a l* O6 W( r$ n6 j; U& n- B$ R
IN EFI_SYSTEM_TABLE *SystemTable)
8 [2 b# p" l2 k6 S% S6 k{
% v3 T9 ^/ u Y4 K EFI_STATUS Status;
/ M; m& l a! w3 i7 z% M EFI_HANDLE *DestAddr;
) T) ?, y: v8 P+ m4 \7 v) c EFI_LOADED_IMAGE *FileHandle01;4 a& V4 B/ r4 ]% ? F
EFI_DEVICE_PATH *FileHandle02; % q- M9 B l0 q, X
EFI_FILE_IO_INTERFACE *FileHandle03;
, Y; Y* K q8 d% C* r/ `& R EFI_FILE *FileHandle04;" s( ]: {3 x" I' P' K
EFI_FILE *FileHandle05;3 c. N+ F/ i8 ]( s8 i, Q" A
CHAR16 *FileName;. E1 z2 E) I' c9 k. s
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
% z1 n# e5 E0 m3 l, g9 J; e UINTN BufferSize = 8, BufferSizeKeySpace = 2;
" ^7 Q }; c, y. ?7 l; k' l9 l int Bit0, Bit1, Key_Space; , {8 f3 |( _5 j* X. {$ `
% b3 R! F& B+ c
FileName = L"NewFile.txt";
R% T3 x; y4 E1 s5 d; B Space_Key = L" ";# K7 r( x1 M& Y8 c* D$ z
+ r2 k% Y: m' M' i' g1 Y
BufferA = L"ABCD";7 K" x* B: T! l4 l L8 k8 z
BufferB = L"EFGH";% g5 d1 Q' E; F5 e, w/ z: N
BufferC = L"IJKL";8 L; K' Q- e7 m
5 t6 I9 \4 h% A C/ r; v Bit0 = 0xff;- N9 Z6 M' ~' u' w/ I X
Bit1 = 0xfe;
( k' U; j3 r- z, j x6 G) ? 0 @3 ?* o/ J' l- w; Y/ @' t
Key_Space = 0x0020;( s8 c+ l! l+ w1 k' q1 V* S) o8 Q
, ~, c6 \) M& `/ K8 U5 Z InitializeLib (ImageHandle, SystemTable);
4 v8 o& z6 g) l3 {0 U, X$ ~
4 H- N. G$ c/ {7 A DestAddr = AllocatePool (BufferSize);
4 x0 h' k3 \. c% u t( d( w' O$ ^/ q
) ^8 ~, ^- g* ]1 n( ]& y8 n Print (L"Value of Bit0 is %x\n", Bit0);
6 y' ?9 F; q' w0 P) e. ~; {# ? Print (L"Value of Bit1 is %x\n", Bit1); - l! ^+ Y1 F# o3 F4 `
/ ^9 }" z7 q! f2 h# N! _! U" R* g
8 U! A1 S# N! F3 {3 D Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
4 G5 I) l, j- w! p: p9 s% }- P& ~8 i4 B if (EFI_ERROR(Status)) {# j/ @! x$ h4 g d* \ h, `9 {
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
+ }5 c+ v6 I$ m5 i1 x3 e" H return EFI_LOAD_ERROR;0 w4 o0 w' r+ A; t
}; ?# G8 `2 Y- u- I( P8 {! L, W
' u9 O$ m4 C# V& v- D, m Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);8 o, ^3 c2 G& m0 x* ?2 k
if (EFI_ERROR(Status)) {
9 g5 h) J0 h: d) s4 O2 f% w, M Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);* k! p$ M% Q3 z: c( b
return EFI_LOAD_ERROR;( X4 Z/ }9 H' k3 y. g
}
" D$ E& Y1 ~. `. H& |
8 r1 {6 T8 ?. r _/ H Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);- A( h& e9 |8 |- B8 N! C7 {& `
if (EFI_ERROR(Status)) {
- S2 H2 p6 d. q4 u' ? Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);3 f4 n) K0 p+ O5 Y& c' [! z! E9 S Q
return EFI_LOAD_ERROR;
: |/ X9 s4 d9 U }
U9 g0 X+ J. c+ k. |+ w
% c7 r: ^* `. b7 M Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);% u, x; z$ v& \) D9 y8 r ~
if (EFI_ERROR(Status)) {; l; Y+ p5 Z) e4 `) k7 r& U
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
& f$ h# w6 I7 o% O return EFI_LOAD_ERROR;
* _$ I- G# @( h) Q }
! y; c1 M5 E, L7 W) `8 U7 V
" r$ D4 [& C. u8 u! r* t' ] Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
1 J5 Y8 `9 O5 M. I3 t7 k4 z, N. u if (EFI_ERROR(Status)) {
0 V7 i6 O/ R$ Q' D3 o Print (L"Could not open file, FileHandle05 err code is %x\n",Status);" \! S; K! L( |$ \
return EFI_LOAD_ERROR;
0 [1 x6 l0 ^; H9 w }/ W0 s2 f# |+ E
& {* Q' c! ]4 r: f& D; N
Status = FileHandle05->SetPosition(FileHandle05, 0);
, T, q5 q0 z/ I+ l. H" Q) H if (EFI_ERROR(Status)) {
) b' ?! f1 X }9 H Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
: N" w7 Y( |4 R- @: o) }- X& e: s return EFI_SUCCESS;
; L& w4 J$ X8 ~7 E$ B }
6 [* N* w9 x; d* I
! h m( {, B3 W Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);1 _$ ^5 y0 Y1 X8 a4 g6 Z7 C( W
Print (L"File Buffersize is %x\n\n", BufferSize);3 ~" X/ L" }7 R l
if (EFI_ERROR(Status)) {! r, [) H! Y9 ]; m9 L6 [9 b
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); D" c/ _- p* j( C3 v4 y
return EFI_SUCCESS;
0 D5 Z# _# o; x } % w6 L9 C1 Q0 d) G+ G3 e# V
% C; ]% p6 l, s
Status = FileHandle05->SetPosition(FileHandle05, 1);
7 D9 L+ \- S9 o4 r( D# t if (EFI_ERROR(Status)) {# e; H% x/ w! g6 L9 n
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 Z+ h, v5 Q, Y0 S/ N5 u, u
return EFI_SUCCESS;
1 R8 {7 J# a0 A" [, J }
3 o: y& ]6 Z' O. C
3 n( m r* {: E2 G( Z, h) ? Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
4 P6 M" H, `& f7 ]6 i Print (L"File Buffersize is %x\n\n", BufferSize);
2 h/ [8 o0 ^0 W' A% r& K& A5 M if (EFI_ERROR(Status)) {; B% w9 a6 Y& i
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
( o# c) n' ] |" J6 a& [ return EFI_SUCCESS;- x* h" y: g) |9 J8 a9 C* [' Z0 ~* ~
} 2 Y5 Z- _) Y* U" U% a
/ l! F9 F8 _4 B8 ?$ `7 H6 Y Status = FileHandle05->SetPosition(FileHandle05, 2); ' p, r, Z9 x4 e9 J7 Q) m
if (EFI_ERROR(Status)) {
. x9 N6 }' t/ I8 Y) b& | Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 s. r! R0 _# [! t+ E i- C return EFI_SUCCESS;
0 [2 X6 u8 M) P$ s$ e' b% r }2 ^0 y# [# ~ l* {0 X
$ E* O! ^/ |/ c% q; l! t Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);! R$ I* z3 W0 t) e0 r
Print (L"File Buffersize is %x\n\n", BufferSize);
: v6 A% M" X4 J, S if (EFI_ERROR(Status)) {
7 R! \3 s( [' L2 |. x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);9 j2 f" V7 ]; S# s0 \" K6 H# e, V! V
return EFI_SUCCESS;! H% U2 C" o+ c0 T
} : k6 M# W: s0 I1 p
# y5 K; k) H( ^$ Y( u6 V7 D//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
1 G- b) N% V3 h) W. Z+ I4 \ U9 u: F$ j Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);7 N2 f7 Z, E. [, ]
Status = FileHandle05->SetPosition(FileHandle05, 2);
0 b! Z) o+ l$ @9 w. b6 x) J0 h if (EFI_ERROR(Status)) {+ ?% e1 s9 l3 i$ X
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
- Y8 h1 E8 G2 B' x7 |' E return EFI_SUCCESS;
' A: F9 M+ x; V( l! X1 I }- K! O$ M) `4 P/ b
+ q4 }9 Z3 M7 g! R& \ Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
4 }! B* u+ m- o, [2 U! P c if (EFI_ERROR(Status)) {
2 `6 V, C6 H, R Print (L"Could not read file, FileHandle05 err code is %x\n",Status);$ f& V e* g3 [5 n" m
return EFI_SUCCESS;
% H' U. _ b! K }
* o# G5 ` \0 W0 d/ z7 } Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);1 D; G+ N/ I( G- Q0 j* G" R
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<, D5 [2 v8 h: q- y
0 Y( V7 d( A- T. d( }
Status = FileHandle05->SetPosition(FileHandle05, 10);
4 O! e8 I* h7 ~9 ^ if (EFI_ERROR(Status)) {
5 n P9 [# M7 o F/ e9 T Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# s: t8 n/ z' G- A N% f6 j
return EFI_SUCCESS;) a: v9 m! b2 _# h8 D1 E* x; Y6 p
}
& k( f; w/ R1 w, G $ S5 [$ P" ~4 G
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
$ [1 @) Q- n! @! g" q# R if (EFI_ERROR(Status)) {) O8 }" f4 F2 |1 ?# ^! m
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* w; \. V; p' C
return EFI_SUCCESS;
& a0 b; x4 _( Y) y+ }) R }4 k( |; g: q5 V9 P7 X
" ~4 A& L5 a* d, n6 j4 F
Status = FileHandle05->SetPosition(FileHandle05, 12); ) i0 V" N/ G# W
if (EFI_ERROR(Status)) {( o8 K1 d0 Q" q4 U
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 q; a- Y ?2 e' e7 d
return EFI_SUCCESS;1 r' j7 F# v8 r
}
$ L! b7 j: a5 z$ @# K& C& p7 c7 _2 b 0 B( B# T. _+ g( j B
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);3 ]6 l& h" t5 d& {; W
if (EFI_ERROR(Status)) {/ i; Y% t- a: r( F( R" G
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);4 g# `3 p, E4 B# Z7 K( J2 t
return EFI_SUCCESS;
2 u& k! m! L; a' R( y }7 D, o8 L, G ~/ e" ?' h' M) B
6 x, @% d0 E% ?7 |' Z) z Status = FileHandle05->SetPosition(FileHandle05, 14); 7 q3 x! K7 `3 j7 U
if (EFI_ERROR(Status)) {
; N) n3 [, ~3 w" N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
F( L" ?5 D/ b9 r- T return EFI_SUCCESS;
1 o# |( t G" @+ G8 V }& t/ k: u# e8 s' B7 O/ H
( i; p7 Y6 o, ~% L0 m: D7 o
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
! d; \; U9 |9 n m- K if (EFI_ERROR(Status)) {6 M; V+ {6 v i' c1 X8 j" n4 M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' r8 V3 \$ A3 F+ @% T. r' m return EFI_SUCCESS;
, F) Z6 K7 ]. p+ E8 D }
4 \$ t* H. C4 K* S! ^* k8 X5 S* X6 l! w0 ?
Status = FileHandle05->SetPosition(FileHandle05, 16); " v# t7 f) v$ y2 M2 N0 `3 F! T
if (EFI_ERROR(Status)) {
9 b- n7 ~0 d1 w! P+ p( I: y Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);; }% a( a" b3 [6 O
return EFI_SUCCESS;6 i; N5 O, Q t% u( G% ?" @8 x; e
}) f5 Q8 X* R2 n. `1 Q9 f/ e0 O/ f$ `
b. M) F c( R7 S7 o" C* I Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: e% Q6 f; Q; P% n, g* n/ [
if (EFI_ERROR(Status)) {& y7 i8 P- ~! U0 M" T& p, d @
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& K: P0 w9 q, G8 H3 _
return EFI_SUCCESS;
$ D$ D8 ?# ?/ T$ k } 3 A8 H$ o7 B. u
# x% z3 C9 N6 P3 T3 P/ j F//---------------------------------------------------------------------------------------------------------------------------------------------------------------
7 S' l) Z* n' z; q8 z0 [0 x h- b
Status = FileHandle05->SetPosition(FileHandle05, 18);
0 ^4 o8 S; t: \8 T if (EFI_ERROR(Status)) {* S- {& [+ \ |( ]; Q6 a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 o" h7 \* H9 C z5 i; _
return EFI_SUCCESS;
+ w1 ~" D* z, X! x }% f# o$ w. C9 O$ ^& } c
7 F4 ?6 ?8 k# ^
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
2 x1 H) w' [0 X if (EFI_ERROR(Status)) {
* ~( V5 Q/ q+ B& k) J Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 Z# `* {8 \$ F" Z
return EFI_SUCCESS;5 h) X' M" L1 Z! o$ Z$ G' }
}
/ J5 V* r/ b) U0 e
$ }% d/ s& m$ h/ F& l" z5 z FreePool(DestAddr);
9 V# u- T2 V3 b M% X; h* x) n1 k) x! y X4 f4 f
Status = FileHandle05->Close(FileHandle05);* u: t& }( t% `+ `; y: M& j
if (EFI_ERROR(Status)) {
3 {/ T0 d% O% l5 \ k Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
/ _- L$ \; U' d return EFI_SUCCESS;
: Z4 ^$ `1 c* w; A6 I } _2 p4 e8 ^7 y0 h/ u
. l) U% [8 ~: N/ L3 a Print(L"File Name = %s has already been created.\n", FileName);
O( e( N2 ]. J+ e. N* j* U5 A% h
+ n$ G* n" L; O! G- u5 x) `* c/ r Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");6 M0 U; X% |- K( E3 E
WaitForKeyOrReset();
2 [. k* x' q' d5 {4 F4 S $ f, M2 y5 B n# t% v4 J
return Status;! F4 A4 `/ a2 }' b/ P" w
} |
|