Newer
Older
AMI-Aptio-BIOS-Reversed / UsbOcUpdateDxeLightningRidgeEXRP / UsbOcUpdateDxeLightningRidgeEXRP.h
@Ajax Dong Ajax Dong 2 days ago 15 KB Init
/**
 * @file UsbOcUpdateDxeLightningRidgeEXRP.h
 *
 * @brief UsbOcUpdateDxeLightningRidgeEXRP - UEFI DXE driver for USB over-current
 *        (OC) protection configuration on the LightningRidgeEXRP platform.
 *
 * MODULE TYPE: DXE Driver (Index 0003 in BIOS FFS)
 * UEFI PHASE:  DXE (Driver Execution Environment)
 *
 * PURPOSE:
 *   This driver is part of Lenovo's UBA (Unified Board Architecture) framework.
 *   It configures USB over-current protection settings for the LightningRidgeEXRP
 *   platform. At entry it:
 *     1. Locates the HOB (Hand-Off Block) list from the UEFI System Table's
 *        configuration table array by matching the EFI_HOB_LIST_GUID.
 *     2. Locates a UBA board-type protocol (shared across platforms) using
 *        gBS->LocateProtocol().
 *     3. Registers USB OC configuration data via the protocol's registration
 *        function, passing a UBA_USBOC_CONFIG_DATA structure (signature "PUSB",
 *        version 2, 0x48c bytes total config size).
 *     4. Provides debug output via the UEFI DebugLib protocol accessed through
 *        the UEFI Boot Services table (gBS).
 *
 * This module is analogous to RomLayoutDxe (NeonCityFPGA), but instead of
 * registering a "PSET" setup configuration, it registers a "PUSB" USB over-current
 * configuration for the LightningRidgeEXRP platform.
 *
 * DEPENDENCIES (Protocols consumed):
 *   - UBA Board-Type Protocol (GUID: e03e0d46-5263-4845-b0a4-58d57b3177e2)
 *     Shared protocol across all UBA platform variants. Located to register
 *     the USB OC configuration.
 *   - EFI_HOB_LIST_GUID (7739f24c-93d7-11d4-9a3a-0090273fc14d)
 *     Used to locate the HOB list from the system configuration table.
 *   - UEFI Debug Library Protocol (implicit via gBS)
 *
 * DEPENDENCIES (Protocols produced):
 *   - UBA LightningRidgeEXRP USB OC Config Protocol
 *     (GUID: 2638009e-3850-4e4b-b05d-042a32dbb9d1)
 *     Registered with a UBA_USBOC_CONFIG_DATA interface.
 *
 * HARDWARE ACCESS:
 *   - CMOS ports 0x70/0x71: Read debug level setting mask from CMOS register 0x4B
 *   - MMIO 0xFDAF0490: Board configuration register (fallback debug level source)
 *
 * LIBRARY DEPENDENCIES:
 *   - UefiBootServicesTableLib (gImageHandle, gST, gBS)
 *   - UefiRuntimeServicesTableLib (gRT)
 *   - DxeHobLib (HOB list parsing)
 *   - BaseLib (unaligned read: ReadUnaligned64)
 *   - DebugLib (DEBUG macro via CmosDebugLevelProvider)
 */

#ifndef _USB_OC_UPDATE_DXE_LIGHTNINGRIDGE_EXRP_H_
#define _USB_OC_UPDATE_DXE_LIGHTNINGRIDGE_EXRP_H_

#include "../uefi_headers/Uefi.h"

// ============================================================================
// GUID Definitions
// ===========================================================================/

///
/// EFI_HOB_LIST_GUID - GUID used to locate the HOB list in the system
/// configuration table.
/// {7739F24C-93D7-11D4-9A3A-0090273FC14D}
///
#define EFI_HOB_LIST_GUID \
  { 0x7739F24C, 0x93D7, 0x11D4, { 0x9A, 0x3A, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D } }

///
/// UBA_BOARD_TYPE_PROTOCOL_GUID
/// Protocol GUID used with gBS->LocateProtocol() to obtain the UBA board-type
/// protocol interface. This is the SAME GUID used by RomLayoutDxe for the
/// NeonCityFPGA platform, indicating a shared protocol across all UBA variants.
/// The platform differentiation is done through the config data content,
/// not the protocol GUID.
/// {E03E0D46-5263-4845-B0A4-58D57B3177E2}
///
#define UBA_BOARD_TYPE_PROTOCOL_GUID \
  { 0xE03E0D46, 0x5263, 0x4845, { 0xB0, 0xA4, 0x58, 0xD5, 0x7B, 0x31, 0x77, 0xE2 } }

