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:
Jassi Brar
2023-03-06 17:18:41 -06:00
committed by Tom Rini
parent 3733e16925
commit 246ec2a12c
5 changed files with 38 additions and 702 deletions

View File

@@ -14,7 +14,6 @@
#include <linux/errno.h>
#include <linux/types.h>
#include <u-boot/crc.h>
/**
* fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
@@ -50,170 +49,6 @@ int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary)
return ops->write_mdata(dev, mdata, primary);
}
/**
* fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
* @dev: FWU metadata device
* @mdata_parts: array for storing the metadata partition numbers
*
* Get the partition numbers on the storage device on which the
* FWU metadata is stored. Two partition numbers will be returned.
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_get_mdata_part_num(struct udevice *dev, uint *mdata_parts)
{
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->get_mdata_part_num) {
log_debug("get_mdata_part_num() method not defined\n");
return -ENOSYS;
}
return ops->get_mdata_part_num(dev, mdata_parts);
}
/**
* fwu_read_mdata_partition() - Read the FWU metadata from a partition
* @dev: FWU metadata device
* @mdata: Copy of the FWU metadata
* @part_num: Partition number from which FWU metadata is to be read
*
* Read the FWU metadata from the specified partition number
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_read_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
uint part_num)
{
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->read_mdata_partition) {
log_debug("read_mdata_partition() method not defined\n");
return -ENOSYS;
}
return ops->read_mdata_partition(dev, mdata, part_num);
}
/**
* fwu_write_mdata_partition() - Write the FWU metadata to a partition
* @dev: FWU metadata device
* @mdata: Copy of the FWU metadata
* @part_num: Partition number to which FWU metadata is to be written
*
* Write the FWU metadata to the specified partition number
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_write_mdata_partition(struct udevice *dev, struct fwu_mdata *mdata,
uint part_num)
{
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->write_mdata_partition) {
log_debug("write_mdata_partition() method not defined\n");
return -ENOSYS;
}
return ops->write_mdata_partition(dev, mdata, part_num);
}
/**
* fwu_mdata_check() - Check if the FWU metadata is valid
* @dev: FWU metadata device
*
* Validate both copies of the FWU metadata. If one of the copies
* has gone bad, restore it from the other copy.
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_mdata_check(struct udevice *dev)
{
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->check_mdata) {
log_debug("check_mdata() method not defined\n");
return -ENOSYS;
}
return ops->check_mdata(dev);
}
/**
* fwu_get_mdata() - Get a FWU metadata copy
* @dev: FWU metadata device
* @mdata: Copy of the FWU metadata
*
* Get a valid copy of the FWU metadata.
*
* Note: This function is to be called first when modifying any fields
* in the metadata. The sequence of calls to modify any field in the
* metadata would be 1) fwu_get_mdata 2) Modify metadata, followed by
* 3) fwu_update_mdata
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_get_mdata(struct udevice *dev, struct fwu_mdata *mdata)
{
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->get_mdata) {
log_debug("get_mdata() method not defined\n");
return -ENOSYS;
}
return ops->get_mdata(dev, mdata);
}
/**
* fwu_update_mdata() - Update the FWU metadata
* @dev: FWU metadata device
* @mdata: Copy of the FWU metadata
*
* Update the FWU metadata structure by writing to the
* FWU metadata partitions.
*
* Note: This function is not to be called directly to update the
* metadata fields. The sequence of function calls should be
* 1) fwu_get_mdata() 2) Modify the medata fields 3) fwu_update_mdata()
*
* The sequence of updating the partitions should be, update the
* primary metadata partition (first partition encountered), followed
* by updating the secondary partition. With this update sequence, in
* the rare scenario that the two metadata partitions are valid but do
* not match, maybe due to power outage at the time of updating the
* metadata copies, the secondary partition can be updated from the
* primary.
*
* Return: 0 if OK, -ve on error
*
*/
int fwu_update_mdata(struct udevice *dev, struct fwu_mdata *mdata)
{
void *buf;
const struct fwu_mdata_ops *ops = device_get_ops(dev);
if (!ops->update_mdata) {
log_debug("get_mdata() method not defined\n");
return -ENOSYS;
}
/*
* Calculate the crc32 for the updated FWU metadata
* and put the updated value in the FWU metadata crc32
* field
*/
buf = &mdata->version;
mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
return ops->update_mdata(dev, mdata);
}
UCLASS_DRIVER(fwu_mdata) = {
.id = UCLASS_FWU_MDATA,
.name = "fwu-mdata",

View File

@@ -28,7 +28,7 @@ static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */
static int gpt_get_mdata_partitions(struct blk_desc *desc)
{
int i, ret;
int i;
u32 nparts;
efi_guid_t part_type_guid;
struct disk_partition info;
@@ -52,12 +52,12 @@ static int gpt_get_mdata_partitions(struct blk_desc *desc)
if (nparts != 2) {
log_debug("Expect two copies of the FWU metadata instead of %d\n",
nparts);
ret = -EINVAL;
} else {
ret = 0;
g_mdata_part[0] = 0;
g_mdata_part[1] = 0;
return -EINVAL;
}
return ret;
return 0;
}
static int gpt_get_mdata_disk_part(struct blk_desc *desc,
@@ -125,115 +125,6 @@ static int gpt_read_write_mdata(struct blk_desc *desc,
return 0;
}
static int fwu_gpt_update_mdata(struct udevice *dev, struct fwu_mdata *mdata)
{
int ret;
struct blk_desc *desc;
struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
desc = dev_get_uclass_plat(priv->blk_dev);
ret = gpt_get_mdata_partitions(desc);
if (ret < 0) {
log_debug("Error getting the FWU metadata partitions\n");
return -ENOENT;
}
/* First write the primary partition */
ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, g_mdata_part[0]);
if (ret < 0) {
log_debug("Updating primary FWU metadata partition failed\n");
return ret;
}
/* And now the replica */
ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, g_mdata_part[1]);
if (ret < 0) {
log_debug("Updating secondary FWU metadata partition failed\n");
return ret;
}
return 0;
}
static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata *mdata)
{
int ret;
ret = gpt_get_mdata_partitions(desc);
if (ret < 0) {
log_debug("Error getting the FWU metadata partitions\n");
return -ENOENT;
}
ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, g_mdata_part[0]);
if (ret < 0) {
log_debug("Failed to read the FWU metadata from the device\n");
return -EIO;
}
ret = fwu_verify_mdata(mdata, 1);
if (!ret)
return 0;
/*
* Verification of the primary FWU metadata copy failed.
* Try to read the replica.
*/
memset(mdata, '\0', sizeof(struct fwu_mdata));
ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, g_mdata_part[1]);
if (ret < 0) {
log_debug("Failed to read the FWU metadata from the device\n");
return -EIO;
}
ret = fwu_verify_mdata(mdata, 0);
if (!ret)
return 0;
/* Both the FWU metadata copies are corrupted. */
return -EIO;
}
static int fwu_gpt_get_mdata(struct udevice *dev, struct fwu_mdata *mdata)
{
struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
return gpt_get_mdata(dev_get_uclass_plat(priv->blk_dev), mdata);
}
static int fwu_gpt_get_mdata_partitions(struct udevice *dev, uint *mdata_parts)
{
struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
int err;
err = gpt_get_mdata_partitions(dev_get_uclass_plat(priv->blk_dev));
if (!err) {
mdata_parts[0] = g_mdata_part[0];
mdata_parts[1] = g_mdata_part[1];
}
return err;
}
static int fwu_gpt_read_mdata_partition(struct udevice *dev,
struct fwu_mdata *mdata, uint part_num)
{
struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
return gpt_read_write_mdata(dev_get_uclass_plat(priv->blk_dev),
mdata, MDATA_READ, part_num);
}
static int fwu_gpt_write_mdata_partition(struct udevice *dev,
struct fwu_mdata *mdata, uint part_num)
{
struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
return gpt_read_write_mdata(dev_get_uclass_plat(priv->blk_dev),
mdata, MDATA_WRITE, part_num);
}
static int fwu_get_mdata_device(struct udevice *dev, struct udevice **mdata_dev)
{
u32 phandle;
@@ -309,11 +200,6 @@ static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
.read_mdata = fwu_gpt_read_mdata,
.write_mdata = fwu_gpt_write_mdata,
.get_mdata = fwu_gpt_get_mdata,
.update_mdata = fwu_gpt_update_mdata,
.get_mdata_part_num = fwu_gpt_get_mdata_partitions,
.read_mdata_partition = fwu_gpt_read_mdata_partition,
.write_mdata_partition = fwu_gpt_write_mdata_partition,
};
static const struct udevice_id fwu_mdata_ids[] = {