diff mbox series

[v4,3/4] mach-snapdragon: implement capsule update support

Message ID 20241012-b4-qcom-capsule-updates-v4-3-abd05f15255f@linaro.org
State New
Delegated to: Caleb Connolly
Headers show
Series qcom: implement capsule updates | expand

Commit Message

Caleb Connolly Oct. 12, 2024, 1:57 p.m. UTC
Qualcomm boards flash U-Boot a variety of partitions, implement support
for determining which slot U-Boot is running from, finding the correct
partition for that slot and configuring the appropriate DFU string.

Initially, we only support the RB3 Gen 2 where U-Boot is flashed to the
UEFI partition, and ignore handling of slots. In the future we will
additionally support booting U-Boot from other partitions (e.g. boot)
and correct handling for A/B.

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
---
 arch/arm/mach-snapdragon/Kconfig          |   3 +
 arch/arm/mach-snapdragon/Makefile         |   1 +
 arch/arm/mach-snapdragon/board.c          |   3 +
 arch/arm/mach-snapdragon/capsule_update.c | 153 ++++++++++++++++++++++++++++++
 arch/arm/mach-snapdragon/qcom-priv.h      |   6 ++
 include/configs/qcom.h                    |   5 +
 6 files changed, 171 insertions(+)

Comments

