mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: "open list:BAREBOX" <barebox@lists.infradead.org>
Subject: [PATCH v2 14/20] ARM: k3: add eMMC barebox update handler
Date: Wed, 12 Feb 2025 15:09:27 +0100	[thread overview]
Message-ID: <20250212-k3-emmc-v2-14-8dd1bb0ce60a@pengutronix.de> (raw)
In-Reply-To: <20250212-k3-emmc-v2-0-8dd1bb0ce60a@pengutronix.de>

This adds a barebox update handler to write a barebox image to the
eMMC boot partitions. The image is written to the currently inactive
boot partition which is made active after a successful write thus
enabling a failsafe update.

The update handler expects an image which has the tiboot3.bin right
at the start of the image and a FIP image containing the next stages
at offset 1MiB. This image can't be generated by barebox itself (as
it requires two barebox images built for different architectures). It
must be generated by an external build system.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-k3/Makefile |  1 +
 arch/arm/mach-k3/bbu.c    | 77 +++++++++++++++++++++++++++++++++++++++++++++++
 include/mach/k3/common.h  | 13 ++++++++
 3 files changed, 91 insertions(+)

diff --git a/arch/arm/mach-k3/Makefile b/arch/arm/mach-k3/Makefile
index 8120c6fd35..b1dfb66d74 100644
--- a/arch/arm/mach-k3/Makefile
+++ b/arch/arm/mach-k3/Makefile
@@ -1,6 +1,7 @@
 obj-y += common.o
 obj-pbl-$(CONFIG_MACH_K3_CORTEX_R5) += r5.o
 obj-pbl-y += ddrss.o
+obj-$(CONFIG_BAREBOX_UPDATE) += bbu.o
 
 extra-$(CONFIG_MACH_K3_CORTEX_R5) += combined-dm-cfg-am625.k3cfg combined-sysfw-cfg-am625.k3cfg
 
diff --git a/arch/arm/mach-k3/bbu.c b/arch/arm/mach-k3/bbu.c
new file mode 100644
index 0000000000..815af086e8
--- /dev/null
+++ b/arch/arm/mach-k3/bbu.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "k3-bbu: " fmt
+
+#include <bbu.h>
+#include <xfuncs.h>
+#include <fcntl.h>
+#include <filetype.h>
+#include <linux/printk.h>
+#include <unistd.h>
+#include <mach/k3/common.h>
+#include <linux/sizes.h>
+#include <libfile.h>
+
+static int k3_bbu_mmc_update(struct bbu_handler *handler,
+				  struct bbu_data *data)
+{
+	int fd, ret;
+	enum filetype type;
+
+	if (data->len < K3_EMMC_BOOTPART_TIBOOT3_BIN_SIZE + SZ_1K) {
+		pr_err("Image is too small\n");
+		return -EINVAL;
+	}
+
+	type = file_detect_type(data->image + K3_EMMC_BOOTPART_TIBOOT3_BIN_SIZE, SZ_1K);
+
+	if (type != filetype_fip) {
+		pr_err("Cannot find FIP image at offset 1M\n");
+		return -EINVAL;
+	}
+
+	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 = pwrite_full(fd, data->image, data->len, 0);
+	if (ret < 0)
+		pr_err("writing to %s failed with %pe\n", data->devicefile, ERR_PTR(ret));
+
+	close(fd);
+
+	return ret < 0 ? ret : 0;
+}
+
+static int k3_bbu_mmc_fip_handler(struct bbu_handler *handler,
+				       struct bbu_data *data)
+{
+	return bbu_mmcboot_handler(handler, data, k3_bbu_mmc_update);
+}
+
+int k3_bbu_emmc_register(const char *name,
+			 const char *devicefile,
+			 unsigned long flags)
+{
+	struct bbu_handler *handler;
+	int ret;
+
+        handler = xzalloc(sizeof(*handler));
+
+        handler->flags = flags | BBU_HANDLER_FLAG_MMC_BOOT_ACK;
+        handler->devicefile = devicefile;
+        handler->name = name;
+        handler->handler = k3_bbu_mmc_fip_handler;
+
+        ret = bbu_register_handler(handler);
+        if (ret)
+                free(handler);
+
+        return ret;
+}
diff --git a/include/mach/k3/common.h b/include/mach/k3/common.h
index 14dfc4a28a..871e9f39e3 100644
--- a/include/mach/k3/common.h
+++ b/include/mach/k3/common.h
@@ -16,4 +16,17 @@ void am625_enable_32k_crystal(void);
 
 #define K3_EMMC_BOOTPART_TIBOOT3_BIN_SIZE	SZ_1M
 
+#ifdef CONFIG_BAREBOX_UPDATE
+int k3_bbu_emmc_register(const char *name,
+			 const char *devicefile,
+			 unsigned long flags);
+#else
+static inline int k3_bbu_emmc_register(const char *name,
+				       const char *devicefile,
+				       unsigned long flags)
+{
+	return 0;
+}
+#endif
+
 #endif /* __MACH_K3_COMMON_H */

-- 
2.39.5




  parent reply	other threads:[~2025-02-12 14:33 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-12 14:09 [PATCH v2 00/20] ARM: K3 updates Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 01/20] ARM: k3: Add function to enable 32k crystal Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 02/20] ARM: k3: add function to detect eMMC boot Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 03/20] ARM: k3: do not mount /boot when booting from eMMC Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 04/20] fip: drop typedefs Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 05/20] fip: use linux list implementation Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 06/20] fip: use uuid_equal() and uuid_is_null() Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 07/20] fiptool: do not typedef structs Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 08/20] fip: add fip_ prefix Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 09/20] fip: add fip_image_open() Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 10/20] ARM: k3: r5: add USB DFU and eMMC boot support Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 11/20] ARM: am625-sk: enable 32k crystal Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 12/20] mci: am654: parse generic mmc node properties Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 13/20] ARM: k3: limit eMMC frequency to 26MHz Sascha Hauer
2025-02-12 14:09 ` Sascha Hauer [this message]
2025-02-12 14:09 ` [PATCH v2 15/20] ARM: am625-sk: put environment on eMMC when booting from it Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 16/20] serial: omap: Use ttyS as Linux console name Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 17/20] ARM: k3: remove beagleplay FIT image Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 18/20] ARM: am625-sk: cleanup board entry Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 19/20] ARM: beagleplay: " Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 20/20] ARM: k3: Add k3-r5_defconfig Sascha Hauer
2025-02-17 11:14 ` [PATCH v2 00/20] ARM: K3 updates Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250212-k3-emmc-v2-14-8dd1bb0ce60a@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox