Author: stefanct Date: Mon Sep 23 17:32:25 2013 New Revision: 1752 URL: http://flashrom.org/trac/flashrom/changeset/1752
Log: layout: Verify layout entries before building a new image using them.
This fixes a SEGFAULT if a layout entry is included that addresses memory outside the current chip's address range. flashrom will only abort if the offending region(s) is/are included else it will just warn.
It will print warnings for regions with negative or zero-length address ranges too, but it will only abort if they are included with -i/--image to reduce the potential of regressions. This is different to the patch committed to the development branch in r1751.
Also, abort for non-write operations if a layout file is given because there is no layout support for non-write operations yet, and some reports show that users expect it to work at least for -r/--read.
Signed-off-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at Acked-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Modified: branches/0.9.7/cli_classic.c branches/0.9.7/flash.h branches/0.9.7/flashrom.8 branches/0.9.7/flashrom.c branches/0.9.7/layout.c
Modified: branches/0.9.7/cli_classic.c ============================================================================== --- branches/0.9.7/cli_classic.c Mon Sep 23 16:21:06 2013 (r1751) +++ branches/0.9.7/cli_classic.c Mon Sep 23 17:32:25 2013 (r1752) @@ -372,6 +372,12 @@ ret = 1; goto out; } + if (layoutfile != NULL && !write_it) { + msg_gerr("Layout files are currently supported for write operations only.\n"); + ret = 1; + goto out; + } + if (process_include_args()) { ret = 1; goto out;
Modified: branches/0.9.7/flash.h ============================================================================== --- branches/0.9.7/flash.h Mon Sep 23 16:21:06 2013 (r1751) +++ branches/0.9.7/flash.h Mon Sep 23 17:32:25 2013 (r1752) @@ -45,6 +45,14 @@ typedef uintptr_t chipaddr; #define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2))
+/* Types and macros regarding the maximum flash space size supported by generic code. */ +typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */ +typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */ +#define FL_MAX_CHIPADDR_BITS (24) +#define FL_MAX_CHIPADDR ((chipoff_t)(1ULL<<FL_MAX_CHIPADDR_BITS)-1) +#define PRIxCHIPADDR "06"PRIx32 +#define PRIuCHIPSIZE PRIu32 + int register_shutdown(int (*function) (void *data), void *data); void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len); void programmer_unmap_flash_region(void *virt_addr, size_t len); @@ -319,7 +327,8 @@ int register_include_arg(char *name); int process_include_args(void); int read_romlayout(char *name); -int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents); +int normalize_romentries(const struct flashctx *flash); +int build_new_image(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents);
/* spi.c */ struct spi_command {
Modified: branches/0.9.7/flashrom.8 ============================================================================== --- branches/0.9.7/flashrom.8 Mon Sep 23 16:21:06 2013 (r1751) +++ branches/0.9.7/flashrom.8 Mon Sep 23 17:32:25 2013 (r1752) @@ -106,7 +106,7 @@ Read ROM layout from .BR <file> . .sp -flashrom supports ROM layouts. This allows you to flash certain parts of +flashrom supports ROM layouts in write operations only. This allows you to flash certain parts of the flash chip only. A ROM layout file contains multiple lines with the following syntax: .sp
Modified: branches/0.9.7/flashrom.c ============================================================================== --- branches/0.9.7/flashrom.c Mon Sep 23 16:21:06 2013 (r1751) +++ branches/0.9.7/flashrom.c Mon Sep 23 17:32:25 2013 (r1752) @@ -1904,6 +1904,12 @@ goto out_nofree; }
+ if (normalize_romentries(flash)) { + msg_cerr("Requested regions can not be handled. Aborting.\n"); + ret = 1; + goto out_nofree; + } + /* Given the existence of read locks, we want to unlock for read, * erase and write. */ @@ -1983,9 +1989,8 @@ } msg_cinfo("done.\n");
- // This should be moved into each flash part's code to do it - // cleanly. This does the job. - handle_romentries(flash, oldcontents, newcontents); + /* Build a new image taking the given layout into account. */ + build_new_image(flash, oldcontents, newcontents);
// ////////////////////////////////////////////////////////////
Modified: branches/0.9.7/layout.c ============================================================================== --- branches/0.9.7/layout.c Mon Sep 23 16:21:06 2013 (r1751) +++ branches/0.9.7/layout.c Mon Sep 23 17:32:25 2013 (r1752) @@ -30,8 +30,8 @@ #define MAX_ROMLAYOUT 32
typedef struct { - unsigned int start; - unsigned int end; + chipoff_t start; + chipoff_t end; unsigned int included; char name[256]; } romlayout_t; @@ -217,7 +217,32 @@ return best_entry; }
-int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents) +/* Validate and - if needed - normalize layout entries. */ +int normalize_romentries(const struct flashctx *flash) +{ + chipsize_t total_size = flash->chip->total_size * 1024; + int ret = 0; + + int i; + for (i = 0; i < romimages; i++) { + if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) { + msg_gwarn("Warning: Address range of region "%s" exceeds the current chip's " + "address space.\n", rom_entries[i].name); + if (rom_entries[i].included) + ret = 1; + } + if (rom_entries[i].start > rom_entries[i].end) { + msg_gwarn("Warning: Size of the address range of region "%s" is not positive.\n", + rom_entries[i].name); + if (rom_entries[i].included) + ret = 1; + } + } + + return ret; +} + +int build_new_image(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents) { unsigned int start = 0; romlayout_t *entry;