diff options
author | Sam McNally <sammc@chromium.org> | 2022-10-18 20:34:42 +1100 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-11-17 09:24:41 +0000 |
commit | 5b6985240bc5e673ec2a744bedda7e22583cea77 (patch) | |
tree | 788d562a3396094daf65e3daa5f065c2d0dba5ee | |
parent | 69d6d85c20910cc1d07752c9468f7822d421339e (diff) | |
download | vboot-5b6985240bc5e673ec2a744bedda7e22583cea77.tar.gz |
futility: updater: Use flashrom dummy programmer to implement --emulate
futility update --emulate and flashrom's dummy programmer serve similar
purposes - both provide support for using a file instead of a real ROM.
The current --emulate implementation involves special-casing before
interacting with flashrom and pre-filling in the current image contents;
an appropriately-configured dummy programmer and unmodified flashrom
interactions could accomplish the same outcome with a more centralised
handling of --emulate. Other --emulate interactions mock out
non-flashrom interactions, so need to continue handling --emulate
specially for now.
Switch --emulate to use the dummy programmer. Add an extra field to
store the original programmer option for deciding on whether the apply
the preserve_me quirk.
BUG=b:253966060
TEST=unit tests
BRANCH=None
Signed-off-by: Sam McNally <sammc@chromium.org>
Change-Id: I687749523f54edcb9dd41cfc85614949b9d6607a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3965582
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
-rw-r--r-- | futility/updater.c | 37 | ||||
-rw-r--r-- | futility/updater.h | 2 | ||||
-rw-r--r-- | futility/updater_utils.c | 58 | ||||
-rwxr-xr-x | tests/futility/test_update.sh | 2 |
4 files changed, 31 insertions, 68 deletions
diff --git a/futility/updater.c b/futility/updater.c index 66529fea..51418847 100644 --- a/futility/updater.c +++ b/futility/updater.c @@ -7,6 +7,7 @@ #include <assert.h> #include <ctype.h> +#include <sys/stat.h> #include "2rsa.h" #include "crossystem.h" @@ -463,7 +464,7 @@ static int preserve_management_engine(struct updater_config *cfg, image_from, image_to, FMAP_SI_DESC); } - if (!strcmp(image_from->programmer, PROG_HOST)) { + if (!strcmp(cfg->original_programmer, PROG_HOST)) { if (try_apply_quirk(QUIRK_PRESERVE_ME, cfg) > 0) { VB2_DEBUG("ME needs to be preserved - preserving %s.\n", FMAP_SI_ME); @@ -1279,6 +1280,7 @@ struct updater_config *updater_new_config(void) return cfg; cfg->image.programmer = PROG_HOST; cfg->image_current.programmer = PROG_HOST; + cfg->original_programmer = PROG_HOST; cfg->ec_image.programmer = PROG_EC; cfg->pd_image.programmer = PROG_PD; @@ -1547,9 +1549,31 @@ int updater_setup_config(struct updater_config *cfg, check_single_image = 1; cfg->image.programmer = arg->programmer; cfg->image_current.programmer = arg->programmer; + cfg->original_programmer = arg->programmer; VB2_DEBUG("AP (host) programmer changed to %s.\n", arg->programmer); } + if (arg->emulation) { + VB2_DEBUG("Using file %s for emulation.\n", arg->emulation); + check_single_image = 1; + struct stat statbuf; + if (stat(arg->emulation, &statbuf)) { + ERROR("Failed to stat emulation file %s\n", + arg->emulation); + return ++errorcnt; + } + + cfg->emulation = arg->emulation; + /* Store ownership of the dummy programmer string in + cfg->emulation_programmer. */ + ASPRINTF(&cfg->emulation_programmer, + "dummy:emulate=VARIABLE_SIZE,size=%d,image=%s", + (int)statbuf.st_size, arg->emulation); + + cfg->image.programmer = cfg->emulation_programmer; + cfg->image_current.programmer = cfg->emulation_programmer; + } + if (arg->sys_props) override_properties_from_list(arg->sys_props, cfg); if (arg->write_protection) { @@ -1560,14 +1584,6 @@ int updater_setup_config(struct updater_config *cfg, } /* Set up archive and load images. */ - if (arg->emulation) { - /* Process emulation file first. */ - cfg->emulation = arg->emulation; - VB2_DEBUG("Using file %s for emulation.\n", arg->emulation); - errorcnt += !!load_firmware_image( - &cfg->image_current, arg->emulation, NULL); - } - /* Always load images specified from command line directly. */ errorcnt += updater_load_images( cfg, arg, arg->image, arg->ec_image, arg->pd_image); @@ -1689,6 +1705,9 @@ void updater_delete_config(struct updater_config *cfg) free_firmware_image(&cfg->image_current); free_firmware_image(&cfg->ec_image); free_firmware_image(&cfg->pd_image); + cfg->image.programmer = cfg->original_programmer; + cfg->image_current.programmer = cfg->original_programmer; + free(cfg->emulation_programmer); remove_all_temp_files(&cfg->tempfiles); if (cfg->archive) archive_close(cfg->archive); diff --git a/futility/updater.h b/futility/updater.h index e526db87..08564b58 100644 --- a/futility/updater.h +++ b/futility/updater.h @@ -77,6 +77,8 @@ struct updater_config { int do_verify; int verbosity; const char *emulation; + char *emulation_programmer; + const char *original_programmer; int override_gbb_flags; uint32_t gbb_flags; }; diff --git a/futility/updater_utils.c b/futility/updater_utils.c index af94690a..a41c4038 100644 --- a/futility/updater_utils.c +++ b/futility/updater_utils.c @@ -618,61 +618,6 @@ static char *get_flashrom_command(enum flash_command flash_cmd, return cmd; } -/* - * Emulates writing a firmware image to the system. - * Returns 0 if success, non-zero if error. - */ -static int emulate_write_firmware(const char *filename, - const struct firmware_image *image, - const char * const sections[]) -{ - int i, errorcnt = 0; - struct firmware_image to_image = {0}; - - INFO("Writing from %s to %s (emu=%s).\n", - image->file_name, image->programmer, filename); - - if (load_firmware_image(&to_image, filename, NULL)) { - ERROR("Cannot load image from %s.\n", filename); - return -1; - } - - if (image->size != to_image.size) { - ERROR("Image size is different (%s:%d != %s:%d)\n", - image->file_name, image->size, to_image.file_name, - to_image.size); - errorcnt++; - goto exit; - } - - if (!sections) { - VB2_DEBUG(" - write the whole image.\n"); - memmove(to_image.data, image->data, image->size); - } - for (i = 0; sections && sections[i]; i++) { - VB2_DEBUG(" - write the section: %s.\n", sections[i]); - if (preserve_firmware_section(image, &to_image, sections[i])) { - ERROR("Failed to write the section: %s\n", sections[i]); - errorcnt++; - /* - * Exit the loop, but still write the file to reflect - * the partial changes - same as real flashrom behavior. - */ - break; - } - } - - if (vb2_write_file(filename, to_image.data, to_image.size)) { - ERROR("Failed writing to file: %s\n", filename); - errorcnt++; - goto exit; - } - -exit: - free_firmware_image(&to_image); - return errorcnt; -} - static int external_flashrom(enum flash_command flash_cmd, struct flashrom_params *params, struct tempfile *tempfiles) @@ -796,9 +741,6 @@ int write_system_firmware(struct updater_config *cfg, struct flashrom_params params = {0}; struct firmware_image *flash_contents = NULL; - if (cfg->emulation) - return emulate_write_firmware(cfg->emulation, image, sections); - if (cfg->use_diff_image && cfg->image_current.data && is_the_same_programmer(&cfg->image_current, image)) flash_contents = &cfg->image_current; diff --git a/tests/futility/test_update.sh b/tests/futility/test_update.sh index e71a2a35..f2ca3034 100755 --- a/tests/futility/test_update.sh +++ b/tests/futility/test_update.sh @@ -334,7 +334,7 @@ test_update "Legacy update" \ # Test quirks test_update "Full update (wrong size)" \ - "${FROM_IMAGE}.large" "!Image size is different" \ + "${FROM_IMAGE}.large" "!Failed writing firmware" \ -i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1 \ --quirks unlock_me_for_update,eve_smm_store |