/** Write the specified number of bytes to serial device. @param[in] This Pointer to EFI_SERIAL_IO_PROTOCOL. @param[in, out] BufferSize On input the size of Buffer, on output the amount of data actually written. @param[in] Buffer The buffer of data to write. @retval EFI_SUCCESS The data were written successfully. @retval EFI_DEVICE_ERROR The device reported an error. @retval EFI_TIMEOUT The write operation was stopped due to timeout. **/ EFI_STATUS EFIAPI SerialWrite ( IN EFI_SERIAL_IO_PROTOCOL *This, IN OUT UINTN *BufferSize, IN VOID *Buffer ) { DEBUG_PORT_HANDLE Handle; Handle = GetDebugPortHandle (); if ((mSerialIoMode.ControlMask & EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE) != 0) { if (*BufferSize == 0) { return EFI_SUCCESS; } if ((mLoopbackBuffer & SERIAL_PORT_LOOPBACK_BUFFER_FULL) != 0) { *BufferSize = 0; return EFI_TIMEOUT; } mLoopbackBuffer = SERIAL_PORT_LOOPBACK_BUFFER_FULL | *(UINT8 *)Buffer; *BufferSize = 1; } else { *BufferSize = DebugPortWriteBuffer (Handle, Buffer, *BufferSize); } return EFI_SUCCESS; }
/** Write the specified number of bytes to serial device. @param[in] This Pointer to EFI_SERIAL_IO_PROTOCOL. @param[in, out] BufferSize On input the size of Buffer, on output the amount of data actually written. @param[in] Buffer The buffer of data to write. @retval EFI_SUCCESS The data were written successfully. @retval EFI_DEVICE_ERROR The device reported an error. @retval EFI_TIMEOUT The write operation was stopped due to timeout. **/ EFI_STATUS EFIAPI SerialWrite ( IN EFI_SERIAL_IO_PROTOCOL *This, IN OUT UINTN *BufferSize, IN VOID *Buffer ) { DEBUG_PORT_HANDLE Handle; BOOLEAN DebugTimerInterruptState; EFI_TPL Tpl; // // Raise TPL to prevent recursion from EFI timer interrupts // Tpl = gBS->RaiseTPL (TPL_NOTIFY); // // Save and disable Debug Timer interrupt to avoid it to access Debug Port // DebugTimerInterruptState = SaveAndSetDebugTimerInterrupt (FALSE); Handle = GetDebugPortHandle (); if ((mSerialIoMode.ControlMask & EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE) != 0) { if (*BufferSize == 0) { return EFI_SUCCESS; } if ((mLoopbackBuffer & SERIAL_PORT_LOOPBACK_BUFFER_FULL) != 0) { *BufferSize = 0; return EFI_TIMEOUT; } mLoopbackBuffer = SERIAL_PORT_LOOPBACK_BUFFER_FULL | *(UINT8 *)Buffer; *BufferSize = 1; } else { *BufferSize = DebugPortWriteBuffer (Handle, Buffer, *BufferSize); } // // Restore Debug Timer interrupt // SaveAndSetDebugTimerInterrupt (DebugTimerInterruptState); // // Restore to original TPL // gBS->RestoreTPL (Tpl); return EFI_SUCCESS; }
/** Initialize debug agent. This function is used to set up debug enviroment for source level debug in SMM code. If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries and initialize debug port. It will get debug agent Mailbox from GUIDed HOB, it it exists, debug agent wiil copied it into the local Mailbox in SMM space. it will overirde IDT table entries and initialize debug port. Context will be NULL. If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug Registers and get local Mailbox in SMM space. Context will be NULL. If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug Registers. Context will be NULL. @param[in] InitFlag Init flag is used to decide initialize process. @param[in] Context Context needed according to InitFlag. @param[in] Function Continue function called by debug agent library; it was optional. **/ VOID EFIAPI InitializeDebugAgent ( IN UINT32 InitFlag, IN VOID *Context, OPTIONAL IN DEBUG_AGENT_CONTINUE Function OPTIONAL ) { EFI_STATUS Status; UINT64 DebugPortHandle; IA32_IDT_GATE_DESCRIPTOR IdtEntry[33]; IA32_DESCRIPTOR IdtDescriptor; IA32_DESCRIPTOR *Ia32Idtr; IA32_IDT_ENTRY *Ia32IdtEntry; IA32_DESCRIPTOR Idtr; UINT16 IdtEntryCount; DEBUG_AGENT_MAILBOX *Mailbox; UINT64 *MailboxLocation; UINT32 DebugTimerFrequency; BOOLEAN PeriodicMode; UINTN TimerCycle; switch (InitFlag) { case DEBUG_AGENT_INIT_SMM: // // Install configuration table for persisted vector handoff info // Status = gSmst->SmmInstallConfigurationTable ( gSmst, &gEfiVectorHandoffTableGuid, (VOID *) &mVectorHandoffInfoDebugAgent[0], sizeof (EFI_VECTOR_HANDOFF_INFO) * mVectorHandoffInfoCount ); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n")); CpuDeadLoop (); } // // Check if Debug Agent initialized in DXE phase // Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &Mailbox); if (Status == EFI_SUCCESS && Mailbox != NULL) { VerifyMailboxChecksum (Mailbox); mMailboxPointer = Mailbox; break; } // // Check if Debug Agent initialized in SEC/PEI phase // Mailbox = GetMailboxFromHob (); if (Mailbox != NULL) { mMailboxPointer = Mailbox; break; } // // Debug Agent was not initialized before, use the local mailbox. // ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX)); Mailbox = &mLocalMailbox; // // Save original IDT entries // AsmReadIdtr (&IdtDescriptor); CopyMem (&IdtEntry, (VOID *)IdtDescriptor.Base, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR)); // // Initialized Debug Agent // InitializeDebugIdt (); // // Initialize Debug Timer hardware and save its frequency // InitializeDebugTimer (&DebugTimerFrequency, TRUE); UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency); DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle, NULL); UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle); mMailboxPointer = Mailbox; // // Trigger one software interrupt to inform HOST // TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE); SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1); // // Memory has been ready // if (IsHostAttached ()) { // // Trigger one software interrupt to inform HOST // TriggerSoftInterrupt (MEMORY_READY_SIGNATURE); } // // Find and report PE/COFF image info to HOST // FindAndReportModuleImageInfo (SIZE_4KB); // // Restore saved IDT entries // CopyMem ((VOID *)IdtDescriptor.Base, &IdtEntry, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR)); break; case DEBUG_AGENT_INIT_ENTER_SMI: SaveDebugRegister (); if (!mSmmDebugIdtInitFlag) { // // We only need to initialize Debug IDT table at first SMI entry // after SMM relocation. // InitializeDebugIdt (); mSmmDebugIdtInitFlag = TRUE; } // // Check if CPU APIC Timer is working, otherwise initialize it. // InitializeLocalApicSoftwareEnable (TRUE); GetApicTimerState (NULL, &PeriodicMode, NULL); TimerCycle = GetApicTimerInitCount (); if (!PeriodicMode || TimerCycle == 0) { InitializeDebugTimer (NULL, FALSE); } Mailbox = GetMailboxPointer (); if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) { // // If Debug Agent has been communicaton state with HOST, we need skip // any break points set in SMM, set Skip Breakpoint flag // mSkipBreakpoint = TRUE; } if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI) == 1) { if (mSkipBreakpoint) { // // Print warning message if ignore smm entry break // DebugPortWriteBuffer ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle, (UINT8 *)mWarningMsgIgnoreSmmEntryBreak, AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak) ); } else { // // If SMM entry break is set, SMM code will be break at here. // CpuBreakpoint (); } } break; case DEBUG_AGENT_INIT_EXIT_SMI: Mailbox = GetMailboxPointer (); // // Clear Skip Breakpoint flag // mSkipBreakpoint = FALSE; RestoreDebugRegister (); break; case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64: if (Context == NULL) { DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n")); CpuDeadLoop (); } else { Ia32Idtr = (IA32_DESCRIPTOR *) Context; Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base); MailboxLocation = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow + (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16)); mMailboxPointer = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation); VerifyMailboxChecksum (mMailboxPointer); // // Get original IDT address and size. // AsmReadIdtr ((IA32_DESCRIPTOR *) &Idtr); IdtEntryCount = (UINT16) ((Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR)); if (IdtEntryCount < 33) { Idtr.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * 33 - 1); Idtr.Base = (UINTN) &mIdtEntryTable; ZeroMem (&mIdtEntryTable, Idtr.Limit + 1); AsmWriteIdtr ((IA32_DESCRIPTOR *) &Idtr); } InitializeDebugIdt (); // // Initialize Debug Timer hardware and save its frequency // InitializeDebugTimer (&DebugTimerFrequency, TRUE); UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency); // // Enable Debug Timer interrupt and CPU interrupt // SaveAndSetDebugTimerInterrupt (TRUE); EnableInterrupts (); FindAndReportModuleImageInfo (SIZE_4KB); } break; default: // // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this // Debug Agent library instance. // DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n")); CpuDeadLoop (); break; } }