diff --git a/source/tool/chipsec/command.py b/source/tool/chipsec/command.py new file mode 100644 index 00000000..b123ea71 --- /dev/null +++ b/source/tool/chipsec/command.py @@ -0,0 +1,33 @@ +#!/usr/local/bin/python +#CHIPSEC: Platform Security Assessment Framework +#Copyright (c) 2016, Google +# +#This program is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public License +#as published by the Free Software Foundation; Version 2. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program; if not, write to the Free Software +#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + + +import chipsec.logger + +class BaseCommand: + + def __init__(self, argv, cs=None): + self.argv = argv + self.logger = chipsec.logger.logger() + self.cs = cs + + def run(self): + raise NotImplementedError('sub class should overwrite the run() method') + + def requires_driver(self): + raise NotImplementedError('sub class should overwrite the requires_driver() method') diff --git a/source/tool/chipsec/utilcmd/acpi_cmd.py b/source/tool/chipsec/utilcmd/acpi_cmd.py index f3527ff2..202071e7 100644 --- a/source/tool/chipsec/utilcmd/acpi_cmd.py +++ b/source/tool/chipsec/utilcmd/acpi_cmd.py @@ -28,14 +28,10 @@ Command-line utility providing access to ACPI tables __version__ = '1.0' import os -import sys import time -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * from chipsec.hal.acpi import * +from chipsec.command import BaseCommand # ################################################################### # @@ -43,7 +39,7 @@ from chipsec.hal.acpi import * # # ################################################################### -def acpi(argv): +class ACPICommand(BaseCommand): """ >>> chipsec_util acpi list >>> chipsec_util acpi table | @@ -54,44 +50,51 @@ def acpi(argv): >>> chipsec_util acpi table XSDT >>> chipsec_util acpi table acpi_table.bin """ - if len(argv) < 3: - print acpi.__doc__ - return - op = argv[2] - t = time.time() - - try: - _acpi = ACPI( chipsec_util._cs ) - except AcpiRuntimeError, msg: - print msg - return - - if ( 'list' == op ): - logger().log( "[CHIPSEC] Enumerating ACPI tables.." ) - _acpi.print_ACPI_table_list() - elif ( 'table' == op ): - if len(argv) < 4: - print acpi.__doc__ + + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True + + def run(self): + if len(self.argv) < 3: + print ACPICommand.__doc__ return - name = argv[ 3 ] - if name in ACPI_TABLES: - if _acpi.is_ACPI_table_present( name ): - logger().log( "[CHIPSEC] reading ACPI table '%s'" % name ) - _acpi.dump_ACPI_table( name ) + op = self.argv[2] + t = time.time() + + try: + _acpi = ACPI(self.cs) + except AcpiRuntimeError, msg: + print msg + return + + if ( 'list' == op ): + self.logger.log( "[CHIPSEC] Enumerating ACPI tables.." ) + _acpi.print_ACPI_table_list() + elif ( 'table' == op ): + if len(self.argv) < 4: + print ACPICommand.__doc__ + return + name = self.argv[ 3 ] + if name in ACPI_TABLES: + if _acpi.is_ACPI_table_present( name ): + self.logger.log( "[CHIPSEC] reading ACPI table '%s'" % name ) + _acpi.dump_ACPI_table( name ) + else: + self.logger.log( "[CHIPSEC] ACPI table '%s' wasn't found" % name ) + elif os.path.exists( name ): + self.logger.log( "[CHIPSEC] reading ACPI table from file '%s'" % name ) + _acpi.dump_ACPI_table( name, True ) else: - logger().log( "[CHIPSEC] ACPI table '%s' wasn't found" % name ) - elif os.path.exists( name ): - logger().log( "[CHIPSEC] reading ACPI table from file '%s'" % name ) - _acpi.dump_ACPI_table( name, True ) + self.logger.error( "Please specify table name or path to a file.\nTable name must be in %s" % ACPI_TABLES.keys() ) + print ACPICommand.__doc__ + return else: - logger().error( "Please specify table name or path to a file.\nTable name must be in %s" % ACPI_TABLES.keys() ) - print acpi.__doc__ + print ACPICommand.__doc__ return - else: - print acpi.__doc__ - return - - logger().log( "[CHIPSEC] (acpi) time elapsed %.3f" % (time.time()-t) ) + self.logger.log( "[CHIPSEC] (acpi) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['acpi'] = {'func' : acpi, 'start_driver' : True, 'help' : acpi.__doc__ } \ No newline at end of file +commands = { 'acpi': ACPICommand } diff --git a/source/tool/chipsec/utilcmd/chipset_cmd.py b/source/tool/chipsec/utilcmd/chipset_cmd.py index a7a69a70..95deb9f7 100644 --- a/source/tool/chipsec/utilcmd/chipset_cmd.py +++ b/source/tool/chipsec/utilcmd/chipset_cmd.py @@ -28,16 +28,7 @@ usage as a standalone utility: __version__ = '1.0' -import os -import sys -import time - -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.chipset import UnknownChipsetError, print_supported_chipsets # ################################################################### @@ -45,15 +36,20 @@ from chipsec.chipset import UnknownChipsetError, print_supported_chipsets # Chipset/CPU Detection # # ################################################################### -def platform(argv): +class PlatformCommand(BaseCommand): """ chipsec_util platform """ - try: - print_supported_chipsets() - logger().log("") - chipsec_util._cs.print_chipset() - except UnknownChipsetError, msg: - logger().error( msg ) -chipsec_util.commands['platform'] = {'func' : platform, 'start_driver' : True , 'help' : platform.__doc__ } + def requires_driver(self): + return True + + def run(self): + try: + print_supported_chipsets() + self.logger.log("") + self.cs.print_chipset() + except UnknownChipsetError, msg: + self.logger.error( msg ) + +commands = { 'platform': PlatformCommand } diff --git a/source/tool/chipsec/utilcmd/cmos_cmd.py b/source/tool/chipsec/utilcmd/cmos_cmd.py index ebe476f5..4dd9504f 100644 --- a/source/tool/chipsec/utilcmd/cmos_cmd.py +++ b/source/tool/chipsec/utilcmd/cmos_cmd.py @@ -24,19 +24,12 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.cmos import CMOS, CmosRuntimeError -def cmos(argv): +class CMOSCommand(BaseCommand): """ >>> chipsec_util cmos dump >>> chipsec_util cmos readl|writel|readh|writeh [byte_val] @@ -47,46 +40,53 @@ def cmos(argv): >>> chipsec_util cmos rl 0x0 >>> chipsec_util cmos wh 0x0 0xCC """ - if 3 > len(argv): - print cmos.__doc__ - return - try: - _cmos = CMOS( ) - except CmosRuntimeError, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - t = time.time() + def run(self): + if len(self.argv) < 3: + print CMOSCommand.__doc__ + return - if ( 'dump' == op ): - logger().log( "[CHIPSEC] Dumping CMOS memory.." ) - _cmos.dump() - elif ( 'readl' == op ): - off = int(argv[3],16) - val = _cmos.read_cmos_low( off ) - logger().log( "[CHIPSEC] CMOS low byte 0x%X = 0x%X" % (off, val) ) - elif ( 'writel' == op ): - off = int(argv[3],16) - val = int(argv[4],16) - logger().log( "[CHIPSEC] Writing CMOS low byte 0x%X <- 0x%X " % (off, val) ) - _cmos.write_cmos_low( off, val ) - elif ( 'readh' == op ): - off = int(argv[3],16) - val = _cmos.read_cmos_high( off ) - logger().log( "[CHIPSEC] CMOS high byte 0x%X = 0x%X" % (off, val) ) - elif ( 'writeh' == op ): - off = int(argv[3],16) - val = int(argv[4],16) - logger().log( "[CHIPSEC] Writing CMOS high byte 0x%X <- 0x%X " % (off, val) ) - _cmos.write_cmos_high( off, val ) - else: - logger().error( "unknown command-line option '%.32s'" % op ) - print usage - return + try: + _cmos = CMOS( ) + except CmosRuntimeError, msg: + print msg + return - logger().log( "[CHIPSEC] (cmos) time elapsed %.3f" % (time.time()-t) ) + op = self.argv[2] + t = time.time() + if ( 'dump' == op ): + self.logger.log( "[CHIPSEC] Dumping CMOS memory.." ) + _cmos.dump() + elif ( 'readl' == op ): + off = int(self.argv[3],16) + val = _cmos.read_cmos_low( off ) + self.logger.log( "[CHIPSEC] CMOS low byte 0x%X = 0x%X" % (off, val) ) + elif ( 'writel' == op ): + off = int(self.argv[3],16) + val = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Writing CMOS low byte 0x%X <- 0x%X " % (off, val) ) + _cmos.write_cmos_low( off, val ) + elif ( 'readh' == op ): + off = int(self.argv[3],16) + val = _cmos.read_cmos_high( off ) + self.logger.log( "[CHIPSEC] CMOS high byte 0x%X = 0x%X" % (off, val) ) + elif ( 'writeh' == op ): + off = int(self.argv[3],16) + val = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Writing CMOS high byte 0x%X <- 0x%X " % (off, val) ) + _cmos.write_cmos_high( off, val ) + else: + self.logger.error( "unknown command-line option '%.32s'" % op ) + print CMOSCommand.__doc__ + return -chipsec_util.commands['cmos'] = {'func' : cmos, 'start_driver' : True, 'help' : cmos.__doc__ } + self.logger.log( "[CHIPSEC] (cmos) time elapsed %.3f" % (time.time()-t) ) + +commands = { 'cmos': CMOSCommand } diff --git a/source/tool/chipsec/utilcmd/cpu_cmd.py b/source/tool/chipsec/utilcmd/cpu_cmd.py index 3e07441f..625c1ddf 100644 --- a/source/tool/chipsec/utilcmd/cpu_cmd.py +++ b/source/tool/chipsec/utilcmd/cpu_cmd.py @@ -24,21 +24,17 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util import chipsec.hal.cpu - -from chipsec.logger import * +from chipsec.command import BaseCommand # ################################################################### # # CPU utility # # ################################################################### -def cpu_cmd( argv ): +class CPUCommand(BaseCommand): """ >>> chipsec_util cpu info >>> chipsec_util cpu cr [value] @@ -49,58 +45,64 @@ def cpu_cmd( argv ): >>> chipsec_util cpu cr 0 0 >>> chipsec_util cpu cr 0 4 0x0 """ - if len(argv) < 3: - print cpu_cmd.__doc__ - return - op = argv[2] - t = time.time() - try: - _cpu = chipsec.hal.cpu.CPU( chipsec_util._cs ) - except chipsec.hal.cpu.CPURuntimeError, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - #_cpu = chipsec_util._cs.cpu + def run(self): + if len(self.argv) < 3: + print CPUCommand.__doc__ + return + op = self.argv[2] + t = time.time() - if 'info' == op: - logger().log( "[CHIPSEC] CPU information:" ) - ht = _cpu.is_HT_active() - threads_per_core = _cpu.get_number_logical_processor_per_core() - threads_per_pkg = _cpu.get_number_logical_processor_per_package() - cores_per_pkg = _cpu.get_number_physical_processor_per_package() - threads_count = _cpu.get_number_threads_from_APIC_table() - sockets_count = _cpu.get_number_sockets_from_APIC_table() - logger().log( " Hyper-Threading : %s" % ('Enabled' if ht else 'Disabled') ) - logger().log( " CPU cores per package : %d" % cores_per_pkg ) - logger().log( " CPU threads per core : %d" % threads_per_core ) - logger().log( " CPU threads per package : %d" % threads_per_pkg ) - logger().log( " Number of sockets : %d" % sockets_count ) - logger().log( " Number of CPU threads : %d" % threads_count ) - - elif 'cr' == op: - if len(argv) < 5: - print cpu_cmd.__doc__ + try: + _cpu = chipsec.hal.cpu.CPU(self.cs) + except chipsec.hal.cpu.CPURuntimeError, msg: + print msg return - cpu_thread_id = int(argv[3],10) - cr_number = int(argv[4],16) + if 'info' == op: + self.logger.log( "[CHIPSEC] CPU information:" ) + ht = _cpu.is_HT_active() + threads_per_core = _cpu.get_number_logical_processor_per_core() + threads_per_pkg = _cpu.get_number_logical_processor_per_package() + cores_per_pkg = _cpu.get_number_physical_processor_per_package() + threads_count = _cpu.get_number_threads_from_APIC_table() + sockets_count = _cpu.get_number_sockets_from_APIC_table() + self.logger.log( " Hyper-Threading : %s" % ('Enabled' if ht else 'Disabled') ) + self.logger.log( " CPU cores per package : %d" % cores_per_pkg ) + self.logger.log( " CPU threads per core : %d" % threads_per_core ) + self.logger.log( " CPU threads per package : %d" % threads_per_pkg ) + self.logger.log( " Number of sockets : %d" % sockets_count ) + self.logger.log( " Number of CPU threads : %d" % threads_count ) + + elif 'cr' == op: + if len(self.argv) < 5: + print CPUCommand.__doc__ + return + + cpu_thread_id = int(self.argv[3],10) + cr_number = int(self.argv[4],16) + + if len(self.argv) > 5: + value = int(self.argv[5], 16) + self.logger.log( "[CHIPSEC] CPU: %d write CR%d <- 0x%08X" % (cpu_thread_id, cr_number, value) ) + self.cs.cpu.write_cr( cpu_thread_id, cr_number, value ) + return True + else: + value = self.cs.cpu.read_cr( cpu_thread_id, cr_number ) + self.logger.log( "[CHIPSEC] CPU: %d read CR%d -> 0x%08X" % (cpu_thread_id, cr_number, value) ) + return value - if len(argv) > 5: - value = int(argv[5], 16) - logger().log( "[CHIPSEC] CPU: %d write CR%d <- 0x%08X" % (cpu_thread_id, cr_number, value) ) - chipsec_util._cs.cpu.write_cr( cpu_thread_id, cr_number, value ) - return True else: - value = chipsec_util._cs.cpu.read_cr( cpu_thread_id, cr_number ) - logger().log( "[CHIPSEC] CPU: %d read CR%d -> 0x%08X" % (cpu_thread_id, cr_number, value) ) - return value - - else: - print cpu_cmd.__doc__ - return - - logger().log( "[CHIPSEC] (cpu) time elapsed %.3f" % (time.time()-t) ) + print CPUCommand.__doc__ + return + + self.logger.log( "[CHIPSEC] (cpu) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['cpu'] = {'func' : cpu_cmd, 'start_driver' : True, 'help' : cpu_cmd.__doc__ } +commands = { 'cpu': CPUCommand } diff --git a/source/tool/chipsec/utilcmd/cpuid_cmd.py b/source/tool/chipsec/utilcmd/cpuid_cmd.py index b6896738..d28082be 100644 --- a/source/tool/chipsec/utilcmd/cpuid_cmd.py +++ b/source/tool/chipsec/utilcmd/cpuid_cmd.py @@ -24,22 +24,14 @@ __version__ = '1.0' -import os -import sys -import time - -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * -#_cs = cs() +from chipsec.command import BaseCommand # ################################################################### # # CPUid # # ################################################################### -def cpuid(argv): +class CPUIDCommand(BaseCommand): """ >>> chipsec_util cpuid [ecx] @@ -47,22 +39,29 @@ def cpuid(argv): >>> chipsec_util cpuid 40000000 """ - if 3 > len(argv): - print cpuid.__doc__ - return - eax = int(argv[2],16) - ecx = int(argv[3],16) if 4 == len(argv) else 0 + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - logger().log( "[CHIPSEC] CPUID < EAX: 0x%08X" % eax) - logger().log( "[CHIPSEC] ECX: 0x%08X" % ecx) + def run(self): + if len(self.argv) < 3: + print CPUIDCommand.__doc__ + return - val = chipsec_util._cs.cpuid.cpuid( eax, ecx ) + eax = int(self.argv[2],16) + ecx = int(self.argv[3],16) if 4 == len(self.argv) else 0 - logger().log( "[CHIPSEC] CPUID > EAX: 0x%08X" % (val[0]) ) - logger().log( "[CHIPSEC] EBX: 0x%08X" % (val[1]) ) - logger().log( "[CHIPSEC] ECX: 0x%08X" % (val[2]) ) - logger().log( "[CHIPSEC] EDX: 0x%08X" % (val[3]) ) + self.logger.log( "[CHIPSEC] CPUID < EAX: 0x%08X" % eax) + self.logger.log( "[CHIPSEC] ECX: 0x%08X" % ecx) + val = self.cs.cpuid.cpuid( eax, ecx ) -chipsec_util.commands['cpuid'] = {'func' : cpuid , 'start_driver' : True, 'help' : cpuid.__doc__ } + self.logger.log( "[CHIPSEC] CPUID > EAX: 0x%08X" % (val[0]) ) + self.logger.log( "[CHIPSEC] EBX: 0x%08X" % (val[1]) ) + self.logger.log( "[CHIPSEC] ECX: 0x%08X" % (val[2]) ) + self.logger.log( "[CHIPSEC] EDX: 0x%08X" % (val[3]) ) + +commands = { 'cpuid': CPUIDCommand } diff --git a/source/tool/chipsec/utilcmd/decode_cmd.py b/source/tool/chipsec/utilcmd/decode_cmd.py index ddbf4928..bd7cfcbc 100644 --- a/source/tool/chipsec/utilcmd/decode_cmd.py +++ b/source/tool/chipsec/utilcmd/decode_cmd.py @@ -36,13 +36,10 @@ This will create multiple log files, binaries, and directories that correspond t __version__ = '1.0' import os -import sys import time -import chipsec_util - -from chipsec.logger import * -import chipsec.file +from chipsec.file import read_file, write_file +from chipsec.command import BaseCommand import chipsec.hal.spi as spi import chipsec.hal.spi_descriptor as spi_descriptor @@ -50,9 +47,7 @@ import chipsec.hal.spi_uefi as spi_uefi import chipsec.hal.uefi as uefi -_uefi = uefi.UEFI( chipsec_util._cs ) - -def decode(argv): +class DecodeCommand(BaseCommand): """ >>> chipsec_util decode [fw_type] @@ -64,70 +59,74 @@ def decode(argv): >>> chipsec_util decode spi.bin vss """ - - if 3 > len(argv): - print decode.__doc__ - return - - if argv[2] == "types": - print "\n should be in [ %s ]\n" % ( " | ".join( ["%s" % t for t in uefi.fw_types] ) ) - return - - rom_file = argv[2] - fwtype = '' - if 4 == len(argv): - fwtype = argv[3] - - logger().log( "[CHIPSEC] Decoding SPI ROM image from a file '%s'" % rom_file ) - t = time.time() - - f = chipsec.file.read_file( rom_file ) - (fd_off, fd) = spi_descriptor.get_spi_flash_descriptor( f ) - if (-1 == fd_off) or (fd is None): - logger().error( "Could not find SPI Flash descriptor in the binary '%s'" % rom_file ) + def requires_driver(self): return False - logger().log( "[CHIPSEC] Found SPI Flash descriptor at offset 0x%x in the binary '%s'" % (fd_off, rom_file) ) - rom = f[fd_off:] - # Decoding Flash Descriptor - #logger().LOG_COMPLETE_FILE_NAME = os.path.join( pth, 'flash_descriptor.log' ) - #parse_spi_flash_descriptor( fd ) + def run(self): + if len(self.argv) < 3: + print DecodeCommand.__doc__ + return + + _uefi = uefi.UEFI( self.cs ) + if self.argv[2] == "types": + print "\n should be in [ %s ]\n" % ( " | ".join( ["%s" % t for t in uefi.fw_types] ) ) + return + + rom_file = self.argv[2] - # Decoding SPI Flash Regions - # flregs[r] = (r,SPI_REGION_NAMES[r],flreg,base,limit,notused) - flregs = spi_descriptor.get_spi_regions( fd ) - if flregs is None: - logger().error( "SPI Flash descriptor region is not valid" ) - return False + fwtype = '' + if 4 == len(self.argv): + fwtype = self.argv[3] - _orig_logname = logger().LOG_FILE_NAME + self.logger.log( "[CHIPSEC] Decoding SPI ROM image from a file '%s'" % rom_file ) + t = time.time() - pth = os.path.join( chipsec_util._cs.helper.getcwd(), rom_file + ".dir" ) - if not os.path.exists( pth ): - os.makedirs( pth ) + f = read_file( rom_file ) + (fd_off, fd) = spi_descriptor.get_spi_flash_descriptor( f ) + if (-1 == fd_off) or (fd is None): + self.logger.error( "Could not find SPI Flash descriptor in the binary '%s'" % rom_file ) + return False - for r in flregs: - idx = r[0] - name = r[1] - base = r[3] - limit = r[4] - notused = r[5] - if not notused: - region_data = rom[base:limit+1] - fname = os.path.join( pth, '%d_%04X-%04X_%s.bin' % (idx, base, limit, name) ) - chipsec.file.write_file( fname, region_data ) - if spi.FLASH_DESCRIPTOR == idx: - # Decoding Flash Descriptor - logger().set_log_file( os.path.join( pth, fname + '.log' ) ) - spi_descriptor.parse_spi_flash_descriptor( region_data ) - elif spi.BIOS == idx: - # Decoding EFI Firmware Volumes - logger().set_log_file( os.path.join( pth, fname + '.log' ) ) - spi_uefi.decode_uefi_region(_uefi, pth, fname, fwtype) + self.logger.log( "[CHIPSEC] Found SPI Flash descriptor at offset 0x%x in the binary '%s'" % (fd_off, rom_file) ) + rom = f[fd_off:] + # Decoding Flash Descriptor + #self.logger.LOG_COMPLETE_FILE_NAME = os.path.join( pth, 'flash_descriptor.log' ) + #parse_spi_flash_descriptor( fd ) - logger().set_log_file( _orig_logname ) - logger().log( "[CHIPSEC] (decode) time elapsed %.3f" % (time.time()-t) ) + # Decoding SPI Flash Regions + # flregs[r] = (r,SPI_REGION_NAMES[r],flreg,base,limit,notused) + flregs = spi_descriptor.get_spi_regions( fd ) + if flregs is None: + self.logger.error( "SPI Flash descriptor region is not valid" ) + return False + _orig_logname = self.logger.LOG_FILE_NAME -chipsec_util.commands['decode'] = {'func' : decode, 'start_driver' : False, 'help' : decode.__doc__ } + pth = os.path.join( self.cs.helper.getcwd(), rom_file + ".dir" ) + if not os.path.exists( pth ): + os.makedirs( pth ) + + for r in flregs: + idx = r[0] + name = r[1] + base = r[3] + limit = r[4] + notused = r[5] + if not notused: + region_data = rom[base:limit+1] + fname = os.path.join( pth, '%d_%04X-%04X_%s.bin' % (idx, base, limit, name) ) + write_file( fname, region_data ) + if spi.FLASH_DESCRIPTOR == idx: + # Decoding Flash Descriptor + self.logger.set_log_file( os.path.join( pth, fname + '.log' ) ) + spi_descriptor.parse_spi_flash_descriptor( region_data ) + elif spi.BIOS == idx: + # Decoding EFI Firmware Volumes + self.logger.set_log_file( os.path.join( pth, fname + '.log' ) ) + spi_uefi.decode_uefi_region(_uefi, pth, fname, fwtype) + + self.logger.set_log_file( _orig_logname ) + self.logger.log( "[CHIPSEC] (decode) time elapsed %.3f" % (time.time()-t) ) + +commands = { "decode": DecodeCommand } diff --git a/source/tool/chipsec/utilcmd/desc_cmd.py b/source/tool/chipsec/utilcmd/desc_cmd.py index 767c4100..c615a743 100644 --- a/source/tool/chipsec/utilcmd/desc_cmd.py +++ b/source/tool/chipsec/utilcmd/desc_cmd.py @@ -27,21 +27,10 @@ The idt and gdt commands print the IDT and GDT, respectively. __version__ = '1.0' -import os -import sys -import time - -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - -#from chipsec.hal.msr import Msr - +from chipsec.command import BaseCommand # CPU descriptor tables -def idt(argv): +class IDTCommand(BaseCommand): """ >>> chipsec_util idt|gdt|ldt [cpu_id] @@ -50,14 +39,19 @@ def idt(argv): >>> chipsec_util idt 0 >>> chipsec_util gdt """ - if (2 == len(argv)): - logger().log( "[CHIPSEC] Dumping IDT of %d CPU threads" % chipsec_util._cs.msr.get_cpu_thread_count() ) - chipsec_util._cs.msr.IDT_all( 4 ) - elif (3 == len(argv)): - tid = int(argv[2],16) - chipsec_util._cs.msr.IDT( tid, 4 ) -def gdt(argv): + def requires_driver(self): + return True + + def run(self): + if (2 == len(self.argv)): + self.logger.log( "[CHIPSEC] Dumping IDT of %d CPU threads" % self.cs.msr.get_cpu_thread_count() ) + self.cs.msr.IDT_all( 4 ) + elif (3 == len(self.argv)): + tid = int(self.argv[2],16) + self.cs.msr.IDT( tid, 4 ) + +class GDTCommand(BaseCommand): """ >>> chipsec_util idt|gdt|ldt [cpu_id] @@ -66,14 +60,19 @@ def gdt(argv): >>> chipsec_util idt 0 >>> chipsec_util gdt """ - if (2 == len(argv)): - logger().log( "[CHIPSEC] Dumping GDT of %d CPU threads" % chipsec_util._cs.msr.get_cpu_thread_count() ) - chipsec_util._cs.msr.GDT_all( 4 ) - elif (3 == len(argv)): - tid = int(argv[2],16) - chipsec_util._cs.msr.GDT( tid, 4 ) -def ldt(argv): + def requires_driver(self): + return True + + def run(self): + if (2 == len(self.argv)): + self.logger.log( "[CHIPSEC] Dumping GDT of %d CPU threads" % self.cs.msr.get_cpu_thread_count() ) + self.cs.msr.GDT_all( 4 ) + elif (3 == len(self.argv)): + tid = int(self.argv[2],16) + self.cs.msr.GDT( tid, 4 ) + +class LDTCommand(BaseCommand): """ >>> chipsec_util idt|gdt|ldt [cpu_id] @@ -82,8 +81,10 @@ def ldt(argv): >>> chipsec_util idt 0 >>> chipsec_util gdt """ - logger().error( "[CHIPSEC] ldt not implemented" ) + def requires_driver(self): + return True + def run(self): + self.logger.error( "[CHIPSEC] ldt not implemented" ) -chipsec_util.commands['idt'] = {'func' : idt, 'start_driver' : True, 'help' : idt.__doc__ } -chipsec_util.commands['gdt'] = {'func' : gdt, 'start_driver' : True, 'help' : gdt.__doc__ } +commands = { 'idt': IDTCommand, 'gdt': GDTCommand } diff --git a/source/tool/chipsec/utilcmd/interrupts_cmd.py b/source/tool/chipsec/utilcmd/interrupts_cmd.py index 0863745b..d78b31c1 100644 --- a/source/tool/chipsec/utilcmd/interrupts_cmd.py +++ b/source/tool/chipsec/utilcmd/interrupts_cmd.py @@ -23,15 +23,7 @@ __version__ = '1.0' -import os -import sys -import time - -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.interrupts import Interrupts # ################################################################### @@ -39,7 +31,7 @@ from chipsec.hal.interrupts import Interrupts # CPU Interrupts # # ################################################################### -def smi(argv): +class SMICommand(BaseCommand): """ >>> chipsec_util smi [RAX] [RBX] [RCX] [RDX] [RSI] [RDI] @@ -48,42 +40,49 @@ def smi(argv): >>> chipsec_util smi 0x0 0xDE 0x0 >>> chipsec_util smi 0x0 0xDE 0x0 0xAAAAAAAAAAAAAAAA .. """ - try: - interrupts = Interrupts( chipsec_util._cs ) - except RuntimeError, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - SMI_code_port_value = 0xF - SMI_data_port_value = 0x0 - if (2 == len(argv)): - print smi.__doc__ - elif (4 < len(argv)): - thread_id = int(argv[2],16) - SMI_code_port_value = int(argv[3],16) - SMI_data_port_value = int(argv[4],16) - logger().log( "[CHIPSEC] Sending SW SMI (code: 0x%02X, data: 0x%02X).." % (SMI_code_port_value, SMI_data_port_value) ) - if (5 == len(argv)): - interrupts.send_SMI_APMC( SMI_code_port_value, SMI_data_port_value ) - elif (11 == len(argv)): - _rax = int(argv[5],16) - _rbx = int(argv[6],16) - _rcx = int(argv[7],16) - _rdx = int(argv[8],16) - _rsi = int(argv[9],16) - _rdi = int(argv[10],16) - logger().log( " RAX: 0x%016X (AX will be overwridden with values of SW SMI ports B2/B3)" % _rax ) - logger().log( " RBX: 0x%016X" % _rbx ) - logger().log( " RCX: 0x%016X" % _rcx ) - logger().log( " RDX: 0x%016X (DX will be overwridden with 0x00B2)" % _rdx ) - logger().log( " RSI: 0x%016X" % _rsi ) - logger().log( " RDI: 0x%016X" % _rdi ) - interrupts.send_SW_SMI( thread_id, SMI_code_port_value, SMI_data_port_value, _rax, _rbx, _rcx, _rdx, _rsi, _rdi ) - else: print smi.__doc__ - else: print smi.__doc__ + def run(self): + try: + interrupts = Interrupts( self.cs ) + except RuntimeError, msg: + print msg + return + + SMI_code_port_value = 0xF + SMI_data_port_value = 0x0 + if (2 == len(self.argv)): + print SMICommand.__doc__ + elif (4 < len(self.argv)): + thread_id = int(self.argv[2],16) + SMI_code_port_value = int(self.argv[3],16) + SMI_data_port_value = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Sending SW SMI (code: 0x%02X, data: 0x%02X).." % (SMI_code_port_value, SMI_data_port_value) ) + if (5 == len(self.argv)): + interrupts.send_SMI_APMC( SMI_code_port_value, SMI_data_port_value ) + elif (11 == len(self.argv)): + _rax = int(self.argv[5],16) + _rbx = int(self.argv[6],16) + _rcx = int(self.argv[7],16) + _rdx = int(self.argv[8],16) + _rsi = int(self.argv[9],16) + _rdi = int(self.argv[10],16) + self.logger.log( " RAX: 0x%016X (AX will be overwridden with values of SW SMI ports B2/B3)" % _rax ) + self.logger.log( " RBX: 0x%016X" % _rbx ) + self.logger.log( " RCX: 0x%016X" % _rcx ) + self.logger.log( " RDX: 0x%016X (DX will be overwridden with 0x00B2)" % _rdx ) + self.logger.log( " RSI: 0x%016X" % _rsi ) + self.logger.log( " RDI: 0x%016X" % _rdi ) + interrupts.send_SW_SMI( thread_id, SMI_code_port_value, SMI_data_port_value, _rax, _rbx, _rcx, _rdx, _rsi, _rdi ) + else: print SMICommand.__doc__ + else: print SMICommand.__doc__ -def nmi(argv): +class NMICommand(BaseCommand): """ >>> chipsec_util nmi @@ -91,18 +90,17 @@ def nmi(argv): >>> chipsec_util nmi """ - if 2 < len(argv): - print nmi.__doc__ + def requires_driver(self): + return True - try: - interrupts = Interrupts( chipsec_util._cs ) - except RuntimeError, msg: - print msg - return + def run(self): + try: + interrupts = Interrupts( self.cs ) + except RuntimeError, msg: + print msg + return - logger().log( "[CHIPSEC] Sending NMI#.." ) - interrupts.send_NMI() + self.logger.log( "[CHIPSEC] Sending NMI#.." ) + interrupts.send_NMI() - -chipsec_util.commands['nmi'] = {'func' : nmi, 'start_driver' : True, 'help' : nmi.__doc__ } -chipsec_util.commands['smi'] = {'func' : smi, 'start_driver' : True, 'help' : smi.__doc__ } +commands = { 'smi': SMICommand, 'nmi': NMICommand } diff --git a/source/tool/chipsec/utilcmd/io_cmd.py b/source/tool/chipsec/utilcmd/io_cmd.py index 704ee50d..5bcb224b 100644 --- a/source/tool/chipsec/utilcmd/io_cmd.py +++ b/source/tool/chipsec/utilcmd/io_cmd.py @@ -27,20 +27,14 @@ The io command allows direct access to read and write I/O port space. __version__ = '1.0' -import os -import sys import time import chipsec_util - import chipsec.hal.iobar - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand # Port I/O -def port_io(argv): +class PortIOCommand(BaseCommand): """ >>> chipsec_util io list >>> chipsec_util io [value] @@ -51,46 +45,54 @@ def port_io(argv): >>> chipsec_util io 0x61 1 >>> chipsec_util io 0x430 byte 0x0 """ - if 3 > len(argv): - print port_io.__doc__ - return - try: - _iobar = chipsec.hal.iobar.iobar( chipsec_util._cs ) - except chipsec.hal.iobar.IOBARRuntimeError, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - if ( 'list' == op ): - _iobar.list_IO_BARs() - return + def run(self): + if len(self.argv) < 3: + print PortIOCommand.__doc__ + return - t = time.time() + try: + _iobar = chipsec.hal.iobar.iobar( self.cs ) + except chipsec.hal.iobar.IOBARRuntimeError, msg: + print msg + return - if len(argv) < 3: - print port_io.__doc__ - return + op = self.argv[2] + if ( 'list' == op ): + _iobar.list_IO_BARs() + return - io_port = int(argv[2],16) + t = time.time() - width = 0x1 - if len(argv) > 3: - width = chipsec_util.get_option_width(argv[3]) if chipsec_util.is_option_valid_width(argv[3]) else int(argv[3],16) + if len(self.argv) < 3: + print PortIOCommand.__doc__ + return - if 5 == len(argv): - value = int(argv[4], 16) - logger().log( "[CHIPSEC] OUT 0x%04X <- 0x%08X (size = 0x%02x)" % (io_port, value, width) ) - if 0x1 == width: chipsec_util._cs.io.write_port_byte( io_port, value ) - elif 0x2 == width: chipsec_util._cs.io.write_port_word( io_port, value ) - elif 0x4 == width: chipsec_util._cs.io.write_port_dword( io_port, value ) - else: - if 0x1 == width: value = chipsec_util._cs.io.read_port_byte( io_port ) - elif 0x2 == width: value = chipsec_util._cs.io.read_port_word( io_port ) - elif 0x4 == width: value = chipsec_util._cs.io.read_port_dword( io_port ) - logger().log( "[CHIPSEC] IN 0x%04X -> 0x%08X (size = 0x%02x)" % (io_port, value, width) ) + io_port = int(self.argv[2],16) - logger().log( "[CHIPSEC] (io) time elapsed %.3f" % (time.time()-t) ) + width = 0x1 + if len(self.argv) > 3: + width = chipsec_util.get_option_width(self.argv[3]) if chipsec_util.is_option_valid_width(self.argv[3]) else int(self.argv[3],16) + + if 5 == len(self.argv): + value = int(self.argv[4], 16) + self.logger.log( "[CHIPSEC] OUT 0x%04X <- 0x%08X (size = 0x%02x)" % (io_port, value, width) ) + if 0x1 == width: self.cs.io.write_port_byte( io_port, value ) + elif 0x2 == width: self.cs.io.write_port_word( io_port, value ) + elif 0x4 == width: self.cs.io.write_port_dword( io_port, value ) + else: + if 0x1 == width: value = self.cs.io.read_port_byte( io_port ) + elif 0x2 == width: value = self.cs.io.read_port_word( io_port ) + elif 0x4 == width: value = self.cs.io.read_port_dword( io_port ) + self.logger.log( "[CHIPSEC] IN 0x%04X -> 0x%08X (size = 0x%02x)" % (io_port, value, width) ) + + self.logger.log( "[CHIPSEC] (io) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['io'] = {'func' : port_io, 'start_driver' : True, 'help' : port_io.__doc__ } +commands = { 'io': PortIOCommand } diff --git a/source/tool/chipsec/utilcmd/iommu_cmd.py b/source/tool/chipsec/utilcmd/iommu_cmd.py index e24eecfd..a070d5b6 100644 --- a/source/tool/chipsec/utilcmd/iommu_cmd.py +++ b/source/tool/chipsec/utilcmd/iommu_cmd.py @@ -27,8 +27,6 @@ Command-line utility providing access to IOMMU engines __version__ = '1.0' -import os -import sys import time import chipsec_util @@ -37,10 +35,11 @@ from chipsec.logger import * from chipsec.file import * from chipsec.hal.iommu import * import chipsec.hal.acpi +from chipsec.command import BaseCommand # I/O Memory Management Unit (IOMMU), e.g. Intel VT-d -def iommu_cmd(argv): +class IOMMUCommand(BaseCommand): """ >>> chipsec_util iommu list >>> chipsec_util iommu config [iommu_engine] @@ -54,54 +53,63 @@ def iommu_cmd(argv): >>> chipsec_util iommu status GFXVTD >>> chipsec_util iommu enable VTD """ - if len(argv) < 3: - print iommu_cmd.__doc__ - return - op = argv[2] - t = time.time() - - try: - _iommu = iommu( chipsec_util._cs ) - except IOMMUError, msg: - print msg - return + + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True + + def run(self): + if len(self.argv) < 3: + print IOMMUCommand.__doc__ + return + op = self.argv[2] + t = time.time() - if ( 'list' == op ): - logger().log( "[CHIPSEC] Enumerating supported IOMMU engines.." ) - logger().log( IOMMU_ENGINES.keys() ) - elif ( 'config' == op or 'status' == op or 'enable' == op or 'disable' == op ): - if len(argv) > 3: - if argv[3] in IOMMU_ENGINES.keys(): - _iommu_engines = [ argv[3] ] + try: + _iommu = iommu( self.cs ) + except IOMMUError, msg: + print msg + return + + if ( 'list' == op ): + self.logger.log( "[CHIPSEC] Enumerating supported IOMMU engines.." ) + self.logger.log( IOMMU_ENGINES.keys() ) + elif ( 'config' == op or 'status' == op or 'enable' == op or 'disable' == op ): + if len(self.argv) > 3: + if self.argv[3] in IOMMU_ENGINES.keys(): + _iommu_engines = [ self.argv[3] ] + else: + self.logger.error( "IOMMU name %s not recognized. Run 'iommu list' command for supported IOMMU names" % self.argv[3] ) + return else: - logger().error( "IOMMU name %s not recognized. Run 'iommu list' command for supported IOMMU names" % argv[3] ) - return + _iommu_engines = IOMMU_ENGINES.keys() + + if 'config' == op: + + try: + _acpi = chipsec.hal.acpi.ACPI( self.cs ) + except chipsec.hal.acpi.AcpiRuntimeError, msg: + print msg + return + + if _acpi.is_ACPI_table_present( chipsec.hal.acpi.ACPI_TABLE_SIG_DMAR ): + self.logger.log( "[CHIPSEC] Dumping contents of DMAR ACPI table..\n" ) + _acpi.dump_ACPI_table( chipsec.hal.acpi.ACPI_TABLE_SIG_DMAR ) + else: + self.logger.log( "[CHIPSEC] Couldn't find DMAR ACPI table\n" ) + + for e in _iommu_engines: + if 'config' == op: _iommu.dump_IOMMU_configuration( e ) + elif 'status' == op: _iommu.dump_IOMMU_status( e ) + elif 'enable' == op: _iommu.set_IOMMU_Translation( e, 1 ) + elif 'disable' == op: _iommu.set_IOMMU_Translation( e, 0 ) else: - _iommu_engines = IOMMU_ENGINES.keys() + print IOMMUCommand.__doc__ + return + + self.logger.log( "[CHIPSEC] (iommu) time elapsed %.3f" % (time.time()-t) ) - if 'config' == op: - try: - _acpi = chipsec.hal.acpi.ACPI( chipsec_util._cs ) - except chipsec.hal.acpi.AcpiRuntimeError, msg: - print msg - return - - if _acpi.is_ACPI_table_present( chipsec.hal.acpi.ACPI_TABLE_SIG_DMAR ): - logger().log( "[CHIPSEC] Dumping contents of DMAR ACPI table..\n" ) - _acpi.dump_ACPI_table( chipsec.hal.acpi.ACPI_TABLE_SIG_DMAR ) - else: - logger().log( "[CHIPSEC] Couldn't find DMAR ACPI table\n" ) - - for e in _iommu_engines: - if 'config' == op: _iommu.dump_IOMMU_configuration( e ) - elif 'status' == op: _iommu.dump_IOMMU_status( e ) - elif 'enable' == op: _iommu.set_IOMMU_Translation( e, 1 ) - elif 'disable' == op: _iommu.set_IOMMU_Translation( e, 0 ) - else: - print iommu_cmd.__doc__ - return - - logger().log( "[CHIPSEC] (iommu) time elapsed %.3f" % (time.time()-t) ) - -chipsec_util.commands['iommu'] = {'func' : iommu_cmd, 'start_driver' : True, 'help' : iommu.__doc__ } \ No newline at end of file +commands = { 'iommu': IOMMUCommand } diff --git a/source/tool/chipsec/utilcmd/mem_cmd.py b/source/tool/chipsec/utilcmd/mem_cmd.py index 88cc29d0..79ec3d5f 100644 --- a/source/tool/chipsec/utilcmd/mem_cmd.py +++ b/source/tool/chipsec/utilcmd/mem_cmd.py @@ -28,18 +28,14 @@ The mem command provides direct access to read and write physical memory. __version__ = '1.0' import os -import sys import time import chipsec_util - -import chipsec.file -import chipsec.logger - -from chipsec.logger import * -from chipsec.file import * -import chipsec.chipset import chipsec.defines +import chipsec.file + +from chipsec.logger import print_buffer +from chipsec.command import BaseCommand def read_mem(pa, size = chipsec.defines.BOUNDARY_4KB): try: @@ -90,7 +86,7 @@ def dump_region_to_path(path, pa_start, pa_end): # Physical Memory -def mem(argv): +class MemCommand(BaseCommand): """ >>> chipsec_util mem [value|buffer_file] >>> @@ -112,105 +108,112 @@ def mem(argv): >>> chipsec_util mem pagedump 0xFED00000 0x100000 """ - phys_address = 0 - size = 0x100 - if 3 > len(argv): - print mem.__doc__ - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - t = time.time() + def run(self): + size = 0x100 - if 'allocate' == op and 4 == len(argv): - size = int(argv[3],16) - (va, pa) = chipsec_util._cs.mem.alloc_physical_mem( size ) - logger().log( '[CHIPSEC] Allocated %X bytes of physical memory: VA = 0x%016X, PA = 0x%016X' % (size, va, pa) ) - - elif 'pagedump' == op and len(argv) > 3: - start = long(argv[3],16) - length = long(argv[4],16) if len(argv) > 4 else chipsec.defines.BOUNDARY_4KB - end = start + length - - dump_region_to_path( chipsec.file.get_main_dir(), start, end ) - - elif 'read' == op: - phys_address = int(argv[3],16) - size = int(argv[4],16) if len(argv) > 4 else 0x100 - logger().log( '[CHIPSEC] reading buffer from memory: PA = 0x%016X, len = 0x%X..' % (phys_address, size) ) - buffer = chipsec_util._cs.mem.read_physical_mem( phys_address, size ) - if len(argv) > 5: - buf_file = argv[5] - chipsec.file.write_file( buf_file, buffer ) - logger().log( "[CHIPSEC] written 0x%X bytes to '%s'" % (len(buffer), buf_file) ) - else: - print_buffer( buffer ) - - elif 'readval' == op: - phys_address = int(argv[3],16) - width = 0x4 - if len(argv) > 4: - width = chipsec_util.get_option_width(argv[4]) if chipsec_util.is_option_valid_width(argv[4]) else int(argv[4],16) - logger().log( '[CHIPSEC] reading %X-byte value from PA 0x%016X..' % (width, phys_address) ) - if 0x1 == width: value = chipsec_util._cs.mem.read_physical_mem_byte ( phys_address ) - elif 0x2 == width: value = chipsec_util._cs.mem.read_physical_mem_word ( phys_address ) - elif 0x4 == width: value = chipsec_util._cs.mem.read_physical_mem_dword( phys_address ) - logger().log( '[CHIPSEC] value = 0x%X' % value ) - - elif 'write' == op: - phys_address = int(argv[3],16) - if len(argv) > 4: - size = int(argv[4],16) - else: - logger().error( "must specify argument in 'mem write'" ) + if len(self.argv) < 3: + print MemCommand.__doc__ return - if len(argv) > 5: - buf_file = argv[5] - if not os.path.exists( buf_file ): - #buffer = buf_file.decode('hex') - try: - buffer = bytearray.fromhex(buf_file) - except ValueError, e: - logger().error( "incorrect specified: '%s'" % buf_file ) - logger().error( str(e) ) - return - logger().log( "[CHIPSEC] read 0x%X hex bytes from command-line: %s'" % (len(buffer), buf_file) ) - else: - buffer = chipsec.file.read_file( buf_file ) - logger().log( "[CHIPSEC] read 0x%X bytes from file '%s'" % (len(buffer), buf_file) ) - if len(buffer) < size: - logger().error( "number of bytes read (0x%X) is less than the specified (0x%X)" % (len(buffer),size) ) + op = self.argv[2] + t = time.time() + + if 'allocate' == op and 4 == len(self.argv): + size = int(self.argv[3],16) + (va, pa) = self.cs.mem.alloc_physical_mem( size ) + self.logger.log( '[CHIPSEC] Allocated %X bytes of physical memory: VA = 0x%016X, PA = 0x%016X' % (size, va, pa) ) + + elif 'pagedump' == op and len(self.argv) > 3: + start = long(self.argv[3],16) + length = long(self.argv[4],16) if len(self.argv) > 4 else chipsec.defines.BOUNDARY_4KB + end = start + length + + dump_region_to_path( chipsec.file.get_main_dir(), start, end ) + + elif 'read' == op: + phys_address = int(self.argv[3],16) + size = int(self.argv[4],16) if len(self.argv) > 4 else 0x100 + self.logger.log( '[CHIPSEC] reading buffer from memory: PA = 0x%016X, len = 0x%X..' % (phys_address, size) ) + buffer = self.cs.mem.read_physical_mem( phys_address, size ) + if len(self.argv) > 5: + buf_file = self.argv[5] + chipsec.file.write_file( buf_file, buffer ) + self.logger.log( "[CHIPSEC] written 0x%X bytes to '%s'" % (len(buffer), buf_file) ) + else: + print_buffer( buffer ) + + elif 'readval' == op: + phys_address = int(self.argv[3],16) + width = 0x4 + if len(self.argv) > 4: + width = chipsec_util.get_option_width(self.argv[4]) if chipsec_util.is_option_valid_width(self.argv[4]) else int(self.argv[4],16) + self.logger.log( '[CHIPSEC] reading %X-byte value from PA 0x%016X..' % (width, phys_address) ) + if 0x1 == width: value = self.cs.mem.read_physical_mem_byte ( phys_address ) + elif 0x2 == width: value = self.cs.mem.read_physical_mem_word ( phys_address ) + elif 0x4 == width: value = self.cs.mem.read_physical_mem_dword( phys_address ) + self.logger.log( '[CHIPSEC] value = 0x%X' % value ) + + elif 'write' == op: + phys_address = int(self.argv[3],16) + if len(self.argv) > 4: + size = int(self.argv[4],16) + else: + self.logger.error( "must specify argument in 'mem write'" ) + return + if len(self.argv) > 5: + buf_file = self.argv[5] + if not os.path.exists( buf_file ): + #buffer = buf_file.decode('hex') + try: + buffer = bytearray.fromhex(buf_file) + except ValueError, e: + self.logger.error( "incorrect specified: '%s'" % buf_file ) + self.logger.error( str(e) ) + return + self.logger.log( "[CHIPSEC] read 0x%X hex bytes from command-line: %s'" % (len(buffer), buf_file) ) + else: + buffer = chipsec.file.read_file( buf_file ) + self.logger.log( "[CHIPSEC] read 0x%X bytes from file '%s'" % (len(buffer), buf_file) ) + + if len(buffer) < size: + self.logger.error( "number of bytes read (0x%X) is less than the specified (0x%X)" % (len(buffer),size) ) + return + + self.logger.log( '[CHIPSEC] writing buffer to memory: PA = 0x%016X, len = 0x%X..' % (phys_address, size) ) + self.cs.mem.write_physical_mem( phys_address, size, buffer ) + else: + self.logger.error( "must specify | argument in 'mem write'" ) return - logger().log( '[CHIPSEC] writing buffer to memory: PA = 0x%016X, len = 0x%X..' % (phys_address, size) ) - chipsec_util._cs.mem.write_physical_mem( phys_address, size, buffer ) + elif 'writeval' == op: + phys_address = int(self.argv[3],16) + if len(self.argv) > 4: + width = chipsec_util.get_option_width(self.argv[4]) if chipsec_util.is_option_valid_width(self.argv[4]) else int(self.argv[4],16) + else: + self.logger.error( "must specify argument in 'mem writeval' as one of %s" % chipsec_util.CMD_OPTS_WIDTH ) + return + if len(self.argv) > 5: + value = int(self.argv[5],16) + else: + self.logger.error( "must specify argument in 'mem writeval'" ) + return + + self.logger.log( '[CHIPSEC] writing %X-byte value 0x%X to PA 0x%016X..' % (width, value, phys_address) ) + if 0x1 == width: self.cs.mem.write_physical_mem_byte ( phys_address, value ) + elif 0x2 == width: self.cs.mem.write_physical_mem_word ( phys_address, value ) + elif 0x4 == width: self.cs.mem.write_physical_mem_dword( phys_address, value ) + else: - logger().error( "must specify | argument in 'mem write'" ) - return + print MemCommand.__doc__ + return - elif 'writeval' == op: - phys_address = int(argv[3],16) - if len(argv) > 4: - width = chipsec_util.get_option_width(argv[4]) if chipsec_util.is_option_valid_width(argv[4]) else int(argv[4],16) - else: - logger().error( "must specify argument in 'mem writeval' as one of %s" % chipsec_util.CMD_OPTS_WIDTH ) - return - if len(argv) > 5: - value = int(argv[5],16) - else: - logger().error( "must specify argument in 'mem writeval'" ) - return + self.logger.log( "[CHIPSEC] (mem) time elapsed %.3f" % (time.time()-t) ) - logger().log( '[CHIPSEC] writing %X-byte value 0x%X to PA 0x%016X..' % (width, value, phys_address) ) - if 0x1 == width: chipsec_util._cs.mem.write_physical_mem_byte ( phys_address, value ) - elif 0x2 == width: chipsec_util._cs.mem.write_physical_mem_word ( phys_address, value ) - elif 0x4 == width: chipsec_util._cs.mem.write_physical_mem_dword( phys_address, value ) - - else: - print mem.__doc__ - return - - logger().log( "[CHIPSEC] (mem) time elapsed %.3f" % (time.time()-t) ) - -chipsec_util.commands['mem'] = {'func' : mem, 'start_driver' : True, 'help' : mem.__doc__ } +commands = { 'mem': MemCommand } diff --git a/source/tool/chipsec/utilcmd/mmcfg_cmd.py b/source/tool/chipsec/utilcmd/mmcfg_cmd.py index fa9e4269..36f93d17 100644 --- a/source/tool/chipsec/utilcmd/mmcfg_cmd.py +++ b/source/tool/chipsec/utilcmd/mmcfg_cmd.py @@ -28,21 +28,14 @@ The mmcfg command allows direct access to memory mapped config space. __version__ = '1.0' -import os -import sys import time -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.mmio import * # Access to Memory Mapped PCIe Configuration Space (MMCFG) -def mmcfg(argv): +class MMCfgCommand(BaseCommand): """ >>> chipsec_util mmcfg [value] @@ -53,50 +46,54 @@ def mmcfg(argv): >>> chipsec_util mmcfg 0 0x1F 0 0xDC 1 0x1 >>> chipsec_util mmcfg 0 0 0 0x98 dword 0x004E0040 """ - t = time.time() - if 2 == len(argv): - #pciexbar = get_PCIEXBAR_base_address( chipsec_util._cs ) - pciexbar = get_MMCFG_base_address( chipsec_util._cs ) - logger().log( "[CHIPSEC] Memory Mapped Config Base: 0x%016X" % pciexbar ) - return - elif 6 > len(argv): - print mmcfg.__doc__ - return + def requires_driver(self): + return True - try: - bus = int(argv[2],16) - device = int(argv[3],16) - function = int(argv[4],16) - offset = int(argv[5],16) + def run(self): + t = time.time() - if 6 == len(argv): - width = 1 - else: - if 'byte' == argv[6]: + if 2 == len(self.argv): + #pciexbar = get_PCIEXBAR_base_address( self.cs ) + pciexbar = get_MMCFG_base_address( self.cs ) + self.logger.log( "[CHIPSEC] Memory Mapped Config Base: 0x%016X" % pciexbar ) + return + elif 6 > len(self.argv): + print MMCfgCommand.__doc__ + return + + try: + bus = int(self.argv[2],16) + device = int(self.argv[3],16) + function = int(self.argv[4],16) + offset = int(self.argv[5],16) + + if 6 == len(self.argv): width = 1 - elif 'word' == argv[6]: - width = 2 - elif 'dword' == argv[6]: - width = 4 else: - width = int(argv[6]) + if 'byte' == self.argv[6]: + width = 1 + elif 'word' == self.argv[6]: + width = 2 + elif 'dword' == self.argv[6]: + width = 4 + else: + width = int(self.argv[6]) - except Exception as e : - print mmcfg.__doc__ - return + except Exception as e : + print MMCfgCommand.__doc__ + return - if 8 == len(argv): - value = int(argv[7], 16) - write_mmcfg_reg( chipsec_util._cs, bus, device, function, offset, width, value ) - #_cs.pci.write_mmcfg_reg( bus, device, function, offset, width, value ) - logger().log( "[CHIPSEC] writing MMCFG register (%02d:%02d.%d + 0x%02X): 0x%X" % (bus, device, function, offset, value) ) - else: - value = read_mmcfg_reg( chipsec_util._cs, bus, device, function, offset, width ) - #value = _cs.pci.read_mmcfg_reg( bus, device, function, offset, width ) - logger().log( "[CHIPSEC] reading MMCFG register (%02d:%02d.%d + 0x%02X): 0x%X" % (bus, device, function, offset, value) ) + if 8 == len(self.argv): + value = int(self.argv[7], 16) + write_mmcfg_reg( self.cs, bus, device, function, offset, width, value ) + #_cs.pci.write_mmcfg_reg( bus, device, function, offset, width, value ) + self.logger.log( "[CHIPSEC] writing MMCFG register (%02d:%02d.%d + 0x%02X): 0x%X" % (bus, device, function, offset, value) ) + else: + value = read_mmcfg_reg( self.cs, bus, device, function, offset, width ) + #value = _cs.pci.read_mmcfg_reg( bus, device, function, offset, width ) + self.logger.log( "[CHIPSEC] reading MMCFG register (%02d:%02d.%d + 0x%02X): 0x%X" % (bus, device, function, offset, value) ) - logger().log( "[CHIPSEC] (mmcfg) time elapsed %.3f" % (time.time()-t) ) + self.logger.log( "[CHIPSEC] (mmcfg) time elapsed %.3f" % (time.time()-t) ) - -chipsec_util.commands['mmcfg'] = {'func' : mmcfg , 'start_driver' : True, 'help' : mmcfg.__doc__ } +commands = { 'mmcfg': MMCfgCommand } diff --git a/source/tool/chipsec/utilcmd/mmio_cmd.py b/source/tool/chipsec/utilcmd/mmio_cmd.py index 46f436a0..1746537c 100644 --- a/source/tool/chipsec/utilcmd/mmio_cmd.py +++ b/source/tool/chipsec/utilcmd/mmio_cmd.py @@ -23,8 +23,6 @@ __version__ = '1.0' -import os -import sys import time import chipsec_util @@ -34,6 +32,7 @@ from chipsec.logger import * from chipsec.file import * from chipsec.hal.mmio import * +from chipsec.command import BaseCommand # ################################################################### @@ -41,7 +40,7 @@ from chipsec.hal.mmio import * # Access to Memory Mapped PCIe Configuration Space (MMCFG) # # ################################################################### -def mmio(argv): +class MMIOCommand(BaseCommand): """ >>> chipsec_util mmio list >>> chipsec_util mmio dump @@ -55,44 +54,52 @@ def mmio(argv): >>> chipsec_util mmio read SPIBAR 0x74 0x4 >>> chipsec_util mmio write SPIBAR 0x74 0x4 0xFFFF0000 """ - t = time.time() - if 3 > len(argv): - print mmio.__doc__ - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - t = time.time() + def run(self): + t = time.time() - if ( 'list' == op ): - list_MMIO_BARs( chipsec_util._cs ) - elif ( 'dump' == op ): - bar = argv[3].upper() - logger().log( "[CHIPSEC] Dumping %s MMIO space.." % bar ) - dump_MMIO_BAR( chipsec_util._cs, bar ) - elif ( 'read' == op ): - bar = argv[3].upper() - off = int(argv[4],16) - width = int(argv[5],16) if len(argv) == 6 else 4 - reg = read_MMIO_BAR_reg( chipsec_util._cs, bar, off, width ) - logger().log( "[CHIPSEC] Read %s + 0x%X: 0x%08X" % (bar,off,reg) ) - elif ( 'write' == op ): - bar = argv[3].upper() - off = int(argv[4],16) - width = int(argv[5],16) if len(argv) == 6 else 4 - if len(argv) == 7: - reg = int(argv[6],16) - logger().log( "[CHIPSEC] Write %s + 0x%X: 0x%08X" % (bar,off,reg) ) - write_MMIO_BAR_reg( chipsec_util._cs, bar, off, reg, width ) - else: - print mmio.__doc__ + if len(self.argv) < 3: + print MMIOCommand.__doc__ return - else: - logger().error( "unknown command-line option '%.32s'" % op ) - print mmio.__doc__ - return - logger().log( "[CHIPSEC] (mmio) time elapsed %.3f" % (time.time()-t) ) + op = self.argv[2] + t = time.time() + + if ( 'list' == op ): + list_MMIO_BARs( self.cs ) + elif ( 'dump' == op ): + bar = self.argv[3].upper() + self.logger.log( "[CHIPSEC] Dumping %s MMIO space.." % bar ) + dump_MMIO_BAR( self.cs, bar ) + elif ( 'read' == op ): + bar = self.argv[3].upper() + off = int(self.argv[4],16) + width = int(self.argv[5],16) if len(self.argv) == 6 else 4 + reg = read_MMIO_BAR_reg( self.cs, bar, off, width ) + self.logger.log( "[CHIPSEC] Read %s + 0x%X: 0x%08X" % (bar,off,reg) ) + elif ( 'write' == op ): + bar = self.argv[3].upper() + off = int(self.argv[4],16) + width = int(self.argv[5],16) if len(self.argv) == 6 else 4 + if len(self.argv) == 7: + reg = int(self.argv[6],16) + self.logger.log( "[CHIPSEC] Write %s + 0x%X: 0x%08X" % (bar,off,reg) ) + write_MMIO_BAR_reg( self.cs, bar, off, reg, width ) + else: + print MMIOCommand.__doc__ + return + else: + self.logger.error( "unknown command-line option '%.32s'" % op ) + print MMIOCommand.__doc__ + return + + self.logger.log( "[CHIPSEC] (mmio) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['mmio'] = {'func' : mmio , 'start_driver' : True, 'help' : mmio.__doc__ } +commands = { 'mmio': MMIOCommand } diff --git a/source/tool/chipsec/utilcmd/msr_cmd.py b/source/tool/chipsec/utilcmd/msr_cmd.py index 9ce9785b..af2a76c2 100644 --- a/source/tool/chipsec/utilcmd/msr_cmd.py +++ b/source/tool/chipsec/utilcmd/msr_cmd.py @@ -27,21 +27,12 @@ The msr command allows direct access to read and write MSRs. __version__ = '1.0' -import os -import sys -import time - -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.msr import Msr # CPU Model Specific Registers -def msr(argv): +class MSRCommand(BaseCommand): """ >>> chipsec_util msr [eax] [edx] [cpu_id] @@ -50,34 +41,41 @@ def msr(argv): >>> chipsec_util msr 0x3A >>> chipsec_util msr 0x8B 0x0 0x0 0 """ - if 3 > len(argv): - print msr.__doc__ - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - #msr = Msr( os_helper ) - msr_addr = int(argv[2],16) + def run(self): + if len(self.argv) < 3: + print MSRCommand.__doc__ + return - if (3 == len(argv)): - for tid in range(chipsec_util._cs.msr.get_cpu_thread_count()): - (eax, edx) = chipsec_util._cs.msr.read_msr( tid, msr_addr ) + #msr = Msr( os_helper ) + msr_addr = int(self.argv[2],16) + + if (3 == len(self.argv)): + for tid in range(self.cs.msr.get_cpu_thread_count()): + (eax, edx) = self.cs.msr.read_msr( tid, msr_addr ) + val64 = ((edx << 32) | eax) + self.logger.log( "[CHIPSEC] CPU%d: RDMSR( 0x%x ) = %016X (EAX=%08X, EDX=%08X)" % (tid, msr_addr, val64, eax, edx) ) + elif (4 == len(self.argv)): + cpu_thread_id = int(self.argv[3], 16) + (eax, edx) = self.cs.msr.read_msr( cpu_thread_id, msr_addr ) val64 = ((edx << 32) | eax) - logger().log( "[CHIPSEC] CPU%d: RDMSR( 0x%x ) = %016X (EAX=%08X, EDX=%08X)" % (tid, msr_addr, val64, eax, edx) ) - elif (4 == len(argv)): - cpu_thread_id = int(argv[3], 16) - (eax, edx) = chipsec_util._cs.msr.read_msr( cpu_thread_id, msr_addr ) - val64 = ((edx << 32) | eax) - logger().log( "[CHIPSEC] CPU%d: RDMSR( 0x%x ) = %016X (EAX=%08X, EDX=%08X)" % (cpu_thread_id, msr_addr, val64, eax, edx) ) - else: - eax = int(argv[3], 16) - edx = int(argv[4], 16) - val64 = ((edx << 32) | eax) - if (5 == len(argv)): - logger().log( "[CHIPSEC] All CPUs: WRMSR( 0x%x ) = %016X" % (msr_addr, val64) ) - for tid in range(chipsec_util._cs.msr.get_cpu_thread_count()): - chipsec_util._cs.msr.write_msr( tid, msr_addr, eax, edx ) - elif (6 == len(argv)): - cpu_thread_id = int(argv[5], 16) - logger().log( "[CHIPSEC] CPU%d: WRMSR( 0x%x ) = %016X" % (cpu_thread_id, msr_addr, val64) ) - chipsec_util._cs.msr.write_msr( cpu_thread_id, msr_addr, eax, edx ) + self.logger.log( "[CHIPSEC] CPU%d: RDMSR( 0x%x ) = %016X (EAX=%08X, EDX=%08X)" % (cpu_thread_id, msr_addr, val64, eax, edx) ) + else: + eax = int(self.argv[3], 16) + edx = int(self.argv[4], 16) + val64 = ((edx << 32) | eax) + if (5 == len(self.argv)): + self.logger.log( "[CHIPSEC] All CPUs: WRMSR( 0x%x ) = %016X" % (msr_addr, val64) ) + for tid in range(self.cs.msr.get_cpu_thread_count()): + self.cs.msr.write_msr( tid, msr_addr, eax, edx ) + elif (6 == len(self.argv)): + cpu_thread_id = int(self.argv[5], 16) + self.logger.log( "[CHIPSEC] CPU%d: WRMSR( 0x%x ) = %016X" % (cpu_thread_id, msr_addr, val64) ) + self.cs.msr.write_msr( cpu_thread_id, msr_addr, eax, edx ) -chipsec_util.commands['msr'] = {'func' : msr , 'start_driver' : True, 'help' : msr.__doc__ } +commands = { 'msr': MSRCommand } diff --git a/source/tool/chipsec/utilcmd/pci_cmd.py b/source/tool/chipsec/utilcmd/pci_cmd.py index e7fffbe1..b30be0b7 100644 --- a/source/tool/chipsec/utilcmd/pci_cmd.py +++ b/source/tool/chipsec/utilcmd/pci_cmd.py @@ -27,21 +27,14 @@ The pci command can enumerate PCI devices and allow direct access to them by bus __version__ = '1.0' -import os -import sys import time -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.pci import * # PCIe Devices and Configuration Registers -def pci(argv): +class PCICommand(BaseCommand): """ >>> chipsec_util pci enumerate >>> chipsec_util pci [value] @@ -54,64 +47,72 @@ def pci(argv): >>> chipsec_util pci 0 0x1F 0 0xDC 1 0x1 >>> chipsec_util pci 0 0 0 0x98 dword 0x004E0040 """ - if 3 > len(argv): - print pci.__doc__ - return - op = argv[2] - t = time.time() + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - if ( 'enumerate' == op ): - logger().log( "[CHIPSEC] Enumerating available PCIe devices.." ) - print_pci_devices( chipsec_util._cs.pci.enumerate_devices() ) - logger().log( "[CHIPSEC] (pci) time elapsed %.3f" % (time.time()-t) ) - return + def run(self): + if len(self.argv) < 3: + print PCICommand.__doc__ + return - try: - bus = int(argv[2],16) - device = int(argv[3],16) - function = int(argv[4],16) - offset = int(argv[5],16) + op = self.argv[2] + t = time.time() - if 6 == len(argv): - width = 1 - else: - if 'byte' == argv[6]: + if ( 'enumerate' == op ): + self.logger.log( "[CHIPSEC] Enumerating available PCIe devices.." ) + print_pci_devices( self.cs.pci.enumerate_devices() ) + self.logger.log( "[CHIPSEC] (pci) time elapsed %.3f" % (time.time()-t) ) + return + + try: + bus = int(self.argv[2],16) + device = int(self.argv[3],16) + function = int(self.argv[4],16) + offset = int(self.argv[5],16) + + if 6 == len(self.argv): width = 1 - elif 'word' == argv[6]: - width = 2 - elif 'dword' == argv[6]: - width = 4 else: - width = int(argv[6]) - except Exception as e : - print pci.__doc__ - return - - if 8 == len(argv): - value = int(argv[7], 16) - if 1 == width: - chipsec_util._cs.pci.write_byte( bus, device, function, offset, value ) - elif 2 == width: - chipsec_util._cs.pci.write_word( bus, device, function, offset, value ) - elif 4 == width: - chipsec_util._cs.pci.write_dword( bus, device, function, offset, value ) - else: - print "ERROR: Unsupported width 0x%x" % width + if 'byte' == self.argv[6]: + width = 1 + elif 'word' == self.argv[6]: + width = 2 + elif 'dword' == self.argv[6]: + width = 4 + else: + width = int(self.argv[6]) + except Exception as e : + print PCICommand.__doc__ return - logger().log( "[CHIPSEC] writing PCI %d/%d/%d, off 0x%02X: 0x%X" % (bus, device, function, offset, value) ) - else: - if 1 == width: - pci_value = chipsec_util._cs.pci.read_byte(bus, device, function, offset) - elif 2 == width: - pci_value = chipsec_util._cs.pci.read_word(bus, device, function, offset) - elif 4 == width: - pci_value = chipsec_util._cs.pci.read_dword(bus, device, function, offset) + + if 8 == len(self.argv): + value = int(self.argv[7], 16) + if 1 == width: + self.cs.pci.write_byte( bus, device, function, offset, value ) + elif 2 == width: + self.cs.pci.write_word( bus, device, function, offset, value ) + elif 4 == width: + self.cs.pci.write_dword( bus, device, function, offset, value ) + else: + print "ERROR: Unsupported width 0x%x" % width + return + self.logger.log( "[CHIPSEC] writing PCI %d/%d/%d, off 0x%02X: 0x%X" % (bus, device, function, offset, value) ) else: - print "ERROR: Unsupported width 0x%x" % width - return - logger().log( "[CHIPSEC] reading PCI B/D/F %d/%d/%d, off 0x%02X: 0x%X" % (bus, device, function, offset, pci_value) ) + if 1 == width: + pci_value = self.cs.pci.read_byte(bus, device, function, offset) + elif 2 == width: + pci_value = self.cs.pci.read_word(bus, device, function, offset) + elif 4 == width: + pci_value = self.cs.pci.read_dword(bus, device, function, offset) + else: + print "ERROR: Unsupported width 0x%x" % width + return + self.logger.log( "[CHIPSEC] reading PCI B/D/F %d/%d/%d, off 0x%02X: 0x%X" % (bus, device, function, offset, pci_value) ) - logger().log( "[CHIPSEC] (pci) time elapsed %.3f" % (time.time()-t) ) + self.logger.log( "[CHIPSEC] (pci) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['pci'] = {'func' : pci , 'start_driver' : True, 'help' : pci.__doc__ } +commands = { 'pci': PCICommand } diff --git a/source/tool/chipsec/utilcmd/smbus_cmd.py b/source/tool/chipsec/utilcmd/smbus_cmd.py index eed59cfd..d5d74a00 100644 --- a/source/tool/chipsec/utilcmd/smbus_cmd.py +++ b/source/tool/chipsec/utilcmd/smbus_cmd.py @@ -23,19 +23,13 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand +from chipsec.logger import print_buffer from chipsec.hal.smbus import * -def smbus(argv): +class SMBusCommand(BaseCommand): """ >>> chipsec_util smbus read [size] >>> chipsec_util smbus write @@ -44,48 +38,55 @@ def smbus(argv): >>> chipsec_util smbus read 0xA0 0x0 0x100 """ - if 3 > len(argv): - print smbus.__doc__ - return - try: - _smbus = SMBus( chipsec_util._cs ) - except BaseException, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - t = time.time() + def run(self): + if len(self.argv) < 3: + print SMBusCommand.__doc__ + return - if not _smbus.is_SMBus_supported(): - logger().log( "[CHIPSEC] SMBus controller is not supported" ) - return + try: + _smbus = SMBus( self.cs ) + except BaseException, msg: + print msg + return - _smbus.display_SMBus_info() + op = self.argv[2] + t = time.time() - if ( 'read' == op ): - dev_addr = int(argv[3],16) - start_off = int(argv[4],16) - if len(argv) > 5: - size = int(argv[5],16) - buf = _smbus.read_range( dev_addr, start_off, size ) - logger().log( "[CHIPSEC] SMBus read: device 0x%X offset 0x%X size 0x%X" % (dev_addr, start_off, size) ) - print_buffer( buf ) + if not _smbus.is_SMBus_supported(): + self.logger.log( "[CHIPSEC] SMBus controller is not supported" ) + return + + _smbus.display_SMBus_info() + + if ( 'read' == op ): + dev_addr = int(self.argv[3],16) + start_off = int(self.argv[4],16) + if len(self.argv) > 5: + size = int(self.argv[5],16) + buf = _smbus.read_range( dev_addr, start_off, size ) + self.logger.log( "[CHIPSEC] SMBus read: device 0x%X offset 0x%X size 0x%X" % (dev_addr, start_off, size) ) + print_buffer( buf ) + else: + val = _smbus.read_byte( dev_addr, start_off ) + self.logger.log( "[CHIPSEC] SMBus read: device 0x%X offset 0x%X = 0x%X" % (dev_addr, start_off, val) ) + elif ( 'write' == op ): + dev_addr = int(self.argv[3],16) + off = int(self.argv[4],16) + val = int(self.argv[5],16) + self.logger.log( "[CHIPSEC] SMBus write: device 0x%X offset 0x%X = 0x%X" % (dev_addr, off, val) ) + _smbus.write_byte( dev_addr, off, val ) else: - val = _smbus.read_byte( dev_addr, start_off ) - logger().log( "[CHIPSEC] SMBus read: device 0x%X offset 0x%X = 0x%X" % (dev_addr, start_off, val) ) - elif ( 'write' == op ): - dev_addr = int(argv[3],16) - off = int(argv[4],16) - val = int(argv[5],16) - logger().log( "[CHIPSEC] SMBus write: device 0x%X offset 0x%X = 0x%X" % (dev_addr, off, val) ) - _smbus.write_byte( dev_addr, off, val ) - else: - logger().error( "unknown command-line option '%.32s'" % op ) - print smbus.__doc__ - return + self.logger.error( "unknown command-line option '%.32s'" % op ) + print SMBusCommand.__doc__ + return - logger().log( "[CHIPSEC] (smbus) time elapsed %.3f" % (time.time()-t) ) + self.logger.log( "[CHIPSEC] (smbus) time elapsed %.3f" % (time.time()-t) ) - -chipsec_util.commands['smbus'] = {'func' : smbus, 'start_driver' : True, 'help' : smbus.__doc__ } +commands = { 'smbus': SMBusCommand } diff --git a/source/tool/chipsec/utilcmd/spd_cmd.py b/source/tool/chipsec/utilcmd/spd_cmd.py index 93a2d7a3..2f4acce3 100644 --- a/source/tool/chipsec/utilcmd/spd_cmd.py +++ b/source/tool/chipsec/utilcmd/spd_cmd.py @@ -23,21 +23,14 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util - - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.smbus import * from chipsec.hal.spd import * -def spd(argv): +class SPDCommand(BaseCommand): """ >>> chipsec_util spd detect >>> chipsec_util spd dump [device_addr] @@ -51,69 +44,76 @@ def spd(argv): >>> chipsec_util spd read 0xA0 0x0 >>> chipsec_util spd write 0xA0 0x0 0xAA """ - if 3 > len(argv): - print spd.__doc__ - return - try: - _smbus = SMBus( chipsec_util._cs ) - _spd = SPD( _smbus ) - except BaseException, msg: - print msg - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - op = argv[2] - t = time.time() - - if not _smbus.is_SMBus_supported(): - logger().log( "[CHIPSEC] SMBus controller is not supported" ) - return - #smbus.display_SMBus_info() - - dev_addr = SPD_SMBUS_ADDRESS - - if( 'detect' == op ): - - logger().log( "[CHIPSEC] Searching for DIMMs with SPD.." ) - _spd.detect() - - elif( 'dump' == op ): - - if len(argv) > 3: - dev = argv[3].upper() - dev_addr = chipsec.hal.spd.SPD_DIMM_ADDRESSES[ dev ] if dev in chipsec.hal.spd.SPD_DIMM_ADDRESSES else int(argv[3],16) - if not _spd.isSPDPresent( dev_addr ): - logger().log( "[CHIPSEC] SPD for DIMM 0x%X is not found" % dev_addr ) - return - _spd.decode( dev_addr ) - else: - _dimms = _spd.detect() - for d in _dimms: _spd.decode( d ) - - elif( 'read' == op ) or ( 'write' == op ): - - if len(argv) > 3: - dev = argv[3].upper() - dev_addr = chipsec.hal.spd.SPD_DIMM_ADDRESSES[ dev ] if dev in chipsec.hal.spd.SPD_DIMM_ADDRESSES else int(argv[3],16) - if not _spd.isSPDPresent( dev_addr ): - logger().log( "[CHIPSEC] SPD for DIMM 0x%X is not found" % dev_addr ) + def run(self): + if len(self.argv) < 3: + print SPDCommand.__doc__ return - off = int(argv[4],16) - if( 'read' == op ): - val = _spd.read_byte( off, dev_addr ) - logger().log( "[CHIPSEC] SPD read: offset 0x%X = 0x%X" % (off, val) ) - elif( 'write' == op ): - val = int(argv[5],16) - logger().log( "[CHIPSEC] SPD write: offset 0x%X = 0x%X" % (off, val) ) - _spd.write_byte( off, val, dev_addr ) + try: + _smbus = SMBus( self.cs ) + _spd = SPD( _smbus ) + except BaseException, msg: + print msg + return - else: - logger().error( "unknown command-line option '%.32s'" % op ) - logger().log( spd.__doc__ ) - return + op = self.argv[2] + t = time.time() - logger().log( "[CHIPSEC] (spd) time elapsed %.3f" % (time.time()-t) ) + if not _smbus.is_SMBus_supported(): + self.logger.log( "[CHIPSEC] SMBus controller is not supported" ) + return + #smbus.display_SMBus_info() + dev_addr = SPD_SMBUS_ADDRESS -chipsec_util.commands['spd'] = {'func' : spd, 'start_driver' : True, 'help' : spd.__doc__ } + if( 'detect' == op ): + + self.logger.log( "[CHIPSEC] Searching for DIMMs with SPD.." ) + _spd.detect() + + elif( 'dump' == op ): + + if len(self.argv) > 3: + dev = self.argv[3].upper() + dev_addr = chipsec.hal.spd.SPD_DIMM_ADDRESSES[ dev ] if dev in chipsec.hal.spd.SPD_DIMM_ADDRESSES else int(self.argv[3],16) + if not _spd.isSPDPresent( dev_addr ): + self.logger.log( "[CHIPSEC] SPD for DIMM 0x%X is not found" % dev_addr ) + return + _spd.decode( dev_addr ) + else: + _dimms = _spd.detect() + for d in _dimms: _spd.decode( d ) + + elif( 'read' == op ) or ( 'write' == op ): + + if len(self.argv) > 3: + dev = self.argv[3].upper() + dev_addr = chipsec.hal.spd.SPD_DIMM_ADDRESSES[ dev ] if dev in chipsec.hal.spd.SPD_DIMM_ADDRESSES else int(self.argv[3],16) + if not _spd.isSPDPresent( dev_addr ): + self.logger.log( "[CHIPSEC] SPD for DIMM 0x%X is not found" % dev_addr ) + return + + off = int(self.argv[4],16) + if( 'read' == op ): + val = _spd.read_byte( off, dev_addr ) + self.logger.log( "[CHIPSEC] SPD read: offset 0x%X = 0x%X" % (off, val) ) + elif( 'write' == op ): + val = int(self.argv[5],16) + self.logger.log( "[CHIPSEC] SPD write: offset 0x%X = 0x%X" % (off, val) ) + _spd.write_byte( off, val, dev_addr ) + + else: + self.logger.error( "unknown command-line option '%.32s'" % op ) + self.logger.log( SPDCommand.__doc__ ) + return + + self.logger.log( "[CHIPSEC] (spd) time elapsed %.3f" % (time.time()-t) ) + +commands = { 'spd': SPDCommand } diff --git a/source/tool/chipsec/utilcmd/spi_cmd.py b/source/tool/chipsec/utilcmd/spi_cmd.py index 397f581d..bff5f999 100644 --- a/source/tool/chipsec/utilcmd/spi_cmd.py +++ b/source/tool/chipsec/utilcmd/spi_cmd.py @@ -35,21 +35,14 @@ The file rom.bin will contain the full binary of the SPI flash. It can then be p __version__ = '1.0' - -import os -import sys import time -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand from chipsec.hal.spi import * # SPI Flash Controller -def spi(argv): +class SPICommand(BaseCommand): """ >>> chipsec_util spi info|dump|read|write|erase|disable-wp [flash_address] [length] [file] @@ -61,81 +54,86 @@ def spi(argv): >>> chipsec_util spi write 0x0 flash_descriptor.bin >>> chipsec_util spi disable-wp """ - if 3 > len(argv): - print spi.__doc__ - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - try: - _spi = SPI( chipsec_util._cs ) - except SpiRuntimeError, msg: - print msg - return + def run(self): + if len(self.argv) < 3: + print SPICommand.__doc__ + return - spi_op = argv[2] + try: + _spi = SPI( self.cs ) + except SpiRuntimeError, msg: + print msg + return - t = time.time() + spi_op = self.argv[2] - if ( 'erase' == spi_op ): - spi_fla = int(argv[3],16) - logger().log( "[CHIPSEC] Erasing SPI Flash block at FLA = 0x%X" % spi_fla ) - #if not _spi.disable_BIOS_write_protection(): - # logger().error( "Could not disable SPI Flash protection. Still trying.." ) + t = time.time() - ok = _spi.erase_spi_block( spi_fla ) - if ok: logger().log_result( "SPI Flash erase done" ) - else: logger().warn( "SPI Flash erase returned error (turn on VERBOSE)" ) - elif ( 'write' == spi_op and 5 == len(argv) ): - spi_fla = int(argv[3],16) - filename = argv[4] - logger().log( "[CHIPSEC] Writing to SPI Flash at FLA = 0x%X from '%.64s'" % (spi_fla, filename) ) - #if not _spi.disable_BIOS_write_protection(): - # logger().error( "Could not disable SPI Flash protection. Still trying.." ) + if ( 'erase' == spi_op ): + spi_fla = int(self.argv[3],16) + self.logger.log( "[CHIPSEC] Erasing SPI Flash block at FLA = 0x%X" % spi_fla ) + #if not _spi.disable_BIOS_write_protection(): + # self.logger.error( "Could not disable SPI Flash protection. Still trying.." ) - ok = _spi.write_spi_from_file( spi_fla, filename ) - if ok: logger().log_result( "SPI Flash write done" ) - else: logger().warn( "SPI Flash write returned error (turn on VERBOSE)" ) - elif ( 'read' == spi_op ): - spi_fla = int(argv[3],16) - length = int(argv[4],16) - logger().log( "[CHIPSEC] Reading 0x%x bytes from SPI Flash starting at FLA = 0x%X" % (length, spi_fla) ) - out_file = None - if 6 == len(argv): - out_file = argv[5] - buf = _spi.read_spi_to_file( spi_fla, length, out_file ) - if (buf is None): logger().error( "SPI Flash read didn't return any data (turn on VERBOSE)" ) - else: logger().log_result( "SPI Flash read done" ) - elif ( 'info' == spi_op ): - logger().log( "[CHIPSEC] SPI Flash Info\n" ) - ok = _spi.display_SPI_map() - elif ( 'dump' == spi_op ): - out_file = 'rom.bin' - if 4 == len(argv): - out_file = argv[3] - logger().log( "[CHIPSEC] Dumping entire SPI Flash to '%s'" % out_file ) - # @TODO: don't assume SPI Flash always ends with BIOS region - (base,limit,freg) = _spi.get_SPI_region( BIOS ) - spi_size = limit + 1 - logger().log( "[CHIPSEC] BIOS Region: Base = 0x%08X, Limit = 0x%08X" % (base,limit) ) - logger().log( "[CHIPSEC] Dumping 0x%08X bytes (to the end of BIOS region)" % spi_size ) - buf = _spi.read_spi_to_file( 0, spi_size, out_file ) - if (buf is None): logger().error( "Dumping SPI Flash didn't return any data (turn on VERBOSE)" ) - else: logger().log_result( "Done dumping SPI Flash" ) + ok = _spi.erase_spi_block( spi_fla ) + if ok: self.logger.log_result( "SPI Flash erase done" ) + else: self.logger.warn( "SPI Flash erase returned error (turn on VERBOSE)" ) + elif ( 'write' == spi_op and 5 == len(self.argv) ): + spi_fla = int(self.argv[3],16) + filename = self.argv[4] + self.logger.log( "[CHIPSEC] Writing to SPI Flash at FLA = 0x%X from '%.64s'" % (spi_fla, filename) ) + #if not _spi.disable_BIOS_write_protection(): + # self.logger.error( "Could not disable SPI Flash protection. Still trying.." ) - elif ( 'disable-wp' == spi_op ): - logger().log( "[CHIPSEC] Trying to disable BIOS write protection.." ) - # - # This write protection only matters for BIOS range in SPI flash memory - # - if _spi.disable_BIOS_write_protection(): - logger().log_good( "BIOS region write protection is disabled in SPI flash" ) + ok = _spi.write_spi_from_file( spi_fla, filename ) + if ok: self.logger.log_result( "SPI Flash write done" ) + else: self.logger.warn( "SPI Flash write returned error (turn on VERBOSE)" ) + elif ( 'read' == spi_op ): + spi_fla = int(self.argv[3],16) + length = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Reading 0x%x bytes from SPI Flash starting at FLA = 0x%X" % (length, spi_fla) ) + out_file = None + if 6 == len(self.argv): + out_file = self.argv[5] + buf = _spi.read_spi_to_file( spi_fla, length, out_file ) + if (buf is None): self.logger.error( "SPI Flash read didn't return any data (turn on VERBOSE)" ) + else: self.logger.log_result( "SPI Flash read done" ) + elif ( 'info' == spi_op ): + self.logger.log( "[CHIPSEC] SPI Flash Info\n" ) + ok = _spi.display_SPI_map() + elif ( 'dump' == spi_op ): + out_file = 'rom.bin' + if 4 == len(self.argv): + out_file = self.argv[3] + self.logger.log( "[CHIPSEC] Dumping entire SPI Flash to '%s'" % out_file ) + # @TODO: don't assume SPI Flash always ends with BIOS region + (base,limit,freg) = _spi.get_SPI_region( BIOS ) + spi_size = limit + 1 + self.logger.log( "[CHIPSEC] BIOS Region: Base = 0x%08X, Limit = 0x%08X" % (base,limit) ) + self.logger.log( "[CHIPSEC] Dumping 0x%08X bytes (to the end of BIOS region)" % spi_size ) + buf = _spi.read_spi_to_file( 0, spi_size, out_file ) + if (buf is None): self.logger.error( "Dumping SPI Flash didn't return any data (turn on VERBOSE)" ) + else: self.logger.log_result( "Done dumping SPI Flash" ) + + elif ( 'disable-wp' == spi_op ): + self.logger.log( "[CHIPSEC] Trying to disable BIOS write protection.." ) + # + # This write protection only matters for BIOS range in SPI flash memory + # + if _spi.disable_BIOS_write_protection(): + self.logger.log_good( "BIOS region write protection is disabled in SPI flash" ) + else: + self.logger.log_bad( "Couldn't disable BIOS region write protection in SPI flash" ) else: - logger().log_bad( "Couldn't disable BIOS region write protection in SPI flash" ) - else: - print spi.__doc__ - return + print SPICommand.__doc__ + return - logger().log( "[CHIPSEC] (spi %s) time elapsed %.3f" % (spi_op, time.time()-t) ) + self.logger.log( "[CHIPSEC] (spi %s) time elapsed %.3f" % (spi_op, time.time()-t) ) - - -chipsec_util.commands['spi'] = {'func' : spi, 'start_driver' : True, 'help' : spi.__doc__ } +commands = { 'spi': SPICommand } diff --git a/source/tool/chipsec/utilcmd/spidesc_cmd.py b/source/tool/chipsec/utilcmd/spidesc_cmd.py index 587156f9..d953ffe7 100644 --- a/source/tool/chipsec/utilcmd/spidesc_cmd.py +++ b/source/tool/chipsec/utilcmd/spidesc_cmd.py @@ -23,18 +23,13 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand +from chipsec.file import read_file from chipsec.hal.spi_descriptor import * -def spidesc(argv): +class SPIDescCommand(BaseCommand): """ >>> chipsec_util spidesc [rom] @@ -42,17 +37,20 @@ def spidesc(argv): >>> chipsec_util spidesc spi.bin """ - if 3 > len(argv): - print spidesc.__doc__ - return + def requires_driver(self): + return False - fd_file = argv[2] - logger().log( "[CHIPSEC] Parsing SPI Flash Descriptor from file '%s'\n" % fd_file ) + def run(self): + if len(self.argv) < 3: + print SPIDescCommand.__doc__ + return - t = time.time() - fd = read_file( fd_file ) - if type(fd) == str: parse_spi_flash_descriptor( fd ) - logger().log( "\n[CHIPSEC] (spidesc) time elapsed %.3f" % (time.time()-t) ) + fd_file = self.argv[2] + self.logger.log( "[CHIPSEC] Parsing SPI Flash Descriptor from file '%s'\n" % fd_file ) + t = time.time() + fd = read_file( fd_file ) + if type(fd) == str: parse_spi_flash_descriptor( fd ) + self.logger.log( "\n[CHIPSEC] (spidesc) time elapsed %.3f" % (time.time()-t) ) -chipsec_util.commands['spidesc'] = {'func' : spidesc, 'start_driver' : False, 'help' : spidesc.__doc__ } +commands = { 'spidesc': SPIDescCommand } diff --git a/source/tool/chipsec/utilcmd/ucode_cmd.py b/source/tool/chipsec/utilcmd/ucode_cmd.py index 9f5465d8..81d5d2b5 100644 --- a/source/tool/chipsec/utilcmd/ucode_cmd.py +++ b/source/tool/chipsec/utilcmd/ucode_cmd.py @@ -23,15 +23,10 @@ __version__ = '1.0' -import os -import sys import time -import chipsec_util - -from chipsec.logger import * -from chipsec.file import * - +from chipsec.command import BaseCommand +from chipsec.file import read_file from chipsec.hal.ucode import Ucode, dump_ucode_update_header # ################################################################### @@ -39,7 +34,7 @@ from chipsec.hal.ucode import Ucode, dump_ucode_update_header # Microcode patches # # ################################################################### -def ucode(argv): +class UCodeCommand(BaseCommand): """ >>> chipsec_util ucode id|load|decode [ucode_update_file (in .PDB or .BIN format)] [cpu_id] @@ -49,51 +44,56 @@ def ucode(argv): >>> chipsec_util ucode load ucode.bin 0 >>> chipsec_util ucode decode ucode.pdb """ - if 3 > len(argv): - print ucode.__doc__ - return + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True - ucode_op = argv[2] - t = time.time() - - if ( 'load' == ucode_op ): - if (4 == len(argv)): - ucode_filename = argv[3] - logger().log( "[CHIPSEC] Loading Microcode update on all cores from '%s'" % ucode_filename ) - chipsec_util._cs.ucode.update_ucode_all_cpus( ucode_filename ) - elif (5 == len(argv)): - ucode_filename = argv[3] - cpu_thread_id = int(argv[4],16) - logger().log( "[CHIPSEC] Loading Microcode update on CPU%d from '%s'" % (cpu_thread_id, ucode_filename) ) - chipsec_util._cs.ucode.update_ucode( cpu_thread_id, ucode_filename ) - else: - print ucode.__doc__ + def run(self): + if len(self.argv) < 3: + print UCodeCommand.__doc__ return - elif ( 'decode' == ucode_op ): - if (4 == len(argv)): - ucode_filename = argv[3] - if (not ucode_filename.endswith('.pdb')): - logger().log( "[CHIPSEC] Ucode update file is not PDB file: '%s'" % ucode_filename ) + + ucode_op = self.argv[2] + t = time.time() + + if ( 'load' == ucode_op ): + if (4 == len(self.argv)): + ucode_filename = self.argv[3] + self.logger.log( "[CHIPSEC] Loading Microcode update on all cores from '%s'" % ucode_filename ) + self.cs.ucode.update_ucode_all_cpus( ucode_filename ) + elif (5 == len(self.argv)): + ucode_filename = self.argv[3] + cpu_thread_id = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Loading Microcode update on CPU%d from '%s'" % (cpu_thread_id, ucode_filename) ) + self.cs.ucode.update_ucode( cpu_thread_id, ucode_filename ) + else: + print UCodeCommand.__doc__ return - pdb_ucode_buffer = read_file( ucode_filename ) - logger().log( "[CHIPSEC] Decoding Microcode Update header of PDB file: '%s'" % ucode_filename ) - dump_ucode_update_header( pdb_ucode_buffer ) - elif ( 'id' == ucode_op ): - if (3 == len(argv)): - for tid in range(chipsec_util._cs.msr.get_cpu_thread_count()): - ucode_update_id = chipsec_util._cs.ucode.ucode_update_id( tid ) - logger().log( "[CHIPSEC] CPU%d: Microcode update ID = 0x%08X" % (tid, ucode_update_id) ) - elif (4 == len(argv)): - cpu_thread_id = int(argv[3],16) - ucode_update_id = chipsec_util._cs.ucode.ucode_update_id( cpu_thread_id ) - logger().log( "[CHIPSEC] CPU%d: Microcode update ID = 0x%08X" % (cpu_thread_id, ucode_update_id) ) - else: - logger().error( "unknown command-line option '%.32s'" % ucode_op ) - print ucode.__doc__ - return + elif ( 'decode' == ucode_op ): + if (4 == len(self.argv)): + ucode_filename = self.argv[3] + if (not ucode_filename.endswith('.pdb')): + self.logger.log( "[CHIPSEC] Ucode update file is not PDB file: '%s'" % ucode_filename ) + return + pdb_ucode_buffer = read_file( ucode_filename ) + self.logger.log( "[CHIPSEC] Decoding Microcode Update header of PDB file: '%s'" % ucode_filename ) + dump_ucode_update_header( pdb_ucode_buffer ) + elif ( 'id' == ucode_op ): + if (3 == len(self.argv)): + for tid in range(self.cs.msr.get_cpu_thread_count()): + ucode_update_id = self.cs.ucode.ucode_update_id( tid ) + self.logger.log( "[CHIPSEC] CPU%d: Microcode update ID = 0x%08X" % (tid, ucode_update_id) ) + elif (4 == len(self.argv)): + cpu_thread_id = int(self.argv[3],16) + ucode_update_id = self.cs.ucode.ucode_update_id( cpu_thread_id ) + self.logger.log( "[CHIPSEC] CPU%d: Microcode update ID = 0x%08X" % (cpu_thread_id, ucode_update_id) ) + else: + self.logger.error( "unknown command-line option '%.32s'" % ucode_op ) + print UCodeCommand.__doc__ + return - logger().log( "[CHIPSEC] (ucode) time elapsed %.3f" % (time.time()-t) ) + self.logger.log( "[CHIPSEC] (ucode) time elapsed %.3f" % (time.time()-t) ) - - -chipsec_util.commands['ucode'] = {'func' : ucode, 'start_driver' : True, 'help' : ucode.__doc__ } +commands = { 'ucode': UCodeCommand } diff --git a/source/tool/chipsec/utilcmd/uefi_cmd.py b/source/tool/chipsec/utilcmd/uefi_cmd.py index 25adb702..5df96132 100644 --- a/source/tool/chipsec/utilcmd/uefi_cmd.py +++ b/source/tool/chipsec/utilcmd/uefi_cmd.py @@ -28,7 +28,6 @@ The uefi command provides access to UEFI variables, both on the live system and __version__ = '1.0' import os -import sys import time import chipsec_util @@ -39,11 +38,11 @@ from chipsec.file import * from chipsec.hal.uefi import * from chipsec.hal.spi_uefi import * -_uefi = UEFI( chipsec_util._cs ) +from chipsec.command import BaseCommand # Unified Extensible Firmware Interface (UEFI) -def uefi(argv): +class UEFICommand(BaseCommand): """ >>> chipsec_util uefi var-list >>> chipsec_util uefi var-find | @@ -74,285 +73,294 @@ def uefi(argv): >>> chipsec_util uefi assemble AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE freeform lzma uefi_file.raw uefi_file.bin >>> chipsec_util uefi replace AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE bios.bin modified_bios.bin uefi_file.bin """ - - if 3 > len(argv): - print uefi.__doc__ - return - - if argv[2] == "types": - print "\n should be in [ %s ]\n" % (" | ".join( ["%s" % t for t in fw_types])) + \ - "chipsec_util uefi keys \n" + \ - " should be one of the following EFI variables\n" + \ - " [ %s ]\n" % (" | ".join( ["%s" % var for var in SECURE_BOOT_KEY_VARIABLES])) - return + + def requires_driver(self): + # No driver required when printing the util documentation + if len(self.argv) < 3: + return False + return True + + def run(self): + _uefi = UEFI( self.cs ) + if len(self.argv) < 3: + print UEFICommand.__doc__ + return - op = argv[2] - t = time.time() + if self.argv[2] == "types": + print "\n should be in [ %s ]\n" % (" | ".join( ["%s" % t for t in fw_types])) + \ + "chipsec_util uefi keys \n" + \ + " should be one of the following EFI variables\n" + \ + " [ %s ]\n" % (" | ".join( ["%s" % var for var in SECURE_BOOT_KEY_VARIABLES])) + return + + op = self.argv[2] + t = time.time() - filename = None - if ( 'var-read' == op ): - if (4 < len(argv)): - name = argv[3] - guid = argv[4] - if (5 < len(argv)): - filename = argv[5] - logger().log( "[CHIPSEC] Reading EFI variable Name='%s' GUID={%s} to '%s' via Variable API.." % (name, guid, filename) ) - var = _uefi.get_EFI_variable( name, guid, filename ) + filename = None + if ( 'var-read' == op ): + if (4 < len(self.argv)): + name = self.argv[3] + guid = self.argv[4] + if (5 < len(self.argv)): + filename = self.argv[5] + self.logger.log( "[CHIPSEC] Reading EFI variable Name='%s' GUID={%s} to '%s' via Variable API.." % (name, guid, filename) ) + var = _uefi.get_EFI_variable( name, guid, filename ) - elif ( 'var-write' == op ): + elif ( 'var-write' == op ): + + if (5 < len(self.argv)): + name = self.argv[3] + guid = self.argv[4] + filename = self.argv[5] + else: + print UEFICommand.__doc__ + return + self.logger.log( "[CHIPSEC] writing EFI variable Name='%s' GUID={%s} from '%s' via Variable API.." % (name, guid, filename) ) + status = _uefi.set_EFI_variable_from_file( name, guid, filename ) + self.logger.log("[CHIPSEC] status: %s" % chipsec.hal.uefi_common.EFI_STATUS_DICT[status]) + if status == 0: + self.logger.log( "[CHIPSEC] set_EFI_variable return SUCCESS status" ) + else: + self.logger.error( "set_EFI_variable wasn't able to modify variable" ) + + elif ( 'var-delete' == op ): + + if (4 < len(self.argv)): + name = self.argv[3] + guid = self.argv[4] + else: + print UEFICommand.__doc__ + return + self.logger.log( "[CHIPSEC] Deleting EFI variable Name='%s' GUID={%s} via Variable API.." % (name, guid) ) + status = _uefi.delete_EFI_variable( name, guid ) + self.logger.log("Returned %s" % chipsec.hal.uefi_common.EFI_STATUS_DICT[status]) + if status == 0: self.logger.log( "[CHIPSEC] delete_EFI_variable return SUCCESS status" ) + else: self.logger.error( "delete_EFI_variable wasn't able to delete variable" ) + + elif ( 'var-list' == op ): + + #infcls = 2 + #if (3 < len(self.argv)): filename = self.argv[3] + #if (4 < len(self.argv)): infcls = int(self.argv[4],16) + self.logger.log( "[CHIPSEC] Enumerating all EFI variables via OS specific EFI Variable API.." ) + efi_vars = _uefi.list_EFI_variables() + if efi_vars is None: + self.logger.log( "[CHIPSEC] Could not enumerate EFI Variables (Legacy OS?). Exit.." ) + return + + self.logger.log( "[CHIPSEC] Decoding EFI Variables.." ) + _orig_logname = self.logger.LOG_FILE_NAME + self.logger.set_log_file( 'efi_variables.lst' ) + #print_sorted_EFI_variables( efi_vars ) + nvram_pth = 'efi_variables.dir' + if not os.path.exists( nvram_pth ): os.makedirs( nvram_pth ) + decode_EFI_variables( efi_vars, nvram_pth ) + self.logger.set_log_file( _orig_logname ) + + #efi_vars = _uefi.list_EFI_variables( infcls, filename ) + #_orig_logname = self.logger.LOG_FILE_NAME + #self.logger.set_log_file( (filename + '.nv.lst') ) + #_uefi.parse_EFI_variables( filename, efi_vars, False, FWType.EFI_FW_TYPE_WIN ) + #self.logger.set_log_file( _orig_logname ) + + self.logger.log( "[CHIPSEC] Variables are in efi_variables.lst log and efi_variables.dir directory" ) + + elif ( 'var-find' == op ): + + _vars = _uefi.list_EFI_variables() + if _vars is None: + self.logger.log_warning( 'Could not enumerate UEFI variables (non-UEFI OS?)' ) + return + + _input_var = self.argv[3] + if ('-' in _input_var): + self.logger.log( "[*] Searching for UEFI variable with GUID {%s}.." % _input_var ) + for name in _vars: + n = 0 + for (off, buf, hdr, data, guid, attrs) in _vars[name]: + if _input_var == guid: + var_fname = '%s_%s_%s_%d.bin' % (name,guid,get_attr_string(attrs).strip(),n) + self.logger.log_good( "Found UEFI variable %s:%s. Dumped to '%s'" % (guid,name,var_fname) ) + write_file( var_fname, data ) + n += 1 + else: + self.logger.log( "[*] Searching for UEFI variable with name %s.." % _input_var ) + for name,_v in _vars.iteritems(): + n = 0 + for (off, buf, hdr, data, guid, attrs) in _v: + if _input_var == name: + var_fname = '%s_%s_%s_%d.bin' % (name,guid,get_attr_string(attrs).strip(),n) + self.logger.log_good( "Found UEFI variable %s:%s. Dumped to '%s'" % (guid,name,var_fname) ) + write_file( var_fname, data ) + n += 1 + + elif ( 'nvram' == op or 'nvram-auth' == op ): + + authvars = ('nvram-auth' == op) + efi_nvram_format = self.argv[3] + if (4 == len(self.argv)): + self.logger.log( "[CHIPSEC] Extracting EFI Variables directly in SPI ROM.." ) + try: + self.cs.init( True ) + _spi = SPI( self.cs ) + except UnknownChipsetError, msg: + print ("ERROR: Unknown chipset vendor (%s)" % str(msg)) + raise + except SpiRuntimeError, msg: + print ("ERROR: SPI initialization error" % str(msg)) + raise + + (bios_base,bios_limit,freg) = _spi.get_SPI_region( BIOS ) + bios_size = bios_limit - bios_base + 1 + self.logger.log( "[CHIPSEC] Reading BIOS: base = 0x%08X, limit = 0x%08X, size = 0x%08X" % (bios_base,bios_limit,bios_size) ) + rom = _spi.read_spi( bios_base, bios_size ) + self.cs.stop( True ) + del _spi + elif (5 == len(self.argv)): + romfilename = self.argv[4] + self.logger.log( "[CHIPSEC] Extracting EFI Variables from ROM file '%s'" % romfilename ) + rom = read_file( romfilename ) + + _orig_logname = self.logger.LOG_FILE_NAME + self.logger.set_log_file( (romfilename + '.nv.lst') ) + _uefi.parse_EFI_variables( romfilename, rom, authvars, efi_nvram_format ) + self.logger.set_log_file( _orig_logname ) + + elif ( 'decode' == op ): + + if (4 < len(self.argv)): + filename = self.argv[3] + fwtype = self.argv[4] + else: + print UEFICommand.__doc__ + return + self.logger.log( "[CHIPSEC] Parsing EFI volumes from '%s'.." % filename ) + _orig_logname = self.logger.LOG_FILE_NAME + self.logger.set_log_file( filename + '.efi_fv.log' ) + cur_dir = self.cs.helper.getcwd() + decode_uefi_region(_uefi, cur_dir, filename, fwtype) + self.logger.set_log_file( _orig_logname ) + + elif ( 'keys' == op ): + + if (3 < len(self.argv)): + var_filename = self.argv[ 3 ] + else: + print UEFICommand.__doc__ + return + self.logger.log( "[CHIPSEC] Parsing EFI variable from '%s'.." % var_filename ) + parse_efivar_file( var_filename ) + + elif ( 'tables' == op ): + self.logger.log( "[CHIPSEC] Searching memory for and dumping EFI tables (this may take a minute)..\n" ) + _uefi.dump_EFI_tables() + + elif ( 's3bootscript' == op ): + self.logger.log( "[CHIPSEC] Searching for and parsing S3 resume bootscripts.." ) + if len(self.argv) > 3: + bootscript_pa = int(self.argv[3],16) + self.logger.log( '[*] Reading S3 boot-script from memory at 0x%016X..' % bootscript_pa ) + script_all = self.cs.mem.read_physical_mem( bootscript_pa, 0x100000 ) + self.logger.log( '[*] Decoding S3 boot-script opcodes..' ) + script_entries = chipsec.hal.uefi.parse_script( script_all, True ) + else: + (bootscript_PAs,parsed_scripts) = _uefi.get_s3_bootscript( True ) + + elif op in ['insert_before', 'insert_after', 'replace']: + + if len(self.argv) < 7: + print UEFICommand.__doc__ + return + + (guid, rom_file, new_file, efi_file) = self.argv[3:7] + + commands = { + 'insert_before' : CMD_UEFI_FILE_INSERT_BEFORE, + 'insert_after' : CMD_UEFI_FILE_INSERT_AFTER, + 'replace' : CMD_UEFI_FILE_REPLACE + } + + if get_guid_bin(guid) == '': + print '*** Error *** Invalid GUID: %s' % guid + return + + if not os.path.isfile(rom_file): + print '*** Error *** File doesn\'t exist: %s' % rom_file + return + + if not os.path.isfile(efi_file): + print '*** Error *** File doesn\'t exist: %s' % efi_file + return + + rom_image = chipsec.file.read_file(rom_file) + efi_image = chipsec.file.read_file(efi_file) + new_image = modify_uefi_region(rom_image, commands[op], guid, efi_image) + chipsec.file.write_file(new_file, new_image) + + elif op == 'remove': + + if len(self.argv) < 6: + print UEFICommand.__doc__ + return + + (guid, rom_file, new_file) = self.argv[3:6] + + if get_guid_bin(guid) == '': + print '*** Error *** Invalid GUID: %s' % guid + return + + if not os.path.isfile(rom_file): + print '*** Error *** File doesn\'t exist: %s' % rom_file + return + + rom_image = chipsec.file.read_file(rom_file) + new_image = modify_uefi_region(rom_image, CMD_UEFI_FILE_REMOVE, guid) + chipsec.file.write_file(new_file, new_image) + + elif op == 'assemble': + + compression = {'none': 0, 'tiano': 1, 'lzma': 2} + + if len(self.argv) < 8: + print UEFICommand.__doc__ + return + + (guid, file_type, comp, raw_file, efi_file) = self.argv[3:8] + + if get_guid_bin(guid) == '': + print '*** Error *** Invalid GUID: %s' % guid + return + + if not os.path.isfile(raw_file): + print '*** Error *** File doesn\'t exist: %s' % raw_file + return + + if comp not in compression: + print '*** Error *** Unknown compression: %s' % comp + return + + compression_type = compression[comp] + + if file_type == 'freeform': + raw_image = chipsec.file.read_file(raw_file) + wrap_image = assemble_uefi_raw(raw_image) + if compression_type > 0: + comp_image = compress_image(_uefi, wrap_image, compression_type) + wrap_image = assemble_uefi_section(comp_image, len(wrap_image), compression_type) + uefi_image = assemble_uefi_file(guid, wrap_image) + chipsec.file.write_file(efi_file, uefi_image) + else: + print '*** Error *** Unknow file type: %s' % file_type + return + + self.logger.log( "[CHIPSEC] UEFI file was successfully assembled! Binary file size: %d, compressed UEFI file size: %d" % (len(raw_image), len(uefi_image)) ) - if (5 < len(argv)): - name = argv[3] - guid = argv[4] - filename = argv[5] else: - print uefi.__doc__ - return - logger().log( "[CHIPSEC] writing EFI variable Name='%s' GUID={%s} from '%s' via Variable API.." % (name, guid, filename) ) - status = _uefi.set_EFI_variable_from_file( name, guid, filename ) - logger().log("[CHIPSEC] status: %s" % chipsec.hal.uefi_common.EFI_STATUS_DICT[status]) - if status == 0: - logger().log( "[CHIPSEC] set_EFI_variable return SUCCESS status" ) - else: - logger().error( "set_EFI_variable wasn't able to modify variable" ) - - elif ( 'var-delete' == op ): - - if (4 < len(argv)): - name = argv[3] - guid = argv[4] - else: - print uefi.__doc__ - return - logger().log( "[CHIPSEC] Deleting EFI variable Name='%s' GUID={%s} via Variable API.." % (name, guid) ) - status = _uefi.delete_EFI_variable( name, guid ) - logger().log("Returned %s" % chipsec.hal.uefi_common.EFI_STATUS_DICT[status]) - if status == 0: logger().log( "[CHIPSEC] delete_EFI_variable return SUCCESS status" ) - else: logger().error( "delete_EFI_variable wasn't able to delete variable" ) - - elif ( 'var-list' == op ): - - #infcls = 2 - #if (3 < len(argv)): filename = argv[3] - #if (4 < len(argv)): infcls = int(argv[4],16) - logger().log( "[CHIPSEC] Enumerating all EFI variables via OS specific EFI Variable API.." ) - efi_vars = _uefi.list_EFI_variables() - if efi_vars is None: - logger().log( "[CHIPSEC] Could not enumerate EFI Variables (Legacy OS?). Exit.." ) + self.logger.error( "Unknown uefi command '%s'" % op ) + print UEFICommand.__doc__ return - logger().log( "[CHIPSEC] Decoding EFI Variables.." ) - _orig_logname = logger().LOG_FILE_NAME - logger().set_log_file( 'efi_variables.lst' ) - #print_sorted_EFI_variables( efi_vars ) - nvram_pth = 'efi_variables.dir' - if not os.path.exists( nvram_pth ): os.makedirs( nvram_pth ) - decode_EFI_variables( efi_vars, nvram_pth ) - logger().set_log_file( _orig_logname ) + self.logger.log( "[CHIPSEC] (uefi) time elapsed %.3f" % (time.time()-t) ) - #efi_vars = _uefi.list_EFI_variables( infcls, filename ) - #_orig_logname = logger().LOG_FILE_NAME - #logger().set_log_file( (filename + '.nv.lst') ) - #_uefi.parse_EFI_variables( filename, efi_vars, False, FWType.EFI_FW_TYPE_WIN ) - #logger().set_log_file( _orig_logname ) - logger().log( "[CHIPSEC] Variables are in efi_variables.lst log and efi_variables.dir directory" ) - - elif ( 'var-find' == op ): - - _vars = _uefi.list_EFI_variables() - if _vars is None: - logger().log_warning( 'Could not enumerate UEFI variables (non-UEFI OS?)' ) - return - - _input_var = argv[3] - if ('-' in _input_var): - logger().log( "[*] Searching for UEFI variable with GUID {%s}.." % _input_var ) - for name in _vars: - n = 0 - for (off, buf, hdr, data, guid, attrs) in _vars[name]: - if _input_var == guid: - var_fname = '%s_%s_%s_%d.bin' % (name,guid,get_attr_string(attrs).strip(),n) - logger().log_good( "Found UEFI variable %s:%s. Dumped to '%s'" % (guid,name,var_fname) ) - write_file( var_fname, data ) - n += 1 - else: - logger().log( "[*] Searching for UEFI variable with name %s.." % _input_var ) - for name,_v in _vars.iteritems(): - n = 0 - for (off, buf, hdr, data, guid, attrs) in _v: - if _input_var == name: - var_fname = '%s_%s_%s_%d.bin' % (name,guid,get_attr_string(attrs).strip(),n) - logger().log_good( "Found UEFI variable %s:%s. Dumped to '%s'" % (guid,name,var_fname) ) - write_file( var_fname, data ) - n += 1 - - elif ( 'nvram' == op or 'nvram-auth' == op ): - - authvars = ('nvram-auth' == op) - efi_nvram_format = argv[3] - if (4 == len(argv)): - logger().log( "[CHIPSEC] Extracting EFI Variables directly in SPI ROM.." ) - try: - chipsec_util._cs.init( True ) - _spi = SPI( chipsec_util._cs ) - except UnknownChipsetError, msg: - print ("ERROR: Unknown chipset vendor (%s)" % str(msg)) - raise - except SpiRuntimeError, msg: - print ("ERROR: SPI initialization error" % str(msg)) - raise - - (bios_base,bios_limit,freg) = _spi.get_SPI_region( BIOS ) - bios_size = bios_limit - bios_base + 1 - logger().log( "[CHIPSEC] Reading BIOS: base = 0x%08X, limit = 0x%08X, size = 0x%08X" % (bios_base,bios_limit,bios_size) ) - rom = _spi.read_spi( bios_base, bios_size ) - chipsec_util._cs.stop( True ) - del _spi - elif (5 == len(argv)): - romfilename = argv[4] - logger().log( "[CHIPSEC] Extracting EFI Variables from ROM file '%s'" % romfilename ) - rom = read_file( romfilename ) - - _orig_logname = logger().LOG_FILE_NAME - logger().set_log_file( (romfilename + '.nv.lst') ) - _uefi.parse_EFI_variables( romfilename, rom, authvars, efi_nvram_format ) - logger().set_log_file( _orig_logname ) - - elif ( 'decode' == op ): - - if (4 < len(argv)): - filename = argv[3] - fwtype = argv[4] - else: - print uefi.__doc__ - return - logger().log( "[CHIPSEC] Parsing EFI volumes from '%s'.." % filename ) - _orig_logname = logger().LOG_FILE_NAME - logger().set_log_file( filename + '.efi_fv.log' ) - cur_dir = chipsec_util._cs.helper.getcwd() - decode_uefi_region(_uefi, cur_dir, filename, fwtype) - logger().set_log_file( _orig_logname ) - - elif ( 'keys' == op ): - - if (3 < len(argv)): - var_filename = argv[ 3 ] - else: - print uefi.__doc__ - return - logger().log( "[CHIPSEC] Parsing EFI variable from '%s'.." % var_filename ) - parse_efivar_file( var_filename ) - - elif ( 'tables' == op ): - logger().log( "[CHIPSEC] Searching memory for and dumping EFI tables (this may take a minute)..\n" ) - _uefi.dump_EFI_tables() - - elif ( 's3bootscript' == op ): - logger().log( "[CHIPSEC] Searching for and parsing S3 resume bootscripts.." ) - if len(argv) > 3: - bootscript_pa = int(argv[3],16) - logger().log( '[*] Reading S3 boot-script from memory at 0x%016X..' % bootscript_pa ) - script_all = chipsec_util._cs.mem.read_physical_mem( bootscript_pa, 0x100000 ) - logger().log( '[*] Decoding S3 boot-script opcodes..' ) - script_entries = chipsec.hal.uefi.parse_script( script_all, True ) - else: - (bootscript_PAs,parsed_scripts) = _uefi.get_s3_bootscript( True ) - - elif op in ['insert_before', 'insert_after', 'replace']: - - if len(argv) < 7: - print uefi.__doc__ - return - - (guid, rom_file, new_file, efi_file) = argv[3:7] - - commands = { - 'insert_before' : CMD_UEFI_FILE_INSERT_BEFORE, - 'insert_after' : CMD_UEFI_FILE_INSERT_AFTER, - 'replace' : CMD_UEFI_FILE_REPLACE - } - - if get_guid_bin(guid) == '': - print '*** Error *** Invalid GUID: %s' % guid - return - - if not os.path.isfile(rom_file): - print '*** Error *** File doesn\'t exist: %s' % rom_file - return - - if not os.path.isfile(efi_file): - print '*** Error *** File doesn\'t exist: %s' % efi_file - return - - rom_image = chipsec.file.read_file(rom_file) - efi_image = chipsec.file.read_file(efi_file) - new_image = modify_uefi_region(rom_image, commands[op], guid, efi_image) - chipsec.file.write_file(new_file, new_image) - - elif op == 'remove': - - if len(argv) < 6: - print uefi.__doc__ - return - - (guid, rom_file, new_file) = argv[3:6] - - if get_guid_bin(guid) == '': - print '*** Error *** Invalid GUID: %s' % guid - return - - if not os.path.isfile(rom_file): - print '*** Error *** File doesn\'t exist: %s' % rom_file - return - - rom_image = chipsec.file.read_file(rom_file) - new_image = modify_uefi_region(rom_image, CMD_UEFI_FILE_REMOVE, guid) - chipsec.file.write_file(new_file, new_image) - - elif op == 'assemble': - - compression = {'none': 0, 'tiano': 1, 'lzma': 2} - - if len(argv) < 8: - print uefi.__doc__ - return - - (guid, file_type, comp, raw_file, efi_file) = argv[3:8] - - if get_guid_bin(guid) == '': - print '*** Error *** Invalid GUID: %s' % guid - return - - if not os.path.isfile(raw_file): - print '*** Error *** File doesn\'t exist: %s' % raw_file - return - - if comp not in compression: - print '*** Error *** Unknown compression: %s' % comp - return - - compression_type = compression[comp] - - if file_type == 'freeform': - raw_image = chipsec.file.read_file(raw_file) - wrap_image = assemble_uefi_raw(raw_image) - if compression_type > 0: - comp_image = compress_image(_uefi, wrap_image, compression_type) - wrap_image = assemble_uefi_section(comp_image, len(wrap_image), compression_type) - uefi_image = assemble_uefi_file(guid, wrap_image) - chipsec.file.write_file(efi_file, uefi_image) - else: - print '*** Error *** Unknow file type: %s' % file_type - return - - logger().log( "[CHIPSEC] UEFI file was successfully assembled! Binary file size: %d, compressed UEFI file size: %d" % (len(raw_image), len(uefi_image)) ) - - else: - logger().error( "Unknown uefi command '%s'" % op ) - print uefi.__doc__ - return - - logger().log( "[CHIPSEC] (uefi) time elapsed %.3f" % (time.time()-t) ) - -chipsec_util.commands['uefi'] = {'func' : uefi, 'start_driver' : True, 'help' : uefi.__doc__ } \ No newline at end of file +commands = { 'uefi': UEFICommand } diff --git a/source/tool/chipsec_util.py b/source/tool/chipsec_util.py index a43b1926..17d527ce 100644 --- a/source/tool/chipsec_util.py +++ b/source/tool/chipsec_util.py @@ -76,7 +76,6 @@ class ChipsecUtil: "All numeric values are in hex\n" + \ " is in {1, byte, 2, word, 4, dword}\n\n" self.commands = {} - self.commands['help'] = {'func' : self.chipsec_util_help, 'start_driver' : False, 'help' : 'chipsec_util help '} # determine if CHIPSEC is loaded as chipsec_*.exe or in python self.CHIPSEC_LOADED_AS_EXE = True if (hasattr(sys, "frozen") or hasattr(sys, "importers")) else False @@ -87,14 +86,14 @@ class ChipsecUtil: """ if len(argv) <= 2: logger().log( '\n[CHIPSEC] chipsec_util command-line extensions should be one of the following:' ) - for cmd in self.commands.keys(): + for cmd in sorted(self.commands.keys() + ['help']): logger().log( ' %s' % cmd ) #logger().log( chipsec_util_commands[cmd]['help'] ) else: print self.global_usage print "\nHelp for %s command:\n" % argv[2] - print self.commands[argv[2]]['help'] + print self.commands[argv[2]].__doc__ def f_mod_zip(self, x): ZIP_UTILCMD_RE = re.compile("^chipsec\/utilcmd\/\w+\.pyc$", re.IGNORECASE) @@ -140,8 +139,8 @@ class ChipsecUtil: #exec 'from chipsec.utilcmd.' + cmd + ' import *' cmd_path = 'chipsec.utilcmd.' + cmd module = importlib.import_module( cmd_path ) - cu = getattr(module, 'chipsec_util') - self.commands.update(cu.commands) + cu = getattr(module, 'commands') + self.commands.update(cu) except ImportError, msg: logger().error( "Couldn't import util command extension '%s'" % cmd ) raise ImportError, msg @@ -149,7 +148,8 @@ class ChipsecUtil: if 1 < len(argv): cmd = argv[ 1 ] if self.commands.has_key( cmd ): - if self.commands[ cmd ]['start_driver']: + comm = self.commands[cmd](argv, cs = _cs) + if comm.requires_driver(): try: _cs.init( _Platform, True ) except UnknownChipsetError, msg: @@ -163,15 +163,17 @@ class ChipsecUtil: sys.exit(-1) logger().log( "[CHIPSEC] Executing command '%s' with args %s\n" % (cmd,argv[2:]) ) - self.commands[ cmd ]['func']( argv ) + comm.run() + if comm.requires_driver(): + _cs.destroy(True) - if self.commands[ cmd ]['start_driver']: _cs.destroy( True ) + elif cmd == 'help': + self.chipsec_util_help(argv) else: logger().error( "Unknown command '%.32s'" % cmd ) else: logger().error( "Not enough parameters" ) self.chipsec_util_help([]) - del _cs exit_code = 32 return exit_code