Nikolai Artemiev has uploaded this change for review.

View Change

tree/: Make chip restore data a void ptr instead of uint8_t

Chip restore callbacks currently are used by
- spi25_statusreg.c unlock functions to restore status register 1.
- s25f.c to restore config register 3.

Both of these cases only need to save a single uint8_t value to restore
the original chip state, however storing a void pointer will allow more
powerful chip restore behaviour and enable writeprotect-based unlocking
as flashrom_wp_cfg objects will be able to be saved and restored.

BUG=b:237485865,b:247421511
BRANCH=none
TEST=todo

Change-Id: I311b468a4b0349f4da9584c12b36af6ec2394527
Signed-off-by: Nikolai Artemiev <nartemiev@google.com>
---
M flashrom.c
M include/flash.h
M s25f.c
M spi25_statusreg.c
4 files changed, 51 insertions(+), 10 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/49/70349/1
diff --git a/flashrom.c b/flashrom.c
index 51a6b64..43410ea 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -97,7 +97,7 @@
}

int register_chip_restore(chip_restore_fn_cb_t func,
- struct flashctx *flash, uint8_t status)
+ struct flashctx *flash, void *data)
{
if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
msg_perr("Tried to register more than %i chip restore"
@@ -105,7 +105,7 @@
return 1;
}
flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
- flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
+ flash->chip_restore_fn[flash->chip_restore_fn_count].data = data;
flash->chip_restore_fn_count++;

return 0;
@@ -118,7 +118,7 @@
while (flash->chip_restore_fn_count > 0) {
int i = --flash->chip_restore_fn_count;
rc |= flash->chip_restore_fn[i].func(
- flash, flash->chip_restore_fn[i].status);
+ flash, flash->chip_restore_fn[i].data);
}

return rc;
diff --git a/include/flash.h b/include/flash.h
index 3f78721..1059c28 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -509,7 +509,7 @@
enum decode_range_func decode_range;
};

-typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, uint8_t status);
+typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, void *data);

struct flashrom_flashctx {
struct flashchip *chip;
@@ -542,7 +542,7 @@
int chip_restore_fn_count;
struct chip_restore_func_data {
chip_restore_fn_cb_t func;
- uint8_t status;
+ void *data;
} chip_restore_fn[MAX_CHIP_RESTORE_FUNCTIONS];
/* Progress reporting */
flashrom_progress_callback *progress_callback;
@@ -616,7 +616,7 @@
int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename);
int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it);
void finalize_flash_access(struct flashctx *);
-int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);
+int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, void *data);

/* Something happened that shouldn't happen, but we can go on. */
#define ERROR_FLASHROM_NONFATAL 0x100
diff --git a/s25f.c b/s25f.c
index 605a560..55c59b0 100644
--- a/s25f.c
+++ b/s25f.c
@@ -21,6 +21,7 @@
* TODO: Implement fancy hybrid sector architecture helpers.
*/

+#include <stdlib.h>
#include <string.h>

#include "chipdrivers.h"
@@ -230,10 +231,13 @@
return s25f_poll_status(flash);
}

-static int s25fs_restore_cr3nv(struct flashctx *flash, uint8_t cfg)
+static int s25fs_restore_cr3nv(struct flashctx *flash, void *data)
{
int ret = 0;

+ uint8_t cfg = *(uint8_t *)data;
+ free(data);
+
msg_cdbg("Restoring CR3NV value to 0x%02x\n", cfg);
ret |= s25fs_write_cr(flash, CR3NV_ADDR, cfg);
ret |= s25fs_software_reset(flash);
@@ -285,8 +289,13 @@
msg_cdbg("\n%s: CR3NV updated (0x%02x -> 0x%02x)\n",
__func__, cfg,
s25fs_read_cr(flash, CR3NV_ADDR));
+
/* Restore CR3V when flashrom exits */
- register_chip_restore(s25fs_restore_cr3nv, flash, cfg);
+ uint8_t *data = calloc(sizeof(uint8_t), 1);
+ if (!data)
+ return 1;
+ *data = cfg;
+ register_chip_restore(s25fs_restore_cr3nv, flash, data);
}

cr3nv_checked = 1;
diff --git a/spi25_statusreg.c b/spi25_statusreg.c
index b178b2e..9266664 100644
--- a/spi25_statusreg.c
+++ b/spi25_statusreg.c
@@ -17,6 +17,8 @@
* GNU General Public License for more details.
*/

+#include <stdlib.h>
+
#include "flash.h"
#include "chipdrivers.h"
#include "spi.h"
@@ -252,8 +254,11 @@
return 0;
}

-static int spi_restore_status(struct flashctx *flash, uint8_t status)
+static int spi_restore_status(struct flashctx *flash, void *data)
{
+ uint8_t status = *(uint8_t *)data;
+ free(data);
+
msg_cdbg("restoring chip status (0x%02x)\n", status);
return spi_write_register(flash, STATUS1, status);
}
@@ -293,7 +298,11 @@
}

/* Restore status register content upon exit in finalize_flash_access(). */
- register_chip_restore(spi_restore_status, flash, status);
+ uint8_t *data = calloc(sizeof(uint8_t), 1);
+ if (!data)
+ return 1;
+ *data = status;
+ register_chip_restore(spi_restore_status, flash, data);

msg_cdbg("Some block protection in effect, disabling... ");
if ((status & lock_mask) != 0) {

To view, visit change 70349. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I311b468a4b0349f4da9584c12b36af6ec2394527
Gerrit-Change-Number: 70349
Gerrit-PatchSet: 1
Gerrit-Owner: Nikolai Artemiev <nartemiev@google.com>
Gerrit-MessageType: newchange