///
/// UBA_LIGHTNINGRIDGEEXRP_USBOC_CONFIG_PROTOCOL_GUID
/// Protocol GUID that is registered by this driver during its entry point.
/// This protocol is installed via the board-type protocol's registration
/// function and represents the USB OC configuration for LightningRidgeEXRP.
/// {2638009E-3850-4E4B-B05D-042A32DBB9D1}
///
#define UBA_LIGHTNINGRIDGEEXRP_USBOC_CONFIG_PROTOCOL_GUID \
  { 0x2638009E, 0x3850, 0x4E4B, { 0xB0, 0x5D, 0x04, 0x2A, 0x32, 0xDB, 0xB9, 0xD1 } }

// ============================================================================
// Structure Definitions
// ===========================================================================/

///
/// UBA_USBOC_CONFIG_DATA
/// Structure passed to the UBA board-type protocol to register the USB
/// over-current configuration data for the LightningRidgeEXRP platform.
///
/// The signature is "PUSB" (little-endian: 'P','U','S','B'), which is "BSUP"
/// in big-endian -- likely derived from "USB Port" or similar naming.
///
typedef struct {
  ///
  /// Signature = "PUSB" (0x42535550 in little-endian).
  ///
  UINT32    Signature;
  ///
  /// Version of this structure. Currently = 2.
  ///
  UINT32    Version;
  ///
  /// Total size of the USB OC configuration data blob. Size = 0x48c (1164) bytes.
  /// This matches the PSET config size from RomLayoutDxe, suggesting a shared
  /// config blob format across UBA module types.
  ///
  UINT64    Size;
} UBA_USBOC_CONFIG_DATA;

///
/// UBA_USBOC_PORT_MAP_ENTRY
/// Structure describing one USB port's over-current (OC) mapping.
/// These entries define how USB ports relate to OC sense pins on the
/// LightningRidgeEXRP platform's USB controller.
///
/// The module has 4 entries in its port mapping table at 0xBA0-0xBD0.
///
typedef struct {
  ///
  /// Offset within the USB controller's register space for this port's
  /// OC configuration.
  ///
  UINT32    ControllerOffset;
  ///
  /// USB port number (1-based or hardware port index).
  ///
  UINT32    UsbPortNumber;
  ///
  /// OC sense pin number or bitmask used to detect over-current conditions
  /// on this port.
  ///
  UINT32    OcPinNumber;
  ///
  /// Configuration flags for this port entry. Semantics are platform-specific.
  ///
  UINT32    Flags;
} UBA_USBOC_PORT_MAP_ENTRY;

///
/// Number of USB OC port mapping entries in the static configuration.
///
#define USB_OC_PORT_MAP_ENTRY_COUNT  4

///
/// UBA_USBOC_CONFIG_FIXED_SIZE
/// The fixed size of the configuration data associated with this module
/// (matching the "PUSB" header's Size field).
///
#define UBA_USBOC_CONFIG_FIXED_SIZE  0x48c

///
/// UBA_USBOC_CONFIG_VERSION
/// Current version of the UBA USB OC config structure.
///
#define UBA_USBOC_CONFIG_VERSION     0x2

///
/// CMOS debug level register. Port 0x70 selects the CMOS register;
/// port 0x71 reads/writes the value.
/// Register 0x4B is used for debug level control.
///
#define CMOS_DEBUG_LEVEL_REGISTER    0x4B

///
/// CMOS index port
///
#define RTC_INDEX_PORT               0x70

///
/// CMOS data port
///
#define RTC_DATA_PORT                0x71

///
/// Board configuration MMIO register address (used for debug level fallback).
///
#define BOARD_CONFIG_MMIO_ADDR       0xFDAF0490ULL

///
/// Debug message severity mask. Used with the DebugLib protocol.
///
#define DEBUG_INFO                   0x80000000

// ============================================================================
// Global Variable Declarations
// ===========================================================================/

//
// UEFI Boot Services Table pointer (cached by UefiBootServicesTableLib).
//
extern EFI_SYSTEM_TABLE   *gST;
extern EFI_BOOT_SERVICES  *gBS;
extern EFI_RUNTIME_SERVICES *gRT;
extern EFI_HANDLE         gImageHandle;

///
/// Pointer to the HOB list, obtained from the system configuration table
/// by searching for the EFI_HOB_LIST_GUID entry (cached at qword_C78).
///
extern VOID               *gHobList;

// ============================================================================
// Function Declarations
// ===========================================================================/

