This is an automated email from Gerrit. "Sergi Granell Escalfet <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9601
-- gerrit commit e3cb3d6f530a1b1751d96499be8d7855301101b5 Author: Sergi Granell Escalfet <[email protected]> Date: Tue Apr 28 12:02:42 2026 +0900 target/image: add image_type_name() and unify type string table Add a static image_type_names[] table mapping enum image_type to the canonical type strings (bin, ihex, elf, mem, s19, build) and expose it via a new image_type_name() function. Refactor identify_image_type() to drive its reverse lookup from the same table, eliminating the duplicate hardcoded string list and ensuring the parser and any future users stay in sync. Change-Id: I992ad34ca548cd0252a55cda7a0a911b48142979 Signed-off-by: Sergi Granell Escalfet <[email protected]> diff --git a/src/target/image.c b/src/target/image.c index 9b7031b290..63d94531d1 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -94,29 +94,35 @@ static int autodetect_image_type(struct image *image, const char *url) return ERROR_OK; } +static const char *const image_type_names[] = { + [IMAGE_BINARY] = "bin", + [IMAGE_IHEX] = "ihex", + [IMAGE_MEMORY] = "mem", + [IMAGE_ELF] = "elf", + [IMAGE_SRECORD] = "s19", + [IMAGE_BUILDER] = "build", +}; + +const char *image_type_name(enum image_type type) +{ + if ((unsigned int)type < ARRAY_SIZE(image_type_names)) + return image_type_names[type]; + return NULL; +} + static int identify_image_type(struct image *image, const char *type_string, const char *url) { if (type_string) { - if (!strcmp(type_string, "bin")) { - image->type = IMAGE_BINARY; - } else if (!strcmp(type_string, "ihex")) { - image->type = IMAGE_IHEX; - } else if (!strcmp(type_string, "elf")) { - image->type = IMAGE_ELF; - } else if (!strcmp(type_string, "mem")) { - image->type = IMAGE_MEMORY; - } else if (!strcmp(type_string, "s19")) { - image->type = IMAGE_SRECORD; - } else if (!strcmp(type_string, "build")) { - image->type = IMAGE_BUILDER; - } else { - LOG_ERROR("Unknown image type: %s, use one of: bin, ihex, elf, mem, s19, build", type_string); - return ERROR_IMAGE_TYPE_UNKNOWN; + for (unsigned int i = 0; i < ARRAY_SIZE(image_type_names); i++) { + if (!strcmp(type_string, image_type_name(i))) { + image->type = (enum image_type)i; + return ERROR_OK; + } } - } else - return autodetect_image_type(image, url); - - return ERROR_OK; + LOG_ERROR("Unknown image type: %s, use one of: bin, ihex, elf, mem, s19, build", type_string); + return ERROR_IMAGE_TYPE_UNKNOWN; + } + return autodetect_image_type(image, url); } static int image_ihex_buffer_complete_inner(struct image *image, diff --git a/src/target/image.h b/src/target/image.h index 03bc068d62..5f783beebd 100644 --- a/src/target/image.h +++ b/src/target/image.h @@ -91,6 +91,7 @@ struct image_mot { uint8_t *buffer; }; +const char *image_type_name(enum image_type type); int image_open(struct image *image, const char *url, const char *type_string); int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read); --
