Use flashprog_flash_getsize() where possible

Change-Id: I4eab12dc9dcf0c7fdffc5c662081868c6f32e36c
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/493
diff --git a/82802ab.c b/82802ab.c
index 0af5d28..3dc48c0 100644
--- a/82802ab.c
+++ b/82802ab.c
@@ -213,7 +213,7 @@
 	}
 
 	/* Read block lock-bits */
-	for (i = 0; i < flash->chip->total_size * 1024; i+= (64 * 1024)) {
+	for (i = 0; i < flashprog_flash_getsize(flash); i+= (64 * 1024)) {
 		bcfg = chip_readb(flash, bios + i + 2); // read block lock config
 		msg_cdbg("block lock at %06x is %slocked!\n", i, bcfg ? "" : "un");
 		if (bcfg) {
@@ -266,7 +266,7 @@
 	}
 
 	/* Read block lock-bits, 8 * 8 KB + 15 * 64 KB */
-	for (i = 0; i < flash->chip->total_size * 1024;
+	for (i = 0; i < flashprog_flash_getsize(flash);
 	     i += (i >= (64 * 1024) ? 64 * 1024 : 8 * 1024)) {
 		bcfg = chip_readb(flash, bios + i + 2); /* read block lock config */
 		msg_cdbg("block lock at %06x is %slocked!\n", i,
diff --git a/at45db.c b/at45db.c
index 2768ace..f8d9983 100644
--- a/at45db.c
+++ b/at45db.c
@@ -234,7 +234,7 @@
 int spi_read_at45db(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 	if ((addr + len) > total_size) {
 		msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n",
 			 __func__, addr, len, total_size);
@@ -266,7 +266,7 @@
 int spi_read_at45db_e8(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 	if ((addr + len) > total_size) {
 		msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n",
 			 __func__, addr, len, total_size);
@@ -344,7 +344,7 @@
 int spi_erase_at45db_page(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 
 	if ((addr % page_size) != 0 || (blocklen % page_size) != 0) {
 		msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
@@ -364,7 +364,7 @@
 int spi_erase_at45db_block(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 
 	if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check blocks not pages
 		msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
@@ -384,7 +384,7 @@
 int spi_erase_at45db_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 
 	if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check sectors not pages
 		msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
@@ -403,7 +403,7 @@
 
 int spi_erase_at45db_chip(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 
 	if ((addr + blocklen) > total_size) {
 		msg_cerr("%s: tried to erase beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
@@ -421,7 +421,7 @@
 int spi_erase_at45cs_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size * 1024;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 	const struct block_eraser be = flash->chip->block_erasers[0];
 	const unsigned int sec_0a_top = be.eraseblocks[0].size;
 	const unsigned int sec_0b_top = be.eraseblocks[0].size + be.eraseblocks[1].size;
@@ -535,14 +535,14 @@
 int spi_write_at45db(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
 {
 	const unsigned int page_size = flash->chip->page_size;
-	const unsigned int total_size = flash->chip->total_size;
+	const unsigned int total_size = flashprog_flash_getsize(flash);
 
 	if ((start % page_size) != 0 || (len % page_size) != 0) {
 		msg_cerr("%s: cannot write partial pages: start=%u, len=%u\n", __func__, start, len);
 		return 1;
 	}
 
-	if ((start + len) > (total_size * 1024)) {
+	if ((start + len) > total_size) {
 		msg_cerr("%s: tried to write beyond flash boundary: start=%u, len=%u, size=%u\n",
 			 __func__, start, len, total_size);
 		return 1;
diff --git a/flashprog.c b/flashprog.c
index f647f75..f4f0f89 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -391,10 +391,10 @@
 	if (!len)
 		return -1;
 
-	if (start + len > flash->chip->total_size * 1024) {
+	if (start + len > flashprog_flash_getsize(flash)) {
 		msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
-			" total_size 0x%x\n", __func__, start, len,
-			flash->chip->total_size * 1024);
+			" total_size 0x%zx\n", __func__, start, len,
+			flashprog_flash_getsize(flash));
 		return -1;
 	}
 
@@ -1612,7 +1612,7 @@
  */
 int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
 {
-	const size_t flash_size = flashctx->chip->total_size * 1024;
+	const size_t flash_size = flashprog_flash_getsize(flashctx);
 
 	if (flash_size > buffer_len)
 		return 2;
@@ -1655,7 +1655,7 @@
 	}
 
 	/* copy the rest of the chip */
-	const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
+	const chipsize_t copy_len = flashprog_flash_getsize(flashctx) - start;
 	memcpy(newcontents + start, oldcontents + start, copy_len);
 }
 
@@ -1678,7 +1678,7 @@
 int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
                          const void *const refbuffer)
 {
-	const size_t flash_size = flashctx->chip->total_size * 1024;
+	const size_t flash_size = flashprog_flash_getsize(flashctx);
 	const bool verify_all = flashctx->flags.verify_whole_chip;
 	const bool verify = flashctx->flags.verify_after_write;
 	const struct flashprog_layout *const verify_layout =
@@ -1812,7 +1812,7 @@
 int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
 {
 	const struct flashprog_layout *const layout = get_layout(flashctx);
-	const size_t flash_size = flashctx->chip->total_size * 1024;
+	const size_t flash_size = flashprog_flash_getsize(flashctx);
 
 	if (buffer_len != flash_size)
 		return 2;
diff --git a/fmap.c b/fmap.c
index 0e8dd95..e8f5b5b 100644
--- a/fmap.c
+++ b/fmap.c
@@ -189,10 +189,10 @@
 	bool fmap_found = false;
 	bool check_offset_0 = true;
 	struct fmap *fmap;
-	const unsigned int chip_size = flashctx->chip->total_size * 1024;
+	const unsigned int chip_size = flashprog_flash_getsize(flashctx);
 	const int sig_len = strlen(FMAP_SIGNATURE);
 
-	if (rom_offset + len > flashctx->chip->total_size * 1024)
+	if (rom_offset + len > flashprog_flash_getsize(flashctx))
 		return 1;
 
 	if (len < sizeof(*fmap))
diff --git a/ichspi.c b/ichspi.c
index 65e79cf..c12bc1c 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -1327,7 +1327,7 @@
 		return -1;
 	}
 
-	if (addr + len > flash->chip->total_size * 1024) {
+	if (addr + len > flashprog_flash_getsize(flash)) {
 		msg_perr("Request to erase some inaccessible memory address(es)"
 			 " (addr=0x%x, len=%d). "
 			 "Not erasing anything.\n", addr, len);
@@ -1359,7 +1359,7 @@
 	uint16_t hsfc;
 	uint8_t block_len;
 
-	if (addr + len > flash->chip->total_size * 1024) {
+	if (addr + len > flashprog_flash_getsize(flash)) {
 		msg_perr("Request to read from an inaccessible memory address "
 			 "(addr=0x%x, len=%d).\n", addr, len);
 		return -1;
@@ -1400,7 +1400,7 @@
 	uint16_t hsfc;
 	uint8_t block_len;
 
-	if (addr + len > flash->chip->total_size * 1024) {
+	if (addr + len > flashprog_flash_getsize(flash)) {
 		msg_perr("Request to write to an inaccessible memory address "
 			 "(addr=0x%x, len=%d).\n", addr, len);
 		return -1;
diff --git a/it87spi.c b/it87spi.c
index 8c32afd..3ac25b1 100644
--- a/it87spi.c
+++ b/it87spi.c
@@ -390,7 +390,7 @@
 	 * the mainboard does not use IT87 SPI translation. This should be done
 	 * via a programmer parameter for the internal programmer.
 	 */
-	if ((flash->chip->total_size * 1024 > it87spi_max_mmapped)) {
+	if ((flashprog_flash_getsize(flash) > it87spi_max_mmapped)) {
 		default_spi_read(flash, buf, start, len);
 	} else {
 		unsigned char *const bios = it87spi_mmapped_flash +
diff --git a/jedec.c b/jedec.c
index 4ff1040..1cb540a 100644
--- a/jedec.c
+++ b/jedec.c
@@ -646,7 +646,7 @@
 	unsigned int mask;
 
 	mask = getaddrmask(flash);
-	if ((addr != 0) || (blocksize != flash->chip->total_size * 1024)) {
+	if ((addr != 0) || (blocksize != flashprog_flash_getsize(flash))) {
 		msg_cerr("%s called with incorrect arguments\n",
 			__func__);
 		return -1;
@@ -732,7 +732,7 @@
 
 static int printlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
 {
-	const unsigned int elems = flash->chip->total_size * 1024 / block_size;
+	const unsigned int elems = flashprog_flash_getsize(flash) / block_size;
 	struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
 	return regspace2_walk_unlockblocks(flash, blocks, &printlock_regspace2_block);
 }
@@ -830,7 +830,7 @@
 
 static int unlock_regspace2_uniform(struct flashctx *flash, unsigned long block_size)
 {
-	const unsigned int elems = flash->chip->total_size * 1024 / block_size;
+	const unsigned int elems = flashprog_flash_getsize(flash) / block_size;
 	struct unlockblock blocks[2] = {{.size = block_size, .count = elems}};
 	return regspace2_walk_unlockblocks(flash, blocks, &unlock_regspace2_block_generic);
 }
diff --git a/layout.c b/layout.c
index 26b6273..56c4d1b 100644
--- a/layout.c
+++ b/layout.c
@@ -215,7 +215,7 @@
 int layout_sanity_checks(const struct flashprog_flashctx *const flash, const bool write_it)
 {
 	const struct flashprog_layout *const layout = get_layout(flash);
-	const chipsize_t total_size = flash->chip->total_size * 1024;
+	const chipsize_t total_size = flashprog_flash_getsize(flash);
 	const size_t gran = gran_to_bytes(flash->chip->gran);
 	int ret = 0;
 
diff --git a/libflashprog.c b/libflashprog.c
index 212293f..4f258b5 100644
--- a/libflashprog.c
+++ b/libflashprog.c
@@ -308,7 +308,7 @@
 	/* Fill default layout covering the whole chip. */
 	if (flashprog_layout_new(&flash->default_layout) ||
 	    flashprog_layout_add_region(flash->default_layout,
-			0, flash->chip->total_size * 1024 - 1, "complete flash") ||
+			0, flashprog_flash_getsize(flash) - 1, "complete flash") ||
 	    flashprog_layout_include_region(flash->default_layout, "complete flash")) {
 		flashprog_flash_release(flash);
 		return 1;
diff --git a/memory_bus.c b/memory_bus.c
index edc48e6..7219171 100644
--- a/memory_bus.c
+++ b/memory_bus.c
@@ -69,7 +69,7 @@
 	flash->virtual_memory = (chipaddr)ERROR_PTR;
 	flash->virtual_registers = (chipaddr)ERROR_PTR;
 
-	const chipsize_t size = flash->chip->total_size * 1024;
+	const chipsize_t size = flashprog_flash_getsize(flash);
 	void *const addr = programmer_map_flash_data(par, size, flash->chip->name);
 	if (addr == ERROR_PTR)
 		return 1;
@@ -92,7 +92,7 @@
 	 * but it might be somewhere completely different on some chips and programmers,
 	 * or not mappable at all. Ignore these problems for now and always report success.
 	 */
-	const chipsize_t size = flash->chip->total_size * 1024;
+	const chipsize_t size = flashprog_flash_getsize(flash);
 	const uintptr_t base = 0xffffffff - size - 0x400000 + 1;
 	void *const addr = programmer_map_flash_region(par, "flash chip registers", base, size);
 	if (addr == ERROR_PTR) {
diff --git a/nicintel_eeprom.c b/nicintel_eeprom.c
index 207782c..e5df1f8 100644
--- a/nicintel_eeprom.c
+++ b/nicintel_eeprom.c
@@ -107,7 +107,7 @@
 {
 	/* Emulated eeprom has a fixed size of 4 KB */
 	flash->chip->total_size = 4;
-	flash->chip->page_size = flash->chip->total_size * 1024;
+	flash->chip->page_size = flashprog_flash_getsize(flash);
 	flash->chip->tested = TEST_OK_PREW;
 	flash->chip->gran = write_gran_1byte_implicit_erase;
 	flash->chip->block_erasers->eraseblocks[0].size = flash->chip->page_size;
@@ -140,7 +140,7 @@
 	flash->chip->tested = TEST_OK_PREW;
 	flash->chip->gran = write_gran_1byte_implicit_erase;
 	flash->chip->block_erasers->eraseblocks[0].size = (EE_PAGE_MASK + 1);
-	flash->chip->block_erasers->eraseblocks[0].count = (flash->chip->total_size * 1024) / (EE_PAGE_MASK + 1);
+	flash->chip->block_erasers->eraseblocks[0].count = (flashprog_flash_getsize(flash)) / (EE_PAGE_MASK + 1);
 
 	return 1;
 }
diff --git a/spi25.c b/spi25.c
index a628770..e5a9ddb 100644
--- a/spi25.c
+++ b/spi25.c
@@ -563,7 +563,7 @@
 int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
 		       unsigned int blocklen)
 {
-	if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
+	if ((addr != 0) || (blocklen != flashprog_flash_getsize(flash))) {
 		msg_cerr("%s called with incorrect arguments\n",
 			__func__);
 		return -1;
@@ -573,7 +573,7 @@
 
 int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
 {
-	if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
+	if ((addr != 0) || (blocklen != flashprog_flash_getsize(flash))) {
 		msg_cerr("%s called with incorrect arguments\n",
 			__func__);
 		return -1;
@@ -584,7 +584,7 @@
 int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
 		       unsigned int blocklen)
 {
-	if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
+	if ((addr != 0) || (blocklen != flashprog_flash_getsize(flash))) {
 		msg_cerr("%s called with incorrect arguments\n",
 			__func__);
 		return -1;
diff --git a/sst28sf040.c b/sst28sf040.c
index 6c22591..ab57835 100644
--- a/sst28sf040.c
+++ b/sst28sf040.c
@@ -115,7 +115,7 @@
 int erase_chip_28sf040(struct flashctx *flash, unsigned int addr,
 		       unsigned int blocklen)
 {
-	if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
+	if ((addr != 0) || (blocklen != flashprog_flash_getsize(flash))) {
 		msg_cerr("%s called with incorrect arguments\n",
 			__func__);
 		return -1;
diff --git a/sst_fwhub.c b/sst_fwhub.c
index 3e53080..cd1465d 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -69,7 +69,7 @@
 {
 	unsigned int i;
 
-	for (i = 0; i < flash->chip->total_size * 1024; i += flash->chip->page_size)
+	for (i = 0; i < flashprog_flash_getsize(flash); i += flash->chip->page_size)
 		check_sst_fwhub_block_lock(flash, i);
 
 	return 0;
@@ -80,7 +80,7 @@
 	unsigned int i;
 	int ret = 0;
 
-	for (i = 0; i < flash->chip->total_size * 1024; i += flash->chip->page_size)
+	for (i = 0; i < flashprog_flash_getsize(flash); i += flash->chip->page_size)
 	{
 		if (clear_sst_fwhub_block_lock(flash, i))
 		{