]>
Commit | Line | Data |
---|---|---|
2e19fd0f | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | SecMain.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | WinNt emulator of SEC phase. It's really a Win32 application, but this is\r | |
18 | Ok since all the other modules for NT32 are NOT Win32 applications.\r | |
19 | \r | |
952261d5 LG |
20 | This program gets NT32 PCD setting and figures out what the memory layout \r |
21 | will be, how may FD's will be loaded and also what the boot mode is.\r | |
2e19fd0f | 22 | \r |
23 | The SEC registers a set of services with the SEC core. gPrivateDispatchTable\r | |
24 | is a list of PPI's produced by the SEC that are availble for usage in PEI.\r | |
25 | \r | |
952261d5 LG |
26 | This code produces 128 K of temporary memory for the PEI stack by directly\r |
27 | allocate memory space with ReadWrite and Execute attribute.\r | |
2e19fd0f | 28 | \r |
29 | --*/\r | |
30 | \r | |
31 | #include "SecMain.h"\r | |
d0dc913e | 32 | \r |
2e19fd0f | 33 | \r |
34 | //\r | |
35 | // Globals\r | |
36 | //\r | |
37 | EFI_PEI_PE_COFF_LOADER_PROTOCOL_INSTANCE mPeiEfiPeiPeCoffLoaderInstance = {\r | |
38 | {\r | |
39 | SecNt32PeCoffGetImageInfo,\r | |
40 | SecNt32PeCoffLoadImage,\r | |
41 | SecNt32PeCoffRelocateImage,\r | |
42 | SecNt32PeCoffUnloadimage\r | |
43 | },\r | |
44 | NULL\r | |
45 | };\r | |
46 | \r | |
47 | \r | |
48 | \r | |
49 | EFI_PEI_PE_COFF_LOADER_PROTOCOL *gPeiEfiPeiPeCoffLoader = &mPeiEfiPeiPeCoffLoaderInstance.PeCoff;\r | |
50 | \r | |
51 | NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi = { SecWinNtPeiLoadFile };\r | |
52 | \r | |
53 | PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi = { SecWinNtPeiAutoScan };\r | |
54 | \r | |
55 | PEI_NT_THUNK_PPI mSecWinNtThunkPpi = { SecWinNtWinNtThunkAddress };\r | |
56 | \r | |
57 | EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode };\r | |
58 | \r | |
59 | NT_FWH_PPI mSecFwhInformationPpi = { SecWinNtFdAddress };\r | |
60 | \r | |
58dcdada | 61 | TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r |
2e19fd0f | 62 | \r |
63 | EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = {\r | |
64 | {\r | |
65 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
66 | &gEfiPeiPeCoffLoaderGuid,\r | |
67 | NULL\r | |
68 | },\r | |
69 | {\r | |
70 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
71 | &gNtPeiLoadFilePpiGuid,\r | |
72 | &mSecNtLoadFilePpi\r | |
73 | },\r | |
74 | {\r | |
75 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
76 | &gPeiNtAutoScanPpiGuid,\r | |
77 | &mSecNtAutoScanPpi\r | |
78 | },\r | |
79 | {\r | |
80 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
81 | &gPeiNtThunkPpiGuid,\r | |
82 | &mSecWinNtThunkPpi\r | |
83 | },\r | |
84 | {\r | |
85 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
86 | &gEfiPeiStatusCodePpiGuid,\r | |
87 | &mSecStatusCodePpi\r | |
88 | },\r | |
58dcdada | 89 | {\r |
90 | EFI_PEI_PPI_DESCRIPTOR_PPI,\r | |
91 | &gEfiTemporaryRamSupportPpiGuid,\r | |
92 | &mSecTemporaryRamSupportPpi\r | |
93 | },\r | |
2e19fd0f | 94 | {\r |
95 | EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r | |
96 | &gNtFwhPpiGuid,\r | |
97 | &mSecFwhInformationPpi\r | |
98 | }\r | |
99 | };\r | |
100 | \r | |
101 | \r | |
102 | //\r | |
103 | // Default information about where the FD is located.\r | |
952261d5 | 104 | // This array gets filled in with information from PcdWinNtFirmwareVolume\r |
2e19fd0f | 105 | // The number of array elements is allocated base on parsing\r |
952261d5 | 106 | // PcdWinNtFirmwareVolume and the memory is never freed.\r |
2e19fd0f | 107 | //\r |
108 | UINTN gFdInfoCount = 0;\r | |
109 | NT_FD_INFO *gFdInfo;\r | |
110 | \r | |
111 | //\r | |
112 | // Array that supports seperate memory rantes.\r | |
952261d5 | 113 | // The memory ranges are set by PcdWinNtMemorySizeForSecMain.\r |
2e19fd0f | 114 | // The number of array elements is allocated base on parsing\r |
952261d5 | 115 | // PcdWinNtMemorySizeForSecMain value and the memory is never freed.\r |
2e19fd0f | 116 | //\r |
117 | UINTN gSystemMemoryCount = 0;\r | |
118 | NT_SYSTEM_MEMORY *gSystemMemory;\r | |
119 | \r | |
120 | \r | |
121 | UINTN mPdbNameModHandleArraySize = 0;\r | |
122 | PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;\r | |
123 | \r | |
124 | \r | |
58dcdada | 125 | VOID\r |
126 | EFIAPI\r | |
127 | SecSwitchStack (\r | |
128 | UINT32 TemporaryMemoryBase,\r | |
129 | UINT32 PermenentMemoryBase\r | |
130 | );\r | |
2e19fd0f | 131 | \r |
132 | INTN\r | |
133 | EFIAPI\r | |
134 | main (\r | |
135 | IN INTN Argc,\r | |
136 | IN CHAR8 **Argv,\r | |
137 | IN CHAR8 **Envp\r | |
138 | )\r | |
139 | /*++\r | |
140 | \r | |
141 | Routine Description:\r | |
142 | Main entry point to SEC for WinNt. This is a Windows program\r | |
143 | \r | |
144 | Arguments:\r | |
145 | Argc - Number of command line arguments\r | |
146 | Argv - Array of command line argument strings\r | |
147 | Envp - Array of environmemt variable strings\r | |
148 | \r | |
149 | Returns:\r | |
150 | 0 - Normal exit\r | |
151 | 1 - Abnormal exit\r | |
152 | \r | |
153 | --*/\r | |
154 | {\r | |
155 | EFI_STATUS Status;\r | |
156 | EFI_PHYSICAL_ADDRESS InitialStackMemory;\r | |
157 | UINT64 InitialStackMemorySize;\r | |
158 | UINTN Index;\r | |
159 | UINTN Index1;\r | |
160 | UINTN Index2;\r | |
161 | UINTN PeiIndex;\r | |
162 | CHAR16 *FileName;\r | |
163 | CHAR16 *FileNamePtr;\r | |
164 | BOOLEAN Done;\r | |
165 | VOID *PeiCoreFile;\r | |
166 | CHAR16 *MemorySizeStr;\r | |
167 | CHAR16 *FirmwareVolumesStr;\r | |
85e43be1 | 168 | UINTN *StackPointer;\r |
706e7534 | 169 | \r |
952261d5 LG |
170 | MemorySizeStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtMemorySizeForSecMain);\r |
171 | FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtFirmwareVolume);\r | |
2e19fd0f | 172 | \r |
173 | printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");\r | |
174 | \r | |
175 | //\r | |
176 | // Make some Windows calls to Set the process to the highest priority in the\r | |
177 | // idle class. We need this to have good performance.\r | |
178 | //\r | |
179 | SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r | |
180 | SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r | |
181 | \r | |
182 | //\r | |
183 | // Allocate space for gSystemMemory Array\r | |
184 | //\r | |
185 | gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1;\r | |
186 | gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r | |
187 | if (gSystemMemory == NULL) {\r | |
188 | printf ("ERROR : Can not allocate memory for %s. Exiting.\n", MemorySizeStr);\r | |
189 | exit (1);\r | |
190 | }\r | |
191 | //\r | |
192 | // Allocate space for gSystemMemory Array\r | |
193 | //\r | |
194 | gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\r | |
195 | gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r | |
196 | if (gFdInfo == NULL) {\r | |
197 | printf ("ERROR : Can not allocate memory for %s. Exiting.\n", FirmwareVolumesStr);\r | |
198 | exit (1);\r | |
199 | }\r | |
200 | //\r | |
201 | // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r | |
202 | //\r | |
203 | printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode));\r | |
204 | \r | |
205 | //\r | |
952261d5 | 206 | // Allocate 128K memory to emulate temp memory for PEI.\r |
2e19fd0f | 207 | // on a real platform this would be SRAM, or using the cache as RAM.\r |
208 | // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping\r | |
209 | //\r | |
952261d5 LG |
210 | InitialStackMemorySize = STACK_SIZE;\r |
211 | InitialStackMemory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (InitialStackMemorySize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r | |
212 | if (InitialStackMemory == 0) {\r | |
213 | printf ("ERROR : Can not allocate enough space for SecStack\n");\r | |
2e19fd0f | 214 | exit (1);\r |
215 | }\r | |
216 | \r | |
85e43be1 | 217 | for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;\r |
218 | StackPointer < (UINTN*) ((UINTN)InitialStackMemory + (SIZE_T) InitialStackMemorySize);\r | |
219 | StackPointer ++) {\r | |
220 | *StackPointer = 0x5AA55AA5;\r | |
221 | }\r | |
222 | \r | |
2e19fd0f | 223 | printf (" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize);\r |
224 | \r | |
225 | //\r | |
226 | // Open All the firmware volumes and remember the info in the gFdInfo global\r | |
227 | //\r | |
228 | FileNamePtr = (CHAR16 *)malloc (StrLen ((CHAR16 *)FirmwareVolumesStr) * sizeof(CHAR16));\r | |
229 | if (FileNamePtr == NULL) {\r | |
230 | printf ("ERROR : Can not allocate memory for firmware volume string\n");\r | |
231 | exit (1);\r | |
232 | }\r | |
233 | \r | |
234 | StrCpy (FileNamePtr, (CHAR16*)FirmwareVolumesStr);\r | |
235 | \r | |
236 | for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL; !Done; Index++) {\r | |
237 | FileName = FileNamePtr;\r | |
238 | for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r | |
239 | ;\r | |
240 | if (FileNamePtr[Index1] == 0) {\r | |
241 | Done = TRUE;\r | |
242 | } else {\r | |
243 | FileNamePtr[Index1] = '\0';\r | |
244 | FileNamePtr = FileNamePtr + Index1 + 1;\r | |
245 | }\r | |
246 | \r | |
247 | //\r | |
248 | // Open the FD and remmeber where it got mapped into our processes address space\r | |
249 | //\r | |
250 | Status = WinNtOpenFile (\r | |
251 | FileName,\r | |
252 | 0,\r | |
253 | OPEN_EXISTING,\r | |
254 | &gFdInfo[Index].Address,\r | |
255 | &gFdInfo[Index].Size\r | |
256 | );\r | |
257 | if (EFI_ERROR (Status)) {\r | |
258 | printf ("ERROR : Can not open Firmware Device File %S (%r). Exiting.\n", FileName, Status);\r | |
259 | exit (1);\r | |
260 | }\r | |
261 | \r | |
262 | printf (" FD loaded from");\r | |
263 | //\r | |
264 | // printf can't print filenames directly as the \ gets interperted as an\r | |
265 | // escape character.\r | |
266 | //\r | |
267 | for (Index2 = 0; FileName[Index2] != '\0'; Index2++) {\r | |
268 | printf ("%c", FileName[Index2]);\r | |
269 | }\r | |
270 | \r | |
271 | if (PeiCoreFile == NULL) {\r | |
272 | //\r | |
273 | // Assume the beginning of the FD is an FV and look for the PEI Core.\r | |
274 | // Load the first one we find.\r | |
275 | //\r | |
276 | Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r | |
277 | if (!EFI_ERROR (Status)) {\r | |
278 | PeiIndex = Index;\r | |
279 | printf (" contains SEC Core");\r | |
280 | }\r | |
281 | }\r | |
282 | \r | |
283 | printf ("\n");\r | |
284 | }\r | |
285 | //\r | |
286 | // Calculate memory regions and store the information in the gSystemMemory\r | |
287 | // global for later use. The autosizing code will use this data to\r | |
288 | // map this memory into the SEC process memory space.\r | |
289 | //\r | |
290 | for (Index = 0, Done = FALSE; !Done; Index++) {\r | |
291 | //\r | |
292 | // Save the size of the memory and make a Unicode filename SystemMemory00, ...\r | |
293 | //\r | |
294 | gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * 0x100000;\r | |
2e19fd0f | 295 | \r |
296 | //\r | |
297 | // Find the next region\r | |
298 | //\r | |
299 | for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++)\r | |
300 | ;\r | |
301 | if (MemorySizeStr[Index1] == 0) {\r | |
302 | Done = TRUE;\r | |
303 | }\r | |
304 | \r | |
305 | MemorySizeStr = MemorySizeStr + Index1 + 1;\r | |
306 | }\r | |
307 | \r | |
308 | printf ("\n");\r | |
309 | \r | |
310 | //\r | |
311 | // Hand off to PEI Core\r | |
312 | //\r | |
313 | SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile);\r | |
314 | \r | |
315 | //\r | |
316 | // If we get here, then the PEI Core returned. This is an error as PEI should\r | |
317 | // always hand off to DXE.\r | |
318 | //\r | |
319 | printf ("ERROR : PEI Core returned\n");\r | |
320 | exit (1);\r | |
321 | }\r | |
322 | \r | |
323 | EFI_STATUS\r | |
324 | WinNtOpenFile (\r | |
325 | IN CHAR16 *FileName,\r | |
326 | IN UINT32 MapSize,\r | |
327 | IN DWORD CreationDisposition,\r | |
328 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r | |
329 | OUT UINT64 *Length\r | |
330 | )\r | |
331 | /*++\r | |
332 | \r | |
333 | Routine Description:\r | |
334 | Opens and memory maps a file using WinNt services. If BaseAddress is non zero\r | |
335 | the process will try and allocate the memory starting at BaseAddress.\r | |
336 | \r | |
337 | Arguments:\r | |
338 | FileName - The name of the file to open and map\r | |
339 | MapSize - The amount of the file to map in bytes\r | |
340 | CreationDisposition - The flags to pass to CreateFile(). Use to create new files for\r | |
341 | memory emulation, and exiting files for firmware volume emulation\r | |
342 | BaseAddress - The base address of the mapped file in the user address space.\r | |
343 | If passed in as NULL the a new memory region is used.\r | |
344 | If passed in as non NULL the request memory region is used for\r | |
345 | the mapping of the file into the process space.\r | |
346 | Length - The size of the mapped region in bytes\r | |
347 | \r | |
348 | Returns:\r | |
349 | EFI_SUCCESS - The file was opened and mapped.\r | |
350 | EFI_NOT_FOUND - FileName was not found in the current directory\r | |
351 | EFI_DEVICE_ERROR - An error occured attempting to map the opened file\r | |
352 | \r | |
353 | --*/\r | |
354 | {\r | |
355 | HANDLE NtFileHandle;\r | |
356 | HANDLE NtMapHandle;\r | |
357 | VOID *VirtualAddress;\r | |
358 | UINTN FileSize;\r | |
359 | \r | |
360 | //\r | |
361 | // Use Win API to open/create a file\r | |
362 | //\r | |
363 | NtFileHandle = CreateFile (\r | |
364 | FileName,\r | |
365 | GENERIC_READ | GENERIC_WRITE,\r | |
366 | FILE_SHARE_READ,\r | |
367 | NULL,\r | |
368 | CreationDisposition,\r | |
369 | FILE_ATTRIBUTE_NORMAL,\r | |
370 | NULL\r | |
371 | );\r | |
372 | if (NtFileHandle == INVALID_HANDLE_VALUE) {\r | |
373 | return EFI_NOT_FOUND;\r | |
374 | }\r | |
375 | //\r | |
376 | // Map the open file into a memory range\r | |
377 | //\r | |
378 | NtMapHandle = CreateFileMapping (\r | |
379 | NtFileHandle,\r | |
380 | NULL,\r | |
381 | PAGE_READWRITE,\r | |
382 | 0,\r | |
383 | MapSize,\r | |
384 | NULL\r | |
385 | );\r | |
386 | if (NtMapHandle == NULL) {\r | |
387 | return EFI_DEVICE_ERROR;\r | |
388 | }\r | |
389 | //\r | |
390 | // Get the virtual address (address in the emulator) of the mapped file\r | |
391 | //\r | |
392 | VirtualAddress = MapViewOfFileEx (\r | |
393 | NtMapHandle,\r | |
394 | FILE_MAP_ALL_ACCESS,\r | |
395 | 0,\r | |
396 | 0,\r | |
397 | MapSize,\r | |
398 | (LPVOID) (UINTN) *BaseAddress\r | |
399 | );\r | |
400 | if (VirtualAddress == NULL) {\r | |
401 | return EFI_DEVICE_ERROR;\r | |
402 | }\r | |
403 | \r | |
404 | if (MapSize == 0) {\r | |
405 | //\r | |
406 | // Seek to the end of the file to figure out the true file size.\r | |
407 | //\r | |
408 | FileSize = SetFilePointer (\r | |
409 | NtFileHandle,\r | |
410 | 0,\r | |
411 | NULL,\r | |
412 | FILE_END\r | |
413 | );\r | |
414 | if (FileSize == -1) {\r | |
415 | return EFI_DEVICE_ERROR;\r | |
416 | }\r | |
417 | \r | |
418 | *Length = (UINT64) FileSize;\r | |
419 | } else {\r | |
420 | *Length = (UINT64) MapSize;\r | |
421 | }\r | |
422 | \r | |
423 | *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAddress;\r | |
424 | \r | |
425 | return EFI_SUCCESS;\r | |
426 | }\r | |
427 | \r | |
2e19fd0f | 428 | \r |
d0dc913e | 429 | #define BYTES_PER_RECORD 512\r |
2e19fd0f | 430 | \r |
431 | EFI_STATUS\r | |
432 | EFIAPI\r | |
433 | SecPeiReportStatusCode (\r | |
e1001af1 | 434 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
2e19fd0f | 435 | IN EFI_STATUS_CODE_TYPE CodeType,\r |
436 | IN EFI_STATUS_CODE_VALUE Value,\r | |
437 | IN UINT32 Instance,\r | |
e1001af1 | 438 | IN CONST EFI_GUID *CallerId,\r |
439 | IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL\r | |
2e19fd0f | 440 | )\r |
441 | /*++\r | |
442 | \r | |
443 | Routine Description:\r | |
444 | \r | |
445 | This routine produces the ReportStatusCode PEI service. It's passed\r | |
446 | up to the PEI Core via a PPI. T\r | |
447 | \r | |
448 | This code currently uses the NT clib printf. This does not work the same way\r | |
449 | as the EFI Print (), as %t, %g, %s as Unicode are not supported.\r | |
450 | \r | |
451 | Arguments:\r | |
452 | (see EFI_PEI_REPORT_STATUS_CODE)\r | |
453 | \r | |
454 | Returns:\r | |
455 | EFI_SUCCESS - Always return success\r | |
456 | \r | |
457 | --*/\r | |
458 | // TODO: PeiServices - add argument and description to function comment\r | |
459 | // TODO: CodeType - add argument and description to function comment\r | |
460 | // TODO: Value - add argument and description to function comment\r | |
461 | // TODO: Instance - add argument and description to function comment\r | |
462 | // TODO: CallerId - add argument and description to function comment\r | |
463 | // TODO: Data - add argument and description to function comment\r | |
464 | {\r | |
465 | CHAR8 *Format;\r | |
2e19fd0f | 466 | VA_LIST Marker;\r |
467 | CHAR8 PrintBuffer[BYTES_PER_RECORD * 2];\r | |
468 | CHAR8 *Filename;\r | |
469 | CHAR8 *Description;\r | |
470 | UINT32 LineNumber;\r | |
d0dc913e | 471 | UINT32 ErrorLevel;\r |
2e19fd0f | 472 | \r |
d0dc913e A |
473 | \r |
474 | if (Data == NULL) {\r | |
475 | } else if (ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r | |
2e19fd0f | 476 | //\r |
d0dc913e | 477 | // Processes ASSERT ()\r |
2e19fd0f | 478 | //\r |
d0dc913e | 479 | printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description);\r |
2e19fd0f | 480 | \r |
d0dc913e A |
481 | } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {\r |
482 | //\r | |
483 | // Process DEBUG () macro \r | |
484 | //\r | |
485 | AsciiVSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r | |
486 | printf (PrintBuffer);\r | |
2e19fd0f | 487 | }\r |
488 | \r | |
489 | return EFI_SUCCESS;\r | |
490 | }\r | |
491 | \r | |
5aae0aa7 | 492 | /**\r |
493 | Transfers control to a function starting with a new stack.\r | |
494 | \r | |
495 | Transfers control to the function specified by EntryPoint using the new stack\r | |
496 | specified by NewStack and passing in the parameters specified by Context1 and\r | |
497 | Context2. Context1 and Context2 are optional and may be NULL. The function\r | |
498 | EntryPoint must never return.\r | |
499 | \r | |
500 | If EntryPoint is NULL, then ASSERT().\r | |
501 | If NewStack is NULL, then ASSERT().\r | |
502 | \r | |
503 | @param EntryPoint A pointer to function to call with the new stack.\r | |
504 | @param Context1 A pointer to the context to pass into the EntryPoint\r | |
505 | function.\r | |
506 | @param Context2 A pointer to the context to pass into the EntryPoint\r | |
507 | function.\r | |
508 | @param NewStack A pointer to the new stack to use for the EntryPoint\r | |
509 | function.\r | |
510 | @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's\r | |
511 | Reserved on other architectures.\r | |
512 | \r | |
513 | **/\r | |
514 | VOID\r | |
515 | EFIAPI\r | |
516 | PeiSwitchStacks (\r | |
517 | IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r | |
518 | IN VOID *Context1, OPTIONAL\r | |
519 | IN VOID *Context2, OPTIONAL\r | |
520 | IN VOID *Context3, OPTIONAL\r | |
521 | IN VOID *NewStack\r | |
522 | )\r | |
523 | {\r | |
524 | BASE_LIBRARY_JUMP_BUFFER JumpBuffer;\r | |
525 | \r | |
526 | ASSERT (EntryPoint != NULL);\r | |
527 | ASSERT (NewStack != NULL);\r | |
528 | \r | |
529 | //\r | |
530 | // Stack should be aligned with CPU_STACK_ALIGNMENT\r | |
531 | //\r | |
532 | ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);\r | |
533 | \r | |
534 | JumpBuffer.Eip = (UINTN)EntryPoint;\r | |
535 | JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*);\r | |
536 | JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3);\r | |
537 | ((VOID**)JumpBuffer.Esp)[1] = Context1;\r | |
538 | ((VOID**)JumpBuffer.Esp)[2] = Context2;\r | |
539 | ((VOID**)JumpBuffer.Esp)[3] = Context3;\r | |
540 | \r | |
541 | LongJump (&JumpBuffer, (UINTN)-1);\r | |
542 | \r | |
543 | \r | |
544 | //\r | |
545 | // InternalSwitchStack () will never return\r | |
546 | //\r | |
547 | ASSERT (FALSE); \r | |
548 | }\r | |
2e19fd0f | 549 | \r |
550 | VOID\r | |
551 | SecLoadFromCore (\r | |
552 | IN UINTN LargestRegion,\r | |
553 | IN UINTN LargestRegionSize,\r | |
554 | IN UINTN BootFirmwareVolumeBase,\r | |
555 | IN VOID *PeiCorePe32File\r | |
556 | )\r | |
557 | /*++\r | |
558 | \r | |
559 | Routine Description:\r | |
560 | This is the service to load the PEI Core from the Firmware Volume\r | |
561 | \r | |
562 | Arguments:\r | |
563 | LargestRegion - Memory to use for PEI.\r | |
564 | LargestRegionSize - Size of Memory to use for PEI\r | |
565 | BootFirmwareVolumeBase - Start of the Boot FV\r | |
566 | PeiCorePe32File - PEI Core PE32\r | |
567 | \r | |
568 | Returns:\r | |
569 | Success means control is transfered and thus we should never return\r | |
570 | \r | |
571 | --*/\r | |
572 | {\r | |
573 | EFI_STATUS Status;\r | |
574 | EFI_PHYSICAL_ADDRESS TopOfMemory;\r | |
575 | VOID *TopOfStack;\r | |
576 | UINT64 PeiCoreSize;\r | |
577 | EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint;\r | |
578 | EFI_PHYSICAL_ADDRESS PeiImageAddress;\r | |
5aae0aa7 | 579 | EFI_SEC_PEI_HAND_OFF *SecCoreData;\r |
58dcdada | 580 | UINTN PeiStackSize;\r |
2e19fd0f | 581 | \r |
582 | //\r | |
583 | // Compute Top Of Memory for Stack and PEI Core Allocations\r | |
584 | //\r | |
58dcdada | 585 | TopOfMemory = LargestRegion + LargestRegionSize;\r |
586 | PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r | |
2e19fd0f | 587 | \r |
588 | //\r | |
58dcdada | 589 | // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r |
590 | // | Heap |\r | |
591 | // | |\r | |
592 | // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r | |
593 | // | |\r | |
594 | // | Stack |\r | |
595 | // |-----------| <---- TemporaryRamBase\r | |
596 | // \r | |
597 | TopOfStack = (VOID *)(LargestRegion + PeiStackSize);\r | |
598 | TopOfMemory = LargestRegion + PeiStackSize;\r | |
599 | \r | |
2e19fd0f | 600 | //\r |
58dcdada | 601 | // Reservet space for storing PeiCore's parament in stack.\r |
602 | // \r | |
603 | TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r | |
2e19fd0f | 604 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r |
2e19fd0f | 605 | \r |
606 | //\r | |
607 | // Patch value in dispatch table values\r | |
608 | //\r | |
609 | gPrivateDispatchTable[0].Ppi = gPeiEfiPeiPeCoffLoader;\r | |
610 | \r | |
611 | //\r | |
612 | // Bind this information into the SEC hand-off state\r | |
613 | //\r | |
5aae0aa7 | 614 | SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;\r |
615 | SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF);\r | |
616 | SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;\r | |
617 | SecCoreData->BootFirmwareVolumeSize = FixedPcdGet32(PcdWinNtFirmwareFdSize);\r | |
58dcdada | 618 | SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; \r |
5aae0aa7 | 619 | SecCoreData->TemporaryRamSize = STACK_SIZE;\r |
ff33b87d | 620 | SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r |
58dcdada | 621 | SecCoreData->StackSize = PeiStackSize;\r |
ff33b87d | 622 | SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize);\r |
58dcdada | 623 | SecCoreData->PeiTemporaryRamSize = STACK_SIZE - PeiStackSize;\r |
2e19fd0f | 624 | \r |
625 | //\r | |
626 | // Load the PEI Core from a Firmware Volume\r | |
627 | //\r | |
628 | Status = SecWinNtPeiLoadFile (\r | |
629 | PeiCorePe32File,\r | |
630 | &PeiImageAddress,\r | |
631 | &PeiCoreSize,\r | |
632 | &PeiCoreEntryPoint\r | |
633 | );\r | |
634 | if (EFI_ERROR (Status)) {\r | |
635 | return ;\r | |
636 | }\r | |
5aae0aa7 | 637 | \r |
2e19fd0f | 638 | //\r |
639 | // Transfer control to the PEI Core\r | |
640 | //\r | |
5aae0aa7 | 641 | PeiSwitchStacks (\r |
2e19fd0f | 642 | (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r |
5aae0aa7 | 643 | SecCoreData,\r |
644 | (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable),\r | |
2e19fd0f | 645 | NULL,\r |
646 | TopOfStack\r | |
647 | );\r | |
648 | //\r | |
649 | // If we get here, then the PEI Core returned. This is an error\r | |
650 | //\r | |
651 | return ;\r | |
652 | }\r | |
653 | \r | |
654 | EFI_STATUS\r | |
655 | EFIAPI\r | |
656 | SecWinNtPeiAutoScan (\r | |
657 | IN UINTN Index,\r | |
658 | OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r | |
659 | OUT UINT64 *MemorySize\r | |
660 | )\r | |
661 | /*++\r | |
662 | \r | |
663 | Routine Description:\r | |
664 | This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r | |
665 | It allows discontiguous memory regions to be supported by the emulator.\r | |
666 | It uses gSystemMemory[] and gSystemMemoryCount that were created by\r | |
952261d5 LG |
667 | parsing PcdWinNtMemorySizeForSecMain value.\r |
668 | The size comes from the Pcd value and the address comes from the memory space \r | |
669 | with ReadWrite and Execute attributes allocated by VirtualAlloc() API.\r | |
2e19fd0f | 670 | \r |
671 | Arguments:\r | |
672 | Index - Which memory region to use\r | |
673 | MemoryBase - Return Base address of memory region\r | |
674 | MemorySize - Return size in bytes of the memory region\r | |
675 | \r | |
676 | Returns:\r | |
677 | EFI_SUCCESS - If memory region was mapped\r | |
678 | EFI_UNSUPPORTED - If Index is not supported\r | |
679 | \r | |
680 | --*/\r | |
681 | {\r | |
2e19fd0f | 682 | if (Index >= gSystemMemoryCount) {\r |
683 | return EFI_UNSUPPORTED;\r | |
684 | }\r | |
952261d5 LG |
685 | \r |
686 | //\r | |
687 | // Allocate enough memory space for emulator \r | |
688 | //\r | |
689 | gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r | |
690 | if (gSystemMemory[Index].Memory == 0) {\r | |
691 | return EFI_OUT_OF_RESOURCES;\r | |
692 | }\r | |
693 | \r | |
694 | *MemoryBase = gSystemMemory[Index].Memory;\r | |
695 | *MemorySize = gSystemMemory[Index].Size;\r | |
2e19fd0f | 696 | \r |
952261d5 | 697 | return EFI_SUCCESS;\r |
2e19fd0f | 698 | }\r |
699 | \r | |
700 | VOID *\r | |
701 | EFIAPI\r | |
702 | SecWinNtWinNtThunkAddress (\r | |
703 | VOID\r | |
704 | )\r | |
705 | /*++\r | |
706 | \r | |
707 | Routine Description:\r | |
708 | Since the SEC is the only Windows program in stack it must export\r | |
709 | an interface to do Win API calls. That's what the WinNtThunk address\r | |
710 | is for. gWinNt is initailized in WinNtThunk.c.\r | |
711 | \r | |
712 | Arguments:\r | |
713 | InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r | |
714 | InterfaceBase - Address of the gWinNt global\r | |
715 | \r | |
716 | Returns:\r | |
717 | EFI_SUCCESS - Data returned\r | |
718 | \r | |
719 | --*/\r | |
720 | {\r | |
721 | return gWinNt;\r | |
722 | }\r | |
723 | \r | |
724 | \r | |
725 | EFI_STATUS\r | |
726 | EFIAPI\r | |
727 | SecWinNtPeiLoadFile (\r | |
728 | IN VOID *Pe32Data,\r | |
729 | IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
730 | IN UINT64 *ImageSize,\r | |
731 | IN EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
732 | )\r | |
733 | /*++\r | |
734 | \r | |
735 | Routine Description:\r | |
736 | Loads and relocates a PE/COFF image into memory.\r | |
737 | \r | |
738 | Arguments:\r | |
739 | Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r | |
740 | ImageAddress - The base address of the relocated PE/COFF image\r | |
741 | ImageSize - The size of the relocated PE/COFF image\r | |
742 | EntryPoint - The entry point of the relocated PE/COFF image\r | |
743 | \r | |
744 | Returns:\r | |
745 | EFI_SUCCESS - The file was loaded and relocated\r | |
746 | EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r | |
747 | \r | |
748 | --*/\r | |
749 | {\r | |
750 | EFI_STATUS Status;\r | |
751 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
752 | \r | |
753 | ZeroMem (&ImageContext, sizeof (ImageContext));\r | |
754 | ImageContext.Handle = Pe32Data;\r | |
755 | \r | |
756 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r | |
757 | \r | |
758 | Status = gPeiEfiPeiPeCoffLoader->GetImageInfo (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
759 | if (EFI_ERROR (Status)) {\r | |
760 | return Status;\r | |
761 | }\r | |
762 | //\r | |
952261d5 LG |
763 | // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue. \r |
764 | // Extra space is for alignment\r | |
2e19fd0f | 765 | //\r |
952261d5 | 766 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r |
2e19fd0f | 767 | if (ImageContext.ImageAddress == 0) {\r |
768 | return EFI_OUT_OF_RESOURCES;\r | |
769 | }\r | |
770 | //\r | |
771 | // Align buffer on section boundry\r | |
772 | //\r | |
773 | ImageContext.ImageAddress += ImageContext.SectionAlignment;\r | |
774 | ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r | |
775 | \r | |
776 | Status = gPeiEfiPeiPeCoffLoader->LoadImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
777 | if (EFI_ERROR (Status)) {\r | |
778 | return Status;\r | |
779 | }\r | |
780 | \r | |
781 | Status = gPeiEfiPeiPeCoffLoader->RelocateImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
782 | if (EFI_ERROR (Status)) {\r | |
783 | return Status;\r | |
784 | }\r | |
785 | \r | |
786 | //\r | |
787 | // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r | |
788 | //\r | |
789 | \r | |
790 | *ImageAddress = ImageContext.ImageAddress;\r | |
791 | *ImageSize = ImageContext.ImageSize;\r | |
792 | *EntryPoint = ImageContext.EntryPoint;\r | |
793 | \r | |
794 | return EFI_SUCCESS;\r | |
795 | }\r | |
796 | \r | |
797 | EFI_STATUS\r | |
798 | EFIAPI\r | |
799 | SecWinNtFdAddress (\r | |
800 | IN UINTN Index,\r | |
801 | IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r | |
802 | IN OUT UINT64 *FdSize\r | |
803 | )\r | |
804 | /*++\r | |
805 | \r | |
806 | Routine Description:\r | |
807 | Return the FD Size and base address. Since the FD is loaded from a\r | |
808 | file into Windows memory only the SEC will know it's address.\r | |
809 | \r | |
810 | Arguments:\r | |
811 | Index - Which FD, starts at zero.\r | |
812 | FdSize - Size of the FD in bytes\r | |
813 | FdBase - Start address of the FD. Assume it points to an FV Header\r | |
814 | \r | |
815 | Returns:\r | |
816 | EFI_SUCCESS - Return the Base address and size of the FV\r | |
817 | EFI_UNSUPPORTED - Index does nto map to an FD in the system\r | |
818 | \r | |
819 | --*/\r | |
820 | {\r | |
821 | if (Index >= gFdInfoCount) {\r | |
822 | return EFI_UNSUPPORTED;\r | |
823 | }\r | |
824 | \r | |
825 | *FdBase = gFdInfo[Index].Address;\r | |
826 | *FdSize = gFdInfo[Index].Size;\r | |
827 | \r | |
828 | if (*FdBase == 0 && *FdSize == 0) {\r | |
829 | return EFI_UNSUPPORTED;\r | |
830 | }\r | |
831 | \r | |
832 | return EFI_SUCCESS;\r | |
833 | }\r | |
834 | \r | |
835 | EFI_STATUS\r | |
836 | EFIAPI\r | |
837 | SecImageRead (\r | |
838 | IN VOID *FileHandle,\r | |
839 | IN UINTN FileOffset,\r | |
840 | IN OUT UINTN *ReadSize,\r | |
841 | OUT VOID *Buffer\r | |
842 | )\r | |
843 | /*++\r | |
844 | \r | |
845 | Routine Description:\r | |
846 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r | |
847 | \r | |
848 | Arguments:\r | |
849 | FileHandle - The handle to the PE/COFF file\r | |
850 | FileOffset - The offset, in bytes, into the file to read\r | |
851 | ReadSize - The number of bytes to read from the file starting at FileOffset\r | |
852 | Buffer - A pointer to the buffer to read the data into.\r | |
853 | \r | |
854 | Returns:\r | |
855 | EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r | |
856 | \r | |
857 | --*/\r | |
858 | {\r | |
859 | CHAR8 *Destination8;\r | |
860 | CHAR8 *Source8;\r | |
861 | UINTN Length;\r | |
862 | \r | |
863 | Destination8 = Buffer;\r | |
864 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r | |
865 | Length = *ReadSize;\r | |
866 | while (Length--) {\r | |
867 | *(Destination8++) = *(Source8++);\r | |
868 | }\r | |
869 | \r | |
870 | return EFI_SUCCESS;\r | |
871 | }\r | |
872 | \r | |
873 | CHAR16 *\r | |
874 | AsciiToUnicode (\r | |
875 | IN CHAR8 *Ascii,\r | |
876 | IN UINTN *StrLen OPTIONAL\r | |
877 | )\r | |
878 | /*++\r | |
879 | \r | |
880 | Routine Description:\r | |
881 | Convert the passed in Ascii string to Unicode.\r | |
882 | Optionally return the length of the strings.\r | |
883 | \r | |
884 | Arguments:\r | |
885 | Ascii - Ascii string to convert\r | |
886 | StrLen - Length of string\r | |
887 | \r | |
888 | Returns:\r | |
889 | Pointer to malloc'ed Unicode version of Ascii\r | |
890 | \r | |
891 | --*/\r | |
892 | {\r | |
893 | UINTN Index;\r | |
894 | CHAR16 *Unicode;\r | |
895 | \r | |
896 | //\r | |
897 | // Allocate a buffer for unicode string\r | |
898 | //\r | |
899 | for (Index = 0; Ascii[Index] != '\0'; Index++)\r | |
900 | ;\r | |
901 | Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r | |
902 | if (Unicode == NULL) {\r | |
903 | return NULL;\r | |
904 | }\r | |
905 | \r | |
906 | for (Index = 0; Ascii[Index] != '\0'; Index++) {\r | |
907 | Unicode[Index] = (CHAR16) Ascii[Index];\r | |
908 | }\r | |
909 | \r | |
910 | Unicode[Index] = '\0';\r | |
911 | \r | |
912 | if (StrLen != NULL) {\r | |
913 | *StrLen = Index;\r | |
914 | }\r | |
915 | \r | |
916 | return Unicode;\r | |
917 | }\r | |
918 | \r | |
919 | UINTN\r | |
920 | CountSeperatorsInString (\r | |
921 | IN const CHAR16 *String,\r | |
922 | IN CHAR16 Seperator\r | |
923 | )\r | |
924 | /*++\r | |
925 | \r | |
926 | Routine Description:\r | |
927 | Count the number of seperators in String\r | |
928 | \r | |
929 | Arguments:\r | |
930 | String - String to process\r | |
931 | Seperator - Item to count\r | |
932 | \r | |
933 | Returns:\r | |
934 | Number of Seperator in String\r | |
935 | \r | |
936 | --*/\r | |
937 | {\r | |
938 | UINTN Count;\r | |
939 | \r | |
940 | for (Count = 0; *String != '\0'; String++) {\r | |
941 | if (*String == Seperator) {\r | |
942 | Count++;\r | |
943 | }\r | |
944 | }\r | |
945 | \r | |
946 | return Count;\r | |
947 | }\r | |
948 | \r | |
949 | \r | |
950 | EFI_STATUS\r | |
951 | AddModHandle (\r | |
952 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
953 | IN VOID *ModHandle\r | |
954 | )\r | |
955 | /*++\r | |
956 | \r | |
957 | Routine Description:\r | |
958 | Store the ModHandle in an array indexed by the Pdb File name.\r | |
959 | The ModHandle is needed to unload the image. \r | |
960 | \r | |
961 | Arguments:\r | |
962 | ImageContext - Input data returned from PE Laoder Library. Used to find the \r | |
963 | .PDB file name of the PE Image.\r | |
964 | ModHandle - Returned from LoadLibraryEx() and stored for call to \r | |
965 | FreeLibrary().\r | |
966 | \r | |
967 | Returns:\r | |
968 | EFI_SUCCESS - ModHandle was stored. \r | |
969 | \r | |
970 | --*/\r | |
971 | {\r | |
972 | UINTN Index;\r | |
973 | PDB_NAME_TO_MOD_HANDLE *Array;\r | |
974 | UINTN PreviousSize;\r | |
975 | \r | |
976 | \r | |
977 | Array = mPdbNameModHandleArray;\r | |
978 | for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r | |
979 | if (Array->PdbPointer == NULL) {\r | |
980 | //\r | |
981 | // Make a copy of the stirng and store the ModHandle\r | |
982 | //\r | |
983 | Array->PdbPointer = malloc (strlen (ImageContext->PdbPointer) + 1);\r | |
984 | ASSERT (Array->PdbPointer != NULL);\r | |
985 | \r | |
986 | strcpy (Array->PdbPointer, ImageContext->PdbPointer);\r | |
987 | Array->ModHandle = ModHandle;\r | |
988 | return EFI_SUCCESS;\r | |
989 | }\r | |
990 | }\r | |
991 | \r | |
992 | //\r | |
993 | // No free space in mPdbNameModHandleArray so grow it by \r | |
994 | // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will\r | |
995 | // copy the old values to the new locaiton. But it does\r | |
996 | // not zero the new memory area.\r | |
997 | //\r | |
998 | PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r | |
999 | mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r | |
1000 | \r | |
1001 | mPdbNameModHandleArray = realloc (mPdbNameModHandleArray, mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE));\r | |
1002 | if (mPdbNameModHandleArray == NULL) {\r | |
1003 | ASSERT (FALSE);\r | |
1004 | return EFI_OUT_OF_RESOURCES;\r | |
1005 | }\r | |
1006 | \r | |
1007 | memset (mPdbNameModHandleArray + PreviousSize, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (PDB_NAME_TO_MOD_HANDLE));\r | |
1008 | \r | |
1009 | return AddModHandle (ImageContext, ModHandle);\r | |
1010 | }\r | |
1011 | \r | |
1012 | \r | |
1013 | VOID *\r | |
1014 | RemoveModeHandle (\r | |
1015 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1016 | )\r | |
1017 | /*++\r | |
1018 | \r | |
1019 | Routine Description:\r | |
1020 | Return the ModHandle and delete the entry in the array.\r | |
1021 | \r | |
1022 | Arguments:\r | |
1023 | ImageContext - Input data returned from PE Laoder Library. Used to find the \r | |
1024 | .PDB file name of the PE Image.\r | |
1025 | \r | |
1026 | Returns:\r | |
1027 | ModHandle - ModHandle assoicated with ImageContext is returned\r | |
1028 | NULL - No ModHandle associated with ImageContext\r | |
1029 | \r | |
1030 | --*/\r | |
1031 | {\r | |
1032 | UINTN Index;\r | |
1033 | PDB_NAME_TO_MOD_HANDLE *Array;\r | |
1034 | \r | |
1035 | if (ImageContext->PdbPointer == NULL) {\r | |
1036 | //\r | |
1037 | // If no PDB pointer there is no ModHandle so return NULL\r | |
1038 | //\r | |
1039 | return NULL;\r | |
1040 | }\r | |
1041 | \r | |
1042 | Array = mPdbNameModHandleArray;\r | |
1043 | for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r | |
1044 | if ((Array->PdbPointer != NULL) && (strcmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r | |
1045 | //\r | |
1046 | // If you find a match return it and delete the entry\r | |
1047 | //\r | |
1048 | free (Array->PdbPointer);\r | |
1049 | Array->PdbPointer = NULL;\r | |
1050 | return Array->ModHandle;\r | |
1051 | }\r | |
1052 | }\r | |
1053 | \r | |
1054 | return NULL;\r | |
1055 | }\r | |
1056 | \r | |
1057 | \r | |
1058 | \r | |
1059 | EFI_STATUS\r | |
1060 | EFIAPI\r | |
1061 | SecNt32PeCoffGetImageInfo (\r | |
1062 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
1063 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1064 | )\r | |
1065 | {\r | |
1066 | EFI_STATUS Status;\r | |
1067 | \r | |
1068 | Status = PeCoffLoaderGetImageInfo (ImageContext);\r | |
1069 | if (EFI_ERROR (Status)) {\r | |
1070 | return Status;\r | |
1071 | }\r | |
1072 | \r | |
1073 | switch (ImageContext->ImageType) {\r | |
1074 | \r | |
1075 | case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r | |
1076 | ImageContext->ImageCodeMemoryType = EfiLoaderCode;\r | |
1077 | ImageContext->ImageDataMemoryType = EfiLoaderData;\r | |
1078 | break;\r | |
1079 | \r | |
1080 | case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r | |
1081 | ImageContext->ImageCodeMemoryType = EfiBootServicesCode;\r | |
1082 | ImageContext->ImageDataMemoryType = EfiBootServicesData;\r | |
1083 | break;\r | |
1084 | \r | |
1085 | case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r | |
1086 | case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:\r | |
1087 | ImageContext->ImageCodeMemoryType = EfiRuntimeServicesCode;\r | |
1088 | ImageContext->ImageDataMemoryType = EfiRuntimeServicesData;\r | |
1089 | break;\r | |
1090 | \r | |
1091 | default:\r | |
1092 | ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r | |
1093 | return RETURN_UNSUPPORTED;\r | |
1094 | }\r | |
1095 | \r | |
1096 | return Status;\r | |
1097 | }\r | |
1098 | \r | |
1099 | EFI_STATUS\r | |
1100 | EFIAPI\r | |
1101 | SecNt32PeCoffLoadImage (\r | |
1102 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
1103 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1104 | )\r | |
1105 | {\r | |
1106 | EFI_STATUS Status;\r | |
1107 | \r | |
1108 | Status = PeCoffLoaderLoadImage (ImageContext);\r | |
1109 | return Status;\r | |
1110 | }\r | |
1111 | \r | |
1112 | EFI_STATUS\r | |
1113 | EFIAPI\r | |
1114 | SecNt32PeCoffRelocateImage (\r | |
1115 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
1116 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1117 | )\r | |
1118 | {\r | |
1119 | EFI_STATUS Status;\r | |
1120 | VOID *DllEntryPoint;\r | |
1121 | CHAR16 *DllFileName;\r | |
1122 | HMODULE Library;\r | |
1123 | UINTN Index;\r | |
1124 | \r | |
1125 | \r | |
1126 | Status = PeCoffLoaderRelocateImage (ImageContext);\r | |
1127 | if (EFI_ERROR (Status)) {\r | |
1128 | //\r | |
1129 | // We could not relocated the image in memory properly\r | |
1130 | //\r | |
1131 | return Status;\r | |
1132 | }\r | |
1133 | \r | |
1134 | //\r | |
1135 | // If we load our own PE COFF images the Windows debugger can not source\r | |
1136 | // level debug our code. If a valid PDB pointer exists usw it to load\r | |
1137 | // the *.dll file as a library using Windows* APIs. This allows \r | |
1138 | // source level debug. The image is still loaded and reloaced\r | |
1139 | // in the Framework memory space like on a real system (by the code above),\r | |
1140 | // but the entry point points into the DLL loaded by the code bellow. \r | |
1141 | //\r | |
1142 | \r | |
1143 | DllEntryPoint = NULL;\r | |
1144 | \r | |
1145 | //\r | |
1146 | // Load the DLL if it's not an EBC image.\r | |
1147 | //\r | |
1148 | if ((ImageContext->PdbPointer != NULL) &&\r | |
1149 | (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r | |
1150 | //\r | |
1151 | // Convert filename from ASCII to Unicode\r | |
1152 | //\r | |
1153 | DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r | |
1154 | \r | |
1155 | //\r | |
1156 | // Check that we have a valid filename\r | |
1157 | //\r | |
1158 | if (Index < 5 || DllFileName[Index - 4] != '.') {\r | |
1159 | free (DllFileName);\r | |
1160 | \r | |
1161 | //\r | |
1162 | // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r | |
1163 | // The image will run, but we just can't source level debug. If we\r | |
1164 | // return an error the image will not run.\r | |
1165 | //\r | |
1166 | return EFI_SUCCESS;\r | |
1167 | }\r | |
1168 | //\r | |
1169 | // Replace .PDB with .DLL on the filename\r | |
1170 | //\r | |
1171 | DllFileName[Index - 3] = 'D';\r | |
1172 | DllFileName[Index - 2] = 'L';\r | |
1173 | DllFileName[Index - 1] = 'L';\r | |
1174 | \r | |
1175 | //\r | |
1176 | // Load the .DLL file into the user process's address space for source \r | |
1177 | // level debug\r | |
1178 | //\r | |
1179 | Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r | |
1180 | if (Library != NULL) {\r | |
1181 | //\r | |
1182 | // InitializeDriver is the entry point we put in all our EFI DLL's. The\r | |
1183 | // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r | |
1184 | // normal DLL entry point of DllMain, and prevents other modules that are\r | |
1185 | // referenced in side the DllFileName from being loaded. There is no error \r | |
1186 | // checking as the we can point to the PE32 image loaded by Tiano. This \r | |
1187 | // step is only needed for source level debuging\r | |
1188 | //\r | |
1189 | DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r | |
1190 | \r | |
1191 | }\r | |
1192 | \r | |
1193 | if ((Library != NULL) && (DllEntryPoint != NULL)) {\r | |
1194 | AddModHandle (ImageContext, Library);\r | |
1195 | ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r | |
1196 | wprintf (L"LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r | |
1197 | } else {\r | |
1198 | wprintf (L"WARNING: No source level debug %s. \n", DllFileName);\r | |
1199 | }\r | |
1200 | \r | |
1201 | free (DllFileName);\r | |
1202 | }\r | |
1203 | \r | |
1204 | //\r | |
1205 | // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r | |
1206 | // The image will run, but we just can't source level debug. If we\r | |
1207 | // return an error the image will not run.\r | |
1208 | //\r | |
1209 | return EFI_SUCCESS;\r | |
1210 | }\r | |
1211 | \r | |
1212 | \r | |
1213 | EFI_STATUS\r | |
1214 | EFIAPI\r | |
1215 | SecNt32PeCoffUnloadimage (\r | |
1216 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
1217 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1218 | )\r | |
1219 | {\r | |
1220 | VOID *ModHandle;\r | |
1221 | \r | |
1222 | ModHandle = RemoveModeHandle (ImageContext);\r | |
1223 | if (ModHandle != NULL) {\r | |
1224 | FreeLibrary (ModHandle);\r | |
1225 | }\r | |
1226 | return EFI_SUCCESS;\r | |
1227 | }\r | |
1228 | \r | |
1229 | VOID\r | |
1230 | _ModuleEntryPoint (\r | |
1231 | VOID\r | |
1232 | )\r | |
1233 | {\r | |
1234 | }\r | |
1235 | \r | |
58dcdada | 1236 | EFI_STATUS\r |
1237 | EFIAPI\r | |
1238 | SecTemporaryRamSupport (\r | |
1239 | IN CONST EFI_PEI_SERVICES **PeiServices,\r | |
1240 | IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r | |
1241 | IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r | |
1242 | IN UINTN CopySize\r | |
1243 | )\r | |
1244 | {\r | |
1245 | //\r | |
1246 | // Migrate the whole temporary memory to permenent memory.\r | |
1247 | // \r | |
1248 | CopyMem (\r | |
1249 | (VOID*)(UINTN)PermanentMemoryBase, \r | |
1250 | (VOID*)(UINTN)TemporaryMemoryBase, \r | |
1251 | CopySize\r | |
1252 | );\r | |
1253 | \r | |
1254 | //\r | |
1255 | // SecSwitchStack function must be invoked after the memory migration\r | |
1256 | // immediatly, also we need fixup the stack change caused by new call into \r | |
1257 | // permenent memory.\r | |
1258 | // \r | |
1259 | SecSwitchStack (\r | |
1260 | (UINT32) TemporaryMemoryBase,\r | |
1261 | (UINT32) PermanentMemoryBase\r | |
1262 | );\r | |
1263 | \r | |
1264 | //\r | |
1265 | // We need *not* fix the return address because currently, \r | |
1266 | // The PeiCore is excuted in flash.\r | |
1267 | //\r | |
1268 | \r | |
1269 | //\r | |
1270 | // Simulate to invalid CAR, terminate CAR\r | |
1271 | // \r | |
1272 | //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);\r | |
1273 | \r | |
1274 | return EFI_SUCCESS;\r | |
1275 | }\r | |
1276 | \r |