mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler
@ 2022-06-02  9:01 Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 1/7] bbu: move barebox_update eMMC boot handling into common code Ahmad Fatoum
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox

STM32MP1 BootROM boots from GPT partition fsbl1 or fsbl2 on SD-Card and
from boot partition on eMMC. Recent TF-A without legacy image support will
then look in a GPT partition named fip in the user area.

With recent patches[1], TF-A will also check offset SZ_256K in the boot
partition to see if the FIP is there.

Add a barebox_update handler that covers these scenarios.

The TF-A patches are not yet upstream, but have all maintainer ACKs and
will likely be part of TF-A v2.8.

[1]: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/15332

Ahmad Fatoum (7):
  bbu: move barebox_update eMMC boot handling into common code
  bbu: use free(NULL) to simplify function cleanup
  bbu: add flag for enabling eMMC boot ack
  bbu: export bbu_std_file_handler for use in custom handlers
  filetype: differentiate between STM32MP FSBL and SSBL images
  ARM: stm32mp: bbu: add FIP update handler
  fastboot: support TF-A FSBL and FIP images for barebox update

 arch/arm/mach-imx/imx-bbu-internal.c     |  48 +-----
 arch/arm/mach-stm32mp/Makefile           |   1 +
 arch/arm/mach-stm32mp/bbu.c              | 197 +++++++++++++++++++++++
 arch/arm/mach-stm32mp/include/mach/bbu.h |  18 ++-
 arch/arm/mach-stm32mp/stm32image.c       |   2 +-
 common/bbu.c                             | 100 ++++++++++--
 common/filetype.c                        |  15 +-
 include/bbu.h                            |   7 +
 include/filetype.h                       |   3 +-
 9 files changed, 327 insertions(+), 64 deletions(-)
 create mode 100644 arch/arm/mach-stm32mp/bbu.c

-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/7] bbu: move barebox_update eMMC boot handling into common code
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 2/7] bbu: use free(NULL) to simplify function cleanup Ahmad Fatoum
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Like with the i.MX, the STM32MP1 BootROM also consults the
EXT_CSD_PARTITION_CONFIG register to find out what to boot.

The barebox_update code used for atomic update on i.MX is thus
useful to the STM32MP as well, so move the boot switching part
to a generic location.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/mach-imx/imx-bbu-internal.c | 48 ++----------------------
 common/bbu.c                         | 55 ++++++++++++++++++++++++++++
 include/bbu.h                        |  3 ++
 3 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index 64d4d77ff596..3b0c587cc572 100644
--- a/arch/arm/mach-imx/imx-bbu-internal.c
+++ b/arch/arm/mach-imx/imx-bbu-internal.c
@@ -422,54 +422,12 @@ static int imx_bbu_update(struct bbu_handler *handler, struct bbu_data *data)
 static int imx_bbu_internal_mmcboot_update(struct bbu_handler *handler,
 					   struct bbu_data *data)
 {
-	struct bbu_data _data = *data;
 	int ret;
-	char *bootpartvar;
-	const char *bootpart;
-	char *devicefile;
-	const char *devname = devpath_to_name(data->devicefile);
 
-	ret = device_detect_by_name(devname);
-	if (ret) {
-		pr_err("Couldn't detect device '%s'\n", devname);
-		return ret;
-	}
-
-	ret = asprintf(&bootpartvar, "%s.boot", devname);
-	if (ret < 0)
-		return ret;
-
-	bootpart = getenv(bootpartvar);
-	if (!bootpart) {
-		pr_err("Couldn't read the value of '%s'\n", bootpartvar);
-		ret = -ENOENT;
-		goto free_bootpartvar;
-	}
-
-	if (!strcmp(bootpart, "boot0")) {
-		bootpart = "boot1";
-	} else {
-		bootpart = "boot0";
-	}
-
-	ret = asprintf(&devicefile, "/dev/%s.%s", devname, bootpart);
-	if (ret < 0)
-		goto free_bootpartvar;
-
-	_data.devicefile = devicefile;
-
-	ret = imx_bbu_update(handler, &_data);
-	if (ret)
-		goto free_devicefile;
-
-	/* on success switch boot source */
-	ret = setenv(bootpartvar, bootpart);
-
-free_devicefile:
-	free(devicefile);
+	ret = bbu_mmcboot_handler(handler, data, imx_bbu_update);
 
-free_bootpartvar:
-	free(bootpartvar);
+	if (ret == -ENOENT)
+		pr_err("Couldn't read the value of .boot parameter\n");
 
 	return ret;
 }
