/** @file
  AhciSmm.c -- AhciSmm

  Recovered naming surface from local module notes:
  AhciSmm.md / README.md

  Copyright (c) HR650X BIOS Decompilation Project
**/

#include "AhciSmm.h"

//
// Global storage recovered from AhciSmm.md "State Management" section
//
EFI_HANDLE            gImageHandle = NULL;             // 0x2E80
EFI_SYSTEM_TABLE      *gST = NULL;                    // 0x2E70
EFI_BOOT_SERVICES     *gBS = NULL;                    // 0x2E78
EFI_RUNTIME_SERVICES  *gRT = NULL;                    // 0x2E88
EFI_SMM_SYSTEM_TABLE2 *gSmst = NULL;                  // 0x2E90
VOID                  *mPcd = NULL;                   // 0x2EA0
VOID                  *gSmmCpuIo2Protocol = NULL;      // 0x2EB0
UINT8                 mSmmMode = 0;                   // 0x2EB8
VOID                  *gSmmServicesTableBase2 = NULL;  // 0x2EE8
VOID                  *gSmmChildProtocol = NULL;       // 0x2EC0
VOID                  *gDxeProtocol = NULL;            // 0x2ED8
VOID                  *gSmiHandlerProtocol = NULL;     // 0x2ED0
VOID                  *gDxeSmiHandlerProtocol = NULL;  // 0x2EF0
UINT8                 gSmmProtocolReady = 0;          // 0x2EC8
VOID                  *gBuf = NULL;                    // 0x2E68
UINT64                gAhciSavedPxClb = 0;            // 0x3010
UINT64                gAhciSavedPxFb = 0;             // 0x3018
UINTN                 gAhciReturnStatus = 0;           // 0x3008
UINT8                 gPortCompletionInProgress = 0;   // 0x2E60
UINT8                 gSoftResetInProgress = 0;        // 0x2E61
UINT32                gSmiCallbackHandle = 0;          // 0x3020
VOID                  *gAhciCommandTable = NULL;       // 0x3028
VOID                  *gAhciReceiveArea = NULL;        // 0x3030
VOID                  *gAhciCommandList = NULL;        // 0x3038

//
// Dispatch handler slots recovered from AhciSmm.md
//
AHCI_SMM_HANDLER pAhciPortInitHandler = NULL;         // 0x3070
AHCI_SMM_HANDLER pAhciDmaCommandHandler = NULL;       // 0x3078
AHCI_SMM_HANDLER pAhciPioDataInHandler = NULL;        // 0x3080
AHCI_SMM_HANDLER pAhciSoftwareResetHandler = NULL;    // 0x3088 (AhciAtapiSoftwareResetHandler, sub_1914)
AHCI_SMM_HANDLER pAhciNonDataCommandHandler = NULL;   // 0x3090 (AhciAtaNonDataCommandHandler, sub_1BB8)
AHCI_SMM_HANDLER pAhciFisReceiveHandler = NULL;       // 0x3098 (AhciCommandCompletionHandler, sub_1EBC)
AHCI_SMM_HANDLER pAhciSoftResetHandler = NULL;        // 0x30A0 (AhciPortSoftResetThunk, sub_1F04)

//
// Recovered semantic aliases for local decompiled symbols:
// sub_49C -> AhciSmmPreInit: boot/runtime context setup
// sub_5DC -> AhciSmmRegisterSmiHandlers: entry dispatch and SMI registration
// sub_2150 -> AhciSmmInitialize (not yet emitted in this recovery slice)
// sub_1F0C -> AhciSmmInstallSmiCallback (not yet emitted in this recovery slice)
//
EFI_STATUS
EFIAPI
AhciSmmPreInit(
  VOID
  )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
AhciSmmRegisterSmiHandlers(
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  return sub_5DC(ImageHandle, SystemTable);
}

//
// Function: _ModuleEntryPoint (0x470)
// UEFI/SMM driver entry point. Initializes globals then executes main
// SMM dispatch/registration flow.
//
EFI_STATUS
EFIAPI
_ModuleEntryPoint(
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  sub_49C();
  return sub_5DC(ImageHandle, SystemTable);
}

//
// Compatibility entry point expected by existing module lists.
//
EFI_STATUS
EFIAPI
ModuleEntryPoint(
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  return _ModuleEntryPoint(ImageHandle, SystemTable);
}

//
// Raw decompiler symbols retained for reference.
//
EFI_STATUS
EFIAPI
sub_49C(
  VOID
  )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
sub_5DC(
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  return EFI_SUCCESS;
}
