From af2d2c04023907a263ad014deb8face8ca18c0c2 Mon Sep 17 00:00:00 2001 From: brentholtsclaw Date: Wed, 7 Sep 2022 11:43:22 -0700 Subject: [PATCH] Fixup merge Signed-off-by: brentholtsclaw --- chipsec/hal/spi_uefi.py | 29 +++++++++++++--------- chipsec/hal/uefi.py | 54 +++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/chipsec/hal/spi_uefi.py b/chipsec/hal/spi_uefi.py index 728d0a89..32b42013 100644 --- a/chipsec/hal/spi_uefi.py +++ b/chipsec/hal/spi_uefi.py @@ -70,17 +70,19 @@ type2ext = {EFI_SECTION_PE32: 'pe32', EFI_SECTION_TE: 'te', EFI_SECTION_PIC: 'pi WRITE_ALL_HASHES = False -def decompress_section_data( section_dir_path, sec_fs_name, compressed_data, compression_type): +def decompress_section_data(section_dir_path, sec_fs_name, compressed_data, compression_type): uefi_uc = UEFICompression() uncompressed_name = os.path.join(section_dir_path, sec_fs_name) - if logger().HAL: logger().log("[uefi] decompressing EFI binary (type = 0x{:X})\n {} ->\n".format(compression_type, uncompressed_name)) + if logger().HAL: + logger().log("[uefi] decompressing EFI binary (type = 0x{:X})\n {} ->\n".format(compression_type, uncompressed_name)) uncompressed_image = uefi_uc.decompress_EFI_binary(compressed_data, compression_type) return uncompressed_image def compress_image(image, compression_type): uefi_uc = UEFICompression() - if logger().HAL: logger().log("[uefi] compressing EFI binary (type = 0x{:X})\n".format(compression_type)) + if logger().HAL: + logger().log("[uefi] compressing EFI binary (type = 0x{:X})\n".format(compression_type)) compressed_image = uefi_uc.compress_EFI_binary(image, compression_type) return compressed_image @@ -219,7 +221,7 @@ def build_efi_modules_tree(fwtype, data, Size, offset, polarity): sec.children = build_efi_model(data, fwtype) sections.append(sec) - sec = NextFwFileSection( data, Size, sec.Size + sec.Offset, polarity) + sec = NextFwFileSection(data, Size, sec.Size + sec.Offset, polarity) secn += 1 return sections @@ -230,7 +232,8 @@ def build_efi_modules_tree(fwtype, data, Size, offset, polarity): # fwtype - platform specific firmware type used to detect NVRAM format (VSS, EVSA, NVAR...) -def build_efi_file_tree (fv_img, fwtype): + +def build_efi_file_tree(fv_img, fwtype): fv_size, HeaderSize, Attributes = GetFvHeader(fv_img) polarity = Attributes & EFI_FVB2_ERASE_POLARITY fwbin = NextFwFile(fv_img, fv_size, HeaderSize, polarity) @@ -267,7 +270,7 @@ def build_efi_tree(data, fwtype): # Detect File System firmware volumes if fv.Guid in EFI_PLATFORM_FS_GUIDS or fv.Guid in EFI_FS_GUIDS: - fwbin = build_efi_file_tree (fv.Image, fwtype) + fwbin = build_efi_file_tree(fv.Image, fwtype) for i in fwbin: fv.children.append(i) @@ -432,8 +435,9 @@ def save_efi_tree(modules, parent=None, save_modules=True, path=None, save_log=T # getNVstore_xxx functions expect FV than a FW file within FV # so for EFI_FILE type of module using parent's Image as NVRAM nvram = parent.Image if (type(m) == EFI_FILE and type(parent) == EFI_FV) else m.Image - parse_EFI_variables( os.path.join(mod_dir_path, 'NVRAM'), nvram, False, m.NVRAMType) - else: raise Exception("NVRAM type cannot be None") + parse_EFI_variables(os.path.join(mod_dir_path, 'NVRAM'), nvram, False, m.NVRAMType) + else: + raise Exception("NVRAM type cannot be None") except Exception: logger().log_warning("couldn't extract NVRAM in {{{}}} using type '{}'".format(m.Guid, m.NVRAMType)) @@ -474,7 +478,7 @@ def parse_uefi_region_from_file(filename, fwtype, outpath=None, filetype=[]): tree_json = save_efi_tree_filetype(tree, path=outpath, filetype=filetype) else: tree_json = save_efi_tree(tree, path=outpath) - write_file( "{}.UEFI.json".format(filename), json.dumps(tree_json, indent=2, separators=(',', ': '), cls=UUIDEncoder) ) + write_file("{}.UEFI.json".format(filename), json.dumps(tree_json, indent=2, separators=(',', ': '), cls=UUIDEncoder)) def decode_uefi_region(pth, fname, fwtype, filetype=[]): @@ -487,7 +491,8 @@ def decode_uefi_region(pth, fname, fwtype, filetype=[]): os.makedirs(fv_pth) # Decoding UEFI Firmware Volumes - if logger().HAL: logger().log( "[spi_uefi] decoding UEFI firmware volumes..." ) + if logger().HAL: + logger().log("[spi_uefi] decoding UEFI firmware volumes...") parse_uefi_region_from_file(fname, fwtype, fv_pth, filetype) # If a specific filetype is wanted, there is no need to check for EFI Variables if filetype: @@ -505,8 +510,8 @@ def decode_uefi_region(pth, fname, fwtype, filetype=[]): if logger().HAL: logger().log_error("unrecognized NVRAM type {}".format(fwtype)) return - nvram_fname = os.path.join( bios_pth, ('nvram_{}'.format(fwtype)) ) - logger().set_log_file( (nvram_fname + '.nvram.lst') ) + nvram_fname = os.path.join(bios_pth, ('nvram_{}'.format(fwtype))) + logger().set_log_file((nvram_fname + '.nvram.lst')) parse_EFI_variables(nvram_fname, region_data, False, fwtype) diff --git a/chipsec/hal/uefi.py b/chipsec/hal/uefi.py index db76c361..6176f07c 100644 --- a/chipsec/hal/uefi.py +++ b/chipsec/hal/uefi.py @@ -216,9 +216,10 @@ def print_sorted_EFI_variables(variables): for name in sorted_names: for rec in variables[name]: # off, buf, hdr, data, guid, attrs - print_efi_variable( rec[0], rec[1], rec[2], name, rec[3], rec[4], rec[5] ) + print_efi_variable(rec[0], rec[1], rec[2], name, rec[3], rec[4], rec[5]) -def decode_EFI_variables( efi_vars, nvram_pth ): + +def decode_EFI_variables(efi_vars, nvram_pth): # print decoded and sorted EFI variables into a log file print_sorted_EFI_variables(efi_vars) # write each EFI variable into its own binary file @@ -246,13 +247,11 @@ def identify_EFI_NVRAM(buffer): if offset != -1: return fw_type return None - def parse_EFI_variables(self, fname, rom, authvars, _fw_type=None): if _fw_type in uefi_platform.fw_types: logger().log("[uefi] Using FW type (NVRAM format): {}".format(_fw_type)) - #self.set_FWType(_fw_type) else: - logger().error("Unrecognized FW type (NVRAM format) '{}'..".format(_fw_type)) + logger().log_error("Unrecognized FW type (NVRAM format) '{}'..".format(_fw_type)) return False logger().log("[uefi] Searching for NVRAM in the binary..") @@ -267,42 +266,43 @@ def parse_EFI_variables(self, fname, rom, authvars, _fw_type=None): efi_vars = uefi_platform.EFI_VAR_DICT[_fw_type]['func_getefivariables'](efi_vars_store) decode_EFI_variables(efi_vars, nvram_pth) else: - logger().error("Did not find NVRAM") + logger().log_error("Did not find NVRAM") return False - return True + return True def find_EFI_variable_store(rom_buffer, _FWType): if rom_buffer is None: - logger().error('rom_buffer is None') + logger().log_error('rom_buffer is None') return None rom = rom_buffer - offset = 0 - size = len(rom_buffer) + offset = 0 + size = len(rom_buffer) nvram_header = None if uefi_platform.EFI_VAR_DICT[_FWType]['func_getnvstore']: (offset, size, nvram_header) = uefi_platform.EFI_VAR_DICT[_FWType]['func_getnvstore'](rom) if (-1 == offset): - logger().error("'func_getnvstore' is defined but could not find EFI NVRAM. Exiting..") + logger().log_error("'func_getnvstore' is defined but could not find EFI NVRAM. Exiting..") return None else: logger().log("[uefi] 'func_getnvstore' is not defined in EFI_VAR_DICT. Assuming start offset 0..") - if -1 == size: size = len(rom_buffer) + if -1 == size: + size = len(rom_buffer) nvram_buf = rom[offset: offset + size] if logger().UTIL_TRACE: logger().log('[uefi] Found EFI NVRAM at offset 0x{:08X}'.format(offset)) - logger().log( """ + logger().log(""" ================================================================== NVRAM: EFI Variable Store ==================================================================""") - if nvram_header: logger().log(nvram_header) + if nvram_header: + logger().log(nvram_header) return nvram_buf - ######################################################################################################## # # UEFI HAL Component @@ -340,29 +340,31 @@ class UEFI(hal_base.HALBase): def dump_EFI_variables_from_SPI(self): return self.read_EFI_variables_from_SPI(0, 0x800000) - def read_EFI_variables_from_SPI( self, BIOS_region_base, BIOS_region_size ): - rom = self.cs.spi.read_spi( BIOS_region_base, BIOS_region_size ) - efi_var_store = self.find_EFI_variable_store( rom, self._FWType ) + def read_EFI_variables_from_SPI(self, BIOS_region_base, BIOS_region_size): + rom = self.cs.spi.read_spi(BIOS_region_base, BIOS_region_size) + efi_var_store = self.find_EFI_variable_store(rom, self._FWType) if efi_var_store: efi_vars = uefi_platform.EFI_VAR_DICT[self._FWType]['func_getefivariables'] return efi_vars return efi_var_store - def read_EFI_variables_from_file( self, filename ): - rom = read_file( filename ) - efi_var_store = self.find_EFI_variable_store( rom, self._FWType ) + def read_EFI_variables_from_file(self, filename): + rom = read_file(filename) + efi_var_store = self.find_EFI_variable_store(rom, self._FWType) if efi_var_store: efi_vars = uefi_platform.EFI_VAR_DICT[self._FWType]['func_getefivariables'] return efi_vars return efi_var_store + # @TODO: Do not use, will be removed - def read_EFI_variables( self, efi_var_store, authvars ): - if ( efi_var_store is None ): - logger().error( 'efi_var_store is None' ) + def read_EFI_variables(self, efi_var_store, authvars): + if (efi_var_store is None): + logger().log_error('efi_var_store is None') return None - variables = uefi_platform.EFI_VAR_DICT[ self._FWType ]['func_getefivariables']( efi_var_store ) - if logger().UTIL_TRACE: print_sorted_EFI_variables( variables ) + variables = uefi_platform.EFI_VAR_DICT[self._FWType]['func_getefivariables'](efi_var_store) + if logger().UTIL_TRACE: + print_sorted_EFI_variables(variables) return variables ######################################################################