/**
 * Module entry point for UsbOcUpdateDxeLightningRidgeEXRP.
 *
 * Initializes UEFI global variables (gImageHandle, gST, gBS, gRT), locates the
 * HOB list via GetHobList(), prints a debug banner via DebugPrint(), locates
 * the UBA board-type protocol, and registers the USB OC configuration data
 * by calling the protocol's registration function at offset +0x10.
 *
 * @param[in] ImageHandle  The firmware-allocated handle for this driver image.
 * @param[in] SystemTable  A pointer to the EFI System Table.
 *
 * @return EFI_SUCCESS     The USB OC config protocol was registered.
 * @return Other           Returned directly from LocateProtocol if the UBA
 *                         board-type protocol is not available.
 *
 * @note 0x42e: Local Interface variable is zero-initialized on stack.
 * @note 0x440: DebugPrint(0x80000000, "UBA:UsbOcUpdate-TypeLightningRidgeEXRP\n")
 * @note 0x45a: gBS->LocateProtocol(&mUbaProtocolGuid, NULL, &Interface)
 * @note 0x481: Interface->RegisterUsbOcConfig(Interface, &mUsbOcConfigGuid,
 *               &mUsbOcConfigHdr, sizeof(UBA_USBOC_CONFIG_DATA))
 */
EFI_STATUS
EFIAPI
_ModuleEntryPoint (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  );

/**
 * Provides USB OC configuration data pointer set.
 *
 * NOTE: This function has no callers in this module (it is dead code).
 * It returns pointers to the three USB OC configuration data blocks:
 *   - &mUsbOcPortMap (0xBA0): USB OC port mapping table
 *   - &mUsbOcConfigData (0xBF0): Additional USB OC configuration data
 *   - &mUsbOcPortDesc (0xC18): USB OC port descriptor array
 *
 * May be intended as a callback or for external use via a function pointer.
 *
 * @param[out] a1  Pointer to receive address of USB OC port mapping table.
 * @param[out] a2  Pointer to receive address of USB OC config data.
 * @param[out] a3  Pointer to receive address of USB OC descriptor array.
 *
 * @return 0 (EFI_SUCCESS).
 */
UINTN
UsbOcGetConfig (
  OUT VOID  **a1,
  OUT VOID  **a2,
  OUT VOID  **a3
  );

/**
 * Locates the HOB (Hand-Off Block) list from the UEFI System Table's
 * configuration table array.
 *
 * Iterates through SystemTable->ConfigurationTable[] looking for an entry
 * whose VendorGuid matches EFI_HOB_LIST_GUID (7739f24c-93d7-11d4-9a3a-0090273fc14d).
 * The comparison is done by matching the first 8 bytes and second 8 bytes of
 * the GUID separately.
 *
 * Results are cached in the global variable qword_C78 (gHobList).
 *
 * @param[in] ImageHandle  The driver image handle (passed through from entry,
 *                         may be unused).
 *
 * @return Pointer to the HOB list, or NULL if not found.
 */
VOID *
GetHobList (
  IN EFI_HANDLE  ImageHandle
  );

/**
 * Compares a GUID against the EFI_HOB_LIST_GUID by comparing its first 8 bytes
 * and second 8 bytes independently.
 *
 * Uses ReadUnaligned64() to perform the comparison.
 *
 * @param[in] ImageHandle  Unused parameter.
 * @param[in] GuidPtr      Pointer to the GUID to compare.
 *
 * @retval TRUE   The GUID matches EFI_HOB_LIST_GUID.
 * @retval FALSE  The GUID does not match.
 */
BOOLEAN
IsHobListGuid (
  IN EFI_HANDLE  ImageHandle,
  IN EFI_GUID    *GuidPtr
  );

/**
 * Debug print function (wraps the UEFI DebugLib protocol).
 *
 * Resolves the DebugLib protocol interface via the UEFI Boot Services table
 * and calls its output function if the debug level determined by CMOS register
 * 0x4B matches the requested severity mask.
 *
 * @param[in] ErrorLevel  The debug error level mask (e.g., DEBUG_INFO = 0x80000000).
 * @param[in] Format      A format string for the debug message.
 * @param[in] ...         Variable arguments for the format string.
 *
 * @return The return value from the DebugLib protocol's output function,
 *         or 0 if the protocol is not available.
 */
UINTN
EFIAPI
DebugPrint (
  IN UINTN       ErrorLevel,
  IN CONST CHAR8 *Format,
  ...
  );

