]>
Commit | Line | Data |
---|---|---|
c9093a06 | 1 | /*++\r |
2 | \r | |
7e9f4e1d | 3 | Copyright (c) 2006 - 2007 Intel Corporation.\r |
c9093a06 | 4 | All rights reserved. This program and the accompanying materials\r |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | SecMain.c\r | |
15 | \r | |
16 | Abstract:\r | |
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 | |
44 | EFI_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 | |
56 | EFI_PEI_PE_COFF_LOADER_PROTOCOL *gPeiEfiPeiPeCoffLoader = &mPeiEfiPeiPeCoffLoaderInstance.PeCoff;\r | |
57 | \r | |
58 | UNIX_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi = { SecWinNtPeiLoadFile };\r | |
59 | \r | |
60 | PEI_UNIX_AUTOSCAN_PPI mSecNtAutoScanPpi = { SecWinNtPeiAutoScan };\r | |
61 | \r | |
62 | PEI_UNIX_THUNK_PPI mSecWinNtThunkPpi = { SecWinNtWinNtThunkAddress };\r | |
63 | \r | |
64 | EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode };\r | |
65 | \r | |
66 | UNIX_FWH_PPI mSecFwhInformationPpi = { SecWinNtFdAddress };\r | |
67 | \r | |
68 | \r | |
69 | EFI_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 | |
110 | UINTN gFdInfoCount = 0;\r | |
111 | UNIX_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 | |
119 | UINTN gSystemMemoryCount = 0;\r | |
120 | UNIX_SYSTEM_MEMORY *gSystemMemory;\r | |
121 | \r | |
122 | \r | |
123 | STATIC | |
124 | EFI_PHYSICAL_ADDRESS * | |
125 | MapMemory ( | |
126 | INTN fd, | |
127 | UINT64 length, | |
128 | INTN prot, | |
129 | INTN flags); | |
130 | \r | |
131 | STATIC | |
132 | EFI_STATUS | |
133 | MapFile ( | |
134 | IN CHAR8 *FileName,\r | |
135 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r | |
136 | OUT UINT64 *Length\r | |
137 | ); | |
138 | \r | |
139 | \r | |
140 | INTN\r | |
141 | EFIAPI\r | |
142 | main (\r | |
143 | IN INTN Argc,\r | |
144 | IN CHAR8 **Argv,\r | |
145 | IN CHAR8 **Envp\r | |
146 | )\r | |
147 | /*++\r | |
148 | \r | |
149 | Routine Description:\r | |
377fc2ae | 150 | Main entry point to SEC for WinNt. This is a unix program\r |
c9093a06 | 151 | \r |
152 | Arguments:\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 | |
157 | Returns:\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 | |
7e9f4e1d | 176 | setbuf(stdout, 0);\r |
177 | setbuf(stderr, 0);\r | |
178 | \r | |
c9093a06 | 179 | MemorySizeStr = (CHAR16 *)PcdGetPtr (PcdUnixMemorySizeForSecMain);\r |
180 | FirmwareVolumesStr = (CHAR16 *)PcdGetPtr (PcdUnixFirmwareVolume);\r | |
181 | \r | |
377fc2ae | 182 | printf ("\nEDK SEC Main UNIX Emulation Environment from www.TianoCore.org\n");\r |
c9093a06 | 183 | \r |
184 | //\r | |
185 | // Allocate space for gSystemMemory Array\r | |
186 | //\r | |
187 | gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1;\r | |
188 | gSystemMemory = calloc (gSystemMemoryCount, sizeof (UNIX_SYSTEM_MEMORY));\r | |
189 | if (gSystemMemory == NULL) {\r | |
190 | printf ("ERROR : Can not allocate memory for system. Exiting.\n"); | |
191 | exit (1);\r | |
192 | }\r | |
193 | //\r | |
194 | // Allocate space for gSystemMemory Array\r | |
195 | //\r | |
196 | gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\r | |
197 | gFdInfo = calloc (gFdInfoCount, sizeof (UNIX_FD_INFO));\r | |
198 | if (gFdInfo == NULL) {\r | |
199 | printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); | |
200 | exit (1);\r | |
201 | }\r | |
202 | //\r | |
203 | // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r | |
204 | //\r | |
205 | printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdUnixBootMode));\r | |
206 | \r | |
207 | //\r | |
208 | // Open up a 128K file to emulate temp memory for PEI.\r | |
209 | // on a real platform this would be SRAM, or using the cache as RAM.\r | |
210 | // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping\r | |
211 | //\r | |
212 | InitialStackMemorySize = 0x20000;\r | |
213 | InitialStackMemory = (UINTN)MapMemory(0, | |
214 | (UINT32) InitialStackMemorySize,\r | |
215 | PROT_READ | PROT_WRITE, | |
216 | MAP_ANONYMOUS | MAP_PRIVATE); | |
217 | if (InitialStackMemory == 0) {\r | |
218 | printf ("ERROR : Can not open SecStack Exiting\n");\r | |
219 | exit (1);\r | |
220 | }\r | |
221 | \r | |
222 | printf (" SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n", | |
223 | (UINTN)(InitialStackMemorySize / 1024), | |
224 | (unsigned long)InitialStackMemory);\r | |
225 | \r | |
226 | //\r | |
227 | // Open All the firmware volumes and remember the info in the gFdInfo global\r | |
228 | //\r | |
229 | FileName = (CHAR8 *)malloc (StrLen (FirmwareVolumesStr) + 1); | |
230 | if (FileName == NULL) {\r | |
231 | printf ("ERROR : Can not allocate memory for firmware volume string\n");\r | |
232 | exit (1);\r | |
233 | }\r | |
234 | \r | |
235 | Index2 = 0; | |
236 | for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL; | |
237 | FirmwareVolumesStr[Index2] != 0; | |
238 | Index++) {\r | |
239 | for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++)\r | |
240 | FileName[Index1++] = FirmwareVolumesStr[Index2]; | |
241 | if (FirmwareVolumesStr[Index2] == '!') | |
242 | Index2++; | |
243 | FileName[Index1] = '\0';\r | |
244 | \r | |
245 | //\r | |
246 | // Open the FD and remmeber where it got mapped into our processes address space\r | |
247 | //\r | |
248 | Status = MapFile (\r | |
249 | FileName,\r | |
250 | &gFdInfo[Index].Address,\r | |
251 | &gFdInfo[Index].Size\r | |
252 | );\r | |
253 | if (EFI_ERROR (Status)) {\r | |
254 | printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, Status);\r | |
255 | exit (1);\r | |
256 | }\r | |
257 | \r | |
258 | printf (" FD loaded from %s at 0x%08lx", | |
259 | FileName, (unsigned long)gFdInfo[Index].Address);\r | |
260 | ||
261 | if (PeiCoreFile == NULL) {\r | |
262 | //\r | |
263 | // Assume the beginning of the FD is an FV and look for the PEI Core.\r | |
264 | // Load the first one we find.\r | |
265 | //\r | |
266 | Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r | |
267 | if (!EFI_ERROR (Status)) {\r | |
268 | PeiIndex = Index;\r | |
269 | printf (" contains SEC Core");\r | |
270 | }\r | |
271 | }\r | |
272 | \r | |
273 | printf ("\n");\r | |
274 | }\r | |
275 | //\r | |
276 | // Calculate memory regions and store the information in the gSystemMemory\r | |
277 | // global for later use. The autosizing code will use this data to\r | |
278 | // map this memory into the SEC process memory space.\r | |
279 | //\r | |
280 | Index1 = 0; | |
281 | Index = 0; | |
282 | while (1) { | |
283 | UINTN val = 0; | |
284 | //\r | |
285 | // Save the size of the memory. | |
286 | //\r | |
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 | \r | |
297 | printf ("\n");\r | |
298 | \r | |
299 | //\r | |
300 | // Hand off to PEI Core\r | |
301 | //\r | |
302 | SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile);\r | |
303 | \r | |
304 | //\r | |
305 | // If we get here, then the PEI Core returned. This is an error as PEI should\r | |
306 | // always hand off to DXE.\r | |
307 | //\r | |
308 | printf ("ERROR : PEI Core returned\n");\r | |
309 | exit (1);\r | |
310 | }\r | |
311 | \r | |
312 | EFI_PHYSICAL_ADDRESS * | |
313 | MapMemory ( | |
314 | INTN fd, | |
315 | UINT64 length, | |
316 | INTN prot, | |
317 | INTN flags) | |
318 | { | |
86e6bb78 | 319 | STATIC UINTN base = 0x40000000; |
320 | CONST UINTN align = (1 << 24); | |
321 | VOID *res = NULL; | |
322 | BOOLEAN isAligned = 0; | |
c9093a06 | 323 | |
6bb60d71 | 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 | } | |
c9093a06 | 341 | return res; |
342 | } | |
343 | ||
344 | EFI_STATUS | |
345 | MapFile ( | |
346 | IN CHAR8 *FileName,\r | |
347 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r | |
348 | OUT UINT64 *Length\r | |
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 | \r | |
409 | #define BYTES_PER_RECORD 512\r | |
410 | \r | |
411 | /**\r | |
412 | Extracts ASSERT() information from a status code structure.\r | |
413 | \r | |
414 | Converts the status code specified by CodeType, Value, and Data to the ASSERT()\r | |
415 | arguments specified by Filename, Description, and LineNumber. If CodeType is \r | |
416 | an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and \r | |
417 | Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract \r | |
418 | Filename, Description, and LineNumber from the optional data area of the \r | |
419 | status code buffer specified by Data. The optional data area of Data contains \r | |
420 | a Null-terminated ASCII string for the FileName, followed by a Null-terminated \r | |
421 | ASCII string for the Description, followed by a 32-bit LineNumber. If the \r | |
422 | ASSERT() information could be extracted from Data, then return TRUE. \r | |
423 | Otherwise, FALSE is returned. \r | |
424 | \r | |
425 | If Data is NULL, then ASSERT().\r | |
426 | If Filename is NULL, then ASSERT().\r | |
427 | If Description is NULL, then ASSERT().\r | |
428 | If LineNumber is NULL, then ASSERT().\r | |
429 | \r | |
430 | @param CodeType The type of status code being converted.\r | |
431 | @param Value The status code value being converted.\r | |
432 | @param Data Pointer to status code data buffer. \r | |
433 | @param Filename Pointer to the source file name that generated the ASSERT().\r | |
434 | @param Description Pointer to the description of the ASSERT().\r | |
435 | @param LineNumber Pointer to source line number that generated the ASSERT().\r | |
436 | \r | |
437 | @retval TRUE The status code specified by CodeType, Value, and Data was \r | |
438 | converted ASSERT() arguments specified by Filename, Description, \r | |
439 | and LineNumber.\r | |
440 | @retval FALSE The status code specified by CodeType, Value, and Data could \r | |
441 | not be converted to ASSERT() arguments.\r | |
442 | \r | |
443 | **/\r | |
444 | STATIC\r | |
445 | BOOLEAN\r | |
446 | ReportStatusCodeExtractAssertInfo (\r | |
447 | IN EFI_STATUS_CODE_TYPE CodeType,\r | |
448 | IN EFI_STATUS_CODE_VALUE Value, \r | |
449 | IN CONST EFI_STATUS_CODE_DATA *Data, \r | |
450 | OUT CHAR8 **Filename,\r | |
451 | OUT CHAR8 **Description,\r | |
452 | OUT UINT32 *LineNumber\r | |
453 | )\r | |
454 | {\r | |
455 | EFI_DEBUG_ASSERT_DATA *AssertData;\r | |
456 | \r | |
457 | ASSERT (Data != NULL);\r | |
458 | ASSERT (Filename != NULL);\r | |
459 | ASSERT (Description != NULL);\r | |
460 | ASSERT (LineNumber != NULL);\r | |
461 | \r | |
462 | if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) && \r | |
463 | ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&\r | |
464 | ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {\r | |
465 | AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);\r | |
466 | *Filename = (CHAR8 *)(AssertData + 1);\r | |
467 | *Description = *Filename + AsciiStrLen (*Filename) + 1;\r | |
468 | *LineNumber = AssertData->LineNumber;\r | |
469 | return TRUE;\r | |
470 | }\r | |
471 | return FALSE;\r | |
472 | }\r | |
473 | \r | |
474 | EFI_STATUS\r | |
475 | EFIAPI\r | |
476 | SecPeiReportStatusCode (\r | |
477 | IN EFI_PEI_SERVICES **PeiServices,\r | |
478 | IN EFI_STATUS_CODE_TYPE CodeType,\r | |
479 | IN EFI_STATUS_CODE_VALUE Value,\r | |
480 | IN UINT32 Instance,\r | |
481 | IN EFI_GUID * CallerId,\r | |
482 | IN EFI_STATUS_CODE_DATA * Data OPTIONAL\r | |
483 | )\r | |
484 | /*++\r | |
485 | \r | |
486 | Routine Description:\r | |
487 | \r | |
488 | This routine produces the ReportStatusCode PEI service. It's passed\r | |
489 | up to the PEI Core via a PPI. T\r | |
490 | \r | |
377fc2ae | 491 | This code currently uses the UNIX clib printf. This does not work the same way\r |
c9093a06 | 492 | as the EFI Print (), as %t, %g, %s as Unicode are not supported.\r |
493 | \r | |
494 | Arguments:\r | |
495 | (see EFI_PEI_REPORT_STATUS_CODE)\r | |
496 | \r | |
497 | Returns:\r | |
498 | EFI_SUCCESS - Always return success\r | |
499 | \r | |
500 | --*/\r | |
501 | // TODO: PeiServices - add argument and description to function comment\r | |
502 | // TODO: CodeType - add argument and description to function comment\r | |
503 | // TODO: Value - add argument and description to function comment\r | |
504 | // TODO: Instance - add argument and description to function comment\r | |
505 | // TODO: CallerId - add argument and description to function comment\r | |
506 | // TODO: Data - add argument and description to function comment\r | |
507 | {\r | |
508 | CHAR8 *Format;\r | |
509 | EFI_DEBUG_INFO *DebugInfo;\r | |
510 | VA_LIST Marker;\r | |
511 | CHAR8 PrintBuffer[BYTES_PER_RECORD * 2];\r | |
512 | CHAR8 *Filename;\r | |
513 | CHAR8 *Description;\r | |
514 | UINT32 LineNumber;\r | |
515 | \r | |
516 | if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {\r | |
517 | //\r | |
518 | // This supports DEBUG () marcos\r | |
519 | // Data format\r | |
520 | // EFI_STATUS_CODE_DATA\r | |
521 | // EFI_DEBUG_INFO\r | |
522 | //\r | |
523 | // The first 12 * UINT64 bytes of the string are really an\r | |
524 | // arguement stack to support varargs on the Format string.\r | |
525 | //\r | |
526 | if (Data != NULL) {\r | |
527 | DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);\r | |
528 | Marker = (VA_LIST) (DebugInfo + 1);\r | |
529 | Format = (CHAR8 *) (((UINT64 *) Marker) + 12);\r | |
530 | \r | |
531 | AsciiVSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r | |
532 | printf (PrintBuffer);\r | |
533 | } else {\r | |
534 | printf ("DEBUG <null>\n");\r | |
535 | }\r | |
536 | }\r | |
537 | \r | |
538 | if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&\r | |
539 | ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED)\r | |
540 | ) {\r | |
541 | if (Data != NULL && ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r | |
542 | //\r | |
543 | // Support ASSERT () macro\r | |
544 | //\r | |
545 | printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description);\r | |
546 | } else {\r | |
547 | printf ("ASSERT <null>\n");\r | |
548 | }\r | |
549 | CpuBreakpoint ();\r | |
550 | }\r | |
551 | \r | |
552 | return EFI_SUCCESS;\r | |
553 | }\r | |
554 | \r | |
555 | \r | |
556 | VOID\r | |
557 | SecLoadFromCore (\r | |
558 | IN UINTN LargestRegion,\r | |
559 | IN UINTN LargestRegionSize,\r | |
560 | IN UINTN BootFirmwareVolumeBase,\r | |
561 | IN VOID *PeiCorePe32File\r | |
562 | )\r | |
563 | /*++\r | |
564 | \r | |
565 | Routine Description:\r | |
566 | This is the service to load the PEI Core from the Firmware Volume\r | |
567 | \r | |
568 | Arguments:\r | |
569 | LargestRegion - Memory to use for PEI.\r | |
570 | LargestRegionSize - Size of Memory to use for PEI\r | |
571 | BootFirmwareVolumeBase - Start of the Boot FV\r | |
572 | PeiCorePe32File - PEI Core PE32\r | |
573 | \r | |
574 | Returns:\r | |
575 | Success means control is transfered and thus we should never return\r | |
576 | \r | |
577 | --*/\r | |
578 | {\r | |
579 | EFI_STATUS Status;\r | |
580 | EFI_PHYSICAL_ADDRESS TopOfMemory;\r | |
581 | VOID *TopOfStack;\r | |
582 | UINT64 PeiCoreSize;\r | |
583 | EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint;\r | |
584 | EFI_PHYSICAL_ADDRESS PeiImageAddress;\r | |
585 | EFI_PEI_STARTUP_DESCRIPTOR *PeiStartup;\r | |
586 | \r | |
587 | //\r | |
588 | // Compute Top Of Memory for Stack and PEI Core Allocations\r | |
589 | //\r | |
590 | TopOfMemory = LargestRegion + LargestRegionSize;\r | |
591 | \r | |
592 | //\r | |
593 | // Allocate 128KB for the Stack\r | |
594 | //\r | |
595 | TopOfStack = (VOID *)((UINTN)TopOfMemory - sizeof (EFI_PEI_STARTUP_DESCRIPTOR) - CPU_STACK_ALIGNMENT);\r | |
596 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r | |
597 | TopOfMemory = TopOfMemory - STACK_SIZE;\r | |
598 | \r | |
599 | //\r | |
600 | // Patch value in dispatch table values\r | |
601 | //\r | |
602 | gPrivateDispatchTable[0].Ppi = gPeiEfiPeiPeCoffLoader;\r | |
603 | \r | |
604 | //\r | |
605 | // Bind this information into the SEC hand-off state\r | |
606 | //\r | |
607 | PeiStartup = (EFI_PEI_STARTUP_DESCRIPTOR *) (UINTN) TopOfStack;\r | |
608 | PeiStartup->DispatchTable = (EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable;\r | |
609 | PeiStartup->SizeOfCacheAsRam = STACK_SIZE;\r | |
610 | PeiStartup->BootFirmwareVolume = BootFirmwareVolumeBase;\r | |
611 | \r | |
612 | //\r | |
613 | // Load the PEI Core from a Firmware Volume\r | |
614 | //\r | |
615 | Status = SecWinNtPeiLoadFile (\r | |
616 | PeiCorePe32File,\r | |
617 | &PeiImageAddress,\r | |
618 | &PeiCoreSize,\r | |
619 | &PeiCoreEntryPoint\r | |
620 | );\r | |
621 | if (EFI_ERROR (Status)) {\r | |
622 | return ;\r | |
623 | }\r | |
624 | printf ("Jump to 0x%08lx\n", (unsigned long)PeiCoreEntryPoint); | |
625 | //\r | |
626 | // Transfer control to the PEI Core\r | |
627 | //\r | |
628 | SwitchStack (\r | |
629 | (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r | |
630 | PeiStartup,\r | |
631 | NULL,\r | |
632 | TopOfStack\r | |
633 | );\r | |
634 | //\r | |
635 | // If we get here, then the PEI Core returned. This is an error\r | |
636 | //\r | |
637 | return ;\r | |
638 | }\r | |
639 | \r | |
640 | EFI_STATUS\r | |
641 | EFIAPI\r | |
642 | SecWinNtPeiAutoScan (\r | |
643 | IN UINTN Index,\r | |
644 | OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r | |
645 | OUT UINT64 *MemorySize\r | |
646 | )\r | |
647 | /*++\r | |
648 | \r | |
649 | Routine Description:\r | |
650 | This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r | |
651 | It allows discontiguous memory regions to be supported by the emulator.\r | |
652 | It uses gSystemMemory[] and gSystemMemoryCount that were created by\r | |
377fc2ae | 653 | parsing the host environment variable EFI_MEMORY_SIZE.\r |
c9093a06 | 654 | The size comes from the varaible and the address comes from the call to\r |
655 | WinNtOpenFile.\r | |
656 | \r | |
657 | Arguments:\r | |
658 | Index - Which memory region to use\r | |
659 | MemoryBase - Return Base address of memory region\r | |
660 | MemorySize - Return size in bytes of the memory region\r | |
661 | \r | |
662 | Returns:\r | |
663 | EFI_SUCCESS - If memory region was mapped\r | |
664 | EFI_UNSUPPORTED - If Index is not supported\r | |
665 | \r | |
666 | --*/\r | |
667 | {\r | |
668 | void *res; | |
669 | \r | |
670 | if (Index >= gSystemMemoryCount) {\r | |
671 | return EFI_UNSUPPORTED;\r | |
672 | }\r | |
673 | \r | |
674 | *MemoryBase = 0;\r | |
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;\r | |
683 | \r | |
684 | return EFI_SUCCESS;\r | |
685 | }\r | |
686 | \r | |
687 | VOID *\r | |
688 | EFIAPI\r | |
689 | SecWinNtWinNtThunkAddress (\r | |
690 | VOID\r | |
691 | )\r | |
692 | /*++\r | |
693 | \r | |
694 | Routine Description:\r | |
377fc2ae | 695 | Since the SEC is the only Unix program in stack it must export\r |
c9093a06 | 696 | an interface to do Win API calls. That's what the WinNtThunk address\r |
697 | is for. gWinNt is initailized in WinNtThunk.c.\r | |
698 | \r | |
699 | Arguments:\r | |
700 | InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r | |
701 | InterfaceBase - Address of the gWinNt global\r | |
702 | \r | |
703 | Returns:\r | |
704 | EFI_SUCCESS - Data returned\r | |
705 | \r | |
706 | --*/\r | |
707 | {\r | |
708 | return gUnix;\r | |
709 | }\r | |
710 | \r | |
711 | \r | |
712 | EFI_STATUS\r | |
713 | EFIAPI\r | |
714 | SecWinNtPeiLoadFile (\r | |
715 | IN VOID *Pe32Data,\r | |
716 | IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
717 | IN UINT64 *ImageSize,\r | |
718 | IN EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
719 | )\r | |
720 | /*++\r | |
721 | \r | |
722 | Routine Description:\r | |
723 | Loads and relocates a PE/COFF image into memory.\r | |
724 | \r | |
725 | Arguments:\r | |
726 | Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r | |
727 | ImageAddress - The base address of the relocated PE/COFF image\r | |
728 | ImageSize - The size of the relocated PE/COFF image\r | |
729 | EntryPoint - The entry point of the relocated PE/COFF image\r | |
730 | \r | |
731 | Returns:\r | |
732 | EFI_SUCCESS - The file was loaded and relocated\r | |
733 | EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r | |
734 | \r | |
735 | --*/\r | |
736 | {\r | |
737 | EFI_STATUS Status;\r | |
738 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
739 | \r | |
740 | ZeroMem (&ImageContext, sizeof (ImageContext));\r | |
741 | ImageContext.Handle = Pe32Data;\r | |
742 | \r | |
743 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r | |
744 | \r | |
745 | Status = gPeiEfiPeiPeCoffLoader->GetImageInfo (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
746 | if (EFI_ERROR (Status)) {\r | |
747 | return Status;\r | |
748 | }\r | |
749 | //\r | |
377fc2ae | 750 | // Allocate space in UNIX (not emulator) memory. Extra space is for alignment\r |
c9093a06 | 751 | //\r |
752 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)));\r | |
753 | if (ImageContext.ImageAddress == 0) {\r | |
754 | return EFI_OUT_OF_RESOURCES;\r | |
755 | }\r | |
756 | //\r | |
757 | // Align buffer on section boundry\r | |
758 | //\r | |
759 | ImageContext.ImageAddress += ImageContext.SectionAlignment;\r | |
760 | ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r | |
761 | ||
762 | \r | |
763 | Status = gPeiEfiPeiPeCoffLoader->LoadImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
764 | if (EFI_ERROR (Status)) {\r | |
765 | return Status;\r | |
766 | }\r | |
767 | \r | |
768 | Status = gPeiEfiPeiPeCoffLoader->RelocateImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r | |
769 | if (EFI_ERROR (Status)) {\r | |
770 | return Status;\r | |
771 | }\r | |
772 | \r | |
773 | //\r | |
774 | // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r | |
775 | //\r | |
776 | \r | |
777 | *ImageAddress = ImageContext.ImageAddress;\r | |
778 | *ImageSize = ImageContext.ImageSize;\r | |
779 | *EntryPoint = ImageContext.EntryPoint;\r | |
780 | \r | |
781 | return EFI_SUCCESS;\r | |
782 | }\r | |
783 | \r | |
784 | EFI_STATUS\r | |
785 | EFIAPI\r | |
786 | SecWinNtFdAddress (\r | |
787 | IN UINTN Index,\r | |
788 | IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r | |
789 | IN OUT UINT64 *FdSize\r | |
790 | )\r | |
791 | /*++\r | |
792 | \r | |
793 | Routine Description:\r | |
794 | Return the FD Size and base address. Since the FD is loaded from a\r | |
377fc2ae | 795 | file into host memory only the SEC will know it's address.\r |
c9093a06 | 796 | \r |
797 | Arguments:\r | |
798 | Index - Which FD, starts at zero.\r | |
799 | FdSize - Size of the FD in bytes\r | |
800 | FdBase - Start address of the FD. Assume it points to an FV Header\r | |
801 | \r | |
802 | Returns:\r | |
803 | EFI_SUCCESS - Return the Base address and size of the FV\r | |
804 | EFI_UNSUPPORTED - Index does nto map to an FD in the system\r | |
805 | \r | |
806 | --*/\r | |
807 | {\r | |
808 | if (Index >= gFdInfoCount) {\r | |
809 | return EFI_UNSUPPORTED;\r | |
810 | }\r | |
811 | \r | |
812 | *FdBase = gFdInfo[Index].Address;\r | |
813 | *FdSize = gFdInfo[Index].Size;\r | |
814 | \r | |
815 | if (*FdBase == 0 && *FdSize == 0) {\r | |
816 | return EFI_UNSUPPORTED;\r | |
817 | }\r | |
818 | \r | |
819 | return EFI_SUCCESS;\r | |
820 | }\r | |
821 | \r | |
822 | EFI_STATUS\r | |
823 | EFIAPI\r | |
824 | SecImageRead (\r | |
825 | IN VOID *FileHandle,\r | |
826 | IN UINTN FileOffset,\r | |
827 | IN OUT UINTN *ReadSize,\r | |
828 | OUT VOID *Buffer\r | |
829 | )\r | |
830 | /*++\r | |
831 | \r | |
832 | Routine Description:\r | |
833 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r | |
834 | \r | |
835 | Arguments:\r | |
836 | FileHandle - The handle to the PE/COFF file\r | |
837 | FileOffset - The offset, in bytes, into the file to read\r | |
838 | ReadSize - The number of bytes to read from the file starting at FileOffset\r | |
839 | Buffer - A pointer to the buffer to read the data into.\r | |
840 | \r | |
841 | Returns:\r | |
842 | EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r | |
843 | \r | |
844 | --*/\r | |
845 | {\r | |
846 | CHAR8 *Destination8;\r | |
847 | CHAR8 *Source8;\r | |
848 | UINTN Length;\r | |
849 | \r | |
850 | Destination8 = Buffer;\r | |
851 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r | |
852 | Length = *ReadSize;\r | |
853 | while (Length--) {\r | |
854 | *(Destination8++) = *(Source8++);\r | |
855 | }\r | |
856 | \r | |
857 | return EFI_SUCCESS;\r | |
858 | }\r | |
859 | \r | |
860 | UINTN\r | |
861 | CountSeperatorsInString (\r | |
862 | IN const CHAR16 *String,\r | |
863 | IN CHAR16 Seperator\r | |
864 | )\r | |
865 | /*++\r | |
866 | \r | |
867 | Routine Description:\r | |
868 | Count the number of seperators in String\r | |
869 | \r | |
870 | Arguments:\r | |
871 | String - String to process\r | |
872 | Seperator - Item to count\r | |
873 | \r | |
874 | Returns:\r | |
875 | Number of Seperator in String\r | |
876 | \r | |
877 | --*/\r | |
878 | {\r | |
879 | UINTN Count;\r | |
880 | \r | |
881 | for (Count = 0; *String != '\0'; String++) {\r | |
882 | if (*String == Seperator) {\r | |
883 | Count++;\r | |
884 | }\r | |
885 | }\r | |
886 | \r | |
887 | return Count;\r | |
888 | }\r | |
889 | \r | |
890 | \r | |
891 | \r | |
892 | EFI_STATUS\r | |
893 | EFIAPI\r | |
894 | SecNt32PeCoffGetImageInfo (\r | |
895 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
896 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
897 | )\r | |
898 | {\r | |
899 | EFI_STATUS Status;\r | |
900 | \r | |
901 | Status = PeCoffLoaderGetImageInfo (ImageContext);\r | |
902 | if (EFI_ERROR (Status)) {\r | |
903 | return Status;\r | |
904 | }\r | |
905 | \r | |
906 | switch (ImageContext->ImageType) {\r | |
907 | \r | |
908 | case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r | |
909 | ImageContext->ImageCodeMemoryType = EfiLoaderCode;\r | |
910 | ImageContext->ImageDataMemoryType = EfiLoaderData;\r | |
911 | break;\r | |
912 | \r | |
913 | case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r | |
914 | ImageContext->ImageCodeMemoryType = EfiBootServicesCode;\r | |
915 | ImageContext->ImageDataMemoryType = EfiBootServicesData;\r | |
916 | break;\r | |
917 | \r | |
918 | case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r | |
919 | case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:\r | |
920 | ImageContext->ImageCodeMemoryType = EfiRuntimeServicesCode;\r | |
921 | ImageContext->ImageDataMemoryType = EfiRuntimeServicesData;\r | |
922 | break;\r | |
923 | \r | |
924 | default:\r | |
925 | ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r | |
926 | return RETURN_UNSUPPORTED;\r | |
927 | }\r | |
928 | \r | |
929 | return Status;\r | |
930 | }\r | |
931 | \r | |
932 | EFI_STATUS\r | |
933 | EFIAPI\r | |
934 | SecNt32PeCoffLoadImage (\r | |
935 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
936 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
937 | )\r | |
938 | {\r | |
939 | EFI_STATUS Status;\r | |
940 | \r | |
941 | Status = PeCoffLoaderLoadImage (ImageContext);\r | |
942 | return Status;\r | |
943 | }\r | |
944 | \r | |
945 | VOID | |
946 | SecUnixLoaderBreak ( | |
947 | VOID | |
948 | ) | |
949 | { | |
950 | } | |
951 | ||
952 | EFI_STATUS\r | |
953 | EFIAPI\r | |
954 | SecNt32PeCoffRelocateImage (\r | |
955 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
956 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
957 | )\r | |
958 | {\r | |
959 | \r | |
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);\r | |
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);\r | |
989 | }\r | |
990 | \r | |
991 | \r | |
992 | EFI_STATUS\r | |
993 | EFIAPI\r | |
994 | SecNt32PeCoffUnloadimage (\r | |
995 | IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r | |
996 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
997 | )\r | |
998 | {\r | |
999 | return EFI_SUCCESS;\r | |
1000 | }\r | |
1001 | \r | |
1002 | VOID\r | |
1003 | _ModuleEntryPoint (\r | |
1004 | VOID\r | |
1005 | )\r | |
1006 | {\r | |
1007 | }\r | |
1008 | \r |