|
发表于 2009-11-11 16:21:18
|
显示全部楼层
我寫了一個 File io 的 EFI shell app
/*
2 `, I0 X1 u# B1 O \1 X * myfileio.c
: x1 d D+ ?7 \5 r6 D6 A * Apps
; ~$ E4 h5 f6 ^& {* l, } */1 ?7 l+ a( Y) P0 t. U
1 r* R, w+ l W#include "efi.h"$ b$ Q$ z0 K4 ?. D; s, ]
#include "efilib.h": ?+ ~. S; m3 E0 z% L. @: f1 r2 K
+ {$ p/ U7 S2 A/ I1 E#define FILE_ATTRIB_CREATENEW EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE
+ p0 n9 ?# K. L; y
+ {- J) o3 c( M2 C! [static EFI_STATUS WaitForKeyOrReset(VOID)
; w' ~( i* D% C{
2 ^% O* e/ y; N3 \. [ EFI_STATUS Status;# H l- k6 b# l
EFI_INPUT_KEY key;
5 o- \! f; W! G' m UINTN index;
, f" @% |4 r5 ^0 a # e+ D; W& Y' H: q K1 y: W& ^& {
for(;;) {* j) [, i6 B2 G+ b/ U
Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);9 n% ]1 l- ]. i2 W3 I t$ b$ Y) n
if (Status == EFI_NOT_READY); r: z6 Q+ H: S
BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);3 \) z# b( u5 [8 e% \
else
# l* P" I" k$ f! K break;8 l1 |1 x: @4 w1 ]2 u
}+ W' U j6 o6 x0 _; g# p% v
if (!EFI_ERROR(Status)) {# h7 }' _4 W7 H. e1 ~6 G; D
if (key.ScanCode == SCAN_ESC)4 a5 q8 C0 l( ^- |2 Q
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);" K) T% s7 D' K0 c( R
}2 w2 r% ^, ~" Q& S5 |8 c
* N& m# M: _8 `$ x: y r return Status;; m( `9 ]) r) v% b0 b
}8 `% i* x$ K0 e
( N1 n( F, ]- s7 AEFI_STATUS! _9 P8 \" v* I j0 j$ V, Z8 ~
EFIAPI* ]% i0 a: c7 O) r' X
MyfileioMain (IN EFI_HANDLE ImageHandle,, s$ Q( F! e( h0 o, m+ o5 d
IN EFI_SYSTEM_TABLE *SystemTable)
, i- L$ K3 s2 z- r; O{* x$ w0 W$ y/ ~" \& W
EFI_STATUS Status;
" S7 W# O6 y; l6 Q EFI_HANDLE *DestAddr; 0 y3 l- J$ p. o( k( d4 |5 d' z
EFI_LOADED_IMAGE *FileHandle01;+ S( n9 w! ?+ v
EFI_DEVICE_PATH *FileHandle02;
7 E0 `7 k, d' i8 |: E1 {: p( Z EFI_FILE_IO_INTERFACE *FileHandle03;0 i3 A5 n4 {# Q" S+ l8 d
EFI_FILE *FileHandle04;. m/ ]9 P4 H" P
EFI_FILE *FileHandle05;
* U, d6 Z$ m/ w, u0 W3 } CHAR16 *FileName;
0 z1 k: ^, F; P2 c8 s4 b) f$ F( c! @0 f CHAR16 *BufferA, *BufferB, *BufferC, *Space_Key;
* n& _0 F R# {/ l UINTN BufferSize = 8, BufferSizeKeySpace = 2;
3 H0 \3 |/ ^; O' V7 x* J& e int Bit0, Bit1, Key_Space; 3 _2 o3 E- x- l* N; J
% U0 C6 k% G$ E# z FileName = L"NewFile.txt";' V# a" V9 v* b4 h
Space_Key = L" ";7 n" H9 F2 e* O3 {. U m
$ _" n' @" Q4 L: G e1 ?/ j
BufferA = L"ABCD";7 x2 i p, C j" _/ z3 B2 Z; G
BufferB = L"EFGH";8 x1 f# h Q g1 D$ a9 U v
BufferC = L"IJKL";
0 l7 S; S0 E, E) Y* c5 d7 }0 l0 W" @3 y3 f! U; x3 ]3 x# t) S" v
Bit0 = 0xff;& q, T& v, |4 h( ]- p, i: H
Bit1 = 0xfe;
2 h" l3 r" A6 U
- `) M. m9 O0 a: a: B5 b Key_Space = 0x0020;
c9 n0 ]) u$ Z8 E! N) d n. ~ ; b7 |2 y. j6 |
InitializeLib (ImageHandle, SystemTable); & u5 L5 a' S% p
; G4 O" c7 ^0 ^9 O, A7 v1 J- ~) b; \ DestAddr = AllocatePool (BufferSize);
" V+ \) @( g# R4 w
0 f1 A9 g" U& V6 t" a+ ~! l Print (L"Value of Bit0 is %x\n", Bit0);
, u4 J/ e3 @; m( |5 B* Y# { Print (L"Value of Bit1 is %x\n", Bit1); % S' }1 H; v5 |9 y. W* B. W
# p9 d1 }: b) W9 J8 ^/ L
8 @& X* c! e1 r( s3 ?. S$ Q Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
: P; w& J$ I4 B! s' l if (EFI_ERROR(Status)) {
* a' D9 E, o4 z4 r+ `; x2 ^! r Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
9 `2 l4 u; a" E- W" b) { return EFI_LOAD_ERROR;. q( C! W6 j7 {4 L" m' e7 x+ D- C
}1 g. |2 ?% [0 p) v# G
! g$ z: ~/ T. v' n: |- ?6 c0 u Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
+ p( h8 ]: Q0 Z) \4 S+ Y9 B, k if (EFI_ERROR(Status)) {0 `% q; G& W2 J! Z$ h
Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
1 G X( `: ~3 A& C return EFI_LOAD_ERROR;
* x+ b7 Q' p6 Y* U }
' r# ~4 d; c6 d/ E, K , @4 U. {) ~, h8 p* C" g
Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);& Y2 x1 Q1 E% B0 B; S: p
if (EFI_ERROR(Status)) {
+ Q& M" Y2 Z! H9 B; v6 L; \. h9 P6 r Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);. X" v6 C6 [; \3 E) v1 Q
return EFI_LOAD_ERROR;
. A9 ?4 o+ d7 H( |" x }9 @, R% b# m y# j8 w, D
' d6 U0 j5 |) M- k1 @7 @ Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);1 K5 P$ E8 p! g, X6 d U' h+ |3 }! w
if (EFI_ERROR(Status)) {
: q4 D Y3 u$ Z6 Q1 H Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);: }" s8 K. L4 {5 N: S
return EFI_LOAD_ERROR;
4 E( {; G# q! o+ C! @, A1 \- d }
]* `0 z9 V) q0 h) c' l # i2 n" W; l6 ]$ f
Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);" b: ^( M+ I1 m3 j: |. t+ B
if (EFI_ERROR(Status)) {
# L1 ]& w% B* E& Q& p! i Print (L"Could not open file, FileHandle05 err code is %x\n",Status); B3 h$ S9 m- Q; d5 `
return EFI_LOAD_ERROR;
U& W/ u! v. d% l$ j% [ }; R* {, [! V, Z2 y' g! r
3 {- O V& T: l% t3 M o Status = FileHandle05->SetPosition(FileHandle05, 0);
; h/ Z- `& j' Q if (EFI_ERROR(Status)) {8 S- D$ x ^( B/ W
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);7 v4 a& h4 Z7 \6 S1 D
return EFI_SUCCESS;
4 w, q5 V8 E- q8 ^& z }& X9 W3 j2 j _* ? V% l
! K9 J" M+ m( l% M- P
Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
5 K# ^' h' r& g4 h Print (L"File Buffersize is %x\n\n", BufferSize);
6 ?+ O$ S- m: D- a' n if (EFI_ERROR(Status)) {
* Q/ d+ T) W+ X6 D Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);8 Y. i2 Y& H$ W V& E8 q! u
return EFI_SUCCESS;
6 s# m) f! N3 I9 _+ h+ O } C; W! i6 }8 S% t* ?/ l; N. H
$ M2 u3 M! F/ ~* x* F Status = FileHandle05->SetPosition(FileHandle05, 1); 6 x; _4 D" E2 J. ?$ f* h
if (EFI_ERROR(Status)) {7 u/ {: {. e# i% [/ z, T
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
' i, w/ @0 K2 Y return EFI_SUCCESS;! Y- A& u0 i: {) A
}# I% V: D7 G* d; u0 k
+ J: }+ X" y/ V7 K& { Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
" b5 Y9 t" j* Z Print (L"File Buffersize is %x\n\n", BufferSize);
8 K- g" w4 T! R6 I0 @ if (EFI_ERROR(Status)) {
" b& j* H. v2 f4 M o Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
% \5 s$ n" G! N( [ E% ] return EFI_SUCCESS;
/ |) k& N w. F3 l" B6 E } + n& r& _8 b1 L
8 \4 h4 l* `, ^6 g4 n- c% r
Status = FileHandle05->SetPosition(FileHandle05, 2); ) L. Z+ m; m$ x% I0 ?
if (EFI_ERROR(Status)) {
; j. |6 J* i2 f# o Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);5 n& H5 Z) M2 k1 R
return EFI_SUCCESS;& w W% \6 L; ~
}2 A3 \ e$ q+ Q- `/ k' [; K
7 o. i6 B( H* a% u3 V8 s) K9 S
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);+ t; w8 [5 O3 _3 z; P# n m
Print (L"File Buffersize is %x\n\n", BufferSize);' C' p9 Q# t* M+ h
if (EFI_ERROR(Status)) {
. E9 m1 r0 ?5 F7 [ Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);- ~3 q& w" e d& W7 T
return EFI_SUCCESS;
, @) s- z) L) A; }/ F% ^ }
+ Y" O Z+ L$ Y3 F1 y2 \7 c8 ~
4 P( [1 A7 ~& D+ r& F1 Y- t//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>( F; K* A! }# w
Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);$ A @$ H4 L6 u. A
Status = FileHandle05->SetPosition(FileHandle05, 2);
9 u; `1 g/ W8 u3 ?* T) @( I" m if (EFI_ERROR(Status)) {
# X! R5 z# P8 f6 D( F Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);' v+ u' a5 S2 _* Y% b0 j' H
return EFI_SUCCESS;9 V" S C" \( Z0 g7 w) L1 z
}/ a' b6 F5 N* j
+ ^ \+ c3 c! A- z/ k7 {* U( V
Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
( Y. ^. C) o! x% K/ {$ N if (EFI_ERROR(Status)) {
- B2 P* r- ]) F. F& A6 K/ a' P; ? Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
1 w- H/ P- s3 d return EFI_SUCCESS;! q$ }5 B8 j5 D* n: |0 u- x1 {
}
* o) a) ^) r4 H/ u$ T* D6 j Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);- k. a4 @% { b5 V& \+ Y
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<
7 V% X1 k; W6 p; J; t# q; |0 ^ G! { q3 q) g7 p) P
Status = FileHandle05->SetPosition(FileHandle05, 10); # b6 {& N- H8 H' X& N& W) G! d' C
if (EFI_ERROR(Status)) {
2 v7 o/ _# P: v+ B. i" t6 P Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);- @2 t6 \ a* A/ E3 n, @ m
return EFI_SUCCESS;5 u# y) Z0 C6 S- r
}
3 Y7 ~4 _' o* r& ]6 h- h3 x
; a; k* ^0 T8 {! ~0 x: H Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);" @3 \, f/ ]! X
if (EFI_ERROR(Status)) {
& I4 F w6 y9 `* d+ H( L Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
2 {$ C9 Z' w5 \ return EFI_SUCCESS;( x- _& V/ M9 z) d* s1 g+ \! N( T9 k
}2 N% K# T4 c! _- L" s
0 k6 Q. t! G1 n
Status = FileHandle05->SetPosition(FileHandle05, 12); $ n3 k: O* {; j( v! a$ I( V% U
if (EFI_ERROR(Status)) { I' I; s' K) ], H3 {
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);9 q) N" ]8 _* Q; r8 J
return EFI_SUCCESS;4 Q9 N* q3 J) T0 S H( e( |
}' \6 p0 ^# N% {
, b/ U5 N8 m4 G$ w! [$ `
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
5 C2 | a4 L) {7 K. R/ f if (EFI_ERROR(Status)) {# N- M* T$ g' ^# M
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
! Z$ w9 \# V, @1 x- v& L return EFI_SUCCESS;1 j; N+ g4 X$ U; ^8 v, R
}
5 N1 j( K% B D" h% l
4 Z) ^: h! w/ {2 ` Status = FileHandle05->SetPosition(FileHandle05, 14);
8 V* A: O7 E8 r$ ]# x* D9 C, f if (EFI_ERROR(Status)) {" L2 N! l$ p" ]5 H
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
0 e6 m! F( R, T* L. o _* R4 G return EFI_SUCCESS;7 G7 j$ i0 G( J( D- O
}! Y. F$ Z. W) U1 Q7 r1 ~" \9 q
1 |5 n4 `4 \" e6 N Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);, G! K, H l6 \2 }8 h
if (EFI_ERROR(Status)) {
! c2 j m( z# Q! v) e' l. L Print (L"Could not write file, FileHandle05 err code is %x\n",Status);( c4 l! b4 r4 q9 v) s
return EFI_SUCCESS;2 K0 O* U# a; T% \5 F0 E
}/ S2 f: M; k/ N, n* Q: ^5 g
/ _9 M/ Q, ^2 G+ I
Status = FileHandle05->SetPosition(FileHandle05, 16); 4 ~; @! Z! M0 a( G- S
if (EFI_ERROR(Status)) {. v9 p5 i3 g8 D8 w, `9 l e
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);0 u! Z }/ N8 i& X0 k
return EFI_SUCCESS;+ X2 {5 z& p3 n: u; i/ D) s# G
}0 D6 z' b" ?8 r3 P
. A; B; d- t9 k2 G7 o0 d
Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);% V, U$ p X5 \, U* G( I
if (EFI_ERROR(Status)) {
1 {" X) F! R, a% f% `) { Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 L7 Q4 B! S6 C. k4 b& ` return EFI_SUCCESS;) ~+ d) h5 a/ O8 g3 V5 r
}
* i$ [6 {* K; z5 Z2 ]; _/ N; \" O! q7 x/ B, t' T; r
//---------------------------------------------------------------------------------------------------------------------------------------------------------------+ ]+ l+ i) `6 L: x1 Y) B" Q$ a
+ y( ^- p# D; l* ^
Status = FileHandle05->SetPosition(FileHandle05, 18); " |2 C% [' q3 {
if (EFI_ERROR(Status)) {5 t& f: A. p$ y( b) o" u
Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);# w2 }# U0 [0 Z" e: U" s
return EFI_SUCCESS;& }0 a5 g2 E/ z* h
}$ N2 b6 i: Q/ C! n: y! \
; L! `, M, @; w7 e3 `* ]
Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
0 ^5 x0 j0 Y/ O3 v if (EFI_ERROR(Status)) {; F$ Q8 f3 Z8 }: r% p- p) @7 O
Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
6 V( v# v3 w8 e, t4 S return EFI_SUCCESS;
% d7 z7 Y- u! U% o% S; v c } + b. h. ?) Z: L1 }9 p" x' h0 q
! N9 j$ }0 q& ?& r. `6 Y6 O FreePool(DestAddr);
$ g; ]1 ]5 K" \$ c, h g* ` v' l- Y# j2 Q9 K! ?1 M6 p
Status = FileHandle05->Close(FileHandle05);
5 H0 N/ m# G& Z7 r" I ` if (EFI_ERROR(Status)) {
2 ?' V$ x1 Q1 u+ d( L8 I9 x3 M, b Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
7 ^2 s6 Q) s) A, V/ r) E return EFI_SUCCESS;$ ?5 e6 }, I, d1 Z; s1 E8 P
}
$ H1 o3 I" W( i4 I, E" Q ' a3 j% b$ Z( Q. @9 r
Print(L"File Name = %s has already been created.\n", FileName);
! Z3 k8 ^- \5 c2 T4 P j, e* s
+ i- ~1 d. V) d! N% I% F2 f* D Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
, V! X* W, S, k3 w ^) v WaitForKeyOrReset();9 s( u' f' h$ ]
3 e0 q9 \8 g" C0 ]0 z g! p return Status;
6 j, n# S; V1 X0 r1 h0 Q7 y} |
|