Nikolai Artemiev has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/58483 )
Change subject: [RFC] writeprotect: implement wp_get_mode() and wp_set_mode() ......................................................................
[RFC] writeprotect: implement wp_get_mode() and wp_set_mode()
Change-Id: I7b68e940f0e1359281806c98e1da119b4caf8405 Signed-off-by: Nikolai Artemiev nartemiev@google.com --- M writeprotect.c 1 file changed, 77 insertions(+), 4 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/83/58483/1
diff --git a/writeprotect.c b/writeprotect.c index ea10047..e6e8df2 100644 --- a/writeprotect.c +++ b/writeprotect.c @@ -310,16 +310,89 @@ return ret; }
+static int decode_wp_mode(struct wp_chip_state *wpst, enum wp_mode *mode) +{ + enum wp_mode mode_table[] = { + WP_MODE_DISABLED, + WP_MODE_HARDWARE, + WP_MODE_POWER_CYCLE, + WP_MODE_PERMANENT, + }; + + uint8_t srp = 0; + for (size_t i = 0; i < wpst->srp_bit_count; i++) + srp |= wpst->srp[i] << i; + + if (srp > 3) + return 1; + + *mode = mode_table[srp]; + return 0; +} + +static int encode_wp_mode(enum wp_mode mode, struct wp_chip_state *wpst) +{ + uint8_t srp; + switch (mode) { + case WP_MODE_DISABLED: + srp = 0; + break; + case WP_MODE_HARDWARE: + srp = 1; + break; + case WP_MODE_POWER_CYCLE: + srp = 2; + break; + case WP_MODE_PERMANENT: + srp = 3; + break; + default: + msg_gerr("Unknown mode!\n"); + return 1; + }; + + /* Set SRP bits in wpst structure. */ + for (size_t i = 0; i < wpst->srp_bit_count; i++) { + wpst->srp[i] = srp & 1; + srp >>= 1; + } + + /* Left over bits => chip does not have enough SRP bits to select mode. */ + if (srp != 0) { + msg_gerr("The chip does not support the requested " + "protection mode.\n"); + return 1; + } + + return 0; +} + + int wp_get_mode(struct flashctx *flash, enum wp_mode *mode) { - /* TODO */ - return 1; + struct wp_chip_state wpst; + if (read_wp_chip_state(flash, &wpst)) + return 1; + + return decode_wp_mode(&wpst, mode); }
int wp_set_mode(struct flashctx *flash, enum wp_mode mode) { - /* TODO */ - return 1; + struct wp_chip_state wpst; + if (read_wp_chip_state(flash, &wpst)) + return 1; + + if (encode_wp_mode(mode, &wpst)) { + return 1; + } + + if (write_wp_chip_state(flash, &wpst)) { + return 1; + } + + msg_ginfo("Sucessfully set the requested protection mode.\n"); + return 0; }
static void print_range(struct wp_range *range)