Edward O'Callaghan submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Edward O'Callaghan: Looks good to me, approved
flashrom.c: implement chip restore callback registration

Allows drivers to register a callback function to reset the
chip state once programming has finished. This is used by
the s25f driver added in a later patch, which needs to change
the chip's sector layout to be able to write to the entire flash.

Adapted from cros flashrom at
`9c4c9a56b6a0370b383df9c75d71b3bd469e672d`.

BUG=b:153800073
BRANCH=none
TEST=builds

Change-Id: I2a522dc1fd3952793fbcad70afc6dd43850fbbc5
Signed-off-by: Nikolai Artemiev <nartemiev@google.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/47276
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
M flash.h
M flashrom.c
2 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/flash.h b/flash.h
index a1e7669..883b6f4 100644
--- a/flash.h
+++ b/flash.h
@@ -103,6 +103,8 @@
*/
#define NUM_ERASEFUNCTIONS 8

+#define MAX_CHIP_RESTORE_FUNCTIONS 4
+
/* Feature bits used for non-SPI only */
#define FEATURE_REGISTERMAP (1 << 0)
#define FEATURE_LONG_RESET (0 << 4)
@@ -248,6 +250,8 @@
struct wp *wp;
};

+typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, uint8_t status);
+
struct flashrom_flashctx {
struct flashchip *chip;
/* FIXME: The memory mappings should be saved in a more structured way. */
@@ -275,6 +279,12 @@
*/
int address_high_byte;
bool in_4ba_mode;
+
+ int chip_restore_fn_count;
+ struct chip_restore_func_data {
+ chip_restore_fn_cb_t func;
+ uint8_t status;
+ } chip_restore_fn[MAX_CHIP_RESTORE_FUNCTIONS];
};

/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
@@ -350,6 +360,7 @@
int do_erase(struct flashctx *);
int do_write(struct flashctx *, const char *const filename, const char *const referencefile);
int do_verify(struct flashctx *, const char *const filename);
+int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);

/* Something happened that shouldn't happen, but we can go on. */
#define ERROR_NONFATAL 0x100
diff --git a/flashrom.c b/flashrom.c
index 7f5f2a8..c89abad 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -580,6 +580,34 @@
return 0;
}

+int register_chip_restore(chip_restore_fn_cb_t func,
+ struct flashctx *flash, uint8_t status)
+{
+ if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
+ msg_perr("Tried to register more than %i chip restore"
+ " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
+ 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_count++;
+
+ return 0;
+}
+
+static int deregister_chip_restore(struct flashctx *flash)
+{
+ int rc = 0;
+
+ 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);
+ }
+
+ return rc;
+}
+
int programmer_init(enum programmer prog, const char *param)
{
int ret;
@@ -2256,6 +2284,7 @@

flash->address_high_byte = -1;
flash->in_4ba_mode = false;
+ flash->chip_restore_fn_count = 0;

/* Be careful about 4BA chips and broken masters */
if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
@@ -2285,6 +2314,7 @@

void finalize_flash_access(struct flashctx *const flash)
{
+ deregister_chip_restore(flash);
unmap_flash(flash);
}


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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I2a522dc1fd3952793fbcad70afc6dd43850fbbc5
Gerrit-Change-Number: 47276
Gerrit-PatchSet: 12
Gerrit-Owner: Nikolai Artemiev <nartemiev@google.com>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Andrew McRae <amcrae@chromium.org>
Gerrit-CC: Paul Menzel <paulepanter@users.sourceforge.net>
Gerrit-CC: Sam McNally <sammc@google.com>
Gerrit-MessageType: merged