Chip locking has three actions you can do with it:
- Print/read the current locking status
- Lock the chip
- Unlock the chip.
Currently, the code usually does lock printing inside the probe
function, and unlocking somewhere in the erase or write function. Only
very few chips reactivate the lock after write/erase. Since many chips
have identical probe/write/erase functions, but totally different
locking, many such functions have been duplicated needlessly.
With this patch, it is possible to call the chip-specific locking
functions from probe/write/erase functions and unify lots of code.
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006(a)gmx.net>
Index: flashrom-lock_refactor/flash.h
===================================================================
--- flashrom-lock_refactor/flash.h (Revision 750)
+++ flashrom-lock_refactor/flash.h (Arbeitskopie)
@@ -175,6 +175,12 @@
CHIP_BUSTYPE_UNKNOWN = CHIP_BUSTYPE_PARALLEL | CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI,
};
+enum lockaction {
+ lock_print,
+ lock_disable,
+ lock_enable,
+};
+
/*
* How many different contiguous runs of erase blocks with one size each do
* we have for a given erase function?
@@ -229,6 +235,7 @@
int (*write) (struct flashchip *flash, uint8_t *buf);
int (*read) (struct flashchip *flash, uint8_t *buf, int start, int len);
+ int (*lock) (struct flashchip *flash, enum lockaction action);
/* Some flash devices have an additional register space. */
chipaddr virtual_memory;
@@ -551,6 +558,7 @@
int spi_send_multicommand(struct spi_command *cmds);
int spi_write_enable(void);
int spi_write_disable(void);
+int spi_lock(struct flashchip *flash, enum lockaction action);
int spi_chip_erase_60(struct flashchip *flash);
int spi_chip_erase_c7(struct flashchip *flash);
int spi_chip_erase_60_c7(struct flashchip *flash);
Index: flashrom-lock_refactor/flashchips.c
===================================================================
--- flashrom-lock_refactor/flashchips.c (Revision 750)
+++ flashrom-lock_refactor/flashchips.c (Arbeitskopie)
@@ -211,6 +211,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -227,6 +228,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -243,6 +245,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -259,6 +262,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -275,6 +279,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -291,6 +296,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -307,6 +313,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -323,6 +330,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -339,6 +347,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -355,6 +364,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -371,6 +381,7 @@
.erase = NULL,
.write = NULL /* Incompatible Page write */,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -387,6 +398,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -403,6 +415,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -419,6 +432,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
/*The AT26DF321 has the same ID as the AT25DF321. */
@@ -436,6 +450,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},*/
{
@@ -452,6 +467,7 @@
.erase = spi_chip_erase_60_c7,
.write = NULL /* Incompatible Page write */,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -468,7 +484,6 @@
.erase = erase_chip_jedec,
.write = write_jedec,
.read = read_memmapped,
-
},
{
@@ -533,6 +548,7 @@
.erase = NULL,
.write = NULL /* Incompatible Page write */,
.read = NULL /* Incompatible read */,
+ .lock = spi_lock,
},
{
@@ -549,6 +565,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -565,6 +582,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -581,6 +599,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -597,6 +616,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -613,6 +633,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -629,6 +650,7 @@
.erase = NULL,
.write = NULL,
.read = NULL /* Incompatible read */,
+ .lock = spi_lock,
},
{
@@ -645,6 +667,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -661,6 +684,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = spi_lock,
},
{
@@ -725,6 +749,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -821,6 +846,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -837,6 +863,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -853,6 +880,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -869,6 +897,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -885,6 +914,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -901,6 +931,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -917,6 +948,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -933,6 +965,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -949,6 +982,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -965,6 +999,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -981,6 +1016,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -997,6 +1033,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1013,6 +1050,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1029,6 +1067,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1045,6 +1084,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1061,6 +1101,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1256,6 +1297,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1288,6 +1330,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1323,6 +1366,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1358,6 +1402,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1393,6 +1438,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1428,6 +1474,7 @@
},
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1444,6 +1491,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1460,6 +1508,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1476,6 +1525,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1492,6 +1542,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1508,6 +1559,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1604,6 +1656,7 @@
.erase = spi_chip_erase_d8,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1620,6 +1673,7 @@
.erase = spi_chip_erase_d8,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1636,6 +1690,7 @@
.erase = spi_chip_erase_d8,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1652,6 +1707,7 @@
.erase = spi_chip_erase_d8,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1668,6 +1724,7 @@
.erase = spi_chip_erase_d8,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1684,6 +1741,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1700,6 +1758,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1716,6 +1775,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1732,6 +1792,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1748,6 +1809,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1764,6 +1826,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1876,6 +1939,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1892,6 +1956,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1908,6 +1973,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1924,6 +1990,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1940,6 +2007,7 @@
.erase = spi_chip_erase_60,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1956,6 +2024,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -1972,6 +2041,7 @@
.erase = spi_chip_erase_60_c7,
.write = spi_chip_write_1,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2420,6 +2490,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
/* The ST M25P05 is a bit of a problem. It has the same ID as the
@@ -2441,6 +2512,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1, /* 128 */
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2457,6 +2529,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
/* The ST M25P10 has the same problem as the M25P05. */
@@ -2474,6 +2547,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_1, /* 128 */
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2490,6 +2564,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2506,6 +2581,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2522,6 +2598,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2538,6 +2615,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2554,6 +2632,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2570,6 +2649,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2586,6 +2666,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2602,6 +2683,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2954,6 +3036,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2970,6 +3053,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -2986,6 +3070,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -3002,6 +3087,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -3018,6 +3104,7 @@
.erase = spi_chip_erase_c7,
.write = spi_chip_write_256,
.read = spi_chip_read,
+ .lock = spi_lock,
},
{
@@ -3258,6 +3345,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{
@@ -3274,6 +3362,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{
@@ -3290,6 +3379,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{
@@ -3306,6 +3396,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{
@@ -3322,6 +3413,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{
@@ -3338,6 +3430,7 @@
.erase = NULL,
.write = NULL,
.read = NULL,
+ .lock = NULL,
},
{ NULL }
Index: flashrom-lock_refactor/spi.c
===================================================================
--- flashrom-lock_refactor/spi.c (Revision 750)
+++ flashrom-lock_refactor/spi.c (Arbeitskopie)
@@ -30,8 +30,6 @@
enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
void *spibar = NULL;
-void spi_prettyprint_status_register(struct flashchip *flash);
-
const struct spi_programmer spi_programmer[] = {
{ /* SPI_CONTROLLER_NONE */
.command = NULL,
@@ -268,11 +266,6 @@
printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
if (id1 == flash->manufacture_id && id2 == flash->model_id) {
- /* Print the status register to tell the
- * user about possible write protection.
- */
- spi_prettyprint_status_register(flash);
-
return 1;
}
@@ -327,11 +320,6 @@
printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
if (id1 == flash->manufacture_id && id2 == flash->model_id) {
- /* Print the status register to tell the
- * user about possible write protection.
- */
- spi_prettyprint_status_register(flash);
-
return 1;
}
@@ -363,10 +351,6 @@
if (id2 != flash->model_id)
return 0;
- /* Print the status register to tell the
- * user about possible write protection.
- */
- spi_prettyprint_status_register(flash);
return 1;
}
@@ -490,6 +474,25 @@
}
}
+int spi_lock(struct flashchip *flash, enum lockaction action)
+{
+ switch (action) {
+ case lock_print:
+ spi_prettyprint_status_register(flash);
+ return 0;
+ break;
+ case lock_disable:
+ return spi_disable_blockprotect();
+ break;
+ case lock_enable:
+ fprintf(stderr, "lock enable not supported.\n");
+ return 1;
+ break;
+ default: /* Work around gcc. It can't see we enumerated all values. */
+ return 1;
+ }
+}
+
int spi_chip_erase_60(struct flashchip *flash)
{
int result;
Index: flashrom-lock_refactor/flashrom.c
===================================================================
--- flashrom-lock_refactor/flashrom.c (Revision 750)
+++ flashrom-lock_refactor/flashrom.c (Arbeitskopie)
@@ -448,6 +448,9 @@
printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
flash->vendor, flash->name, flash->total_size,
flashbuses_to_text(flash->bustype), base);
+ /* Print the locking status. */
+ if (flash->lock)
+ flash->lock(flash, lock_print);
return flash;
}
--
Developer quote of the week:
"We are juggling too many chainsaws and flaming arrows and tigers."