]>
Commit | Line | Data |
---|---|---|
804405e7 | 1 | /*++ |
2 | ||
3 | Copyright (c) 2006 - 2007 Intel Corporation. | |
4 | All rights reserved. This program and the accompanying materials | |
5 | are licensed and made available under the terms and conditions of the BSD License | |
6 | which accompanies this distribution. The full text of the license may be found at | |
7 | http://opensource.org/licenses/bsd-license.php | |
8 | ||
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. | |
11 | ||
12 | Module Name: | |
13 | ||
14 | SecMain.c | |
15 | ||
16 | Abstract: | |
17 | WinNt emulator of SEC phase. It's really a Posix application, but this is | |
18 | Ok since all the other modules for NT32 are NOT Posix applications. | |
19 | ||
20 | This program processes host environment variables and figures out | |
21 | what the memory layout will be, how may FD's will be loaded and also | |
22 | what the boot mode is. | |
23 | ||
24 | The SEC registers a set of services with the SEC core. gPrivateDispatchTable | |
25 | is a list of PPI's produced by the SEC that are availble for usage in PEI. | |
26 | ||
27 | This code produces 128 K of temporary memory for the PEI stack by opening a | |
28 | host file and mapping it directly to memory addresses. | |
29 | ||
30 | The system.cmd script is used to set host environment variables that drive | |
31 | the configuration opitons of the SEC. | |
32 | ||
33 | --*/ | |
34 | ||
35 | #include "SecMain.h" | |
36 | #include <sys/mman.h> | |
37 | #include <Ppi/UnixPeiLoadFile.h> | |
804405e7 | 38 | #include <Ppi/TemporaryRamSupport.h> |
39 | #include <dlfcn.h> | |
40 | // | |
41 | // Globals | |
42 | // | |
804405e7 | 43 | |
73aa7f04 | 44 | UNIX_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi = { SecWinNtPeiLoadFile }; |
804405e7 | 45 | |
73aa7f04 | 46 | PEI_UNIX_AUTOSCAN_PPI mSecNtAutoScanPpi = { SecWinNtPeiAutoScan }; |
804405e7 | 47 | |
73aa7f04 | 48 | PEI_UNIX_THUNK_PPI mSecWinNtThunkPpi = { SecWinNtWinNtThunkAddress }; |
804405e7 | 49 | |
73aa7f04 | 50 | EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode }; |
804405e7 | 51 | |
73aa7f04 | 52 | UNIX_FWH_PPI mSecFwhInformationPpi = { SecWinNtFdAddress }; |
804405e7 | 53 | |
54 | TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport}; | |
55 | ||
56 | EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = { | |
804405e7 | 57 | { |
58 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
59 | &gUnixPeiLoadFilePpiGuid, | |
60 | &mSecNtLoadFilePpi | |
61 | }, | |
62 | { | |
63 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
64 | &gPeiUnixAutoScanPpiGuid, | |
65 | &mSecNtAutoScanPpi | |
66 | }, | |
67 | { | |
68 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
69 | &gPeiUnixThunkPpiGuid, | |
70 | &mSecWinNtThunkPpi | |
71 | }, | |
72 | { | |
73 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
74 | &gEfiPeiStatusCodePpiGuid, | |
75 | &mSecStatusCodePpi | |
76 | }, | |
77 | { | |
78 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
79 | &gEfiTemporaryRamSupportPpiGuid, | |
80 | &mSecTemporaryRamSupportPpi | |
81 | }, | |
82 | { | |
83 | ||
84 | EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, | |
85 | &gUnixFwhPpiGuid, | |
86 | &mSecFwhInformationPpi | |
87 | } | |
88 | }; | |
89 | ||
90 | ||
91 | // | |
92 | // Default information about where the FD is located. | |
93 | // This array gets filled in with information from EFI_FIRMWARE_VOLUMES | |
94 | // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd. | |
95 | // The number of array elements is allocated base on parsing | |
96 | // EFI_FIRMWARE_VOLUMES and the memory is never freed. | |
97 | // | |
98 | UINTN gFdInfoCount = 0; | |
99 | UNIX_FD_INFO *gFdInfo; | |
100 | ||
101 | // | |
102 | // Array that supports seperate memory rantes. | |
103 | // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable. | |
104 | // The number of array elements is allocated base on parsing | |
105 | // EFI_MEMORY_SIZE and the memory is never freed. | |
106 | // | |
107 | UINTN gSystemMemoryCount = 0; | |
108 | UNIX_SYSTEM_MEMORY *gSystemMemory; | |
109 | ||
110 | VOID | |
111 | EFIAPI | |
112 | SecSwitchStack ( | |
113 | UINT32 TemporaryMemoryBase, | |
114 | UINT32 PermenentMemoryBase | |
115 | ); | |
116 | ||
804405e7 | 117 | EFI_PHYSICAL_ADDRESS * |
118 | MapMemory ( | |
119 | INTN fd, | |
120 | UINT64 length, | |
121 | INTN prot, | |
122 | INTN flags); | |
123 | ||
804405e7 | 124 | EFI_STATUS |
125 | MapFile ( | |
126 | IN CHAR8 *FileName, | |
127 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
128 | OUT UINT64 *Length | |
129 | ); | |
398b646f | 130 | EFI_STATUS |
131 | EFIAPI | |
132 | SecNt32PeCoffRelocateImage ( | |
133 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
134 | ); | |
804405e7 | 135 | |
136 | ||
137 | INTN | |
138 | EFIAPI | |
139 | main ( | |
140 | IN INTN Argc, | |
141 | IN CHAR8 **Argv, | |
142 | IN CHAR8 **Envp | |
143 | ) | |
144 | /*++ | |
145 | ||
146 | Routine Description: | |
147 | Main entry point to SEC for WinNt. This is a unix program | |
148 | ||
149 | Arguments: | |
150 | Argc - Number of command line arguments | |
151 | Argv - Array of command line argument strings | |
152 | Envp - Array of environmemt variable strings | |
153 | ||
154 | Returns: | |
155 | 0 - Normal exit | |
156 | 1 - Abnormal exit | |
157 | ||
158 | --*/ | |
159 | { | |
160 | EFI_STATUS Status; | |
161 | EFI_PHYSICAL_ADDRESS InitialStackMemory; | |
162 | UINT64 InitialStackMemorySize; | |
163 | UINTN Index; | |
164 | UINTN Index1; | |
165 | UINTN Index2; | |
166 | UINTN PeiIndex; | |
167 | CHAR8 *FileName; | |
168 | BOOLEAN Done; | |
169 | VOID *PeiCoreFile; | |
170 | CHAR16 *MemorySizeStr; | |
171 | CHAR16 *FirmwareVolumesStr; | |
172 | UINTN *StackPointer; | |
173 | ||
174 | setbuf(stdout, 0); | |
175 | setbuf(stderr, 0); | |
176 | ||
177 | MemorySizeStr = (CHAR16 *) FixedPcdGetPtr (PcdUnixMemorySizeForSecMain); | |
178 | FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdUnixFirmwareVolume); | |
179 | ||
180 | printf ("\nEDK SEC Main UNIX Emulation Environment from www.TianoCore.org\n"); | |
181 | ||
182 | // | |
183 | // Allocate space for gSystemMemory Array | |
184 | // | |
185 | gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1; | |
186 | gSystemMemory = calloc (gSystemMemoryCount, sizeof (UNIX_SYSTEM_MEMORY)); | |
187 | if (gSystemMemory == NULL) { | |
188 | printf ("ERROR : Can not allocate memory for system. Exiting.\n"); | |
189 | exit (1); | |
190 | } | |
191 | // | |
192 | // Allocate space for gSystemMemory Array | |
193 | // | |
194 | gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1; | |
195 | gFdInfo = calloc (gFdInfoCount, sizeof (UNIX_FD_INFO)); | |
196 | if (gFdInfo == NULL) { | |
197 | printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); | |
198 | exit (1); | |
199 | } | |
200 | // | |
201 | // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION) | |
202 | // | |
203 | printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdUnixBootMode)); | |
204 | ||
205 | // | |
206 | // Open up a 128K file to emulate temp memory for PEI. | |
207 | // on a real platform this would be SRAM, or using the cache as RAM. | |
208 | // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping | |
209 | // | |
210 | InitialStackMemorySize = STACK_SIZE; | |
211 | InitialStackMemory = (UINTN)MapMemory(0, | |
212 | (UINT32) InitialStackMemorySize, | |
213 | PROT_READ | PROT_WRITE, | |
214 | MAP_ANONYMOUS | MAP_PRIVATE); | |
215 | if (InitialStackMemory == 0) { | |
216 | printf ("ERROR : Can not open SecStack Exiting\n"); | |
217 | exit (1); | |
218 | } | |
219 | ||
220 | printf (" SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n", | |
221 | (UINTN)(InitialStackMemorySize / 1024), | |
222 | (unsigned long)InitialStackMemory); | |
223 | ||
224 | for (StackPointer = (UINTN*) (UINTN) InitialStackMemory; | |
225 | StackPointer < (UINTN*) ((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize); | |
226 | StackPointer ++) { | |
227 | *StackPointer = 0x5AA55AA5; | |
228 | } | |
229 | ||
230 | // | |
231 | // Open All the firmware volumes and remember the info in the gFdInfo global | |
232 | // | |
233 | FileName = (CHAR8 *)malloc (StrLen (FirmwareVolumesStr) + 1); | |
234 | if (FileName == NULL) { | |
235 | printf ("ERROR : Can not allocate memory for firmware volume string\n"); | |
236 | exit (1); | |
237 | } | |
238 | ||
239 | Index2 = 0; | |
240 | for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL; | |
241 | FirmwareVolumesStr[Index2] != 0; | |
242 | Index++) { | |
243 | for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++) | |
244 | FileName[Index1++] = FirmwareVolumesStr[Index2]; | |
245 | if (FirmwareVolumesStr[Index2] == '!') | |
246 | Index2++; | |
247 | FileName[Index1] = '\0'; | |
248 | ||
249 | // | |
250 | // Open the FD and remmeber where it got mapped into our processes address space | |
251 | // | |
252 | Status = MapFile ( | |
253 | FileName, | |
254 | &gFdInfo[Index].Address, | |
255 | &gFdInfo[Index].Size | |
256 | ); | |
257 | if (EFI_ERROR (Status)) { | |
258 | printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, Status); | |
259 | exit (1); | |
260 | } | |
261 | ||
262 | printf (" FD loaded from %s at 0x%08lx", | |
263 | FileName, (unsigned long)gFdInfo[Index].Address); | |
264 | ||
265 | if (PeiCoreFile == NULL) { | |
266 | // | |
267 | // Assume the beginning of the FD is an FV and look for the PEI Core. | |
268 | // Load the first one we find. | |
269 | // | |
270 | Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile); | |
271 | if (!EFI_ERROR (Status)) { | |
272 | PeiIndex = Index; | |
273 | printf (" contains SEC Core"); | |
274 | } | |
275 | } | |
276 | ||
277 | printf ("\n"); | |
278 | } | |
279 | // | |
280 | // Calculate memory regions and store the information in the gSystemMemory | |
281 | // global for later use. The autosizing code will use this data to | |
282 | // map this memory into the SEC process memory space. | |
283 | // | |
284 | Index1 = 0; | |
285 | Index = 0; | |
286 | while (1) { | |
287 | UINTN val = 0; | |
288 | // | |
289 | // Save the size of the memory. | |
290 | // | |
291 | while (MemorySizeStr[Index1] >= '0' && MemorySizeStr[Index1] <= '9') { | |
292 | val = val * 10 + MemorySizeStr[Index1] - '0'; | |
293 | Index1++; | |
294 | } | |
295 | gSystemMemory[Index++].Size = val * 0x100000; | |
296 | if (MemorySizeStr[Index1] == 0) | |
297 | break; | |
298 | Index1++; | |
299 | } | |
300 | ||
301 | printf ("\n"); | |
302 | ||
303 | // | |
304 | // Hand off to PEI Core | |
305 | // | |
306 | SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile); | |
307 | ||
308 | // | |
309 | // If we get here, then the PEI Core returned. This is an error as PEI should | |
310 | // always hand off to DXE. | |
311 | // | |
312 | printf ("ERROR : PEI Core returned\n"); | |
313 | exit (1); | |
314 | } | |
315 | ||
316 | EFI_PHYSICAL_ADDRESS * | |
317 | MapMemory ( | |
318 | INTN fd, | |
319 | UINT64 length, | |
320 | INTN prot, | |
321 | INTN flags) | |
322 | { | |
323 | STATIC UINTN base = 0x40000000; | |
324 | CONST UINTN align = (1 << 24); | |
325 | VOID *res = NULL; | |
326 | BOOLEAN isAligned = 0; | |
327 | ||
328 | // | |
329 | // Try to get an aligned block somewhere in the address space of this | |
330 | // process. | |
331 | // | |
332 | while((!isAligned) && (base != 0)) { | |
333 | res = mmap ((void *)base, length, prot, flags, fd, 0); | |
334 | if (res == MAP_FAILED) { | |
335 | return NULL; | |
336 | } | |
337 | if ((((UINTN)res) & ~(align-1)) == (UINTN)res) { | |
338 | isAligned=1; | |
339 | } | |
340 | else { | |
341 | munmap(res, length); | |
342 | base += align; | |
343 | } | |
344 | } | |
345 | return res; | |
346 | } | |
347 | ||
348 | EFI_STATUS | |
349 | MapFile ( | |
350 | IN CHAR8 *FileName, | |
351 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
352 | OUT UINT64 *Length | |
353 | ) | |
354 | /*++ | |
355 | ||
356 | Routine Description: | |
357 | Opens and memory maps a file using WinNt services. If BaseAddress is non zero | |
358 | the process will try and allocate the memory starting at BaseAddress. | |
359 | ||
360 | Arguments: | |
361 | FileName - The name of the file to open and map | |
362 | MapSize - The amount of the file to map in bytes | |
363 | CreationDisposition - The flags to pass to CreateFile(). Use to create new files for | |
364 | memory emulation, and exiting files for firmware volume emulation | |
365 | BaseAddress - The base address of the mapped file in the user address space. | |
366 | If passed in as NULL the a new memory region is used. | |
367 | If passed in as non NULL the request memory region is used for | |
368 | the mapping of the file into the process space. | |
369 | Length - The size of the mapped region in bytes | |
370 | ||
371 | Returns: | |
372 | EFI_SUCCESS - The file was opened and mapped. | |
373 | EFI_NOT_FOUND - FileName was not found in the current directory | |
374 | EFI_DEVICE_ERROR - An error occured attempting to map the opened file | |
375 | ||
376 | --*/ | |
377 | { | |
378 | int fd; | |
379 | VOID *res; | |
380 | UINTN FileSize; | |
381 | ||
382 | fd = open (FileName, O_RDONLY); | |
383 | if (fd < 0) | |
384 | return EFI_NOT_FOUND; | |
385 | FileSize = lseek (fd, 0, SEEK_END); | |
386 | ||
387 | #if 0 | |
388 | if (IsMain) | |
389 | { | |
390 | /* Read entry address. */ | |
391 | lseek (fd, FileSize - 0x20, SEEK_SET); | |
392 | if (read (fd, &EntryAddress, 4) != 4) | |
393 | { | |
394 | close (fd); | |
395 | return EFI_DEVICE_ERROR; | |
396 | } | |
397 | } | |
398 | #endif | |
399 | ||
400 | res = MapMemory(fd, FileSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE); | |
401 | ||
402 | close (fd); | |
403 | ||
404 | if (res == MAP_FAILED) | |
405 | return EFI_DEVICE_ERROR; | |
406 | ||
407 | *Length = (UINT64) FileSize; | |
408 | *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res; | |
409 | ||
410 | return EFI_SUCCESS; | |
411 | } | |
412 | ||
413 | #define BYTES_PER_RECORD 512 | |
414 | ||
415 | EFI_STATUS | |
416 | EFIAPI | |
417 | SecPeiReportStatusCode ( | |
418 | IN EFI_PEI_SERVICES **PeiServices, | |
419 | IN EFI_STATUS_CODE_TYPE CodeType, | |
420 | IN EFI_STATUS_CODE_VALUE Value, | |
421 | IN UINT32 Instance, | |
422 | IN EFI_GUID * CallerId, | |
423 | IN EFI_STATUS_CODE_DATA * Data OPTIONAL | |
424 | ) | |
425 | /*++ | |
426 | ||
427 | Routine Description: | |
428 | ||
429 | This routine produces the ReportStatusCode PEI service. It's passed | |
430 | up to the PEI Core via a PPI. T | |
431 | ||
432 | This code currently uses the UNIX clib printf. This does not work the same way | |
433 | as the EFI Print (), as %t, %g, %s as Unicode are not supported. | |
434 | ||
435 | Arguments: | |
436 | (see EFI_PEI_REPORT_STATUS_CODE) | |
437 | ||
438 | Returns: | |
439 | EFI_SUCCESS - Always return success | |
440 | ||
441 | --*/ | |
442 | // TODO: PeiServices - add argument and description to function comment | |
443 | // TODO: CodeType - add argument and description to function comment | |
444 | // TODO: Value - add argument and description to function comment | |
445 | // TODO: Instance - add argument and description to function comment | |
446 | // TODO: CallerId - add argument and description to function comment | |
447 | // TODO: Data - add argument and description to function comment | |
448 | { | |
449 | CHAR8 *Format; | |
9c98c8e1 | 450 | BASE_LIST Marker; |
804405e7 | 451 | CHAR8 PrintBuffer[BYTES_PER_RECORD * 2]; |
452 | CHAR8 *Filename; | |
453 | CHAR8 *Description; | |
454 | UINT32 LineNumber; | |
455 | UINT32 ErrorLevel; | |
456 | ||
457 | ||
458 | if (Data == NULL) { | |
459 | } else if (ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) { | |
460 | // | |
461 | // Processes ASSERT () | |
462 | // | |
463 | printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description); | |
464 | ||
465 | } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) { | |
466 | // | |
467 | // Process DEBUG () macro | |
468 | // | |
9c98c8e1 | 469 | AsciiBSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker); |
804405e7 | 470 | printf (PrintBuffer); |
471 | } | |
472 | ||
473 | return EFI_SUCCESS; | |
474 | } | |
475 | ||
476 | /** | |
477 | Transfers control to a function starting with a new stack. | |
478 | ||
479 | Transfers control to the function specified by EntryPoint using the new stack | |
480 | specified by NewStack and passing in the parameters specified by Context1 and | |
481 | Context2. Context1 and Context2 are optional and may be NULL. The function | |
482 | EntryPoint must never return. | |
483 | ||
484 | If EntryPoint is NULL, then ASSERT(). | |
485 | If NewStack is NULL, then ASSERT(). | |
486 | ||
487 | @param EntryPoint A pointer to function to call with the new stack. | |
488 | @param Context1 A pointer to the context to pass into the EntryPoint | |
489 | function. | |
490 | @param Context2 A pointer to the context to pass into the EntryPoint | |
491 | function. | |
492 | @param NewStack A pointer to the new stack to use for the EntryPoint | |
493 | function. | |
494 | @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's | |
495 | Reserved on other architectures. | |
496 | ||
497 | **/ | |
498 | VOID | |
499 | EFIAPI | |
500 | PeiSwitchStacks ( | |
501 | IN SWITCH_STACK_ENTRY_POINT EntryPoint, | |
502 | IN VOID *Context1, OPTIONAL | |
503 | IN VOID *Context2, OPTIONAL | |
504 | IN VOID *Context3, OPTIONAL | |
505 | IN VOID *NewStack | |
506 | ) | |
507 | { | |
508 | BASE_LIBRARY_JUMP_BUFFER JumpBuffer; | |
509 | ||
510 | ASSERT (EntryPoint != NULL); | |
511 | ASSERT (NewStack != NULL); | |
512 | ||
513 | // | |
514 | // Stack should be aligned with CPU_STACK_ALIGNMENT | |
515 | // | |
516 | ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0); | |
517 | ||
518 | JumpBuffer.Eip = (UINTN)EntryPoint; | |
519 | JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*); | |
520 | JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3); | |
521 | ((VOID**)JumpBuffer.Esp)[1] = Context1; | |
522 | ((VOID**)JumpBuffer.Esp)[2] = Context2; | |
523 | ((VOID**)JumpBuffer.Esp)[3] = Context3; | |
524 | ||
525 | LongJump (&JumpBuffer, (UINTN)-1); | |
526 | ||
527 | ||
528 | // | |
529 | // InternalSwitchStack () will never return | |
530 | // | |
531 | ASSERT (FALSE); | |
532 | } | |
533 | ||
534 | VOID | |
535 | SecLoadFromCore ( | |
536 | IN UINTN LargestRegion, | |
537 | IN UINTN LargestRegionSize, | |
538 | IN UINTN BootFirmwareVolumeBase, | |
539 | IN VOID *PeiCorePe32File | |
540 | ) | |
541 | /*++ | |
542 | ||
543 | Routine Description: | |
544 | This is the service to load the PEI Core from the Firmware Volume | |
545 | ||
546 | Arguments: | |
547 | LargestRegion - Memory to use for PEI. | |
548 | LargestRegionSize - Size of Memory to use for PEI | |
549 | BootFirmwareVolumeBase - Start of the Boot FV | |
550 | PeiCorePe32File - PEI Core PE32 | |
551 | ||
552 | Returns: | |
553 | Success means control is transfered and thus we should never return | |
554 | ||
555 | --*/ | |
556 | { | |
557 | EFI_STATUS Status; | |
558 | EFI_PHYSICAL_ADDRESS TopOfMemory; | |
559 | VOID *TopOfStack; | |
560 | UINT64 PeiCoreSize; | |
561 | EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint; | |
562 | EFI_PHYSICAL_ADDRESS PeiImageAddress; | |
563 | EFI_SEC_PEI_HAND_OFF *SecCoreData; | |
564 | UINTN PeiStackSize; | |
565 | ||
566 | // | |
567 | // Compute Top Of Memory for Stack and PEI Core Allocations | |
568 | // | |
569 | TopOfMemory = LargestRegion + LargestRegionSize; | |
570 | PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1); | |
571 | ||
572 | // | |
573 | // |-----------| <---- TemporaryRamBase + TemporaryRamSize | |
574 | // | Heap | | |
575 | // | | | |
576 | // |-----------| <---- StackBase / PeiTemporaryMemoryBase | |
577 | // | | | |
578 | // | Stack | | |
579 | // |-----------| <---- TemporaryRamBase | |
580 | // | |
581 | TopOfStack = (VOID *)(LargestRegion + PeiStackSize); | |
582 | TopOfMemory = LargestRegion + PeiStackSize; | |
583 | ||
584 | // | |
585 | // Reservet space for storing PeiCore's parament in stack. | |
586 | // | |
587 | TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT); | |
588 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); | |
589 | ||
398b646f | 590 | |
804405e7 | 591 | // |
592 | // Bind this information into the SEC hand-off state | |
593 | // | |
594 | SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack; | |
595 | SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF); | |
596 | SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase; | |
597 | SecCoreData->BootFirmwareVolumeSize = FixedPcdGet32(PcdUnixFirmwareFdSize); | |
598 | SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; | |
599 | SecCoreData->TemporaryRamSize = STACK_SIZE; | |
600 | SecCoreData->StackBase = SecCoreData->TemporaryRamBase; | |
601 | SecCoreData->StackSize = PeiStackSize; | |
602 | SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize); | |
603 | SecCoreData->PeiTemporaryRamSize = STACK_SIZE - PeiStackSize; | |
604 | ||
605 | // | |
606 | // Load the PEI Core from a Firmware Volume | |
607 | // | |
608 | Status = SecWinNtPeiLoadFile ( | |
609 | PeiCorePe32File, | |
610 | &PeiImageAddress, | |
611 | &PeiCoreSize, | |
612 | &PeiCoreEntryPoint | |
613 | ); | |
614 | if (EFI_ERROR (Status)) { | |
615 | return ; | |
616 | } | |
617 | ||
618 | // | |
619 | // Transfer control to the PEI Core | |
620 | // | |
621 | PeiSwitchStacks ( | |
622 | (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint, | |
623 | SecCoreData, | |
624 | (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable), | |
625 | NULL, | |
626 | TopOfStack | |
627 | ); | |
628 | // | |
629 | // If we get here, then the PEI Core returned. This is an error | |
630 | // | |
631 | return ; | |
632 | } | |
633 | ||
634 | EFI_STATUS | |
635 | EFIAPI | |
636 | SecWinNtPeiAutoScan ( | |
637 | IN UINTN Index, | |
638 | OUT EFI_PHYSICAL_ADDRESS *MemoryBase, | |
639 | OUT UINT64 *MemorySize | |
640 | ) | |
641 | /*++ | |
642 | ||
643 | Routine Description: | |
644 | This service is called from Index == 0 until it returns EFI_UNSUPPORTED. | |
645 | It allows discontiguous memory regions to be supported by the emulator. | |
646 | It uses gSystemMemory[] and gSystemMemoryCount that were created by | |
647 | parsing the host environment variable EFI_MEMORY_SIZE. | |
648 | The size comes from the varaible and the address comes from the call to | |
649 | WinNtOpenFile. | |
650 | ||
651 | Arguments: | |
652 | Index - Which memory region to use | |
653 | MemoryBase - Return Base address of memory region | |
654 | MemorySize - Return size in bytes of the memory region | |
655 | ||
656 | Returns: | |
657 | EFI_SUCCESS - If memory region was mapped | |
658 | EFI_UNSUPPORTED - If Index is not supported | |
659 | ||
660 | --*/ | |
661 | { | |
662 | void *res; | |
663 | ||
664 | if (Index >= gSystemMemoryCount) { | |
665 | return EFI_UNSUPPORTED; | |
666 | } | |
667 | ||
668 | *MemoryBase = 0; | |
669 | res = MapMemory(0, gSystemMemory[Index].Size, | |
670 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
671 | MAP_PRIVATE | MAP_ANONYMOUS); | |
672 | if (res == MAP_FAILED) | |
673 | return EFI_DEVICE_ERROR; | |
674 | *MemorySize = gSystemMemory[Index].Size; | |
675 | *MemoryBase = (UINTN)res; | |
676 | gSystemMemory[Index].Memory = *MemoryBase; | |
677 | ||
678 | return EFI_SUCCESS; | |
679 | } | |
680 | ||
681 | VOID * | |
682 | EFIAPI | |
683 | SecWinNtWinNtThunkAddress ( | |
684 | VOID | |
685 | ) | |
686 | /*++ | |
687 | ||
688 | Routine Description: | |
689 | Since the SEC is the only Unix program in stack it must export | |
690 | an interface to do Win API calls. That's what the WinNtThunk address | |
691 | is for. gWinNt is initailized in WinNtThunk.c. | |
692 | ||
693 | Arguments: | |
694 | InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL); | |
695 | InterfaceBase - Address of the gWinNt global | |
696 | ||
697 | Returns: | |
698 | EFI_SUCCESS - Data returned | |
699 | ||
700 | --*/ | |
701 | { | |
702 | return gUnix; | |
703 | } | |
704 | ||
705 | ||
706 | EFI_STATUS | |
707 | EFIAPI | |
708 | SecWinNtPeiLoadFile ( | |
709 | IN VOID *Pe32Data, | |
710 | IN EFI_PHYSICAL_ADDRESS *ImageAddress, | |
711 | IN UINT64 *ImageSize, | |
712 | IN EFI_PHYSICAL_ADDRESS *EntryPoint | |
713 | ) | |
714 | /*++ | |
715 | ||
716 | Routine Description: | |
717 | Loads and relocates a PE/COFF image into memory. | |
718 | ||
719 | Arguments: | |
720 | Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated | |
721 | ImageAddress - The base address of the relocated PE/COFF image | |
722 | ImageSize - The size of the relocated PE/COFF image | |
723 | EntryPoint - The entry point of the relocated PE/COFF image | |
724 | ||
725 | Returns: | |
726 | EFI_SUCCESS - The file was loaded and relocated | |
727 | EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file | |
728 | ||
729 | --*/ | |
730 | { | |
731 | EFI_STATUS Status; | |
732 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; | |
733 | ||
734 | ZeroMem (&ImageContext, sizeof (ImageContext)); | |
735 | ImageContext.Handle = Pe32Data; | |
736 | ||
737 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead; | |
738 | ||
398b646f | 739 | Status = PeCoffLoaderGetImageInfo (&ImageContext); |
804405e7 | 740 | if (EFI_ERROR (Status)) { |
741 | return Status; | |
742 | } | |
743 | // | |
744 | // Allocate space in UNIX (not emulator) memory. Extra space is for alignment | |
745 | // | |
746 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2))); | |
747 | if (ImageContext.ImageAddress == 0) { | |
748 | return EFI_OUT_OF_RESOURCES; | |
749 | } | |
750 | // | |
751 | // Align buffer on section boundry | |
752 | // | |
f7bef78c | 753 | ImageContext.ImageAddress += ImageContext.SectionAlignment - 1; |
804405e7 | 754 | ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1); |
755 | ||
756 | ||
398b646f | 757 | Status = PeCoffLoaderLoadImage (&ImageContext); |
804405e7 | 758 | if (EFI_ERROR (Status)) { |
759 | return Status; | |
760 | } | |
761 | ||
398b646f | 762 | Status = SecNt32PeCoffRelocateImage(&ImageContext); |
804405e7 | 763 | if (EFI_ERROR (Status)) { |
764 | return Status; | |
765 | } | |
766 | ||
767 | // | |
768 | // BugBug: Flush Instruction Cache Here when CPU Lib is ready | |
769 | // | |
770 | ||
771 | *ImageAddress = ImageContext.ImageAddress; | |
772 | *ImageSize = ImageContext.ImageSize; | |
773 | *EntryPoint = ImageContext.EntryPoint; | |
774 | ||
775 | return EFI_SUCCESS; | |
776 | } | |
777 | ||
778 | EFI_STATUS | |
779 | EFIAPI | |
780 | SecWinNtFdAddress ( | |
781 | IN UINTN Index, | |
782 | IN OUT EFI_PHYSICAL_ADDRESS *FdBase, | |
783 | IN OUT UINT64 *FdSize | |
784 | ) | |
785 | /*++ | |
786 | ||
787 | Routine Description: | |
788 | Return the FD Size and base address. Since the FD is loaded from a | |
789 | file into host memory only the SEC will know it's address. | |
790 | ||
791 | Arguments: | |
792 | Index - Which FD, starts at zero. | |
793 | FdSize - Size of the FD in bytes | |
794 | FdBase - Start address of the FD. Assume it points to an FV Header | |
795 | ||
796 | Returns: | |
797 | EFI_SUCCESS - Return the Base address and size of the FV | |
798 | EFI_UNSUPPORTED - Index does nto map to an FD in the system | |
799 | ||
800 | --*/ | |
801 | { | |
802 | if (Index >= gFdInfoCount) { | |
803 | return EFI_UNSUPPORTED; | |
804 | } | |
805 | ||
806 | *FdBase = gFdInfo[Index].Address; | |
807 | *FdSize = gFdInfo[Index].Size; | |
808 | ||
809 | if (*FdBase == 0 && *FdSize == 0) { | |
810 | return EFI_UNSUPPORTED; | |
811 | } | |
812 | ||
813 | return EFI_SUCCESS; | |
814 | } | |
815 | ||
816 | EFI_STATUS | |
817 | EFIAPI | |
818 | SecImageRead ( | |
819 | IN VOID *FileHandle, | |
820 | IN UINTN FileOffset, | |
821 | IN OUT UINTN *ReadSize, | |
822 | OUT VOID *Buffer | |
823 | ) | |
824 | /*++ | |
825 | ||
826 | Routine Description: | |
827 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file | |
828 | ||
829 | Arguments: | |
830 | FileHandle - The handle to the PE/COFF file | |
831 | FileOffset - The offset, in bytes, into the file to read | |
832 | ReadSize - The number of bytes to read from the file starting at FileOffset | |
833 | Buffer - A pointer to the buffer to read the data into. | |
834 | ||
835 | Returns: | |
836 | EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset | |
837 | ||
838 | --*/ | |
839 | { | |
840 | CHAR8 *Destination8; | |
841 | CHAR8 *Source8; | |
842 | UINTN Length; | |
843 | ||
844 | Destination8 = Buffer; | |
845 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset); | |
846 | Length = *ReadSize; | |
847 | while (Length--) { | |
848 | *(Destination8++) = *(Source8++); | |
849 | } | |
850 | ||
851 | return EFI_SUCCESS; | |
852 | } | |
853 | ||
854 | UINTN | |
855 | CountSeperatorsInString ( | |
856 | IN const CHAR16 *String, | |
857 | IN CHAR16 Seperator | |
858 | ) | |
859 | /*++ | |
860 | ||
861 | Routine Description: | |
862 | Count the number of seperators in String | |
863 | ||
864 | Arguments: | |
865 | String - String to process | |
866 | Seperator - Item to count | |
867 | ||
868 | Returns: | |
869 | Number of Seperator in String | |
870 | ||
871 | --*/ | |
872 | { | |
873 | UINTN Count; | |
874 | ||
875 | for (Count = 0; *String != '\0'; String++) { | |
876 | if (*String == Seperator) { | |
877 | Count++; | |
878 | } | |
879 | } | |
880 | ||
881 | return Count; | |
882 | } | |
883 | ||
884 | ||
885 | ||
804405e7 | 886 | VOID |
887 | SecUnixLoaderBreak ( | |
888 | VOID | |
889 | ) | |
890 | { | |
891 | } | |
892 | ||
893 | EFI_STATUS | |
894 | EFIAPI | |
895 | SecNt32PeCoffRelocateImage ( | |
804405e7 | 896 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext |
897 | ) | |
898 | { | |
899 | void * Handle; | |
900 | void * Entry; | |
901 | EFI_STATUS Status; | |
902 | ||
903 | Handle = NULL; | |
904 | Entry = NULL; | |
905 | Status = PeCoffLoaderRelocateImage (ImageContext); | |
906 | fprintf (stderr, | |
907 | "Loading %s 0x%08lx - entry point 0x%08lx\n", | |
908 | ImageContext->PdbPointer, | |
909 | (unsigned long)ImageContext->ImageAddress, | |
910 | (unsigned long)ImageContext->EntryPoint); | |
911 | ||
865c7e17 | 912 | Handle = dlopen(ImageContext->PdbPointer, RTLD_NOW); |
913 | ||
914 | if (Handle) { | |
915 | Entry = dlsym(Handle, "_ModuleEntryPoint"); | |
916 | } else { | |
917 | printf("%s\n", dlerror()); | |
918 | } | |
919 | ||
920 | if (Entry != NULL) { | |
921 | ImageContext->EntryPoint = Entry; | |
922 | printf("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, Entry); | |
865c7e17 | 923 | } |
924 | ||
804405e7 | 925 | SecUnixLoaderBreak (); |
926 | ||
927 | return Status; | |
928 | } | |
929 | ||
930 | ||
931 | EFI_STATUS | |
932 | EFIAPI | |
933 | SecNt32PeCoffUnloadimage ( | |
804405e7 | 934 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext |
935 | ) | |
936 | { | |
937 | return EFI_SUCCESS; | |
938 | } | |
939 | ||
940 | VOID | |
941 | ModuleEntryPoint ( | |
942 | VOID | |
943 | ) | |
944 | { | |
945 | } | |
946 | ||
947 | EFI_STATUS | |
948 | EFIAPI | |
949 | SecTemporaryRamSupport ( | |
950 | IN CONST EFI_PEI_SERVICES **PeiServices, | |
951 | IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase, | |
952 | IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase, | |
953 | IN UINTN CopySize | |
954 | ) | |
955 | { | |
956 | // | |
957 | // Migrate the whole temporary memory to permenent memory. | |
958 | // | |
959 | CopyMem ( | |
960 | (VOID*)(UINTN)PermanentMemoryBase, | |
961 | (VOID*)(UINTN)TemporaryMemoryBase, | |
962 | CopySize | |
963 | ); | |
964 | ||
965 | // | |
966 | // SecSwitchStack function must be invoked after the memory migration | |
967 | // immediatly, also we need fixup the stack change caused by new call into | |
968 | // permenent memory. | |
969 | // | |
970 | SecSwitchStack ( | |
971 | (UINT32) TemporaryMemoryBase, | |
972 | (UINT32) PermanentMemoryBase | |
973 | ); | |
974 | ||
975 | // | |
976 | // We need *not* fix the return address because currently, | |
977 | // The PeiCore is excuted in flash. | |
978 | // | |
979 | ||
980 | // | |
1fd8c31a | 981 | // Simulate to invalid temporary memory, terminate temporary memory |
804405e7 | 982 | // |
983 | //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize); | |
984 | ||
985 | return EFI_SUCCESS; | |
986 | } |