|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
4 \# {1 c" l* ?& {2 O2 U% O. t * myfileio.c
5 ^& t+ W& m" H6 E6 F * Apps; L8 P$ Z- `" S5 o+ a
*/2 t: A2 L* m4 x
: F$ N3 m4 ^$ {) ?3 v
#include "efi.h"
! X- }$ O( S' d& B4 b) p. _#include "efilib.h"
! R( h( \) C1 G
! |% x4 j5 G# }* a1 ~#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
% `4 R2 N# v6 w6 }) _7 p
+ U9 s4 `# D6 v9 k$ b9 B0 m# Mstatic EFI_STATUS WaitForKeyOrReset(VOID)
: {6 R; K. q+ }/ t{. U q2 \6 _% B9 f
EFI_STATUS Status;. L" T ]$ @. `3 `) `* Y& B3 l
EFI_INPUT_KEY key;/ i4 Y9 H/ n( u9 T% B9 [
UINTN index;
0 k( h4 S7 \" I; c8 x) m + r7 H! D4 c5 U3 L! h7 B
for(;;) {; J/ l9 ^6 ]" {- c
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
* Y8 A5 @3 S1 ] if (Status == EFI_NOT_READY)
+ |2 {, g6 X! L3 U, n5 L5 o2 ^ BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
+ W0 E) J& N7 `4 h* O# l! _ else
- x5 f2 e* i1 \+ c) p break;+ i/ c4 `2 r% S( |1 k( P: `0 H
}* o% N4 {. d, \% C# E( H4 [" {
if (!EFI_ERROR(Status)) {/ h6 j, ^5 s& H$ _* H" P8 _
if (key.ScanCode == SCAN_ESC). @! {+ w0 L, K" k% a. y
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
5 M' d; }* G6 ~- E# @4 u }
5 s! B; |% c: R. P8 M ( Y2 U, r& N! P, x7 {$ Y
return Status;
. d* _& e/ E+ ~; D+ P2 [" a8 g9 _}/ T9 z o; c, j) e D5 J
/ y! U# t( J* o ^% Q3 w& Q# LEFI_STATUS! M8 {7 X; v( H6 d0 I7 f# V5 Z5 V. r
EFIAPI. }/ q" o, [+ M5 I% x- O
MyfileioMain (IN EFI_HANDLE ImageHandle,
5 f+ ~; F# `! Z$ [9 c! _ IN EFI_SYSTEM_TABLE *SystemTable)! ^; E% x; P, B! u
{6 p1 l2 c6 ?( y( Y% S
EFI_STATUS Status;$ X0 X5 F' h, }1 ]; C9 Y
EFI_HANDLE *DestAddr;
! Y; R; j5 _& G) z EFI_LOADED_IMAGE *FileHandle01;
8 p4 |) F0 E# V ]$ P EFI_DEVICE_PATH *FileHandle02; / m0 D. y; Y o9 v6 Q
EFI_FILE_IO_INTERFACE *FileHandle03;4 N% o/ y4 N3 s. f) ]
EFI_FILE *FileHandle04;
; f6 v4 U, i& [( R7 p5 @5 {0 r# {( ^4 I EFI_FILE *FileHandle05;
; b9 X B' s& Q CHAR16 *FileName;/ W+ ^6 _7 `4 ^
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
8 O( b; e- X+ g# c$ h UINTN BufferSize = 8, BufferSizeKeySpace = 2;" B# V( d' Y, D8 {, o
int Bit0, Bit1, Key_Space; " [7 J t$ @6 z% o0 a" ?
& Q, A! K0 h: t% S6 ]- P" A4 p, v% f4 ` FileName = L"NewFile.txt";; ^. a' A+ d: S( h% s# q
Space_Key = L" ";
3 K% r# l' @( F/ W) b% D: \2 |1 E
# }" a* w- w; [$ J9 t BufferA = L"ABCD";5 t: X' Y0 A7 E9 A Q' [
BufferB = L"EFGH";7 n! M# t( e8 f3 G& l
BufferC = L"IJKL";: ]! v# F+ ?3 n/ H
* {- N/ u% w( J e" k
Bit0 = 0xff;
% U; w+ ]/ h! u# u0 Q% M3 A6 j Bit1 = 0xfe;; H% x$ e- \0 A
2 J& v( W1 V, n3 U Key_Space = 0x0020;1 }7 `" m c- }. `( x
- }# V# v! o9 U9 f
InitializeLib (ImageHandle, SystemTable); % P1 X" D/ q1 B5 d; R# X9 f J
! v4 ?& R) B# X9 _4 _! F
DestAddr = AllocatePool (BufferSize);
9 @3 k/ `6 _! @2 J, K0 j* m8 n& w" @
Print (L"Value of Bit0 is %x\n", Bit0);0 w8 S F/ K9 ~5 [' ?3 S* n- B
Print (L"Value of Bit1 is %x\n", Bit1);
8 g0 i9 o; P# F: y
7 Q' o* T, U& z * C+ N3 ^. L, w; x8 p. f4 K' C
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);, B9 W/ i/ N* r; n
if (EFI_ERROR(Status)) {: i7 F- u/ O' `1 b' e+ N
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
& h+ N9 e4 M. M% J return EFI_LOAD_ERROR;
+ p2 r& }; I1 u9 }- k* |' Q, g: ]9 y# x }- _; N% Z7 H- V; x) x
3 ^2 _9 s# P& s! C; H! ?! p. R Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
; H: K4 U1 R# @: V R: F if (EFI_ERROR(Status)) {
! @" E2 p. [1 F2 l! f; V Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);6 X O/ h9 E8 w8 M6 H
return EFI_LOAD_ERROR;
" @8 L- C0 o% q7 ?/ O } " B1 S. P# B v6 \3 c3 H. |; h( f
6 h, x* w# j1 V0 ?( k Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);( D$ n& h, q8 Y) P" K5 g
if (EFI_ERROR(Status)) {
6 ?* r; t9 B5 W Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);+ F! y% S+ r, J9 R
return EFI_LOAD_ERROR;
+ q: f, V, V7 F- H }' g" v- ]$ r: r! D
* u# ~# z o) E5 _4 Y Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);* S/ |6 D8 _( i$ a
if (EFI_ERROR(Status)) {
' {9 v6 k7 {3 i; v Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);, P! H# z" H3 H: D
return EFI_LOAD_ERROR;
% V1 G6 q* |+ {# r2 s# j } * ]2 g4 k5 u: u9 K
7 g7 U4 r8 R) |! O. H5 ?$ v
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
, t7 v" w- v3 @# |, \ G0 z# n if (EFI_ERROR(Status)) {$ q7 M3 i& w: A- |, V! o- Q6 }
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);( F% `, s' p7 s* w
return EFI_LOAD_ERROR;
/ \$ n# m& x8 i. J* e9 L0 \ }0 K' f& N! L5 V4 G- Z
7 {+ C- A2 c: _4 f$ C9 p: ^, @. q Status = FileHandle05->SetPosition(FileHandle05, 0); + h4 D5 s4 l5 |) |/ b, L B
if (EFI_ERROR(Status)) {3 m0 [: X. s; ~+ n; `, Y) j
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& O* u7 U9 X' l return EFI_SUCCESS;
* b: \9 s& V9 U: W: L }
4 Q' [% x5 [5 a- q: a# V' C
: ]4 H2 P/ w& r( o4 x- A6 W6 N Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
. g* S% B- V8 k. Z; ^1 e Print (L"File Buffersize is %x\n\n", BufferSize);
) l& x+ d% m$ g) N$ l if (EFI_ERROR(Status)) {. T1 x% c: C' \$ P2 }
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# R, Q6 Y8 Z" @5 V, _- L9 E+ } return EFI_SUCCESS;
0 f1 y! W, F# _ } / d4 n$ r8 i3 o3 }' O& J2 C
1 z3 n! V7 o! W6 y/ Z1 a3 U* @: V Status = FileHandle05->SetPosition(FileHandle05, 1); 6 J8 |8 D7 `% o' a9 b, w8 Q/ Y
if (EFI_ERROR(Status)) {
2 w. x7 k1 `3 ^* @ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);& M! z: e% C, c/ t% Q/ Q
return EFI_SUCCESS;& P! F8 k5 Y0 m
}0 n2 w8 D7 c# a% s8 q9 O( h
1 ?+ N" @/ }" s k' i Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);. J$ o6 r: C0 j. Z4 b% B8 ^9 N4 \
Print (L"File Buffersize is %x\n\n", BufferSize);- x$ q& F9 h- b
if (EFI_ERROR(Status)) {& z" f( H0 u' J0 S3 |: ^
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
2 D0 A4 ]9 a: }9 D9 c$ W: g return EFI_SUCCESS;5 C8 X* [) B2 @$ A3 ?
} 5 k+ L" V* ~7 S+ e
4 R( @" o- X2 Q U
Status = FileHandle05->SetPosition(FileHandle05, 2); , e2 s* A' n& B3 f% `9 i% ?
if (EFI_ERROR(Status)) {
/ I2 g2 `) w" u" J- r( z% N Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 O3 m7 n, s* Y. {0 k
return EFI_SUCCESS;5 W6 E4 N; g0 G& Q, R8 q2 m
}; L9 Z" y3 I! V7 |
; C, r; I8 c. P Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);' ?/ Q5 j* B8 e# E% O4 o; T: ~
Print (L"File Buffersize is %x\n\n", BufferSize);8 H$ t4 {; o1 Q
if (EFI_ERROR(Status)) {8 H8 Q% E7 y, E9 ~
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);. c0 f& U) Y. Q. f
return EFI_SUCCESS;7 C- H+ e w2 w; {! H! P; t
} % }$ _$ a# g* H) {
! g) G( e6 T$ r' p" Q+ r& B, V0 F! J6 J6 k
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
& M+ k7 i! D1 s9 u Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);5 P( |' `/ k+ M
Status = FileHandle05->SetPosition(FileHandle05, 2); Z: W6 f( M. {. R. D% R
if (EFI_ERROR(Status)) {$ j( ?7 m$ o. G6 A
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# c7 b! [0 V }
return EFI_SUCCESS;9 B: }* { x( F9 x1 p* s, N* M% D+ n
}
! _8 v+ `* R0 Q
: a+ o% c2 i$ g% u* X9 o6 t+ ^4 m Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);- R1 D7 f' G% ^7 W
if (EFI_ERROR(Status)) {+ O( G7 S% w' Y( l2 F
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
( h6 [0 x' _! k return EFI_SUCCESS;
Z: r# _+ `9 w% U } # u) Y! K8 a/ ]& P/ n
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
( }, z8 e- z( r, J) o$ L' A! S//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<' G$ |* f+ l7 G* f$ N' G7 B# Z) i0 P
+ J3 J" V+ \% ?$ q; M Status = FileHandle05->SetPosition(FileHandle05, 10);
$ T4 Q3 G& H" o8 z if (EFI_ERROR(Status)) {& L3 N" g) A! P3 u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);+ u# D% I( g+ v5 ^6 n3 y
return EFI_SUCCESS;1 Y) e* f5 [( ^# J- v7 X
}2 m( I5 ~ B! j. m; [. v2 |
7 [: L e$ T. c3 u; q. z2 u0 a* H
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
1 D- l. a% b* R" C& F' f if (EFI_ERROR(Status)) {6 J# `% t' B, F0 a
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" c/ O+ `$ F8 [9 u9 U return EFI_SUCCESS;+ L" f9 G% K1 [$ H& s# z d; [3 s
}3 z/ T6 r& O V$ O; u* ]
( G: i5 w6 w/ c6 b
Status = FileHandle05->SetPosition(FileHandle05, 12); 5 q: U0 L# M5 O) F" ?% @! U& P
if (EFI_ERROR(Status)) {4 X4 f1 [0 J }( H0 \) f0 U& L1 h
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' S w+ F4 \. z, h2 u+ ? return EFI_SUCCESS;; F) i) S1 O/ N4 D+ v* e7 r+ I' `4 ~* g
}$ Q2 ^/ n2 S4 P9 N
: a6 v! c: A8 M- r d# o
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, c. H" r6 I" j. `" x if (EFI_ERROR(Status)) {
# \* D# f6 Y3 U) j" k; U Print (L"Could not write file, FileHandle05 err code is %x\n",Status);$ R8 P& h" y, z7 [4 G* V5 f- _
return EFI_SUCCESS;
8 L: l- g4 G _4 L- @( e& o: N( M& Q }
) t" X( L/ X& [6 R$ l* _
2 ]% m( }7 O+ r* z/ k$ {1 ?- l/ ?$ W Status = FileHandle05->SetPosition(FileHandle05, 14);
7 r3 x* y4 w4 u& @9 z( h if (EFI_ERROR(Status)) {
( J5 V @) n! T) I7 {% Z( M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 f8 i2 u9 y/ x5 E; ?% F& Z) D
return EFI_SUCCESS;
* H$ F* Q/ N* z! ^. l8 G9 _2 T }3 x2 } _& A& s# R
& i; [- `4 O) i7 G8 L Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);- O$ R+ u1 {2 H1 F
if (EFI_ERROR(Status)) {. ^4 E3 T+ c3 }* A" u: B
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ q; z" R5 j) L0 F+ n return EFI_SUCCESS;
$ X9 n; K9 M9 F4 u6 E6 f }
1 g5 _' V. h- N# w; p! b+ a ]! b( u0 f% @, P
Status = FileHandle05->SetPosition(FileHandle05, 16);
& i$ l0 o+ j8 |: ` if (EFI_ERROR(Status)) {$ h; [% f) c( y G7 b, E2 E
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
( W; r! |$ Q( M- X7 F return EFI_SUCCESS;
7 l8 F4 ]1 `: o& q( E0 o }
8 a6 K6 u+ ]5 |: V6 ], `0 \1 a $ I/ d9 a' l" W) U! F/ W/ t0 ]
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
# }- ~. v( P7 n3 l if (EFI_ERROR(Status)) {# F+ K9 m% f7 g: h$ L3 ?; o0 O
Print (L"Could not write file, FileHandle05 err code is %x\n",Status); x$ c: v' l7 z4 k
return EFI_SUCCESS;
( F0 T5 Q2 B1 m/ t! O } $ v+ V. \7 y" v6 Z6 Z
$ C( T: y) [6 b( F$ {) C
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
6 h* g+ R3 \$ P0 k
) h; z; q/ S. _+ q5 W, l Status = FileHandle05->SetPosition(FileHandle05, 18); % Z" z- {4 F" s# n6 G% }
if (EFI_ERROR(Status)) {
! a0 _- [) _/ L$ i: W' M Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
4 s/ v% Z- h0 M& @% U, { return EFI_SUCCESS;
0 p* k8 g" ]8 m( t& m1 y }
1 h, a8 n# S( X; G; w9 v
& L+ f# D8 L5 v7 P; U* o7 Z Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
7 w9 c) ?4 v0 {' ]# I if (EFI_ERROR(Status)) {
! V% A) g% w7 |( i# D) R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
/ Q! g- K3 K, ]6 h; n return EFI_SUCCESS;! q6 l: g* I1 B3 A
}
- s. s0 j! G& i5 {$ N0 ?+ x
) G: z; M" v/ Z2 \$ _ FreePool(DestAddr);
4 _* B* V0 E# X( h7 |$ Q
& x) b9 V7 J0 }& g Status = FileHandle05->Close(FileHandle05);
4 h) C) l1 `3 N8 ~4 ? if (EFI_ERROR(Status)) {
- W L G1 G! C( w. R F! M% k3 S4 o Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
! ?/ h1 s4 ?8 f% _ return EFI_SUCCESS;
' Q, e" l; }0 g1 D8 r3 V$ ]4 J, g9 j }
9 D) s9 j( {5 q3 b+ c) D, ^ 2 W/ f: a) P8 i" G% W
Print(L"File Name = %s has already been created.\n", FileName);
" ~5 w- c4 G9 ^* u; a. K3 }2 p! h$ a
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
( w1 e( V' _" X8 y% e WaitForKeyOrReset();! o) o+ M- G8 b* z
6 F) p/ I- z7 e7 ?- b: x
return Status;
% `! }4 \; h+ a2 O7 d} |
|