3 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
14 @param Vol - File System Volume
15 @param FileName - The file to be read.
16 @param BufferSize - The file buffer size
17 @param Buffer - The file buffer
19 @retval EFI_SUCCESS - read file successfully
20 @retval EFI_NOT_FOUND - file not found
26 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
,
28 OUT UINTN
*BufferSize
,
33 EFI_FILE_HANDLE RootDir
;
34 EFI_FILE_HANDLE Handle
;
36 EFI_FILE_INFO
*FileInfo
;
41 // Open the root directory
43 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
44 if (EFI_ERROR (Status
)) {
51 Status
= RootDir
->Open (
58 if (EFI_ERROR (Status
)) {
59 RootDir
->Close (RootDir
);
63 RootDir
->Close (RootDir
);
66 // Get the file information
68 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
70 FileInfo
= AllocateZeroPool (FileInfoSize
);
71 if (FileInfo
== NULL
) {
72 Handle
->Close (Handle
);
76 Status
= Handle
->GetInfo (
82 if (EFI_ERROR (Status
)) {
83 Handle
->Close (Handle
);
84 gBS
->FreePool (FileInfo
);
89 // Allocate buffer for the file data. The last CHAR16 is for L'\0'
91 TempBufferSize
= (UINTN
) FileInfo
->FileSize
+ sizeof(CHAR16
);
92 TempBuffer
= AllocateZeroPool (TempBufferSize
);
93 if (TempBuffer
== NULL
) {
94 Handle
->Close (Handle
);
95 gBS
->FreePool (FileInfo
);
99 gBS
->FreePool (FileInfo
);
102 // Read the file data to the buffer
104 Status
= Handle
->Read (
109 if (EFI_ERROR (Status
)) {
110 Handle
->Close (Handle
);
111 gBS
->FreePool (TempBuffer
);
115 Handle
->Close (Handle
);
117 *BufferSize
= TempBufferSize
;
118 *Buffer
= TempBuffer
;
125 If ScanFs is FLASE, it will use DebuggerPrivate->Vol as default Fs.
126 If ScanFs is TRUE, it will scan all FS and check the file.
127 If there is only one file match the name, it will be read.
128 If there is more than one file match the name, it will return Error.
130 @param DebuggerPrivate - EBC Debugger private data structure
131 @param FileName - The file to be read.
132 @param BufferSize - The file buffer size
133 @param Buffer - The file buffer
134 @param ScanFs - Need Scan all FS
136 @retval EFI_SUCCESS - read file successfully
137 @retval EFI_NOT_FOUND - file not found
138 @retval EFI_NO_MAPPING - there is duplicated files found
144 IN EFI_DEBUGGER_PRIVATE_DATA
*DebuggerPrivate
,
146 OUT UINTN
*BufferSize
,
152 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
153 UINTN TempBufferSize
;
156 EFI_HANDLE
*HandleBuffer
;
162 if ((FileName
== NULL
) || (Buffer
== NULL
)) {
163 return EFI_INVALID_PARAMETER
;
170 if (DebuggerPrivate
->Vol
== NULL
) {
171 return EFI_INVALID_PARAMETER
;
174 // Read file directly from Vol
176 return ReadFileFromVol (DebuggerPrivate
->Vol
, FileName
, BufferSize
, Buffer
);
184 // Get all Vol handle
186 Status
= gBS
->LocateHandleBuffer (
188 &gEfiSimpleFileSystemProtocolGuid
,
193 if (EFI_ERROR (Status
) && (NoHandles
== 0)) {
194 return EFI_NOT_FOUND
;
198 // Walk through each Vol
200 DebuggerPrivate
->Vol
= NULL
;
203 for (Index
= 0; Index
< NoHandles
; Index
++) {
204 Status
= gBS
->HandleProtocol (
206 &gEfiSimpleFileSystemProtocolGuid
,
209 if (EFI_ERROR(Status
)) {
213 Status
= ReadFileFromVol (Vol
, FileName
, &TempBufferSize
, &TempBuffer
);
214 if (!EFI_ERROR (Status
)) {
216 // Read file OK, check duplication
218 if (DebuggerPrivate
->Vol
!= NULL
) {
220 // Find the duplicated file
222 gBS
->FreePool (TempBuffer
);
223 gBS
->FreePool (*Buffer
);
224 EDBPrint (L
"Duplicated FileName found!\n");
225 return EFI_NO_MAPPING
;
230 DebuggerPrivate
->Vol
= Vol
;
231 *BufferSize
= TempBufferSize
;
232 *Buffer
= TempBuffer
;
240 if (DebuggerPrivate
->Vol
== NULL
) {
241 return EFI_NOT_FOUND
;
252 Get file name under this dir with index
254 @param DebuggerPrivate - EBC Debugger private data structure
255 @param DirName - The dir to be read.
256 @param FileName - The file name pattern under this dir
257 @param Index - The file index under this dir
259 @return File Name which match the pattern and index.
264 GetFileNameUnderDir (
265 IN EFI_DEBUGGER_PRIVATE_DATA
*DebuggerPrivate
,
272 EFI_FILE_HANDLE RootDir
;
273 EFI_FILE_HANDLE Handle
;
275 EFI_FILE_INFO
*FileInfo
;
276 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
280 if (DebuggerPrivate
->Vol
== NULL
) {
281 Status
= gBS
->LocateProtocol (
282 &gEfiSimpleFileSystemProtocolGuid
,
284 (VOID
**) &DebuggerPrivate
->Vol
286 if (EFI_ERROR(Status
)) {
290 Vol
= DebuggerPrivate
->Vol
;
293 // Open the root directory
295 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
296 if (EFI_ERROR (Status
)) {
303 Status
= RootDir
->Open (
310 if (EFI_ERROR (Status
)) {
311 RootDir
->Close (RootDir
);
314 RootDir
->Close (RootDir
);
319 Status
= Handle
->SetPosition (Handle
, 0);
320 if (EFI_ERROR (Status
)) {
321 Handle
->Close (Handle
);
326 // Get the file information
328 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
330 FileInfo
= AllocateZeroPool (FileInfoSize
);
331 if (FileInfo
== NULL
) {
332 Handle
->Close (Handle
);
337 // Walk through each file in the directory
345 FileInfoSize
= sizeof(EFI_FILE_INFO
) + 1024;
347 Status
= Handle
->Read (
352 if (EFI_ERROR (Status
) || (FileInfoSize
== 0)) {
356 if ((FileInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
362 // Only deal with the EFI key file
364 if (!StrEndWith (FileInfo
->FileName
, FileName
)) {
368 if (FileIndex
== *Index
) {
369 TempName
= StrDuplicate (FileInfo
->FileName
);
380 gBS
->FreePool (FileInfo
);
381 Handle
->Close (Handle
);