From 6ad9dcb262bc29618240bebc3909e43065a94866 Mon Sep 17 00:00:00 2001 From: Geoffroy Van Cutsem Date: Tue, 26 Oct 2021 11:06:28 +0200 Subject: [PATCH] config_tools: fix crash in board_inspector if cpuid is missing Fix a crash in the 'board_inspector.py' tool in case 'cpuid' is not installed on the system. The tools crashes because 'cpuid' is used before the check for dependencies is done in the code. That check for dependencies is done in the 'legacy/board_parser.py' file. But the native_check() function that is called before it also expects the cpuid tool to be installed. The fix is to move the check for dependencies in the main 'board_inspector.py' file, before any other operation is done. Tracked-On: #6719 Signed-off-by: Geoffroy Van Cutsem --- .../board_inspector/board_inspector.py | 27 +++++++++++++++++++ .../board_inspector/legacy/board_parser.py | 25 ----------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/misc/config_tools/board_inspector/board_inspector.py b/misc/config_tools/board_inspector/board_inspector.py index 0fbc7caa0..dbfd28650 100755 --- a/misc/config_tools/board_inspector/board_inspector.py +++ b/misc/config_tools/board_inspector/board_inspector.py @@ -16,6 +16,30 @@ from cpuparser import parse_cpuid, get_online_cpu_ids script_dir = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(script_dir)) +def check_deps(): + # Check that the required tools are installed on the system + BIN_LIST = ['cpuid', 'rdmsr', 'lspci', ' dmidecode', 'blkid', 'stty'] + cpuid_min_ver = 20170122 + for execute in BIN_LIST: + res = subprocess.Popen("which {}".format(execute), + shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, close_fds=True) + + line = res.stdout.readline().decode('ascii') + if not line: + logging.warning("'{}' cannot be found, please install it!".format(execute)) + sys.exit(1) + + if execute == 'cpuid': + res = subprocess.Popen("cpuid -v", + shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, close_fds=True) + line = res.stdout.readline().decode('ascii') + version = line.split()[2] + if int(version) < cpuid_min_ver: + logging.warning("This tool requires CPUID version >= {}".format(cpuid_min_ver)) + sys.exit(1) + def native_check(): cpu_ids = get_online_cpu_ids() cpu_id = cpu_ids.pop(0) @@ -25,6 +49,9 @@ def native_check(): "supported under KVM/QEMU. Unexpected results may occur when deviating from that combination.") def main(board_name, board_xml, args): + # Check that the dependencies are met + check_deps() + # Check if this is native os native_check() diff --git a/misc/config_tools/board_inspector/legacy/board_parser.py b/misc/config_tools/board_inspector/legacy/board_parser.py index f90e7f278..0d4762464 100755 --- a/misc/config_tools/board_inspector/legacy/board_parser.py +++ b/misc/config_tools/board_inspector/legacy/board_parser.py @@ -18,12 +18,8 @@ import parser_lib OUTPUT = "./out/" PY_CACHE = "__pycache__" -# This file store information which query from hw board -BIN_LIST = ['cpuid', 'rdmsr', 'lspci', ' dmidecode', 'blkid', 'stty'] - CPU_VENDOR = "GenuineIntel" - def check_permission(): """Check if it is root permission""" if os.getuid(): @@ -51,27 +47,6 @@ def check_env(): parser_lib.print_red("Please run this tools on {}!".format(CPU_VENDOR)) sys.exit(1) - # check if required tools are exists - for excute in BIN_LIST: - res = subprocess.Popen("which {}".format(excute), - shell=True, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, close_fds=True) - - line = res.stdout.readline().decode('ascii') - if not line: - parser_lib.print_yel("'{}' not found, please install it!".format(excute)) - sys.exit(1) - - if excute == 'cpuid': - res = subprocess.Popen("cpuid -v", - shell=True, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, close_fds=True) - line = res.stdout.readline().decode('ascii') - version = line.split()[2] - if int(version) < 20170122: - parser_lib.print_yel("Need CPUID version >= 20170122") - sys.exit(1) - if os.path.exists(OUTPUT): shutil.rmtree(OUTPUT)