From f3749c018c06ebc475cc746771f8bbccbf7865b7 Mon Sep 17 00:00:00 2001
From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Date: Thu, 20 Mar 2025 14:41:41 +0000
Subject: [PATCH] fwu_arm_psa: corstone1000: Enable FWU support

Provide the update_info structure and FWU configs for Corstone-1000

update_info defines the image to be used for FWU.
The FWU configs enabled are the FWU Arm PSA configs.

Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Signed-off-by: Frazer Carsley <frazer.carsley@arm.com>
Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20250702152528.1180414-1-abdellatif.elkhlifi@arm.com/]
---
 board/armltd/corstone1000/corstone1000.c | 143 ++++++++++++++++++++++-
 configs/corstone1000_defconfig           |  12 +-
 2 files changed, 152 insertions(+), 3 deletions(-)

diff --git a/board/armltd/corstone1000/corstone1000.c b/board/armltd/corstone1000/corstone1000.c
index 16d0e679c3e..d2176b9174d 100644
--- a/board/armltd/corstone1000/corstone1000.c
+++ b/board/armltd/corstone1000/corstone1000.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * (C) Copyright 2022 ARM Limited
+ * (C) Copyright 2022, 2025 Arm Limited
  * (C) Copyright 2022 Linaro
  * Rui Miguel Silva <rui.silva@linaro.org>
  */
@@ -8,8 +8,11 @@
 #include <blk.h>
 #include <cpu_func.h>
 #include <dm.h>
+#include <generated/dt.h>
 #include <env.h>
+#include <efi_loader.h>
 #include <fwu.h>
+#include <fwu_arm_psa.h>
 #include <netdev.h>
 #include <nvmxip.h>
 #include <part.h>
@@ -21,6 +24,133 @@
 #define CORSTONE1000_KERNEL_PRIMARY "kernel_primary"
 #define CORSTONE1000_KERNEL_SECONDARY "kernel_secondary"
 
