|
|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*+ z5 i% Z% A' q
* myfileio.c5 q$ N# w6 T0 |; N, a
* Apps
- R. k9 i5 Z0 L7 B4 j" Q */$ S; s H3 o: {; v. Y" a$ K: b
4 s: I" P8 M) e( l- g# ^
#include "efi.h": ?9 f0 \7 e/ F o, i" o, A/ b+ l
#include "efilib.h"
# X7 `" |$ @# j5 C0 J5 }* z4 M7 r, h/ n) X: J' _% h6 H& L
#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
( ^5 \+ Q% O: Q7 B5 ^5 K; P' h9 _2 D9 [ O
static EFI_STATUS WaitForKeyOrReset(VOID)
$ Y3 |; _0 v# m2 ~! N- T& ^{
9 w/ T2 d+ T( [- y1 i* d6 ?$ T EFI_STATUS Status;) N* K# k8 W& [6 S! }% s O0 d) g
EFI_INPUT_KEY key;$ A4 Z/ m0 ~" Y* K% A9 e
UINTN index;
: z; V9 Y, l; T7 Z3 ]/ D 5 g: ?- g4 e* y$ I8 T! ?) v
for(;;) {
9 r7 A3 C1 S# T: K Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);' j. }! z. v& i8 ^
if (Status == EFI_NOT_READY)( ~: y9 D% s/ r+ e% V A0 [- k- |
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
) b! Q+ }, k1 q6 y7 L) R1 X) r& k! g8 m else% x5 i1 r! H' F- B
break;
, ^% ?! k5 t1 R2 E: k7 V, G& u }
" b+ _# \6 V! e1 a if (!EFI_ERROR(Status)) {8 k( u$ B0 k" Z3 I& j1 s
if (key.ScanCode == SCAN_ESC)
' G5 o, C6 j" \ i( a% s/ z6 |, I. S RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
& w' P8 I/ [& r; \! u }5 \1 R& @) x3 J2 f/ W3 l
% i9 S" Z1 X. I0 D: h
return Status;0 V3 N8 A( p' a n Z. T
}& K. |# ~/ | v5 u" Z6 k1 A
7 s; T! ]& K. s. H* Q- f
EFI_STATUS- S" Q, \+ [# I6 p
EFIAPI
+ Z# q- |# p2 b2 P( hMyfileioMain (IN EFI_HANDLE ImageHandle,
3 c/ Z) n6 v" Z$ K: d! I* ` IN EFI_SYSTEM_TABLE *SystemTable)
' h" ^0 P+ P. `( o5 o! N/ s{ p5 {( ^ L8 v# {! ]# _
EFI_STATUS Status;- i1 F7 A- n" ^4 o/ K* {0 x; n
EFI_HANDLE *DestAddr; . |7 e+ V4 ` Z' S. t
EFI_LOADED_IMAGE *FileHandle01;
* m& S. T: B+ K/ {$ a. x% |' l) U1 Q EFI_DEVICE_PATH *FileHandle02;
* z/ g! e N, }/ o+ K4 k EFI_FILE_IO_INTERFACE *FileHandle03;
3 i2 \( i6 N6 M$ i EFI_FILE *FileHandle04;" m. M: L3 K' r$ f2 o
EFI_FILE *FileHandle05;: g2 {& S4 y' ?. [) W% [$ I
CHAR16 *FileName;# V) v6 u( S! r" b" Q
CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;* h6 g2 M$ D* m7 z; D& @6 C+ R6 l: v
UINTN BufferSize = 8, BufferSizeKeySpace = 2;1 R' e1 Z& p$ Z
int Bit0, Bit1, Key_Space;
. j; B! j, U2 o6 O( D' G5 C5 Z, q6 B" C3 U3 s( G
FileName = L"NewFile.txt";
9 [ o' W3 @& O/ }+ K4 ] Space_Key = L" ";) m. Q$ I- n8 i& B: p- h
! H! C+ H. @5 B1 Q6 ?- |
BufferA = L"ABCD";3 P9 P& {# z/ V: i
BufferB = L"EFGH";3 j X: H# A4 r5 ]5 v/ {
BufferC = L"IJKL";& Q3 ?. L, M4 S$ c9 }
( n. ]" Y- }+ P3 Q
Bit0 = 0xff;, C5 F* B- G/ y o- L
Bit1 = 0xfe;
' N4 C j/ [5 a( Z
* `3 f( f2 l Q9 H7 b R Key_Space = 0x0020;' v; ?( U5 |) y6 j
, p+ ^. _3 m' X8 T; w# h2 x9 |7 A& ^ InitializeLib (ImageHandle, SystemTable);
1 o8 |* w5 W2 f9 ~ a; R4 G0 @
* }9 s* K- w( g9 B) u5 q) m DestAddr = AllocatePool (BufferSize); ( n) n) k( G+ o# x' o
( p" C/ c4 E2 a$ p+ L4 x+ u Print (L"Value of Bit0 is %x\n", Bit0);
/ f& L+ N& ?; z! l3 \ Print (L"Value of Bit1 is %x\n", Bit1);
% _. A4 H# z/ Y B- ]
8 c+ v3 ]; t/ b$ m
8 p$ v" c5 r4 f! {4 ? Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
& T" l3 y6 b9 k if (EFI_ERROR(Status)) {2 h. ]1 D. _2 X/ |" D0 Y
Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);3 N& `1 Z: K. @5 b8 _5 C
return EFI_LOAD_ERROR;
. T) a* v! i2 d) P }
; G/ }' a$ M o" U ~
& P a4 i& }: V. m) o$ e o+ b Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);! j3 G) [4 ^' N
if (EFI_ERROR(Status)) {; |2 H2 c% E. I$ o: F2 K9 _
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);' O" V; Z r" r" H5 D& K$ c8 l
return EFI_LOAD_ERROR;1 g, a0 c) l# j+ @; g1 \
} , ^2 A. ~1 G3 I
0 R( H! J% r6 y, h* w
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);4 t" p! G# f+ I; y0 R
if (EFI_ERROR(Status)) {( W4 z# j- S. k8 ^4 J
Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);- w, u! F% F+ w4 `
return EFI_LOAD_ERROR;, k, y. O. @0 ~
}
2 B/ b: j; g( G! S; ]5 h2 ~1 w& H( K' D+ ?4 {% l0 F- B+ e4 w
Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
- n* |) I0 P" O) h+ k `$ q if (EFI_ERROR(Status)) {1 T4 z1 U* P: ~0 T6 T' S* i' A- a
Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
5 ?9 V( [6 |& L; _ return EFI_LOAD_ERROR;' ^+ H% b1 ?5 a- M: W" L
}
- g* e6 \( i' P+ k O7 i+ k! v' F F9 U8 D' w- V
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);) j) N- P8 g! C# t2 {0 h
if (EFI_ERROR(Status)) {$ {; z u/ J" I- A, J9 D( |
Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
- J1 d4 u* r! N/ W return EFI_LOAD_ERROR;( Q2 s# z- [5 j; P. s% V
}
V: Q- S0 \% [+ e7 R4 s# L
0 B, i7 T. q, A) g4 N& |8 i( ~ Status = FileHandle05->SetPosition(FileHandle05, 0);
- S4 n9 i& d% O0 L$ I/ ~ if (EFI_ERROR(Status)) {. R5 e9 q+ z8 r/ p) b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 ^: Q0 U2 [$ c9 v
return EFI_SUCCESS;
. E2 E5 y. _. V6 J }; B/ I) x. P) [& I/ B' {0 b
! f1 V/ B9 A" f5 j! o Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
) ]8 n$ a0 x$ M( l, N* f Print (L"File Buffersize is %x\n\n", BufferSize);
7 Y5 R* {" F/ J+ q if (EFI_ERROR(Status)) {
) {1 [- P% ]# P- o+ w1 [ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);- @% G1 v0 F. E" E$ }3 J
return EFI_SUCCESS;. \ [2 ^- B3 m; S0 Q! R
}
. T# ?$ r S1 M1 ^) P2 H
# D. Y# _3 G& g6 ]( I2 i* { Status = FileHandle05->SetPosition(FileHandle05, 1);
) P, G& q! p1 Z' D/ b+ F1 R if (EFI_ERROR(Status)) {. q1 B3 f5 d+ s& q# u! X' e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
2 b$ Y$ T" W9 a8 F return EFI_SUCCESS;
. x% P& ]4 @% [' O/ M }
- Z# D" P" F/ _2 F! Q9 O / z* S! }" l% d' Z3 l
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);& o7 b% \$ l7 ^" F. u
Print (L"File Buffersize is %x\n\n", BufferSize);: ]/ I- [- [& L: L
if (EFI_ERROR(Status)) {
) N$ A8 L9 _5 A. N# ` Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
: k+ n5 M! u) n& M6 F8 [( {, ]" n return EFI_SUCCESS;
2 {% e" C4 U+ H( ]5 d! D) s }
2 J7 K. B. V$ y0 | 5 u; o0 h# d$ v4 {
Status = FileHandle05->SetPosition(FileHandle05, 2);
5 S; B7 `8 h: S+ _, B" v Y. a if (EFI_ERROR(Status)) {8 T1 W- L, \- @( A# y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
6 ]2 N6 s+ \* z( W6 X. m: N* B return EFI_SUCCESS;
4 i+ F7 V8 `+ \9 a }
3 J- x: z4 P# m" m8 X9 g7 f, I ) w5 a5 X$ k4 [0 F3 E
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
8 Q6 j% Q% [# F Print (L"File Buffersize is %x\n\n", BufferSize);
4 m8 V# T" h6 a6 @( q4 J( q if (EFI_ERROR(Status)) { F, O0 M: e7 q2 b! f# b+ m
Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status); d6 o1 E9 C- N2 R; H B# O
return EFI_SUCCESS;! z% L! }/ f) Z) I- R
}
* {1 o! w+ T% i+ v 2 i. Q5 ?. D$ a
//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
7 G0 u" U2 D+ @ Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
- r8 U! _' T* G. B Status = FileHandle05->SetPosition(FileHandle05, 2);
7 q8 M) d7 |. ^* M( Z if (EFI_ERROR(Status)) {$ \) H# ]9 A. { u' Y
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);2 W: J' J5 l. @$ R( E
return EFI_SUCCESS;& T- ~6 d) L: [% w& K- U% W( w: s
}
+ e: {6 Z9 F- f# b2 ^* j L0 S) z) M. T
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);. c/ R/ h+ P/ _
if (EFI_ERROR(Status)) {) @0 x+ J: D3 Z$ g2 c8 J2 L9 `
Print (L"Could not read file, FileHandle05 err code is %x\n",Status);+ Y$ s7 z/ `0 B5 P% ?8 v
return EFI_SUCCESS;" ^4 t( r+ a! A# l: t1 E
} 9 k7 @4 [, @% C# S
Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
- W8 I' O) O" |' I" K//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<4 r) \, Y( s7 D% N% c
3 b. I; f" R. ^7 H0 f; A. x. i Status = FileHandle05->SetPosition(FileHandle05, 10); + j# L" W0 P# M6 w& d
if (EFI_ERROR(Status)) {+ X& G6 i+ K/ B
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);$ A* P6 ?2 ?# U
return EFI_SUCCESS;$ H# `! R2 ~( b( U1 F8 c, ?
}
* Z) n' e8 l m : Z3 H8 ?2 K8 [7 n# E
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
. Y, Y0 c3 n4 @% t; [( e u5 X if (EFI_ERROR(Status)) {# c$ o3 Q$ a& k9 D$ v. a
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
% L. y& M. @% `+ {! o8 m' H return EFI_SUCCESS;9 y- O9 q2 w- B
}8 c2 \2 w( e0 x! H: g
! P" l6 K5 i( E9 l
Status = FileHandle05->SetPosition(FileHandle05, 12); 8 `6 H1 v+ ^) C4 g I
if (EFI_ERROR(Status)) {
' d0 ?3 g6 m7 k9 T* x2 H Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
+ I2 a' a2 B9 |5 m( r return EFI_SUCCESS;8 }( j9 A6 r: m) g4 D+ _6 z
}
/ j" H% _) k; g$ q1 x! [
& b* G; X( N6 N Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);* O/ [) m1 T/ i0 f
if (EFI_ERROR(Status)) {
" T# }9 \: l; T p; k, ~ Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
7 ^7 F" C9 ?* _* h, W return EFI_SUCCESS;* l; |) i( H% k; {: C0 y2 J8 O
}
* T6 o' A. D- F1 v- O0 j5 w. Z! i8 |
Status = FileHandle05->SetPosition(FileHandle05, 14);
5 \" ~+ }+ o- g if (EFI_ERROR(Status)) {# v; |3 X" ]' R' A* c) g, m1 b
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status); F' j- J |. ]7 u* |' ~, L6 P
return EFI_SUCCESS;
9 q7 V6 Y8 J$ e. y" k }* s+ R9 Y; h9 M/ P
0 r) m6 x* z, t
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
7 }) d) N4 I! y1 ]# r if (EFI_ERROR(Status)) {9 R$ f# l9 u6 b8 R; O. z
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
" W/ B. v+ g0 j5 M% O, j- P2 U return EFI_SUCCESS;' Q/ C2 c2 W6 M2 ?9 ?. n
}1 @- X( V) S" y5 m
; t4 K1 \" y- V; Y3 o/ t2 e$ n
Status = FileHandle05->SetPosition(FileHandle05, 16); . Q3 [" t$ N, D/ ~* M
if (EFI_ERROR(Status)) {+ O9 S0 P4 P6 p
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
; k% G6 h! p; o* k! Y& r; } return EFI_SUCCESS;4 m# O* t9 C2 |, j4 i6 e. u+ L( H
}
) b, r; h+ J9 v" b5 k. i5 s3 P % L Q$ N/ m* j
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
) q) p9 l5 o( y# U, N/ G$ h* A5 q/ z3 W if (EFI_ERROR(Status)) {
0 j% I- j8 G. a7 Z Print (L"Could not write file, FileHandle05 err code is %x\n",Status);: g! N, f; i9 q# _
return EFI_SUCCESS;* l# ]9 T5 G! i
}
/ M. \' ?! a4 r7 g" ?# b9 v. @+ n$ C7 @3 \8 b
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
1 S; W) X" i, L1 B8 b
. k6 E- c6 N% U" } Status = FileHandle05->SetPosition(FileHandle05, 18); 5 Z6 x% X+ Q' m( v7 L
if (EFI_ERROR(Status)) {" c; S, i0 A! D R
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
. ^+ ?+ X+ [4 r return EFI_SUCCESS;: T2 _/ K6 b" U9 A2 ^
}
0 T d8 l3 x& f A9 {1 y) J0 k6 t
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);4 K$ m( z! s4 S6 }( U( H& ~
if (EFI_ERROR(Status)) {
8 y4 G% r$ [- l$ {0 X* u Print (L"Could not write file, FileHandle05 err code is %x\n",Status);2 `8 w. F+ ?8 K% ^. |
return EFI_SUCCESS;1 I- z8 ^* E0 Z- f8 b2 m) c f
}
9 e9 J8 V5 i8 t9 N; w
8 P. T" @! h% O5 s4 y$ }; V" S FreePool(DestAddr);
) a9 }% s$ c5 I; l: |1 d u; V& C
Status = FileHandle05->Close(FileHandle05);: m+ F1 N' x' E6 {$ v9 A' n; m
if (EFI_ERROR(Status)) {
: t# n& S# _0 f2 M | Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
2 T6 ]) W. m7 i C1 B return EFI_SUCCESS;! |8 N5 {- ^' b
}% m) x; @! Y/ T+ @5 R8 T
# ] E- z. m8 m v+ Q) z
Print(L"File Name = %s has already been created.\n", FileName);
/ e. {6 F/ x C% l& u# v0 V
3 t$ U+ N2 t$ Q6 [ Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
' m `6 M0 A7 c WaitForKeyOrReset();/ K. r5 d/ O$ o J/ x! h
. _9 ~$ e4 x P( ~3 K- y, l& T$ A; l return Status;8 m4 W& N, p( @
} |
|