{"id":2196996,"url":"http://patchwork.ozlabs.org/api/1.0/patches/2196996/?format=json","project":{"id":18,"url":"http://patchwork.ozlabs.org/api/1.0/projects/18/?format=json","name":"U-Boot","link_name":"uboot","list_id":"u-boot.lists.denx.de","list_email":"u-boot@lists.denx.de","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<9b0880ae234d8244f032eac6cee32a1031de26a3.1771275704.git.daniel@makrotopia.org>","date":"2026-02-16T21:21:33","name":"[RFC,02/20] boot: image-loader: add block device backend","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"69600afb402bf6cafb83f4ed9ae007e7e9921f53","submitter":{"id":64091,"url":"http://patchwork.ozlabs.org/api/1.0/people/64091/?format=json","name":"Daniel Golle","email":"daniel@makrotopia.org"},"delegate":{"id":3651,"url":"http://patchwork.ozlabs.org/api/1.0/users/3651/?format=json","username":"trini","first_name":"Tom","last_name":"Rini","email":"trini@ti.com"},"mbox":"http://patchwork.ozlabs.org/project/uboot/patch/9b0880ae234d8244f032eac6cee32a1031de26a3.1771275704.git.daniel@makrotopia.org/mbox/","series":[{"id":492351,"url":"http://patchwork.ozlabs.org/api/1.0/series/492351/?format=json","date":"2026-02-16T21:21:14","name":"boot: add OpenWrt boot method and on-demand FIT loading","version":1,"mbox":"http://patchwork.ozlabs.org/series/492351/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2196996/checks/","tags":{},"headers":{"Return-Path":"<u-boot-bounces@lists.denx.de>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de\n (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de;\n envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org)","phobos.denx.de;\n dmarc=none (p=none dis=none) header.from=makrotopia.org","phobos.denx.de;\n spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de","phobos.denx.de; dmarc=none (p=none dis=none)\n header.from=makrotopia.org","phobos.denx.de;\n spf=pass smtp.mailfrom=daniel@makrotopia.org"],"Received":["from phobos.denx.de (phobos.denx.de\n [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fFG3G16N2z1xwD\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 17 Feb 2026 08:23:22 +1100 (AEDT)","from h2850616.stratoserver.net (localhost [IPv6:::1])\n\tby phobos.denx.de (Postfix) with ESMTP id 45DBB83CD3;\n\tMon, 16 Feb 2026 22:22:57 +0100 (CET)","by phobos.denx.de (Postfix, from userid 109)\n id 9FDCA83AA9; Mon, 16 Feb 2026 22:21:59 +0100 (CET)","from pidgin.makrotopia.org (pidgin.makrotopia.org\n [IPv6:2a07:2ec0:3002::65])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))\n (No client certificate requested)\n by phobos.denx.de (Postfix) with ESMTPS id CE3B483B8A\n for <u-boot@lists.denx.de>; Mon, 16 Feb 2026 22:21:53 +0100 (CET)","from local\n by pidgin.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256)\n (Exim 4.99) (envelope-from <daniel@makrotopia.org>)\n id 1vs62C-000000002cS-3u9J; Mon, 16 Feb 2026 21:21:37 +0000"],"X-Spam-Checker-Version":"SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de","X-Spam-Level":"","X-Spam-Status":"No, score=-1.9 required=5.0 tests=BAYES_00,\n RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham\n autolearn_force=no version=3.4.2","Date":"Mon, 16 Feb 2026 21:21:33 +0000","From":"Daniel Golle <daniel@makrotopia.org>","To":"Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,\n Quentin Schulz <quentin.schulz@cherry.de>,\n Daniel Golle <daniel@makrotopia.org>,\n Kory Maincent <kory.maincent@bootlin.com>,\n Mattijs Korpershoek <mkorpershoek@kernel.org>,\n Martin Schwan <m.schwan@phytec.de>, Anshul Dalal <anshuld@ti.com>,\n Ilias Apalodimas <ilias.apalodimas@linaro.org>,\n Sughosh Ganu <sughosh.ganu@arm.com>, Aristo Chen <jj251510319013@gmail.com>,\n\t=?utf-8?b?54mbIOW/l+Wujw==?= <Zone.Niuzh@hotmail.com>,\n Marek Vasut <marek.vasut+renesas@mailbox.org>,\n Heinrich Schuchardt <xypron.glpk@gmx.de>,\n Wolfgang Wallner <wolfgang.wallner@at.abb.com>,\n Frank Wunderlich <frank-w@public-files.de>,\n David Lechner <dlechner@baylibre.com>,\n Osama Abdelkader <osama.abdelkader@gmail.com>,\n Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>,\n Michael Trimarchi <michael@amarulasolutions.com>,\n Miquel Raynal <miquel.raynal@bootlin.com>,\n Andrew Goodbody <andrew.goodbody@linaro.org>,\n Yegor Yefremov <yegorslists@googlemail.com>,\n Mike Looijmans <mike.looijmans@topic.nl>,\n Weijie Gao <weijie.gao@mediatek.com>,\n Alexander Stein <alexander.stein@ew.tq-group.com>,\n Neil Armstrong <neil.armstrong@linaro.org>,\n Mayuresh Chitale <mchitale@ventanamicro.com>,\n Paul HENRYS <paul.henrys_ext@softathome.com>, u-boot@lists.denx.de","Cc":"John Crispin <john@phrozen.org>, Paul Spooren <mail@aparcar.org>","Subject":"[RFC PATCH 02/20] boot: image-loader: add block device backend","Message-ID":"\n <9b0880ae234d8244f032eac6cee32a1031de26a3.1771275704.git.daniel@makrotopia.org>","References":"<cover.1771275704.git.daniel@makrotopia.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<cover.1771275704.git.daniel@makrotopia.org>","X-Mailman-Approved-At":"Mon, 16 Feb 2026 22:22:55 +0100","X-BeenThere":"u-boot@lists.denx.de","X-Mailman-Version":"2.1.39","Precedence":"list","List-Id":"U-Boot discussion <u-boot.lists.denx.de>","List-Unsubscribe":"<https://lists.denx.de/options/u-boot>,\n <mailto:u-boot-request@lists.denx.de?subject=unsubscribe>","List-Archive":"<https://lists.denx.de/pipermail/u-boot/>","List-Post":"<mailto:u-boot@lists.denx.de>","List-Help":"<mailto:u-boot-request@lists.denx.de?subject=help>","List-Subscribe":"<https://lists.denx.de/listinfo/u-boot>,\n <mailto:u-boot-request@lists.denx.de?subject=subscribe>","Errors-To":"u-boot-bounces@lists.denx.de","Sender":"\"U-Boot\" <u-boot-bounces@lists.denx.de>","X-Virus-Scanned":"clamav-milter 0.103.8 at phobos.denx.de","X-Virus-Status":"Clean"},"content":"Add a block device storage backend for the image_loader framework.\n\nimage_loader_init_blk() takes a device and partition specification\nstring, resolves the partition, and installs a .read() callback that\ntranslates byte-offset reads into blk_dread() calls with proper\nsector alignment.\n\nPartitions can be identified by number or by name, following the\nsyntax of part_get_info_by_dev_and_name_or_num():\n  \"0:4\"        partition 4 on device 0\n  \"0#kernel\"   partition named \"kernel\" on device 0\n\nThis is important for systems where partition numbers are not stable\nacross firmware updates.\n\nSub-sector reads (offset or size not aligned to blk_desc->blksz) use\na single-sector bounce buffer to avoid overreading into adjacent RAM.\n\nThe .cleanup callback frees the allocated private context.\n\nGated by CONFIG_IMAGE_LOADER_BLK (depends on BLK && PARTITIONS &&\nIMAGE_LOADER).\n\nSigned-off-by: Daniel Golle <daniel@makrotopia.org>\n---\n boot/Kconfig            |   8 +++\n boot/Makefile           |   1 +\n boot/image-loader-blk.c | 133 ++++++++++++++++++++++++++++++++++++++++\n include/image-loader.h  |  20 ++++++\n 4 files changed, 162 insertions(+)\n create mode 100644 boot/image-loader-blk.c","diff":"diff --git a/boot/Kconfig b/boot/Kconfig\nindex f6908e04a51..e94b52288a3 100644\n--- a/boot/Kconfig\n+++ b/boot/Kconfig\n@@ -1187,6 +1187,14 @@ config IMAGE_LOADER_MAX_REGIONS\n \t  images (FDT structure + kernel + device tree + ramdisk +\n \t  a few loadable sub-images).\n \n+config IMAGE_LOADER_BLK\n+\tbool \"Block device backend for image loader\"\n+\tdepends on IMAGE_LOADER && BLK && PARTITIONS\n+\thelp\n+\t  Allows loading images from block device partitions (MMC, SATA,\n+\t  USB, etc.) using the image_loader framework. Partitions can\n+\t  be identified by number or name.\n+\n config DISTRO_DEFAULTS\n \tbool \"(deprecated) Script-based booting of Linux distributions\"\n \tselect CMDLINE\ndiff --git a/boot/Makefile b/boot/Makefile\nindex 1dbc285dad8..ac006bbaa82 100644\n--- a/boot/Makefile\n+++ b/boot/Makefile\n@@ -74,6 +74,7 @@ obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_SIMPLE_OS) += vbe_simple_os.o\n obj-$(CONFIG_$(PHASE_)BOOTMETH_ANDROID) += bootmeth_android.o\n \n obj-$(CONFIG_IMAGE_LOADER) += image-loader.o\n+obj-$(CONFIG_IMAGE_LOADER_BLK) += image-loader-blk.o\n \n obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_ABREC) += vbe_abrec.o vbe_common.o\n obj-$(CONFIG_$(PHASE_)BOOTMETH_VBE_ABREC_FW) += vbe_abrec_fw.o\ndiff --git a/boot/image-loader-blk.c b/boot/image-loader-blk.c\nnew file mode 100644\nindex 00000000000..3f9a309a60c\n--- /dev/null\n+++ b/boot/image-loader-blk.c\n@@ -0,0 +1,133 @@\n+// SPDX-License-Identifier: GPL-2.0+\n+/*\n+ * Block device backend for image_loader\n+ *\n+ * Copyright (C) 2026 Daniel Golle <daniel@makrotopia.org>\n+ */\n+\n+#include <blk.h>\n+#include <image-loader.h>\n+#include <malloc.h>\n+#include <memalign.h>\n+#include <part.h>\n+#include <log.h>\n+\n+struct image_loader_blk_priv {\n+\tstruct blk_desc *desc;\n+\tlbaint_t part_start;\n+\tlbaint_t part_size;\n+};\n+\n+/**\n+ * blk_read_partial() - Read a partial sector via bounce buffer\n+ *\n+ * Reads one full sector into a stack-allocated bounce buffer, then\n+ * copies @len bytes starting at byte offset @skip within that sector\n+ * into @dst.\n+ *\n+ * @desc:\tBlock device descriptor\n+ * @lba:\tAbsolute LBA of the sector to read\n+ * @skip:\tByte offset within the sector\n+ * @len:\tNumber of bytes to copy\n+ * @dst:\tDestination buffer\n+ * Return: 0 on success, -EIO on read failure\n+ */\n+static int blk_read_partial(struct blk_desc *desc, lbaint_t lba,\n+\t\t\t    ulong skip, ulong len, void *dst)\n+{\n+\tALLOC_CACHE_ALIGN_BUFFER(u8, sec, desc->blksz);\n+\n+\tif (blk_dread(desc, lba, 1, sec) != 1)\n+\t\treturn -EIO;\n+\n+\tmemcpy(dst, sec + skip, len);\n+\n+\treturn 0;\n+}\n+\n+static int image_loader_blk_read(struct image_loader *ldr, ulong src,\n+\t\t\t\t ulong size, void *dst)\n+{\n+\tstruct image_loader_blk_priv *priv = ldr->priv;\n+\tstruct blk_desc *desc = priv->desc;\n+\tunsigned long blksz = desc->blksz;\n+\tlbaint_t lba = priv->part_start + src / blksz;\n+\tulong head = src % blksz;\n+\tu8 *out = dst;\n+\tlbaint_t n;\n+\tint ret;\n+\n+\t/* Bounds check */\n+\tif (src + size > (ulong)priv->part_size * blksz) {\n+\t\tlog_err(\"image_loader_blk: read at 0x%lx+0x%lx exceeds partition size\\n\",\n+\t\t\tsrc, size);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/* Handle unaligned head */\n+\tif (head) {\n+\t\tulong chunk = min(size, blksz - head);\n+\n+\t\tret = blk_read_partial(desc, lba, head, chunk, out);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tout += chunk;\n+\t\tsize -= chunk;\n+\t\tlba++;\n+\t}\n+\n+\t/* Aligned middle — read whole sectors directly into dst */\n+\tif (size >= blksz) {\n+\t\tn = size / blksz;\n+\n+\t\tif (blk_dread(desc, lba, n, out) != n)\n+\t\t\treturn -EIO;\n+\n+\t\tout += n * blksz;\n+\t\tsize -= n * blksz;\n+\t\tlba += n;\n+\t}\n+\n+\t/* Handle unaligned tail */\n+\tif (size) {\n+\t\tret = blk_read_partial(desc, lba, 0, size, out);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static void image_loader_blk_cleanup(struct image_loader *ldr)\n+{\n+\tfree(ldr->priv);\n+}\n+\n+int image_loader_init_blk(struct image_loader *ldr, const char *ifname,\n+\t\t\t  const char *dev_part_str)\n+{\n+\tstruct image_loader_blk_priv *priv;\n+\tstruct blk_desc *desc;\n+\tstruct disk_partition info;\n+\tint ret;\n+\n+\tret = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str,\n+\t\t\t\t\t\t   &desc, &info, 0);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tpriv = malloc(sizeof(*priv));\n+\tif (!priv)\n+\t\treturn -ENOMEM;\n+\n+\tpriv->desc = desc;\n+\tpriv->part_start = info.start;\n+\tpriv->part_size = info.size;\n+\n+\tldr->read = image_loader_blk_read;\n+\tldr->cleanup = image_loader_blk_cleanup;\n+\tldr->priv = priv;\n+\n+\treturn 0;\n+}\ndiff --git a/include/image-loader.h b/include/image-loader.h\nindex e273b1ca50f..1a9048ba482 100644\n--- a/include/image-loader.h\n+++ b/include/image-loader.h\n@@ -138,4 +138,24 @@ void *image_loader_map(struct image_loader *ldr, ulong img_offset,\n void *image_loader_map_to(struct image_loader *ldr, ulong img_offset,\n \t\t\t  ulong size, void *dst);\n \n+/**\n+ * image_loader_init_blk() - Initialise loader for a block device partition\n+ *\n+ * Resolves the partition using @ifname and @dev_part_str, then installs\n+ * a .read() callback that translates byte-offset reads into blk_dread()\n+ * calls. The dev_part_str accepts the same formats as\n+ * part_get_info_by_dev_and_name_or_num():\n+ *\n+ *   \"0:4\"        partition 4 on device 0\n+ *   \"0#kernel\"   partition named \"kernel\" on device 0\n+ *   \"0:1\"        partition 1 on device 0\n+ *\n+ * @ldr:\t\tThe image loader to initialise\n+ * @ifname:\t\tBlock interface name (e.g. \"mmc\", \"scsi\")\n+ * @dev_part_str:\tDevice and partition specification\n+ * Return: 0 on success, negative errno on failure\n+ */\n+int image_loader_init_blk(struct image_loader *ldr, const char *ifname,\n+\t\t\t  const char *dev_part_str);\n+\n #endif /* __IMAGE_LOADER_H */\n","prefixes":["RFC","02/20"]}