I think this is needed for SPI Flash that has a page size of 512.
I have also changed the loop condition in a few places. If the length is not a multiple of stride we need to round up the result of the divide. (Either we need to round up the divide or we should replace the lines "limit = min(stride, len - i * stride);" with "limit = stride;". Rounding up the result of the divide seems to be the correct thing to do.).
Signed-off-by: Mark Marshall mark.marshall@omicron.at --- flash.h | 2 ++ flashrom.c | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/flash.h b/flash.h index 0181798..876fdfd 100644 --- a/flash.h +++ b/flash.h @@ -66,11 +66,13 @@ enum chipbustype { * - 128 bytes: If less than 128 bytes are written, the rest will be erased. Each write to a 128-byte region * will trigger an automatic erase before anything is written. Very uncommon behaviour. * - 256 bytes: If less than 256 bytes are written, the contents of the unwritten bytes are undefined. + * - 512 bytes: If less than 512 bytes are written, the contents of the unwritten bytes are undefined. */ enum write_granularity { write_gran_256bytes = 0, /* We assume 256 byte granularity by default. */ write_gran_1bit, write_gran_1byte, + write_gran_512bytes, };
/* diff --git a/flashrom.c b/flashrom.c index b682923..8413a4b 100644 --- a/flashrom.c +++ b/flashrom.c @@ -706,6 +706,13 @@ int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum { int result = 0; unsigned int i, j, limit; + static uint8_t all_ffs[512]; + static int all_ffs_init = 0; + + if (!all_ffs_init) { + memset(all_ffs, 0xff, sizeof(all_ffs)); + all_ffs_init = 1; + }
switch (gran) { case write_gran_1bit: @@ -723,21 +730,31 @@ int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum } break; case write_gran_256bytes: - for (j = 0; j < len / 256; j++) { + for (j = 0; j < (len + 255) / 256; j++) { limit = min (256, len - j * 256); /* Are 'have' and 'want' identical? */ if (!memcmp(have + j * 256, want + j * 256, limit)) continue; - /* have needs to be in erased state. */ - for (i = 0; i < limit; i++) - if (have[j * 256 + i] != 0xff) { - result = 1; - break; - } - if (result) + /* 'have' needs to be in erased state. */ + if (memcmp(have + j * 256, all_ffs, limit)) { + result = 1; break; + } } break; + case write_gran_512bytes: + for (j = 0; j < (len + 511) / 512; j++) { + limit = min (512, len - j * 512); + /* Are 'have' and 'want' identical? */ + if (!memcmp(have + j * 512, want + j * 512, limit)) + continue; + /* 'have' needs to be in erased state. */ + if (memcmp(have + j * 512, all_ffs, limit)) { + result = 1; + break; + } + } + break; default: msg_cerr("%s: Unsupported granularity! Please report a bug at " "flashrom@flashrom.org\n", __func__); @@ -784,6 +801,9 @@ static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, uns case write_gran_256bytes: stride = 256; break; + case write_gran_512bytes: + stride = 512; + break; default: msg_cerr("%s: Unsupported granularity! Please report a bug at " "flashrom@flashrom.org\n", __func__); @@ -792,7 +812,7 @@ static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, uns */ return 0; } - for (i = 0; i < len / stride; i++) { + for (i = 0; i < (len + stride - 1) / stride; i++) { limit = min(stride, len - i * stride); /* Are 'have' and 'want' identical? */ if (memcmp(have + i * stride, want + i * stride, limit)) {