|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*0 e5 q' l8 h5 I8 u+ K% k
* myfileio.c! w* J4 D# E' f# b/ p2 d
* Apps
. y1 L" }# h6 g: N9 h; \; e3 a H */
! [$ O1 W$ E/ T4 D3 Z
. g+ T5 [- r" R% P2 r" Z4 V#include "efi.h"
4 B5 I {9 H; e* D$ K# u#include "efilib.h"; a- }$ U% s2 v8 `
9 c$ y0 @* @# l! m#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE7 h- b' G0 A; h- q( U3 W; L# }
+ J3 P+ f: Z& `, A
static EFI_STATUS WaitForKeyOrReset(VOID)% I: [% i* h4 m- j* U8 P( T4 } O
{
0 G/ w K1 Y/ N8 d* l EFI_STATUS Status;
( l% D0 X6 _ I) E" D" h$ P9 J EFI_INPUT_KEY key;
% N [. d4 ~, p. g UINTN index;
: b% ^3 G* g1 \$ b - e: ]0 c0 A1 t' ~' ~; W2 C! B. q. i
for(;;) {- n9 ~* x `! ~ m9 a
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);& N4 l6 V$ q! m
if (Status == EFI_NOT_READY)1 @/ `. \$ G, @& F$ l8 A; F
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
' R8 k6 L$ s! N9 t else
! n2 f$ L5 I4 A break;
' N- G) V" P; A8 m3 h2 p b5 X9 ] }9 j5 J: R$ W, b' I( D
if (!EFI_ERROR(Status)) {/ O- z; [2 e# @" ?
if (key.ScanCode == SCAN_ESC)
" Q+ e. [9 I: Q4 }' j3 I RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);; @ U k$ d! e9 I/ K
}
! |0 P! Z) F+ k. s6 \" s* i% [
+ }! V9 ?% x. p2 p- Q0 ~. | return Status;. p/ p; t: U1 w) ^8 G0 ^
}
) C7 n; G" j9 f. k' l: E' V3 a; w3 ]8 _$ a6 Z
EFI_STATUS, _3 a ~ i6 C6 V% K: o- v
EFIAPI
) X9 I) C" T" Y1 O. AMyfileioMain (IN EFI_HANDLE ImageHandle,4 Z. X0 y9 F! K4 s5 n: [: [
IN EFI_SYSTEM_TABLE *SystemTable)9 |& _8 B8 L, I' i# p( w( o2 ~
{% }6 {* K: `; [3 ^
EFI_STATUS Status;" D S$ q/ w2 [ h- X9 H
EFI_HANDLE *DestAddr;
3 `( m7 ]1 B% ~/ R; L! C EFI_LOADED_IMAGE *FileHandle01;
" i. f5 m4 Y" f8 `" R+ Y7 ? EFI_DEVICE_PATH *FileHandle02; 1 ^# W$ Y* d- z' O0 I6 A
EFI_FILE_IO_INTERFACE *FileHandle03;
6 H. h! A" t7 G. ^" V2 Z8 T( Y4 e EFI_FILE *FileHandle04;* }5 s# N5 X) w7 F0 O0 @; @# `
EFI_FILE *FileHandle05;" k: M- R! ^3 I: g* B
CHAR16 *FileName;
/ `7 Z: q' o$ D: f+ i4 r. f% } CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;2 n7 K' E: I+ U6 B; m
UINTN BufferSize = 8, BufferSizeKeySpace = 2;8 H1 y6 Z/ N" `1 [( K+ }, k
int Bit0, Bit1, Key_Space; + l* U2 _7 v8 p) u; h
# f3 N2 b% i! c$ B( F FileName = L"NewFile.txt";
# p: M7 ^) ?8 T% X Space_Key = L" ";; v* B0 A# W. a
5 O' |: p/ N% Z* K; h9 O8 I) s! Y5 l
BufferA = L"ABCD";+ U! P- g& R) @% d9 O' r' r9 z
BufferB = L"EFGH";
) V! k% U! P L" u! m; ^ BufferC = L"IJKL";& L& {1 \& N( p5 P4 W( W5 d
1 [4 c n# n5 t0 w: J8 _
Bit0 = 0xff;
/ x$ y% p2 {' o( A- k' S Bit1 = 0xfe;
3 J4 m7 ]0 a9 l, @9 _: t! \
' N: p. n" T$ t2 J( q Key_Space = 0x0020;! d) G( {/ s8 W' ]/ A$ a0 X$ \% ?- Q5 F
- M# a" x, F, Q6 H; l Y
InitializeLib (ImageHandle, SystemTable); # e* v5 \0 g$ N0 s& O* v: m) D
, Q$ }! z/ r# C! U% z* w4 g/ l DestAddr = AllocatePool (BufferSize); n# O& Y0 A M5 g$ F" {) i
- q1 L" j! S, K6 O; u! L! h, l: Q2 o
Print (L"Value of Bit0 is %x\n", Bit0);
4 g8 b; g& a. b# J2 T$ G4 v) z6 n Print (L"Value of Bit1 is %x\n", Bit1);
# v X' {( I# T9 M6 H* `) d5 O2 |( o# U. v3 k' i$ t6 f- [
1 l) {2 ]# g! E7 e, s Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
( y/ y# |6 f# G3 I if (EFI_ERROR(Status)) {5 v! z- d2 s1 L' d
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);( @6 ]/ B+ E( u6 t8 C
return EFI_LOAD_ERROR;
# w5 k1 M _ w }
4 {- g, v6 I3 C5 \5 {/ B
6 l8 ]/ K8 N% @5 c( [1 y Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
, e* ]( Z8 J, Q if (EFI_ERROR(Status)) {8 m+ L+ A$ ^# T; O6 j# O
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);! M& p# s) ?0 y
return EFI_LOAD_ERROR;
+ Y* m! t) i1 x$ | i" x } : N2 O1 j5 c+ m1 P4 {
* w5 x3 l, n" G \+ {) p
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
) |( V7 f$ w$ V if (EFI_ERROR(Status)) {
8 o! {1 K* m3 A/ b) y7 ]* _ Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);5 P3 j7 i9 t# a& W
return EFI_LOAD_ERROR;
1 F5 W' K9 ]5 z5 g* D! b }
+ ~, j5 K( z! X7 \6 W. J5 {7 D {7 n/ K6 p1 ]9 i
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
: c! \ m6 ?7 T; Y1 H% C4 [% e if (EFI_ERROR(Status)) {% n1 X0 W' w0 D- g8 L3 s
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
9 b- b) g- O W return EFI_LOAD_ERROR;
: P% ?5 h5 c7 a7 g3 }2 i: p } ( H7 T; W& j0 O! A! v
; [9 r- [" j6 O. j: P7 t Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
3 o& g" ]' |& M3 d* c2 S; P if (EFI_ERROR(Status)) {
5 |; K1 Q; g( \% Z Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
# x; f) Z! z+ X6 g return EFI_LOAD_ERROR;$ b7 J. X. O) K- W3 Q
}
0 }% ^' @- s5 m4 W
5 j( R! K) K0 Y Status = FileHandle05->SetPosition(FileHandle05, 0);
1 r0 P- e @3 ^/ Q7 g if (EFI_ERROR(Status)) {; k' D/ E3 R0 P2 T# T5 W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 U8 _' y }$ ^% z* Z& _ return EFI_SUCCESS;
$ g5 V( H: E& m0 i# L- Z }* g4 d3 Y- \6 |' {6 n
; M) Y' y% H. h- o Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
8 Z- q7 S; B/ R7 |1 e9 O" n& F Print (L"File Buffersize is %x\n\n", BufferSize);% l1 X6 Y, X# [" Q) N! W
if (EFI_ERROR(Status)) {2 d+ u1 B6 U, U3 J
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);$ w2 b6 t% @3 n
return EFI_SUCCESS;
, ^7 S* Y0 q6 B- O6 t6 d3 o( T } - l5 U, B/ y: M( p* f9 a
+ s1 S, F5 @( ]9 V, q8 ^; _8 e9 @ Status = FileHandle05->SetPosition(FileHandle05, 1); : B' \6 c9 C4 ` E1 a: N/ A
if (EFI_ERROR(Status)) {
8 f: S" q7 G: @& O& O Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) d+ Q( W0 ]! Y, V6 {. O: Q2 r0 u return EFI_SUCCESS;
# W) F, c3 U3 Y }8 B3 s/ s8 @) U% z& X6 L* o/ ?, o
, k: j# a1 v) @% b. M/ C# ? Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);# ^- g K* W+ [7 Q+ F
Print (L"File Buffersize is %x\n\n", BufferSize);
3 Y, {0 R- }1 i; i/ C if (EFI_ERROR(Status)) {* l, Q3 S9 h0 B. \. h9 g$ O
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
0 O1 Y; {# Y4 e0 S. T return EFI_SUCCESS;: V; O3 i$ y8 ]" C' Y
}
6 c8 \. ^+ K: }+ Z8 e2 |
9 w' t- q8 g# V" A# W( X3 M v1 t Status = FileHandle05->SetPosition(FileHandle05, 2);
0 [" O; x- K/ D A: v6 y if (EFI_ERROR(Status)) {
3 j6 w5 m* a% Z9 E$ q: B3 Z Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
7 i6 T' u, k8 `& N* P. V& l return EFI_SUCCESS;
7 H3 u" B( A" ~+ i1 ? }+ F; H% H4 k3 U t ]4 z& M6 l3 {
% G( u5 f$ g+ l) o9 g: X Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
3 Z \' C2 g8 D! k. ?! A Print (L"File Buffersize is %x\n\n", BufferSize);
- A: x, q% I' x% n* G if (EFI_ERROR(Status)) {, z( E# J8 ^7 K* @8 ^! e
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
7 l- _- B ]. P2 L$ q6 N return EFI_SUCCESS;
' g) X1 r) g6 _! H% T% {$ T } ) Y. [, D: K4 P5 V( \3 H; ~: q
* K5 n9 C" C3 F//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>! e; h8 S2 S. R+ l+ W+ q+ L* @
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);9 u9 A: f' {8 w
Status = FileHandle05->SetPosition(FileHandle05, 2);
5 R8 _% p& Z, ?% p( A$ l s3 N if (EFI_ERROR(Status)) {
6 G1 y% @! c$ f& t Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 B3 e) p& M8 {& s( `& j return EFI_SUCCESS;
, ~# D6 n' Q N& u) M) s }- `4 V1 Z/ g- j% \- M% _. l" F% F
) b2 T( N1 h; ]& M. |1 N3 f
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);0 `. }' o: w1 m6 p9 |5 G# Y1 ?4 j
if (EFI_ERROR(Status)) {
1 d- I5 k2 G: g( t Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
) m# {- l% z; h8 R. E3 W0 {" @ return EFI_SUCCESS;9 @: R' T* K! x. n; U- e& ~
} 8 Z# Z3 ]% w- Y$ Q1 K. R
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);# |. \0 f! i9 a/ q. @4 B
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<* E% Y, [1 |' K& b/ H2 L2 O$ b
- _8 q* r: U9 Y" @
Status = FileHandle05->SetPosition(FileHandle05, 10);
9 q+ Y8 n! s* f/ j if (EFI_ERROR(Status)) {: q5 W, }% Y" a' L4 f+ w9 _
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);* l' U2 `& H+ D9 x- ^, _4 s+ w5 M
return EFI_SUCCESS;
6 c' C* a% z4 E/ j% ^4 m }
; j, R8 i4 d# ~1 W+ ` ; K) Z, e# u6 x' ~4 e+ d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);1 e/ G6 ~- T2 G% f4 a
if (EFI_ERROR(Status)) {3 x3 ]6 S' z* @) N4 G
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
: V/ G8 h% B3 R' r- I4 x return EFI_SUCCESS;
. ~8 M- G2 n& m4 q( I }
4 X2 l8 G$ L# t: ~$ p' z8 }" g- T
Status = FileHandle05->SetPosition(FileHandle05, 12);
9 n% D% W! \, \2 { if (EFI_ERROR(Status)) {9 G6 ~3 ], y. I `$ {+ }. X3 a
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);: N/ _; Q0 `, S/ F. j/ S
return EFI_SUCCESS;
9 F t! P/ R6 o* `+ P2 a. k }
* N7 Y9 g; N6 f9 _! p
; ]% D7 c! x' [ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& y x4 \, B% v' u if (EFI_ERROR(Status)) {
/ F0 n' u% p1 C7 `7 H# ^ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
+ i5 w2 V4 z* U) H return EFI_SUCCESS;
1 [' w$ l0 {0 i( N/ L }4 S: r/ d' L- M9 u, A7 u
8 t1 E& {* Q. z8 X# j) U Status = FileHandle05->SetPosition(FileHandle05, 14);
6 \1 T: z# x# F4 F& _& d if (EFI_ERROR(Status)) {
9 S; c1 A& n' q6 ?, m8 E8 ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% w" w; M0 ]: H; S1 [/ A
return EFI_SUCCESS;
3 v% T; l; Z8 w6 j/ L }+ F1 E! I& }, I* m% O" k
; r# R+ H! }0 P4 t+ S: v; E8 {" s Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: Q" q v8 \3 c# y$ l; {- t
if (EFI_ERROR(Status)) {
8 v) X8 n4 H9 D4 c Print (L"Could not write file, FileHandle05 err code is %x\n",Status);* S; T5 \1 c2 ]+ R+ }- X6 c1 B
return EFI_SUCCESS;( ^+ N$ j+ p) |, Q
}
' R9 j U: t% I1 |9 ]
4 u3 \; }: n' Q2 G7 ?- ^ Status = FileHandle05->SetPosition(FileHandle05, 16); 7 v) j" Q8 b% A5 S
if (EFI_ERROR(Status)) {0 N# x+ ]$ t1 o/ r
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. `* T! `& M3 k
return EFI_SUCCESS;+ _& |0 R6 Q& _- ^0 ?6 w, y+ }( \
}
$ l' @+ y' V. s; a$ k* ?! }3 W0 q* M2 m 6 y9 e Y1 A( ^" U
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);+ I# _ L1 {0 m1 a
if (EFI_ERROR(Status)) {
$ W2 t+ H( o- a Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 T: N& g2 [7 Z3 L {; c L3 I return EFI_SUCCESS;0 M4 b. s, X; V9 v7 s- t; F
} 3 D& u4 ~$ M. }/ r6 L5 p* _# j
+ W& e* [5 Z& T- W//---------------------------------------------------------------------------------------------------------------------------------------------------------------
5 J" L# k& V" ~2 a9 o2 z. g5 O# f' ]) C6 A# [9 c, ]7 N3 {; d
Status = FileHandle05->SetPosition(FileHandle05, 18);
( p }& }! K e' c; r' o0 T$ p if (EFI_ERROR(Status)) {0 b2 Z) ?& h/ e2 n0 M
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 B! l; N! u; d, x w$ }
return EFI_SUCCESS;2 ]- m! G. x* t2 _! u
}
- O- y) j* n0 F: m" q9 f( d
6 S9 P- Y4 F; @. O! v. x+ Y) D Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
! \# _% T- g* W/ i' N if (EFI_ERROR(Status)) {
f6 t, I2 T- S* b Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& f: L4 y) S/ Z8 v5 _3 G
return EFI_SUCCESS;6 S. S% m) f l0 { a
}
" _' r8 O7 j) ^/ @5 z
. D+ h& h$ k' b! s* k* H1 s FreePool(DestAddr); - H% k) S: K9 h, I; p
9 `+ V, f p/ @5 J8 A3 J* B
Status = FileHandle05->Close(FileHandle05);
% O! H9 {2 z* I) H6 G if (EFI_ERROR(Status)) {
# m3 ~5 C1 C$ V0 g4 \+ z Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
2 b8 a$ U8 T5 M% T( h return EFI_SUCCESS;
( Y* b! h/ Y* ^. t) J% @. E5 u }
' G& r# Y& Z# z) X* @; w8 |7 m1 ~ $ J- J# y) V4 \, y5 G* {1 }
Print(L"File Name = %s has already been created.\n", FileName);8 Q% R7 ^* R/ c0 e* Z) U
; V5 C7 s8 U4 r, \( {) n+ X [
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
% W% ~5 U9 N9 S3 c" d) k: O WaitForKeyOrReset();- \, u9 e0 g( z) P( X u
& B: `- k R# H7 L! P return Status;6 m, o! H% I7 N& Y
} |
|