mirror of
https://github.com/chipsec/chipsec
synced 2026-06-08 13:31:00 +00:00
Type Hint and fstring updates to virtmem.py
Signed-off-by: Frinzell, Aaron <aaron.frinzell@intel.com>
This commit is contained in:
committed by
Nathaniel Mitchell
parent
f2d2e02395
commit
7a33faa7d2
+27
-38
@@ -30,8 +30,8 @@ usage:
|
||||
"""
|
||||
|
||||
import struct
|
||||
|
||||
from chipsec.logger import logger, print_buffer
|
||||
from typing import Tuple
|
||||
from chipsec.logger import logger, print_buffer_bytes
|
||||
from chipsec.hal import hal_base
|
||||
|
||||
|
||||
@@ -49,84 +49,73 @@ class VirtMemory(hal_base.HALBase):
|
||||
|
||||
# Reading virtual memory
|
||||
|
||||
def read_virtual_mem(self, virt_address, length):
|
||||
if logger().HAL:
|
||||
logger().log("[mem] 0x{:016X}".format(virt_address))
|
||||
def read_virtual_mem(self, virt_address: int, length: int) -> int:
|
||||
logger().log_hal(f'[mem] 0x{virt_address:016X}')
|
||||
phys_address = self.va2pa(virt_address)
|
||||
return self.helper.read_physical_mem(phys_address, length)
|
||||
|
||||
def read_virtual_mem_dword(self, virt_address):
|
||||
def read_virtual_mem_dword(self, virt_address: int) -> int:
|
||||
phys_address = self.va2pa(virt_address)
|
||||
out_buf = self.helper.read_physical_mem(phys_address, 4)
|
||||
value = struct.unpack('=I', out_buf)[0]
|
||||
if logger().HAL:
|
||||
logger().log('[mem] dword at VA = 0x{:016X}: 0x{:08X}'.format(virt_address, value))
|
||||
logger().log_hal(f'[mem] dword at VA = 0x{virt_address:016X}: 0x{value:08X}')
|
||||
return value
|
||||
|
||||
def read_virtual_mem_word(self, virt_address):
|
||||
def read_virtual_mem_word(self, virt_address: int) -> int:
|
||||
phys_address = self.va2pa(virt_address)
|
||||
out_buf = self.helper.read_physical_mem(phys_address, 2)
|
||||
value = struct.unpack('=H', out_buf)[0]
|
||||
if logger().HAL:
|
||||
logger().log('[mem] word at VA = 0x{:016X}: 0x{:04X}'.format(virt_address, value))
|
||||
logger().log_hal(f'[mem] word at VA = 0x{virt_address:016X}: 0x{value:04X}')
|
||||
return value
|
||||
|
||||
def read_virtual_mem_byte(self, virt_address):
|
||||
def read_virtual_mem_byte(self, virt_address: int) -> int:
|
||||
phys_address = self.va2pa(virt_address)
|
||||
out_buf = self.helper.read_physical_mem(phys_address, 1)
|
||||
value = struct.unpack('=B', out_buf)[0]
|
||||
if logger().HAL:
|
||||
logger().log('[mem] byte at VA = 0x{:016X}: 0x{:02X}'.format(virt_address, value))
|
||||
logger().log_hal(f'[mem] byte at VA = 0x{virt_address:016X}: 0x{value:02X}')
|
||||
return value
|
||||
|
||||
# Writing virtual memory
|
||||
|
||||
def write_virtual_mem(self, virt_address, length, buf):
|
||||
def write_virtual_mem(self, virt_address: int, length: int, buf: bytes) -> int:
|
||||
logger().log_hal(f'[mem] buffer len = 0x{length:X} to VA = 0x{virt_address:016X}')
|
||||
if logger().HAL:
|
||||
logger().log('[mem] buffer len = 0x{:X} to VA = 0x{:016X}'.format(length, virt_address))
|
||||
print_buffer(buf)
|
||||
print_buffer_bytes(buf)
|
||||
phys_address = self.va2pa(virt_address)
|
||||
return self.helper.write_physical_mem(phys_address, length, buf)
|
||||
|
||||
def write_virtual_mem_dword(self, virt_address, dword_value):
|
||||
if logger().HAL:
|
||||
logger().log('[mem] dword to VA = 0x{:016X} <- 0x{:08X}'.format(virt_address, dword_value))
|
||||
def write_virtual_mem_dword(self, virt_address: int, dword_value: int) -> int:
|
||||
logger().log_hal(f'[mem] dword to VA = 0x{virt_address:016X} <- 0x{dword_value:08X}')
|
||||
phys_address = self.va2pa(virt_address)
|
||||
return self.helper.write_physical_mem(phys_address, 4, struct.pack('I', dword_value))
|
||||
|
||||
def write_virtual_mem_word(self, virt_address, word_value):
|
||||
if logger().HAL:
|
||||
logger().log('[mem] word to VA = 0x{:016X} <- 0x{:04X}'.format(virt_address, word_value))
|
||||
def write_virtual_mem_word(self, virt_address: int, word_value: int) -> int:
|
||||
logger().log_hal(f'[mem] word to VA = 0x{virt_address:016X} <- 0x{word_value:04X}')
|
||||
phys_address = self.va2pa(virt_address)
|
||||
return self.helper.write_physical_mem(phys_address, 2, struct.pack('H', word_value))
|
||||
|
||||
def write_virtual_mem_byte(self, virt_address, byte_value):
|
||||
if logger().HAL:
|
||||
logger().log('[mem] byte to VA = 0x{:016X} <- 0x{:02X}'.format(virt_address, byte_value))
|
||||
def write_virtual_mem_byte(self, virt_address: int, byte_value: int) -> int:
|
||||
logger().log_hal(f'[mem] byte to VA = 0x{virt_address:016X} <- 0x{byte_value:02X}')
|
||||
phys_address = self.va2pa(virt_address)
|
||||
return self.helper.write_physical_mem(phys_address, 1, struct.pack('B', byte_value))
|
||||
|
||||
# Allocate virtual memory buffer
|
||||
|
||||
def alloc_virtual_mem(self, length, max_phys_address=0xFFFFFFFFFFFFFFFF):
|
||||
def alloc_virtual_mem(self, length: int, max_phys_address: int = 0xFFFFFFFFFFFFFFFF) -> Tuple[int, int]:
|
||||
(va, pa) = self.helper.alloc_physical_mem(length, max_phys_address)
|
||||
if logger().HAL:
|
||||
logger().log('[mem] Allocated: PA = 0x{:016X}, VA = 0x{:016X}'.format(pa, va))
|
||||
logger().log_hal(f'[mem] Allocated: PA = 0x{pa:016X}, VA = 0x{va:016X}')
|
||||
return (va, pa)
|
||||
|
||||
def va2pa(self, va):
|
||||
def va2pa(self, va: int) -> int:
|
||||
(pa, error_code) = self.helper.va2pa(va)
|
||||
if error_code:
|
||||
if logger().HAL:
|
||||
logger().log('[mem] Looks like VA (0x{:016X}) not mapped'.format(va))
|
||||
return
|
||||
if logger().HAL:
|
||||
logger().log('[mem] VA (0x{:016X}) -> PA (0x{:016X})'.format(va, pa))
|
||||
logger().log_hal(f'[mem] Looks like VA (0x{va:016X}) not mapped')
|
||||
return va
|
||||
logger().log_hal(f'[mem] VA (0x{va:016X}) -> PA (0x{pa:016X})')
|
||||
return pa
|
||||
|
||||
def free_virtual_mem(self, virt_address):
|
||||
def free_virtual_mem(self, virt_address: int) -> bool:
|
||||
pa = self.va2pa(virt_address)
|
||||
ret = self.helper.free_physical_mem(pa)
|
||||
if logger().HAL:
|
||||
logger().log('[mem] Deallocated : VA = 0x{:016X}'.format(virt_address))
|
||||
logger().log_hal(f'[mem] Deallocated : VA = 0x{virt_address:016X}')
|
||||
return ret == 1
|
||||
|
||||
Reference in New Issue
Block a user