flashrom.c: Introduce gran_to_bytes()
Change-Id: Ib41f9581060350f41620e9e377b9bafa58677882
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72545
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/flashrom.c b/flashrom.c
index d65cf9f..4258018 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -401,6 +401,23 @@
return ret;
}
+static size_t gran_to_bytes(const enum write_granularity gran)
+{
+ switch (gran) {
+ case write_gran_1bit: return 1;
+ case write_gran_1byte: return 1;
+ case write_gran_1byte_implicit_erase: return 1;
+ case write_gran_128bytes: return 128;
+ case write_gran_256bytes: return 256;
+ case write_gran_264bytes: return 264;
+ case write_gran_512bytes: return 512;
+ case write_gran_528bytes: return 528;
+ case write_gran_1024bytes: return 1024;
+ case write_gran_1056bytes: return 1056;
+ default: return 0;
+ }
+}
+
/* Helper function for need_erase() that focuses on granularities of gran bytes. */
static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
unsigned int gran, const uint8_t erased_value)
@@ -436,54 +453,34 @@
static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
enum write_granularity gran, const uint8_t erased_value)
{
- int result = 0;
unsigned int i;
+ size_t stride;
switch (gran) {
case write_gran_1bit:
- for (i = 0; i < len; i++)
- if ((have[i] & want[i]) != want[i]) {
- result = 1;
- break;
- }
- break;
+ for (i = 0; i < len; i++) {
+ if ((have[i] & want[i]) != want[i])
+ return 1;
+ }
+ return 0;
case write_gran_1byte:
- for (i = 0; i < len; i++)
- if ((have[i] != want[i]) && (have[i] != erased_value)) {
- result = 1;
- break;
- }
- break;
- case write_gran_128bytes:
- result = need_erase_gran_bytes(have, want, len, 128, erased_value);
- break;
- case write_gran_256bytes:
- result = need_erase_gran_bytes(have, want, len, 256, erased_value);
- break;
- case write_gran_264bytes:
- result = need_erase_gran_bytes(have, want, len, 264, erased_value);
- break;
- case write_gran_512bytes:
- result = need_erase_gran_bytes(have, want, len, 512, erased_value);
- break;
- case write_gran_528bytes:
- result = need_erase_gran_bytes(have, want, len, 528, erased_value);
- break;
- case write_gran_1024bytes:
- result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
- break;
- case write_gran_1056bytes:
- result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
- break;
+ for (i = 0; i < len; i++) {
+ if ((have[i] != want[i]) && (have[i] != erased_value))
+ return 1;
+ }
+ return 0;
case write_gran_1byte_implicit_erase:
/* Do not erase, handle content changes from anything->0xff by writing 0xff. */
- result = 0;
- break;
+ return 0;
default:
+ stride = gran_to_bytes(gran);
+ if (stride) {
+ return need_erase_gran_bytes(have, want, len, stride, erased_value);
+ }
msg_cerr("%s: Unsupported granularity! Please report a bug at "
"flashrom-stable@flashrom.org\n", __func__);
+ return 0;
}
- return result;
}
/**
@@ -515,36 +512,10 @@
{
bool need_write = false;
unsigned int rel_start = 0, first_len = 0;
- unsigned int i, limit, stride;
+ unsigned int i, limit;
- switch (gran) {
- case write_gran_1bit:
- case write_gran_1byte:
- case write_gran_1byte_implicit_erase:
- stride = 1;
- break;
- case write_gran_128bytes:
- stride = 128;
- break;
- case write_gran_256bytes:
- stride = 256;
- break;
- case write_gran_264bytes:
- stride = 264;
- break;
- case write_gran_512bytes:
- stride = 512;
- break;
- case write_gran_528bytes:
- stride = 528;
- break;
- case write_gran_1024bytes:
- stride = 1024;
- break;
- case write_gran_1056bytes:
- stride = 1056;
- break;
- default:
+ const size_t stride = gran_to_bytes(gran);
+ if (!stride) {
msg_cerr("%s: Unsupported granularity! Please report a bug at "
"flashrom-stable@flashrom.org\n", __func__);
/* Claim that no write was needed. A write with unknown