|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
9 @4 i# v5 } X! s' V * myfileio.c
& H1 `6 D# |! y5 G0 { * Apps
6 k s8 v k) T# V! ? */, a4 `1 a6 h/ T' s
- B- F! R5 F9 ]6 h7 e#include "efi.h"
* c) M+ N5 M* L9 I# }, J3 u2 r3 T#include "efilib.h"1 r! R/ ~) x4 t2 h- @
8 o0 {8 x( v* B
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE9 l6 Z( ]9 ?6 h# ~
* v( w( K c( R7 Kstatic EFI_STATUS WaitForKeyOrReset(VOID)) w' u. y0 N' }6 f5 n/ \" n# G
{# L' T% P3 c' o& {
EFI_STATUS Status;
; C- }! T1 y$ c4 z% m& f EFI_INPUT_KEY key;
, C6 b# u4 }- j UINTN index;
[, {& N! z7 d7 o M2 A
/ m( l7 s# \8 Q8 p+ i; \- o for(;;) {
( s. t- o; x% B% { Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
8 S, S) v' q9 w7 Z% N3 f+ ^ if (Status == EFI_NOT_READY)* M8 l( e5 e z" R I
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
' \ J z6 G# E& L else8 o7 [0 t# X. W
break;
7 z$ _% o' M# m7 P1 E }
! t7 b5 k5 e% m; O& R5 ^7 T- m if (!EFI_ERROR(Status)) {
. m* d( x% A; q2 j, B if (key.ScanCode == SCAN_ESC)1 N3 G/ z% h/ Q8 S' e
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);; @( U( x2 `( o2 X
}* i0 j5 B4 Z* @: [5 J
! \6 x+ q" T- W" I& C. U
return Status;8 H0 E4 C( V9 t; a; B) q l" F: _
}
- }( }! U8 j* U$ ]( A6 T7 f+ v& q
, N7 P1 c8 B: A; SEFI_STATUS
8 i- N) x8 R- L' N' ~EFIAPI
3 h, I% H. z! h% t& AMyfileioMain (IN EFI_HANDLE ImageHandle,0 L/ H2 n! N1 r# ~. ]4 B$ `: D
IN EFI_SYSTEM_TABLE *SystemTable)5 ~, {; q0 i" |6 ?4 n
{/ w% Q: y4 z3 u( n% C. G
EFI_STATUS Status;
3 o) A& p. ~+ v3 [5 q* h" A1 v EFI_HANDLE *DestAddr; * y" ~% z7 l' u& }8 t& a
EFI_LOADED_IMAGE *FileHandle01;( c+ ~% l: v* Y* B5 Z
EFI_DEVICE_PATH *FileHandle02;
+ k6 S! X9 V1 ~) f4 _5 C EFI_FILE_IO_INTERFACE *FileHandle03;8 [( w( K: g1 M8 b
EFI_FILE *FileHandle04;
5 b* o. v" v2 y EFI_FILE *FileHandle05;; f+ Z4 e* o; Q5 b0 _* ~' E3 i
CHAR16 *FileName;. q3 r& |7 N) m4 b# q {5 K. T# R
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
1 S/ E! m) K: C9 {. B9 Q1 W! V9 ]' ] UINTN BufferSize = 8, BufferSizeKeySpace = 2;
8 e, \0 w/ O1 N9 ^6 v; {" N, |5 \/ y. { int Bit0, Bit1, Key_Space; 7 D8 |8 ^* Q3 j$ I/ r( l/ ?
; _2 |4 U: i2 W U; x* l FileName = L"NewFile.txt";
, h) r; L+ f2 b! j+ U/ S* ] Space_Key = L" ";
$ H. s. S5 i- `1 g# F 2 Y* m" T9 N5 I: l1 z
BufferA = L"ABCD";2 u4 w& A4 `$ J: O
BufferB = L"EFGH";
4 L+ L; X" }7 j1 M' J BufferC = L"IJKL";4 `+ D# ~/ f/ l, I2 I. G
0 z8 x& [* {; Q @9 k
Bit0 = 0xff;% @3 A: {- E. w6 O
Bit1 = 0xfe;% Y- w* G4 n1 J; b! r
2 a% s, e! _- p' m Key_Space = 0x0020;
& `4 O2 k4 R% T" `, Q0 \
4 |& h! W* z# }5 B; R InitializeLib (ImageHandle, SystemTable);
- e3 f! D: s9 s; c9 [5 ~* B7 x& a% G# x. X* ^. S0 m
DestAddr = AllocatePool (BufferSize);
[" E1 l+ P) I: a z4 z% V" w6 b; m# [8 X" f2 t( V1 ?. ^$ Y
Print (L"Value of Bit0 is %x\n", Bit0);
% F+ C% n( w. J: N' B5 j; n+ | Print (L"Value of Bit1 is %x\n", Bit1);
3 z- v7 W! n0 p* w g. W% j0 i. a
. z. P; M' J1 E+ M5 [& I
& M/ e) n8 ?: c$ |' q! r1 c7 X5 M Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);7 M' ~1 x0 f; f+ A
if (EFI_ERROR(Status)) {
9 F) h* |- x( }/ r Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);* f/ O/ S: N- \
return EFI_LOAD_ERROR;
- L, u5 ~' ~- T) t: i4 _* { }
3 ]/ W% \; G3 t4 R
* J$ t5 ^9 t* ]; \ Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
8 B9 }* r; G# H7 c5 z if (EFI_ERROR(Status)) {3 `. i5 |* I9 w$ q' \8 I- s6 O
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);# S1 L+ J" N) O9 L# Q
return EFI_LOAD_ERROR;' c5 B% I5 N% W! E
}
9 W1 c4 h& }) J' {4 w/ S, k0 j $ n8 h# g0 _+ E' a7 L/ s
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);4 E( w; ?- j) g( R
if (EFI_ERROR(Status)) {! V# M6 s; \* b; q+ l
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);5 P8 L% S) g8 k7 ?
return EFI_LOAD_ERROR;4 y5 ^! K7 |5 y
}
! d) S; X3 H; Y5 Z0 {
) [* v( t; F; I z, G1 ] Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);: J$ ]. P) X/ ^' C* V! d$ h' Z) l
if (EFI_ERROR(Status)) {
# {1 f! F! u) {0 P Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);' I5 W6 A5 i1 k/ \, I. Q7 e
return EFI_LOAD_ERROR;7 K6 U, R" a/ q* S
} 2 `% i4 X6 T9 V8 o2 I0 E. U( E
1 K5 }: ^( z i8 n/ A
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
9 g' u- Z8 G2 f if (EFI_ERROR(Status)) {
2 r* i5 J3 w8 e- K& U Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
& l5 b8 i/ j! H) l return EFI_LOAD_ERROR;& ^. G1 b% H6 s# i5 x
}
' n* \9 l' `' x& p3 j2 t ; S5 P( ~& L. ^8 U! S
Status = FileHandle05->SetPosition(FileHandle05, 0); 1 B0 P" R+ E* [% j7 u" n
if (EFI_ERROR(Status)) {5 ~/ ^/ F1 r7 t8 e4 J
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 R- o$ q! V& H7 N9 _2 ^1 Q return EFI_SUCCESS;# [2 ]; H9 \& X) H' p
}
" }4 Q7 o$ k, f* g+ [ # i5 @# d( x$ Y
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
$ Z8 N3 Y4 R* O" a) b( j/ J5 H Print (L"File Buffersize is %x\n\n", BufferSize);5 ^" n8 q( x: ^) N B. ]: h. c
if (EFI_ERROR(Status)) {" F) s2 [( a- ^
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
1 z" N; O- Q5 I v: d return EFI_SUCCESS;
9 N4 `7 o3 e) F/ U, @2 r: x }
1 u/ D; l! f* C5 l Q$ V/ E/ N
. d; I$ E! b z4 F( ^% b Status = FileHandle05->SetPosition(FileHandle05, 1); $ q x' a& I+ {3 Z% _2 z
if (EFI_ERROR(Status)) {
- e4 A. F0 X6 P- g9 m( U+ _ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);1 K# X/ G8 w/ a/ K" Y
return EFI_SUCCESS;
0 \, t) j* w! E+ W4 ^ }
9 o3 x$ C0 z% Y8 o: C b1 o
% g8 `9 X8 ^0 Z Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
$ I+ x" }$ S: \! [9 p( f Print (L"File Buffersize is %x\n\n", BufferSize);$ n7 P' @/ Y0 H4 s
if (EFI_ERROR(Status)) {
7 Y9 B3 p ]9 x Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
5 X- B _ g! j* o2 O% E0 J return EFI_SUCCESS;
% z5 w( G$ X" s# u0 v5 Q9 ]) d3 m6 m4 L }
* o& z8 X- V. m* x * B1 D& f& T1 R
Status = FileHandle05->SetPosition(FileHandle05, 2);
& L+ n/ `7 L6 b7 F8 O if (EFI_ERROR(Status)) {
5 p" I- i) T' H2 L6 a/ ? Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);. H/ _: a+ S, @% e
return EFI_SUCCESS;2 c/ \. I" L; X
}
I2 g+ ]8 S$ h 1 x c/ S3 r& m, x# A- \
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
3 c0 j+ o4 n/ \ Print (L"File Buffersize is %x\n\n", BufferSize);
6 N4 ^1 r/ f' P q if (EFI_ERROR(Status)) {% k" S( T N. s: [7 r; s
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);+ y) J, h |) W2 p, D. o
return EFI_SUCCESS;
, U. _" W5 e4 D9 ?# e; ` } " m4 H" G0 M. o8 {
1 O. Z( B5 q4 f: x/ u7 {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>> y8 O- P. i0 G* C7 d1 T* I
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
& X# p, y j: E; ` j5 G Status = FileHandle05->SetPosition(FileHandle05, 2);
. t; \. g* x* _8 p if (EFI_ERROR(Status)) {
( B; @ s! S6 t Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);4 o5 v1 z3 g: ~- g$ L* L# |
return EFI_SUCCESS;9 x; b* Z$ Y7 o+ z( f
}/ b5 q0 t0 W5 E t+ s6 Y
" m7 I; y0 `0 o& b
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);) X, h U' I+ G" ]3 X& a
if (EFI_ERROR(Status)) {3 I8 H. P3 a8 p1 L
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);: I" T0 {$ c! S5 L
return EFI_SUCCESS;" {9 ]5 i, x# l5 ~6 M p" E
}
: e* y( R+ h& _: k5 } Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
% Z/ X8 q* ^: W {. n" e//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
$ `6 g! u. {; u
8 R) n+ l" y4 Z" ]+ n6 ?2 g2 `+ W Status = FileHandle05->SetPosition(FileHandle05, 10); 2 Y( Y7 g3 Y- l% v* u) Q2 X8 q* X+ s
if (EFI_ERROR(Status)) {# ^, p7 F% K) h4 y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
1 r/ O( ]* b: E! Z+ w/ F return EFI_SUCCESS;
) a6 a/ _4 ~1 |3 f6 `: f# F }# ?4 ?$ {" s+ [3 J: C" e/ B
$ N# [5 U/ ^" z7 Y2 @( E Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
& _5 K$ R" l9 L, K if (EFI_ERROR(Status)) {
( [7 u5 {8 i+ J6 q- s Print (L"Could not write file, FileHandle05 err code is %x\n",Status);# K* L H9 }0 `9 a }% W+ G
return EFI_SUCCESS;9 j8 d. {# X9 V+ |& C9 a
}4 }! C9 U/ n3 E8 V
1 y: Z3 w. H# C3 J# w+ l Status = FileHandle05->SetPosition(FileHandle05, 12);
" e* V T7 B6 H5 f: l if (EFI_ERROR(Status)) {
+ t' u" C) p+ @2 `+ ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 q3 ~) J9 s3 N) z1 R
return EFI_SUCCESS;
9 [3 v$ ?& v7 \4 A# L }
5 I. r. H$ \& T. c$ M6 i' } 7 s2 Z% U) A# v* v6 a4 M
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
, h* z9 X, w2 m* J5 ^ if (EFI_ERROR(Status)) {4 b; o7 f6 K( |! d/ P
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
$ _# X* K) U$ }" p. K, M+ ` return EFI_SUCCESS;. Y7 ~; h: Z" P( v# P
}
' X' z7 J. h$ f/ v$ i5 V G+ p7 N% l, X% Q3 k( X5 K7 N
Status = FileHandle05->SetPosition(FileHandle05, 14); 5 E3 l- T- ?9 X; ^3 z5 _0 |
if (EFI_ERROR(Status)) {5 G0 \$ r+ l( l0 X% U+ m
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);" s% O- D* C. z9 c5 N8 _0 _
return EFI_SUCCESS;. ?! R8 o/ G7 N, y
}
, F5 P$ Y! c1 Y
) j- Y9 s0 A+ Y* J/ y8 L0 t Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);! C! B; M# O4 e( Q9 U
if (EFI_ERROR(Status)) {" L) x* J0 F `. L7 {
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);, T, }, k. h& Q3 B
return EFI_SUCCESS;
5 y# A% G- T" g* Y9 U" [ }$ ]) l a( H: T: T
: s- L+ ^4 }: R R/ C6 d
Status = FileHandle05->SetPosition(FileHandle05, 16);
2 X4 S. L/ l a3 Q) n if (EFI_ERROR(Status)) {
' n/ E, |' D6 }, G" |: R' ^ Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
* o: |1 f+ |' F return EFI_SUCCESS;0 K- ?0 U' Y& G" }
}
: F& O9 r) J2 ` ! e! \! R4 l$ `5 j# J
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
- C) T# [3 O6 ?$ K/ Q3 {/ R& f if (EFI_ERROR(Status)) {( V: P! i. `2 @
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
1 Z1 c6 s1 s1 R. `# d/ k! G) {" @. e return EFI_SUCCESS;
1 I6 U m4 y( Y$ j% U+ c }
2 n# c* A; n/ {& Z8 w# l5 b/ F q+ a, z; K" ` Z- j
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
# r0 R0 q1 ^5 {4 B
$ @8 x- [: B, R$ o1 V2 v Status = FileHandle05->SetPosition(FileHandle05, 18); ' ^6 z# @9 {1 r( u- V. K6 d
if (EFI_ERROR(Status)) {
. |/ g& U( o# |# n9 V1 l, V Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
/ y( K( z- b [: q9 B return EFI_SUCCESS;
5 d% ~' Q( `5 K4 Q; a& w' l }
8 D' ?' |" v8 ^
5 _, M" }2 \- K Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);! @ e$ ]6 `# t! F X) U
if (EFI_ERROR(Status)) {. R/ x0 @/ @3 l
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
, p4 N3 h, V8 k- {, a* o! e return EFI_SUCCESS;
" t. e5 l# T0 `. e$ s }
0 h$ p. t. @! k+ m5 u- G0 s
$ |" \( X3 y8 y5 E6 T0 c FreePool(DestAddr);
& @; O8 u3 w- {, S
& l9 T7 \# X s0 C0 o% u2 m4 @ Status = FileHandle05->Close(FileHandle05);4 z8 h8 D( o- @4 Y% v
if (EFI_ERROR(Status)) {) @: P5 b4 a8 p6 ?7 S' Q3 G9 U
Print (L"Could not close file, FileHandle05 err code is %x\n",Status);/ l+ F* D" v) s- a. b; }
return EFI_SUCCESS;
0 @6 s9 u! w, P% R } H# a9 R: C* T9 M* H
7 I& ]6 y8 G6 L, r8 |( b: H
Print(L"File Name = %s has already been created.\n", FileName);
; G* |; Y: E, B; Q3 b" t2 v) D# k( v7 q2 j+ |, @
Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
& l" [5 t0 Q4 A WaitForKeyOrReset();
5 r" R/ W `; n' w. a ) o0 u/ E! f7 O
return Status;" {7 d1 f" R% D
} |
|