]>
Commit | Line | Data |
---|---|---|
949f388f | 1 | /*++ @file |
2 | ||
3 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR> | |
4 | Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR> | |
5 | This program and the accompanying materials | |
6 | are licensed and made available under the terms and conditions of the BSD License | |
7 | which accompanies this distribution. The full text of the license may be found at | |
8 | http://opensource.org/licenses/bsd-license.php | |
9 | ||
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. | |
12 | ||
13 | **/ | |
14 | ||
15 | #include "SecMain.h" | |
16 | ||
17 | #ifdef __APPLE__ | |
18 | #define MAP_ANONYMOUS MAP_ANON | |
949f388f | 19 | #endif |
20 | ||
21 | ||
22 | // | |
23 | // Globals | |
24 | // | |
25 | ||
26 | EMU_THUNK_PPI mSecEmuThunkPpi = { | |
27 | GasketSecUnixPeiAutoScan, | |
28 | GasketSecUnixFdAddress, | |
65e3f333 | 29 | GasketSecEmuThunkAddress |
949f388f | 30 | }; |
31 | ||
0bc9421b | 32 | char *gGdbWorkingFileName = NULL; |
febb2276 | 33 | unsigned int mScriptSymbolChangesCount = 0; |
949f388f | 34 | |
35 | ||
36 | // | |
37 | // Default information about where the FD is located. | |
38 | // This array gets filled in with information from EFI_FIRMWARE_VOLUMES | |
39 | // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd. | |
40 | // The number of array elements is allocated base on parsing | |
41 | // EFI_FIRMWARE_VOLUMES and the memory is never freed. | |
42 | // | |
65e3f333 | 43 | UINTN gFdInfoCount = 0; |
44 | EMU_FD_INFO *gFdInfo; | |
949f388f | 45 | |
46 | // | |
47 | // Array that supports seperate memory rantes. | |
48 | // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable. | |
49 | // The number of array elements is allocated base on parsing | |
50 | // EFI_MEMORY_SIZE and the memory is never freed. | |
51 | // | |
65e3f333 | 52 | UINTN gSystemMemoryCount = 0; |
53 | EMU_SYSTEM_MEMORY *gSystemMemory; | |
949f388f | 54 | |
55 | ||
56 | ||
57 | UINTN mImageContextModHandleArraySize = 0; | |
58 | IMAGE_CONTEXT_TO_MOD_HANDLE *mImageContextModHandleArray = NULL; | |
59 | ||
946bfba2 | 60 | EFI_PEI_PPI_DESCRIPTOR *gPpiList; |
949f388f | 61 | |
f70c2a12 | 62 | |
63 | int gInXcode = 0; | |
64 | ||
65 | ||
66 | /*++ | |
67 | Breakpoint target for Xcode project. Set in the Xcode XML | |
68 | ||
69 | Xcode breakpoint will 'source SecMain.gdb' | |
70 | gGdbWorkingFileName is set to SecMain.gdb | |
71 | ||
72 | **/ | |
73 | VOID | |
74 | SecGdbConfigBreak ( | |
75 | VOID | |
76 | ) | |
77 | { | |
78 | } | |
79 | ||
80 | ||
81 | ||
949f388f | 82 | /*++ |
83 | ||
84 | Routine Description: | |
85 | Main entry point to SEC for Unix. This is a unix program | |
86 | ||
87 | Arguments: | |
88 | Argc - Number of command line arguments | |
89 | Argv - Array of command line argument strings | |
90 | Envp - Array of environmemt variable strings | |
91 | ||
92 | Returns: | |
93 | 0 - Normal exit | |
94 | 1 - Abnormal exit | |
95 | ||
96 | **/ | |
65e3f333 | 97 | int |
98 | main ( | |
99 | IN int Argc, | |
100 | IN char **Argv, | |
101 | IN char **Envp | |
102 | ) | |
949f388f | 103 | { |
104 | EFI_STATUS Status; | |
105 | EFI_PHYSICAL_ADDRESS InitialStackMemory; | |
106 | UINT64 InitialStackMemorySize; | |
107 | UINTN Index; | |
108 | UINTN Index1; | |
109 | UINTN Index2; | |
110 | UINTN PeiIndex; | |
111 | CHAR8 *FileName; | |
112 | BOOLEAN Done; | |
65e3f333 | 113 | EFI_PEI_FILE_HANDLE FileHandle; |
114 | VOID *SecFile; | |
949f388f | 115 | CHAR16 *MemorySizeStr; |
116 | CHAR16 *FirmwareVolumesStr; | |
117 | UINTN *StackPointer; | |
0bc9421b | 118 | FILE *GdbTempFile; |
f70c2a12 | 119 | |
120 | // | |
121 | // Xcode does not support sourcing gdb scripts directly, so the Xcode XML | |
122 | // has a break point script to source the GdbRun script. | |
123 | // | |
124 | SecGdbConfigBreak (); | |
125 | ||
126 | // | |
127 | // If dlopen doesn't work, then we build a gdb script to allow the | |
128 | // symbols to be loaded. | |
129 | // | |
130 | Index = strlen (*Argv); | |
131 | gGdbWorkingFileName = AllocatePool (Index + strlen(".gdb") + 1); | |
132 | strcpy (gGdbWorkingFileName, *Argv); | |
133 | strcat (gGdbWorkingFileName, ".gdb"); | |
134 | ||
135 | // | |
136 | // Empty out the gdb symbols script file. | |
137 | // | |
138 | GdbTempFile = fopen (gGdbWorkingFileName, "w"); | |
139 | if (GdbTempFile != NULL) { | |
140 | fclose (GdbTempFile); | |
141 | } | |
949f388f | 142 | |
f70c2a12 | 143 | printf ("\nEDK II UNIX Host Emulation Environment from edk2.sourceforge.net\n"); |
144 | ||
65e3f333 | 145 | setbuf (stdout, 0); |
146 | setbuf (stderr, 0); | |
949f388f | 147 | |
148 | MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize); | |
149 | FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume); | |
150 | ||
949f388f | 151 | // |
152 | // PPIs pased into PEI_CORE | |
153 | // | |
949f388f | 154 | AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi); |
949f388f | 155 | |
156 | SecInitThunkProtocol (); | |
157 | ||
158 | // | |
159 | // Emulator Bus Driver Thunks | |
160 | // | |
161 | AddThunkProtocol (&gX11ThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE); | |
162 | AddThunkProtocol (&gPosixFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE); | |
033d0e5f | 163 | AddThunkProtocol (&gBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE); |
2b59fcd5 | 164 | AddThunkProtocol (&gSnpThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuNetworkInterface), TRUE); |
033d0e5f | 165 | |
949f388f | 166 | // |
167 | // Emulator other Thunks | |
168 | // | |
169 | AddThunkProtocol (&gPthreadThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuApCount), FALSE); | |
170 | ||
171 | // EmuSecLibConstructor (); | |
65e3f333 | 172 | |
65e3f333 | 173 | gPpiList = GetThunkPpiList (); |
174 | ||
949f388f | 175 | // |
176 | // Allocate space for gSystemMemory Array | |
177 | // | |
178 | gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1; | |
b8800fc5 | 179 | gSystemMemory = AllocateZeroPool (gSystemMemoryCount * sizeof (EMU_SYSTEM_MEMORY)); |
949f388f | 180 | if (gSystemMemory == NULL) { |
181 | printf ("ERROR : Can not allocate memory for system. Exiting.\n"); | |
182 | exit (1); | |
183 | } | |
184 | // | |
185 | // Allocate space for gSystemMemory Array | |
186 | // | |
187 | gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1; | |
b8800fc5 | 188 | gFdInfo = AllocateZeroPool (gFdInfoCount * sizeof (EMU_FD_INFO)); |
949f388f | 189 | if (gFdInfo == NULL) { |
190 | printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); | |
191 | exit (1); | |
192 | } | |
193 | ||
194 | printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode)); | |
195 | ||
196 | // | |
65e3f333 | 197 | // Open up a 128K file to emulate temp memory for SEC. |
949f388f | 198 | // on a real platform this would be SRAM, or using the cache as RAM. |
199 | // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping | |
200 | // | |
201 | InitialStackMemorySize = STACK_SIZE; | |
65e3f333 | 202 | InitialStackMemory = (UINTN)MapMemory ( |
203 | 0, (UINT32) InitialStackMemorySize, | |
204 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | |
205 | ); | |
949f388f | 206 | if (InitialStackMemory == 0) { |
207 | printf ("ERROR : Can not open SecStack Exiting\n"); | |
208 | exit (1); | |
209 | } | |
210 | ||
65e3f333 | 211 | printf (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n", |
949f388f | 212 | (unsigned int)(InitialStackMemorySize / 1024), |
65e3f333 | 213 | (unsigned long)InitialStackMemory |
214 | ); | |
949f388f | 215 | |
216 | for (StackPointer = (UINTN*) (UINTN) InitialStackMemory; | |
217 | StackPointer < (UINTN*)(UINTN)((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize); | |
218 | StackPointer ++) { | |
219 | *StackPointer = 0x5AA55AA5; | |
220 | } | |
221 | ||
222 | // | |
223 | // Open All the firmware volumes and remember the info in the gFdInfo global | |
224 | // | |
b8800fc5 | 225 | FileName = (CHAR8 *) AllocatePool (StrLen (FirmwareVolumesStr) + 1); |
949f388f | 226 | if (FileName == NULL) { |
227 | printf ("ERROR : Can not allocate memory for firmware volume string\n"); | |
228 | exit (1); | |
229 | } | |
230 | ||
231 | Index2 = 0; | |
65e3f333 | 232 | for (Done = FALSE, Index = 0, PeiIndex = 0, SecFile = NULL; |
949f388f | 233 | FirmwareVolumesStr[Index2] != 0; |
234 | Index++) { | |
65e3f333 | 235 | for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++) { |
949f388f | 236 | FileName[Index1++] = FirmwareVolumesStr[Index2]; |
65e3f333 | 237 | } |
238 | if (FirmwareVolumesStr[Index2] == '!') { | |
949f388f | 239 | Index2++; |
65e3f333 | 240 | } |
949f388f | 241 | FileName[Index1] = '\0'; |
242 | ||
8052c4a2 | 243 | if (Index == 0) { |
244 | // Map FV Recovery Read Only and other areas Read/Write | |
245 | Status = MapFd0 ( | |
246 | FileName, | |
247 | &gFdInfo[0].Address, | |
248 | &gFdInfo[0].Size | |
249 | ); | |
250 | } else { | |
251 | // | |
252 | // Open the FD and remmeber where it got mapped into our processes address space | |
253 | // Maps Read Only | |
254 | // | |
255 | Status = MapFile ( | |
256 | FileName, | |
257 | &gFdInfo[Index].Address, | |
258 | &gFdInfo[Index].Size | |
259 | ); | |
260 | } | |
949f388f | 261 | if (EFI_ERROR (Status)) { |
262 | printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, (unsigned int)Status); | |
263 | exit (1); | |
264 | } | |
265 | ||
65e3f333 | 266 | printf (" FD loaded from %s at 0x%08lx",FileName, (unsigned long)gFdInfo[Index].Address); |
949f388f | 267 | |
65e3f333 | 268 | if (SecFile == NULL) { |
949f388f | 269 | // |
65e3f333 | 270 | // Assume the beginning of the FD is an FV and look for the SEC Core. |
949f388f | 271 | // Load the first one we find. |
272 | // | |
65e3f333 | 273 | FileHandle = NULL; |
274 | Status = PeiServicesFfsFindNextFile ( | |
275 | EFI_FV_FILETYPE_SECURITY_CORE, | |
276 | (EFI_PEI_FV_HANDLE)(UINTN)gFdInfo[Index].Address, | |
277 | &FileHandle | |
278 | ); | |
949f388f | 279 | if (!EFI_ERROR (Status)) { |
65e3f333 | 280 | Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile); |
281 | if (!EFI_ERROR (Status)) { | |
282 | PeiIndex = Index; | |
283 | printf (" contains SEC Core"); | |
284 | } | |
949f388f | 285 | } |
286 | } | |
287 | ||
288 | printf ("\n"); | |
289 | } | |
290 | // | |
291 | // Calculate memory regions and store the information in the gSystemMemory | |
292 | // global for later use. The autosizing code will use this data to | |
293 | // map this memory into the SEC process memory space. | |
294 | // | |
295 | Index1 = 0; | |
296 | Index = 0; | |
297 | while (1) { | |
298 | UINTN val = 0; | |
299 | // | |
300 | // Save the size of the memory. | |
301 | // | |
302 | while (MemorySizeStr[Index1] >= '0' && MemorySizeStr[Index1] <= '9') { | |
303 | val = val * 10 + MemorySizeStr[Index1] - '0'; | |
304 | Index1++; | |
305 | } | |
306 | gSystemMemory[Index++].Size = val * 0x100000; | |
65e3f333 | 307 | if (MemorySizeStr[Index1] == 0) { |
949f388f | 308 | break; |
65e3f333 | 309 | } |
949f388f | 310 | Index1++; |
311 | } | |
312 | ||
313 | printf ("\n"); | |
314 | ||
315 | // | |
65e3f333 | 316 | // Hand off to SEC |
949f388f | 317 | // |
65e3f333 | 318 | SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, SecFile); |
949f388f | 319 | |
320 | // | |
65e3f333 | 321 | // If we get here, then the SEC Core returned. This is an error as SEC should |
322 | // always hand off to PEI Core and then on to DXE Core. | |
949f388f | 323 | // |
65e3f333 | 324 | printf ("ERROR : SEC returned\n"); |
949f388f | 325 | exit (1); |
326 | } | |
327 | ||
65e3f333 | 328 | |
949f388f | 329 | EFI_PHYSICAL_ADDRESS * |
330 | MapMemory ( | |
bfa084fa | 331 | IN INTN fd, |
332 | IN UINT64 length, | |
333 | IN INTN prot, | |
334 | IN INTN flags | |
335 | ) | |
949f388f | 336 | { |
337 | STATIC UINTN base = 0x40000000; | |
338 | CONST UINTN align = (1 << 24); | |
339 | VOID *res = NULL; | |
340 | BOOLEAN isAligned = 0; | |
341 | ||
342 | // | |
343 | // Try to get an aligned block somewhere in the address space of this | |
344 | // process. | |
345 | // | |
346 | while((!isAligned) && (base != 0)) { | |
347 | res = mmap ((void *)base, length, prot, flags, fd, 0); | |
348 | if (res == MAP_FAILED) { | |
349 | return NULL; | |
350 | } | |
351 | if ((((UINTN)res) & ~(align-1)) == (UINTN)res) { | |
352 | isAligned=1; | |
65e3f333 | 353 | } else { |
949f388f | 354 | munmap(res, length); |
355 | base += align; | |
356 | } | |
357 | } | |
358 | return res; | |
359 | } | |
360 | ||
65e3f333 | 361 | |
949f388f | 362 | /*++ |
363 | ||
364 | Routine Description: | |
365 | Opens and memory maps a file using Unix services. If BaseAddress is non zero | |
366 | the process will try and allocate the memory starting at BaseAddress. | |
367 | ||
368 | Arguments: | |
369 | FileName - The name of the file to open and map | |
370 | MapSize - The amount of the file to map in bytes | |
371 | CreationDisposition - The flags to pass to CreateFile(). Use to create new files for | |
372 | memory emulation, and exiting files for firmware volume emulation | |
373 | BaseAddress - The base address of the mapped file in the user address space. | |
374 | If passed in as NULL the a new memory region is used. | |
375 | If passed in as non NULL the request memory region is used for | |
376 | the mapping of the file into the process space. | |
377 | Length - The size of the mapped region in bytes | |
378 | ||
379 | Returns: | |
380 | EFI_SUCCESS - The file was opened and mapped. | |
381 | EFI_NOT_FOUND - FileName was not found in the current directory | |
382 | EFI_DEVICE_ERROR - An error occured attempting to map the opened file | |
383 | ||
384 | **/ | |
65e3f333 | 385 | EFI_STATUS |
386 | MapFile ( | |
387 | IN CHAR8 *FileName, | |
388 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
389 | OUT UINT64 *Length | |
390 | ) | |
949f388f | 391 | { |
8052c4a2 | 392 | int fd; |
949f388f | 393 | VOID *res; |
394 | UINTN FileSize; | |
395 | ||
7e284acb | 396 | fd = open (FileName, O_RDWR); |
65e3f333 | 397 | if (fd < 0) { |
949f388f | 398 | return EFI_NOT_FOUND; |
65e3f333 | 399 | } |
949f388f | 400 | FileSize = lseek (fd, 0, SEEK_END); |
401 | ||
402 | ||
946bfba2 | 403 | res = MapMemory (fd, FileSize, PROT_READ | PROT_EXEC, MAP_PRIVATE); |
949f388f | 404 | |
405 | close (fd); | |
406 | ||
7e284acb | 407 | if (res == NULL) { |
408 | perror ("MapFile() Failed"); | |
949f388f | 409 | return EFI_DEVICE_ERROR; |
65e3f333 | 410 | } |
8052c4a2 | 411 | |
949f388f | 412 | *Length = (UINT64) FileSize; |
413 | *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res; | |
414 | ||
415 | return EFI_SUCCESS; | |
416 | } | |
417 | ||
8052c4a2 | 418 | EFI_STATUS |
419 | MapFd0 ( | |
420 | IN CHAR8 *FileName, | |
421 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
422 | OUT UINT64 *Length | |
423 | ) | |
424 | { | |
425 | int fd; | |
946bfba2 | 426 | void *res, *res2, *res3; |
8052c4a2 | 427 | UINTN FileSize; |
428 | UINTN FvSize; | |
946bfba2 | 429 | void *EmuMagicPage; |
8052c4a2 | 430 | |
431 | fd = open (FileName, O_RDWR); | |
432 | if (fd < 0) { | |
433 | return EFI_NOT_FOUND; | |
434 | } | |
435 | FileSize = lseek (fd, 0, SEEK_END); | |
436 | ||
437 | FvSize = FixedPcdGet64 (PcdEmuFlashFvRecoverySize); | |
438 | ||
439 | // Assume start of FD is Recovery FV, and make it write protected | |
440 | res = mmap ( | |
441 | (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase), | |
442 | FvSize, | |
946bfba2 | 443 | PROT_READ | PROT_EXEC, |
8052c4a2 | 444 | MAP_PRIVATE, |
445 | fd, | |
446 | 0 | |
447 | ); | |
448 | if (res == MAP_FAILED) { | |
946bfba2 | 449 | perror ("MapFd0() Failed res ="); |
8052c4a2 | 450 | close (fd); |
451 | return EFI_DEVICE_ERROR; | |
946bfba2 | 452 | } else if (res != (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase)) { |
453 | // We could not load at the build address, so we need to allow writes | |
454 | munmap (res, FvSize); | |
455 | res = mmap ( | |
456 | (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase), | |
457 | FvSize, | |
458 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
459 | MAP_PRIVATE, | |
460 | fd, | |
461 | 0 | |
462 | ); | |
463 | if (res == MAP_FAILED) { | |
464 | perror ("MapFd0() Failed res ="); | |
465 | close (fd); | |
466 | return EFI_DEVICE_ERROR; | |
467 | } | |
8052c4a2 | 468 | } |
469 | ||
470 | // Map the rest of the FD as read/write | |
471 | res2 = mmap ( | |
112a857f | 472 | (void *)(UINTN)(FixedPcdGet64 (PcdEmuFlashFvRecoveryBase) + FvSize), |
8052c4a2 | 473 | FileSize - FvSize, |
474 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
475 | MAP_SHARED, | |
476 | fd, | |
477 | FvSize | |
478 | ); | |
479 | close (fd); | |
480 | if (res2 == MAP_FAILED) { | |
946bfba2 | 481 | perror ("MapFd0() Failed res2 ="); |
8052c4a2 | 482 | return EFI_DEVICE_ERROR; |
483 | } | |
484 | ||
946bfba2 | 485 | // |
486 | // If enabled use the magic page to communicate between modules | |
487 | // This replaces the PI PeiServicesTable pointer mechanism that | |
488 | // deos not work in the emulator. It also allows the removal of | |
489 | // writable globals from SEC, PEI_CORE (libraries), PEIMs | |
490 | // | |
491 | EmuMagicPage = (void *)(UINTN)FixedPcdGet64 (PcdPeiServicesTablePage); | |
492 | if (EmuMagicPage != NULL) { | |
493 | res3 = mmap ( | |
494 | (void *)EmuMagicPage, | |
495 | 4096, | |
496 | PROT_READ | PROT_WRITE, | |
497 | MAP_PRIVATE | MAP_ANONYMOUS, | |
498 | 0, | |
499 | 0 | |
500 | ); | |
501 | if (res3 != EmuMagicPage) { | |
502 | printf ("MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (long unsigned int)EmuMagicPage); | |
503 | return EFI_DEVICE_ERROR; | |
504 | } | |
505 | } | |
506 | ||
8052c4a2 | 507 | *Length = (UINT64) FileSize; |
508 | *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res; | |
509 | ||
510 | return EFI_SUCCESS; | |
511 | } | |
949f388f | 512 | |
513 | ||
949f388f | 514 | /*++ |
515 | ||
516 | Routine Description: | |
65e3f333 | 517 | This is the service to load the SEC Core from the Firmware Volume |
949f388f | 518 | |
519 | Arguments: | |
65e3f333 | 520 | LargestRegion - Memory to use for SEC. |
949f388f | 521 | LargestRegionSize - Size of Memory to use for PEI |
522 | BootFirmwareVolumeBase - Start of the Boot FV | |
65e3f333 | 523 | PeiCorePe32File - SEC PE32 |
949f388f | 524 | |
525 | Returns: | |
526 | Success means control is transfered and thus we should never return | |
527 | ||
528 | **/ | |
65e3f333 | 529 | VOID |
530 | SecLoadFromCore ( | |
531 | IN UINTN LargestRegion, | |
532 | IN UINTN LargestRegionSize, | |
533 | IN UINTN BootFirmwareVolumeBase, | |
534 | IN VOID *PeiCorePe32File | |
535 | ) | |
949f388f | 536 | { |
537 | EFI_STATUS Status; | |
538 | EFI_PHYSICAL_ADDRESS TopOfMemory; | |
539 | VOID *TopOfStack; | |
949f388f | 540 | EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint; |
949f388f | 541 | EFI_SEC_PEI_HAND_OFF *SecCoreData; |
542 | UINTN PeiStackSize; | |
543 | ||
544 | // | |
545 | // Compute Top Of Memory for Stack and PEI Core Allocations | |
546 | // | |
547 | TopOfMemory = LargestRegion + LargestRegionSize; | |
548 | PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1); | |
549 | ||
550 | // | |
551 | // |-----------| <---- TemporaryRamBase + TemporaryRamSize | |
552 | // | Heap | | |
553 | // | | | |
554 | // |-----------| <---- StackBase / PeiTemporaryMemoryBase | |
555 | // | | | |
556 | // | Stack | | |
557 | // |-----------| <---- TemporaryRamBase | |
558 | // | |
559 | TopOfStack = (VOID *)(LargestRegion + PeiStackSize); | |
560 | TopOfMemory = LargestRegion + PeiStackSize; | |
561 | ||
562 | // | |
563 | // Reservet space for storing PeiCore's parament in stack. | |
564 | // | |
565 | TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT); | |
566 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); | |
567 | ||
568 | ||
569 | // | |
570 | // Bind this information into the SEC hand-off state | |
571 | // | |
65e3f333 | 572 | SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack; |
949f388f | 573 | SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF); |
574 | SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase; | |
575 | SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdEmuFirmwareFdSize); | |
576 | SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; | |
577 | SecCoreData->TemporaryRamSize = STACK_SIZE; | |
578 | SecCoreData->StackBase = SecCoreData->TemporaryRamBase; | |
579 | SecCoreData->StackSize = PeiStackSize; | |
580 | SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize); | |
581 | SecCoreData->PeiTemporaryRamSize = STACK_SIZE - PeiStackSize; | |
582 | ||
583 | // | |
65e3f333 | 584 | // Find the SEC Core Entry Point |
949f388f | 585 | // |
65e3f333 | 586 | Status = SecPeCoffGetEntryPoint (PeiCorePe32File, (VOID **)&PeiCoreEntryPoint); |
949f388f | 587 | if (EFI_ERROR (Status)) { |
588 | return ; | |
589 | } | |
590 | ||
591 | // | |
65e3f333 | 592 | // Transfer control to the SEC Core |
949f388f | 593 | // |
594 | PeiSwitchStacks ( | |
595 | (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint, | |
596 | SecCoreData, | |
65e3f333 | 597 | (VOID *)gPpiList, |
949f388f | 598 | TopOfStack |
599 | ); | |
600 | // | |
65e3f333 | 601 | // If we get here, then the SEC Core returned. This is an error |
949f388f | 602 | // |
603 | return ; | |
604 | } | |
605 | ||
65e3f333 | 606 | |
949f388f | 607 | /*++ |
608 | ||
609 | Routine Description: | |
610 | This service is called from Index == 0 until it returns EFI_UNSUPPORTED. | |
611 | It allows discontiguous memory regions to be supported by the emulator. | |
612 | It uses gSystemMemory[] and gSystemMemoryCount that were created by | |
613 | parsing the host environment variable EFI_MEMORY_SIZE. | |
614 | The size comes from the varaible and the address comes from the call to | |
615 | UnixOpenFile. | |
616 | ||
617 | Arguments: | |
618 | Index - Which memory region to use | |
619 | MemoryBase - Return Base address of memory region | |
620 | MemorySize - Return size in bytes of the memory region | |
621 | ||
622 | Returns: | |
623 | EFI_SUCCESS - If memory region was mapped | |
624 | EFI_UNSUPPORTED - If Index is not supported | |
625 | ||
626 | **/ | |
65e3f333 | 627 | EFI_STATUS |
65e3f333 | 628 | SecUnixPeiAutoScan ( |
629 | IN UINTN Index, | |
630 | OUT EFI_PHYSICAL_ADDRESS *MemoryBase, | |
631 | OUT UINT64 *MemorySize | |
632 | ) | |
949f388f | 633 | { |
634 | void *res; | |
635 | ||
636 | if (Index >= gSystemMemoryCount) { | |
637 | return EFI_UNSUPPORTED; | |
638 | } | |
639 | ||
640 | *MemoryBase = 0; | |
65e3f333 | 641 | res = MapMemory ( |
642 | 0, gSystemMemory[Index].Size, | |
643 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
644 | MAP_PRIVATE | MAP_ANONYMOUS | |
645 | ); | |
646 | if (res == MAP_FAILED) { | |
949f388f | 647 | return EFI_DEVICE_ERROR; |
65e3f333 | 648 | } |
949f388f | 649 | *MemorySize = gSystemMemory[Index].Size; |
650 | *MemoryBase = (UINTN)res; | |
651 | gSystemMemory[Index].Memory = *MemoryBase; | |
652 | ||
653 | return EFI_SUCCESS; | |
654 | } | |
655 | ||
65e3f333 | 656 | |
1d7ac5a6 | 657 | /*++ |
658 | ||
659 | Routine Description: | |
660 | Check to see if an address range is in the EFI GCD memory map. | |
661 | ||
662 | This is all of GCD for system memory passed to DXE Core. FV | |
663 | mapping and other device mapped into system memory are not | |
664 | inlcuded in the check. | |
665 | ||
666 | Arguments: | |
667 | Index - Which memory region to use | |
668 | MemoryBase - Return Base address of memory region | |
669 | MemorySize - Return size in bytes of the memory region | |
670 | ||
671 | Returns: | |
672 | TRUE - Address is in the EFI GCD memory map | |
673 | FALSE - Address is NOT in memory map | |
674 | ||
675 | **/ | |
676 | BOOLEAN | |
677 | EfiSystemMemoryRange ( | |
678 | IN VOID *MemoryAddress | |
679 | ) | |
680 | { | |
681 | UINTN Index; | |
682 | EFI_PHYSICAL_ADDRESS MemoryBase; | |
683 | ||
684 | MemoryBase = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryAddress; | |
685 | for (Index = 0; Index < gSystemMemoryCount; Index++) { | |
686 | if ((MemoryBase >= gSystemMemory[Index].Memory) && | |
687 | (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)) ) { | |
688 | return TRUE; | |
689 | } | |
690 | } | |
691 | ||
692 | return FALSE; | |
693 | } | |
694 | ||
695 | ||
949f388f | 696 | /*++ |
697 | ||
698 | Routine Description: | |
699 | Since the SEC is the only Unix program in stack it must export | |
700 | an interface to do POSIX calls. gUnix is initailized in UnixThunk.c. | |
701 | ||
702 | Arguments: | |
703 | InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL); | |
704 | InterfaceBase - Address of the gUnix global | |
705 | ||
706 | Returns: | |
707 | EFI_SUCCESS - Data returned | |
708 | ||
709 | **/ | |
65e3f333 | 710 | VOID * |
65e3f333 | 711 | SecEmuThunkAddress ( |
712 | VOID | |
713 | ) | |
949f388f | 714 | { |
715 | return &gEmuThunkProtocol; | |
716 | } | |
717 | ||
718 | ||
949f388f | 719 | |
720 | RETURN_STATUS | |
721 | EFIAPI | |
722 | SecPeCoffGetEntryPoint ( | |
723 | IN VOID *Pe32Data, | |
724 | IN OUT VOID **EntryPoint | |
725 | ) | |
726 | { | |
65e3f333 | 727 | EFI_STATUS Status; |
728 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; | |
0ede3853 | 729 | |
730 | ZeroMem (&ImageContext, sizeof (ImageContext)); | |
731 | ImageContext.Handle = Pe32Data; | |
732 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead; | |
733 | ||
734 | Status = PeCoffLoaderGetImageInfo (&ImageContext); | |
735 | if (EFI_ERROR (Status)) { | |
736 | return Status; | |
737 | } | |
738 | ||
946bfba2 | 739 | if (ImageContext.ImageAddress != (UINTN)Pe32Data) { |
e7523e06 | 740 | // |
741 | // Relocate image to match the address where it resides | |
742 | // | |
743 | ImageContext.ImageAddress = (UINTN)Pe32Data; | |
744 | Status = PeCoffLoaderLoadImage (&ImageContext); | |
745 | if (EFI_ERROR (Status)) { | |
746 | return Status; | |
747 | } | |
949f388f | 748 | |
e7523e06 | 749 | Status = PeCoffLoaderRelocateImage (&ImageContext); |
750 | if (EFI_ERROR (Status)) { | |
751 | return Status; | |
752 | } | |
946bfba2 | 753 | } else { |
754 | // | |
755 | // Or just return image entry point | |
756 | // | |
757 | ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer (Pe32Data); | |
758 | Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint); | |
759 | if (EFI_ERROR (Status)) { | |
760 | return Status; | |
761 | } | |
762 | ImageContext.EntryPoint = (UINTN)*EntryPoint; | |
763 | } | |
949f388f | 764 | |
65e3f333 | 765 | // On Unix a dlopen is done that will change the entry point |
766 | SecPeCoffRelocateImageExtraAction (&ImageContext); | |
767 | *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint; | |
768 | ||
949f388f | 769 | return Status; |
770 | } | |
771 | ||
772 | ||
773 | ||
949f388f | 774 | /*++ |
775 | ||
776 | Routine Description: | |
777 | Return the FD Size and base address. Since the FD is loaded from a | |
778 | file into host memory only the SEC will know it's address. | |
779 | ||
780 | Arguments: | |
781 | Index - Which FD, starts at zero. | |
782 | FdSize - Size of the FD in bytes | |
783 | FdBase - Start address of the FD. Assume it points to an FV Header | |
784 | FixUp - Difference between actual FD address and build address | |
785 | ||
786 | Returns: | |
787 | EFI_SUCCESS - Return the Base address and size of the FV | |
788 | EFI_UNSUPPORTED - Index does nto map to an FD in the system | |
789 | ||
790 | **/ | |
65e3f333 | 791 | EFI_STATUS |
65e3f333 | 792 | SecUnixFdAddress ( |
793 | IN UINTN Index, | |
794 | IN OUT EFI_PHYSICAL_ADDRESS *FdBase, | |
795 | IN OUT UINT64 *FdSize, | |
796 | IN OUT EFI_PHYSICAL_ADDRESS *FixUp | |
797 | ) | |
949f388f | 798 | { |
799 | if (Index >= gFdInfoCount) { | |
800 | return EFI_UNSUPPORTED; | |
801 | } | |
802 | ||
803 | *FdBase = gFdInfo[Index].Address; | |
804 | *FdSize = gFdInfo[Index].Size; | |
805 | *FixUp = 0; | |
806 | ||
807 | if (*FdBase == 0 && *FdSize == 0) { | |
808 | return EFI_UNSUPPORTED; | |
809 | } | |
810 | ||
811 | if (Index == 0) { | |
812 | // | |
813 | // FD 0 has XIP code and well known PCD values | |
814 | // If the memory buffer could not be allocated at the FD build address | |
815 | // the Fixup is the difference. | |
816 | // | |
817 | *FixUp = *FdBase - PcdGet64 (PcdEmuFdBaseAddress); | |
818 | } | |
819 | ||
820 | return EFI_SUCCESS; | |
821 | } | |
822 | ||
949f388f | 823 | |
949f388f | 824 | /*++ |
825 | ||
826 | Routine Description: | |
827 | Count the number of seperators in String | |
828 | ||
829 | Arguments: | |
830 | String - String to process | |
831 | Seperator - Item to count | |
832 | ||
833 | Returns: | |
834 | Number of Seperator in String | |
835 | ||
836 | **/ | |
65e3f333 | 837 | UINTN |
838 | CountSeperatorsInString ( | |
839 | IN const CHAR16 *String, | |
840 | IN CHAR16 Seperator | |
841 | ) | |
949f388f | 842 | { |
843 | UINTN Count; | |
844 | ||
845 | for (Count = 0; *String != '\0'; String++) { | |
846 | if (*String == Seperator) { | |
847 | Count++; | |
848 | } | |
849 | } | |
850 | ||
851 | return Count; | |
852 | } | |
853 | ||
854 | ||
0ede3853 | 855 | EFI_STATUS |
7af94c1d | 856 | EFIAPI |
0ede3853 | 857 | SecImageRead ( |
858 | IN VOID *FileHandle, | |
859 | IN UINTN FileOffset, | |
860 | IN OUT UINTN *ReadSize, | |
861 | OUT VOID *Buffer | |
862 | ) | |
863 | /*++ | |
864 | ||
865 | Routine Description: | |
866 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file | |
867 | ||
868 | Arguments: | |
869 | FileHandle - The handle to the PE/COFF file | |
870 | FileOffset - The offset, in bytes, into the file to read | |
871 | ReadSize - The number of bytes to read from the file starting at FileOffset | |
872 | Buffer - A pointer to the buffer to read the data into. | |
873 | ||
874 | Returns: | |
875 | EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset | |
876 | ||
877 | **/ | |
878 | { | |
879 | CHAR8 *Destination8; | |
880 | CHAR8 *Source8; | |
881 | UINTN Length; | |
882 | ||
883 | Destination8 = Buffer; | |
884 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset); | |
885 | Length = *ReadSize; | |
886 | while (Length--) { | |
887 | *(Destination8++) = *(Source8++); | |
888 | } | |
889 | ||
890 | return EFI_SUCCESS; | |
891 | } | |
892 | ||
893 | ||
949f388f | 894 | /*++ |
895 | ||
896 | Routine Description: | |
897 | Store the ModHandle in an array indexed by the Pdb File name. | |
898 | The ModHandle is needed to unload the image. | |
899 | ||
900 | Arguments: | |
901 | ImageContext - Input data returned from PE Laoder Library. Used to find the | |
902 | .PDB file name of the PE Image. | |
903 | ModHandle - Returned from LoadLibraryEx() and stored for call to | |
904 | FreeLibrary(). | |
905 | ||
906 | Returns: | |
907 | EFI_SUCCESS - ModHandle was stored. | |
908 | ||
909 | **/ | |
65e3f333 | 910 | EFI_STATUS |
911 | AddHandle ( | |
912 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, | |
913 | IN VOID *ModHandle | |
914 | ) | |
949f388f | 915 | { |
916 | UINTN Index; | |
917 | IMAGE_CONTEXT_TO_MOD_HANDLE *Array; | |
918 | UINTN PreviousSize; | |
919 | ||
920 | ||
921 | Array = mImageContextModHandleArray; | |
922 | for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) { | |
923 | if (Array->ImageContext == NULL) { | |
924 | // | |
925 | // Make a copy of the stirng and store the ModHandle | |
926 | // | |
927 | Array->ImageContext = ImageContext; | |
928 | Array->ModHandle = ModHandle; | |
929 | return EFI_SUCCESS; | |
930 | } | |
931 | } | |
932 | ||
933 | // | |
934 | // No free space in mImageContextModHandleArray so grow it by | |
935 | // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will | |
936 | // copy the old values to the new locaiton. But it does | |
937 | // not zero the new memory area. | |
938 | // | |
939 | PreviousSize = mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE); | |
940 | mImageContextModHandleArraySize += MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE; | |
941 | ||
b8800fc5 | 942 | mImageContextModHandleArray = ReallocatePool ( |
943 | (mImageContextModHandleArraySize - 1) * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE), | |
944 | mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE), | |
945 | mImageContextModHandleArray | |
946 | ); | |
949f388f | 947 | if (mImageContextModHandleArray == NULL) { |
948 | ASSERT (FALSE); | |
949 | return EFI_OUT_OF_RESOURCES; | |
950 | } | |
951 | ||
952 | memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE)); | |
953 | ||
954 | return AddHandle (ImageContext, ModHandle); | |
955 | } | |
956 | ||
957 | ||
949f388f | 958 | /*++ |
959 | ||
960 | Routine Description: | |
961 | Return the ModHandle and delete the entry in the array. | |
962 | ||
963 | Arguments: | |
964 | ImageContext - Input data returned from PE Laoder Library. Used to find the | |
965 | .PDB file name of the PE Image. | |
966 | ||
967 | Returns: | |
968 | ModHandle - ModHandle assoicated with ImageContext is returned | |
969 | NULL - No ModHandle associated with ImageContext | |
970 | ||
971 | **/ | |
65e3f333 | 972 | VOID * |
973 | RemoveHandle ( | |
974 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
975 | ) | |
949f388f | 976 | { |
977 | UINTN Index; | |
978 | IMAGE_CONTEXT_TO_MOD_HANDLE *Array; | |
979 | ||
980 | if (ImageContext->PdbPointer == NULL) { | |
981 | // | |
982 | // If no PDB pointer there is no ModHandle so return NULL | |
983 | // | |
984 | return NULL; | |
985 | } | |
986 | ||
987 | Array = mImageContextModHandleArray; | |
988 | for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) { | |
989 | if (Array->ImageContext == ImageContext) { | |
990 | // | |
991 | // If you find a match return it and delete the entry | |
992 | // | |
993 | Array->ImageContext = NULL; | |
994 | return Array->ModHandle; | |
995 | } | |
996 | } | |
997 | ||
998 | return NULL; | |
999 | } | |
1000 | ||
1001 | ||
1002 | ||
949f388f | 1003 | BOOLEAN |
1004 | IsPdbFile ( | |
1005 | IN CHAR8 *PdbFileName | |
1006 | ) | |
1007 | { | |
1008 | UINTN Len; | |
1009 | ||
1010 | if (PdbFileName == NULL) { | |
1011 | return FALSE; | |
1012 | } | |
1013 | ||
1014 | Len = strlen (PdbFileName); | |
1015 | if ((Len < 5)|| (PdbFileName[Len - 4] != '.')) { | |
1016 | return FALSE; | |
1017 | } | |
1018 | ||
1019 | if ((PdbFileName[Len - 3] == 'P' || PdbFileName[Len - 3] == 'p') && | |
1020 | (PdbFileName[Len - 2] == 'D' || PdbFileName[Len - 2] == 'd') && | |
1021 | (PdbFileName[Len - 1] == 'B' || PdbFileName[Len - 1] == 'b')) { | |
1022 | return TRUE; | |
1023 | } | |
1024 | ||
1025 | return FALSE; | |
1026 | } | |
1027 | ||
1028 | ||
1029 | #define MAX_SPRINT_BUFFER_SIZE 0x200 | |
1030 | ||
1031 | void | |
1032 | PrintLoadAddress ( | |
1033 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
1034 | ) | |
1035 | { | |
1036 | if (ImageContext->PdbPointer == NULL) { | |
1037 | fprintf (stderr, | |
1038 | "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n", | |
1039 | (unsigned long)(ImageContext->ImageAddress), | |
1040 | (unsigned long)ImageContext->EntryPoint | |
1041 | ); | |
1042 | } else { | |
1043 | fprintf (stderr, | |
1044 | "0x%08lx Loading %s with entry point 0x%08lx\n", | |
1045 | (unsigned long)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders), | |
1046 | ImageContext->PdbPointer, | |
1047 | (unsigned long)ImageContext->EntryPoint | |
1048 | ); | |
1049 | } | |
1050 | // Keep output synced up | |
1051 | fflush (stderr); | |
1052 | } | |
1053 | ||
1054 | ||
0bc9421b | 1055 | /** |
1056 | Loads the image using dlopen so symbols will be automatically | |
1057 | loaded by gdb. | |
1058 | ||
1059 | @param ImageContext The PE/COFF image context | |
1060 | ||
1061 | @retval TRUE - The image was successfully loaded | |
1062 | @retval FALSE - The image was successfully loaded | |
1063 | ||
1064 | **/ | |
1065 | BOOLEAN | |
1066 | DlLoadImage ( | |
949f388f | 1067 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext |
1068 | ) | |
1069 | { | |
1070 | ||
1071 | #ifdef __APPLE__ | |
949f388f | 1072 | |
0bc9421b | 1073 | return FALSE; |
949f388f | 1074 | |
1075 | #else | |
1076 | ||
1077 | void *Handle = NULL; | |
1078 | void *Entry = NULL; | |
1079 | ||
1080 | if (ImageContext->PdbPointer == NULL) { | |
0bc9421b | 1081 | return FALSE; |
949f388f | 1082 | } |
1083 | ||
1084 | if (!IsPdbFile (ImageContext->PdbPointer)) { | |
0bc9421b | 1085 | return FALSE; |
949f388f | 1086 | } |
1087 | ||
65e3f333 | 1088 | fprintf ( |
1089 | stderr, | |
949f388f | 1090 | "Loading %s 0x%08lx - entry point 0x%08lx\n", |
1091 | ImageContext->PdbPointer, | |
1092 | (unsigned long)ImageContext->ImageAddress, | |
65e3f333 | 1093 | (unsigned long)ImageContext->EntryPoint |
1094 | ); | |
949f388f | 1095 | |
1096 | Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW); | |
f70c2a12 | 1097 | if (Handle != NULL) { |
949f388f | 1098 | Entry = dlsym (Handle, "_ModuleEntryPoint"); |
f70c2a12 | 1099 | AddHandle (ImageContext, Handle); |
949f388f | 1100 | } else { |
1101 | printf("%s\n", dlerror()); | |
1102 | } | |
1103 | ||
1104 | if (Entry != NULL) { | |
1105 | ImageContext->EntryPoint = (UINTN)Entry; | |
65e3f333 | 1106 | printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (unsigned long)Entry); |
0bc9421b | 1107 | return TRUE; |
1108 | } else { | |
1109 | return FALSE; | |
949f388f | 1110 | } |
1111 | ||
949f388f | 1112 | #endif |
0bc9421b | 1113 | } |
1114 | ||
1115 | ||
1116 | /** | |
1117 | Adds the image to a gdb script so it's symbols can be loaded. | |
1118 | The AddFirmwareSymbolFile helper macro is used. | |
1119 | ||
1120 | @param ImageContext The PE/COFF image context | |
1121 | ||
1122 | **/ | |
1123 | VOID | |
1124 | GdbScriptAddImage ( | |
1125 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
1126 | ) | |
1127 | { | |
0bc9421b | 1128 | |
1129 | PrintLoadAddress (ImageContext); | |
1130 | ||
1131 | if (ImageContext->PdbPointer != NULL && !IsPdbFile (ImageContext->PdbPointer)) { | |
1132 | FILE *GdbTempFile; | |
1133 | GdbTempFile = fopen (gGdbWorkingFileName, "a"); | |
1134 | if (GdbTempFile != NULL) { | |
1135 | long unsigned int SymbolsAddr = (long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders); | |
1136 | mScriptSymbolChangesCount++; | |
1137 | fprintf ( | |
1138 | GdbTempFile, | |
1139 | "AddFirmwareSymbolFile 0x%x %s 0x%08lx\n", | |
1140 | mScriptSymbolChangesCount, | |
1141 | ImageContext->PdbPointer, | |
1142 | SymbolsAddr | |
1143 | ); | |
1144 | fclose (GdbTempFile); | |
1145 | } else { | |
1146 | ASSERT (FALSE); | |
1147 | } | |
0bc9421b | 1148 | } |
949f388f | 1149 | } |
1150 | ||
1151 | ||
1152 | VOID | |
1153 | EFIAPI | |
0bc9421b | 1154 | SecPeCoffRelocateImageExtraAction ( |
1155 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
949f388f | 1156 | ) |
1157 | { | |
0bc9421b | 1158 | if (!DlLoadImage (ImageContext)) { |
1159 | GdbScriptAddImage (ImageContext); | |
1160 | } | |
1161 | } | |
949f388f | 1162 | |
949f388f | 1163 | |
0bc9421b | 1164 | /** |
1165 | Adds the image to a gdb script so it's symbols can be unloaded. | |
1166 | The RemoveFirmwareSymbolFile helper macro is used. | |
1167 | ||
1168 | @param ImageContext The PE/COFF image context | |
1169 | ||
1170 | **/ | |
1171 | VOID | |
1172 | GdbScriptRemoveImage ( | |
1173 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
1174 | ) | |
1175 | { | |
949f388f | 1176 | FILE *GdbTempFile; |
949f388f | 1177 | |
0bc9421b | 1178 | // |
1179 | // Need to skip .PDB files created from VC++ | |
1180 | // | |
1181 | if (IsPdbFile (ImageContext->PdbPointer)) { | |
1182 | return; | |
1183 | } | |
1184 | ||
949f388f | 1185 | // |
0bc9421b | 1186 | // Write the file we need for the gdb script |
949f388f | 1187 | // |
0bc9421b | 1188 | GdbTempFile = fopen (gGdbWorkingFileName, "a"); |
1189 | if (GdbTempFile != NULL) { | |
1190 | mScriptSymbolChangesCount++; | |
1191 | fprintf ( | |
1192 | GdbTempFile, | |
1193 | "RemoveFirmwareSymbolFile 0x%x %s\n", | |
1194 | mScriptSymbolChangesCount, | |
1195 | ImageContext->PdbPointer | |
1196 | ); | |
1197 | fclose (GdbTempFile); | |
1198 | } else { | |
1199 | ASSERT (FALSE); | |
1200 | } | |
0bc9421b | 1201 | } |
1202 | ||
949f388f | 1203 | |
0bc9421b | 1204 | VOID |
1205 | EFIAPI | |
1206 | SecPeCoffUnloadImageExtraAction ( | |
1207 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
1208 | ) | |
1209 | { | |
1210 | VOID *Handle; | |
1211 | ||
1212 | // | |
1213 | // Check to see if the image symbols were loaded with gdb script, or dlopen | |
1214 | // | |
1215 | Handle = RemoveHandle (ImageContext); | |
febb2276 | 1216 | if (Handle != NULL) { |
0bc9421b | 1217 | #ifndef __APPLE__ |
1218 | dlclose (Handle); | |
949f388f | 1219 | #endif |
0bc9421b | 1220 | return; |
1221 | } | |
1222 | ||
1223 | GdbScriptRemoveImage (ImageContext); | |
949f388f | 1224 | } |
1225 | ||
1226 |