3 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 @param Vol - File System Volume
21 @param FileName - The file to be read.
22 @param BufferSize - The file buffer size
23 @param Buffer - The file buffer
25 @retval EFI_SUCCESS - read file successfully
26 @retval EFI_NOT_FOUND - file not found
32 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
,
34 OUT UINTN
*BufferSize
,
39 EFI_FILE_HANDLE RootDir
;
40 EFI_FILE_HANDLE Handle
;
42 EFI_FILE_INFO
*FileInfo
;
47 // Open the root directory
49 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
50 if (EFI_ERROR (Status
)) {
57 Status
= RootDir
->Open (
64 if (EFI_ERROR (Status
)) {
65 RootDir
->Close (RootDir
);
69 RootDir
->Close (RootDir
);
72 // Get the file information
74 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
76 FileInfo
= AllocateZeroPool (FileInfoSize
);
77 if (FileInfo
== NULL
) {
78 Handle
->Close (Handle
);
82 Status
= Handle
->GetInfo (
88 if (EFI_ERROR (Status
)) {
89 Handle
->Close (Handle
);
90 gBS
->FreePool (FileInfo
);
95 // Allocate buffer for the file data. The last CHAR16 is for L'\0'
97 TempBufferSize
= (UINTN
) FileInfo
->FileSize
+ sizeof(CHAR16
);
98 TempBuffer
= AllocateZeroPool (TempBufferSize
);
99 if (TempBuffer
== NULL
) {
100 Handle
->Close (Handle
);
101 gBS
->FreePool (FileInfo
);
105 gBS
->FreePool (FileInfo
);
108 // Read the file data to the buffer
110 Status
= Handle
->Read (
115 if (EFI_ERROR (Status
)) {
116 Handle
->Close (Handle
);
117 gBS
->FreePool (TempBuffer
);
121 Handle
->Close (Handle
);
123 *BufferSize
= TempBufferSize
;
124 *Buffer
= TempBuffer
;
131 If ScanFs is FLASE, it will use DebuggerPrivate->Vol as default Fs.
132 If ScanFs is TRUE, it will scan all FS and check the file.
133 If there is only one file match the name, it will be read.
134 If there is more than one file match the name, it will return Error.
136 @param DebuggerPrivate - EBC Debugger private data structure
137 @param FileName - The file to be read.
138 @param BufferSize - The file buffer size
139 @param Buffer - The file buffer
140 @param ScanFs - Need Scan all FS
142 @retval EFI_SUCCESS - read file successfully
143 @retval EFI_NOT_FOUND - file not found
144 @retval EFI_NO_MAPPING - there is duplicated files found
150 IN EFI_DEBUGGER_PRIVATE_DATA
*DebuggerPrivate
,
152 OUT UINTN
*BufferSize
,
158 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
159 UINTN TempBufferSize
;
162 EFI_HANDLE
*HandleBuffer
;
168 if ((FileName
== NULL
) || (Buffer
== NULL
)) {
169 return EFI_INVALID_PARAMETER
;
176 if (DebuggerPrivate
->Vol
== NULL
) {
177 return EFI_INVALID_PARAMETER
;
180 // Read file directly from Vol
182 return ReadFileFromVol (DebuggerPrivate
->Vol
, FileName
, BufferSize
, Buffer
);
190 // Get all Vol handle
192 Status
= gBS
->LocateHandleBuffer (
194 &gEfiSimpleFileSystemProtocolGuid
,
199 if (EFI_ERROR (Status
) && (NoHandles
== 0)) {
200 return EFI_NOT_FOUND
;
204 // Walk through each Vol
206 DebuggerPrivate
->Vol
= NULL
;
209 for (Index
= 0; Index
< NoHandles
; Index
++) {
210 Status
= gBS
->HandleProtocol (
212 &gEfiSimpleFileSystemProtocolGuid
,
215 if (EFI_ERROR(Status
)) {
219 Status
= ReadFileFromVol (Vol
, FileName
, &TempBufferSize
, &TempBuffer
);
220 if (!EFI_ERROR (Status
)) {
222 // Read file OK, check duplication
224 if (DebuggerPrivate
->Vol
!= NULL
) {
226 // Find the duplicated file
228 gBS
->FreePool (TempBuffer
);
229 gBS
->FreePool (*Buffer
);
230 EDBPrint (L
"Duplicated FileName found!\n");
231 return EFI_NO_MAPPING
;
236 DebuggerPrivate
->Vol
= Vol
;
237 *BufferSize
= TempBufferSize
;
238 *Buffer
= TempBuffer
;
246 if (DebuggerPrivate
->Vol
== NULL
) {
247 return EFI_NOT_FOUND
;
258 Get file name under this dir with index
260 @param DebuggerPrivate - EBC Debugger private data structure
261 @param DirName - The dir to be read.
262 @param FileName - The file name pattern under this dir
263 @param Index - The file index under this dir
265 @return File Name which match the pattern and index.
270 GetFileNameUnderDir (
271 IN EFI_DEBUGGER_PRIVATE_DATA
*DebuggerPrivate
,
278 EFI_FILE_HANDLE RootDir
;
279 EFI_FILE_HANDLE Handle
;
281 EFI_FILE_INFO
*FileInfo
;
282 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
286 if (DebuggerPrivate
->Vol
== NULL
) {
287 Status
= gBS
->LocateProtocol (
288 &gEfiSimpleFileSystemProtocolGuid
,
290 (VOID
**) &DebuggerPrivate
->Vol
292 if (EFI_ERROR(Status
)) {
296 Vol
= DebuggerPrivate
->Vol
;
299 // Open the root directory
301 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
302 if (EFI_ERROR (Status
)) {
309 Status
= RootDir
->Open (
316 if (EFI_ERROR (Status
)) {
317 RootDir
->Close (RootDir
);
320 RootDir
->Close (RootDir
);
325 Status
= Handle
->SetPosition (Handle
, 0);
326 if (EFI_ERROR (Status
)) {
327 Handle
->Close (Handle
);
332 // Get the file information
334 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
336 FileInfo
= AllocateZeroPool (FileInfoSize
);
337 if (FileInfo
== NULL
) {
338 Handle
->Close (Handle
);
343 // Walk through each file in the directory
351 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
353 Status
= Handle
->Read (
358 if (EFI_ERROR (Status
) || (FileInfoSize
== 0)) {
362 if ((FileInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
368 // Only deal with the EFI key file
370 if (!StrEndWith (FileInfo
->FileName
, FileName
)) {
374 if (FileIndex
== *Index
) {
375 TempName
= StrDuplicate (FileInfo
->FileName
);
386 gBS
->FreePool (FileInfo
);
387 Handle
->Close (Handle
);