diff --git a/src/mtd.c b/src/mtd.c index 8c183da..54875a5 100644 --- a/src/mtd.c +++ b/src/mtd.c @@ -507,8 +507,9 @@ bool mtd_flash_offset(int num, const char *name, uint64_t size, return known; } -// What the last enum_mtd_info() found the partitions to cover -static uint64_t flash_size; +// What the last enum_mtd_info() found the partitions to cover, and how big +// the chip is at least +static uint64_t flash_size, chip_size; uint64_t mtd_flash_size() { return flash_size; } @@ -593,6 +594,7 @@ void enum_mtd_info(void *ctx, cb_mtd cb) { sizes[kept++] = sizes[k]; } flash_size = mtd_covered_size(kept, offs, has_off, sizes); + chip_size = mtd_chip_size(kept, offs, has_off, sizes); for (int i = 0; i < n; i++) { if (mtds[i].valid && !cb(mtds[i].i, mtds[i].name, &mtds[i].mtd, ctx)) @@ -617,7 +619,7 @@ cJSON *get_mtd_info() { cJSON *j_inner = ctx.json; if (ctx.totalsz) - ADD_PARAM_FMT("size", "%dM", (int)(mtd_flash_size() / 1024 / 1024)); + ADD_PARAM_FMT("size", "%dM", (int)(chip_size / 1024 / 1024)); if (hal_fmc_mode) { const char *fmc_mode = hal_fmc_mode(); diff --git a/src/mtd_alias.c b/src/mtd_alias.c index 2fa9749..70ddac0 100644 --- a/src/mtd_alias.c +++ b/src/mtd_alias.c @@ -60,6 +60,15 @@ uint64_t mtd_covered_size(int n, const uint64_t *off, const bool *has_off, } } +uint64_t mtd_chip_size(int n, const uint64_t *off, const bool *has_off, + const uint64_t *size) { + uint64_t reach = mtd_covered_size(n, off, has_off, size); + for (int i = 0; i < n; i++) + if (has_off[i] && off[i] + size[i] > reach) + reach = off[i] + size[i]; + return reach; +} + // The kernel's memparse(): strtoull with a size suffix static uint64_t memparse(const char *s, const char **end) { char *e; diff --git a/src/mtd_alias.h b/src/mtd_alias.h index c624329..acf5d55 100644 --- a/src/mtd_alias.h +++ b/src/mtd_alias.h @@ -21,6 +21,13 @@ void mtd_mark_aliases(int n, const uint64_t *off, const bool *has_off, uint64_t mtd_covered_size(int n, const uint64_t *off, const bool *has_off, const uint64_t *size); +/* How big the chip is at least: as far as a partition reaches, where offsets + * are known, or what the partitions cover. Flash that no partition holds -- a + * GM8126's boot header at 0, a gap between two -- counts here and not in + * mtd_covered_size(), which is what a backup of the partitions holds. */ +uint64_t mtd_chip_size(int n, const uint64_t *off, const bool *has_off, + const uint64_t *size); + /* * Where a partition starts, for kernels before 4.4: they have no * /sys/class/mtd/mtdN/offset. A partition is found by name and size, and is diff --git a/src/mtd_alias_test.c b/src/mtd_alias_test.c index 2eec3c7..fc5afb6 100644 --- a/src/mtd_alias_test.c +++ b/src/mtd_alias_test.c @@ -99,6 +99,24 @@ int main(void) { CHECK(sz == 0x800000, "partly known %llx", (unsigned long long)sz); CHECK(!mtd_covered_size(0, NULL, NULL, NULL), "no partitions"); + // The chip reaches as far as its last partition: the GM8128 of #305, + // whose header at 0 and a block at 0x66000 no partition holds, is 16 MB + uint64_t gm_off[5] = {0x67000, 0x568000, 0x1000, 0x6000, 0xfa0000}; + uint64_t gm_size[5] = {0x501000, 0xa38000, 0x5000, 0x60000, 0x60000}; + bool gm_has[5] = {true, true, true, true, true}; + sz = mtd_covered_size(5, gm_off, gm_has, gm_size); + CHECK(sz == 0xffe000, "gm covered %llx", (unsigned long long)sz); + sz = mtd_chip_size(5, gm_off, gm_has, gm_size); + CHECK(sz == 0x1000000, "gm chip %llx", (unsigned long long)sz); + sz = mtd_chip_size(4, any_off, any_has, any_size); + CHECK(sz == 0x800000, "chip %llx", (unsigned long long)sz); + // Without offsets it is what the partitions cover + for (int i = 0; i < 5; i++) + gm_has[i] = false; + sz = mtd_chip_size(5, gm_off, gm_has, gm_size); + CHECK(sz == 0xffe000, "gm no offsets %llx", (unsigned long long)sz); + CHECK(!mtd_chip_size(0, NULL, NULL, NULL), "no partitions, no chip"); + // One partition alone is never an alias. uint64_t one = 0x1000000, zero = 0; bool f = false, a;