TianoCore EDK2 master
Loading...
Searching...
No Matches
EdbSupportFile.c
Go to the documentation of this file.
1
9#include "Edb.h"
10
24EFIAPI
27 IN CHAR16 *FileName,
28 OUT UINTN *BufferSize,
29 OUT VOID **Buffer
30 )
31{
32 EFI_STATUS Status;
33 EFI_FILE_HANDLE RootDir;
34 EFI_FILE_HANDLE Handle;
35 UINTN FileInfoSize;
37 UINTN TempBufferSize;
38 VOID *TempBuffer;
39
40 //
41 // Open the root directory
42 //
43 Status = Vol->OpenVolume (Vol, &RootDir);
44 if (EFI_ERROR (Status)) {
45 return Status;
46 }
47
48 //
49 // Open the file
50 //
51 Status = RootDir->Open (
52 RootDir,
53 &Handle,
54 FileName,
55 EFI_FILE_MODE_READ,
56 0
57 );
58 if (EFI_ERROR (Status)) {
59 RootDir->Close (RootDir);
60 return Status;
61 }
62
63 RootDir->Close (RootDir);
64
65 //
66 // Get the file information
67 //
68 FileInfoSize = sizeof (EFI_FILE_INFO) + 1024;
69
70 FileInfo = AllocateZeroPool (FileInfoSize);
71 if (FileInfo == NULL) {
72 Handle->Close (Handle);
73 return Status;
74 }
75
76 Status = Handle->GetInfo (
77 Handle,
78 &gEfiFileInfoGuid,
79 &FileInfoSize,
81 );
82 if (EFI_ERROR (Status)) {
83 Handle->Close (Handle);
84 gBS->FreePool (FileInfo);
85 return Status;
86 }
87
88 //
89 // Allocate buffer for the file data. The last CHAR16 is for L'\0'
90 //
91 TempBufferSize = (UINTN)FileInfo->FileSize + sizeof (CHAR16);
92 TempBuffer = AllocateZeroPool (TempBufferSize);
93 if (TempBuffer == NULL) {
94 Handle->Close (Handle);
95 gBS->FreePool (FileInfo);
96 return Status;
97 }
98
99 gBS->FreePool (FileInfo);
100
101 //
102 // Read the file data to the buffer
103 //
104 Status = Handle->Read (
105 Handle,
106 &TempBufferSize,
107 TempBuffer
108 );
109 if (EFI_ERROR (Status)) {
110 Handle->Close (Handle);
111 gBS->FreePool (TempBuffer);
112 return Status;
113 }
114
115 Handle->Close (Handle);
116
117 *BufferSize = TempBufferSize;
118 *Buffer = TempBuffer;
119 return EFI_SUCCESS;
120}
121
142EFIAPI
144 IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
145 IN CHAR16 *FileName,
146 OUT UINTN *BufferSize,
147 OUT VOID **Buffer,
148 IN BOOLEAN ScanFs
149 )
150{
151 EFI_STATUS Status;
153 UINTN TempBufferSize;
154 VOID *TempBuffer;
155 UINTN NoHandles;
156 EFI_HANDLE *HandleBuffer;
157 UINTN Index;
158
159 //
160 // Check parameters
161 //
162 if ((FileName == NULL) || (Buffer == NULL)) {
163 return EFI_INVALID_PARAMETER;
164 }
165
166 //
167 // not scan fs
168 //
169 if (!ScanFs) {
170 if (DebuggerPrivate->Vol == NULL) {
171 return EFI_INVALID_PARAMETER;
172 }
173
174 //
175 // Read file directly from Vol
176 //
177 return ReadFileFromVol (DebuggerPrivate->Vol, FileName, BufferSize, Buffer);
178 }
179
180 //
181 // need scan fs
182 //
183
184 //
185 // Get all Vol handle
186 //
187 Status = gBS->LocateHandleBuffer (
189 &gEfiSimpleFileSystemProtocolGuid,
190 NULL,
191 &NoHandles,
192 &HandleBuffer
193 );
194 if (EFI_ERROR (Status) && (NoHandles == 0)) {
195 return EFI_NOT_FOUND;
196 }
197
198 //
199 // Walk through each Vol
200 //
201 DebuggerPrivate->Vol = NULL;
202 *BufferSize = 0;
203 *Buffer = NULL;
204 for (Index = 0; Index < NoHandles; Index++) {
205 Status = gBS->HandleProtocol (
206 HandleBuffer[Index],
207 &gEfiSimpleFileSystemProtocolGuid,
208 (VOID **)&Vol
209 );
210 if (EFI_ERROR (Status)) {
211 continue;
212 }
213
214 Status = ReadFileFromVol (Vol, FileName, &TempBufferSize, &TempBuffer);
215 if (!EFI_ERROR (Status)) {
216 //
217 // Read file OK, check duplication
218 //
219 if (DebuggerPrivate->Vol != NULL) {
220 //
221 // Find the duplicated file
222 //
223 gBS->FreePool (TempBuffer);
224 gBS->FreePool (*Buffer);
225 EDBPrint (L"Duplicated FileName found!\n");
226 return EFI_NO_MAPPING;
227 } else {
228 //
229 // Record value
230 //
231 DebuggerPrivate->Vol = Vol;
232 *BufferSize = TempBufferSize;
233 *Buffer = TempBuffer;
234 }
235 }
236 }
237
238 //
239 // Scan Fs done
240 //
241 if (DebuggerPrivate->Vol == NULL) {
242 return EFI_NOT_FOUND;
243 }
244
245 //
246 // Done
247 //
248 return EFI_SUCCESS;
249}
250
263CHAR16 *
264EFIAPI
266 IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
267 IN CHAR16 *DirName,
268 IN CHAR16 *FileName,
269 IN OUT UINTN *Index
270 )
271{
272 EFI_STATUS Status;
273 EFI_FILE_HANDLE RootDir;
274 EFI_FILE_HANDLE Handle;
275 UINTN FileInfoSize;
278 VOID *TempName;
279 UINTN FileIndex;
280
281 if (DebuggerPrivate->Vol == NULL) {
282 Status = gBS->LocateProtocol (
283 &gEfiSimpleFileSystemProtocolGuid,
284 NULL,
285 (VOID **)&DebuggerPrivate->Vol
286 );
287 if (EFI_ERROR (Status)) {
288 return NULL;
289 }
290 }
291
292 Vol = DebuggerPrivate->Vol;
293
294 //
295 // Open the root directory
296 //
297 Status = Vol->OpenVolume (Vol, &RootDir);
298 if (EFI_ERROR (Status)) {
299 return NULL;
300 }
301
302 //
303 // Open the file
304 //
305 Status = RootDir->Open (
306 RootDir,
307 &Handle,
308 DirName,
309 EFI_FILE_MODE_READ,
310 EFI_FILE_DIRECTORY
311 );
312 if (EFI_ERROR (Status)) {
313 RootDir->Close (RootDir);
314 return NULL;
315 }
316
317 RootDir->Close (RootDir);
318
319 //
320 // Set Dir Position
321 //
322 Status = Handle->SetPosition (Handle, 0);
323 if (EFI_ERROR (Status)) {
324 Handle->Close (Handle);
325 return NULL;
326 }
327
328 //
329 // Get the file information
330 //
331 FileInfoSize = sizeof (EFI_FILE_INFO) + 1024;
332
333 FileInfo = AllocateZeroPool (FileInfoSize);
334 if (FileInfo == NULL) {
335 Handle->Close (Handle);
336 return NULL;
337 }
338
339 //
340 // Walk through each file in the directory
341 //
342 FileIndex = 0;
343 TempName = NULL;
344 while (TRUE) {
345 //
346 // Read a file entry
347 //
348 FileInfoSize = sizeof (EFI_FILE_INFO) + 1024;
349
350 Status = Handle->Read (
351 Handle,
352 &FileInfoSize,
354 );
355 if (EFI_ERROR (Status) || (FileInfoSize == 0)) {
356 break;
357 }
358
359 if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
360 //
361 // This is a file
362 //
363
364 //
365 // Only deal with the EFI key file
366 //
367 if (!StrEndWith (FileInfo->FileName, FileName)) {
368 continue;
369 }
370
371 if (FileIndex == *Index) {
372 TempName = StrDuplicate (FileInfo->FileName);
373 *Index = *Index + 1;
374 break;
375 }
376
377 FileIndex++;
378 }
379 }
380
381 //
382 // Free resources
383 //
384 gBS->FreePool (FileInfo);
385 Handle->Close (Handle);
386
387 return TempName;
388}
UINT64 UINTN
BOOLEAN EFIAPI StrEndWith(IN CHAR16 *Str, IN CHAR16 *SubStr)
CHAR16 *EFIAPI StrDuplicate(IN CHAR16 *Src)
UINTN EFIAPI EDBPrint(IN CONST CHAR16 *Format,...)
Definition: EdbSupportUI.c:683
EFI_STATUS EFIAPI ReadFileFromVol(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol, IN CHAR16 *FileName, OUT UINTN *BufferSize, OUT VOID **Buffer)
EFI_STATUS EFIAPI ReadFileToBuffer(IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate, IN CHAR16 *FileName, OUT UINTN *BufferSize, OUT VOID **Buffer, IN BOOLEAN ScanFs)
CHAR16 *EFIAPI GetFileNameUnderDir(IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate, IN CHAR16 *DirName, IN CHAR16 *FileName, IN OUT UINTN *Index)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_FILE_INFO * FileInfo(IN EFI_FILE_HANDLE FHand)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
@ ByProtocol
Definition: UefiSpec.h:1518
UINT64 Attribute
Definition: FileInfo.h:47
CHAR16 FileName[1]
Definition: FileInfo.h:52
UINT64 FileSize
Definition: FileInfo.h:27