summaryrefslogtreecommitdiffstats
path: root/src/image.c
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2012-08-24 10:33:38 +0200
committerPali Rohár <pali.rohar@gmail.com>2012-08-24 10:33:38 +0200
commit36aff5fca48f74f1e00d0941bba150fca3a44e40 (patch)
tree6e44d797d7a3b1ebcfa953a99c7de67e76571fca /src/image.c
parentd9aeffa1d064e7bbae05860e5a51950d1a72373b (diff)
download0xFFFF-36aff5fca48f74f1e00d0941bba150fca3a44e40.tar.bz2
image: Check if specified image type and device are correct
Diffstat (limited to 'src/image.c')
-rw-r--r--src/image.c41
1 files changed, 34 insertions, 7 deletions
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);