Skip to content

Commit 8a88e63

Browse files
committed
Update to hals
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
1 parent b5a34ff commit 8a88e63

File tree

9 files changed

+105
-109
lines changed

9 files changed

+105
-109
lines changed

chipsec/hal/common/cpuid.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ def __init__(self, cs):
4040
self.helper = cs.helper
4141

4242
def cpuid(self, eax: int, ecx: int) -> Tuple[int, int, int, int]:
43-
logger().log_hal(f'[cpuid] in: EAX=0x{eax:08X}, ECX=0x{ecx:08X}')
44-
(eax, ebx, ecx, edx) = self.helper.cpuid(eax, ecx)
45-
logger().log_hal(f'[cpuid] out: EAX=0x{eax:08X}, EBX=0x{ebx:08X}, ECX=0x{ecx:08X}, EDX=0x{edx:08X}')
46-
return (eax, ebx, ecx, edx)
43+
return self.cs.hals.CPU.cpuid(eax, ecx)
4744

4845
def get_proc_info(self):
4946
(eax, _, _, _) = self.cpuid(0x01, 0x00)
@@ -53,7 +50,9 @@ def get_mfgid(self) -> str:
5350
(_,ebx, ecx, edx) = self.cpuid(0x00, 0x00)
5451
mfg_barray = ebx.to_bytes(4, byteorder) + edx.to_bytes(4, byteorder) + ecx.to_bytes(4, byteorder)
5552
return bytestostring(unpack('<12s', mfg_barray)[0])
53+
54+
5655

5756

5857

59-
haldata = {"arch":[hal_base.HALBase.MfgIds.Any], 'name': ['CpuId']} #change arch to CPUID genuine intel/amd
58+
haldata = {"arch":[hal_base.HALBase.MfgIds.Any], 'name': ['CpuId']}

chipsec/hal/common/ec.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,29 @@ class EC(hal_base.HALBase):
8484
# Wait for EC input buffer empty
8585
def _wait_ec_inbuf_empty(self) -> bool:
8686
to = 1000
87-
while (self.cs.hals.Io.read(IO_PORT_EC_STATUS) & EC_STS_IBF) and to:
87+
while (self.cs.hals.Io.read(IO_PORT_EC_STATUS, 1) & EC_STS_IBF) and to:
8888
to = to - 1
8989
return True
9090

9191
# Wait for EC output buffer full
9292
def _wait_ec_outbuf_full(self) -> bool:
9393
to = 1000
94-
while not (self.cs.hals.Io.read(IO_PORT_EC_STATUS) & EC_STS_OBF) and to:
94+
while not (self.cs.hals.Io.read(IO_PORT_EC_STATUS, 1) & EC_STS_OBF) and to:
9595
to = to - 1
9696
return True
9797

9898
def write_command(self, command: int) -> None:
9999
self._wait_ec_inbuf_empty()
100-
return self.cs.hals.Io.write(IO_PORT_EC_COMMAND, command)
100+
return self.cs.hals.Io.write(IO_PORT_EC_COMMAND, command, 1)
101101

102102
def write_data(self, data: int) -> None:
103103
self._wait_ec_inbuf_empty()
104-
return self.cs.hals.Io.write(IO_PORT_EC_DATA, data)
104+
return self.cs.hals.Io.write(IO_PORT_EC_DATA, data, 1)
105105

106106
def read_data(self) -> Optional[int]:
107107
if not self._wait_ec_outbuf_full():
108108
return None
109-
return self.cs.hals.Io.read(IO_PORT_EC_DATA)
109+
return self.cs.hals.Io.read(IO_PORT_EC_DATA, 1)
110110

111111
def read_memory(self, offset: int) -> Optional[int]:
112112
self.write_command(EC_COMMAND_ACPI_READ)
@@ -167,17 +167,17 @@ def write_range(self, start_offset: int, buffer: bytes) -> bool:
167167
# EC Intex I/O access
168168
#
169169
def read_idx(self, offset: int) -> int:
170-
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRL, offset & 0xFF)
171-
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRH, (offset >> 8) & 0xFF)
172-
value = self.cs.hals.Io.read(IO_PORT_EC_INDEX_DATA)
170+
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRL, offset & 0xFF, 1)
171+
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRH, (offset >> 8) & 0xFF, 1)
172+
value = self.cs.hals.Io.read(IO_PORT_EC_INDEX_DATA, 1)
173173
self.logger.log_hal(f'[ec] index read: offset 0x{offset:02X} > 0x{value:02X}:')
174174
return value
175175

