|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*/ A e+ G$ Q* I; K# Q
* myfileio.c
6 \: ~% k' y5 f, V& C2 [ * Apps
3 u, X' I: a: l- w# ?! E0 m */3 b1 H" s0 u" c
2 d5 u2 y7 [* x T! I
#include "efi.h"
) O9 F* Q$ X7 v5 P, L#include "efilib.h"
/ P( b% w3 r. D" k5 y+ a, u9 g! [# x( p, D
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE; _5 b' l) W& ^% `, P Q, \0 O
5 |, B& y0 V5 N; L, K" Kstatic EFI_STATUS WaitForKeyOrReset(VOID)7 @; q' X9 q) [3 r- N; L
{
" R1 H8 [/ W: R1 \# C EFI_STATUS Status;
8 z5 Y& x# U+ T& x9 _ EFI_INPUT_KEY key;( B. O4 P3 T$ b7 z4 a( g
UINTN index;8 ^) l: c% |4 M i# r6 Y" U4 Y7 O/ g
, k) R! M" ~8 {# A7 B for(;;) {' b" H4 L7 b& H z; h( \" g
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);6 z/ Q8 D7 B) f9 M
if (Status == EFI_NOT_READY): W8 o7 D8 P+ m6 q
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
5 t% f g& [* B* t" n1 _ c( O" W else: m. S; U2 D) _+ c8 S5 T* h
break;7 b' k8 q. }6 W! {& v
}
" ~4 m" P' m4 V4 E& }# ^ if (!EFI_ERROR(Status)) {
- T8 n/ F& i8 D- k8 W if (key.ScanCode == SCAN_ESC)# w/ [; s6 T; m( P
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
+ Z- k$ U8 R( Y5 f }
3 G1 e# O2 L" E `1 u. K- o R$ d 8 l. _0 O! S" O9 [+ Z
return Status;
/ T5 s; w$ z5 O: a! {}
1 Y. S, i" a& T! B, Z: W# f3 N8 h2 l L4 V/ p8 t8 I$ }
EFI_STATUS
$ w$ ^& a/ o1 z! b3 X* CEFIAPI( Z6 g+ N% M1 C% z
MyfileioMain (IN EFI_HANDLE ImageHandle,+ z. C( E+ ^& [3 \( a% O
IN EFI_SYSTEM_TABLE *SystemTable)
% I) S# N8 X3 @. n6 U{5 o7 E& h6 a6 F2 t5 Y8 ?3 N
EFI_STATUS Status;! @' G3 J) A6 f* Y1 D6 z$ `+ K$ z
EFI_HANDLE *DestAddr;
9 v2 g/ @ k. e9 C4 }' J EFI_LOADED_IMAGE *FileHandle01;
5 Q, [& m3 A+ B6 t5 @) r$ @ EFI_DEVICE_PATH *FileHandle02; & \% V: P' o$ g1 e' A' R {' L
EFI_FILE_IO_INTERFACE *FileHandle03;
2 h# O8 Z5 e$ u8 i EFI_FILE *FileHandle04;7 f2 B2 s2 B h U; ^" `
EFI_FILE *FileHandle05;1 w: D3 t' v4 B) D7 l5 e
CHAR16 *FileName;7 S0 Q' `6 {6 ]
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
% G: b! q" B' o5 L UINTN BufferSize = 8, BufferSizeKeySpace = 2;, _/ I3 e5 l; K8 }7 b
int Bit0, Bit1, Key_Space;
1 ~1 b4 F) h" `% \3 K
5 b% r: {+ Z4 W7 V I3 k FileName = L"NewFile.txt";
# k. I" y' S" _- h6 D; K2 f Space_Key = L" ";7 ?& D y6 @& Y |
% [* R' [" i( E7 F: @5 ] BufferA = L"ABCD";' Y8 h! g9 u7 }( F8 r1 D7 P
BufferB = L"EFGH";+ w& }( f( E* ]0 F" |
BufferC = L"IJKL";
# C" q% l9 O5 p4 ?" E6 c7 l/ r8 _; T, W7 ?3 y/ r
Bit0 = 0xff;6 R U5 x, v- C# J: B0 J
Bit1 = 0xfe;
2 c6 t ?' i4 N
! e$ X2 {: j" J( Z9 S Key_Space = 0x0020;
4 q8 A) ?) |' D) d& ?2 B- d
8 ~$ D. S: \# ^( i InitializeLib (ImageHandle, SystemTable); & X8 Q# u* l+ N- S8 r* A9 L, W
`7 u2 h; u, y" }( k DestAddr = AllocatePool (BufferSize);
* q/ Q2 A; h1 g$ R' L4 G! k. R3 j' U& @. O" Y: E2 S
Print (L"Value of Bit0 is %x\n", Bit0);# M0 {: |4 a- V! G/ \& H& e$ {
Print (L"Value of Bit1 is %x\n", Bit1);
7 u ]; l0 F5 c* C
. f8 L, e% _6 k4 ?' i0 r2 M
. v- @" G: @3 w& x& I9 V1 e& D Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);9 f0 ?8 L3 d0 {" G
if (EFI_ERROR(Status)) { _3 P1 I5 B! o" Z7 J0 ]
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
. v3 |3 n( P) b+ B# @ return EFI_LOAD_ERROR;$ X! S( B; f7 m1 K( E! q* }" p
}
6 Q8 S }5 ~( j
- t+ J5 g6 D9 j0 H/ v$ s Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);4 w: `( u( y/ d4 Z% R, b3 d2 A
if (EFI_ERROR(Status)) {
' z* Q+ h) e4 k n" N0 E Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
5 O6 L% _8 u/ J) E return EFI_LOAD_ERROR;
: m( t8 j! n1 `8 ?% d }
/ P5 A7 a& e3 s$ _, ~& C8 z6 d 8 J: S- j" k/ E; b; O8 n
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
# C& e6 O% h7 y, V' @ if (EFI_ERROR(Status)) { L, F# s/ ^7 o' ]- h
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);; L2 B; ^$ t6 e% g' X; y
return EFI_LOAD_ERROR;
! m: K! Q/ c7 W- u2 t4 J# e! ` } u b4 U4 \+ `$ g7 o) p O
2 i3 w+ o( }; L4 L
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);' v- v% k1 H" {6 R+ @- o" c
if (EFI_ERROR(Status)) {
: z" _6 n7 r! t% e4 y' t Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);/ K+ | m" R' \; S" {
return EFI_LOAD_ERROR;& G: Y; G# X# ~5 \! g
} , m, X2 X" S$ ?' q
# O0 V4 g$ q3 [" i% b1 v& N Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
0 D4 `! S- E. t if (EFI_ERROR(Status)) {
2 b7 M+ p$ @' g0 e Print (L"Could not open file, FileHandle05 err code is %x\n",Status);* w6 h, {( ^+ D5 H8 v$ g
return EFI_LOAD_ERROR;
! ^- J3 Y+ T: }# f1 n' ]8 i& ?5 b @ }" T& P2 ^; k! u4 k, m- Z. }
" @: b6 O; h0 Y6 X Status = FileHandle05->SetPosition(FileHandle05, 0);
5 e3 k4 t# M( I: Y if (EFI_ERROR(Status)) {# _6 Y) F% O/ c& [. P# `. `# v
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);% {3 a% Q' B) G2 n
return EFI_SUCCESS;$ Z' J# k7 v# ?
}
- m4 V% @; }0 ]# d7 L* P k9 p
* |1 T; v. |. j% z) o4 ~( f* H Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);) k- o$ k1 m' q, ]/ ~" {) B
Print (L"File Buffersize is %x\n\n", BufferSize);0 m f! F# C( l$ B
if (EFI_ERROR(Status)) {/ h: ?% i' I) j' R" C6 k
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
# M5 ?* b5 u2 Q% `: e2 J# @6 e6 V0 G# h return EFI_SUCCESS;
( z7 ~" r7 ~1 L: q }
; y* n# Y+ u9 ?4 N! F1 K. s: {4 R9 S/ Q R5 Y4 L' T
Status = FileHandle05->SetPosition(FileHandle05, 1); ; ]) F% W* C# x1 p) |
if (EFI_ERROR(Status)) {
y: J/ m: X" u0 }7 P3 H f. R Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);8 v/ o1 \# Z5 ]* g* Q2 }
return EFI_SUCCESS;
/ e* ~/ {% k4 S9 d+ _0 N; f }! m2 s5 P1 e; n; ~# \9 w8 _
7 K$ r+ k% g: l- t; x4 h: q1 `3 D
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1); R: v( w& Z- ?. W# j( c
Print (L"File Buffersize is %x\n\n", BufferSize);
! C& c1 d* o: z; q- c" f if (EFI_ERROR(Status)) {
8 @. I1 f# t0 v" K) w Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);( ^# ~! j6 R% _( P
return EFI_SUCCESS;* j: \; R7 g$ k; h
} 6 l6 N l7 Z4 e
1 A8 R" l$ K1 K# ? Status = FileHandle05->SetPosition(FileHandle05, 2); ! d9 [9 J9 c- s; n' H
if (EFI_ERROR(Status)) {( @( c9 T/ k& H. C/ H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
$ Y* {8 a6 g* H- [ return EFI_SUCCESS;* V1 n6 H# z, h* U( I8 Z! a
}% ?- z! q( R$ P/ b4 F, s
1 x& a; F7 s( h2 s( X Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
+ A) z' ~( b, X- f2 X8 s Print (L"File Buffersize is %x\n\n", BufferSize);
% Q8 f: X; E& v2 n if (EFI_ERROR(Status)) {3 D V' t7 n& ]+ K7 I% ?/ i
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
% u& l7 k" ?* [, I8 D% D+ { return EFI_SUCCESS;! ~; m$ ~+ J |( j6 l9 v/ ] i# \
} 4 _) C# B1 n! u; ~: c. s
' `5 E' H2 p: G" {$ c$ ?
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
. \ O! P0 g/ a$ F1 q" k) K Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
) o# \+ k) g% y5 p$ ~+ v Status = FileHandle05->SetPosition(FileHandle05, 2);
& u9 V2 ~+ R9 a( v% |% I" M0 |& J if (EFI_ERROR(Status)) {
( P/ U1 c& ?* z8 T9 L$ z5 x Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
5 ^0 o( A; b, ^. P return EFI_SUCCESS;' {8 D; ^! ~4 @8 _6 V" v" I
}# H, V% t' G _, T$ h6 y1 M; K
' E+ a) j* E' `8 {1 s1 P1 R# E" B
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
" _6 M$ x, @/ W if (EFI_ERROR(Status)) {
# C; F* X! N% r Print (L"Could not read file, FileHandle05 err code is %x\n",Status);+ @6 T! a, Q g& F% D2 V
return EFI_SUCCESS;2 {/ a9 [. b( r* S) ]8 [
}
% ^. X, e3 K8 C; c' i) N Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);, ^# W1 d( t/ j, u0 W) L
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<4 J( V+ x# [; X# _# [$ Z: ?
' w4 D7 n/ K5 y ]$ a' {& k Status = FileHandle05->SetPosition(FileHandle05, 10); 5 R) {' V- C; j. W9 y) I
if (EFI_ERROR(Status)) {$ }7 o# x3 j7 t: y; `
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ O0 x4 [! W% U( }
return EFI_SUCCESS;: l+ a& o# ]5 _. [/ A0 |8 e3 Z) V
}
' c/ F5 I S& ?0 L 4 E0 T1 D @4 M; M3 ~
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);$ \$ G7 A4 H# ]( F; |8 ~# e4 N- ?
if (EFI_ERROR(Status)) {
% R$ S. D6 L s* Y# h; d Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
' \/ Q* l f7 N; g$ B# j return EFI_SUCCESS;- d! z3 b7 B; O' ~, b1 N" W
}# Q S6 j) s" l9 @2 O6 I# p
4 Y7 w& y9 W/ P Status = FileHandle05->SetPosition(FileHandle05, 12); / Y @: M, }4 m( n( u3 b
if (EFI_ERROR(Status)) {
1 M; }1 W+ p: x# ~ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
& Q9 Z! q+ {0 V, B. p. a3 e: v return EFI_SUCCESS;
7 f( B0 A/ i' Z1 ~' k1 W }
% n6 [8 G/ }" E O
/ A3 F7 @7 T0 C Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);: A4 `9 L9 y, C3 `' X# r
if (EFI_ERROR(Status)) {( H4 t# u0 C$ o0 X. l
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 |& [* T8 U) J' | a5 ]
return EFI_SUCCESS;% A. ~* ]$ H. d7 L1 {: R
}. _ X! \. Q9 m9 I
' S% x" N: W: i, S
Status = FileHandle05->SetPosition(FileHandle05, 14); ' c e, A f1 x/ U/ g# K9 D7 @8 v7 P
if (EFI_ERROR(Status)) {+ w. S/ I: P# [7 Z8 M( T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 [ B. V$ b3 G
return EFI_SUCCESS;! m4 K& J* Q* |! N, j+ D
}9 s, U! t8 |- R$ v* y0 i
# I9 A( T. X: J9 H" |* [
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
+ p5 H3 V. z3 q1 r if (EFI_ERROR(Status)) {
5 J2 G* s& s. O# r! ^4 R Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 ]- u( I% @8 j/ P4 }) G w return EFI_SUCCESS;
4 F$ X. Q/ [+ z }
- o4 W( S# s6 g' d/ B
6 K$ a- h9 X" Y6 R Status = FileHandle05->SetPosition(FileHandle05, 16);
1 Q; c; |0 U3 ` if (EFI_ERROR(Status)) {
. X+ J$ y5 P* z: p: B2 @7 z K Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);) R# i8 t% ^7 Y& i, W
return EFI_SUCCESS;
& q$ A U" u; ^; k }
2 }! q" Y: p2 y/ t% } - F! h2 p4 D8 h$ w$ j# Z
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! q: a0 `8 n7 V4 j+ Y
if (EFI_ERROR(Status)) {2 k# c& E# ?- c
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
8 W0 M: w. `9 |/ ~' g8 M return EFI_SUCCESS;7 j x! F: f' B! Z; b
} 2 z- i: G: P' W8 y& F% N: j6 X0 _- \
6 W: D9 F5 C. n5 s5 [! M//---------------------------------------------------------------------------------------------------------------------------------------------------------------- M! Z7 }1 _+ a$ _! K. F" `- B8 D
# h b$ V# B* H1 M p0 k- S* Z
Status = FileHandle05->SetPosition(FileHandle05, 18);
/ Q9 @9 c6 }: P8 s if (EFI_ERROR(Status)) {! K( O5 u7 S, z( {4 ?
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; @9 p' _6 m1 A4 g( x+ U: l return EFI_SUCCESS;
2 L; f/ L6 v& g* k5 r }3 p5 a* g: ?% g/ ?
. m, [3 O/ q! g0 n4 o0 k
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);. j: t) N, k4 w3 r/ L: ?, F S
if (EFI_ERROR(Status)) {) [2 }+ t8 P! A
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 m3 i' Q1 L. l" K6 p. S return EFI_SUCCESS;
A7 P$ G8 c1 ~0 R9 M/ I3 K } 5 p) l" B- ?% ~4 ~" n% c: W% E8 l
; Q+ ^: U% \: Y( { FreePool(DestAddr);
( G# r# W* h; Y' a* ^* j0 [+ U2 P; c, B# [! l
Status = FileHandle05->Close(FileHandle05);9 u3 C5 o9 Y$ k) `1 r; r4 W
if (EFI_ERROR(Status)) {
* }& w: B2 D [+ Y9 E+ X1 |, p# { Print (L"Could not close file, FileHandle05 err code is %x\n",Status);2 t. o! `. m9 z& b! n2 A
return EFI_SUCCESS;
) P' i p- k1 L8 w2 n2 G8 g R- E }
& t$ }; U- b; }3 J 8 a4 c) c9 K# a5 S% P1 G& {
Print(L"File Name = %s has already been created.\n", FileName);$ S+ a/ @% u! I9 r
/ X4 R* T- Z) P5 o- g3 J Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");' J$ R& ~; i( d+ ~% Y4 t
WaitForKeyOrReset();
+ ]3 w5 g% A& f q
. y8 o7 T, m( \1 r3 L6 I return Status;
! m8 f/ k6 E; c" T p; @} |
|