diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | futility/updater.c | 85 | ||||
-rw-r--r-- | futility/updater.h | 5 | ||||
-rw-r--r-- | futility/updater_dut.c | 137 | ||||
-rw-r--r-- | futility/updater_manifest.c | 30 | ||||
-rw-r--r-- | futility/updater_quirks.c | 8 | ||||
-rw-r--r-- | futility/updater_utils.c | 65 | ||||
-rw-r--r-- | futility/updater_utils.h | 47 |
8 files changed, 207 insertions, 171 deletions
@@ -674,6 +674,7 @@ FUTIL_SRCS = \ ifneq ($(filter-out 0,${USE_FLASHROM}),) FUTIL_SRCS += host/lib/flashrom_drv.c \ futility/updater_archive.c \ + futility/updater_dut.c \ futility/updater_manifest.c \ futility/updater_quirks.c \ futility/updater_utils.c \ diff --git a/futility/updater.c b/futility/updater.c index 8a5a0be5..96004713 100644 --- a/futility/updater.c +++ b/futility/updater.c @@ -10,7 +10,6 @@ #include <sys/stat.h> #include "2rsa.h" -#include "crossystem.h" #include "futility.h" #include "host_misc.h" #include "updater.h" @@ -33,27 +32,7 @@ enum rootkey_compat_result { ROOTKEY_COMPAT_REKEY_TO_DEV, }; -/* - * Gets the system property by given type. - * If the property was not loaded yet, invoke the property getter function - * and cache the result. - * Returns the property value. - */ -int get_system_property(enum system_property_type property_type, - struct updater_config *cfg) -{ - struct system_property *prop; - - assert(property_type < SYS_PROP_MAX); - prop = &cfg->system_properties[property_type]; - if (!prop->initialized) { - prop->initialized = 1; - prop->value = prop->getter(); - } - return prop->value; -} - -static void print_system_properties(struct updater_config *cfg) +static void print_dut_properties(struct updater_config *cfg) { int i; @@ -62,37 +41,35 @@ static void print_system_properties(struct updater_config *cfg) * system, so we want to peek at them first and then print out. */ VB2_DEBUG("Scanning system properties...\n"); - for (i = 0; i < SYS_PROP_MAX; i++) { - get_system_property((enum system_property_type)i, cfg); - } + for (i = 0; i < DUT_PROP_MAX; i++) + dut_get_property((enum dut_property_type)i, cfg); printf("System properties: ["); - for (i = 0; i < SYS_PROP_MAX; i++) { + for (i = 0; i < DUT_PROP_MAX; i++) { printf("%d,", - get_system_property((enum system_property_type)i, cfg)); + dut_get_property((enum dut_property_type)i, cfg)); } printf("]\n"); } /* * Overrides the return value of a system property. - * After invoked, next call to get_system_property(type, cfg) will return + * After invoked, next call to dut_get_property(type, cfg) will return * the given value. */ -static void override_system_property(enum system_property_type property_type, - struct updater_config *cfg, - int value) +static void override_dut_property(enum dut_property_type property_type, + struct updater_config *cfg, int value) { - struct system_property *prop; + struct dut_property *prop; - assert(property_type < SYS_PROP_MAX); - prop = &cfg->system_properties[property_type]; + assert(property_type < DUT_PROP_MAX); + prop = &cfg->dut_properties[property_type]; prop->initialized = 1; prop->value = value; } /* - * Overrides system properties from a given list. + * Overrides DUT properties from a given list. * The list should be string of integers eliminated by comma and/or space. * For example, "1 2 3" and "1,2,3" both overrides first 3 properties. * To skip some properties you have to use comma, for example @@ -120,15 +97,15 @@ static void override_properties_from_list(const char *override_list, } if (!isascii(c) || !(isdigit(c) || c == '-')) continue; - if (i >= SYS_PROP_MAX) { + if (i >= DUT_PROP_MAX) { ERROR("Too many fields (max is %d): %s.\n", - SYS_PROP_MAX, override_list); + DUT_PROP_MAX, override_list); return; } v = strtol(s, &e, 0); s = e - 1; VB2_DEBUG("property[%d].value = %ld\n", i, v); - override_system_property((enum system_property_type)i, cfg, v); + override_dut_property((enum dut_property_type)i, cfg, v); wait_comma = 1; i++; } @@ -250,7 +227,7 @@ static const char *decide_rw_target(struct updater_config *cfg, int is_vboot2) { const char *a = FMAP_RW_SECTION_A, *b = FMAP_RW_SECTION_B; - int slot = get_system_property(SYS_PROP_MAINFW_ACT, cfg); + int slot = dut_get_property(DUT_PROP_MAINFW_ACT, cfg); /* In vboot1, always update B and check content with A. */ if (!is_vboot2) @@ -268,7 +245,7 @@ static const char *decide_rw_target(struct updater_config *cfg, } /* - * Sets any needed system properties to indicate system should try the new + * Sets any needed DUT properties to indicate system should try the new * firmware on next boot. * The `target` argument is an FMAP section name indicating which to try. * Returns 0 if success, non-zero if error. @@ -303,19 +280,19 @@ static int set_try_cookies(struct updater_config *cfg, const char *target, } if (is_vboot2) { - if (VbSetSystemPropertyString("fw_try_next", slot)) { + if (dut_set_property_string("fw_try_next", slot)) { ERROR("Failed to set fw_try_next to %s.\n", slot); return -1; } if (!has_update && - VbSetSystemPropertyString("fw_result", "success")) { + dut_set_property_string("fw_result", "success")) { ERROR("Failed to set fw_result to success.\n"); return -1; } } /* fw_try_count is identical to fwb_tries in vboot1. */ - if (VbSetSystemPropertyInt("fw_try_count", tries)) { + if (dut_set_property_int("fw_try_count", tries)) { ERROR("Failed to set fw_try_count to %d.\n", tries); return -1; } @@ -380,7 +357,7 @@ static int write_optional_firmware(struct updater_config *cfg, * only if it is OK. */ if (check_programmer_wp && - get_system_property(SYS_PROP_WP_HW, cfg) == WP_ENABLED && + dut_get_property(DUT_PROP_WP_HW, cfg) == WP_ENABLED && flashrom_get_wp(image->programmer, -1) == WP_ENABLED) { ERROR("Target %s is write protected, skip updating.\n", image->programmer); @@ -827,7 +804,7 @@ static int do_check_compatible_tpm_keys(struct updater_config *cfg, return -1; /* The stored tpm_fwver can be 0 (b/116298359#comment3). */ - tpm_fwver = get_system_property(SYS_PROP_TPM_FWVER, cfg); + tpm_fwver = dut_get_property(DUT_PROP_TPM_FWVER, cfg); if (tpm_fwver < 0) { /* * tpm_fwver is commonly misreported in --ccd mode, so allow @@ -931,7 +908,7 @@ static enum updater_error_codes update_try_rw_firmware( { const char *target, *self_target; int has_update = 1; - int is_vboot2 = get_system_property(SYS_PROP_FW_VBOOT2, cfg); + int is_vboot2 = dut_get_property(DUT_PROP_FW_VBOOT2, cfg); preserve_gbb(image_from, image_to, 1, 0, 0); if (!wp_enabled && section_needs_update( @@ -1149,7 +1126,7 @@ enum updater_error_codes update_firmware(struct updater_config *cfg) if (cfg->try_update == TRY_UPDATE_DEFERRED_APPLY) { INFO("Apply deferred updates, only setting cookies for the " "next boot slot.\n"); - int vboot2 = get_system_property(SYS_PROP_FW_VBOOT2, cfg); + int vboot2 = dut_get_property(DUT_PROP_FW_VBOOT2, cfg); if (set_try_cookies( cfg, decide_rw_target(cfg, TARGET_UPDATE, vboot2), @@ -1201,8 +1178,8 @@ enum updater_error_codes update_firmware(struct updater_config *cfg) wp_enabled = is_write_protection_enabled(cfg); STATUS("Write protection: %d (%s; HW=%d, SW=%d).\n", wp_enabled, wp_enabled ? "enabled" : "disabled", - get_system_property(SYS_PROP_WP_HW, cfg), - get_system_property(SYS_PROP_WP_SW, cfg)); + dut_get_property(DUT_PROP_WP_HW, cfg), + dut_get_property(DUT_PROP_WP_SW, cfg)); if (try_apply_quirk(QUIRK_ENLARGE_IMAGE, cfg)) return UPDATE_ERR_SYSTEM_IMAGE; @@ -1214,7 +1191,7 @@ enum updater_error_codes update_firmware(struct updater_config *cfg) return UPDATE_ERR_SYSTEM_IMAGE; if (debugging_enabled) - print_system_properties(cfg); + print_dut_properties(cfg); if (cfg->legacy_update) return update_legacy_firmware(cfg, image_to); @@ -1260,8 +1237,8 @@ struct updater_config *updater_new_config(void) cfg->check_platform = 1; cfg->do_verify = 1; - init_system_properties(&cfg->system_properties[0], - ARRAY_SIZE(cfg->system_properties)); + dut_init_properties(&cfg->dut_properties[0], + ARRAY_SIZE(cfg->dut_properties)); updater_register_quirks(cfg); return cfg; } @@ -1573,8 +1550,8 @@ int updater_setup_config(struct updater_config *cfg, if (arg->write_protection) { /* arg->write_protection must be done after arg->sys_props. */ int r = strtol(arg->write_protection, NULL, 0); - override_system_property(SYS_PROP_WP_HW, cfg, r); - override_system_property(SYS_PROP_WP_SW, cfg, r); + override_dut_property(DUT_PROP_WP_HW, cfg, r); + override_dut_property(DUT_PROP_WP_SW, cfg, r); } /* Set up archive and load images. */ diff --git a/futility/updater.h b/futility/updater.h index cedeb302..a8bbfe31 100644 --- a/futility/updater.h +++ b/futility/updater.h @@ -72,7 +72,7 @@ enum try_update_type { struct updater_config { struct firmware_image image, image_current; struct firmware_image ec_image, pd_image; - struct system_property system_properties[SYS_PROP_MAX]; + struct dut_property dut_properties[DUT_PROP_MAX]; struct quirk_entry quirks[QUIRK_MAX]; struct u_archive *archive; struct tempfile tempfiles; @@ -233,9 +233,6 @@ void updater_register_quirks(struct updater_config *cfg); /* Gets the value (setting) of specified quirks from updater configuration. */ int get_config_quirk(enum quirk_types quirk, const struct updater_config *cfg); -/* Gets the system property by given type. Returns the property value. */ -int get_system_property(enum system_property_type property_type, - struct updater_config *cfg); /* * Gets the default quirk config string from target image name. * Returns a string (in same format as --quirks) to load or NULL if no quirks. diff --git a/futility/updater_dut.c b/futility/updater_dut.c new file mode 100644 index 00000000..689dd227 --- /dev/null +++ b/futility/updater_dut.c @@ -0,0 +1,137 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * The DUT interface helper functions for the firmware updater. + */ + +#include <assert.h> +#ifdef HAVE_CROSID +#include <crosid.h> +#endif +#include "crossystem.h" +#include "updater.h" + +/** + * dut_get_manifest_key() - Wrapper to get the firmware manifest key from crosid + * + * @manifest_key_out - Output parameter of the firmware manifest key. + * + * Returns: + * - <0 if libcrosid is unavailable or there was an error reading + * device data + * - >=0 (the matched device index) success + */ +int dut_get_manifest_key(char **manifest_key_out) +{ +#ifdef HAVE_CROSID + return crosid_get_firmware_manifest_key(manifest_key_out); +#else + ERROR("This version of futility was compiled without libcrosid " + "(perhaps compiled outside of the Chrome OS build system?) and " + "the update command is not fully supported. Either compile " + "from the Chrome OS build, or pass --model to manually specify " + "the machine model.\n"); + return -1; +#endif +} + +int dut_set_property_string(const char *key, const char *value) +{ + return VbSetSystemPropertyString(key, value); +} + +const char *dut_get_property_string(const char *key, char *dest, size_t size) +{ + return VbGetSystemPropertyString(key, dest, size); +} + +int dut_set_property_int(const char *key, const int value) +{ + return VbSetSystemPropertyInt(key, value); +} + +int dut_get_property_int(const char *key) +{ + return VbGetSystemPropertyInt(key); +} + +/* An helper function to return "mainfw_act" system property. */ +static int dut_get_mainfw_act(struct updater_config *cfg) +{ + char buf[VB_MAX_STRING_PROPERTY]; + + if (!dut_get_property_string("mainfw_act", buf, sizeof(buf))) + return SLOT_UNKNOWN; + + if (strcmp(buf, FWACT_A) == 0) + return SLOT_A; + else if (strcmp(buf, FWACT_B) == 0) + return SLOT_B; + + return SLOT_UNKNOWN; +} + +/* A helper function to return the "tpm_fwver" system property. */ +static int dut_get_tpm_fwver(struct updater_config *cfg) +{ + return dut_get_property_int("tpm_fwver"); +} + +/* A helper function to return the "hardware write protection" status. */ +static int dut_get_wp_hw(struct updater_config *cfg) +{ + /* wpsw refers to write protection 'switch', not 'software'. */ + return dut_get_property_int("wpsw_cur") ? WP_ENABLED : WP_DISABLED; +} + +/* A helper function to return "fw_vboot2" system property. */ +static int dut_get_fw_vboot2(struct updater_config *cfg) +{ + return dut_get_property_int("fw_vboot2"); +} + +static int dut_get_platform_version(struct updater_config *cfg) +{ + return dut_get_property_int("board_id"); +} + +/* Helper function to return host software write protection status. */ +static int dut_get_wp_sw(struct updater_config *cfg) +{ + return flashrom_get_wp(PROG_HOST, -1); +} + +/* Helper functions to use or configure the DUT properties. */ + +/* + * Gets the DUT system property by given type. + * If the property was not loaded yet, invoke the property getter function + * and cache the result. + * Returns the property value. + */ +int dut_get_property(enum dut_property_type property_type, + struct updater_config *cfg) +{ + struct dut_property *prop; + + assert(property_type < DUT_PROP_MAX); + prop = &cfg->dut_properties[property_type]; + if (!prop->initialized) { + prop->initialized = 1; + prop->value = prop->getter(cfg); + } + return prop->value; +} + +void dut_init_properties(struct dut_property *props, int num) +{ + memset(props, 0, num * sizeof(*props)); + assert(num >= DUT_PROP_MAX); + props[DUT_PROP_MAINFW_ACT].getter = dut_get_mainfw_act; + props[DUT_PROP_TPM_FWVER].getter = dut_get_tpm_fwver; + props[DUT_PROP_FW_VBOOT2].getter = dut_get_fw_vboot2; + props[DUT_PROP_PLATFORM_VER].getter = dut_get_platform_version; + props[DUT_PROP_WP_HW].getter = dut_get_wp_hw; + props[DUT_PROP_WP_SW].getter = dut_get_wp_sw; +} diff --git a/futility/updater_manifest.c b/futility/updater_manifest.c index ca338c40..6d7d9d41 100644 --- a/futility/updater_manifest.c +++ b/futility/updater_manifest.c @@ -10,10 +10,6 @@ #include <sys/types.h> #endif -#ifdef HAVE_CROSID -#include <crosid.h> -#endif - #include "updater.h" #include "util_misc.h" @@ -619,30 +615,6 @@ static int manifest_from_simple_folder(struct manifest *manifest) return 0; } -/** - * get_manifest_key() - Wrapper to get the firmware manifest key from crosid - * - * @manifest_key_out - Output parameter of the firmware manifest key. - * - * Returns: - * - <0 if libcrosid is unavailable or there was an error reading - * device data - * - >=0 (the matched device index) success - */ -static int get_manifest_key(char **manifest_key_out) -{ -#ifdef HAVE_CROSID - return crosid_get_firmware_manifest_key(manifest_key_out); -#else - ERROR("This version of futility was compiled without libcrosid " - "(perhaps compiled outside of the Chrome OS build system?) and " - "the update command is not fully supported. Either compile " - "from the Chrome OS build, or pass --model to manually specify " - "the machine model.\n"); - return -1; -#endif -} - /* * Finds the existing model_config from manifest that best matches current * system (as defined by model_name). @@ -665,7 +637,7 @@ const struct model_config *manifest_find_model(const struct manifest *manifest, return &manifest->models[0]; if (!model_name) { - matched_index = get_manifest_key(&manifest_key); + matched_index = dut_get_manifest_key(&manifest_key); if (matched_index < 0) { ERROR("Failed to get device identity. " "Run \"crosid -v\" for explanation.\n"); diff --git a/futility/updater_quirks.c b/futility/updater_quirks.c index 4975997a..a46c426e 100644 --- a/futility/updater_quirks.c +++ b/futility/updater_quirks.c @@ -91,7 +91,7 @@ static int is_ec_software_sync_enabled(struct updater_config *cfg) const struct vb2_gbb_header *gbb; /* Check if current system has disabled software sync or no support. */ - if (!(VbGetSystemPropertyInt("vdat_flags") & VBSD_EC_SOFTWARE_SYNC)) { + if (!(dut_get_property_int("vdat_flags") & VBSD_EC_SOFTWARE_SYNC)) { INFO("EC Software Sync is not available.\n"); return 0; } @@ -156,7 +156,7 @@ static int ec_ro_software_sync(struct updater_config *cfg) "update by EC RO software sync.\n"); return 1; } - VbSetSystemPropertyInt("try_ro_sync", 1); + dut_set_property_int("try_ro_sync", 1); return 0; } @@ -166,7 +166,7 @@ static int ec_ro_software_sync(struct updater_config *cfg) static int is_ec_in_rw(void) { char buf[VB_MAX_STRING_PROPERTY]; - return (VbGetSystemPropertyString("ecfw_act", buf, sizeof(buf)) && + return (dut_get_property_string("ecfw_act", buf, sizeof(buf)) && strcasecmp(buf, "RW") == 0); } @@ -276,7 +276,7 @@ static int quirk_unlock_wilco_me_for_update(struct updater_config *cfg) static int quirk_min_platform_version(struct updater_config *cfg) { int min_version = get_config_quirk(QUIRK_MIN_PLATFORM_VERSION, cfg); - int platform_version = get_system_property(SYS_PROP_PLATFORM_VER, cfg); + int platform_version = dut_get_property(DUT_PROP_PLATFORM_VER, cfg); VB2_DEBUG("Minimum required version=%d, current platform version=%d\n", min_version, platform_version); diff --git a/futility/updater_utils.c b/futility/updater_utils.c index 904b6b4e..b04e56ca 100644 --- a/futility/updater_utils.c +++ b/futility/updater_utils.c @@ -16,7 +16,6 @@ #endif #include "2common.h" -#include "crossystem.h" #include "host_misc.h" #include "util_misc.h" #include "updater.h" @@ -364,11 +363,11 @@ const struct vb2_gbb_header *find_gbb(const struct firmware_image *image) int is_write_protection_enabled(struct updater_config *cfg) { /* Default to enabled. */ - int wp = get_system_property(SYS_PROP_WP_HW, cfg); + int wp = dut_get_property(DUT_PROP_WP_HW, cfg); if (wp == WP_DISABLED) return wp; /* For error or enabled, check WP SW. */ - wp = get_system_property(SYS_PROP_WP_SW, cfg); + wp = dut_get_property(DUT_PROP_WP_SW, cfg); /* Consider all errors as enabled. */ if (wp != WP_DISABLED) return WP_ENABLED; @@ -410,47 +409,6 @@ char *host_shell(const char *command) return strdup(buf); } - -/* An helper function to return "mainfw_act" system property. */ -static int host_get_mainfw_act(void) -{ - char buf[VB_MAX_STRING_PROPERTY]; - - if (!VbGetSystemPropertyString("mainfw_act", buf, sizeof(buf))) - return SLOT_UNKNOWN; - - if (strcmp(buf, FWACT_A) == 0) - return SLOT_A; - else if (strcmp(buf, FWACT_B) == 0) - return SLOT_B; - - return SLOT_UNKNOWN; -} - -/* A helper function to return the "tpm_fwver" system property. */ -static int host_get_tpm_fwver(void) -{ - return VbGetSystemPropertyInt("tpm_fwver"); -} - -/* A helper function to return the "hardware write protection" status. */ -static int host_get_wp_hw(void) -{ - /* wpsw refers to write protection 'switch', not 'software'. */ - return VbGetSystemPropertyInt("wpsw_cur") ? WP_ENABLED : WP_DISABLED; -} - -/* A helper function to return "fw_vboot2" system property. */ -static int host_get_fw_vboot2(void) -{ - return VbGetSystemPropertyInt("fw_vboot2"); -} - -static int host_get_platform_version(void) -{ - return VbGetSystemPropertyInt("board_id"); -} - void prepare_servo_control(const char *control_name, int on) { char *cmd; @@ -770,25 +728,6 @@ int write_system_firmware(struct updater_config *cfg, return r; } -/* Helper function to return host software write protection status. */ -static int host_get_wp_sw(void) -{ - return flashrom_get_wp(PROG_HOST, -1); -} - -/* Helper function to configure all properties. */ -void init_system_properties(struct system_property *props, int num) -{ - memset(props, 0, num * sizeof(*props)); - assert(num >= SYS_PROP_MAX); - props[SYS_PROP_MAINFW_ACT].getter = host_get_mainfw_act; - props[SYS_PROP_TPM_FWVER].getter = host_get_tpm_fwver; - props[SYS_PROP_FW_VBOOT2].getter = host_get_fw_vboot2; - props[SYS_PROP_PLATFORM_VER].getter = host_get_platform_version; - props[SYS_PROP_WP_HW].getter = host_get_wp_hw; - props[SYS_PROP_WP_SW].getter = host_get_wp_sw; -} - /* * Helper function to create a new temporary file. * All files created will be removed remove_all_temp_files(). diff --git a/futility/updater_utils.h b/futility/updater_utils.h index a5d012b8..93964a4d 100644 --- a/futility/updater_utils.h +++ b/futility/updater_utils.h @@ -156,6 +156,11 @@ int preserve_firmware_section(const struct firmware_image *image_from, const char *section_name); /* + * Returns rootkey hash of firmware image, or NULL on failure. + */ +const char *get_firmware_rootkey_hash(const struct firmware_image *image); + +/* * Finds the GBB (Google Binary Block) header on a given firmware image. * Returns a pointer to valid GBB header, or NULL on not found. */ @@ -223,29 +228,37 @@ const char *cbfs_extract_file(const char *image_file, const char *cbfs_name, struct tempfile *tempfiles); -/* Utilities for accessing system properties */ -struct system_property { - int (*getter)(void); +/* DUT related functions (implementations in updater_dut.c) */ + +struct dut_property { + int (*getter)(struct updater_config *cfg); int value; int initialized; }; -enum system_property_type { - SYS_PROP_MAINFW_ACT, - SYS_PROP_TPM_FWVER, - SYS_PROP_FW_VBOOT2, - SYS_PROP_PLATFORM_VER, - SYS_PROP_WP_HW, - SYS_PROP_WP_SW, - SYS_PROP_MAX +enum dut_property_type { + DUT_PROP_MAINFW_ACT, + DUT_PROP_TPM_FWVER, + DUT_PROP_FW_VBOOT2, + DUT_PROP_PLATFORM_VER, + DUT_PROP_WP_HW, + DUT_PROP_WP_SW, + DUT_PROP_MAX }; -/* Helper function to initialize system properties. */ -void init_system_properties(struct system_property *props, int num); +/* Helper function to initialize DUT properties. */ +void dut_init_properties(struct dut_property *props, int num); -/* - * Returns rootkey hash of firmware image, or NULL on failure. - */ -const char *get_firmware_rootkey_hash(const struct firmware_image *image); +/* Gets the DUT system property by given type. Returns the property value. */ +int dut_get_property(enum dut_property_type property_type, + struct updater_config *cfg); + +int dut_set_property_string(const char *key, const char *value); +const char *dut_get_property_string(const char *key, char *dest, size_t size); +int dut_set_property_int(const char *key, const int value); +int dut_get_property_int(const char *key); + +/* Gets the 'firmware manifest key' on the DUT. */ +int dut_get_manifest_key(char **manifest_key_out); #endif /* VBOOT_REFERENCE_FUTILITY_UPDATER_UTILS_H_ */ |