)\r
{\r
DEBUG_AGENT_MAILBOX *Mailbox;\r
+ DEBUG_AGENT_MAILBOX *NewMailbox;\r
DEBUG_AGENT_MAILBOX MailboxInStack;\r
DEBUG_AGENT_PHASE2_CONTEXT Phase2Context;\r
DEBUG_AGENT_CONTEXT_POSTMEM_SEC *DebugAgentContext;\r
UINT64 DebugPortHandle;\r
UINT64 MailboxLocation;\r
UINT64 *MailboxLocationPointer;\r
+ EFI_PHYSICAL_ADDRESS Address; \r
\r
DisableInterrupts ();\r
\r
// Fix up Debug Port handle address and mailbox address\r
//\r
DebugAgentContext = (DEBUG_AGENT_CONTEXT_POSTMEM_SEC *) Context;\r
- DebugPortHandle = (UINT64)(UINT32)(Mailbox->DebugPortHandle + DebugAgentContext->StackMigrateOffset);\r
- UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
- Mailbox = (DEBUG_AGENT_MAILBOX *) ((UINTN) Mailbox + DebugAgentContext->StackMigrateOffset);\r
- MailboxLocation = (UINT64)(UINTN)Mailbox;\r
- //\r
- // Build mailbox location in HOB and fix-up its address\r
- //\r
- MailboxLocationPointer = BuildGuidDataHob (\r
- &gEfiDebugAgentGuid,\r
- &MailboxLocation,\r
- sizeof (UINT64)\r
- );\r
- MailboxLocationPointer = (UINT64 *) ((UINTN) MailboxLocationPointer + DebugAgentContext->HeapMigrateOffset);\r
+ if (DebugAgentContext != NULL) {\r
+ DebugPortHandle = (UINT64)(UINT32)(Mailbox->DebugPortHandle + DebugAgentContext->StackMigrateOffset);\r
+ UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
+ Mailbox = (DEBUG_AGENT_MAILBOX *) ((UINTN) Mailbox + DebugAgentContext->StackMigrateOffset);\r
+ MailboxLocation = (UINT64)(UINTN)Mailbox;\r
+ //\r
+ // Build mailbox location in HOB and fix-up its address\r
+ //\r
+ MailboxLocationPointer = BuildGuidDataHob (\r
+ &gEfiDebugAgentGuid,\r
+ &MailboxLocation,\r
+ sizeof (UINT64)\r
+ );\r
+ MailboxLocationPointer = (UINT64 *) ((UINTN) MailboxLocationPointer + DebugAgentContext->HeapMigrateOffset);\r
+ } else {\r
+ //\r
+ // DebugAgentContext is NULL. Then, Mailbox can directly be copied into memory.\r
+ // Allocate ACPI NVS memory for new Mailbox and Debug Port Handle buffer\r
+ //\r
+ Status = PeiServicesAllocatePages (\r
+ EfiACPIMemoryNVS,\r
+ EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
+ &Address\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
+ //\r
+ // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox\r
+ // and Debug Port Handle buffer in the allocated pool that may be marked as free by DXE Core after DXE Core\r
+ // reallocates the HOB.\r
+ //\r
+ CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
+ CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
+ UpdateMailboxContent (NewMailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, (UINT64)(UINTN)(NewMailbox + 1));\r
+ MailboxLocation = (UINT64)(UINTN)NewMailbox;\r
+ //\r
+ // Build mailbox location in HOB\r
+ //\r
+ MailboxLocationPointer = BuildGuidDataHob (\r
+ &gEfiDebugAgentGuid,\r
+ &MailboxLocation,\r
+ sizeof (UINT64)\r
+ );\r
+ }\r
//\r
// Update IDT entry to save the location saved mailbox pointer\r
//\r