Skip to content

Commit

Permalink
register objects attempt 1
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Mitchell <[email protected]>
  • Loading branch information
npmitche committed Mar 6, 2024
1 parent 8d6868b commit 0987631
Show file tree
Hide file tree
Showing 10 changed files with 967 additions and 636 deletions.
11 changes: 8 additions & 3 deletions chipsec/chipset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from chipsec.logger import logger
from chipsec.defines import ARCH_VID
from chipsec.library.register import Register
from chipsec.library.registertype.register_factory import RegisterFactory
from chipsec.library.lock import Lock
from chipsec.library.control import Control
from chipsec.library.device import Device
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self):
self.using_return_codes = False
self.consistency_checking = False
self.lock = Lock(self)
self.register :Register = Register(self)
self.registers = []
self.control = Control(self)
self.device = Device(self)

Expand Down Expand Up @@ -112,6 +112,7 @@ def basic_init_with_helper(cls, helper = None):
_cs.load_helper(helper)
_cs.start_helper()
return _cs

def init(self, platform_code, req_pch_code, helper_name=None, start_helper=True, load_config=True, ignore_platform=False):
self.load_config = load_config
_unknown_proc = True
Expand Down Expand Up @@ -147,13 +148,17 @@ def init(self, platform_code, req_pch_code, helper_name=None, start_helper=True,
# Load Bus numbers for this platform.
if logger().DEBUG:
logger().log("[*] Discovering Bus Configuration:")
breakpoint()
if _unknown_pch:
msg = f'Unknown PCH: VID = 0x{self.Cfg.pch_vid:04X}, DID = 0x{self.Cfg.pch_did:04X}, RID = 0x{self.Cfg.pch_rid:02X}'
if self.Cfg.is_pch_req() and start_helper:
logger().log_error(f'Chipset requires a supported PCH to be loaded. {msg}')
raise UnknownChipsetError(msg)
else:
logger().log(f'[!] {msg}; Using Default.')
RegisterFactory(self).create_all_registers()
self.registers = self.Cfg.REGISTER_OBJS

if start_helper and ((logger().VERBOSE) or (load_config and (_unknown_pch or _unknown_proc))):
pci.print_pci_devices(self.pci.enumerate_devices())
if _unknown_pch or _unknown_proc:
Expand Down Expand Up @@ -250,5 +255,5 @@ def cs():
global _chipset

if _chipset is None:
_chipset: Chipset = Chipset()
_chipset = Chipset()
return _chipset
1 change: 1 addition & 0 deletions chipsec/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self):
self.logger = logger()
self.CONFIG_PCI = {}
self.REGISTERS = {}
self.REGISTER_OBJS = {}
self.MMIO_BARS = {}
self.IO_BARS = {}
self.IMA_REGISTERS = {}
Expand Down
52 changes: 38 additions & 14 deletions chipsec/hal/pci.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,33 +243,57 @@ def __init__(self, cs):
#

def read_dword(self, bus: int, device: int, function: int, address: int) -> int:
value = self.helper.read_pci_reg(bus, device, function, address, 4)
logger().log_hal(f'[pci] reading B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{value:08X}')
value = self.read(bus, device, function, address, 4)
return value

def read_word(self, bus: int, device: int, function: int, address: int) -> int:
word_value = self.helper.read_pci_reg(bus, device, function, address, 2)
logger().log_hal(f'[pci] reading B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{word_value:04X}')
word_value = self.read(bus, device, function, address, 2)
return word_value

def read_byte(self, bus: int, device: int, function: int, address: int) -> int:
byte_value = self.helper.read_pci_reg(bus, device, function, address, 1)
logger().log_hal(f'[pci] reading B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{byte_value:02X}')
byte_value = self.read(bus, device, function, address, 1)
return byte_value

def read(self, bus: int, device: int, function: int, address: int, size: int) -> int:
max_size = 4
if not (size in [1, 2] or (size % max_size == 0 and size <= 1780)):
raise IndexError("Size out of bounds. Must be 1, 2, or multiple of 4.")
if size > max_size:
value = 0
while size > 0:
value_l = self.helper.read_pci_reg(bus, device, function, address, max_size)
value = (value << 32) | value_l
size -= max_size
address += max_size
else:
value = self.helper.read_pci_reg(bus, device, function, address, size)

logger().log_hal(f'[pci] reading B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{value:0{size*2}X}')
return value

def write_byte(self, bus: int, device: int, function: int, address: int, byte_value: int) -> None:
self.helper.write_pci_reg(bus, device, function, address, byte_value, 1)
logger().log_hal(f'[pci] writing B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{byte_value:02X}')
return None
return self.write(bus, device, function, address, byte_value, 1)

def write_word(self, bus: int, device: int, function: int, address: int, word_value: int) -> None:
self.helper.write_pci_reg(bus, device, function, address, word_value, 2)
logger().log_hal(f'[pci] writing B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{word_value:04X}')
return None
return self.write(bus, device, function, address, word_value, 2)

def write_dword(self, bus: int, device: int, function: int, address: int, dword_value: int) -> None:
self.helper.write_pci_reg(bus, device, function, address, dword_value, 4)
logger().log_hal(f'[pci] writing B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{dword_value:08X}')
return self.write(bus, device, function, address, dword_value, 4)

def write(self, bus: int, device: int, function: int, address: int, value: int, size: int) -> None:
max_size = 4
if size not in [1, 2] or (size % max_size == 0 and size <= 1780):
raise IndexError("Size out of bounds. Must be 1, 2, or multiple of 4.")
if size > max_size:
while size > 0:
self.helper.write_pci_reg(bus, device, function, address, value & 0xFFFFFFFF, max_size)
value = (value >> 32)
size -= max_size
address += max_size
else:
self.helper.write_pci_reg(bus, device, function, address, value, size)

logger().log_hal(f'[pci] writing B/D/F: {bus:d}/{device:d}/{function:d}, offset: 0x{address:02X}, value: 0x{value:0{size*2}X}')
return None

#
Expand Down
11 changes: 6 additions & 5 deletions chipsec/library/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
# [email protected]
#


from chipsec.library.register import RegisterType
from chipsec.logger import logger
from typing import List, Optional, Tuple, Union
from chipsec.exceptions import CSFirstNotFoundError, CSBusNotFoundError, DeviceNotFoundError
Expand All @@ -30,17 +28,20 @@
#
##################################################################################


class Device:
def __init__(self, cs) -> None:
self.cs = cs

def get_first_bus(self, device:dict) -> int:
@staticmethod
def get_first_bus(device: dict) -> int:
'''Retrieves first value in bus list for PCI device'''
if 'bus' in device:
return self.get_first(device['bus'])
return Device.get_first(device['bus'])
raise CSBusNotFoundError()

def get_first(self, a_list:Union[list, int]) -> int:
@staticmethod
def get_first(a_list: Union[list, int]) -> int:
'''Returns received integer or first item from recieved list'''
if type(a_list) is int:
return a_list
Expand Down
Loading

0 comments on commit 0987631

Please sign in to comment.