From 03c6c3899f6b2e1b9f3ff08817ef491eee99286f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Mon, 29 Sep 2014 00:45:17 +0200 Subject: main: Fix memory corruption --- src/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index fdd2b4b..2daf848 100644 --- a/src/main.c +++ b/src/main.c @@ -210,7 +210,7 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { exit(1); } lseek(fd, 0, SEEK_SET); - layout = malloc(len); + layout = malloc(len+1); if ( ! layout ) { ALLOC_ERROR(); exit(1); @@ -219,6 +219,8 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { ERROR_INFO("Cannot read %lu bytes from layout file %s", len, layout_file); exit(1); } + layout[len] = 0; + close(fd); } image = image_alloc_from_file(file, type, device, hwrevs, version, layout); -- cgit v1.2.3 From 84fc04f647d425a38f3e5f0a77def3ab346520bd Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Mon, 29 Sep 2014 00:48:13 +0200 Subject: main: Implement -t (filter by type) for -e (dump images) --- src/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 2daf848..6aa4870 100644 --- a/src/main.c +++ b/src/main.c @@ -1177,9 +1177,21 @@ int main(int argc, char **argv) { buf[0] = 0; } - for ( i = 0; i < IMAGE_COUNT; ++i ) - if ( image_tmp_name(i) ) - dev_dump_image(dev, i, image_tmp_name(i)); + if ( filter_type ) { + enum image_type type = image_type_from_string(filter_type_arg); + if ( ! type || ! image_tmp_name(type) ) { + ERROR("Specified unknown image type for filtering: %s", filter_type_arg); + ret = 1; + goto clean; + } + ret = dev_dump_image(dev, type, image_tmp_name(type)); + if ( ret != 0 ) + goto clean; + } else { + for ( i = 0; i < IMAGE_COUNT; ++i ) + if ( image_tmp_name(i) ) + dev_dump_image(dev, i, image_tmp_name(i)); + } if ( buf[0] ) if ( chdir(buf) < 0 ) -- cgit v1.2.3 From c88e5264c979a10efe6d2209393c8df248ffc5bd Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Mon, 29 Sep 2014 00:48:37 +0200 Subject: main: Update usage --- src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 6aa4870..041299d 100644 --- a/src/main.c +++ b/src/main.c @@ -53,8 +53,8 @@ static void show_usage(void) { " -f flash all specified images\n" " -c cold flash 2nd and secondary images\n" " -x [/dev/mtd] check for bad blocks on mtd device (default: all)\n" - " -E file dump all device images to one fiasco image, see -t\n" - " -e [dir] dump all device images to directory, see -t (default: current)\n" + " -E file dump all device images to one fiasco image\n" + " -e [dir] dump all device images (or one -t) to directory (default: current)\n" "\n" "Device configuration:\n" @@ -84,7 +84,7 @@ static void show_usage(void) { "\n" "Image filters:\n" - " -t types filter images by type\n" + " -t type filter images by type\n" " -d dev filter images by device\n" " -w hw filter images by HW revision\n" "\n" -- cgit v1.2.3 From 8d52950ede71561d1f266833f06e747b7d6e0768 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 19 Oct 2014 20:18:47 +0200 Subject: main: Use snprintf instead sprintf and initialize buffer to NULL term --- src/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 041299d..ff99dc3 100644 --- a/src/main.c +++ b/src/main.c @@ -1210,7 +1210,8 @@ int main(int argc, char **argv) { if ( ! image_tmp_name(i) ) continue; - sprintf(buf, "%hd", dev->detected_hwrev); + buf[0] = 0; + snprintf(buf, sizeof(buf), "%hd", dev->detected_hwrev); switch ( i ) { case IMAGE_2ND: @@ -1312,14 +1313,16 @@ int main(int argc, char **argv) { break; } - sprintf(buf, "%s-%s:%hd_%s", image_type_to_string(i), device_to_string(dev->detected_device), dev->detected_hwrev, ptr); + buf[0] = 0; + snprintf(buf, sizeof(buf), "%s-%s:%hd_%s", image_type_to_string(i), device_to_string(dev->detected_device), dev->detected_hwrev, ptr); printf("Renaming %s image file to %s...\n", image_type_to_string(i), buf); if ( rename(image_tmp_name(i), buf) < 0 ) { ERROR_INFO("Renaming failed"); - sprintf(buf, "%s-%s_%s", image_type_to_string(i), device_to_string(dev->detected_device), ptr); + buf[0] = 0; + snprintf(buf, sizeof(buf), "%s-%s_%s", image_type_to_string(i), device_to_string(dev->detected_device), ptr); printf("Trying to rename %s image file to %s...\n", image_type_to_string(i), buf); if ( rename(image_tmp_name(i), buf) < 0 ) -- cgit v1.2.3 From f3d203c205c9d4f1192f7e989eeeb80187c230fc Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 18:06:22 +0100 Subject: main: Correctly set and free processed images This will fix more memory corruptions (specially at quit). --- src/main.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index ff99dc3..34ebffd 100644 --- a/src/main.c +++ b/src/main.c @@ -663,6 +663,9 @@ int main(int argc, char **argv) { goto clean; } filter_images_by_type(type, &image_first); + /* make sure that fiasco_in has valid images */ + if ( fiasco_in ) + fiasco_in->first = image_first; } /* filter images by device */ @@ -674,11 +677,18 @@ int main(int argc, char **argv) { goto clean; } filter_images_by_device(device, &image_first); + /* make sure that fiasco_in has valid images */ + if ( fiasco_in ) + fiasco_in->first = image_first; } /* filter images by hwrev */ - if ( filter_hwrev ) + if ( filter_hwrev ) { filter_images_by_hwrev(atoi(filter_hwrev_arg), &image_first); + /* make sure that fiasco_in has valid images */ + if ( fiasco_in ) + fiasco_in->first = image_first; + } /* reorder images for flashing (first x-loader, second secondary) */ /* set 2nd and secondary images for cold-flashing */ @@ -742,11 +752,12 @@ int main(int argc, char **argv) { image_ptr = next; } + /* make sure that fiasco_in has valid images */ + if ( fiasco_in ) + fiasco_in->first = image_first; + } - /* make sure that fiasco_in has valid images*/ - if ( fiasco_in ) - fiasco_in->first = image_first; /* identify images */ if ( image_ident ) { @@ -784,8 +795,9 @@ int main(int argc, char **argv) { WARNING("Removing unknown image (specified by %s %s)", image_ptr->image->orig_filename ? "file" : "fiasco", image_ptr->image->orig_filename ? image_ptr->image->orig_filename : "image"); if ( image_ptr == image_first ) image_first = next; - image_list_unlink(image_ptr); - free(image_ptr); + if ( fiasco_in && image_ptr == fiasco_in->first ) + fiasco_in->first = fiasco_in->first->next; + image_list_del(image_ptr); } image_ptr = next; } @@ -985,6 +997,8 @@ int main(int argc, char **argv) { filter_images_by_device(dev->detected_device, &image_first); if ( detected_hwrev ) filter_images_by_hwrev(dev->detected_hwrev, &image_first); + if ( fiasco_in && ( detected_device || detected_hwrev ) ) + fiasco_in->first = image_first; /* set kernel and initfs images for loading */ if ( dev_load ) { @@ -1049,8 +1063,7 @@ int main(int argc, char **argv) { if ( fiasco_in && image_kernel == fiasco_in->first ) fiasco_in->first = fiasco_in->first->next; - image_list_unlink(image_kernel); - free(image_kernel); + image_list_del(image_kernel); image_kernel = NULL; } @@ -1061,11 +1074,10 @@ int main(int argc, char **argv) { if ( image_initfs == image_first ) image_first = image_first->next; - if ( fiasco_in && image_kernel == fiasco_in->first ) + if ( fiasco_in && image_initfs == fiasco_in->first ) fiasco_in->first = fiasco_in->first->next; - image_list_unlink(image_initfs); - free(image_initfs); + image_list_del(image_initfs); image_initfs = NULL; } } @@ -1081,11 +1093,10 @@ int main(int argc, char **argv) { if ( image_ptr == image_first ) image_first = image_first->next; - if ( fiasco_in && image_kernel == fiasco_in->first ) + if ( fiasco_in && image_ptr == fiasco_in->first ) fiasco_in->first = fiasco_in->first->next; - image_list_unlink(image_ptr); - free(image_ptr); + image_list_del(image_ptr); image_ptr = next; } } -- cgit v1.2.3 From 65888448bf184a830d0b2c0938491bdb6e269893 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 18:09:06 +0100 Subject: main: When doing normal flash filter all 2nd images This will fix infinite loop after successfull fiasco image flash --- src/main.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 34ebffd..0552593 100644 --- a/src/main.c +++ b/src/main.c @@ -758,6 +758,23 @@ int main(int argc, char **argv) { } + /* remove 2nd image when doing normal flash */ + if ( dev_flash ) { + image_ptr = image_first; + while ( image_ptr ) { + struct image_list * next = image_ptr->next; + if ( image_ptr->image->type == IMAGE_2ND ) { + if ( image_ptr == image_first ) + image_first = next; + image_list_del(image_ptr); + } + image_ptr = next; + } + + /* make sure that fiasco_in has valid images */ + if ( fiasco_in ) + fiasco_in->first = image_first; + } /* identify images */ if ( image_ident ) { -- cgit v1.2.3