+#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
+
+/* The total number of upgradable images including the start and end dummy payloads */
+#define CAPSULE_PAYLOADS_MAX_COUNT (CONFIG_FWU_NUM_IMAGES_PER_BANK + 2)
+
+/*
+ * GUIDs for capsule updatable firmware images
+ *
+ * The GUIDs are generating with the UUIDv5 format.
+ * Namespace used for FVP GUIDs: 989f3a4e-46e0-4cd0-9877-a25c70c01329
+ * Namespace used for MPS3 GUIDs: df1865d1-90fb-4d59-9c38-c9f2c1bba8cc
+ * Names: the image names stated in the fw_name field
+ */
+
+#define FWU_FVP_BL2_IMAGE_GUID \
+	EFI_GUID(0xf1d883f9, 0xdfeb, 0x5363, 0x98, 0xd8,	\
+		0x68, 0x6e, 0xe3, 0xb6, 0x9f, 0x4f)
+
+#define FWU_MPS3_BL2_IMAGE_GUID \
+	EFI_GUID(0xfbfbefaa, 0x0a56, 0x50d5, 0xb6, 0x51,	\
+		0x74, 0x09, 0x1d, 0x3d, 0x62, 0xcf)
+
+#define FWU_FVP_TFM_S_IMAGE_GUID \
+	EFI_GUID(0x7fad470e, 0x5ec5, 0x5c03, 0xa2, 0xc1,	\
+		0x47, 0x56, 0xb4, 0x95, 0xde, 0x61)
+
+#define FWU_MPS3_TFM_S_IMAGE_GUID \
+	EFI_GUID(0xaf4cc7ad, 0xee2e, 0x5a39, 0xaa, 0xd5,	\
+		0xfa, 0xc8, 0xa1, 0xe6, 0x17, 0x3c)
+
+#define FWU_FVP_FIP_IMAGE_GUID \
+	EFI_GUID(0xf1933675, 0x5a8c, 0x5b6d, 0x9e, 0xf4,	\
+		0x84, 0x67, 0x39, 0xe8, 0x9b, 0xc8)
+
+#define FWU_MPS3_FIP_IMAGE_GUID \
+	EFI_GUID(0x55302f96, 0xc4f0, 0x5cf9, 0x86, 0x24,	\
+		0xe7, 0xcc, 0x38, 0x8f, 0x2b, 0x68)
+
+#define FWU_FVP_INITRAMFS_IMAGE_GUID \
+	EFI_GUID(0xf771aff9, 0xc7e9, 0x5f99, 0x9e, 0xda,	\
+		0x23, 0x69, 0xdd, 0x69, 0x4f, 0x61)
+
+#define FWU_MPS3_INITRAMFS_IMAGE_GUID \
+	EFI_GUID(0x3e8ac972, 0xc33c, 0x5cc9, 0x90, 0xa0,	\
+		0xcd, 0xd3, 0x15, 0x96, 0x83, 0xea)
+
+enum fw_image_index {
+	FW_IMAGE_INDEX_BL2 = 1,
+	FW_IMAGE_INDEX_TFM_S,
+	FW_IMAGE_INDEX_FIP,
+	FW_IMAGE_INDEX_INITRAMFS,
+	FW_IMAGE_INDEX_DUMMY_START,
+	FW_IMAGE_INDEX_DUMMY_END
+};
+
+struct efi_fw_image fw_mps3_images[] = {
+	{
+		.image_type_id = FWU_MPS3_BL2_IMAGE_GUID,
+		.fw_name = u"BL2",
+		.image_index = FW_IMAGE_INDEX_BL2,
+	},
+	{
+		.image_type_id = FWU_MPS3_TFM_S_IMAGE_GUID,
+		.fw_name = u"TFM_S",
+		.image_index = FW_IMAGE_INDEX_TFM_S,
+	},
+	{
+		.image_type_id = FWU_MPS3_FIP_IMAGE_GUID,
+		.fw_name = u"FIP",
+		.image_index = FW_IMAGE_INDEX_FIP,
+	},
+	{
+		.image_type_id = FWU_MPS3_INITRAMFS_IMAGE_GUID,
+		.fw_name = u"INITRAMFS",
+		.image_index = FW_IMAGE_INDEX_INITRAMFS,
+	},
+	{
+		.image_type_id = FWU_DUMMY_START_IMAGE_GUID,
+		.fw_name = u"DUMMY_START",
+		.image_index = FW_IMAGE_INDEX_DUMMY_START,
+	},
+	{
+		.image_type_id = FWU_DUMMY_END_IMAGE_GUID,
+		.fw_name = u"DUMMY_END",
+		.image_index = FW_IMAGE_INDEX_DUMMY_END,
+	},
+};
+
+struct efi_fw_image fw_fvp_images[] = {
+	{
+		.image_type_id = FWU_FVP_BL2_IMAGE_GUID,
+		.fw_name = u"BL2",
+		.image_index = FW_IMAGE_INDEX_BL2,
+	},
+	{
+		.image_type_id = FWU_FVP_TFM_S_IMAGE_GUID,
+		.fw_name = u"TFM_S",
+		.image_index = FW_IMAGE_INDEX_TFM_S,
+	},
+	{
+		.image_type_id = FWU_FVP_FIP_IMAGE_GUID,
+		.fw_name = u"FIP",
+		.image_index = FW_IMAGE_INDEX_FIP,
+	},
+	{
+		.image_type_id = FWU_FVP_INITRAMFS_IMAGE_GUID,
+		.fw_name = u"INITRAMFS",
+		.image_index = FW_IMAGE_INDEX_INITRAMFS,
+	},
+	{
+		.image_type_id = FWU_DUMMY_START_IMAGE_GUID,
+		.fw_name = u"DUMMY_START",
+		.image_index = FW_IMAGE_INDEX_DUMMY_START,
+	},
+	{
+		.image_type_id = FWU_DUMMY_END_IMAGE_GUID,
+		.fw_name = u"DUMMY_END",
+		.image_index = FW_IMAGE_INDEX_DUMMY_END,
+	},
+};
+
+struct efi_capsule_update_info update_info = {
+	.num_images = CAPSULE_PAYLOADS_MAX_COUNT,
+};
+
+#endif /* EFI_HAVE_CAPSULE_SUPPORT */
+
 static int corstone1000_boot_idx;
 
 static struct mm_region corstone1000_mem_map[] = {
@@ -77,6 +207,17 @@ static struct mm_region corstone1000_mem_map[] = {
 
 struct mm_region *mem_map = corstone1000_mem_map;
 
+int board_init(void)
+{
+#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
+	if (!strcmp(DEVICE_TREE, "corstone1000-fvp"))
+		update_info.images = fw_fvp_images;
+	else
+		update_info.images = fw_mps3_images;
+#endif
+	return 0;
+}
+
 int dram_init(void)
 {
 	gd->ram_size = PHYS_SDRAM_1_SIZE;
diff --git a/configs/corstone1000_defconfig b/configs/corstone1000_defconfig
index 527a679b785..a75877a9174 100644
--- a/configs/corstone1000_defconfig
+++ b/configs/corstone1000_defconfig
@@ -10,11 +10,16 @@ CONFIG_DEFAULT_DEVICE_TREE="corstone1000-mps3"
 CONFIG_SYS_BOOTM_LEN=0x800000
 CONFIG_SYS_LOAD_ADDR=0x82100000
 CONFIG_IDENT_STRING=" corstone1000 aarch64 "
+CONFIG_FWU_NUM_IMAGES_PER_BANK=4
 CONFIG_EFI_MM_COMM_TEE=y
 CONFIG_FFA_SHARED_MM_BUF_SIZE=4096
 CONFIG_FFA_SHARED_MM_BUF_OFFSET=0
 CONFIG_FFA_SHARED_MM_BUF_ADDR=0x02000000
+CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y
 CONFIG_EFI_CAPSULE_ON_DISK=y
+CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y
+CONFIG_EFI_CAPSULE_AUTHENTICATE=y
+CONFIG_EFI_CAPSULE_CRT_FILE="CRT.crt"
 CONFIG_FIT=y
 CONFIG_DISTRO_DEFAULTS=y
 CONFIG_BOOTDELAY=3
@@ -26,7 +31,7 @@ CONFIG_SYS_CBSIZE=512
 CONFIG_LOGLEVEL=7
 # CONFIG_DISPLAY_CPUINFO is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
-# CONFIG_BOARD_INIT is not set
+CONFIG_BOARD_INIT=y
 CONFIG_BOARD_LATE_INIT=y
 CONFIG_SYS_PROMPT="corstone1000# "
 # CONFIG_CMD_CONSOLE is not set
@@ -39,6 +44,7 @@ CONFIG_CMD_LOADM=y
 CONFIG_CMD_USB=y
 # CONFIG_CMD_SETEXPR is not set
 CONFIG_CMD_CACHE=y
+CONFIG_CMD_EFIDEBUG=y
 CONFIG_CMD_RTC=y
 CONFIG_CMD_TIME=y
 CONFIG_CMD_GETTIME=y
@@ -68,4 +74,6 @@ CONFIG_USB_ISP1760=y
 # CONFIG_RANDOM_UUID is not set
 CONFIG_ERRNO_STR=y
 CONFIG_FWU_MULTI_BANK_UPDATE=y
-CONFIG_FWU_MDATA_V1=y
+CONFIG_FWU_MDATA_V2=y
+CONFIG_FWU_ARM_PSA=y
+# CONFIG_TOOLS_MKEFICAPSULE is not set
