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