fwu: meta-data: switch to management by common code
The common code can now read, verify and fix meta-data copies while exposing one consistent structure to users. Only the .read_mdata() and .write_mdata() callbacks of fwu_mdata_ops are needed. Get rid of .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() and .write_mdata_partition() and also the corresponding wrapper functions. Signed-off-by: Jassi Brar <jaswinder.singh@linaro.org> Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org> Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org> Tested-by: Sughosh Ganu <sughosh.ganu@linaro.org>
This commit is contained in:
@@ -33,26 +33,6 @@ enum {
|
||||
BOTH_PARTS,
|
||||
};
|
||||
|
||||
static int fwu_get_dev_mdata(struct udevice **dev, struct fwu_mdata *mdata)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = uclass_first_device_err(UCLASS_FWU_MDATA, dev);
|
||||
if (ret) {
|
||||
log_debug("Cannot find fwu device\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!mdata)
|
||||
return 0;
|
||||
|
||||
ret = fwu_get_mdata(*dev, mdata);
|
||||
if (ret < 0)
|
||||
log_debug("Unable to get valid FWU metadata\n");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int trial_counter_update(u16 *trial_state_ctr)
|
||||
{
|
||||
bool delete;
|
||||
@@ -282,136 +262,6 @@ ret_mdata:
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* fwu_verify_mdata() - Verify the FWU metadata
|
||||
* @mdata: FWU metadata structure
|
||||
* @pri_part: FWU metadata partition is primary or secondary
|
||||
*
|
||||
* Verify the FWU metadata by computing the CRC32 for the metadata
|
||||
* structure and comparing it against the CRC32 value stored as part
|
||||
* of the structure.
|
||||
*
|
||||
* Return: 0 if OK, -ve on error
|
||||
*
|
||||
*/
|
||||
int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
|
||||
{
|
||||
u32 calc_crc32;
|
||||
void *buf;
|
||||
|
||||
buf = &mdata->version;
|
||||
calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
|
||||
|
||||
if (calc_crc32 != mdata->crc32) {
|
||||
log_debug("crc32 check failed for %s FWU metadata partition\n",
|
||||
pri_part ? "primary" : "secondary");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
|
||||
*
|
||||
* Read both the metadata copies from the storage media, verify their checksum,
|
||||
* and ascertain that both copies match. If one of the copies has gone bad,
|
||||
* restore it from the good copy.
|
||||
*
|
||||
* Return: 0 if OK, -ve on error
|
||||
*
|
||||
*/
|
||||
int fwu_check_mdata_validity(void)
|
||||
{
|
||||
int ret;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata pri_mdata;
|
||||
struct fwu_mdata secondary_mdata;
|
||||
uint mdata_parts[2];
|
||||
uint valid_partitions, invalid_partitions;
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, NULL);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/*
|
||||
* Check if the platform has defined its own
|
||||
* function to check the metadata partitions'
|
||||
* validity. If so, that takes precedence.
|
||||
*/
|
||||
ret = fwu_mdata_check(dev);
|
||||
if (!ret || ret != -ENOSYS)
|
||||
return ret;
|
||||
|
||||
/*
|
||||
* Two FWU metadata partitions are expected.
|
||||
* If we don't have two, user needs to create
|
||||
* them first
|
||||
*/
|
||||
valid_partitions = 0;
|
||||
ret = fwu_get_mdata_part_num(dev, mdata_parts);
|
||||
if (ret < 0) {
|
||||
log_debug("Error getting the FWU metadata partitions\n");
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
ret = fwu_read_mdata_partition(dev, &pri_mdata, mdata_parts[0]);
|
||||
if (!ret) {
|
||||
ret = fwu_verify_mdata(&pri_mdata, 1);
|
||||
if (!ret)
|
||||
valid_partitions |= PRIMARY_PART;
|
||||
}
|
||||
|
||||
ret = fwu_read_mdata_partition(dev, &secondary_mdata, mdata_parts[1]);
|
||||
if (!ret) {
|
||||
ret = fwu_verify_mdata(&secondary_mdata, 0);
|
||||
if (!ret)
|
||||
valid_partitions |= SECONDARY_PART;
|
||||
}
|
||||
|
||||
if (valid_partitions == (PRIMARY_PART | SECONDARY_PART)) {
|
||||
/*
|
||||
* Before returning, check that both the
|
||||
* FWU metadata copies are the same. If not,
|
||||
* populate the secondary partition from the
|
||||
* primary partition copy.
|
||||
*/
|
||||
if (!memcmp(&pri_mdata, &secondary_mdata,
|
||||
sizeof(struct fwu_mdata))) {
|
||||
ret = 0;
|
||||
} else {
|
||||
log_info("Both FWU metadata copies are valid but do not match.");
|
||||
log_info(" Restoring the secondary partition from the primary\n");
|
||||
ret = fwu_write_mdata_partition(dev, &pri_mdata,
|
||||
mdata_parts[1]);
|
||||
if (ret)
|
||||
log_debug("Restoring secondary FWU metadata partition failed\n");
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!(valid_partitions & BOTH_PARTS)) {
|
||||
log_info("Both FWU metadata partitions invalid\n");
|
||||
ret = -EBADMSG;
|
||||
goto out;
|
||||
}
|
||||
|
||||
invalid_partitions = valid_partitions ^ BOTH_PARTS;
|
||||
ret = fwu_write_mdata_partition(dev,
|
||||
(invalid_partitions == PRIMARY_PART) ?
|
||||
&secondary_mdata : &pri_mdata,
|
||||
(invalid_partitions == PRIMARY_PART) ?
|
||||
mdata_parts[0] : mdata_parts[1]);
|
||||
|
||||
if (ret)
|
||||
log_debug("Restoring %s FWU metadata partition failed\n",
|
||||
(invalid_partitions == PRIMARY_PART) ?
|
||||
"primary" : "secondary");
|
||||
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* fwu_get_active_index() - Get active_index from the FWU metadata
|
||||
* @active_idx: active_index value to be read
|
||||
@@ -424,19 +274,14 @@ out:
|
||||
*/
|
||||
int fwu_get_active_index(uint *active_idx)
|
||||
{
|
||||
int ret;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
int ret = 0;
|
||||
struct fwu_mdata *mdata = &g_mdata;
|
||||
|
||||
/*
|
||||
* Found the FWU metadata partition, now read the active_index
|
||||
* value
|
||||
*/
|
||||
*active_idx = mdata.active_index;
|
||||
*active_idx = mdata->active_index;
|
||||
if (*active_idx >= CONFIG_FWU_NUM_BANKS) {
|
||||
log_debug("Active index value read is incorrect\n");
|
||||
ret = -EINVAL;
|
||||
@@ -457,30 +302,25 @@ int fwu_get_active_index(uint *active_idx)
|
||||
int fwu_set_active_index(uint active_idx)
|
||||
{
|
||||
int ret;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
struct fwu_mdata *mdata = &g_mdata;
|
||||
|
||||
if (active_idx >= CONFIG_FWU_NUM_BANKS) {
|
||||
log_debug("Invalid active index value\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/*
|
||||
* Update the active index and previous_active_index fields
|
||||
* in the FWU metadata
|
||||
*/
|
||||
mdata.previous_active_index = mdata.active_index;
|
||||
mdata.active_index = active_idx;
|
||||
mdata->previous_active_index = mdata->active_index;
|
||||
mdata->active_index = active_idx;
|
||||
|
||||
/*
|
||||
* Now write this updated FWU metadata to both the
|
||||
* FWU metadata partitions
|
||||
*/
|
||||
ret = fwu_update_mdata(dev, &mdata);
|
||||
ret = fwu_sync_mdata(mdata, BOTH_PARTS);
|
||||
if (ret) {
|
||||
log_debug("Failed to update FWU metadata partitions\n");
|
||||
ret = -EIO;
|
||||
@@ -510,15 +350,10 @@ int fwu_get_image_index(u8 *image_index)
|
||||
u8 alt_num;
|
||||
uint update_bank;
|
||||
efi_guid_t *image_guid, image_type_id;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
struct fwu_mdata *mdata = &g_mdata;
|
||||
struct fwu_image_entry *img_entry;
|
||||
struct fwu_image_bank_info *img_bank_info;
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = fwu_plat_get_update_index(&update_bank);
|
||||
if (ret) {
|
||||
log_debug("Failed to get the FWU update bank\n");
|
||||
@@ -539,11 +374,11 @@ int fwu_get_image_index(u8 *image_index)
|
||||
*/
|
||||
for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
|
||||
if (!guidcmp(&image_type_id,
|
||||
&mdata.img_entry[i].image_type_uuid)) {
|
||||
img_entry = &mdata.img_entry[i];
|
||||
&mdata->img_entry[i].image_type_uuid)) {
|
||||
img_entry = &mdata->img_entry[i];
|
||||
img_bank_info = &img_entry->img_bank_info[update_bank];
|
||||
image_guid = &img_bank_info->image_uuid;
|
||||
ret = fwu_plat_get_alt_num(dev, image_guid, &alt_num);
|
||||
ret = fwu_plat_get_alt_num(g_dev, image_guid, &alt_num);
|
||||
if (ret) {
|
||||
log_debug("alt_num not found for partition with GUID %pUs\n",
|
||||
image_guid);
|
||||
@@ -578,26 +413,21 @@ int fwu_revert_boot_index(void)
|
||||
{
|
||||
int ret;
|
||||
u32 cur_active_index;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
struct fwu_mdata *mdata = &g_mdata;
|
||||
|
||||
/*
|
||||
* Swap the active index and previous_active_index fields
|
||||
* in the FWU metadata
|
||||
*/
|
||||
cur_active_index = mdata.active_index;
|
||||
mdata.active_index = mdata.previous_active_index;
|
||||
mdata.previous_active_index = cur_active_index;
|
||||
cur_active_index = mdata->active_index;
|
||||
mdata->active_index = mdata->previous_active_index;
|
||||
mdata->previous_active_index = cur_active_index;
|
||||
|
||||
/*
|
||||
* Now write this updated FWU metadata to both the
|
||||
* FWU metadata partitions
|
||||
*/
|
||||
ret = fwu_update_mdata(dev, &mdata);
|
||||
ret = fwu_sync_mdata(mdata, BOTH_PARTS);
|
||||
if (ret) {
|
||||
log_debug("Failed to update FWU metadata partitions\n");
|
||||
ret = -EIO;
|
||||
@@ -624,16 +454,11 @@ int fwu_revert_boot_index(void)
|
||||
static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
|
||||
{
|
||||
int ret, i;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
struct fwu_mdata *mdata = &g_mdata;
|
||||
struct fwu_image_entry *img_entry;
|
||||
struct fwu_image_bank_info *img_bank_info;
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
img_entry = &mdata.img_entry[0];
|
||||
img_entry = &mdata->img_entry[0];
|
||||
for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
|
||||
if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) {
|
||||
img_bank_info = &img_entry[i].img_bank_info[bank];
|
||||
@@ -642,7 +467,7 @@ static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
|
||||
else
|
||||
img_bank_info->accepted = 0;
|
||||
|
||||
ret = fwu_update_mdata(dev, &mdata);
|
||||
ret = fwu_sync_mdata(mdata, BOTH_PARTS);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@@ -777,8 +602,6 @@ static int fwu_boottime_checks(void *ctx, struct event *event)
|
||||
{
|
||||
int ret;
|
||||
u32 boot_idx, active_idx;
|
||||
struct udevice *dev;
|
||||
struct fwu_mdata mdata = { 0 };
|
||||
|
||||
/* Don't have boot time checks on sandbox */
|
||||
if (IS_ENABLED(CONFIG_SANDBOX)) {
|
||||
@@ -786,9 +609,17 @@ static int fwu_boottime_checks(void *ctx, struct event *event)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = fwu_check_mdata_validity();
|
||||
if (ret)
|
||||
return 0;
|
||||
ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev);
|
||||
if (ret) {
|
||||
log_debug("Cannot find fwu device\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = fwu_get_verified_mdata(NULL);
|
||||
if (ret) {
|
||||
log_debug("Unable to read meta-data\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the Boot Index, i.e. the bank from
|
||||
@@ -824,11 +655,7 @@ static int fwu_boottime_checks(void *ctx, struct event *event)
|
||||
if (efi_init_obj_list() != EFI_SUCCESS)
|
||||
return 0;
|
||||
|
||||
ret = fwu_get_dev_mdata(&dev, &mdata);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
in_trial = in_trial_state(&mdata);
|
||||
in_trial = in_trial_state(&g_mdata);
|
||||
if (!in_trial || (ret = fwu_trial_count_update()) > 0)
|
||||
ret = trial_counter_update(NULL);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user