summaryrefslogtreecommitdiff
path: root/lib/fwu_updates
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fwu_updates')
-rw-r--r--lib/fwu_updates/Kconfig34
-rw-r--r--lib/fwu_updates/Makefile8
-rw-r--r--lib/fwu_updates/fwu.c677
-rw-r--r--lib/fwu_updates/fwu_gpt.c123
-rw-r--r--lib/fwu_updates/fwu_mtd.c187
5 files changed, 1029 insertions, 0 deletions
diff --git a/lib/fwu_updates/Kconfig b/lib/fwu_updates/Kconfig
new file mode 100644
index 00000000000..d35247d0e5d
--- /dev/null
+++ b/lib/fwu_updates/Kconfig
@@ -0,0 +1,34 @@
+menuconfig FWU_MULTI_BANK_UPDATE
+ bool "Enable FWU Multi Bank Update Feature"
+ depends on EFI_CAPSULE_ON_DISK
+ select PARTITION_TYPE_GUID
+ select FWU_MDATA
+ imply EFI_CAPSULE_ON_DISK_EARLY
+ select EVENT
+ help
+ Feature for updating firmware images on platforms having
+ multiple banks(copies) of the firmware images. One of the
+ bank is selected for updating all the firmware components
+
+if FWU_MULTI_BANK_UPDATE
+
+config FWU_NUM_BANKS
+ int "Number of Banks defined by the platform"
+ help
+ Define the number of banks of firmware images on a platform
+
+config FWU_NUM_IMAGES_PER_BANK
+ int "Number of firmware images per bank"
+ help
+ Define the number of firmware images per bank. This value
+ should be the same for all the banks.
+
+config FWU_TRIAL_STATE_CNT
+ int "Number of times system boots in Trial State"
+ default 3
+ help
+ With FWU Multi Bank Update feature enabled, number of times
+ the platform is allowed to boot in Trial State after an
+ update.
+
+endif
diff --git a/lib/fwu_updates/Makefile b/lib/fwu_updates/Makefile
new file mode 100644
index 00000000000..c9e3c06b489
--- /dev/null
+++ b/lib/fwu_updates/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_FWU_MULTI_BANK_UPDATE) += fwu.o
+obj-$(CONFIG_FWU_MDATA_GPT_BLK) += fwu_gpt.o
+obj-$(CONFIG_FWU_MDATA_MTD) += fwu_mtd.o
diff --git a/lib/fwu_updates/fwu.c b/lib/fwu_updates/fwu.c
new file mode 100644
index 00000000000..86518108c2d
--- /dev/null
+++ b/lib/fwu_updates/fwu.c
@@ -0,0 +1,677 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <dm.h>
+#include <efi.h>
+#include <efi_loader.h>
+#include <efi_variable.h>
+#include <event.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+
+#include <u-boot/crc.h>
+
+static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */
+static struct udevice *g_dev;
+static u8 in_trial;
+static u8 boottime_check;
+
+enum {
+ IMAGE_ACCEPT_SET = 1,
+ IMAGE_ACCEPT_CLEAR,
+};
+
+enum {
+ PRIMARY_PART = 1,
+ SECONDARY_PART,
+ BOTH_PARTS,
+};
+
+static int trial_counter_update(u16 *trial_state_ctr)
+{
+ bool delete;
+ u32 var_attr;
+ efi_status_t status;
+ efi_uintn_t var_size;
+
+ delete = !trial_state_ctr ? true : false;
+ var_size = !trial_state_ctr ? 0 : (efi_uintn_t)sizeof(*trial_state_ctr);
+ var_attr = !trial_state_ctr ? 0 : EFI_VARIABLE_NON_VOLATILE |
+ EFI_VARIABLE_BOOTSERVICE_ACCESS;
+ status = efi_set_variable_int(u"TrialStateCtr",
+ &efi_global_variable_guid,
+ var_attr,
+ var_size, trial_state_ctr, false);
+
+ if ((delete && (status != EFI_NOT_FOUND &&
+ status != EFI_SUCCESS)) ||
+ (!delete && status != EFI_SUCCESS))
+ return -1;
+
+ return 0;
+}
+
+static int trial_counter_read(u16 *trial_state_ctr)
+{
+ efi_status_t status;
+ efi_uintn_t var_size;
+
+ var_size = (efi_uintn_t)sizeof(trial_state_ctr);
+ status = efi_get_variable_int(u"TrialStateCtr",
+ &efi_global_variable_guid,
+ NULL,
+ &var_size, trial_state_ctr,
+ NULL);
+ if (status != EFI_SUCCESS) {
+ log_err("Unable to read TrialStateCtr variable\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int fwu_trial_count_update(void)
+{
+ int ret;
+ u16 trial_state_ctr;
+
+ ret = trial_counter_read(&trial_state_ctr);
+ if (ret) {
+ log_debug("Unable to read trial_state_ctr\n");
+ goto out;
+ }
+
+ ++trial_state_ctr;
+ if (trial_state_ctr > CONFIG_FWU_TRIAL_STATE_CNT) {
+ log_info("Trial State count exceeded. Revert back to previous_active_index\n");
+ ret = fwu_revert_boot_index();
+ if (ret)
+ log_err("Unable to revert active_index\n");
+ ret = 1;
+ } else {
+ log_info("Trial State count: attempt %d out of %d\n",
+ trial_state_ctr, CONFIG_FWU_TRIAL_STATE_CNT);
+ ret = trial_counter_update(&trial_state_ctr);
+ if (ret)
+ log_err("Unable to increment TrialStateCtr variable\n");
+ }
+
+out:
+ return ret;
+}
+
+static int in_trial_state(struct fwu_mdata *mdata)
+{
+ u32 i, active_bank;
+ struct fwu_image_entry *img_entry;
+ struct fwu_image_bank_info *img_bank_info;
+
+ active_bank = mdata->active_index;
+ img_entry = &mdata->img_entry[0];
+ for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
+ img_bank_info = &img_entry[i].img_bank_info[active_bank];
+ if (!img_bank_info->accepted) {
+ log_info("System booting in Trial State\n");
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id)
+{
+ int i;
+ struct efi_fw_image *image;
+
+ image = update_info.images;
+ for (i = 0; i < update_info.num_images; i++) {
+ if (image_index == image[i].image_index) {
+ guidcpy(image_type_id, &image[i].image_type_id);
+ return 0;
+ }
+ }
+
+ return -ENOENT;
+}
+
+/**
+ * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided
+ * @mdata: FWU metadata structure
+ * @part: Bitmask of FWU metadata partitions to be written to
+ *
+ * Return: 0 if OK, -ve on error
+ */
+static int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
+{
+ void *buf = &mdata->version;
+ int err;
+
+ if (part == BOTH_PARTS) {
+ err = fwu_sync_mdata(mdata, SECONDARY_PART);
+ if (err)
+ return err;
+ part = PRIMARY_PART;
+ }
+
+ /*
+ * Calculate the crc32 for the updated FWU metadata
+ * and put the updated value in the FWU metadata crc32
+ * field
+ */
+ mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+ err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART);
+ if (err) {
+ log_err("Unable to write %s mdata\n",
+ part == PRIMARY_PART ? "primary" : "secondary");
+ return err;
+ }
+
+ /* update the cached copy of meta-data */
+ memcpy(&g_mdata, mdata, sizeof(struct fwu_mdata));
+
+ return 0;
+}
+
+static inline int mdata_crc_check(struct fwu_mdata *mdata)
+{
+ void *buf = &mdata->version;
+ u32 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+ return calc_crc32 == mdata->crc32 ? 0 : -EINVAL;
+}
+
+/**
+ * fwu_get_mdata() - Read, verify and return the FWU metadata
+ * @mdata: Output FWU metadata read or NULL
+ *
+ * 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_get_mdata(struct fwu_mdata *mdata)
+{
+ int err;
+ bool parts_ok[2] = { false };
+ struct fwu_mdata s, *parts_mdata[2];
+
+ parts_mdata[0] = &g_mdata;
+ parts_mdata[1] = &s;
+
+ /* if mdata already read and ready */
+ err = mdata_crc_check(parts_mdata[0]);
+ if (!err)
+ goto ret_mdata;
+ /* else read, verify and, if needed, fix mdata */
+
+ for (int i = 0; i < 2; i++) {
+ parts_ok[i] = false;
+ err = fwu_read_mdata(g_dev, parts_mdata[i], !i);
+ if (!err) {
+ err = mdata_crc_check(parts_mdata[i]);
+ if (!err)
+ parts_ok[i] = true;
+ else
+ log_debug("mdata : %s crc32 failed\n", i ? "secondary" : "primary");
+ }
+ }
+
+ if (parts_ok[0] && parts_ok[1]) {
+ /*
+ * Before returning, check that both the
+ * FWU metadata copies are the same.
+ */
+ err = memcmp(parts_mdata[0], parts_mdata[1], sizeof(struct fwu_mdata));
+ if (!err)
+ goto ret_mdata;
+
+ /*
+ * If not, populate the secondary partition from the
+ * primary partition copy.
+ */
+ log_info("Both FWU metadata copies are valid but do not match.");
+ log_info(" Restoring the secondary partition from the primary\n");
+ parts_ok[1] = false;
+ }
+
+ for (int i = 0; i < 2; i++) {
+ if (parts_ok[i])
+ continue;
+
+ memcpy(parts_mdata[i], parts_mdata[1 - i], sizeof(struct fwu_mdata));
+ err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART);
+ if (err) {
+ log_debug("mdata : %s write failed\n", i ? "secondary" : "primary");
+ return err;
+ }
+ }
+
+ret_mdata:
+ if (!err && mdata)
+ memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata));
+
+ return err;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(uint *active_idx)
+{
+ 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;
+ if (*active_idx >= CONFIG_FWU_NUM_BANKS) {
+ log_debug("Active index value read is incorrect\n");
+ ret = -EINVAL;
+ }
+
+ return ret;
+}
+
+/**
+ * fwu_set_active_index() - Set active_index in the FWU metadata
+ * @active_idx: active_index value to be set
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_set_active_index(uint active_idx)
+{
+ int ret;
+ struct fwu_mdata *mdata = &g_mdata;
+
+ if (active_idx >= CONFIG_FWU_NUM_BANKS) {
+ log_debug("Invalid active index value\n");
+ return -EINVAL;
+ }
+
+ /*
+ * 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;
+
+ /*
+ * Now write this updated FWU metadata to both the
+ * FWU metadata partitions
+ */
+ ret = fwu_sync_mdata(mdata, BOTH_PARTS);
+ if (ret) {
+ log_debug("Failed to update FWU metadata partitions\n");
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * fwu_get_dfu_alt_num() - Get the dfu_alt_num to be used for capsule update
+ * @image_index: The Image Index for the image
+ * @alt_num: pointer to store dfu_alt_num
+ *
+ * Currently, the capsule update driver uses the DFU framework for
+ * the updates. This function gets the DFU alt number which is to
+ * be used for capsule update.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num)
+{
+ int ret, i;
+ uint update_bank;
+ efi_guid_t *image_guid, image_type_id;
+ struct fwu_mdata *mdata = &g_mdata;
+ struct fwu_image_entry *img_entry;
+ struct fwu_image_bank_info *img_bank_info;
+
+ ret = fwu_plat_get_update_index(&update_bank);
+ if (ret) {
+ log_debug("Failed to get the FWU update bank\n");
+ goto out;
+ }
+
+ ret = fwu_get_image_type_id(image_index, &image_type_id);
+ if (ret) {
+ log_debug("Unable to get image_type_id for image_index %u\n",
+ image_index);
+ goto out;
+ }
+
+ ret = -EINVAL;
+ /*
+ * The FWU metadata has been read. Now get the image_uuid for the
+ * image with the update_bank.
+ */
+ 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];
+ img_bank_info = &img_entry->img_bank_info[update_bank];
+ image_guid = &img_bank_info->image_uuid;
+ 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);
+ else
+ log_debug("alt_num %d for partition %pUs\n",
+ *alt_num, image_guid);
+
+ goto out;
+ }
+ }
+
+ log_err("Partition with the image type %pUs not found\n",
+ &image_type_id);
+
+out:
+ return ret;
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+ int ret;
+ u32 cur_active_index;
+ 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;
+
+ /*
+ * Now write this updated FWU metadata to both the
+ * FWU metadata partitions
+ */
+ ret = fwu_sync_mdata(mdata, BOTH_PARTS);
+ if (ret) {
+ log_debug("Failed to update FWU metadata partitions\n");
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: GUID of the image type for which the accepted bit is to be
+ * set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ * cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
+{
+ int ret, i;
+ struct fwu_mdata *mdata = &g_mdata;
+ struct fwu_image_entry *img_entry;
+ struct fwu_image_bank_info *img_bank_info;
+
+ 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];
+ if (action == IMAGE_ACCEPT_SET)
+ img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
+ else
+ img_bank_info->accepted = 0;
+
+ ret = fwu_sync_mdata(mdata, BOTH_PARTS);
+ goto out;
+ }
+ }
+
+ /* Image not found */
+ ret = -ENOENT;
+
+out:
+ return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: GUID of the image type for which the accepted bit is to be
+ * cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+ return fwu_clrset_image_accept(img_type_id, bank,
+ IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: GUID of the image type for which the accepted bit is to be
+ * cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+ return fwu_clrset_image_accept(img_type_id, bank,
+ IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_plat_get_update_index() - Get the value of the update bank
+ * @update_idx: Bank number to which images are to be updated
+ *
+ * Get the value of the bank(partition) to which the update needs to be
+ * made.
+ *
+ * Note: This is a weak function and platforms can override this with
+ * their own implementation for selection of the update bank.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+__weak int fwu_plat_get_update_index(uint *update_idx)
+{
+ int ret;
+ u32 active_idx;
+
+ ret = fwu_get_active_index(&active_idx);
+ if (ret < 0)
+ return -1;
+
+ *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS;
+
+ return ret;
+}
+
+/**
+ * fwu_plat_get_bootidx() - Get the value of the boot index
+ * @boot_idx: Boot index value
+ *
+ * Get the value of the bank(partition) from which the platform
+ * has booted. This value is passed to U-Boot from the earlier
+ * stage bootloader which loads and boots all the relevant
+ * firmware images
+ */
+__weak void fwu_plat_get_bootidx(uint *boot_idx)
+{
+ int ret;
+
+ ret = fwu_get_active_index(boot_idx);
+ if (ret < 0)
+ *boot_idx = 0; /* Dummy value */
+}
+
+/**
+ * fwu_update_checks_pass() - Check if FWU update can be done
+ *
+ * Check if the FWU update can be executed. The updates are
+ * allowed only when the platform is not in Trial State and
+ * the boot time checks have passed
+ *
+ * Return: 1 if OK, 0 if checks do not pass
+ *
+ */
+u8 fwu_update_checks_pass(void)
+{
+ return !in_trial && boottime_check;
+}
+
+/**
+ * fwu_empty_capsule_checks_pass() - Check if empty capsule can be processed
+ *
+ * Check if the empty capsule can be processed to either accept or revert
+ * an earlier executed update. The empty capsules need to be processed
+ * only when the platform is in Trial State and the boot time checks have
+ * passed
+ *
+ * Return: 1 if OK, 0 if not to be allowed
+ *
+ */
+u8 fwu_empty_capsule_checks_pass(void)
+{
+ return in_trial && boottime_check;
+}
+
+/**
+ * fwu_trial_state_ctr_start() - Start the Trial State counter
+ *
+ * Start the counter to identify the platform booting in the
+ * Trial State. The counter is implemented as an EFI variable.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_trial_state_ctr_start(void)
+{
+ int ret;
+ u16 trial_state_ctr;
+
+ trial_state_ctr = 0;
+ ret = trial_counter_update(&trial_state_ctr);
+ if (ret)
+ log_err("Unable to initialise TrialStateCtr\n");
+
+ return ret;
+}
+
+static int fwu_boottime_checks(void)
+{
+ int ret;
+ u32 boot_idx, active_idx;
+
+ ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev);
+ if (ret) {
+ log_debug("Cannot find fwu device\n");
+ return ret;
+ }
+
+ /* Don't have boot time checks on sandbox */
+ if (IS_ENABLED(CONFIG_SANDBOX)) {
+ boottime_check = 1;
+ return 0;
+ }
+
+ ret = fwu_get_mdata(NULL);
+ if (ret) {
+ log_debug("Unable to read meta-data\n");
+ return ret;
+ }
+
+ /*
+ * Get the Boot Index, i.e. the bank from
+ * which the platform has booted. This value
+ * gets passed from the ealier stage bootloader
+ * which booted u-boot, e.g. tf-a. If the
+ * boot index is not the same as the
+ * active_index read from the FWU metadata,
+ * update the active_index.
+ */
+ fwu_plat_get_bootidx(&boot_idx);
+ if (boot_idx >= CONFIG_FWU_NUM_BANKS) {
+ log_err("Received incorrect value of boot_index\n");
+ return 0;
+ }
+
+ ret = fwu_get_active_index(&active_idx);
+ if (ret) {
+ log_err("Unable to read active_index\n");
+ return 0;
+ }
+
+ if (boot_idx != active_idx) {
+ log_info("Boot idx %u is not matching active idx %u, changing active_idx\n",
+ boot_idx, active_idx);
+ ret = fwu_set_active_index(boot_idx);
+ if (!ret)
+ boottime_check = 1;
+ }
+
+ if (efi_init_obj_list() != EFI_SUCCESS)
+ return 0;
+
+ in_trial = in_trial_state(&g_mdata);
+ if (!in_trial || (ret = fwu_trial_count_update()) > 0)
+ ret = trial_counter_update(NULL);
+
+ if (!ret)
+ boottime_check = 1;
+
+ return 0;
+}
+EVENT_SPY_SIMPLE(EVT_MAIN_LOOP, fwu_boottime_checks);
diff --git a/lib/fwu_updates/fwu_gpt.c b/lib/fwu_updates/fwu_gpt.c
new file mode 100644
index 00000000000..21a573c9342
--- /dev/null
+++ b/lib/fwu_updates/fwu_gpt.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <blk.h>
+#include <dfu.h>
+#include <efi.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <log.h>
+#include <part.h>
+
+#include <linux/errno.h>
+
+static int get_gpt_dfu_identifier(struct blk_desc *desc, efi_guid_t *image_guid)
+{
+ int i;
+ struct disk_partition info;
+ efi_guid_t unique_part_guid;
+
+ for (i = 1; i < MAX_SEARCH_PARTITIONS; i++) {
+ if (part_get_info(desc, i, &info))
+ continue;
+ uuid_str_to_bin(info.uuid, unique_part_guid.b,
+ UUID_STR_FORMAT_GUID);
+
+ if (!guidcmp(&unique_part_guid, image_guid))
+ return i;
+ }
+
+ log_err("No partition found with image_guid %pUs\n", image_guid);
+ return -ENOENT;
+}
+
+static int fwu_alt_num_for_dfu_dev(struct dfu_entity *dfu, int dev_num,
+ int part, unsigned char dfu_dev,
+ u8 *alt_num)
+{
+ int ret;
+
+ switch(dfu_dev) {
+ case DFU_DEV_MMC:
+ if (dfu->layout == DFU_RAW_ADDR &&
+ dfu->data.mmc.dev_num == dev_num &&
+ dfu->data.mmc.part == part) {
+ *alt_num = dfu->alt;
+ ret = 0;
+ } else {
+ ret = -ENOENT;
+ }
+ break;
+ default:
+ ret = -ENOENT;
+ }
+
+ return ret;
+}
+
+static int fwu_gpt_get_alt_num(struct blk_desc *desc, efi_guid_t *image_guid,
+ u8 *alt_num, unsigned char dfu_dev)
+{
+ int ret = -1;
+ int i, part, dev_num;
+ struct dfu_entity *dfu;
+
+ dev_num = desc->devnum;
+ part = get_gpt_dfu_identifier(desc, image_guid);
+ if (part < 0)
+ return -ENOENT;
+
+ ret = dfu_init_env_entities(NULL, NULL);
+ if (ret)
+ goto out;
+
+ i = 0;
+ while (true) {
+ dfu = dfu_get_entity(i++);
+ if (!dfu) {
+ ret = -ENOENT;
+ break;
+ }
+
+ if (dfu->dev_type != dfu_dev)
+ continue;
+
+ ret = fwu_alt_num_for_dfu_dev(dfu, dev_num, part, dfu_dev,
+ alt_num);
+ if (!ret)
+ break;
+ }
+
+out:
+ dfu_free_entities();
+
+ return ret;
+}
+
+/**
+ * fwu_plat_get_alt_num() - Get the DFU alt number
+ * @dev: FWU metadata device
+ * @image_guid: GUID value of the image for which the alt num is to
+ * be obtained
+ * @alt_num: The DFU alt number for the image that is to be updated
+ *
+ * Get the DFU alt number for the image that is to be updated. The
+ * image is identified with the image_guid parameter that is passed
+ * to the function.
+ *
+ * Note: This is a weak function and platforms can override this with
+ * their own implementation for obtaining the alt number value.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+__weak int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid,
+ u8 *alt_num)
+{
+ struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+
+ return fwu_gpt_get_alt_num(dev_get_uclass_plat(priv->blk_dev),
+ image_guid, alt_num, DFU_DEV_MMC);
+}
diff --git a/lib/fwu_updates/fwu_mtd.c b/lib/fwu_updates/fwu_mtd.c
new file mode 100644
index 00000000000..69cd3d7001f
--- /dev/null
+++ b/lib/fwu_updates/fwu_mtd.c
@@ -0,0 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023, Linaro Limited
+ */
+
+#include <dm.h>
+#include <dfu.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+#include <mtd.h>
+#include <uuid.h>
+#include <vsprintf.h>
+
+#include <dm/ofnode.h>
+
+struct fwu_mtd_image_info
+fwu_mtd_images[CONFIG_FWU_NUM_BANKS * CONFIG_FWU_NUM_IMAGES_PER_BANK];
+
+static struct fwu_mtd_image_info *mtd_img_by_uuid(const char *uuidbuf)
+{
+ int num_images = ARRAY_SIZE(fwu_mtd_images);
+
+ for (int i = 0; i < num_images; i++)
+ if (!strcmp(uuidbuf, fwu_mtd_images[i].uuidbuf))
+ return &fwu_mtd_images[i];
+
+ return NULL;
+}
+
+int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num,
+ const char *mtd_dev)
+{
+ struct fwu_mtd_image_info *mtd_img_info;
+ char uuidbuf[UUID_STR_LEN + 1];
+ fdt_addr_t offset, size = 0;
+ struct dfu_entity *dfu;
+ int i, nalt, ret;
+
+ mtd_probe_devices();
+
+ uuid_bin_to_str(image_id->b, uuidbuf, UUID_STR_FORMAT_STD);
+
+ mtd_img_info = mtd_img_by_uuid(uuidbuf);
+ if (!mtd_img_info) {
+ log_err("%s: Not found partition for image %s\n", __func__, uuidbuf);
+ return -ENOENT;
+ }
+
+ offset = mtd_img_info->start;
+ size = mtd_img_info->size;
+
+ ret = dfu_init_env_entities(NULL, NULL);
+ if (ret)
+ return -ENOENT;
+
+ nalt = 0;
+ list_for_each_entry(dfu, &dfu_list, list)
+ nalt++;
+
+ if (!nalt) {
+ log_warning("No entities in dfu_alt_info\n");
+ dfu_free_entities();
+ return -ENOENT;
+ }
+
+ ret = -ENOENT;
+ for (i = 0; i < nalt; i++) {
+ dfu = dfu_get_entity(i);
+
+ /* Only MTD RAW access */
+ if (!dfu || dfu->dev_type != DFU_DEV_MTD ||
+ dfu->layout != DFU_RAW_ADDR ||
+ dfu->data.mtd.start != offset ||
+ dfu->data.mtd.size != size)
+ continue;
+
+ *alt_num = dfu->alt;
+ ret = 0;
+ break;
+ }
+
+ dfu_free_entities();
+
+ log_debug("%s: %s -> %d\n", __func__, uuidbuf, *alt_num);
+ return ret;
+}
+
+/**
+ * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform
+ * @dev: FWU device
+ * @image_id: Image GUID for which DFU alt number needs to be retrieved
+ * @alt_num: Pointer to the alt_num
+ *
+ * Get the DFU alt number from the platform for the image specified by the
+ * image GUID.
+ *
+ * Note: This is a weak function and platforms can override this with
+ * their own implementation for obtaining the alt number value.
+ *
+ * Return: 0 if OK, -ve on error
+ */
+__weak int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_id,
+ u8 *alt_num)
+{
+ return fwu_mtd_get_alt_num(image_id, alt_num, "nor1");
+}
+
+static int gen_image_alt_info(char *buf, size_t len, int sidx,
+ struct fwu_image_entry *img, struct mtd_info *mtd)
+{
+ char *p = buf, *end = buf + len;
+ int i;
+
+ p += snprintf(p, end - p, "mtd %s", mtd->name);
+ if (end < p) {
+ log_err("%s:%d Run out of buffer\n", __func__, __LINE__);
+ return -E2BIG;
+ }
+
+ /*
+ * List the image banks in the FWU mdata and search the corresponding
+ * partition based on partition's uuid.
+ */
+ for (i = 0; i < CONFIG_FWU_NUM_BANKS; i++) {
+ struct fwu_mtd_image_info *mtd_img_info;
+ struct fwu_image_bank_info *bank;
+ char uuidbuf[UUID_STR_LEN + 1];
+ u32 offset, size;
+
+ /* Query a partition by image UUID */
+ bank = &img->img_bank_info[i];
+ uuid_bin_to_str(bank->image_uuid.b, uuidbuf, UUID_STR_FORMAT_STD);
+
+ mtd_img_info = mtd_img_by_uuid(uuidbuf);
+ if (!mtd_img_info) {
+ log_err("%s: Not found partition for image %s\n", __func__, uuidbuf);
+ break;
+ }
+
+ offset = mtd_img_info->start;
+ size = mtd_img_info->size;
+
+ p += snprintf(p, end - p, "%sbank%d raw %x %x",
+ i == 0 ? "=" : ";", i, offset, size);
+ if (end < p) {
+ log_err("%s:%d Run out of buffer\n", __func__, __LINE__);
+ return -E2BIG;
+ }
+ }
+
+ if (i == CONFIG_FWU_NUM_BANKS)
+ return 0;
+
+ return -ENOENT;
+}
+
+int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd)
+{
+ struct fwu_mdata mdata;
+ int i, l, ret;
+
+ ret = fwu_get_mdata(&mdata);
+ if (ret < 0) {
+ log_err("Failed to get the FWU mdata.\n");
+ return ret;
+ }
+
+ for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
+ ret = gen_image_alt_info(buf, len, i * CONFIG_FWU_NUM_BANKS,
+ &mdata.img_entry[i], mtd);
+ if (ret)
+ break;
+
+ l = strlen(buf);
+ /* Replace the last ';' with '&' if there is another image. */
+ if (i != CONFIG_FWU_NUM_IMAGES_PER_BANK - 1 && l) {
+ buf[l] = '&';
+ buf++;
+ }
+ len -= l;
+ buf += l;
+ }
+
+ return ret;
+}