diff --git a/common/bbu.c b/common/bbu.c
index cd7bdc40b72a..6a47b21a55b0 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -19,6 +19,7 @@
 #include <malloc.h>
 #include <linux/stat.h>
 #include <image-metadata.h>
+#include <environment.h>
 #include <file-list.h>
 
 static LIST_HEAD(bbu_image_handlers);
@@ -304,6 +305,60 @@ struct bbu_std {
 	enum filetype filetype;
 };
 
+int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
+			int (*chained_handler)(struct bbu_handler *, struct bbu_data *))
+{
+	struct bbu_data _data = *data;
+	int ret;
+	char *bootpartvar;
+	const char *bootpart;
+	char *devicefile;
+	const char *devname = devpath_to_name(data->devicefile);
+
+	ret = device_detect_by_name(devname);
+	if (ret) {
+		pr_err("Couldn't detect device '%s'\n", devname);
+		return ret;
+	}
+
+	ret = asprintf(&bootpartvar, "%s.boot", devname);
+	if (ret < 0)
+		return ret;
+
+	bootpart = getenv(bootpartvar);
+	if (!bootpart) {
+		ret = -ENOENT;
+		goto free_bootpartvar;
+	}
+
+	if (!strcmp(bootpart, "boot0")) {
+		bootpart = "boot1";
+	} else {
+		bootpart = "boot0";
+	}
+
+	ret = asprintf(&devicefile, "/dev/%s.%s", devname, bootpart);
+	if (ret < 0)
+		goto free_bootpartvar;
+
+	_data.devicefile = devicefile;
+
+	ret = chained_handler(handler, &_data);
+	if (ret < 0)
+		goto free_devicefile;
+
+	/* on success switch boot source */
+	ret = setenv(bootpartvar, bootpart);
+
+free_devicefile:
+	free(devicefile);
+
+free_bootpartvar:
+	free(bootpartvar);
+
+	return ret;
+}
+
 static int bbu_std_file_handler(struct bbu_handler *handler,
 					struct bbu_data *data)
 {
diff --git a/include/bbu.h b/include/bbu.h
index 3128339068ee..bf5f2158df72 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -50,6 +50,9 @@ void bbu_handlers_list(void);
 
 struct file_list;
 
+int bbu_mmcboot_handler(struct bbu_handler *, struct bbu_data *,
+			int (*chained_handler)(struct bbu_handler *, struct bbu_data *));
+
 #ifdef CONFIG_BAREBOX_UPDATE
 
 int bbu_register_handler(struct bbu_handler *);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 2/7] bbu: use free(NULL) to simplify function cleanup
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 1/7] bbu: move barebox_update eMMC boot handling into common code Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 3/7] bbu: add flag for enabling eMMC boot ack Ahmad Fatoum
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We will add a third allocated string in a follow up commit, so instead
of having a third label to selectively free it, just initialize all
the pointers to NULL and free them unconditionally to simplify the code.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/bbu.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/common/bbu.c b/common/bbu.c
index 6a47b21a55b0..4d92d70ff9e3 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -310,9 +310,8 @@ int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
 {
 	struct bbu_data _data = *data;
 	int ret;
-	char *bootpartvar;
+	char *devicefile = NULL, *bootpartvar = NULL;
 	const char *bootpart;
-	char *devicefile;
 	const char *devname = devpath_to_name(data->devicefile);
 
 	ret = device_detect_by_name(devname);
@@ -328,7 +327,7 @@ int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
 	bootpart = getenv(bootpartvar);
 	if (!bootpart) {
 		ret = -ENOENT;
-		goto free_bootpartvar;
+		goto out;
 	}
 
 	if (!strcmp(bootpart, "boot0")) {
@@ -339,21 +338,19 @@ int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
 
 	ret = asprintf(&devicefile, "/dev/%s.%s", devname, bootpart);
 	if (ret < 0)
-		goto free_bootpartvar;
+		goto out;
 
 	_data.devicefile = devicefile;
 
 	ret = chained_handler(handler, &_data);
 	if (ret < 0)
-		goto free_devicefile;
+		goto out;
 
 	/* on success switch boot source */
 	ret = setenv(bootpartvar, bootpart);
 
-free_devicefile:
+out:
 	free(devicefile);
-
-free_bootpartvar:
 	free(bootpartvar);
 
 	return ret;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 3/7] bbu: add flag for enabling eMMC boot ack
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 1/7] bbu: move barebox_update eMMC boot handling into common code Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 2/7] bbu: use free(NULL) to simplify function cleanup Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 4/7] bbu: export bbu_std_file_handler for use in custom handlers Ahmad Fatoum
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

