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