/**
 * ASSERT assertion failure handler.
 *
 * Called when a runtime assertion fails. Resolves the DebugLib protocol
 * and calls its assertion failure handler.
 *
 * @param[in] FileName     Source file name where the assertion occurred.
 * @param[in] LineNumber   Line number of the assertion.
 * @param[in] Description  Description of the failed assertion.
 *
 * @return 0 if the DebugLib protocol is not available.
 */
UINTN
DebugAssert (
  IN CONST CHAR8  *FileName,
  IN UINTN        LineNumber,
  IN CONST CHAR8  *Description
  );

/**
 * Reads an unaligned 64-bit value from memory.
 *
 * Wraps the BaseLib ReadUnaligned64() function with a NULL pointer check.
 *
 * @param[in] Buffer  Pointer to the memory to read. Must not be NULL.
 *
 * @return The 64-bit value read from the given address.
 */
UINT64
ReadUnaligned64 (
  IN CONST VOID  *Buffer
  );

// ============================================================================
// UBA Board-Type Protocol Interface
// ===========================================================================/

///
/// UBA_BOARD_TYPE_PROTOCOL
///
/// The protocol interface obtained via gBS->LocateProtocol() using the GUID
/// UBA_BOARD_TYPE_PROTOCOL_GUID. This protocol is shared across all UBA
/// platform variants (NeonCityFPGA, LightningRidgeEXRP, etc.).
///
/// This protocol is used to register platform-specific configuration data.
/// The registration function is at offset 0x10 within the interface structure.
/// The assert handler is at offset 0x08.
///
typedef struct {
  ///
  /// Unknown field at offset 0x00 (may contain protocol revision or header).
  ///
  UINT64   Reserved0;
  ///
  /// Function at offset 0x08. Debug assertion handler.
  /// Called from DebugAssert() when an assertion fires.
  ///
  /// This matches the signature: UINTN (EFIAPI *DebugAssert)(
  ///   IN CONST CHAR8  *FileName,
  ///   IN UINTN        LineNumber,
  ///   IN CONST CHAR8  *Description
  ///   );
  ///
  UINT64   DebugAssertHandler;
  ///
  /// Function at offset 0x10. Registers the USB OC configuration data.
  ///
  /// @param[in] This            Pointer to the UBA board-type protocol interface.
  /// @param[in] ConfigGuid      GUID of the protocol to register
  ///                            (UBA_LIGHTNINGRIDGEEXRP_USBOC_CONFIG_PROTOCOL_GUID).
  /// @param[in] ConfigData      Pointer to the UBA_USBOC_CONFIG_DATA structure.
  /// @param[in] ConfigDataSize  Size of the configuration data structure in bytes
  ///                            (16 = sizeof(UBA_USBOC_CONFIG_DATA)).
  ///
  /// @return EFI_STATUS.
  ///
  EFI_STATUS
  (EFIAPI *RegisterUsbOcConfig) (
    IN VOID                    *This,
    IN EFI_GUID                *ConfigGuid,
    IN UBA_USBOC_CONFIG_DATA   *ConfigData,
    IN UINT64                  ConfigDataSize
    );
} UBA_BOARD_TYPE_PROTOCOL;

///
/// DEBUGLIB_PROTOCOL
///
/// The protocol interface for the UEFI Debug Library protocol, obtained via
/// gBS->LocateProtocol(). Lazily resolved and cached by GetDebugProtocol().
///
typedef struct {
  ///
  /// Function at offset 0x00. Debug print/output function.
  ///
  /// @param[in] ErrorLevel  Debug error level mask.
  /// @param[in] Format      Format string.
  /// @param[in] VaList      Variable argument list.
  ///
  /// @return Number of characters output, or implementation-defined value.
  ///
  UINTN
  (EFIAPI *DebugPrint) (
    IN UINTN       ErrorLevel,
    IN CONST CHAR8 *Format,
    IN VA_LIST     VaList
    );
  ///
  /// Function at offset 0x08. Debug assertion handler.
  ///
  /// @param[in] FileName     Source file name.
  /// @param[in] LineNumber   Line number of the assertion.
  /// @param[in] Description  Description of the failed assertion.
  ///
  /// @return Implementation-defined (typically 0 or an abort status).
  ///
  UINTN
  (EFIAPI *DebugAssert) (
    IN CONST CHAR8  *FileName,
    IN UINTN        LineNumber,
    IN CONST CHAR8  *Description
    );
} DEBUGLIB_PROTOCOL;

#endif /* _USB_OC_UPDATE_DXE_LIGHTNINGRIDGE_EXRP_H_ */