From 36aff5fca48f74f1e00d0941bba150fca3a44e40 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 24 Aug 2012 10:33:38 +0200 Subject: image: Check if specified image type and device are correct --- src/image.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'src/image.c') diff --git a/src/image.c b/src/image.c index dde7f95..aed3d2f 100644 --- a/src/image.c +++ b/src/image.c @@ -134,15 +134,38 @@ char * image_name_alloc_from_values(struct image * image) { } -static void image_append(struct image * image, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { +static int image_append(struct image * image, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { + + enum image_type detected_type; image->hash = image_hash_from_data(image); - image->device = device_from_string(device); + image->device = DEVICE_ANY; + + if ( device && device[0] ) { + image->device = device_from_string(device); + if ( image->device == DEVICE_UNKNOWN ) { + ERROR("Specified Device %s is unknown", device); + image_free(image); + return -1; + } + } - if ( type && type[0] ) + detected_type = image_type_from_data(image); + image->type = detected_type; + + if ( type && type[0] ) { image->type = image_type_from_string(type); - else - image->type = image_type_from_data(image); + if ( image->type == IMAGE_UNKNOWN ) { + ERROR("Specified Image type %s is unknown", type); + image_free(image); + return -1; + } + if ( ! noverify && detected_type != image->type ) { + ERROR("Image type mishmash (detected %s, got %s)", image_type_to_string(detected_type), type); + image_free(image); + return -1; + } + } if ( hwrevs && hwrevs[0] ) image->hwrevs = strdup(hwrevs); @@ -159,6 +182,8 @@ static void image_append(struct image * image, const char * type, const char * d else image->layout = NULL; + return 0; + } static void image_align(struct image * image) { @@ -214,7 +239,8 @@ struct image * image_alloc_from_file(const char * file, const char * type, const image->orig_filename = strdup(file); lseek(image->fd, 0, SEEK_SET); - image_append(image, type, device, hwrevs, version, layout); + if ( image_append(image, type, device, hwrevs, version, layout) < 0 ) + return NULL; if ( ( ! type || ! type[0] ) && ( ! device || ! device[0] ) && ( ! hwrevs || ! hwrevs[0] ) && ( ! version || ! version[0] ) ) image_missing_values_from_name(image, file); @@ -237,7 +263,8 @@ struct image * image_alloc_from_shared_fd(int fd, size_t size, size_t offset, ui image->offset = offset; image->cur = 0; - image_append(image, type, device, hwrevs, version, layout); + if ( image_append(image, type, device, hwrevs, version, layout) < 0 ) + return NULL; if ( ! noverify && image->hash != hash ) { ERROR("Image hash mishmash (counted %#04x, got %#04x)", image->hash, hash); -- cgit v1.2.3