176176
def write_idx(self, offset: int, value: int) -> bool:
177177
self.logger.log_hal(f'[ec] index write: offset 0x{offset:02X} < 0x{value:02X}:')
178-
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRL, offset & 0xFF)
179-
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRH, (offset >> 8) & 0xFF)
180-
self.cs.hals.Io.write(IO_PORT_EC_INDEX_DATA, value & 0xFF)
178+
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRL, offset & 0xFF, 1)
179+
self.cs.hals.Io.write(IO_PORT_EC_INDEX_ADDRH, (offset >> 8) & 0xFF, 1)
180+
self.cs.hals.Io.write(IO_PORT_EC_INDEX_DATA, value & 0xFF, 1)
181181
return True
182182

183183

chipsec/hal/common/interrupts.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
from chipsec.hal import hal_base
3636
from chipsec.library.logger import logger, print_buffer_bytes
3737
from chipsec.hal.common.acpi import ACPI
38+
from chipsec.library.exceptions import CSReadError
3839
from chipsec.hal.common.acpi_tables import UEFI_TABLE, GAS
3940
from chipsec.library.defines import bytestostring
4041

4142
SMI_APMC_PORT = 0xB2
42-
SMI_DATA_PORT = 0xB3
4343

4444
NMI_TCO1_CTL = 0x8 # NMI_NOW is bit [8] in TCO1_CTL (or bit [1] in TCO1_CTL + 1)
4545
NMI_NOW = 0x1
@@ -76,14 +76,25 @@ def send_SW_SMI_timed(self, thread_id: int, SMI_code_port_value: int, SMI_data_p
7676

7777
def send_SMI_APMC(self, SMI_code_port_value: int, SMI_data_port_value: int) -> None:
7878
logger().log_hal(f"[intr] sending SMI via APMC ports: code 0xB2 <- 0x{SMI_code_port_value:02X}, data 0xB3 <- 0x{SMI_data_port_value:02X}")
79-
self.cs.hals.Io.write_port_byte(SMI_DATA_PORT, SMI_data_port_value)
80-
return self.cs.hals.Io.write_port_byte(SMI_APMC_PORT, SMI_code_port_value)
79+
SMI_code_data = (SMI_data_port_value << 8 | SMI_code_port_value)
80+
return self.cs.hals.Io.write_port_word(SMI_APMC_PORT, SMI_code_data)
8181

8282
def send_NMI(self) -> None:
83-
logger().log_hal("[intr] Sending NMI# through TCO1_CTL[NMI_NOW]")
84-
reg, ba = self.cs.device.get_IO_space("TCOBASE")
85-
tcobase = self.cs.register.read_field(reg, ba)
86-
return self.cs.hals.Io.write_port_byte(tcobase + NMI_TCO1_CTL + 1, NMI_NOW)
83+
# logger().log_hal("[intr] Sending NMI# through TCO1_CTL[NMI_NOW]")
84+
# reg, ba = self.cs.device.get_IO_space("TCOBASE")
85+
# breakpoint()
86+
# tcobase = self.cs.register.read_field(reg, ba)
87+
# return self.cs.hals.Io.write_port_byte(tcobase + NMI_TCO1_CTL + 1, NMI_NOW)
88+
smbus_instance = self.cs.device.get_obj('8086.SMBUS')
89+
if smbus_instance is not None:
90+
self.logger.log_hal("[intr] Sending NMI# through TCO1_CTL[NMI_NOW]")
91+
try:
92+
tcobase, _ = self.cs.hals.IOBAR.get_IO_BAR_base_address("8086.SMBUS.TCOBASE", smbus_instance.instances[0])
93+
return self.cs.hals.IOBAR.write_port_byte(tcobase + NMI_TCO1_CTL + 1, NMI_NOW)
94+
except CSReadError:
95+
self.logger.log("Error finding register 8086.SMBUS.TCOBASE")
96+
else:
97+
self.logger.log("Unable to find register 8086.SMBUS.TCOBASE")
8798

8899
def find_ACPI_SMI_Buffer(self) -> Optional[UEFI_TABLE.CommBuffInfo]:
89100
logger().log_hal("Parsing ACPI tables to identify Communication Buffer")

chipsec/hal/common/iobar.py

Lines changed: 69 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from chipsec.library.logger import logger
3434
from chipsec.library.exceptions import IOBARNotFoundError
3535
from chipsec.library.exceptions import CSReadError
36+
from chipsec.library.registers.io import IO
3637

3738
DEFAULT_IO_BAR_SIZE = 0x100
3839

@@ -57,50 +58,50 @@ def is_IO_BAR_defined(self, bar_name: str) -> bool:
5758
#
5859
# Get base address of I/O range by IO BAR name
5960
#
60-
def get_IO_BAR_base_address(self, bar_name: str) -> Tuple[int, int]:
61-
bar = self.cs.Cfg.IO_BARS[bar_name]
62-
if bar is None or bar == {}:
61+
def get_IO_BAR_base_address(self, bar_name: str, instance) -> Tuple[int, int]:
62+
reglist = self.cs.register.get_list_by_name(bar_name)
63+
bar = reglist[0].get_def(bar_name)
64+
if not bar:
6365
raise IOBARNotFoundError(f'IOBARNotFound: {bar_name}')
66+
base = 0
67+
empmty_base = 0
6468

65-
if 'register' in bar:
66-
bar_reg = bar['register']
67-
if 'base_field' in bar:
68-
base_field = bar['base_field']
69+
if bar.register:
70+
if instance is not None:
71+
bar_reg = self.cs.register.get_instance_by_name(bar.register, instance)
72+
else:
73+
bar_reg = self.cs.register.get_list_by_name(bar.register)[0]
74+
75+
if bar.base_field:
76+
base_field = bar.base_field
6977
try:
70-
base = self.cs.register.read_field(bar_reg, base_field, preserve_field_position=True)
78+
base = bar_reg.get_field(base_field)
7179
except Exception:
72-
base = 0
80+
pass
7381
try:
74-
empty_base = self.cs.register.get_field_mask(bar_reg, base_field, preserve_field_position=True)
82+
empty_base = bar_reg.get_field_mask(base_field, True)
7583
except Exception:
76-
empty_base = 0
84+
pass
7785
else:
7886
try:
79-
base = self.cs.register.read(bar_reg)
87+
base = bar_reg.read()
8088
except Exception:
81-
base = 0
89+
pass
8290
try:
83-
empty_base = self.cs.register.get_field_mask(bar_reg, preserve_field_position=True)
91+
empty_base = bar_reg.get_mask()
8492
except Exception:
85-
empty_base = 0
86-
else:
87-
# this method is not preferred
88-
base = self.cs.hals.Pci.read_word(self.cs.device.get_first_bus(bar), bar['dev'], bar['fun'], bar['reg'])
89-
empty_base = 0xFFFF
90-
91-
if 'fixed_address' in bar and (base == empty_base or base == 0):
92-
base = bar['fixed_address']
93-
if logger().HAL:
94-
logger().log(f'[iobar] Using fixed address for {bar_name}: 0x{base:016X}')
95-
96-
if 'mask' in bar:
97-
base = base & bar['mask']
98-
if 'offset' in bar:
99-
base = base + bar['offset']
100-
size = bar['size'] if ('size' in bar) else DEFAULT_IO_BAR_SIZE
101-
102-
if logger().HAL:
103-
logger().log(f'[iobar] {bar_name}: 0x{base:04X} (size = 0x{size:X})')
93+
pass
94+
95+
if bar.fixed_address and (base == empty_base or base == 0):
96+
base = bar.fixed_address
97+
self.logger.log_hal(f'[iobar] Using fixed address for {bar_name}: 0x{base:016X}')
98+
99+
if bar.mask:
100+
base = base & bar.mask
101+
if bar.offset:
102+
base = base + bar.offset
103+
size = bar.size if bar.size else DEFAULT_IO_BAR_SIZE
104+
self.logger.log_hal(f'[iobar] {bar_name}: 0x{base:04X} (size = 0x{size:X})')
104105
if base == 0:
105106
raise CSReadError(f'IOBAR ({bar_name}) base address is 0')
106107
return base, size
@@ -109,12 +110,11 @@ def get_IO_BAR_base_address(self, bar_name: str) -> Tuple[int, int]:
109110
# Read I/O register from I/O range defined by I/O BAR name
110111
#
111112
def read_IO_BAR_reg(self, bar_name: str, offset: int, size: int) -> int:
112-
if logger().HAL:
113-
logger().log(f'[iobar] read {bar_name} + 0x{offset:X} ({size:d})')
113+
self.logger.log_hal(f'[iobar] read {bar_name} + 0x{offset:X} ({size:d})')
114114
(bar_base, bar_size) = self.get_IO_BAR_base_address(bar_name)
115115
io_port = bar_base + offset
116-
if offset > bar_size and logger().HAL:
117-
logger().log_warning(f'offset 0x{offset:X} is outside {bar_name} size (0x{size:X})')
116+
if offset > bar_size and self.logger.HAL:
117+
self.logger.log_warning(f'offset 0x{offset:X} is outside {bar_name} size (0x{size:X})')
118118
value = self.cs.hals.Io.read(io_port, size)
119119
return value
120120

@@ -123,63 +123,50 @@ def read_IO_BAR_reg(self, bar_name: str, offset: int, size: int) -> int:
123123
#
124124
def write_IO_BAR_reg(self, bar_name: str, offset: int, size: int, value: int) -> int:
125125
(bar_base, bar_size) = self.get_IO_BAR_base_address(bar_name)
126-
if logger().HAL:
127-
logger().log(f'[iobar] write {bar_name} + 0x{offset:X} ({size:d}): 0x{value:X}')
126+
self.logger.log_hal(f'[iobar] write {bar_name} + 0x{offset:X} ({size:d}): 0x{value:X}')
128127
io_port = bar_base + offset
129-
if offset > bar_size and logger().HAL:
130-
logger().log_warning(f'offset 0x{offset:X} is outside {bar_name} size (0x{size:X})')
128+
if offset > bar_size and self.logger.HAL:
129+
self.logger.log_warning(f'offset 0x{offset:X} is outside {bar_name} size (0x{size:X})')
131130
return self.cs.hals.Io.write(io_port, value, size)
132131

133132
#
134133
# Check if I/O range is enabled by BAR name
135134
#
136135
def is_IO_BAR_enabled(self, bar_name: str) -> bool:
137-
bar = self.cs.Cfg.IO_BARS[bar_name]
136+
if not self.is_IO_BAR_defined(bar_name):
137+
return False
138+
bar = IO.get_def(bar_name)
138139
is_enabled = True
139-
if 'register' in bar:
140-
bar_reg = bar['register']
141-
if 'enable_field' in bar:
142-
bar_en_field = bar['enable_field']
143-
is_enabled = (1 == self.cs.register.read_field(bar_reg, bar_en_field))
140+
if bar.register:
141+
bar_reg = bar.register
142+
if bar.enable_field:
143+
bar_en_field = bar.enable_field
144+
is_enabled = (1 == bar_reg.read_field(bar_en_field))
144145
return is_enabled
145146

146147
def list_IO_BARs(self) -> None:
147148
logger().log('')
148149
logger().log('--------------------------------------------------------------------------------')
149-
logger().log(' I/O Range | BAR Register | Base | Size | En? | Description')
150+
logger().log(f' {"I/O Range":35} | {"B:D.F":7} | {"Base":16} | {"Size":8} | {"En?":3} | Description')
150151
logger().log('--------------------------------------------------------------------------------')
151-
for _bar_name in self.cs.Cfg.IO_BARS:
152-
if not self.is_IO_BAR_defined(_bar_name):
153-
continue
154-
_bar = self.cs.Cfg.IO_BARS[_bar_name]
155-
bus_data = []
156-
if 'register' in _bar:
157-
bus_data = self.cs.register.get_bus(_bar['register'])
158-
if not bus_data:
159-
if 'bus' in self.cs.register.get_def(_bar['register']):
160-
bus_data = [self.cs.register.get_def(_bar['register'])['bus']]
161-
elif 'bus' in _bar:
162-
bus_data.extend(_bar['bus'])
163-
else:
164-
continue
165-
166-
for bus in bus_data:
167-
try:
168-
(_base, _size) = self.get_IO_BAR_base_address(_bar_name)
169-
except CSReadError:
170-
if self.logger.HAL:
171-
self.logger.log(f"Unable to find IO BAR {_bar_name}")
172-
continue
173-
_en = self.is_IO_BAR_enabled(_bar_name)
174-
175-
if 'register' in _bar:
176-
_s = _bar['register']
177-
if 'offset' in _bar:
178-
_s += (f' + 0x{_bar["offset"]:X}')
179-
else:
180-
_s = f'{bus:02X}:{_bar["dev"]:02X}.{_bar["fun"]:01X} + {_bar["reg"]}'
181-
182-
logger().log(f' {_bar_name:12} | {_s:14} | {_base:016X} | {_size:08X} | {_en:d} | {_bar["desc"]}')
152+
for vid in self.cs.Cfg.IO_BARS:
153+
for dev in self.cs.Cfg.IO_BARS[vid]:
154+
for _bar_name in self.cs.Cfg.IO_BARS[vid][dev]:
155+
bar_name = f'{vid}.{dev}.{_bar_name}'
156+
if not self.is_IO_BAR_defined(bar_name):
157+
continue
158+
_bar = self.cs.Cfg.IO_BARS[vid][dev][_bar_name]
159+
160+
for instance in _bar.instances:
161+
(_base, _size) = _bar.get_base(instance)
162+
if _base is None:
163+
(_base, _size) = self.get_IO_BAR_base_address(bar_name, instance)
164+
_en = self.is_IO_BAR_enabled(bar_name, instance)
165+
if instance.bus is not None:
166+
bdf = f'{instance.bus:02X}:{instance.dev:02X}.{instance.fun:1X}'
167+
else:
168+
bdf = 'fixed'
169+
logger().log(f' {_bar_name:35} | {bdf:7} | {_base:016X} | {_size:08X} | {_en:d} | {_bar["desc"]}')
183170

184171
#
185172
# Read I/O range by I/O BAR name
@@ -199,10 +186,10 @@ def dump_IO_BAR(self, bar_name: str, size: int = 1) -> None:
199186
(range_base, range_size) = self.get_IO_BAR_base_address(bar_name)
200187
n = range_size // size
201188
fmt = f'0{size * 2:d}X'
202-
logger().log(f"[iobar] I/O BAR {bar_name}:")
189+
self.logger.log(f"[iobar] I/O BAR {bar_name}:")
203190
for i in range(n):
204191
reg = self.cs.hals.Io.read(range_base + i * size, size)
205-
logger().log(f'{size * i:+04X}: {reg:{fmt}}')
192+
self.logger.log(f'{size * i:+04X}: {reg:{fmt}}')
206193

207194

208195
haldata = {"arch":[hal_base.HALBase.MfgIds.Any], 'name': ['IOBAR']}

chipsec/hal/common/mmio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ def get_MMIO_BAR_base_address(self, bar_name: str, pciobj: Optional[int] = None)
295295
size = DEFAULT_MMIO_BAR_SIZE
296296
self.logger.log_hal('[mmio] {}: 0x{:016X} (size = 0x{:X})'.format(bar_name, base, size))
297297
if base == 0:
298-
breakpoint()
299298
self.logger.log_hal('[mmio] Base address was determined to be 0.')
300299
raise CSReadError('[mmio] Base address was determined to be 0')
301300

chipsec/hal/common/pci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,4 @@ def is_enabled(self, bus: int, dev: int, fun: int) -> bool:
358358
return False
359359
return True
360360

361-
haldata = {"arch":[HALBase.MfgIds.Any, HALBase.MfgIds.Intel], 'name': ['Pci']} #change arch to CPUID genuine intel/amd
361+
haldata = {"arch":[HALBase.MfgIds.Any, HALBase.MfgIds.Intel], 'name': ['Pci']}

chipsec/hal/intel/cpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,4 @@ def dump_page_tables_all(self) -> None:
240240
self.dump_page_tables(cr3)
241241

242242

243-
haldata = {"arch":[hal_base.HALBase.MfgIds.Any], 'name': ['CPU']} #change arch to CPUID genuine intel/amd
243+
haldata = {"arch":[hal_base.HALBase.MfgIds.Any], 'name': ['CPU']}

chipsec/library/device.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def get_bus(self, device_name: str) -> List[int]:
9090
"""Retrieves bus value(s) from PCI device"""
9191
dev_list = self.get_objlist(device_name)
9292
buses = []
93-
breakpoint()
9493
for dev in dev_list:
9594
for instance_key in dev.instances.keys():
9695
buses.append(dev.instances[instance_key].bus)

tests/utilcmd/run_chipsec_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def setup_run_destroy_util_get_log_output(init_replay_file: str, util_name: str,
5454
replayHelper = rph.ReplayHelper(init_replay_file)
5555
csu._helper = replayHelper
5656
retval = run_chipsec_util(csu, util_replay_file)
57+
clear_cs()
5758
logger_calls = []
5859
for func in logging_fucntions_to_capture:
5960
if hasattr(chipsec.library.logger._logger, func):

0 commit comments

Comments
 (0)