]> git.proxmox.com Git - mirror_edk2.git/blame - EdkUnixPkg/Sec/SecMain.c
Unix version of EFI emulator
[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
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
20 This program processes Windows environment variables and figures out\r
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
28 Windows file and mapping it directly to memory addresses.\r
29\r
30 The system.cmd script is used to set windows environment variables that drive\r
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
106// EFI_FIRMWARE_VOLUMES is a Windows environment variable set by system.cmd.\r
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
150 Main entry point to SEC for WinNt. This is a Windows program\r
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
179 printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");\r
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{
316 static UINTN base = 0x40000000;
317 const UINTN align = (1 << 24);
318 void *res;
319
320 res = mmap ((void *)base, length, prot, flags, fd, 0);
321 if (res == MAP_FAILED)
322 return NULL;
323
324 // Guard page.
325 base += length + 4096;
326 base = (base + align - 1) & ~(align - 1);
327
328 return res;
329}
330
331EFI_STATUS
332MapFile (
333 IN CHAR8 *FileName,\r
334 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
335 OUT UINT64 *Length\r
336 )
337/*++
338
339Routine Description:
340 Opens and memory maps a file using WinNt services. If BaseAddress is non zero
341 the process will try and allocate the memory starting at BaseAddress.
342
343Arguments:
344 FileName - The name of the file to open and map
345 MapSize - The amount of the file to map in bytes
346 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
347 memory emulation, and exiting files for firmware volume emulation
348 BaseAddress - The base address of the mapped file in the user address space.
349 If passed in as NULL the a new memory region is used.
350 If passed in as non NULL the request memory region is used for
351 the mapping of the file into the process space.
352 Length - The size of the mapped region in bytes
353
354Returns:
355 EFI_SUCCESS - The file was opened and mapped.
356 EFI_NOT_FOUND - FileName was not found in the current directory
357 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
358
359--*/
360{
361 int fd;
362 VOID *res;
363 UINTN FileSize;
364
365 fd = open (FileName, O_RDONLY);
366 if (fd < 0)
367 return EFI_NOT_FOUND;
368 FileSize = lseek (fd, 0, SEEK_END);
369
370#if 0
371 if (IsMain)
372 {
373 /* Read entry address. */
374 lseek (fd, FileSize - 0x20, SEEK_SET);
375 if (read (fd, &EntryAddress, 4) != 4)
376 {
377 close (fd);
378 return EFI_DEVICE_ERROR;
379 }
380 }
381#endif
382
383 res = MapMemory(fd, FileSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
384
385 close (fd);
386
387 if (res == MAP_FAILED)
388 return EFI_DEVICE_ERROR;
389
390 *Length = (UINT64) FileSize;
391 *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res;
392
393 return EFI_SUCCESS;
394}
395\r
396#define BYTES_PER_RECORD 512\r
397\r
398/**\r
399 Extracts ASSERT() information from a status code structure.\r
400\r
401 Converts the status code specified by CodeType, Value, and Data to the ASSERT()\r
402 arguments specified by Filename, Description, and LineNumber. If CodeType is \r
403 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and \r
404 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract \r
405 Filename, Description, and LineNumber from the optional data area of the \r
406 status code buffer specified by Data. The optional data area of Data contains \r
407 a Null-terminated ASCII string for the FileName, followed by a Null-terminated \r
408 ASCII string for the Description, followed by a 32-bit LineNumber. If the \r
409 ASSERT() information could be extracted from Data, then return TRUE. \r
410 Otherwise, FALSE is returned. \r
411\r
412 If Data is NULL, then ASSERT().\r
413 If Filename is NULL, then ASSERT().\r
414 If Description is NULL, then ASSERT().\r
415 If LineNumber is NULL, then ASSERT().\r
416\r
417 @param CodeType The type of status code being converted.\r
418 @param Value The status code value being converted.\r
419 @param Data Pointer to status code data buffer. \r
420 @param Filename Pointer to the source file name that generated the ASSERT().\r
421 @param Description Pointer to the description of the ASSERT().\r
422 @param LineNumber Pointer to source line number that generated the ASSERT().\r
423\r
424 @retval TRUE The status code specified by CodeType, Value, and Data was \r
425 converted ASSERT() arguments specified by Filename, Description, \r
426 and LineNumber.\r
427 @retval FALSE The status code specified by CodeType, Value, and Data could \r
428 not be converted to ASSERT() arguments.\r
429\r
430**/\r
431STATIC\r
432BOOLEAN\r
433ReportStatusCodeExtractAssertInfo (\r
434 IN EFI_STATUS_CODE_TYPE CodeType,\r
435 IN EFI_STATUS_CODE_VALUE Value, \r
436 IN CONST EFI_STATUS_CODE_DATA *Data, \r
437 OUT CHAR8 **Filename,\r
438 OUT CHAR8 **Description,\r
439 OUT UINT32 *LineNumber\r
440 )\r
441{\r
442 EFI_DEBUG_ASSERT_DATA *AssertData;\r
443\r
444 ASSERT (Data != NULL);\r
445 ASSERT (Filename != NULL);\r
446 ASSERT (Description != NULL);\r
447 ASSERT (LineNumber != NULL);\r
448\r
449 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) && \r
450 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&\r
451 ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {\r
452 AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);\r
453 *Filename = (CHAR8 *)(AssertData + 1);\r
454 *Description = *Filename + AsciiStrLen (*Filename) + 1;\r
455 *LineNumber = AssertData->LineNumber;\r
456 return TRUE;\r
457 }\r
458 return FALSE;\r
459}\r
460\r
461EFI_STATUS\r
462EFIAPI\r
463SecPeiReportStatusCode (\r
464 IN EFI_PEI_SERVICES **PeiServices,\r
465 IN EFI_STATUS_CODE_TYPE CodeType,\r
466 IN EFI_STATUS_CODE_VALUE Value,\r
467 IN UINT32 Instance,\r
468 IN EFI_GUID * CallerId,\r
469 IN EFI_STATUS_CODE_DATA * Data OPTIONAL\r
470 )\r
471/*++\r
472\r
473Routine Description:\r
474\r
475 This routine produces the ReportStatusCode PEI service. It's passed\r
476 up to the PEI Core via a PPI. T\r
477\r
478 This code currently uses the NT clib printf. This does not work the same way\r
479 as the EFI Print (), as %t, %g, %s as Unicode are not supported.\r
480\r
481Arguments:\r
482 (see EFI_PEI_REPORT_STATUS_CODE)\r
483\r
484Returns:\r
485 EFI_SUCCESS - Always return success\r
486\r
487--*/\r
488// TODO: PeiServices - add argument and description to function comment\r
489// TODO: CodeType - add argument and description to function comment\r
490// TODO: Value - add argument and description to function comment\r
491// TODO: Instance - add argument and description to function comment\r
492// TODO: CallerId - add argument and description to function comment\r
493// TODO: Data - add argument and description to function comment\r
494{\r
495 CHAR8 *Format;\r
496 EFI_DEBUG_INFO *DebugInfo;\r
497 VA_LIST Marker;\r
498 CHAR8 PrintBuffer[BYTES_PER_RECORD * 2];\r
499 CHAR8 *Filename;\r
500 CHAR8 *Description;\r
501 UINT32 LineNumber;\r
502\r
503 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {\r
504 //\r
505 // This supports DEBUG () marcos\r
506 // Data format\r
507 // EFI_STATUS_CODE_DATA\r
508 // EFI_DEBUG_INFO\r
509 //\r
510 // The first 12 * UINT64 bytes of the string are really an\r
511 // arguement stack to support varargs on the Format string.\r
512 //\r
513 if (Data != NULL) {\r
514 DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);\r
515 Marker = (VA_LIST) (DebugInfo + 1);\r
516 Format = (CHAR8 *) (((UINT64 *) Marker) + 12);\r
517\r
518 AsciiVSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r
519 printf (PrintBuffer);\r
520 } else {\r
521 printf ("DEBUG <null>\n");\r
522 }\r
523 }\r
524\r
525 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&\r
526 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED)\r
527 ) {\r
528 if (Data != NULL && ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r
529 //\r
530 // Support ASSERT () macro\r
531 //\r
532 printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description);\r
533 } else {\r
534 printf ("ASSERT <null>\n");\r
535 }\r
536 CpuBreakpoint ();\r
537 }\r
538\r
539 return EFI_SUCCESS;\r
540}\r
541\r
542\r
543VOID\r
544SecLoadFromCore (\r
545 IN UINTN LargestRegion,\r
546 IN UINTN LargestRegionSize,\r
547 IN UINTN BootFirmwareVolumeBase,\r
548 IN VOID *PeiCorePe32File\r
549 )\r
550/*++\r
551\r
552Routine Description:\r
553 This is the service to load the PEI Core from the Firmware Volume\r
554\r
555Arguments:\r
556 LargestRegion - Memory to use for PEI.\r
557 LargestRegionSize - Size of Memory to use for PEI\r
558 BootFirmwareVolumeBase - Start of the Boot FV\r
559 PeiCorePe32File - PEI Core PE32\r
560\r
561Returns:\r
562 Success means control is transfered and thus we should never return\r
563\r
564--*/\r
565{\r
566 EFI_STATUS Status;\r
567 EFI_PHYSICAL_ADDRESS TopOfMemory;\r
568 VOID *TopOfStack;\r
569 UINT64 PeiCoreSize;\r
570 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint;\r
571 EFI_PHYSICAL_ADDRESS PeiImageAddress;\r
572 EFI_PEI_STARTUP_DESCRIPTOR *PeiStartup;\r
573\r
574 //\r
575 // Compute Top Of Memory for Stack and PEI Core Allocations\r
576 //\r
577 TopOfMemory = LargestRegion + LargestRegionSize;\r
578\r
579 //\r
580 // Allocate 128KB for the Stack\r
581 //\r
582 TopOfStack = (VOID *)((UINTN)TopOfMemory - sizeof (EFI_PEI_STARTUP_DESCRIPTOR) - CPU_STACK_ALIGNMENT);\r
583 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
584 TopOfMemory = TopOfMemory - STACK_SIZE;\r
585\r
586 //\r
587 // Patch value in dispatch table values\r
588 //\r
589 gPrivateDispatchTable[0].Ppi = gPeiEfiPeiPeCoffLoader;\r
590\r
591 //\r
592 // Bind this information into the SEC hand-off state\r
593 //\r
594 PeiStartup = (EFI_PEI_STARTUP_DESCRIPTOR *) (UINTN) TopOfStack;\r
595 PeiStartup->DispatchTable = (EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable;\r
596 PeiStartup->SizeOfCacheAsRam = STACK_SIZE;\r
597 PeiStartup->BootFirmwareVolume = BootFirmwareVolumeBase;\r
598\r
599 //\r
600 // Load the PEI Core from a Firmware Volume\r
601 //\r
602 Status = SecWinNtPeiLoadFile (\r
603 PeiCorePe32File,\r
604 &PeiImageAddress,\r
605 &PeiCoreSize,\r
606 &PeiCoreEntryPoint\r
607 );\r
608 if (EFI_ERROR (Status)) {\r
609 return ;\r
610 }\r
611 printf ("Jump to 0x%08lx\n", (unsigned long)PeiCoreEntryPoint);
612 //\r
613 // Transfer control to the PEI Core\r
614 //\r
615 SwitchStack (\r
616 (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r
617 PeiStartup,\r
618 NULL,\r
619 TopOfStack\r
620 );\r
621 //\r
622 // If we get here, then the PEI Core returned. This is an error\r
623 //\r
624 return ;\r
625}\r
626\r
627EFI_STATUS\r
628EFIAPI\r
629SecWinNtPeiAutoScan (\r
630 IN UINTN Index,\r
631 OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
632 OUT UINT64 *MemorySize\r
633 )\r
634/*++\r
635\r
636Routine Description:\r
637 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
638 It allows discontiguous memory regions to be supported by the emulator.\r
639 It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
640 parsing the Windows environment variable EFI_MEMORY_SIZE.\r
641 The size comes from the varaible and the address comes from the call to\r
642 WinNtOpenFile.\r
643\r
644Arguments:\r
645 Index - Which memory region to use\r
646 MemoryBase - Return Base address of memory region\r
647 MemorySize - Return size in bytes of the memory region\r
648\r
649Returns:\r
650 EFI_SUCCESS - If memory region was mapped\r
651 EFI_UNSUPPORTED - If Index is not supported\r
652\r
653--*/\r
654{\r
655 void *res;
656\r
657 if (Index >= gSystemMemoryCount) {\r
658 return EFI_UNSUPPORTED;\r
659 }\r
660\r
661 *MemoryBase = 0;\r
662 res = MapMemory(0, gSystemMemory[Index].Size,
663 PROT_READ | PROT_WRITE | PROT_EXEC,
664 MAP_PRIVATE | MAP_ANONYMOUS);
665 if (res == MAP_FAILED)
666 return EFI_DEVICE_ERROR;
667 *MemorySize = gSystemMemory[Index].Size;
668 *MemoryBase = (UINTN)res;
669 gSystemMemory[Index].Memory = *MemoryBase;\r
670\r
671 return EFI_SUCCESS;\r
672}\r
673\r
674VOID *\r
675EFIAPI\r
676SecWinNtWinNtThunkAddress (\r
677 VOID\r
678 )\r
679/*++\r
680\r
681Routine Description:\r
682 Since the SEC is the only Windows program in stack it must export\r
683 an interface to do Win API calls. That's what the WinNtThunk address\r
684 is for. gWinNt is initailized in WinNtThunk.c.\r
685\r
686Arguments:\r
687 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
688 InterfaceBase - Address of the gWinNt global\r
689\r
690Returns:\r
691 EFI_SUCCESS - Data returned\r
692\r
693--*/\r
694{\r
695 return gUnix;\r
696}\r
697\r
698\r
699EFI_STATUS\r
700EFIAPI\r
701SecWinNtPeiLoadFile (\r
702 IN VOID *Pe32Data,\r
703 IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r
704 IN UINT64 *ImageSize,\r
705 IN EFI_PHYSICAL_ADDRESS *EntryPoint\r
706 )\r
707/*++\r
708\r
709Routine Description:\r
710 Loads and relocates a PE/COFF image into memory.\r
711\r
712Arguments:\r
713 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
714 ImageAddress - The base address of the relocated PE/COFF image\r
715 ImageSize - The size of the relocated PE/COFF image\r
716 EntryPoint - The entry point of the relocated PE/COFF image\r
717\r
718Returns:\r
719 EFI_SUCCESS - The file was loaded and relocated\r
720 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
721\r
722--*/\r
723{\r
724 EFI_STATUS Status;\r
725 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
726\r
727 ZeroMem (&ImageContext, sizeof (ImageContext));\r
728 ImageContext.Handle = Pe32Data;\r
729\r
730 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
731\r
732 Status = gPeiEfiPeiPeCoffLoader->GetImageInfo (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
733 if (EFI_ERROR (Status)) {\r
734 return Status;\r
735 }\r
736 //\r
737 // Allocate space in NT (not emulator) memory. Extra space is for alignment\r
738 //\r
739 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)));\r
740 if (ImageContext.ImageAddress == 0) {\r
741 return EFI_OUT_OF_RESOURCES;\r
742 }\r
743 //\r
744 // Align buffer on section boundry\r
745 //\r
746 ImageContext.ImageAddress += ImageContext.SectionAlignment;\r
747 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
748
749\r
750 Status = gPeiEfiPeiPeCoffLoader->LoadImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
751 if (EFI_ERROR (Status)) {\r
752 return Status;\r
753 }\r
754\r
755 Status = gPeiEfiPeiPeCoffLoader->RelocateImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
756 if (EFI_ERROR (Status)) {\r
757 return Status;\r
758 }\r
759\r
760 //\r
761 // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r
762 //\r
763\r
764 *ImageAddress = ImageContext.ImageAddress;\r
765 *ImageSize = ImageContext.ImageSize;\r
766 *EntryPoint = ImageContext.EntryPoint;\r
767\r
768 return EFI_SUCCESS;\r
769}\r
770\r
771EFI_STATUS\r
772EFIAPI\r
773SecWinNtFdAddress (\r
774 IN UINTN Index,\r
775 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
776 IN OUT UINT64 *FdSize\r
777 )\r
778/*++\r
779\r
780Routine Description:\r
781 Return the FD Size and base address. Since the FD is loaded from a\r
782 file into Windows memory only the SEC will know it's address.\r
783\r
784Arguments:\r
785 Index - Which FD, starts at zero.\r
786 FdSize - Size of the FD in bytes\r
787 FdBase - Start address of the FD. Assume it points to an FV Header\r
788\r
789Returns:\r
790 EFI_SUCCESS - Return the Base address and size of the FV\r
791 EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
792\r
793--*/\r
794{\r
795 if (Index >= gFdInfoCount) {\r
796 return EFI_UNSUPPORTED;\r
797 }\r
798\r
799 *FdBase = gFdInfo[Index].Address;\r
800 *FdSize = gFdInfo[Index].Size;\r
801\r
802 if (*FdBase == 0 && *FdSize == 0) {\r
803 return EFI_UNSUPPORTED;\r
804 }\r
805\r
806 return EFI_SUCCESS;\r
807}\r
808\r
809EFI_STATUS\r
810EFIAPI\r
811SecImageRead (\r
812 IN VOID *FileHandle,\r
813 IN UINTN FileOffset,\r
814 IN OUT UINTN *ReadSize,\r
815 OUT VOID *Buffer\r
816 )\r
817/*++\r
818\r
819Routine Description:\r
820 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
821\r
822Arguments:\r
823 FileHandle - The handle to the PE/COFF file\r
824 FileOffset - The offset, in bytes, into the file to read\r
825 ReadSize - The number of bytes to read from the file starting at FileOffset\r
826 Buffer - A pointer to the buffer to read the data into.\r
827\r
828Returns:\r
829 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
830\r
831--*/\r
832{\r
833 CHAR8 *Destination8;\r
834 CHAR8 *Source8;\r
835 UINTN Length;\r
836\r
837 Destination8 = Buffer;\r
838 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
839 Length = *ReadSize;\r
840 while (Length--) {\r
841 *(Destination8++) = *(Source8++);\r
842 }\r
843\r
844 return EFI_SUCCESS;\r
845}\r
846\r
847UINTN\r
848CountSeperatorsInString (\r
849 IN const CHAR16 *String,\r
850 IN CHAR16 Seperator\r
851 )\r
852/*++\r
853\r
854Routine Description:\r
855 Count the number of seperators in String\r
856\r
857Arguments:\r
858 String - String to process\r
859 Seperator - Item to count\r
860\r
861Returns:\r
862 Number of Seperator in String\r
863\r
864--*/\r
865{\r
866 UINTN Count;\r
867\r
868 for (Count = 0; *String != '\0'; String++) {\r
869 if (*String == Seperator) {\r
870 Count++;\r
871 }\r
872 }\r
873\r
874 return Count;\r
875}\r
876\r
877\r
878\r
879EFI_STATUS\r
880EFIAPI\r
881SecNt32PeCoffGetImageInfo (\r
882 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
883 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
884 )\r
885{\r
886 EFI_STATUS Status;\r
887\r
888 Status = PeCoffLoaderGetImageInfo (ImageContext);\r
889 if (EFI_ERROR (Status)) {\r
890 return Status;\r
891 }\r
892\r
893 switch (ImageContext->ImageType) {\r
894\r
895 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
896 ImageContext->ImageCodeMemoryType = EfiLoaderCode;\r
897 ImageContext->ImageDataMemoryType = EfiLoaderData;\r
898 break;\r
899\r
900 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
901 ImageContext->ImageCodeMemoryType = EfiBootServicesCode;\r
902 ImageContext->ImageDataMemoryType = EfiBootServicesData;\r
903 break;\r
904\r
905 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
906 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:\r
907 ImageContext->ImageCodeMemoryType = EfiRuntimeServicesCode;\r
908 ImageContext->ImageDataMemoryType = EfiRuntimeServicesData;\r
909 break;\r
910\r
911 default:\r
912 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r
913 return RETURN_UNSUPPORTED;\r
914 }\r
915\r
916 return Status;\r
917}\r
918\r
919EFI_STATUS\r
920EFIAPI\r
921SecNt32PeCoffLoadImage (\r
922 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
923 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
924 )\r
925{\r
926 EFI_STATUS Status;\r
927\r
928 Status = PeCoffLoaderLoadImage (ImageContext);\r
929 return Status;\r
930}\r
931\r
932VOID
933SecUnixLoaderBreak (
934 VOID
935 )
936{
937}
938
939EFI_STATUS\r
940EFIAPI\r
941SecNt32PeCoffRelocateImage (\r
942 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
943 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
944 )\r
945{\r
946\r
947#if 0
948 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
949 EFI_IMAGE_SECTION_HEADER *Sec;
950 INTN i;
951#endif
952
953 fprintf (stderr,
954 "Loading %s 0x%08lx - entry point 0x%08lx\n",
955 ImageContext->PdbPointer,
956 (unsigned long)ImageContext->ImageAddress,
957 (unsigned long)ImageContext->EntryPoint);
958
959#if 0
960 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)
961 ((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);\r
962 Sec = (EFI_IMAGE_SECTION_HEADER*)
963 ((UINTN)ImageContext->ImageAddress
964 + ImageContext->PeCoffHeaderOffset
965 + sizeof(UINT32)
966 + sizeof(EFI_IMAGE_FILE_HEADER)
967 + Hdr.Pe32->FileHeader.SizeOfOptionalHeader);
968 for (i = 0; i < Hdr.Pe32->FileHeader.NumberOfSections; i++)
969 fprintf (stderr, " %s 0x%08lx\n",
970 Sec[i].Name, (unsigned long)Sec[i].VirtualAddress);
971#endif
972
973 SecUnixLoaderBreak ();
974
975 return PeCoffLoaderRelocateImage (ImageContext);\r
976}\r
977\r
978\r
979EFI_STATUS\r
980EFIAPI\r
981SecNt32PeCoffUnloadimage (\r
982 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
983 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
984 )\r
985{\r
986 return EFI_SUCCESS;\r
987}\r
988\r
989VOID\r
990_ModuleEntryPoint (\r
991 VOID\r
992 )\r
993{\r
994}\r
995\r