From 3034162f30a5eee479a269a45f65eb3915bee3ae Mon Sep 17 00:00:00 2001 From: Eren Terzioglu Date: Wed, 23 Oct 2024 11:55:19 +0200 Subject: [PATCH] Improve nxdiag example for Espressif devices --- system/nxdiag/Kconfig | 6 +++ system/nxdiag/Makefile | 4 ++ system/nxdiag/nxdiag.c | 9 ++++ tools/host_sysinfo.py | 103 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) diff --git a/system/nxdiag/Kconfig b/system/nxdiag/Kconfig index 6a9f5c1e94..ceec711f8d 100644 --- a/system/nxdiag/Kconfig +++ b/system/nxdiag/Kconfig @@ -68,4 +68,10 @@ config SYSTEM_NXDIAG_ESPRESSIF ---help--- Enable Espressif-specific information and checks. +config SYSTEM_NXDIAG_ESPRESSIF_CHIP + bool "Espressif Chip" + default n + ---help--- + Enable Espressif-specific information about chip. Chip must be connected during build process. + endif # SYSTEM_NXDIAG diff --git a/system/nxdiag/Makefile b/system/nxdiag/Makefile index 743610166f..f25d17aac8 100644 --- a/system/nxdiag/Makefile +++ b/system/nxdiag/Makefile @@ -89,6 +89,10 @@ else NXDIAG_FLAGS += --espressif "$(TOPDIR)" "$(HALDIR)" endif +ifeq ($(CONFIG_SYSTEM_NXDIAG_ESPRESSIF_CHIP),y) +NXDIAG_FLAGS += --espressif_chip +endif + endif # Common build diff --git a/system/nxdiag/nxdiag.c b/system/nxdiag/nxdiag.c index b3e56240e6..24ff8ca77b 100644 --- a/system/nxdiag/nxdiag.c +++ b/system/nxdiag/nxdiag.c @@ -345,6 +345,15 @@ int main(int argc, char *argv[]) print_array(ESPRESSIF_TOOLCHAIN, ESPRESSIF_TOOLCHAIN_ARRAY_SIZE); printf("Esptool version: %s\n\n", ESPRESSIF_ESPTOOL); printf("HAL version: %s\n\n", ESPRESSIF_HAL); +#ifdef CONFIG_SYSTEM_NXDIAG_ESPRESSIF_CHIP + printf("CHIP ID: %s\n\n", ESPRESSIF_CHIP_ID); + printf("Flash ID:\n"); + print_array(ESPRESSIF_FLASH_ID, ESPRESSIF_FLASH_ID_ARRAY_SIZE); + printf("Security information:\n"); + print_array(ESPRESSIF_SECURITY_INFO, ESPRESSIF_SECURITY_INFO_ARRAY_SIZE); + printf("Flash status: %s\n\n", ESPRESSIF_FLASH_STAT); + printf("MAC address: %s\n\n", ESPRESSIF_MAC_ADDR); +#endif #endif } diff --git a/tools/host_sysinfo.py b/tools/host_sysinfo.py index 4904038f0b..9e8528b0e0 100755 --- a/tools/host_sysinfo.py +++ b/tools/host_sysinfo.py @@ -29,6 +29,66 @@ def __call__(self, parser, namespace, values, option_string=None): # Espressif # +def run_espressif_tool(cmd): + tool = "esptool.py" + strings_out = "" + command = "{} {}".format(tool, cmd) + + strings_out = subprocess.run( + command, universal_newlines=True, shell=True, capture_output=True + ) + + return strings_out.stdout + +def get_espressif_chip_id(): + output = run_espressif_tool("chip_id") + string_out = next((s for s in output.split("\n") if "Chip ID" in s), "Not found") + if string_out != "Not found": + string_out = string_out.split("Warning: ")[-1] + return string_out + +def get_espressif_flash_id(): + strings_out = [] + output = run_espressif_tool("flash_id") + + strings_out.append(next((s for s in output.split("\n") if "Manufacturer" in s), "Manufacturer: Not found")) + strings_out.append(next((s for s in output.split("\n") if "Device" in s), "Device: Not found")) + + return strings_out + +def get_espressif_security_info(): + output = run_espressif_tool("get_security_info") + + start_string = "=====================" + stop_string = "Hard resetting via RTS pin..." + output = output.split("\n") + strings_out = [] + + str_out = next((s for s in output if start_string in s), "Not found") + if str_out != "Not found": + start_index = output.index(start_string) + 1 + stop_index = output.index(stop_string) + strings_out = output[start_index:stop_index] + else: + strings_out.append(str_out) + + return strings_out + +def get_espressif_flash_status(): + output = run_espressif_tool("read_flash_status") + + string_out = next((s for s in output.split("\n") if "Status value" in s), "Not found") + if string_out != "Not found": + string_out = string_out.split("Status value: ")[-1] + return string_out + +def get_espressif_mac_address(): + output = run_espressif_tool("read_mac") + + string_out = next((s for s in output.split("\n") if "MAC" in s), "Not found") + if string_out != "Not found": + string_out = string_out.split("MAC: ")[-1] + return string_out def get_espressif_bootloader_version(bindir): """ @@ -557,6 +617,40 @@ def generate_header(args): info["ESPRESSIF_HAL"] ) + if args.espressif_chip and info["ESPRESSIF_ESPTOOL"] not in "Not found": + info["ESPRESSIF_CHIP_ID"] = get_espressif_chip_id() + output += 'static const char ESPRESSIF_CHIP_ID[] = "{}";\n\n'.format( + info["ESPRESSIF_CHIP_ID"] + ) + + info["ESPRESSIF_FLASH_ID"] = get_espressif_flash_id() + output += "#define ESPRESSIF_FLASH_ID_ARRAY_SIZE {}\n".format( + len(info["ESPRESSIF_FLASH_ID"]) + ) + output += "static const char *ESPRESSIF_FLASH_ID[ESPRESSIF_FLASH_ID_ARRAY_SIZE] =\n{\n" + for each_item in info["ESPRESSIF_FLASH_ID"]: + output += ' "{}",\n'.format(each_item) + output += "};\n\n" + + info["ESPRESSIF_SECURITY_INFO"] = get_espressif_security_info() + output += "#define ESPRESSIF_SECURITY_INFO_ARRAY_SIZE {}\n".format( + len(info["ESPRESSIF_SECURITY_INFO"]) + ) + output += "static const char *ESPRESSIF_SECURITY_INFO[ESPRESSIF_SECURITY_INFO_ARRAY_SIZE] =\n{\n" + for each_item in info["ESPRESSIF_SECURITY_INFO"]: + output += ' "{}",\n'.format(each_item) + output += "};\n\n" + + info["ESPRESSIF_FLASH_STAT"] = get_espressif_flash_status() + output += 'static const char ESPRESSIF_FLASH_STAT[] = "{}";\n\n'.format( + info["ESPRESSIF_FLASH_STAT"] + ) + + info["ESPRESSIF_MAC_ADDR"] = get_espressif_mac_address() + output += 'static const char ESPRESSIF_MAC_ADDR[] = "{}";\n\n'.format( + info["ESPRESSIF_MAC_ADDR"] + ) + output += "#endif /* __SYSTEM_INFO_H */\n" return output @@ -590,6 +684,9 @@ def generate_header(args): --espressif : Get Espressif specific information. Requires the path to the bootloader binary directory. + --espressif_chip: + Get Espressif specific information about chip. + Requires device connection during build. """ # Generic arguments @@ -647,6 +744,12 @@ def generate_header(args): help="Get Espressif specific information. Requires the path to the bootloader binary and ESP HAL directories.", ) + parser.add_argument( + "--espressif_chip", + action='store_true', + help="Get Espressif specific information about chip. Requires device connection during build.", + ) + # Parse arguments if len(sys.argv) == 1: