|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
, e( t, u9 D. O9 v7 T/ j1 Z' h * myfileio.c3 b5 g) ?7 M/ r9 [; v9 Y3 K1 z
* Apps
) g. q' X$ R* i$ ?- s) h, l */" w) }* S& |3 `5 N, l/ t
f( g8 L( {9 K& V. L
#include "efi.h"8 W/ {0 a9 u$ _9 N# M8 v- n
#include "efilib.h"
$ k) q& J6 t$ _5 @. r7 [, t7 h+ [' C+ @
2 F8 [! l% w) A#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
' v& i) e( }4 [5 R' C8 O5 H
1 L, M8 Y: |( F' xstatic EFI_STATUS WaitForKeyOrReset(VOID)3 \! o7 i9 @) o2 W% t& n
{8 f; y* X4 N$ S
EFI_STATUS Status;2 l: `6 H( N+ {% G# n% _
EFI_INPUT_KEY key;5 K+ @/ B: \5 N
UINTN index;
6 ]% |4 a- R) k- M2 w 0 v, Y" J# S- P+ I! `9 W. b- ]
for(;;) {
' F( ^8 ]4 ~: x C* m Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);- `8 J! c+ @ X8 p6 ^" F3 C" `& `9 z
if (Status == EFI_NOT_READY)' a3 Q* q; ^0 I! G2 Z* v6 K& a
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);$ a0 M1 x% n4 k, Q. `
else
0 X: w7 G" C9 f: ]8 v break;( s$ w- R& n8 X: @+ ~! {
}7 R) e9 B- L, Y$ O( L9 g7 ^3 C- C
if (!EFI_ERROR(Status)) {
+ l/ _; c# n. U( v if (key.ScanCode == SCAN_ESC)- ^; u' d# J9 F4 ^7 Q+ `
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);" ^( Q5 t& j# _% x1 Q# z; X$ i
}: B' j4 \$ m* i* M/ I! J! x
* G) E6 {$ C, H" ]6 j6 b
return Status;7 L5 X2 P* d) I; M, o9 [
}4 i; U; h8 W% l' A
" s; p) Q% N5 m# n6 X. }2 l" R7 |
EFI_STATUS \0 F. G2 ?9 I+ Z K) F
EFIAPI
; p! }* F& O! c+ e9 {8 w, c0 ?3 eMyfileioMain (IN EFI_HANDLE ImageHandle,% L. p7 |; v; d0 D* @) [# ?, T
IN EFI_SYSTEM_TABLE *SystemTable)
* H' d) q( E" q( }) A2 ]/ G0 x' t{5 _; |0 u8 ~" E
EFI_STATUS Status;
% E& ]5 y4 w: c. N. K. x EFI_HANDLE *DestAddr;
+ P5 J5 ~. D; P+ S; G& ] EFI_LOADED_IMAGE *FileHandle01;' [( C! O6 }- Y3 L; l
EFI_DEVICE_PATH *FileHandle02; % \% D: t2 f. }3 R' y3 U
EFI_FILE_IO_INTERFACE *FileHandle03;3 `1 W- Z; H9 T
EFI_FILE *FileHandle04; a& M2 s! W# `
EFI_FILE *FileHandle05;
$ h, s$ }7 T* u9 W( Z* ^5 E0 v+ d6 i CHAR16 *FileName;
. L( I l, y7 _% l, Q; n CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
* D) T+ b0 Z1 B* O' ^ UINTN BufferSize = 8, BufferSizeKeySpace = 2;
1 _9 U* @" H9 q: `" r! a int Bit0, Bit1, Key_Space;
- S/ o1 y( ^5 X. ~+ n' z+ _' w- ]2 t+ l
FileName = L"NewFile.txt";' a& @0 k/ C3 E5 ~
Space_Key = L" ";
/ D' ]$ }- f+ v% f5 W
7 W, u$ u ^4 c+ v BufferA = L"ABCD";
3 n( {: ?* F1 n! U+ e/ ~4 e BufferB = L"EFGH";4 {' ^0 r2 G; O
BufferC = L"IJKL";5 x% J2 d4 B. Y4 L( _6 H! D% A% H
- F4 x7 m! ~9 V9 W Bit0 = 0xff;; S5 E5 u2 Z( |" a. |9 ]+ _) N1 q$ [
Bit1 = 0xfe;
5 O1 q2 Y& O+ l5 g1 W6 e' V
' Q% u: n! N7 G6 ~0 q Key_Space = 0x0020;- j W/ O. H* s. B
, U" ^% D j% r, Y2 A InitializeLib (ImageHandle, SystemTable);
2 X# D0 s) a, `1 X& H/ K; m0 M: l, s, Z* m
DestAddr = AllocatePool (BufferSize); & k" h4 q- Y* @: f! ~" m1 C* y
( s: o7 c" k1 g
Print (L"Value of Bit0 is %x\n", Bit0);$ F% _ @" d* ]2 N# u$ J
Print (L"Value of Bit1 is %x\n", Bit1); - e9 m: n" t- t- E Q2 W3 Z
- @- o( {6 u6 R6 U6 v! d 3 R; z5 R# z d: t. p% y1 }
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
8 b. M. R0 t% }; V3 H, j if (EFI_ERROR(Status)) {. x( x0 ]/ b& M
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
/ R6 g* W# L) k2 ^7 m* ]( c; } return EFI_LOAD_ERROR;
/ R0 h! m$ i) L. E }
+ I9 ~5 X _* ]- N- w; F
. ]% P* t9 J4 v c' b& M Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
: t# T, _7 P2 A( w& @) w if (EFI_ERROR(Status)) {
4 G; E- A `0 g. [# L Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
: z2 J/ h7 ?# a+ } return EFI_LOAD_ERROR;
0 y0 V: I: D. [/ |1 D# b$ B* Z/ M }
% ]3 Q' i% h6 U5 g8 n : @4 p& `! ?* k3 `* e7 u' h
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
( |* s# F1 j+ a if (EFI_ERROR(Status)) {8 l8 _3 U4 B4 u& p$ `
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);0 E! {$ Z/ F; Q5 A+ d# I
return EFI_LOAD_ERROR;
! T0 j& ~8 W; x& a2 O! X# Q }
+ s' X" Z7 t: U# e" f& T; b7 A9 P- [
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04); |# j7 c/ `% R& e9 B7 { ]: \
if (EFI_ERROR(Status)) {
3 W5 ~; B P$ r Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);7 p5 d+ U) h& i4 @
return EFI_LOAD_ERROR;" M9 u m$ Y' x& r$ J
} + I) b4 u7 J7 Y1 z9 F" A. Q5 z1 ]0 N
: p) `: Y4 I# r7 _, _# W& j/ `6 f* D
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
9 i2 D2 c# F# [0 S& M7 m if (EFI_ERROR(Status)) {
; f b$ n( `1 ?, V Print (L"Could not open file, FileHandle05 err code is %x\n",Status);+ c S6 t/ j3 {+ v, y8 f) H4 u
return EFI_LOAD_ERROR;
7 q$ D! c6 Z; ~5 v. q }% v- G% Y0 E# ?1 w$ m& o8 \8 c
# V7 H$ _) K p! L' a. ~
Status = FileHandle05->SetPosition(FileHandle05, 0); % f. b. `6 f- V4 m- L8 T
if (EFI_ERROR(Status)) {
6 l6 t( s1 H5 L4 g Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); l6 d- n/ F/ o' d r! M
return EFI_SUCCESS;
" U+ O/ c0 y+ J1 R$ g& [" q1 r }6 T1 G0 X8 z' y: i% s
% n* F$ R) i% Q$ i( Y/ Q, @2 {
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);* \0 _% i, @% } X: J6 M
Print (L"File Buffersize is %x\n\n", BufferSize);! f, `2 {6 G" K g8 ^9 `& W. `
if (EFI_ERROR(Status)) {
( b3 V1 a+ t, W3 i2 w$ S; U: z Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
& h3 D7 F- w% x2 |. f- T return EFI_SUCCESS;
# B7 u# K6 g6 a- m( L' t }
7 k' u- O& e1 E5 ?+ @1 K- ] D7 x& n9 y+ P5 K$ S6 \1 t
Status = FileHandle05->SetPosition(FileHandle05, 1);
* h; i3 z" E' Q# _4 n' s3 T if (EFI_ERROR(Status)) {
: B2 K. B M: A3 f3 @6 k) ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& {) B, ~9 M5 ^5 f return EFI_SUCCESS;
4 P% E* Z2 E* }" C, A! I }! i, Y6 _0 o! m+ S5 P' O
, }9 E) o8 i( w ~ J0 x
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
# T9 a6 t2 i: x: y5 p. C3 @ Print (L"File Buffersize is %x\n\n", BufferSize);/ Y( T7 k& w7 E4 Q3 ~7 W: o m
if (EFI_ERROR(Status)) {
& E5 a( K* y; L: [+ V Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
' |; t) m- r- w! T* _ return EFI_SUCCESS;
/ z* M8 p j0 t2 k }
1 T6 L& N8 n/ T1 E# }
% u+ q3 v- P8 B2 X! q) l' \ Status = FileHandle05->SetPosition(FileHandle05, 2); c+ ^0 i" ^* d% @% n$ m }- x# o
if (EFI_ERROR(Status)) {3 @- _, i; U$ b. W# G! X
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# |' B" e( @# |: O+ W. p
return EFI_SUCCESS;
8 M0 \$ W W' s( q }
" ? R, T! V: x. a" p) }/ L 7 k8 v v1 H" r: x) G+ J( C/ \' N
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
/ o; v8 ~9 g6 H9 Z! {% l Print (L"File Buffersize is %x\n\n", BufferSize);
. F& F S) k1 |9 d+ J& E% D if (EFI_ERROR(Status)) {, N2 G- T6 n4 |
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
) i% H2 j' p& y/ V return EFI_SUCCESS;
) l0 e$ m+ q3 [* N5 z/ J. S }
, R# m ?# C7 |( w
7 l Y) p \( u! Q7 {7 Y; {//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>5 N% b2 L1 }3 h+ @3 `
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);3 N% V' x6 w0 v% Q5 m) i
Status = FileHandle05->SetPosition(FileHandle05, 2);
3 E- b f' u* u( u' ~* r2 C if (EFI_ERROR(Status)) {
. C) L, s4 V) C! x2 \# M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 i$ K* ?$ n- \9 M; I0 [3 ~' X return EFI_SUCCESS;$ r7 H: ~3 _( J) B( A, n* n% u
}
) J! ]6 ^. \: ^# C' D; V8 o) h' l" R# f) o. l8 K
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
! V) S: `' l. n) L- R+ t. i P if (EFI_ERROR(Status)) {
( k7 i/ K2 Z$ p1 t$ S. j Print (L"Could not read file, FileHandle05 err code is %x\n",Status);1 f- H% s" j& k
return EFI_SUCCESS;
0 G0 H+ ]; s4 F @7 G: y# u- p } 0 I7 `3 o! a9 a: K! d) |/ Q3 `5 J
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);) q- R# X( q& a6 M. `
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<$ E2 ?% [* D- K& G! G5 ]4 J
; ?; r+ Q* g: E! Y r/ H* k
Status = FileHandle05->SetPosition(FileHandle05, 10); . u9 F t- L6 s6 h' f% u) U0 D
if (EFI_ERROR(Status)) {
; B& l# B4 F( j6 p! ^9 r. H/ V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
# ^/ h, o/ |9 H# i" e- r& f return EFI_SUCCESS;
8 @4 ~- A8 S1 U }- k/ J7 d! Z2 @& }, p: u7 Q+ r
X) T' ]* v; M% n( U6 s+ ] Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);* d+ w" d1 T& D" \
if (EFI_ERROR(Status)) {% r" W' G0 L% Z- L/ \
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);5 M" m5 Q/ q0 r( |! y" C
return EFI_SUCCESS;4 a c( ~) \+ J s+ P T, a
}
0 W; k0 f. v7 H% B2 D3 g4 a* W4 e7 F: l4 a
Status = FileHandle05->SetPosition(FileHandle05, 12);
+ b8 h. U) B: d8 H; @4 h if (EFI_ERROR(Status)) {
' U5 v. o* z4 [, ^0 Y; _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 o' [# p- q; x$ b& N, g8 `
return EFI_SUCCESS;
, p% Y' X4 Y& @6 R }
3 J. c8 H, E8 `3 K* e & M6 q$ r! r. a, d; c5 d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);# a/ _9 }6 c) a. [+ P* i' a3 {6 w( u
if (EFI_ERROR(Status)) {
4 H+ l1 @" V: j1 k6 Q3 o Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& m, p' P# E7 }4 S
return EFI_SUCCESS;
9 O- x5 m: H+ g6 N! J9 b }
3 s' Z7 O6 n, S9 w/ [) g3 z7 E Y9 [5 x/ p# r2 U9 N8 l
Status = FileHandle05->SetPosition(FileHandle05, 14);
- x6 c1 w; m6 \* T if (EFI_ERROR(Status)) {
7 `9 d' ~; i# h* R% P5 H6 x' j! ` Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
! Q9 Q' \, M h8 h+ Z( M3 } return EFI_SUCCESS;
2 a+ _/ u$ @4 @, a: Z: s; d5 G3 x }
$ p' h2 ]5 ^8 C+ g- N' q6 d & {& I: Z1 t1 b+ D9 o& y
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);( Z( W+ r/ f1 t& x& t
if (EFI_ERROR(Status)) {
5 V% q D" O6 Z+ p I* o( O7 E( \ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
f! E C& C! d, W& i/ {1 |$ y return EFI_SUCCESS;
' E8 h1 T% z3 Q$ h }1 c( n# z. [ ]6 Z. Q# p
# w' U) C6 B ~ w4 }) v$ i: A Status = FileHandle05->SetPosition(FileHandle05, 16);
4 |, c- V8 z, x3 c if (EFI_ERROR(Status)) {0 F$ E3 L& @* Q$ @4 |
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; p" H% L% [' l; Q3 s! q* y4 u8 Y return EFI_SUCCESS;' Z3 k% \, }8 x o6 L
}1 |) N+ ]* o% r* W6 _1 H
6 Q1 U# _* _+ I( n* C& r8 p# e
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: T- j8 }2 S8 Z: B" \
if (EFI_ERROR(Status)) {# z2 `1 I" k- \" F
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);7 S3 S9 ]+ D/ g N' K
return EFI_SUCCESS;
+ H8 P2 g8 R+ t' s } , W2 W7 f1 Y+ g8 l) l/ J+ m7 r
; ^! W2 o8 K$ B( B! x
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
T$ M+ k, h2 ]) g
5 x9 w8 G4 X! p, l Status = FileHandle05->SetPosition(FileHandle05, 18); 3 I( D. F' ?" f& `
if (EFI_ERROR(Status)) {+ s$ K/ s% N) v. S8 ?" [% j: Z* b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; _& y1 F3 S* i: r return EFI_SUCCESS;
) z" ~) i& R9 I$ t; z$ n) @ }* r5 \5 W/ S; w4 M- `
; U9 Y: f. F2 t! [+ [! s, R2 g; o, p Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
# j2 U1 D( R5 ~8 u8 x if (EFI_ERROR(Status)) {
; S/ Z8 i, p7 @" r" k- g J Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 D8 L3 s/ N; ~8 f( }; O1 e return EFI_SUCCESS;
9 |; y% o% Z& o, O2 p V }
$ \0 d/ g. _+ W' E, P' B" j y$ T3 S, ?4 M/ o+ |3 F$ k# b
FreePool(DestAddr); , K7 B& M' E( E- G8 b% Y; N2 T
" [5 N5 z6 T' j4 x/ ] Status = FileHandle05->Close(FileHandle05); H8 c- A0 O' Y$ i
if (EFI_ERROR(Status)) {
( }3 i. [: O/ W Print (L"Could not close file, FileHandle05 err code is %x\n",Status);+ ]4 N# N8 P3 }
return EFI_SUCCESS;
3 N ]" i+ }+ m' c8 W }+ y$ L$ S: A" I4 x
! [# N$ E; L/ f: p# y. I Print(L"File Name = %s has already been created.\n", FileName);
( R, G& [! j$ W' i( c$ w# q8 J* K: ~4 X
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");8 l& N2 j# v- V) [' i8 `, L
WaitForKeyOrReset();
5 ]+ X, ?+ v8 K4 i! J' h
: g' @4 r6 \/ J5 ], j return Status;: X: c; E7 u. {% D a
} |
|