# Path: dbginterface.py
# def LocalKernelDebugger(quiet=True):
#     """| Check that all conditions to Local Kernel Debugging are met
#        | and return a LKD (subclass of :class:`LocalKernelDebuggerBase`
#     """
#     if not windows.utils.check_debug():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging on kernel not in DEBUG mode")
#     if not windows.utils.check_is_elevated():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from non-Admin process")
#     windows.utils.enable_privilege(SE_DEBUG_NAME, True)
#     if windows.system.bitness == 64:
#         if windows.current_process.is_wow_64:
#             raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from SysWow64 process (please launch from 64bits python)")
#         return LocalKernelDebugger64(quiet)
#     return LocalKernelDebugger32(quiet)


import os
import sys
from dbginterface import LocalKernelDebugger
"""A simple demonstration of LKD to read data from a system bus using any custom
driver. Here we read the BIOS_CNTL (bus = 0x00, device = 0x1F, function = 0x00, 
offset = 0xDC) PCI register which is in charge of protecting the SPI Flash, 
containing the BIOS, on the Platform Controller HUB (PCH)"""

if os.getcwd().endswith("example"):
    sys.path.append(os.path.realpath(".."))
else:
    sys.path.append(os.path.realpath("."))
    

# Indicates PCI configuration space.
# Value from _BUS_DATA_TYPE
# https://msdn.microsoft.com/en-us/library/windows/hardware/ff540700(v=vs.85).aspx
PCIConfiguration = 4
# LPC device PCI configuration space
bus_num = 0
device_num = 0x1F
function_num = 0
# BIOS_CNTL register
offset_bios_cntl = 0xDC

SMM_BWP_mask = 1 << 5   # 0x20
BLE_mask = 1 << 1       # 0x2
BIOSWE_mask = 1 << 0    # 0x1

if __name__ == '__main__':

# Path: dbginterface.py
# def LocalKernelDebugger(quiet=True):
#     """| Check that all conditions to Local Kernel Debugging are met
#        | and return a LKD (subclass of :class:`LocalKernelDebuggerBase`
#     """
#     if not windows.utils.check_debug():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging on kernel not in DEBUG mode")
#     if not windows.utils.check_is_elevated():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from non-Admin process")
#     windows.utils.enable_privilege(SE_DEBUG_NAME, True)
#     if windows.system.bitness == 64:
#         if windows.current_process.is_wow_64:
#             raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from SysWow64 process (please launch from 64bits python)")
#         return LocalKernelDebugger64(quiet)
#     return LocalKernelDebugger32(quiet)


# Path: example/smm_check.py
import os
import sys
from dbginterface import LocalKernelDebugger

"""A simple demonstration of LKD to read data from a system bus using any custom
driver. Here we read the BIOS_CNTL (bus = 0x00, device = 0x1F, function = 0x00, 
offset = 0xDC) PCI register which is in charge of protecting the SPI Flash, 
containing the BIOS, on the Platform Controller HUB (PCH)"""

if os.getcwd().endswith("example"):
    sys.path.append(os.path.realpath(".."))
else:
    sys.path.append(os.path.realpath("."))
    

# Indicates PCI configuration space.
# Value from _BUS_DATA_TYPE
# https://msdn.microsoft.com/en-us/library/windows/hardware/ff540700(v=vs.85).aspx
PCIConfiguration = 4
# LPC device PCI configuration space
bus_num = 0
device_num = 0x1F
function_num = 0
# BIOS_CNTL register
offset_bios_cntl = 0xDC

SMM_BWP_mask = 1 << 5   # 0x20
BLE_mask = 1 << 1       # 0x2
BIOSWE_mask = 1 << 0    # 0x1

if __name__ == '__main__':

    kdbg = LocalKernelDebugger()