|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/** y# y& h3 a( V2 {5 F- h9 |
* myfileio.c
; z( y/ @7 w3 ~% C' r. q * Apps1 W# E D8 p9 c- |
*/
6 O) i# B- |* n4 x$ C7 P, g! I4 P) m! \2 X
#include "efi.h"
% ~% H4 D: h Z5 X2 s#include "efilib.h"
! G6 \8 W" Y' b Z
8 L! w5 ]" G! B$ k1 J* r. e#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
$ }3 O! o9 u' }5 V/ j# A; X
( p2 s& z. }& V/ Z- P8 xstatic EFI_STATUS WaitForKeyOrReset(VOID)
3 I8 G% E, f& Z/ e$ `- i8 U{2 ]! l6 n- o5 r4 Q' [; L
EFI_STATUS Status;
7 j! M5 Y p5 m( H0 Z4 A( z EFI_INPUT_KEY key;, V$ @8 \$ Q) J# m! e7 Z
UINTN index;8 W; i& V/ z$ e+ }. r
; R3 Z* Z$ h4 [( q+ I- O
for(;;) {. Z9 O9 o4 B3 z
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
9 _1 H' F- }+ F if (Status == EFI_NOT_READY)
( p! Q. k0 Y9 l BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);: q- B1 Y0 D2 ? s6 s/ J0 W+ B) L
else* A/ k% Y# K$ ~) i
break;
" X6 u6 R- W0 O4 Z! i( L0 Z" k }* K1 U/ n" z: U. Z/ F' q; a
if (!EFI_ERROR(Status)) {4 n) q: t3 Z& P7 z& J1 N; E0 I8 ?
if (key.ScanCode == SCAN_ESC)2 M7 ~! e9 x7 k; _" j- [( C
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);& }# g! }8 M# P3 `
}
' g- x4 W9 Z: B; U. v% T
# { c% l0 F4 C0 h! V, a/ E return Status;
; y* n, A; g) ^/ q0 x0 @1 \# U0 B+ a}
M9 Z4 c8 Y5 \. J
! V& S$ t& U- L: UEFI_STATUS/ ~. v1 {2 J$ a: U; e+ g
EFIAPI& Q5 g& F! l2 h. N0 F% |9 Y
MyfileioMain (IN EFI_HANDLE ImageHandle,0 V6 k# l7 x1 H x
IN EFI_SYSTEM_TABLE *SystemTable)- _- J$ }- e. L( L% C1 ^+ F( U# Y3 z ^
{
9 m/ ?0 ]$ S3 p o EFI_STATUS Status;
0 C2 X' ]7 `* g+ M. q4 Y EFI_HANDLE *DestAddr;
- B' h4 [4 f. q b% J. x4 f6 P$ r EFI_LOADED_IMAGE *FileHandle01;
$ }2 [- W* p# ^/ M EFI_DEVICE_PATH *FileHandle02; & F5 i6 F: F9 V1 U+ _8 U! Y; Q
EFI_FILE_IO_INTERFACE *FileHandle03;
% }4 r7 T, F. k EFI_FILE *FileHandle04;
4 Z5 `/ c0 E- A4 m EFI_FILE *FileHandle05;8 l/ S) j/ L7 \7 |
CHAR16 *FileName;
7 W( v) F3 F9 o N0 u5 E CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
( ~4 K6 n4 `3 j# b( q. g5 l UINTN BufferSize = 8, BufferSizeKeySpace = 2;
! }0 l1 k# k K5 N int Bit0, Bit1, Key_Space;
6 y' P; j$ [- p" q( h. E! h5 V% w. [7 g5 M' J* ^, @( f
FileName = L"NewFile.txt";' Z8 e: P/ `; U% y/ B6 d
Space_Key = L" ";
A4 C. [, A: O" y# K$ s# V+ d
5 G' y7 M3 [# `8 J6 u4 H7 L$ _$ x BufferA = L"ABCD";" g. E( f G" {) {4 {0 [. E' G
BufferB = L"EFGH";
8 Y, K, c; o- T: Q BufferC = L"IJKL";2 T6 ]6 L3 h6 _
0 I' w3 o' e* C/ i- [0 W Bit0 = 0xff;
3 E; i: U& o$ V# v) g/ g Bit1 = 0xfe;; \/ h, a$ `9 c' K( _
( m& [* r" `1 K% [2 i. `7 x
Key_Space = 0x0020;! Z* w* q6 a: y8 S
4 k h6 Q+ o( x" Y, F- }7 Y: V& \" E
InitializeLib (ImageHandle, SystemTable);
/ `8 b A+ o( r4 `6 o) n' o( |* Y6 k- [* l7 r. T* J
DestAddr = AllocatePool (BufferSize);
8 F: r$ O6 i3 ~+ r$ s5 e$ S( S% e- ]) ^1 P9 j. P8 \0 ]
Print (L"Value of Bit0 is %x\n", Bit0);* f. q& l9 o) E; G
Print (L"Value of Bit1 is %x\n", Bit1); 6 B( B7 Z& }$ B6 D* Q4 o/ _
* F- g! R7 @7 W, P / w8 M0 g' S( p& ~
Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
7 ~3 a* f" R% ?' a if (EFI_ERROR(Status)) {. s& P! \0 E) Y& @7 `9 f1 x
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);8 g" I/ A# N U& B# d' T
return EFI_LOAD_ERROR;
9 u( [: |9 l3 u5 U W1 A4 a/ s }
: u9 I; m4 w R# B
0 H3 r$ V" F& w Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
. f5 }. g6 P o( V" A9 i# G if (EFI_ERROR(Status)) {0 ` H: v3 N* N+ F; Q
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
" G- y. h& V+ u return EFI_LOAD_ERROR;' ~# m/ }( l9 N3 n* g
} ) C1 w$ k5 z% O& p
' G" }9 J) F* } Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
* s3 O& z, n# ?& R0 h0 r' G8 L( Z if (EFI_ERROR(Status)) {
$ @6 p/ p, P& d' k' x Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
$ m% L8 P* R: n5 \* s# ^ return EFI_LOAD_ERROR;
; ~/ X" r7 T. Q D }; c5 I2 c3 y) H+ V# c
3 @6 W7 H2 s* a5 [! n; ]4 u
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);) w( L m* Y# a; ?# e
if (EFI_ERROR(Status)) {
* h5 M; B2 i, G: K0 s- n Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
# J, q- X" ]' B; m$ `' r7 [* p return EFI_LOAD_ERROR;; F( c7 q8 i$ a. Q4 ?
} ! c! `! r( H9 j6 S
- _. F4 [5 q+ v0 R/ d
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);4 z: q% j2 t, @ C
if (EFI_ERROR(Status)) {
6 J1 a6 a+ ^2 S4 E, m. y6 U9 e Print (L"Could not open file, FileHandle05 err code is %x\n",Status);( }$ ?2 m/ S% a. F, G1 j( G* K
return EFI_LOAD_ERROR;
" u J0 X0 m. H! f7 ?6 A0 y' F) Q }9 v4 c Z6 [. Q1 n) d. ` @ w
# q5 m4 o, b) P, J Status = FileHandle05->SetPosition(FileHandle05, 0); . J( S7 m6 ?: [- q, H$ {3 L
if (EFI_ERROR(Status)) {/ q( ]2 o( c' I W2 R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
) P" Y& Z2 d) d5 U5 y0 d) f return EFI_SUCCESS;2 q6 M& u! m' K5 b
}
, k# G9 R6 O. P& { ( Q8 w3 i! C7 B
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
* @1 b( a. O) y3 V- m9 a Print (L"File Buffersize is %x\n\n", BufferSize);9 i0 q$ c2 C4 _ P$ X! S b
if (EFI_ERROR(Status)) {3 U% O$ L4 {' Q' Z# ]
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 P4 S9 a5 C3 t; u. E2 v return EFI_SUCCESS;8 W& H( x+ t8 H2 `, g6 p9 j
}
8 c7 o. T- ]- A) K) J7 J# ^, e6 J3 N1 N- D9 O! _7 a
Status = FileHandle05->SetPosition(FileHandle05, 1);
/ Y! i* P" O% l6 _) f# D if (EFI_ERROR(Status)) {) D( c- }) q5 \$ B) {$ @
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' p' o4 K i4 b5 J* j return EFI_SUCCESS;* Z( U& A7 n: s7 K
}8 I5 I% t" k9 A: j9 _5 i4 V
! m9 @9 C( b* E8 x/ ]4 q) Y Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
" w' Z& M6 g' i9 ^2 j5 I; ^& L Print (L"File Buffersize is %x\n\n", BufferSize);) A% h7 j2 h; g
if (EFI_ERROR(Status)) {
2 }. K* Y3 n! c! X5 N R6 T Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
3 J9 A. _* u. `9 n return EFI_SUCCESS;
9 U2 N& ~) W5 \2 f } 8 k$ x: m- _1 O: S6 V
$ k: B+ u! k" l+ |
Status = FileHandle05->SetPosition(FileHandle05, 2);
: }9 l9 j* a* x! M/ A if (EFI_ERROR(Status)) {* I( |! {0 l/ N. t V
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 z/ G; A p# j$ @. M0 Q, X9 e3 B return EFI_SUCCESS; y* ]7 e4 K7 P
}
. o$ L7 H' J' z! ]
' V9 R# f$ ~/ O4 R! D6 F Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
5 [! b8 W, P, s/ @ Print (L"File Buffersize is %x\n\n", BufferSize);
* Y o5 X [7 ^- W if (EFI_ERROR(Status)) {
0 X w3 \3 Y& B; R( Y1 ` Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);' p% |# s8 E6 v) Q
return EFI_SUCCESS;
) J' u) C4 {" `2 }) s+ g } ( i8 b3 E0 I1 m) Y1 t
* G% s9 P6 o' D8 l# C+ z7 z9 x9 {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>> {, Y& h- r/ V }, @9 b
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);6 P" R1 w! O4 H& e5 U3 s
Status = FileHandle05->SetPosition(FileHandle05, 2);
, o0 F) T0 m- x# D/ |; G- K; U$ ` if (EFI_ERROR(Status)) {6 i$ t# n& {) w
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ \* H; |) B3 w7 A9 p" y
return EFI_SUCCESS;" \0 z2 S; E1 B! ~
}
$ {, i) d, B% {. a
' ] F+ {4 o6 ^5 T: R Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
7 r& f2 I4 p7 _* q: c if (EFI_ERROR(Status)) {
C6 c0 w0 v# X4 h( G Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
6 E& ]( y6 v2 A+ i return EFI_SUCCESS;
" B$ a" F3 G& ~ }
9 O! q1 E# Y, \9 p Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
6 W M9 f! y. t% ]1 I//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
. L: k: y( Y+ {0 g" |$ m7 T3 p# q* r6 P, ^& U) v/ i1 Y, ^
Status = FileHandle05->SetPosition(FileHandle05, 10); - B+ [8 z( p: X7 Z( h: J
if (EFI_ERROR(Status)) {6 {6 w0 r& i2 c: H) M( T; n6 E7 v
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 h$ \# u; ^: g( @! g
return EFI_SUCCESS;! K) j ?/ p# P B8 w
}' C: ?; r K; |) i
6 \( z- p: N4 @2 t Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
4 s& g& ] g" ^, D( L# s% I! o if (EFI_ERROR(Status)) {, A) E! C3 `- H/ H
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, g& Q( L% a9 c) N% d/ J0 U! P3 l return EFI_SUCCESS;
/ G7 P1 g/ P; e. j! @ }/ N( H1 |4 y$ h8 j* w
4 p% z$ M% `4 G9 u Status = FileHandle05->SetPosition(FileHandle05, 12);
# n# G+ P, [! v: S+ H if (EFI_ERROR(Status)) {
; `8 Q8 n5 `1 j. k6 ~3 C Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 J7 R' _' O0 M r4 H( A return EFI_SUCCESS;
6 C: p; I* k' D# G( [% t5 Z. K }
' S* V& G- V# x
4 ~/ F: k* R% o; r5 ^2 d; \ Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( p- N/ c$ t# D3 ]& N8 P if (EFI_ERROR(Status)) {" ^3 t, ?; _0 }
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
* B0 k, H7 @1 g" s) I U return EFI_SUCCESS;
+ v+ b* ], `# N2 m }; n4 o+ y" r& n7 X* s: r
. b" z% q7 r/ k
Status = FileHandle05->SetPosition(FileHandle05, 14); ; x9 k& w/ G T5 b) L4 O% `
if (EFI_ERROR(Status)) {
7 u0 ?' h7 {, N3 |& ^1 q, n Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);6 X" x# ? K! E. l9 V- c
return EFI_SUCCESS;
4 t9 Y$ V* c5 [( X }
) o, v$ k+ X" _$ F; v+ K+ z1 ^& @ , Q% Z7 f0 ?# C+ Q% v* A3 w6 H$ R
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
2 o b( m2 [6 p# p6 R/ w' W' A if (EFI_ERROR(Status)) {
4 `7 R! j \% {( E Print (L"Could not write file, FileHandle05 err code is %x\n",Status);& h: V# K- `1 o5 ^
return EFI_SUCCESS;
4 k# S2 y5 Q! G0 } }/ e+ M8 j$ S: I
: c% n+ C! X7 L7 r2 b, N* U# C
Status = FileHandle05->SetPosition(FileHandle05, 16);
5 f# P7 b: A7 W* Q9 W if (EFI_ERROR(Status)) {5 _5 D- F. W1 t! R- I/ R9 f. Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
" d# z8 e8 ]0 w; f7 `0 l/ N return EFI_SUCCESS;
# \0 ~9 ^4 l+ J }
/ f8 m3 B" _5 _5 { 0 s0 f# z% J1 w$ Q' ~. c' h$ |
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
( ?8 N* U: A3 {) c: e S if (EFI_ERROR(Status)) {& _9 I; Y. N+ k# b
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
4 |' `$ g* h9 }+ L( r return EFI_SUCCESS;0 @3 C! @! q) J: q+ Z+ k, M5 b: k
}
2 Y% ?, h2 r5 k; M. k' ~4 r: B
+ H j% h1 F2 a; v1 D; `//---------------------------------------------------------------------------------------------------------------------------------------------------------------! J3 D0 q! b2 W! u2 h/ ]
# X3 w6 N( \# q5 [ Status = FileHandle05->SetPosition(FileHandle05, 18); ( r& i' U2 d; q9 i' C
if (EFI_ERROR(Status)) {
3 i& t6 Z0 j8 Q3 r; _ Y3 o Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 ~- @9 Y- _% y5 R1 Q' J
return EFI_SUCCESS;- ~. n1 g* {1 ]0 `
}
5 b- v' w3 Q0 Z9 ^6 l9 C m+ x + b, t; V! n" |4 w
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
+ A! E: e% W! Y7 ~. M% e/ d4 D) L if (EFI_ERROR(Status)) {
- M' {' t2 e; \( Z/ B/ w$ O/ o5 S Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
9 y1 q b, S. u, V* f return EFI_SUCCESS;
; s2 Z1 h2 }7 `, n }
' s; q/ Q; |2 P3 y% s- _ 6 U+ L3 @8 K9 X/ t2 q# g% `; o
FreePool(DestAddr);
% g5 A- F: q% k/ W8 ]# u1 v5 r+ L" x" u8 k8 M* }3 F* N
Status = FileHandle05->Close(FileHandle05);, G& `& ?& S, s' W$ ^5 s f
if (EFI_ERROR(Status)) {8 a2 b, \+ U6 l0 i! W& u" I
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
2 R; ~; _1 G; w' p2 ]( z, C return EFI_SUCCESS;
7 t" |1 G `) M* f }
; \( w3 ]7 d1 F U: y. a0 k9 B m# ]8 F5 y% u
Print(L"File Name = %s has already been created.\n", FileName);
# C) b- q( r' l/ E3 n3 \: d
, }( M3 t# m' X0 |7 }6 @0 i! c% t Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
2 u, I8 L, \% y. l. L- m+ Z WaitForKeyOrReset();% {+ U8 p2 B5 I' ~9 d+ n4 z4 }; o
- g4 A- g8 x+ T e5 t; e
return Status;) `; x: S6 `3 `
} |
|