2010/10/21 Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
New version. This one should address most review comments.
If anyone feels adventurous, I would love to see logs on Intel/VIA
chipsets with SPI (preferably locked down chipsets or with r1193 reverted).
Tests on boards which need a board enable and where you can lock down
the whole chip or parts of it would be appreciated as well.
Thanks to Richard A. Smith for testing on Dediprog.
If you're going to review this, make sure you keep a stack of bananas
(quickly mobilized carbohydrates for your brain), a bucket of ice (to
prevent brain overheating) and a bottle of aspirin handy. If any code is
unclear, please tell me and I'll try to add comments to improve readability.
This code has been tested. Testing erase (and checking with a separate
readback that erase actually worked) and write (same test with separate
readback) would be highly appreciated. Verbose logs are even more
appreciated.
I think the code is ready for merge if you trust write/erase to never
fail. The error cases still need to be tested. Should we reread the
whole chip if write/erase failed to make sure our current view of the
chip contents is not stale?
This patch makes flashrom use real partial writes. If you write an image
full of 0xff, flashrom will erase and detect that no write is needed. If
you write an image which differs only in some parts from the current
flash contents, flashrom will detect that and not touch unchanged areas.
Fix a long-standing bug in need_erase() for 256 byte granularity as well.
Nice side benefit: Detailed progress printing.
S means skipped
E means erased
W means written
Thanks for Andrew Morgan for testing countless iterations of this patch.
Thanks to David Hendricks for the review.
@@ -846,10 +849,85 @@
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Index: flashrom-partial_write_rolling_erase_write/flashrom.c
===================================================================
--- flashrom-partial_write_rolling_erase_write/flashrom.c (Revision 1215)
+++ flashrom-partial_write_rolling_erase_write/flashrom.c (Arbeitskopie)
@@ -793,6 +793,7 @@
* Check if the buffer @have can be programmed to the content of @want without
* erasing. This is only possible if all chunks of size @gran are either kept
* as-is or changed from an all-ones state to any other state.
+ *
* The following write granularities (enum @gran) are known:
* - 1 bit. Each bit can be cleared individually.
* - 1 byte. A byte can be written once. Further writes to an already written
@@ -803,10 +804,12 @@
* this function.
* - 256 bytes. If less than 256 bytes are written, the contents of the
* unwritten bytes are undefined.
+ * Warning: This function assumes that @have and @want point to naturally
+ * aligned regions.
*
* @have buffer with current content
* @want buffer with desired content
- * @len length of the verified area
+ * @len length of the checked area
* @gran write granularity (enum, not count)
* @return 0 if no erase is needed, 1 otherwise
*/
@@ -838,7 +841,7 @@
continue;
/* have needs to be in erased state. */
for (i = 0; i < limit; i++)
- if (have[i] != 0xff) {
+ if (have[j * 256 + i] != 0xff) {
result = 1;
break;
}
break;+ * @first_start offset of the first byte which needs to be written (passed in
}
break;
+ default:
+ msg_cerr("%s: Unsupported granularity! Please report a bug at "
+ "flashrom@flashrom.org\n", __func__);
}
return result;
}
+/**
+ * Check if the buffer @have needs to be programmed to get the content of @want.
+ * If yes, return 1 and fill in first_start with the start address of the
+ * write operation and first_len with the length of the first to-be-written
+ * chunk. If not, return 0 and leave first_start and first_len undefined.
+ *
+ * Warning: This function assumes that @have and @want point to naturally
+ * aligned regions.
+ *
+ * @have buffer with current content
+ * @want buffer with desired content
+ * @len length of the checked area
+ * @gran write granularity (enum, not count)
+ value is discarded, new value is written when get_next_write
+ returns)
+ * @return length of the first contiguous area which needs to be written
+ * 0 if no write is needed
+ *+ int *first_start, enum write_granularity gran)
+ * FIXME: This function needs a parameter which tells it about coalescing
+ * in relation to the max write length of the programmer and the max write
+ * length of the chip.
+ */
+static int get_next_write(uint8_t *have, uint8_t *want, int len,
+{
+ int need_write = 0;
+ int first_len = 0;
+ int i, limit, stride;
++ stride = 1;
+ switch (gran) {
+ case write_gran_1bit:
+ case write_gran_1byte:
+ break;+ stride = 256;
+ case write_gran_256bytes:
+ break;
+ default:+ /* Claim that no write was needed. A write with unknown
+ msg_cerr("%s: Unsupported granularity! Please report a bug at "
+ "flashrom@flashrom.org\n", __func__);
+ * granularity is too dangerous to try.
+ */
+ return 0;
+ }
+ for (i = 0; i < len / stride; i++) {
+ limit = min(stride, len - i * stride);
+ /* Are 'have' and 'want' identical? */+ if (memcmp(have + i * stride, want + i * stride, limit)) {
+ if (!need_write) {
+ /* First location where have and want differ. */
+ need_write = 1;
+ *first_start = i * stride;
+ }
+ } else {
+ if (need_write) {
+ /* First location where have and want+ first_len = i * stride - *first_start;
+ * do not differ anymore.
+ */
+ break;+ if (need_write && ! first_len)
+ }
+ }
+ }
+ /* Did the loop terminate without setting first_len? */
+ first_len = min(i * stride - *first_start, len);
+
+ return first_len;
+}@@ -1203,7 +1281,8 @@
+
/* This function generates various test patterns useful for testing controller
* and chip communication as well as chip behaviour.
*
return ret;@@ -1271,10 +1350,68 @@
}
-/* This function shares a lot of its structure with erase_flash().
+/* This function shares a lot of its structure with erase_and_write_flash() and
+ * walk_eraseregions().
* Even if an error is found, the function will keep going and check the rest.
*/
static int selfcheck_eraseblocks(struct flashchip *flash)
+ /* get_next_write() sets starthere to a new value after the call. */return ret;
}
+static int erase_and_write_block_helper(struct flashchip *flash,
+ unsigned int start, unsigned int len,
+ uint8_t *oldcontents,
+ uint8_t *newcontents,
+ int (*erasefn) (struct flashchip *flash,
+ unsigned int addr,
+ unsigned int len))
+{
+ int starthere = 0;
+ int lenhere = 0;
+ int ret = 0;
+ int skip = 1;
+ int writecount = 0;
+ enum write_granularity gran = write_gran_256bytes; /* FIXME */
+
+ /* oldcontents and newcontents are opaque to walk_eraseregions, and
+ * need to be adjusted here to keep the impression of proper abstraction
+ */
+ oldcontents += start;
+ newcontents += start;
+ msg_cdbg(":");
+ /* FIXME: Assume 256 byte granularity for now to play it safe. */
+ if (need_erase(oldcontents, newcontents, len, gran)) {
+ msg_cdbg("E");
+ ret = erasefn(flash, start, len);
+ if (ret)
+ return ret;
+ /* Erase was successful. Adjust oldcontents. */
+ memset(oldcontents, 0xff, len);
+ skip = 0;
+ }
+ while ((lenhere = get_next_write(oldcontents + starthere,
+ newcontents + starthere,+ gran))) {
+ len - starthere, &starthere,
@@ -1286,21 +1423,34 @@+ if (!writecount++)
+ msg_cdbg("W");
+ /* Needs the partial write function signature. */
+ ret = flash->write(flash, newcontents + starthere,
+ start + starthere, lenhere);
+ if (ret)
+ return ret;
+ starthere += lenhere;
+ skip = 0;
+ }
+ if (skip)
+ msg_cdbg("S");
+ return ret;
+}
+
static int walk_eraseregions(struct flashchip *flash, int erasefunction,
int (*do_something) (struct flashchip *flash,
unsigned int addr,
- unsigned int len))
+ unsigned int len,
+ uint8_t *param1,
+ uint8_t *param2,
+ int (*erasefn) (
+ struct flashchip *flash,
+ unsigned int addr,
+ unsigned int len)),
+ void *param1, void *param2)
{
int i, j;
unsigned int start = 0;
@@ -1324,12 +1474,14 @@*/
len = eraser.eraseblocks[i].size;
for (j = 0; j < eraser.eraseblocks[i].count; j++) {
- msg_cdbg("0x%06x-0x%06x, ", start,
+ /* Print this for every block except the first one. */
+ if (i || j)
+ msg_cdbg(", ");
+ msg_cdbg("0x%06x-0x%06x", start,
start + len - 1);
- if (do_something(flash, start, len))
+ if (do_something(flash, start, len, param1, param2,
+ eraser.block_erase)) {
+ msg_cdbg("\n");
return 1;
+ }
start += len;
}
}
+ msg_cdbg("\n");
return 0;
}
-int erase_flash(struct flashchip *flash)
+int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
{
int k, ret = 0, found = 0;
+ uint8_t *curcontents;
+ unsigned long size = flash->total_size * 1024;
- msg_cinfo("Erasing flash chip... ");
+ curcontents = (uint8_t *) malloc(size);
+ /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
+ memcpy(curcontents, oldcontents, size);
+
+ msg_cinfo("Erasing and writing flash chip... ");
for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
struct block_eraser eraser = flash->block_erasers[k];
}@@ -1338,7 +1490,7 @@
found = 1;
msg_cdbg("trying... ");
- ret = walk_eraseregions(flash, k, eraser.block_erase);
+ ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, curcontents, newcontents);
msg_cdbg("\n");
/* If everything is OK, don't try another erase function. */
if (!ret)
break;
}
+ /* Free the scratchpad. */
+ free(curcontents);
if (!found) {
msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
return 1;
if (ret) {
msg_cerr("FAILED!\n");
} else {
- msg_cinfo("SUCCESS.\n");
+ msg_cinfo("Done.\n");
}
return ret;
}
@@ -1637,6 +1789,19 @@
programmer_shutdown();@@ -1644,15 +1809,14 @@
return ret;
}
+
+ oldcontents = (uint8_t *) malloc(size);
+ /* Assume worst case: All bits are 0. */
+ memset(oldcontents, 0x00, size);
+ newcontents = (uint8_t *) malloc(size);
+ /* Assume best case: All bits should be 1. */
+ memset(newcontents, 0xff, size);
+ /* Side effect of the assumptions above: Default write action is erase
+ * because newcontents looks like a completely erased chip, and
+ * oldcontents being completely 0x00 means we have to erase everything
+ * before we can write.
+ */
+
if (erase_it) {
/* FIXME: Do we really want the scary warning if erase failed?
* After all, after erase the chip is either blank or partially
* so if the user wanted erase and reboots afterwards, the user+ ret = 1;
* knows very well that booting won't work.
*/
- if (erase_flash(flash)) {
+ if (erase_and_write_flash(flash, oldcontents, newcontents)) {
emergency_help_message();
- programmer_shutdown();
- return 1;
}
+ programmer_shutdown();
+ return ret;
}@@ -1665,8 +1829,6 @@
- newcontents = (uint8_t *) calloc(size, sizeof(char));
-
if (write_it || verify_it) {
if (read_buf_from_file(newcontents, size, filename)) {
programmer_shutdown();
#endif@@ -1686,9 +1848,9 @@
}
- oldcontents = (uint8_t *) calloc(size, sizeof(char));
-
/* Read the whole chip to be able to check whether regions need to be
* erased and to give better diagnostics in case write fails.
* The alternative would be to read only the regions which are to be
// ////////////////////////////////////////////////////////////@@ -1702,26 +1864,6 @@
if (write_it) {
- if (erase_flash(flash)) {
- msg_cerr("Uh oh. Erase failed. Checking if anything "
- "changed.\n");
+ if (erase_and_write_flash(flash, oldcontents, newcontents)) {
+ msg_cerr("Uh oh. Erase/write failed. Checking if "
+ "anything changed.\n");
if (!flash->read(flash, newcontents, 0, size)) {
if (!memcmp(oldcontents, newcontents, size)) {
msg_cinfo("Good. It seems nothing was "
programmer_shutdown();if (verify_it) {
return 1;
}
- msg_cinfo("Writing flash chip... ");
- ret = flash->write(flash, newcontents, 0, size);
- if (ret) {
- msg_cerr("Uh oh. Write failed. Checking if anything "
- "changed.\n");
- if (!flash->read(flash, newcontents, 0, size)) {
- if (!memcmp(oldcontents, newcontents, size)) {
- msg_cinfo("Good. It seems nothing was "
- "changed.\n");
- nonfatal_help_message();
- programmer_shutdown();
- return 1;
- }
- }
- emergency_help_message();
- programmer_shutdown();
- return 1;
- } else {
- msg_cinfo("COMPLETE.\n");
- }
}
--
http://www.hailfinger.org/
_______________________________________________
flashrom mailing list
flashrom@flashrom.org
http://www.flashrom.org/mailman/listinfo/flashrom