bbu_mmcboot_handler() already takes care to switch $mmc.boot to the
freshly read inactive partition. On some SoCs like the STM32MP1, this is
not enough, but the boot ack bit must be set as well, so the BootROM can
communicate with the eMMC.

Have this happen as part of the eMMC boot switch after a successful
update if bbu_data::flags has BBU_FLAG_MMC_BOOT_ACK set.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/bbu.c  | 17 ++++++++++++++++-
 include/bbu.h |  1 +
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/common/bbu.c b/common/bbu.c
index 4d92d70ff9e3..26b997c02ed1 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -310,7 +310,7 @@ int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
 {
 	struct bbu_data _data = *data;
 	int ret;
-	char *devicefile = NULL, *bootpartvar = NULL;
+	char *devicefile = NULL, *bootpartvar = NULL, *bootackvar = NULL;
 	const char *bootpart;
 	const char *devname = devpath_to_name(data->devicefile);
 
@@ -346,10 +346,25 @@ int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
 	if (ret < 0)
 		goto out;
 
+	/*
+	 * This flag can be set in the chained handler or by
+	 * bbu_mmcboot_handler's caller
+	 */
+	if ((_data.flags | data->flags) & BBU_FLAG_MMC_BOOT_ACK) {
+		ret = asprintf(&bootackvar, "%s.boot_ack", devname);
+		if (ret < 0)
+			goto out;
+
+		ret = setenv(bootackvar, "1");
+		if (ret)
+			goto out;
+	}
+
 	/* on success switch boot source */
 	ret = setenv(bootpartvar, bootpart);
 
 out:
+	free(bootackvar);
 	free(devicefile);
 	free(bootpartvar);
 
diff --git a/include/bbu.h b/include/bbu.h
index bf5f2158df72..bd19fa187faf 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -10,6 +10,7 @@
 struct bbu_data {
 #define BBU_FLAG_FORCE	(1 << 0)
 #define BBU_FLAG_YES	(1 << 1)
+#define BBU_FLAG_MMC_BOOT_ACK	(1 << 2)
 	unsigned long flags;
 	int force;
 	const void *image;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 4/7] bbu: export bbu_std_file_handler for use in custom handlers
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2022-06-02  9:01 ` [PATCH 3/7] bbu: add flag for enabling eMMC boot ack Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 5/7] filetype: differentiate between STM32MP FSBL and SSBL images Ahmad Fatoum
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

bbu_register_std_file_update() registers an update handler that updates
a single file, usually a partition. Depending on SoC, we may want to
compute the file path at install time. To save custom bbu code the
hassle of reimplementing bbu_std_file_handler(), export it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/bbu.c  | 33 ++++++++++++++++++++-------------
 include/bbu.h |  3 +++
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/common/bbu.c b/common/bbu.c
index 26b997c02ed1..d243ac89dd9f 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -371,23 +371,13 @@ out:
 	return ret;
 }
 
-static int bbu_std_file_handler(struct bbu_handler *handler,
-					struct bbu_data *data)
+int bbu_std_file_handler(struct bbu_handler *handler,
+			 struct bbu_data *data)
 {
-	struct bbu_std *std = container_of(handler, struct bbu_std, handler);
 	int fd, ret;
-	enum filetype filetype;
 	struct stat s;
 	unsigned oflags = O_WRONLY;
 
-	filetype = file_detect_type(data->image, data->len);
-	if (filetype != std->filetype) {
-		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
-				file_type_to_string(std->filetype),
-				file_type_to_string(filetype)))
-			return -EINVAL;
-	}
-
 	device_detect_by_name(devpath_to_name(data->devicefile));
 
 	ret = stat(data->devicefile, &s);
@@ -436,6 +426,23 @@ err_close:
 	return ret;
 }
 
+static int bbu_std_file_handler_checked(struct bbu_handler *handler,
+					struct bbu_data *data)
+{
+	struct bbu_std *std = container_of(handler, struct bbu_std, handler);
+	enum filetype filetype;
+
+	filetype = file_detect_type(data->image, data->len);
+	if (filetype != std->filetype) {
+		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
+				file_type_to_string(std->filetype),
+				file_type_to_string(filetype)))
+			return -EINVAL;
+	}
+
+	return bbu_std_file_handler(handler, data);
+}
+
 /**
  * bbu_register_std_file_update() - register a barebox update handler for a
  *                                  standard file-to-device-copy operation
@@ -466,7 +473,7 @@ int bbu_register_std_file_update(const char *name, unsigned long flags,
 	handler->flags = flags;
 	handler->devicefile = devicefile;
 	handler->name = name;
-	handler->handler = bbu_std_file_handler;
+	handler->handler = bbu_std_file_handler_checked;
 
 	ret = bbu_register_handler(handler);
 	if (ret)
diff --git a/include/bbu.h b/include/bbu.h
index bd19fa187faf..2dad26a12788 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -54,6 +54,9 @@ struct file_list;
 int bbu_mmcboot_handler(struct bbu_handler *, struct bbu_data *,
 			int (*chained_handler)(struct bbu_handler *, struct bbu_data *));
 
+int bbu_std_file_handler(struct bbu_handler *handler,
+			 struct bbu_data *data);
+
 #ifdef CONFIG_BAREBOX_UPDATE
 
 int bbu_register_handler(struct bbu_handler *);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 5/7] filetype: differentiate between STM32MP FSBL and SSBL images
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2022-06-02  9:01 ` [PATCH 4/7] bbu: export bbu_std_file_handler for use in custom handlers Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 6/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We have some special handling for legacy (non-FIP) STM32 images:
We have a bootm handler for chainloading and an update handler
for use with GPT ssbl partitions. Both aren't applicable to the
TF-A image used as FSBL. As barebox always has 0x00000000 at
offset 0xfc and TF-A alrways has 0x10000000, we can use that
to differentiate between the two images to make sure we refuse
TF-A images when barebox images are expected.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/mach-stm32mp/include/mach/bbu.h |  2 +-
 arch/arm/mach-stm32mp/stm32image.c       |  2 +-
 common/filetype.c                        | 13 ++++++++++---
 include/filetype.h                       |  3 ++-
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/arm/mach-stm32mp/include/mach/bbu.h b/arch/arm/mach-stm32mp/include/mach/bbu.h
index d49fb045ea06..3a6951a8f1a0 100644
--- a/arch/arm/mach-stm32mp/include/mach/bbu.h
+++ b/arch/arm/mach-stm32mp/include/mach/bbu.h
@@ -10,7 +10,7 @@ static inline int stm32mp_bbu_mmc_register_handler(const char *name,
 						   unsigned long flags)
 {
 	return bbu_register_std_file_update(name, flags, devicefile,
-					    filetype_stm32_image_v1);
+					    filetype_stm32_image_ssbl_v1);
 }
 
 #endif /* MACH_STM32MP_BBU_H_ */