Neil Armstrong Oct. 14, 2024, 6:53 a.m. UTC | #1
On 12/10/2024 15:57, Caleb Connolly wrote:
> Qualcomm boards flash U-Boot a variety of partitions, implement support
> for determining which slot U-Boot is running from, finding the correct
> partition for that slot and configuring the appropriate DFU string.
> 
> Initially, we only support the RB3 Gen 2 where U-Boot is flashed to the
> UEFI partition, and ignore handling of slots. In the future we will
> additionally support booting U-Boot from other partitions (e.g. boot)
> and correct handling for A/B.
> 
> Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
> ---
>   arch/arm/mach-snapdragon/Kconfig          |   3 +
>   arch/arm/mach-snapdragon/Makefile         |   1 +
>   arch/arm/mach-snapdragon/board.c          |   3 +
>   arch/arm/mach-snapdragon/capsule_update.c | 153 ++++++++++++++++++++++++++++++
>   arch/arm/mach-snapdragon/qcom-priv.h      |   6 ++
>   include/configs/qcom.h                    |   5 +
>   6 files changed, 171 insertions(+)
> 
> diff --git a/arch/arm/mach-snapdragon/Kconfig b/arch/arm/mach-snapdragon/Kconfig
> index 536960b83c3b..f82cd0d32915 100644
> --- a/arch/arm/mach-snapdragon/Kconfig
> +++ b/arch/arm/mach-snapdragon/Kconfig
> @@ -19,8 +19,11 @@ config SPL_SYS_MALLOC_F
>   
>   config SPL_SYS_MALLOC_F_LEN
>   	default 0x2000
>   
> +config SYS_MALLOC_LEN
> +	default 0x800000
> +
>   config LNX_KRNL_IMG_TEXT_OFFSET_BASE
>   	default 0x80000000
>   
>   config SYS_BOARD
> diff --git a/arch/arm/mach-snapdragon/Makefile b/arch/arm/mach-snapdragon/Makefile
> index 7a4495c8108f..343e825c6fdd 100644
> --- a/arch/arm/mach-snapdragon/Makefile
> +++ b/arch/arm/mach-snapdragon/Makefile
> @@ -2,5 +2,6 @@
>   #
>   # (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
>   
>   obj-y += board.o
> +obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += capsule_update.o
>   obj-$(CONFIG_OF_LIVE) += of_fixup.o
> diff --git a/arch/arm/mach-snapdragon/board.c b/arch/arm/mach-snapdragon/board.c
> index 2ab2ceb51389..fbb6cf7c6599 100644
> --- a/arch/arm/mach-snapdragon/board.c
> +++ b/arch/arm/mach-snapdragon/board.c
> @@ -447,8 +447,11 @@ int board_late_init(void)
>   
>   	configure_env();
>   	qcom_late_init();
>   
> +	/* Configure the dfu_string for capsule updates */
> +	qcom_configure_capsule_updates();
> +
>   	return 0;
>   }
>   
>   static void build_mem_map(void)
> diff --git a/arch/arm/mach-snapdragon/capsule_update.c b/arch/arm/mach-snapdragon/capsule_update.c
> new file mode 100644
> index 000000000000..bf75a9a1b24c
> --- /dev/null
> +++ b/arch/arm/mach-snapdragon/capsule_update.c
> @@ -0,0 +1,153 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Capsule update support for Qualcomm boards.
> + *
> + * Copyright (c) 2024 Linaro Ltd.
> + * Author: Caleb Connolly <caleb.connolly@linaro.org>
> + */
> +
> +#define pr_fmt(fmt) "QCOM-FMP: " fmt
> +
> +#include <dm/device.h>
> +#include <dm/uclass.h>
> +#include <efi.h>
> +#include <efi_loader.h>
> +#include <malloc.h>
> +#include <scsi.h>
> +#include <part.h>
> +#include <linux/err.h>
> +
> +#include "qcom-priv.h"
> +
> +/*
> + * NOTE: for now this implementation only supports the rb3gen2. Supporting other
> + * boards that boot in different ways (e.g. chainloaded from ABL) will require
> + * additional complexity to properly create the dfu string and fw_images array.
> + */
> +
> +/*
> + * To handle different variants like chainloaded U-Boot here we'll need to
> + * build the fw_images array dynamically at runtime. It looks like
> + * mach-rockchip is a good example for how to do this.
> + * Detecting which image types a board uses is TBD, hence for now we only
> + * support the one new board that runs U-Boot as its primary bootloader.
> + */
> +struct efi_fw_image fw_images[] = {
> +	{
> +		/* U-Boot flashed to the uefi_X partition (e.g. rb3gen2) */
> +		.fw_name = u"UBOOT_UEFI_PARTITION",
> +		.image_index = 1,
> +	},
> +};
> +
> +struct efi_capsule_update_info update_info = {
> +	/* Filled in by configure_dfu_string() */
> +	.dfu_string = NULL,
> +	.num_images = ARRAY_SIZE(fw_images),
> +	.images = fw_images,
> +};
> +
> +/* LSB first */
> +struct part_slot_status {
> +	u16: 2;
> +	u16 active : 1;
> +	u16: 3;
> +	u16 successful : 1;
> +	u16 unbootable : 1;
> +	u16 tries_remaining : 4;
> +};
> +
> +static int find_boot_partition(const char *partname, struct blk_desc *blk_dev, char *name)
> +{
> +	int ret;
> +	int partnum;
> +	struct disk_partition info;
> +	struct part_slot_status *slot_status;
> +
> +	for (partnum = 1;; partnum++) {
> +		ret = part_get_info(blk_dev, partnum, &info);
> +		if (ret)
> +			return ret;
> +
> +		slot_status = (struct part_slot_status *)&info.type_flags;
> +		log_io("%16s: Active: %1d, Successful: %1d, Unbootable: %1d, Tries left: %1d\n",
> +		       info.name, slot_status->active,
> +		       slot_status->successful, slot_status->unbootable,
> +		       slot_status->tries_remaining);
> +		/*
> +		 * FIXME: eventually we'll want to find the active/inactive variant of the partition
> +		 * but on the rb3gen2 these values might all be 0
> +		 */
> +		if (!strncmp(info.name, partname, strlen(partname))) {
> +			log_debug("Found active %s partition: '%s'!\n", partname, info.name);
> +			strlcpy(name, info.name, sizeof(info.name));
> +			return partnum;
> +		}
> +	}
> +
> +	return -1;
> +}
> +
> +/**
> + * qcom_configure_capsule_updates() - Configure the DFU string for capsule updates
> + *
> + * U-Boot is flashed to the boot partition on Qualcomm boards. In most cases there
> + * are two boot partitions, boot_a and boot_b. As we don't currently support doing
> + * full A/B updates, we only support updating the currently active boot partition.
> + *
> + * So we need to find the current slot suffix and the associated boot partition.
> + * We do this by looking for the boot partition that has the 'active' flag set
> + * in the GPT partition vendor attribute bits.
> + */
> +void qcom_configure_capsule_updates(void)
> +{
> +	struct blk_desc *desc;
> +	int ret = 0, partnum = -1, devnum;
> +	static char dfu_string[32] = { 0 };
> +	char name[32]; /* GPT partition name */
> +	char *partname = "uefi_a";

NIT

Could this be moved to a CONFIG option ? Only the new non-mobile platforms have this partition,
we could switch it to boot_a for the mobile platforms with the right config option.

No need to resend for that

Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org>


> +	struct udevice *dev = NULL;
> +
> +	if (IS_ENABLED(CONFIG_SCSI)) {
> +		/* Scan for SCSI devices */
> +		ret = scsi_scan(false);
> +		if (ret) {
> +			debug("Failed to scan SCSI devices: %d\n", ret);
> +			return;
> +		}
> +	}
> +
> +	uclass_foreach_dev_probe(UCLASS_BLK, dev) {
> +		if (device_get_uclass_id(dev) != UCLASS_BLK)
> +			continue;
> +
> +		desc = dev_get_uclass_plat(dev);
> +		if (!desc || desc->part_type == PART_TYPE_UNKNOWN)
> +			continue;
> +		devnum = desc->devnum;
> +		partnum = find_boot_partition(partname, desc,
> +					      name);
> +		if (partnum >= 0)
> +			break;
> +	}
> +
> +	if (partnum < 0) {
> +		log_err("Failed to find boot partition\n");
> +		return;
> +	}
> +
> +	switch (desc->uclass_id) {
> +	case UCLASS_SCSI:
> +		snprintf(dfu_string, 32, "scsi %d=u-boot.bin part %d", devnum, partnum);
> +		break;
> +	case UCLASS_MMC:
> +		snprintf(dfu_string, 32, "mmc 0=u-boot.bin part %d %d", devnum, partnum);
> +		break;
> +	default:
> +		debug("Unsupported storage uclass: %d\n", desc->uclass_id);
> +		return;
> +	}
> +	log_debug("boot partition is %s, DFU string: '%s'\n", name, dfu_string);
> +
> +	update_info.dfu_string = dfu_string;
> +}
> diff --git a/arch/arm/mach-snapdragon/qcom-priv.h b/arch/arm/mach-snapdragon/qcom-priv.h
> index 0a7ed5eff8b8..74d39197b89f 100644
> --- a/arch/arm/mach-snapdragon/qcom-priv.h
> +++ b/arch/arm/mach-snapdragon/qcom-priv.h
> @@ -2,8 +2,14 @@
>   
>   #ifndef __QCOM_PRIV_H__
>   #define __QCOM_PRIV_H__
>   
> +#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
> +void qcom_configure_capsule_updates(void);
> +#else
> +void qcom_configure_capsule_updates(void) {}
> +#endif /* EFI_HAVE_CAPSULE_SUPPORT */
> +
>   #if CONFIG_IS_ENABLED(OF_LIVE)
>   /**
>    * qcom_of_fixup_nodes() - Fixup Qualcomm DT nodes
>    *
> diff --git a/include/configs/qcom.h b/include/configs/qcom.h
> index 5b5ebbd844df..9b41ab9e982b 100644
> --- a/include/configs/qcom.h
> +++ b/include/configs/qcom.h
> @@ -10,5 +10,10 @@
>   #define __CONFIGS_SNAPDRAGON_H
>   
>   #define CFG_SYS_BAUDRATE_TABLE	{ 115200, 230400, 460800, 921600 }
>   
> +// 2a5aa852-b856-4d97-baa9-5c5f4421551f
> +#define QUALCOMM_UBOOT_BOOT_IMAGE_GUID \
> +	EFI_GUID(0x2a5aa852, 0xb856, 0x4d97, 0xba, 0xa9, \
> +		0x5c, 0x5f, 0x44, 0x21, 0x55, 0x1f)
> +
>   #endif
>
Ilias Apalodimas Oct. 14, 2024, 9:02 a.m. UTC | #2
On Sat, 12 Oct 2024 at 16:57, Caleb Connolly <caleb.connolly@linaro.org> wrote:
>
> Qualcomm boards flash U-Boot a variety of partitions, implement support
> for determining which slot U-Boot is running from, finding the correct
> partition for that slot and configuring the appropriate DFU string.
>
> Initially, we only support the RB3 Gen 2 where U-Boot is flashed to the
> UEFI partition, and ignore handling of slots. In the future we will
> additionally support booting U-Boot from other partitions (e.g. boot)
> and correct handling for A/B.
>
> Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
> ---
>  arch/arm/mach-snapdragon/Kconfig          |   3 +
>  arch/arm/mach-snapdragon/Makefile         |   1 +
>  arch/arm/mach-snapdragon/board.c          |   3 +
>  arch/arm/mach-snapdragon/capsule_update.c | 153 ++++++++++++++++++++++++++++++
>  arch/arm/mach-snapdragon/qcom-priv.h      |   6 ++
>  include/configs/qcom.h                    |   5 +
>  6 files changed, 171 insertions(+)
>
> diff --git a/arch/arm/mach-snapdragon/Kconfig b/arch/arm/mach-snapdragon/Kconfig
> index 536960b83c3b..f82cd0d32915 100644
> --- a/arch/arm/mach-snapdragon/Kconfig
> +++ b/arch/arm/mach-snapdragon/Kconfig
> @@ -19,8 +19,11 @@ config SPL_SYS_MALLOC_F
>
>  config SPL_SYS_MALLOC_F_LEN
>         default 0x2000
>
> +config SYS_MALLOC_LEN
> +       default 0x800000
> +
>  config LNX_KRNL_IMG_TEXT_OFFSET_BASE
>         default 0x80000000
>
>  config SYS_BOARD
> diff --git a/arch/arm/mach-snapdragon/Makefile b/arch/arm/mach-snapdragon/Makefile
> index 7a4495c8108f..343e825c6fdd 100644
> --- a/arch/arm/mach-snapdragon/Makefile
> +++ b/arch/arm/mach-snapdragon/Makefile
> @@ -2,5 +2,6 @@
>  #
>  # (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
>
>  obj-y += board.o
> +obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += capsule_update.o
>  obj-$(CONFIG_OF_LIVE) += of_fixup.o
> diff --git a/arch/arm/mach-snapdragon/board.c b/arch/arm/mach-snapdragon/board.c
> index 2ab2ceb51389..fbb6cf7c6599 100644
> --- a/arch/arm/mach-snapdragon/board.c
> +++ b/arch/arm/mach-snapdragon/board.c
> @@ -447,8 +447,11 @@ int board_late_init(void)
>
>         configure_env();
>         qcom_late_init();
>
> +       /* Configure the dfu_string for capsule updates */
> +       qcom_configure_capsule_updates();
> +
>         return 0;
>  }
>
>  static void build_mem_map(void)
> diff --git a/arch/arm/mach-snapdragon/capsule_update.c b/arch/arm/mach-snapdragon/capsule_update.c
> new file mode 100644
> index 000000000000..bf75a9a1b24c
> --- /dev/null
> +++ b/arch/arm/mach-snapdragon/capsule_update.c
> @@ -0,0 +1,153 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Capsule update support for Qualcomm boards.
> + *
> + * Copyright (c) 2024 Linaro Ltd.
> + * Author: Caleb Connolly <caleb.connolly@linaro.org>
> + */
> +
> +#define pr_fmt(fmt) "QCOM-FMP: " fmt
> +
> +#include <dm/device.h>
> +#include <dm/uclass.h>
> +#include <efi.h>
> +#include <efi_loader.h>
> +#include <malloc.h>
> +#include <scsi.h>
> +#include <part.h>
> +#include <linux/err.h>
> +
> +#include "qcom-priv.h"
> +
> +/*
> + * NOTE: for now this implementation only supports the rb3gen2. Supporting other
> + * boards that boot in different ways (e.g. chainloaded from ABL) will require
> + * additional complexity to properly create the dfu string and fw_images array.
> + */
> +
> +/*
> + * To handle different variants like chainloaded U-Boot here we'll need to
> + * build the fw_images array dynamically at runtime. It looks like
> + * mach-rockchip is a good example for how to do this.
> + * Detecting which image types a board uses is TBD, hence for now we only
> + * support the one new board that runs U-Boot as its primary bootloader.
> + */
> +struct efi_fw_image fw_images[] = {
> +       {
> +               /* U-Boot flashed to the uefi_X partition (e.g. rb3gen2) */
> +               .fw_name = u"UBOOT_UEFI_PARTITION",
> +               .image_index = 1,
> +       },
> +};
> +
> +struct efi_capsule_update_info update_info = {
> +       /* Filled in by configure_dfu_string() */
> +       .dfu_string = NULL,
> +       .num_images = ARRAY_SIZE(fw_images),
> +       .images = fw_images,
> +};
> +
> +/* LSB first */
> +struct part_slot_status {
> +       u16: 2;
> +       u16 active : 1;
> +       u16: 3;
> +       u16 successful : 1;
> +       u16 unbootable : 1;
> +       u16 tries_remaining : 4;
> +};
> +
> +static int find_boot_partition(const char *partname, struct blk_desc *blk_dev, char *name)
> +{
> +       int ret;
> +       int partnum;
> +       struct disk_partition info;
> +       struct part_slot_status *slot_status;
> +
> +       for (partnum = 1;; partnum++) {
> +               ret = part_get_info(blk_dev, partnum, &info);
> +               if (ret)
> +                       return ret;
> +
> +               slot_status = (struct part_slot_status *)&info.type_flags;
> +               log_io("%16s: Active: %1d, Successful: %1d, Unbootable: %1d, Tries left: %1d\n",
> +                      info.name, slot_status->active,
> +                      slot_status->successful, slot_status->unbootable,
> +                      slot_status->tries_remaining);
> +               /*
> +                * FIXME: eventually we'll want to find the active/inactive variant of the partition
> +                * but on the rb3gen2 these values might all be 0
> +                */
> +               if (!strncmp(info.name, partname, strlen(partname))) {
> +                       log_debug("Found active %s partition: '%s'!\n", partname, info.name);
> +                       strlcpy(name, info.name, sizeof(info.name));
> +                       return partnum;
> +               }
> +       }
> +
> +       return -1;
> +}
> +
> +/**
> + * qcom_configure_capsule_updates() - Configure the DFU string for capsule updates
> + *
> + * U-Boot is flashed to the boot partition on Qualcomm boards. In most cases there
> + * are two boot partitions, boot_a and boot_b. As we don't currently support doing
> + * full A/B updates, we only support updating the currently active boot partition.
> + *
> + * So we need to find the current slot suffix and the associated boot partition.
> + * We do this by looking for the boot partition that has the 'active' flag set
> + * in the GPT partition vendor attribute bits.
> + */
> +void qcom_configure_capsule_updates(void)
> +{
> +       struct blk_desc *desc;
> +       int ret = 0, partnum = -1, devnum;
> +       static char dfu_string[32] = { 0 };
> +       char name[32]; /* GPT partition name */
> +       char *partname = "uefi_a";
> +       struct udevice *dev = NULL;
> +
> +       if (IS_ENABLED(CONFIG_SCSI)) {
> +               /* Scan for SCSI devices */
> +               ret = scsi_scan(false);
> +               if (ret) {
> +                       debug("Failed to scan SCSI devices: %d\n", ret);
> +                       return;
> +               }
> +       }
> +
> +       uclass_foreach_dev_probe(UCLASS_BLK, dev) {
> +               if (device_get_uclass_id(dev) != UCLASS_BLK)
> +                       continue;
> +
> +               desc = dev_get_uclass_plat(dev);
> +               if (!desc || desc->part_type == PART_TYPE_UNKNOWN)
> +                       continue;
> +               devnum = desc->devnum;
> +               partnum = find_boot_partition(partname, desc,
> +                                             name);
> +               if (partnum >= 0)
> +                       break;
> +       }
> +
> +       if (partnum < 0) {
> +               log_err("Failed to find boot partition\n");
> +               return;
> +       }
> +
> +       switch (desc->uclass_id) {
> +       case UCLASS_SCSI:
> +               snprintf(dfu_string, 32, "scsi %d=u-boot.bin part %d", devnum, partnum);
> +               break;
> +       case UCLASS_MMC:
> +               snprintf(dfu_string, 32, "mmc 0=u-boot.bin part %d %d", devnum, partnum);
> +               break;
> +       default:
> +               debug("Unsupported storage uclass: %d\n", desc->uclass_id);
> +               return;
> +       }
> +       log_debug("boot partition is %s, DFU string: '%s'\n", name, dfu_string);
> +
> +       update_info.dfu_string = dfu_string;
> +}
> diff --git a/arch/arm/mach-snapdragon/qcom-priv.h b/arch/arm/mach-snapdragon/qcom-priv.h
> index 0a7ed5eff8b8..74d39197b89f 100644
> --- a/arch/arm/mach-snapdragon/qcom-priv.h
> +++ b/arch/arm/mach-snapdragon/qcom-priv.h
> @@ -2,8 +2,14 @@
>
>  #ifndef __QCOM_PRIV_H__
>  #define __QCOM_PRIV_H__
>
> +#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
> +void qcom_configure_capsule_updates(void);
> +#else
> +void qcom_configure_capsule_updates(void) {}
> +#endif /* EFI_HAVE_CAPSULE_SUPPORT */
> +
>  #if CONFIG_IS_ENABLED(OF_LIVE)
>  /**
>   * qcom_of_fixup_nodes() - Fixup Qualcomm DT nodes
>   *
> diff --git a/include/configs/qcom.h b/include/configs/qcom.h
> index 5b5ebbd844df..9b41ab9e982b 100644
> --- a/include/configs/qcom.h
> +++ b/include/configs/qcom.h
> @@ -10,5 +10,10 @@
>  #define __CONFIGS_SNAPDRAGON_H
>
>  #define CFG_SYS_BAUDRATE_TABLE { 115200, 230400, 460800, 921600 }
>
> +// 2a5aa852-b856-4d97-baa9-5c5f4421551f
> +#define QUALCOMM_UBOOT_BOOT_IMAGE_GUID \
> +       EFI_GUID(0x2a5aa852, 0xb856, 0x4d97, 0xba, 0xa9, \
> +               0x5c, 0x5f, 0x44, 0x21, 0x55, 0x1f)
> +
>  #endif
>
> --
> 2.46.2
>


Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
diff mbox series

Patch

diff --git a/arch/arm/mach-snapdragon/Kconfig b/arch/arm/mach-snapdragon/Kconfig
index 536960b83c3b..f82cd0d32915 100644
--- a/arch/arm/mach-snapdragon/Kconfig
+++ b/arch/arm/mach-snapdragon/Kconfig
@@ -19,8 +19,11 @@  config SPL_SYS_MALLOC_F
 
 config SPL_SYS_MALLOC_F_LEN
 	default 0x2000
 
+config SYS_MALLOC_LEN
+	default 0x800000
+
 config LNX_KRNL_IMG_TEXT_OFFSET_BASE
 	default 0x80000000
 
 config SYS_BOARD
diff --git a/arch/arm/mach-snapdragon/Makefile b/arch/arm/mach-snapdragon/Makefile
index 7a4495c8108f..343e825c6fdd 100644
--- a/arch/arm/mach-snapdragon/Makefile
+++ b/arch/arm/mach-snapdragon/Makefile
@@ -2,5 +2,6 @@ 
 #
 # (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
 
 obj-y += board.o
+obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += capsule_update.o
 obj-$(CONFIG_OF_LIVE) += of_fixup.o
diff --git a/arch/arm/mach-snapdragon/board.c b/arch/arm/mach-snapdragon/board.c
index 2ab2ceb51389..fbb6cf7c6599 100644
--- a/arch/arm/mach-snapdragon/board.c
+++ b/arch/arm/mach-snapdragon/board.c
@@ -447,8 +447,11 @@  int board_late_init(void)
 
 	configure_env();
 	qcom_late_init();
 
+	/* Configure the dfu_string for capsule updates */
+	qcom_configure_capsule_updates();
+
 	return 0;
 }
 
 static void build_mem_map(void)
