]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/EbcDebugger/EdbSupportFile.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcDebugger / EdbSupportFile.c
1 /** @file
2
3 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6
7 **/
8
9 #include "Edb.h"
10
11 /**
12 Read a file.
13
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
18
19 @retval EFI_SUCCESS - read file successfully
20 @retval EFI_NOT_FOUND - file not found
21
22 **/
23 EFI_STATUS
24 EFIAPI
25 ReadFileFromVol (
26 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol,
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;
36 EFI_FILE_INFO *FileInfo;
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,
80 FileInfo
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
122 /**
123
124 Read a file.
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.
129
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
135
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
139
140 **/
141 EFI_STATUS
142 EFIAPI
143 ReadFileToBuffer (
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;
152 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol;
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 (
188 ByProtocol,
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
251 /**
252
253 Get file name under this dir with index
254
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
259
260 @return File Name which match the pattern and index.
261
262 **/
263 CHAR16 *
264 EFIAPI
265 GetFileNameUnderDir (
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;
276 EFI_FILE_INFO *FileInfo;
277 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol;
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,
353 FileInfo
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 }