diff --git a/arch/arm/mach-stm32mp/stm32image.c b/arch/arm/mach-stm32mp/stm32image.c
index caff68651c47..7867418e6caa 100644
--- a/arch/arm/mach-stm32mp/stm32image.c
+++ b/arch/arm/mach-stm32mp/stm32image.c
@@ -40,7 +40,7 @@ static int do_bootm_stm32image(struct image_data *data)
 static struct image_handler image_handler_stm32_image_v1_handler = {
 	.name = "STM32 image (v1)",
 	.bootm = do_bootm_stm32image,
-	.filetype = filetype_stm32_image_v1,
+	.filetype = filetype_stm32_image_ssbl_v1,
 };
 
 static int stm32mp_register_stm32image_image_handler(void)
diff --git a/common/filetype.c b/common/filetype.c
index 0ded64b83c00..3e9e14c1d79e 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -71,7 +71,8 @@ static const struct filetype_str filetype_str[] = {
 	[filetype_layerscape_qspi_image] = { "Layerscape QSPI image", "layerscape-qspi-PBL" },
 	[filetype_ubootvar] = { "U-Boot environmemnt variable data",
 				"ubootvar" },
-	[filetype_stm32_image_v1] = { "STM32 image (v1)", "stm32-image-v1" },
+	[filetype_stm32_image_fsbl_v1] = { "STM32MP FSBL image (v1)", "stm32-fsbl-v1" },
+	[filetype_stm32_image_ssbl_v1] = { "STM32MP SSBL image (v1)", "stm32-ssbl-v1" },
 	[filetype_zynq_image] = { "Zynq image", "zynq-image" },
 	[filetype_mxs_sd_image] = { "i.MX23/28 SD card image", "mxs-sd-image" },
 	[filetype_rockchip_rkns_image] = { "Rockchip boot image", "rk-image" },
@@ -372,8 +373,14 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
 		return filetype_unknown;
 
 	if (strncmp(buf8, "STM\x32", 4) == 0) {
-		if (buf8[74] == 0x01)
-			return filetype_stm32_image_v1;
+		if (buf8[74] == 0x01) {
+			switch(le32_to_cpu(buf[63])) {
+			case 0x00000000:
+				return filetype_stm32_image_ssbl_v1;
+			case 0x10000000:
+				return filetype_stm32_image_fsbl_v1;
+			}
+		}
 	}
 
 	if (bufsize < 512)
diff --git a/include/filetype.h b/include/filetype.h
index 9b7499fdf307..00d54e48d528 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -52,7 +52,8 @@ enum filetype {
 	filetype_layerscape_image,
 	filetype_layerscape_qspi_image,
 	filetype_ubootvar,
-	filetype_stm32_image_v1,
+	filetype_stm32_image_fsbl_v1,
+	filetype_stm32_image_ssbl_v1,
 	filetype_zynq_image,
 	filetype_mxs_sd_image,
 	filetype_rockchip_rkns_image,
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 6/7] ARM: stm32mp: bbu: add FIP update handler
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (4 preceding siblings ...)
  2022-06-02  9:01 ` [PATCH 5/7] filetype: differentiate between STM32MP FSBL and SSBL images Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-02  9:01 ` [PATCH 7/7] fastboot: support TF-A FSBL and FIP images for barebox update Ahmad Fatoum
  2022-06-03  7:17 ` [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Sascha Hauer
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

BootROM boots from GPT partition fsbl1 or fsbl2 on SD-Card and from
boot partition on eMMC. Recent TF-A without legacy image support will
then look in a GPT partition named fip in the user area.

With recent patches[1], TF-A will also check offset SZ_256K in the boot
partition to see if the FIP is there.

Add a barebox_update handler that covers these scenarios.

[1]: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/15332
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/mach-stm32mp/Makefile           |   1 +
 arch/arm/mach-stm32mp/bbu.c              | 197 +++++++++++++++++++++++
 arch/arm/mach-stm32mp/include/mach/bbu.h |  16 ++
 3 files changed, 214 insertions(+)
 create mode 100644 arch/arm/mach-stm32mp/bbu.c

diff --git a/arch/arm/mach-stm32mp/Makefile b/arch/arm/mach-stm32mp/Makefile
index 86c13b8fca8a..837449150cff 100644
--- a/arch/arm/mach-stm32mp/Makefile
+++ b/arch/arm/mach-stm32mp/Makefile
@@ -4,3 +4,4 @@ obj-y := init.o
 obj-pbl-y := ddrctrl.o
 pbl-y := bl33-generic.o
 obj-$(CONFIG_BOOTM) += stm32image.o
+obj-$(CONFIG_BAREBOX_UPDATE) += bbu.o
diff --git a/arch/arm/mach-stm32mp/bbu.c b/arch/arm/mach-stm32mp/bbu.c
new file mode 100644
index 000000000000..545965198f4d
--- /dev/null
+++ b/arch/arm/mach-stm32mp/bbu.c
@@ -0,0 +1,197 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define pr_fmt(fmt) "stm32mp-bbu: " fmt
+#include <common.h>
+#include <malloc.h>
+#include <bbu.h>
+#include <filetype.h>
+#include <errno.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/sizes.h>
+#include <linux/stat.h>
+#include <ioctl.h>
+#include <mach/bbu.h>
+#include <libfile.h>
+#include <linux/bitfield.h>
+
+#define STM32MP_BBU_IMAGE_HAVE_FSBL		BIT(0)
+#define STM32MP_BBU_IMAGE_HAVE_FIP		BIT(1)
+
+struct stm32mp_bbu_handler {
+	struct bbu_handler handler;
+	loff_t offset;
+};
+
+#define to_stm32mp_bbu_handler(h) container_of(h, struct stm32mp_bbu_handler, h)
+
+static int stm32mp_bbu_gpt_part_update(struct bbu_handler *handler,
+				       const struct bbu_data *data,
+				       const char *part, bool optional)
+{
+	struct bbu_data gpt_data = *data;
+	struct stat st;
+	int ret;
+
+	gpt_data.devicefile = basprintf("%s.%s", gpt_data.devicefile, part);
+	if (!gpt_data.devicefile)
+		return -ENOMEM;
+
+	pr_debug("Attempting %s update\n", gpt_data.devicefile);
+
+	ret = stat(gpt_data.devicefile, &st);
+	if (ret == -ENOENT) {
+		if (optional)
+			return 0;
+		pr_err("Partition %s does not exist\n", gpt_data.devicefile);
+	}
+	if (ret)
+		goto out;
+
+	ret = bbu_std_file_handler(handler, &gpt_data);
+out:
+	kfree_const(gpt_data.devicefile);
+	return ret;
+}
+
+static int stm32mp_bbu_mmc_update(struct bbu_handler *handler,
+				  struct bbu_data *data)
+{
+	struct stm32mp_bbu_handler *priv = to_stm32mp_bbu_handler(handler);
+	int fd, ret;
+	size_t image_len = data->len;
+	const void *buf = data->image;
+	struct stat st;
+
+	pr_debug("Attempting eMMC boot partition update\n");
+
+	ret = bbu_confirm(data);
+	if (ret)
+		return ret;
+
+	fd = open(data->devicefile, O_RDWR);
+	if (fd < 0)
+		return fd;
+
+	ret = fstat(fd, &st);
+	if (ret)
+		goto close;
+
+	if (st.st_size < priv->offset || image_len > st.st_size - priv->offset) {
+		ret = -ENOSPC;
+		goto close;
+	}
+
+	ret = pwrite_full(fd, buf, image_len, priv->offset);
+	if (ret < 0)
+		pr_err("writing to %s failed with %pe\n", data->devicefile, ERR_PTR(ret));
+
+close:
+	close(fd);
+
+	return ret < 0 ? ret : 0;
+}
+
+/*
+ * TF-A compiled with STM32_EMMC_BOOT will first check for FIP image
+ * at offset SZ_256K and then in GPT partition of that name.
+ */
+static int stm32mp_bbu_mmc_fip_handler(struct bbu_handler *handler,
+				       struct bbu_data *data)
+{
+	struct stm32mp_bbu_handler *priv = to_stm32mp_bbu_handler(handler);
+	enum filetype filetype;
+	int image_flags = 0, ret;
+	bool is_emmc = true;
+
+	filetype = file_detect_type(data->image, data->len);
+
+	switch (filetype) {
+	case filetype_stm32_image_fsbl_v1:
+		priv->offset = 0;
+		image_flags |= STM32MP_BBU_IMAGE_HAVE_FSBL;
+		if (data->len > SZ_256K)
+			image_flags |= STM32MP_BBU_IMAGE_HAVE_FIP;
+		break;
+	default:
+		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
+				file_type_to_string(filetype_fip),
+				file_type_to_string(filetype)))
+			return -EINVAL;
+		/* If forced assume it's a SSBL */
+		filetype = filetype_fip;
+		fallthrough;
+	case filetype_fip:
+		priv->offset = SZ_256K;
+		image_flags |= STM32MP_BBU_IMAGE_HAVE_FIP;
+		break;
+	}
+
+	pr_debug("Handling %s\n", file_type_to_string(filetype));
+
+	data->flags |= BBU_FLAG_MMC_BOOT_ACK;
+
+	ret = bbu_mmcboot_handler(handler, data, stm32mp_bbu_mmc_update);
+	if (ret == -ENOENT) {
+		pr_debug("Not an eMMC, falling back to GPT fsbl1 partition\n");
+		is_emmc = false;
+		ret = 0;
+	}
+	if (ret < 0) {
+		pr_debug("eMMC boot update failed: %pe\n", ERR_PTR(ret));
+		return ret;
+	}
+
+	if (!is_emmc && (image_flags & STM32MP_BBU_IMAGE_HAVE_FSBL)) {
+		struct bbu_data fsbl1_data = *data;
+
+		fsbl1_data.len = min_t(size_t, fsbl1_data.len, SZ_256K);
+
+		/*
+		 * BootROM tells TF-A which fsbl slot was booted in r0, but TF-A
+		 * doesn't yet propagate this to us, so for now always flash
+		 * fsbl1
+		 */
+		ret = stm32mp_bbu_gpt_part_update(handler, &fsbl1_data, "fsbl1", false);
+	}
+
+	if (ret == 0 && (image_flags & STM32MP_BBU_IMAGE_HAVE_FIP)) {
+		struct bbu_data fip_data = *data;
+
+		if (image_flags & STM32MP_BBU_IMAGE_HAVE_FSBL) {
+			fip_data.image += SZ_256K;
+			fip_data.len -= SZ_256K;
+		}
+
+		/* No fip GPT partition in eMMC user area is usually ok, as
+		 * that means TF-A is configured to load FIP from eMMC boot part
+		 */
+		ret = stm32mp_bbu_gpt_part_update(handler, &fip_data, "fip", is_emmc);
+	}
+
+	if (ret < 0)
+		pr_debug("eMMC user area update failed: %pe\n", ERR_PTR(ret));
+
+	return ret;
+}
+
+int stm32mp_bbu_mmc_fip_register(const char *name,
+				 const char *devicefile,
+				 unsigned long flags)
+{
+	struct stm32mp_bbu_handler *priv;
+	int ret;
+
+	priv = xzalloc(sizeof(*priv));
+
+	priv->handler.flags = flags;
+	priv->handler.devicefile = devicefile;
+	priv->handler.name = name;
+	priv->handler.handler = stm32mp_bbu_mmc_fip_handler;
+
+	ret = bbu_register_handler(&priv->handler);
+	if (ret)
+		free(priv);
+
+	return ret;
+}
diff --git a/arch/arm/mach-stm32mp/include/mach/bbu.h b/arch/arm/mach-stm32mp/include/mach/bbu.h
index 3a6951a8f1a0..b469cdeb7c93 100644
--- a/arch/arm/mach-stm32mp/include/mach/bbu.h
+++ b/arch/arm/mach-stm32mp/include/mach/bbu.h
@@ -13,4 +13,20 @@ static inline int stm32mp_bbu_mmc_register_handler(const char *name,
 					    filetype_stm32_image_ssbl_v1);
 }
 
+#ifdef CONFIG_BAREBOX_UPDATE
+
+int stm32mp_bbu_mmc_fip_register(const char *name, const char *devicefile,
+				 unsigned long flags);
+
+#else
+
+static inline int stm32mp_bbu_mmc_fip_register(const char *name,
+					       const char *devicefile,
+					       unsigned long flags)
+{
+	return -ENOSYS;
+}
+
+#endif
+
 #endif /* MACH_STM32MP_BBU_H_ */
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 7/7] fastboot: support TF-A FSBL and FIP images for barebox update
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (5 preceding siblings ...)
  2022-06-02  9:01 ` [PATCH 6/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
@ 2022-06-02  9:01 ` Ahmad Fatoum
  2022-06-03  7:17 ` [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Sascha Hauer
  7 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2022-06-02  9:01 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Newly added stm32mp_bbu_mmc_fip_register() accepts two kinds of barebox
images: A FIP image containing barebox as well as a FIP image preceded
by TF-A as a STM32 FSBL image. Inform filetype_is_barebox_image of these
file types, so the handler can be invoked via fastboot when
global.fastboot.bbu=1.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/filetype.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/common/filetype.c b/common/filetype.c
index 3e9e14c1d79e..8f79f48bc122 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -484,6 +484,8 @@ bool filetype_is_barebox_image(enum filetype ft)
 	case filetype_ch_image_be:
 	case filetype_layerscape_image:
 	case filetype_layerscape_qspi_image:
+	case filetype_stm32_image_fsbl_v1:
+	case filetype_fip:
 		return true;
 	default:
 		return false;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler
  2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
                   ` (6 preceding siblings ...)
  2022-06-02  9:01 ` [PATCH 7/7] fastboot: support TF-A FSBL and FIP images for barebox update Ahmad Fatoum
@ 2022-06-03  7:17 ` Sascha Hauer
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2022-06-03  7:17 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Thu, Jun 02, 2022 at 11:01:26AM +0200, Ahmad Fatoum wrote:
> STM32MP1 BootROM boots from GPT partition fsbl1 or fsbl2 on SD-Card and
> from boot partition on eMMC. Recent TF-A without legacy image support will
> then look in a GPT partition named fip in the user area.
> 
> With recent patches[1], TF-A will also check offset SZ_256K in the boot
> partition to see if the FIP is there.
> 
> Add a barebox_update handler that covers these scenarios.
> 
> The TF-A patches are not yet upstream, but have all maintainer ACKs and
> will likely be part of TF-A v2.8.
> 
> [1]: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/15332
> 
> Ahmad Fatoum (7):
>   bbu: move barebox_update eMMC boot handling into common code
>   bbu: use free(NULL) to simplify function cleanup
>   bbu: add flag for enabling eMMC boot ack
>   bbu: export bbu_std_file_handler for use in custom handlers
>   filetype: differentiate between STM32MP FSBL and SSBL images
>   ARM: stm32mp: bbu: add FIP update handler
>   fastboot: support TF-A FSBL and FIP images for barebox update

Applied, thanks

Sascha

> 
>  arch/arm/mach-imx/imx-bbu-internal.c     |  48 +-----
>  arch/arm/mach-stm32mp/Makefile           |   1 +
>  arch/arm/mach-stm32mp/bbu.c              | 197 +++++++++++++++++++++++
>  arch/arm/mach-stm32mp/include/mach/bbu.h |  18 ++-
>  arch/arm/mach-stm32mp/stm32image.c       |   2 +-
>  common/bbu.c                             | 100 ++++++++++--
>  common/filetype.c                        |  15 +-
>  include/bbu.h                            |   7 +
>  include/filetype.h                       |   3 +-
>  9 files changed, 327 insertions(+), 64 deletions(-)
>  create mode 100644 arch/arm/mach-stm32mp/bbu.c
> 
> -- 
> 2.30.2
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-06-03  7:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-02  9:01 [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 1/7] bbu: move barebox_update eMMC boot handling into common code Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 2/7] bbu: use free(NULL) to simplify function cleanup Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 3/7] bbu: add flag for enabling eMMC boot ack Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 4/7] bbu: export bbu_std_file_handler for use in custom handlers Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 5/7] filetype: differentiate between STM32MP FSBL and SSBL images Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 6/7] ARM: stm32mp: bbu: add FIP update handler Ahmad Fatoum
2022-06-02  9:01 ` [PATCH 7/7] fastboot: support TF-A FSBL and FIP images for barebox update Ahmad Fatoum
2022-06-03  7:17 ` [PATCH 0/7] ARM: stm32mp: bbu: add FIP update handler Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox