diff options
author | Hung-Te Lin <hungte@chromium.org> | 2018-08-31 15:16:59 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-09-10 01:13:20 -0700 |
commit | 8c564a18969b14f7a86ffd93089bba9f37cb53a5 (patch) | |
tree | f2994e5fa9a031cd32ee2da82568820cf6ae8187 | |
parent | c6c620194ff4a44626b550338973df7aa00886f7 (diff) | |
download | vboot-8c564a18969b14f7a86ffd93089bba9f37cb53a5.tar.gz |
futility: cmd_update: Add quirk 'enlarge_image'
Some devices may have shipped with a smaller image that the real flash may be
larger, especially if the device's original flash has been EOL'ed.
The quirk 'enlarge_image' allows changing image size according to current_image
size by padding 0xFF (flash default value).
BUG=chromium:875551
TEST=make futil; tests/futility/run_test_scripts.sh $(pwd)/build/futility
BRANCH=None
Change-Id: I84373cfa9bcbd98a2cd96a7dd4bed27a6f724cf3
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1198806
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | futility/cmd_update.c | 71 | ||||
-rwxr-xr-x | tests/futility/test_update.sh | 22 |
2 files changed, 86 insertions, 7 deletions
diff --git a/futility/cmd_update.c b/futility/cmd_update.c index 33f71309..1551627c 100644 --- a/futility/cmd_update.c +++ b/futility/cmd_update.c @@ -122,7 +122,7 @@ struct quirk_entry { }; enum quirk_types { - QUIRK_TEST, + QUIRK_ENLARGE_IMAGE, QUIRK_MAX, }; @@ -732,6 +732,28 @@ static void free_image(struct firmware_image *image) free(image->emulation); memset(image, 0, sizeof(*image)); } +/* + * Reloads a firmware image from file. + * Keeps special configuration like emulation. + * Returns 0 on success, otherwise failure. + */ +static int reload_image(const char *file_name, struct firmware_image *image) +{ + char *emulation = image->emulation; + int r; + + /* + * All values except emulation and programmer will be re-constructed + * in load_image. `programmer` is not touched in free_image so we only + * need to keep `emulation`. + */ + image->emulation = NULL; + free_image(image); + r = load_image(file_name, image); + if (r == 0) + image->emulation = emulation; + return r; +} /* * Decides which target in RW firmware to manipulate. @@ -1385,6 +1407,41 @@ static int check_compatible_tpm_keys(struct updater_config *cfg, return 0; } +/* + * Quirk to enlarge a firmware image to match flash size. This is needed by + * devices using multiple SPI flash with different sizes, for example 8M and + * 16M. The image_to will be padded with 0xFF using the size of image_from. + * Returns 0 on success, otherwise failure. + */ +static int quirk_enlarge_image(struct updater_config *cfg) +{ + struct firmware_image *image_from = &cfg->image_current, + *image_to = &cfg->image; + const char *tmp_path; + size_t to_write; + FILE *fp; + + if (image_from->size <= image_to->size) + return 0; + + tmp_path = create_temp_file(); + if (!tmp_path) + return -1; + + DEBUG("Resize image from %u to %u.", image_to->size, image_from->size); + to_write = image_from->size - image_to->size; + vb2_write_file(tmp_path, image_to->data, image_to->size); + fp = fopen(tmp_path, "ab"); + if (!fp) { + ERROR("Cannot open temporary file %s.", tmp_path); + return -1; + } + while (to_write-- > 0) + fputc('\xff', fp); + fclose(fp); + return reload_image(tmp_path, image_to); +} + enum updater_error_codes { UPDATE_ERR_DONE, UPDATE_ERR_NEED_RO_UPDATE, @@ -1599,6 +1656,9 @@ static enum updater_error_codes update_firmware(struct updater_config *cfg) get_system_property(SYS_PROP_WP_HW, cfg), get_system_property(SYS_PROP_WP_SW, cfg)); + if (try_apply_quirk(QUIRK_ENLARGE_IMAGE, cfg)) + return UPDATE_ERR_SYSTEM_IMAGE; + if (debugging_enabled) print_system_properties(cfg); @@ -1701,7 +1761,12 @@ static int do_update(int argc, char *argv[]) [SYS_PROP_WP_SW] = {.getter = host_get_wp_sw}, }, .quirks = { - [QUIRK_TEST] = {.name="test", .help="Dummy quirk"}, + [QUIRK_ENLARGE_IMAGE] = { + .name="enlarge_image", + .help="Enlarge firmware image by flash size.", + .apply=quirk_enlarge_image, + }, + }, }; @@ -1727,8 +1792,6 @@ static int do_update(int argc, char *argv[]) break; case 'L': list_config_quirks(&cfg); - /* TODO(hungte): Remove this experimental quirk. */ - try_apply_quirk(QUIRK_TEST, &cfg); return 0; case 'm': if (strcmp(optarg, "autoupdate") == 0) { diff --git a/tests/futility/test_update.sh b/tests/futility/test_update.sh index 8cba724b..4748f10a 100755 --- a/tests/futility/test_update.sh +++ b/tests/futility/test_update.sh @@ -25,9 +25,10 @@ test_quirks() { tr '\n' ' ' } -test "$(test_quirks "test")" = "test,1 " -test "$(test_quirks "test=2")" = "test,2 " -test "$(test_quirks " test, test=2")" = "test,1 test,2 " +test "$(test_quirks "enlarge_image")" = "enlarge_image,1 " +test "$(test_quirks "enlarge_image=2")" = "enlarge_image,2 " +test "$(test_quirks " enlarge_image, enlarge_image=2")" = \ + "enlarge_image,1 enlarge_image,2 " # Test data files LINK_BIOS="${SCRIPTDIR}/data/bios_link_mp.bin" @@ -84,6 +85,10 @@ FROM_DIFFERENT_ROOTKEY_IMAGE="${FROM_IMAGE}2" cp -f "${FROM_IMAGE}" "${FROM_DIFFERENT_ROOTKEY_IMAGE}" "${FUTILITY}" gbb -s --rootkey="${TMP}.to/rootkey" "${FROM_IMAGE}" +# Hack for quirks +cp -f "${FROM_IMAGE}" "${FROM_IMAGE}.large" +truncate -s $((8388608 * 2)) "${FROM_IMAGE}.large" + # Generate expected results. cp -f "${TO_IMAGE}" "${TMP}.expected.full" cp -f "${FROM_IMAGE}" "${TMP}.expected.rw" @@ -105,6 +110,8 @@ cp -f "${FROM_IMAGE}" "${TMP}.expected.legacy" RW_SECTION_B:${TMP}.to/RW_SECTION_B "${FUTILITY}" load_fmap "${TMP}.expected.legacy" \ RW_LEGACY:${TMP}.to/RW_LEGACY +cp -f "${TMP}.expected.full" "${TMP}.expected.large" +dd if=/dev/zero bs=8388608 count=1 | tr '\000' '\377' >>"${TMP}.expected.large" test_update() { local test_name="$1" @@ -212,3 +219,12 @@ test_update "RW update (vboot1, B->B)" \ test_update "Legacy update" \ "${FROM_IMAGE}" "${TMP}.expected.legacy" \ -i "${TO_IMAGE}" --mode=legacy + +# Test quirks +test_update "Full update (wrong size)" \ + "${FROM_IMAGE}.large" "!Image size is different" \ + -i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1 + +test_update "Full update (--quirks enlarge_image)" \ + "${FROM_IMAGE}.large" "${TMP}.expected.large" --quirks enlarge_image \ + -i "${TO_IMAGE}" --wp=0 --sys_props 0,0x10001,1 |