diff --git a/arch/arm/mach-snapdragon/capsule_update.c b/arch/arm/mach-snapdragon/capsule_update.c
new file mode 100644
index 000000000000..bf75a9a1b24c
--- /dev/null
+++ b/arch/arm/mach-snapdragon/capsule_update.c
@@ -0,0 +1,153 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Capsule update support for Qualcomm boards.
+ *
+ * Copyright (c) 2024 Linaro Ltd.
+ * Author: Caleb Connolly <caleb.connolly@linaro.org>
+ */
+
+#define pr_fmt(fmt) "QCOM-FMP: " fmt
+
+#include <dm/device.h>
+#include <dm/uclass.h>
+#include <efi.h>
+#include <efi_loader.h>
+#include <malloc.h>
+#include <scsi.h>
+#include <part.h>
+#include <linux/err.h>
+
+#include "qcom-priv.h"
+
+/*
+ * NOTE: for now this implementation only supports the rb3gen2. Supporting other
+ * boards that boot in different ways (e.g. chainloaded from ABL) will require
+ * additional complexity to properly create the dfu string and fw_images array.
+ */
+
+/*
+ * To handle different variants like chainloaded U-Boot here we'll need to
+ * build the fw_images array dynamically at runtime. It looks like
+ * mach-rockchip is a good example for how to do this.
+ * Detecting which image types a board uses is TBD, hence for now we only
+ * support the one new board that runs U-Boot as its primary bootloader.
+ */
+struct efi_fw_image fw_images[] = {
+	{
+		/* U-Boot flashed to the uefi_X partition (e.g. rb3gen2) */
+		.fw_name = u"UBOOT_UEFI_PARTITION",
+		.image_index = 1,
+	},
+};
+
+struct efi_capsule_update_info update_info = {
+	/* Filled in by configure_dfu_string() */
+	.dfu_string = NULL,
+	.num_images = ARRAY_SIZE(fw_images),
+	.images = fw_images,
+};
+
+/* LSB first */
+struct part_slot_status {
+	u16: 2;
+	u16 active : 1;
+	u16: 3;
+	u16 successful : 1;
+	u16 unbootable : 1;
+	u16 tries_remaining : 4;
+};
+
+static int find_boot_partition(const char *partname, struct blk_desc *blk_dev, char *name)
+{
+	int ret;
+	int partnum;
+	struct disk_partition info;
+	struct part_slot_status *slot_status;
+
+	for (partnum = 1;; partnum++) {
+		ret = part_get_info(blk_dev, partnum, &info);
+		if (ret)
+			return ret;
+
+		slot_status = (struct part_slot_status *)&info.type_flags;
+		log_io("%16s: Active: %1d, Successful: %1d, Unbootable: %1d, Tries left: %1d\n",
+		       info.name, slot_status->active,
+		       slot_status->successful, slot_status->unbootable,
+		       slot_status->tries_remaining);
+		/*
+		 * FIXME: eventually we'll want to find the active/inactive variant of the partition
+		 * but on the rb3gen2 these values might all be 0
+		 */
+		if (!strncmp(info.name, partname, strlen(partname))) {
+			log_debug("Found active %s partition: '%s'!\n", partname, info.name);
+			strlcpy(name, info.name, sizeof(info.name));
+			return partnum;
+		}
+	}
+
+	return -1;
+}
+
+/**
+ * qcom_configure_capsule_updates() - Configure the DFU string for capsule updates
+ *
+ * U-Boot is flashed to the boot partition on Qualcomm boards. In most cases there
+ * are two boot partitions, boot_a and boot_b. As we don't currently support doing
+ * full A/B updates, we only support updating the currently active boot partition.
+ *
+ * So we need to find the current slot suffix and the associated boot partition.
+ * We do this by looking for the boot partition that has the 'active' flag set
+ * in the GPT partition vendor attribute bits.
+ */
+void qcom_configure_capsule_updates(void)
+{
+	struct blk_desc *desc;
+	int ret = 0, partnum = -1, devnum;
+	static char dfu_string[32] = { 0 };
+	char name[32]; /* GPT partition name */
+	char *partname = "uefi_a";
+	struct udevice *dev = NULL;
+
+	if (IS_ENABLED(CONFIG_SCSI)) {
+		/* Scan for SCSI devices */
+		ret = scsi_scan(false);
+		if (ret) {
+			debug("Failed to scan SCSI devices: %d\n", ret);
+			return;
+		}
+	}
+
+	uclass_foreach_dev_probe(UCLASS_BLK, dev) {
+		if (device_get_uclass_id(dev) != UCLASS_BLK)
+			continue;
+
+		desc = dev_get_uclass_plat(dev);
+		if (!desc || desc->part_type == PART_TYPE_UNKNOWN)
+			continue;
+		devnum = desc->devnum;
+		partnum = find_boot_partition(partname, desc,
+					      name);
+		if (partnum >= 0)
+			break;
+	}
+
+	if (partnum < 0) {
+		log_err("Failed to find boot partition\n");
+		return;
+	}
+
+	switch (desc->uclass_id) {
+	case UCLASS_SCSI:
+		snprintf(dfu_string, 32, "scsi %d=u-boot.bin part %d", devnum, partnum);
+		break;
+	case UCLASS_MMC:
+		snprintf(dfu_string, 32, "mmc 0=u-boot.bin part %d %d", devnum, partnum);
+		break;
+	default:
+		debug("Unsupported storage uclass: %d\n", desc->uclass_id);
+		return;
+	}
+	log_debug("boot partition is %s, DFU string: '%s'\n", name, dfu_string);
+
+	update_info.dfu_string = dfu_string;
+}
diff --git a/arch/arm/mach-snapdragon/qcom-priv.h b/arch/arm/mach-snapdragon/qcom-priv.h
index 0a7ed5eff8b8..74d39197b89f 100644
--- a/arch/arm/mach-snapdragon/qcom-priv.h
+++ b/arch/arm/mach-snapdragon/qcom-priv.h
@@ -2,8 +2,14 @@ 
 
 #ifndef __QCOM_PRIV_H__
 #define __QCOM_PRIV_H__
 
+#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
+void qcom_configure_capsule_updates(void);
+#else
+void qcom_configure_capsule_updates(void) {}
+#endif /* EFI_HAVE_CAPSULE_SUPPORT */
+
 #if CONFIG_IS_ENABLED(OF_LIVE)
 /**
  * qcom_of_fixup_nodes() - Fixup Qualcomm DT nodes
  *
diff --git a/include/configs/qcom.h b/include/configs/qcom.h
index 5b5ebbd844df..9b41ab9e982b 100644
--- a/include/configs/qcom.h
+++ b/include/configs/qcom.h
@@ -10,5 +10,10 @@ 
 #define __CONFIGS_SNAPDRAGON_H
 
 #define CFG_SYS_BAUDRATE_TABLE	{ 115200, 230400, 460800, 921600 }
 
+// 2a5aa852-b856-4d97-baa9-5c5f4421551f
+#define QUALCOMM_UBOOT_BOOT_IMAGE_GUID \
+	EFI_GUID(0x2a5aa852, 0xb856, 0x4d97, 0xba, 0xa9, \
+		0x5c, 0x5f, 0x44, 0x21, 0x55, 0x1f)
+
 #endif