summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNico Huber <nico.h@gmx.de>2019-06-18 23:39:56 +0200
committerNico Huber <nico.h@gmx.de>2019-06-21 11:54:19 +0000
commitd8b2e808cd46986f945ba9cf3b90c70fe58de9c6 (patch)
tree4094be4996c4ae32a78e0e13c558ee78bcdd85dc
parent0373ce31fe5b11dcf23b27fbc221ba019a1cf7f1 (diff)
downloadflashrom-d8b2e808cd46986f945ba9cf3b90c70fe58de9c6.tar.gz
flashrom-d8b2e808cd46986f945ba9cf3b90c70fe58de9c6.tar.bz2
flashrom-d8b2e808cd46986f945ba9cf3b90c70fe58de9c6.zip
spi: Move 16MiB partitioning up into spi_chip_read()
We enforced a 16MiB limit in spi_read_chunked() for multi-die flash chips that can't be fully read at once. The same limit can be useful for dediprog programmers. So move it into a more generic place. Change-Id: Iab1fd5b2ea550b4b3ef3e8402e0b6ca218485a51 Signed-off-by: Nico Huber <nico.h@gmx.de> Reviewed-on: https://review.coreboot.org/c/flashrom/+/33613 Reviewed-by: Ryan O'Leary Reviewed-by: ron minnich <rminnich@gmail.com> Reviewed-by: David Hendricks <david.hendricks@gmail.com>
-rw-r--r--flash.h6
-rw-r--r--spi.c14
-rw-r--r--spi25.c41
3 files changed, 28 insertions, 33 deletions
diff --git a/flash.h b/flash.h
index 5bbfa0aa5..b60a98097 100644
--- a/flash.h
+++ b/flash.h
@@ -37,6 +37,12 @@
#include "libflashrom.h"
#include "layout.h"
+#define KiB (1024)
+#define MiB (1024 * KiB)
+
+/* Assumes `n` and `a` are at most 64-bit wide (to avoid typeof() operator). */
+#define ALIGN_DOWN(n, a) ((n) & ~((uint64_t)(a) - 1))
+
#define ERROR_PTR ((void*)-1)
/* Error codes */
diff --git a/spi.c b/spi.c
index a4dba1973..489baf306 100644
--- a/spi.c
+++ b/spi.c
@@ -99,7 +99,19 @@ int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned i
int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
unsigned int len)
{
- return flash->mst->spi.read(flash, buf, start, len);
+ int ret;
+ size_t to_read;
+ for (; len; len -= to_read, buf += to_read, start += to_read) {
+ /* Do not cross 16MiB boundaries in a single transfer.
+ This helps with
+ o multi-die 4-byte-addressing chips,
+ o dediprog that has a protocol limit of 32MiB-512B. */
+ to_read = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len);
+ ret = flash->mst->spi.read(flash, buf, start, to_read);
+ if (ret)
+ return ret;
+ }
+ return 0;
}
/*
diff --git a/spi25.c b/spi25.c
index fd87dc9a5..cca2a0476 100644
--- a/spi25.c
+++ b/spi25.c
@@ -657,43 +657,20 @@ int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
/*
* Read a part of the flash chip.
- * FIXME: Use the chunk code from Michael Karcher instead.
- * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
+ * Data is read in chunks with a maximum size of chunksize.
*/
int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
unsigned int len, unsigned int chunksize)
{
- int rc = 0;
- unsigned int i, j, starthere, lenhere, toread;
- /* Limit for multi-die 4-byte-addressing chips. */
- unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
-
- /* Warning: This loop has a very unusual condition and body.
- * The loop needs to go through each area with at least one affected
- * byte. The lowest area number is (start / area_size) since that
- * division rounds down. The highest area number we want is the area
- * where the last byte of the range lives. That last byte has the
- * address (start + len - 1), thus the highest area number is
- * (start + len - 1) / area_size. Since we want to include that last
- * area as well, the loop condition uses <=.
- */
- for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
- /* Byte position of the first byte in the range in this area. */
- /* starthere is an offset to the base address of the chip. */
- starthere = max(start, i * area_size);
- /* Length of bytes in the range in this area. */
- lenhere = min(start + len, (i + 1) * area_size) - starthere;
- for (j = 0; j < lenhere; j += chunksize) {
- toread = min(chunksize, lenhere - j);
- rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
- if (rc)
- break;
- }
- if (rc)
- break;
+ int ret;
+ size_t to_read;
+ for (; len; len -= to_read, buf += to_read, start += to_read) {
+ to_read = min(chunksize, len);
+ ret = spi_nbyte_read(flash, start, buf, to_read);
+ if (ret)
+ return ret;
}
-
- return rc;
+ return 0;
}
/*