summaryrefslogtreecommitdiff
path: root/host/lib/flashrom_drv.c
diff options
context:
space:
mode:
authorNikolai Artemiev <nartemiev@google.com>2023-02-21 13:52:32 +1100
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-16 02:40:04 +0000
commit4976c1a60ca660d530e6dcaeb6dbd6fe4403fed7 (patch)
treef3dc4223f38823b051e8ca6c29fcf9c6a142f126 /host/lib/flashrom_drv.c
parente29eac41dce1a5e423d739fc47abfd2b2bda22df (diff)
downloadvboot-4976c1a60ca660d530e6dcaeb6dbd6fe4403fed7.tar.gz
Add a new subcommand for getting/setting flash properties such as the flash size and writeprotect configuration. The operations provided by `futility flash` require less information from the user and are less error prone than the equivalents provided by `flashrom`. For example, --wp-enable automatically choses the protection range based on the firmware image and --wp-status gives a warning if the protection range does not match the RO firmware region. BUG=b:268574030 BRANCH=none TEST=`futility flash --{flash-size,wp-enable,wp-disable,wp-status}` Co-authored-by: Edward O'Callaghan <quasisec@google.com> Signed-off-by: Edward O'Callaghan <quasisec@google.com> Signed-off-by: Nikolai Artemiev <nartemiev@google.com> Change-Id: I36d7468616a5bcdf3c4542d48652bd24c3377a61 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/4279661 Reviewed-by: Edward O'Callaghan <quasisec@chromium.org> Commit-Queue: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'host/lib/flashrom_drv.c')
-rw-r--r--host/lib/flashrom_drv.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/host/lib/flashrom_drv.c b/host/lib/flashrom_drv.c
index 09e7f468..6eb0de8d 100644
--- a/host/lib/flashrom_drv.c
+++ b/host/lib/flashrom_drv.c
@@ -311,3 +311,95 @@ err_init:
return ret;
}
+
+int flashrom_set_wp(const char *prog_with_params, bool wp_mode,
+ uint32_t wp_start, uint32_t wp_len, int verbosity)
+{
+ int ret = 1;
+
+ g_verbose_screen = (verbosity == -1) ? FLASHROM_MSG_INFO : verbosity;
+
+ struct flashrom_programmer *prog = NULL;
+ struct flashrom_flashctx *flashctx = NULL;
+
+ struct flashrom_wp_cfg *cfg = NULL;
+
+ char *programmer, *params;
+ char *tmp = flashrom_extract_params(prog_with_params, &programmer,
+ &params);
+
+ flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
+
+ if (flashrom_init(1)
+ || flashrom_programmer_init(&prog, programmer, params))
+ goto err_init;
+
+ if (flashrom_flash_probe(&flashctx, prog, NULL))
+ goto err_probe;
+
+ if (flashrom_wp_cfg_new(&cfg) != FLASHROM_WP_OK)
+ goto err_cleanup;
+
+ enum flashrom_wp_mode mode = wp_mode ?
+ FLASHROM_WP_MODE_HARDWARE : FLASHROM_WP_MODE_DISABLED;
+ flashrom_wp_set_mode(cfg, mode);
+ flashrom_wp_set_range(cfg, wp_start, wp_len);
+
+ if (flashrom_wp_write_cfg(flashctx, cfg) != FLASHROM_WP_OK)
+ goto err_write_cfg;
+
+ ret = 0;
+
+err_write_cfg:
+ flashrom_wp_cfg_release(cfg);
+
+err_cleanup:
+ flashrom_flash_release(flashctx);
+
+err_probe:
+ if (flashrom_programmer_shutdown(prog))
+ ret = 1;
+
+err_init:
+ free(tmp);
+
+ return ret;
+}
+
+int flashrom_get_size(const char *prog_with_params,
+ uint32_t *flash_len, int verbosity)
+{
+ int r = 0;
+
+ g_verbose_screen = (verbosity == -1) ? FLASHROM_MSG_INFO : verbosity;
+
+ char *programmer, *params;
+ char *tmp = flashrom_extract_params(prog_with_params,
+ &programmer, &params);
+
+ struct flashrom_programmer *prog = NULL;
+ struct flashrom_flashctx *flashctx = NULL;
+
+ flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
+
+ if (flashrom_init(1) ||
+ flashrom_programmer_init(&prog, programmer, params)) {
+ r = -1;
+ goto err_init;
+ }
+ if (flashrom_flash_probe(&flashctx, prog, NULL)) {
+ r = -1;
+ goto err_probe;
+ }
+
+ *flash_len = flashrom_flash_getsize(flashctx);
+
+ flashrom_flash_release(flashctx);
+
+err_probe:
+ r |= flashrom_programmer_shutdown(prog);
+
+err_init:
+ free(tmp);
+ return r;
+}