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
;
175 // Read file directly from Vol
177 return ReadFileFromVol (DebuggerPrivate
->Vol
, FileName
, BufferSize
, Buffer
);
185 // Get all Vol handle
187 Status
= gBS
->LocateHandleBuffer (
189 &gEfiSimpleFileSystemProtocolGuid
,
194 if (EFI_ERROR (Status
) && (NoHandles
== 0)) {
195 return EFI_NOT_FOUND
;
199 // Walk through each Vol
201 DebuggerPrivate
->Vol
= NULL
;
204 for (Index
= 0; Index
< NoHandles
; Index
++) {
205 Status
= gBS
->HandleProtocol (
207 &gEfiSimpleFileSystemProtocolGuid
,
210 if (EFI_ERROR (Status
)) {
214 Status
= ReadFileFromVol (Vol
, FileName
, &TempBufferSize
, &TempBuffer
);
215 if (!EFI_ERROR (Status
)) {
217 // Read file OK, check duplication
219 if (DebuggerPrivate
->Vol
!= NULL
) {
221 // Find the duplicated file
223 gBS
->FreePool (TempBuffer
);
224 gBS
->FreePool (*Buffer
);
225 EDBPrint (L
"Duplicated FileName found!\n");
226 return EFI_NO_MAPPING
;
231 DebuggerPrivate
->Vol
= Vol
;
232 *BufferSize
= TempBufferSize
;
233 *Buffer
= TempBuffer
;
241 if (DebuggerPrivate
->Vol
== NULL
) {
242 return EFI_NOT_FOUND
;
253 Get file name under this dir with index
255 @param DebuggerPrivate - EBC Debugger private data structure
256 @param DirName - The dir to be read.
257 @param FileName - The file name pattern under this dir
258 @param Index - The file index under this dir
260 @return File Name which match the pattern and index.
265 GetFileNameUnderDir (
266 IN EFI_DEBUGGER_PRIVATE_DATA
*DebuggerPrivate
,
273 EFI_FILE_HANDLE RootDir
;
274 EFI_FILE_HANDLE Handle
;
276 EFI_FILE_INFO
*FileInfo
;
277 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Vol
;
281 if (DebuggerPrivate
->Vol
== NULL
) {
282 Status
= gBS
->LocateProtocol (
283 &gEfiSimpleFileSystemProtocolGuid
,
285 (VOID
**)&DebuggerPrivate
->Vol
287 if (EFI_ERROR (Status
)) {
292 Vol
= DebuggerPrivate
->Vol
;
295 // Open the root directory
297 Status
= Vol
->OpenVolume (Vol
, &RootDir
);
298 if (EFI_ERROR (Status
)) {
305 Status
= RootDir
->Open (
312 if (EFI_ERROR (Status
)) {
313 RootDir
->Close (RootDir
);
317 RootDir
->Close (RootDir
);
322 Status
= Handle
->SetPosition (Handle
, 0);
323 if (EFI_ERROR (Status
)) {
324 Handle
->Close (Handle
);
329 // Get the file information
331 FileInfoSize
= sizeof (EFI_FILE_INFO
) + 1024;
333 FileInfo
= AllocateZeroPool (FileInfoSize
);
334 if (FileInfo
== NULL
) {
335 Handle
->Close (Handle
);
340 // Walk through each file in the directory
348 FileInfoSize
= sizeof (EFI_FILE_INFO
) + 1024;
350 Status
= Handle
->Read (
355 if (EFI_ERROR (Status
) || (FileInfoSize
== 0)) {
359 if ((FileInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
365 // Only deal with the EFI key file
367 if (!StrEndWith (FileInfo
->FileName
, FileName
)) {
371 if (FileIndex
== *Index
) {
372 TempName
= StrDuplicate (FileInfo
->FileName
);
384 gBS
->FreePool (FileInfo
);
385 Handle
->Close (Handle
);