summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Benn <evanbenn@chromium.org>2022-12-01 15:05:00 +1100
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-01-06 21:42:23 +0000
commitceaff1c0c3b16fb27e3ce14982930813aec9f38d (patch)
tree5add4ffef063d8976275e98dc07b161687ee713f
parent42b3ebdd0233fe23e3a8333af1ce6731d71f5f6b (diff)
downloadvboot-ceaff1c0c3b16fb27e3ce14982930813aec9f38d.tar.gz
futility: Add read command
Add a command that reads AP firmware to a specified file path. BUG=b:260531154 BRANCH=None TEST=FEATURES=test emerge-grunt vboot_reference TEST=futility read /tmp/bios TEST=futility read /tmp/bios -p ec TEST=env SERVOD_NAME=grunt futility read /tmp/bios --servo Change-Id: I82fe0381b6f61ca4d67a9f5c27353e18ed4abe39 Signed-off-by: Evan Benn <evanbenn@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/4075310 Reviewed-by: Edward O'Callaghan <quasisec@chromium.org> Reviewed-by: Yu-Ping Wu <yupingso@chromium.org> Commit-Queue: Edward O'Callaghan <quasisec@chromium.org>
-rw-r--r--Makefile1
-rw-r--r--futility/cmd_read.c134
-rwxr-xr-xtests/futility/run_test_scripts.sh1
-rwxr-xr-xtests/futility/test_read.sh21
4 files changed, 157 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 8214d790..0f58909f 100644
--- a/Makefile
+++ b/Makefile
@@ -644,6 +644,7 @@ FUTIL_SRCS = \
futility/cmd_gscvd.c \
futility/cmd_load_fmap.c \
futility/cmd_pcr.c \
+ futility/cmd_read.c \
futility/cmd_show.c \
futility/cmd_sign.c \
futility/cmd_update.c \
diff --git a/futility/cmd_read.c b/futility/cmd_read.c
new file mode 100644
index 00000000..e0c83616
--- /dev/null
+++ b/futility/cmd_read.c
@@ -0,0 +1,134 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+
+#include "futility.h"
+#include "updater.h"
+
+#ifdef USE_FLASHROM
+
+/* Command line options */
+static struct option const long_opts[] = {
+ SHARED_FLASH_ARGS_LONGOPTS
+ /* name has_arg *flag val */
+ {"help", 0, NULL, 'h'},
+ {"debug", 0, NULL, 'd'},
+ {"verbose", 0, NULL, 'v'},
+ {NULL, 0, NULL, 0},
+};
+
+static const char *const short_opts = "hdv" SHARED_FLASH_ARGS_SHORTOPTS;
+
+static void print_help(int argc, char *argv[])
+{
+ printf("\n"
+ "Usage: " MYNAME " %s [OPTIONS] FILE\n"
+ "\n"
+ "Reads AP firmware to the FILE\n"
+ "-d, --debug \tPrint debugging messages\n"
+ "-v, --verbose \tPrint verbose messages\n"
+ SHARED_FLASH_ARGS_HELP,
+ argv[0]);
+}
+
+static int do_read(int argc, char *argv[])
+{
+ struct updater_config *cfg;
+ struct updater_config_arguments args = {0};
+ int i, errorcnt = 0, update_needed = 1;
+ const char *prepare_ctrl_name = NULL;
+ char *servo_programmer = NULL;
+ char *output_file_name = NULL;
+
+ cfg = updater_new_config();
+ assert(cfg);
+
+ opterr = 0;
+ while ((i = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) {
+ if (handle_flash_argument(&args, i, optarg))
+ continue;
+ switch (i) {
+ case 'h':
+ print_help(argc, argv);
+ updater_delete_config(cfg);
+ return !!errorcnt;
+ case 'd':
+ debugging_enabled = 1;
+ args.verbosity++;
+ break;
+ case 'v':
+ args.verbosity++;
+ break;
+ case '?':
+ errorcnt++;
+ if (optopt)
+ ERROR("Unrecognized option: -%c\n", optopt);
+ else if (argv[optind - 1])
+ ERROR("Unrecognized option (possibly '%s')\n",
+ argv[optind - 1]);
+ else
+ ERROR("Unrecognized option.\n");
+ break;
+ default:
+ errorcnt++;
+ ERROR("Failed parsing options.\n");
+ }
+ }
+ if (argc - optind < 1) {
+ fprintf(stderr, "\nERROR: missing output filename\n");
+ print_help(argc, argv);
+ return 1;
+ }
+ output_file_name = argv[optind++];
+ if (optind < argc) {
+ errorcnt++;
+ ERROR("Unexpected arguments.\n");
+ }
+
+ if (!errorcnt && args.detect_servo) {
+ servo_programmer = host_detect_servo(&prepare_ctrl_name);
+
+ if (!servo_programmer)
+ errorcnt++;
+ else if (!args.programmer)
+ args.programmer = servo_programmer;
+ }
+
+ if (!errorcnt)
+ errorcnt += updater_setup_config(cfg, &args, &update_needed);
+ if (!errorcnt && update_needed) {
+ prepare_servo_control(prepare_ctrl_name, 1);
+ if (load_system_firmware(cfg, &cfg->image_current))
+ errorcnt++;
+ prepare_servo_control(prepare_ctrl_name, 0);
+ }
+ if (!errorcnt)
+ if (write_to_file("Wrote AP firmware to", output_file_name,
+ cfg->image_current.data,
+ cfg->image_current.size))
+ errorcnt++;
+
+ free(servo_programmer);
+ updater_delete_config(cfg);
+ return !!errorcnt;
+}
+#define CMD_HELP_STR "Read AP firmware"
+
+#else /* USE_FLASHROM */
+
+static int do_read(int argc, char *argv[])
+{
+ FATAL(MYNAME " was built without flashrom support, `read` command unavailable!\n");
+ return -1;
+}
+#define CMD_HELP_STR "Read system firmware (unavailable in this build)"
+
+#endif /* !USE_FLASHROM */
+
+DECLARE_FUTIL_COMMAND(read, do_read, VBOOT_VERSION_ALL, CMD_HELP_STR);
diff --git a/tests/futility/run_test_scripts.sh b/tests/futility/run_test_scripts.sh
index 09086998..662304da 100755
--- a/tests/futility/run_test_scripts.sh
+++ b/tests/futility/run_test_scripts.sh
@@ -36,6 +36,7 @@ ${SCRIPT_DIR}/futility/test_sign_usbpd1.sh
${SCRIPT_DIR}/futility/test_update.sh
${SCRIPT_DIR}/futility/test_file_types.sh
${SCRIPT_DIR}/futility/test_gscvd.sh
+${SCRIPT_DIR}/futility/test_read.sh
"
# Get ready...
diff --git a/tests/futility/test_read.sh b/tests/futility/test_read.sh
new file mode 100755
index 00000000..2720c1fb
--- /dev/null
+++ b/tests/futility/test_read.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+# Copyright 2022 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+set -eux
+
+me=${0##*/}
+TMP="${me}.tmp"
+
+# Work in scratch directory
+cd "${OUTDIR}"
+
+PEPPY_BIOS="${SCRIPT_DIR}/futility/data/bios_peppy_mp.bin"
+
+"${FUTILITY}" read --emulate="${PEPPY_BIOS}" "${TMP}"
+cmp "${PEPPY_BIOS}" "${TMP}"
+
+# cleanup
+rm -f "${TMP}"*
+exit 0