From a5ca0e0214c8d283e8a47030ff32b55fbc9c04ae Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:08:53 +0300 Subject: [PATCH 1/2] infinity6c: the split NOR layout, without device tree partitions The split NOR layout for the 5.10.61 SigmaStar infinity6c kernel, the same code as infinity6 (#195) and the other 4.9 kernels, on 5.10's partition model: - drivers/mtd/split_layout.c (MTD_SPLIT_LAYOUT): when the uImage at 0x50000 is followed by the root squashfs at the next 64 KiB boundary, register boot, env, firmware (to the end of the chip) and inside it kernel, rootfs and rootfs_data; the bootloader's mtdparts= is not used then. Anything else in the firmware area -- the fixed slots above all -- or a read error leaves the driver's partitions as before. 5.10's add_mtd_partitions() removes what it added when one partition fails, so the table is all there or not at all, and mtdpart.c needs none of the 4.9 port's bookkeeping; the root is looked up on the master's own partition list. - sstar/flash/nor/mtd_serflash.c (SS_NOR_ONEBIN, what infinity6c builds): tries that first, then registers as before with the command line's partitions. A registration that fails falls back to them. - init/do_mounts.c: root_dev_from_flash: the split rootfs is the root, and a root= naming an MTD device gives way to it. The SigmaStar U-Boot reads 2 MiB from 0x50000 and bootm's it, so it boots this layout unchanged. infinity6c's 8 MB lite images outgrew the fixed 5120K rootfs slot (OpenIPC/firmware ssc377/ssc378 lite: "size exceeded by 4KB"), and the split image lets the kernel's slack in its 2 MiB slot go to them. Off unless a board config sets CONFIG_MTD_SPLIT_LAYOUT=y. --- drivers/mtd/Kconfig | 12 ++ drivers/mtd/Makefile | 1 + drivers/mtd/split_layout.c | 199 +++++++++++++++++++++++++ drivers/sstar/flash/nor/mtd_serflash.c | 13 ++ include/linux/mtd/split_layout.h | 21 +++ include/linux/root_dev.h | 7 + init/do_mounts.c | 35 +++++ 7 files changed, 288 insertions(+) create mode 100644 drivers/mtd/split_layout.c create mode 100644 include/linux/mtd/split_layout.h diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 65cfacfa734..6c51fc67b50 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -190,6 +190,18 @@ config MTD_SWAP The driver provides wear leveling by storing erase counter into the OOB. +config MTD_SPLIT_LAYOUT + bool "OpenIPC split NOR layout, without device tree partitions" + depends on MTD=y && MTD_BLOCK=y + help + When the NOR flash holds a uImage at 0x50000 with the root squashfs + at the next 64 KiB boundary (OpenIPC's firmware.bin), register boot, + env, firmware and, inside it, kernel, rootfs and rootfs_data from the + images themselves, and mount the root from that rootfs whatever + MTD root= the bootloader passes. Any other contents of the flash + keep the driver's usual partitions. Only drivers that call + mtd_split_layout_register() take part. + config MTD_PARTITIONED_MASTER bool "Retain master device when partitioned" default n diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile index 593d0593a03..82aa47f53a3 100644 --- a/drivers/mtd/Makefile +++ b/drivers/mtd/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_MTD) += mtd.o mtd-y := mtdcore.o mtdsuper.o mtdconcat.o mtdpart.o mtdchar.o obj-y += parsers/ +obj-$(CONFIG_MTD_SPLIT_LAYOUT) += split_layout.o # 'Users' - code which presents functionality to userspace. obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o diff --git a/drivers/mtd/split_layout.c b/drivers/mtd/split_layout.c new file mode 100644 index 00000000000..e55219b962b --- /dev/null +++ b/drivers/mtd/split_layout.c @@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The split NOR layout, for kernels without device tree partitions. + * + * OpenIPC writes one firmware.bin at the start of the firmware area (0x50000, + * after a 256K bootloader and its 64K environment): a legacy uImage, then the + * root squashfs at the next 64 KiB boundary. Nothing records where one ends + * and the next starts but the images themselves, and on the device tree + * kernels mtdsplit reads them to divide the "firmware" partition at boot. + * This kernel takes its NOR partitions from the command line, not the device + * tree, so this reads the same two headers and registers the partitions + * mtdsplit would have made: boot, env, firmware (to the end of the chip) and, + * inside it, kernel, rootfs and rootfs_data, in the order and with the names + * the device tree kernels give them. Whatever the bootloader passes in + * mtdparts= describes its own idea of the flash and is ignored, as on those + * kernels; so is a root= naming an MTD device (init/do_mounts.c). + * + * Any other flash contents -- the fixed kernel and rootfs slots above all, + * whose squashfs sits at the 2 MiB slot boundary rather than right after the + * kernel -- are not this layout, and the driver registers its partitions as + * it always did. + * + * The same code as the 4.9 kernels' (infinity6, goke-gk710x, hi3516cv100, + * hi3520dv200), on the 5.10 partition model: add_mtd_partitions() removes + * what it added when one partition fails, so the table is either all there + * or not at all, and the partitions hang off master->partitions. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define SPLIT_BOOT_SIZE 0x40000ULL +#define SPLIT_ENV_SIZE 0x10000ULL +#define SPLIT_FW_START (SPLIT_BOOT_SIZE + SPLIT_ENV_SIZE) +#define SPLIT_BLOCK 0x10000ULL + +#define UIMAGE_HDR_SIZE 64 +#define UIMAGE_MAGIC 0x27051956 +#define SQUASHFS_MAGIC_LE 0x73717368 /* "hsqs" */ +#define SQUASHFS_SB_READ 48 /* through bytes_used */ + +static u64 split_round(u64 x) +{ + return (x + SPLIT_BLOCK - 1) & ~(SPLIT_BLOCK - 1); +} + +/* len bytes at from, all of them or an error. A corrected bitflip is a read. */ +static int split_read(struct mtd_info *mtd, loff_t from, size_t len, u_char *buf) +{ + size_t retlen = 0; + int err; + + err = mtd_read(mtd, from, len, &retlen, buf); + if (err == -EUCLEAN) + err = 0; + if (!err && retlen != len) + err = -EIO; + return err; +} + +static u32 be32_at(const u8 *p) +{ + return ((u32)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; +} + +static u32 le32_at(const u8 *p) +{ + return p[0] | (p[1] << 8) | (p[2] << 16) | ((u32)p[3] << 24); +} + +/* + * Where the kernel image ends and the rootfs starts, and where the rootfs + * ends, when the firmware area holds this layout; -ENOENT when it does not. + */ +static int split_find(struct mtd_info *mtd, u64 *rootfs, u64 *data) +{ + u8 buf[UIMAGE_HDR_SIZE]; + u64 used; + int err; + + if (mtd->type != MTD_NORFLASH || !mtd->erasesize || + SPLIT_BLOCK % mtd->erasesize || + mtd->size < SPLIT_FW_START + 2 * SPLIT_BLOCK) + return -ENOENT; + + err = split_read(mtd, SPLIT_FW_START, UIMAGE_HDR_SIZE, buf); + if (err) + return err; + if (be32_at(buf) != UIMAGE_MAGIC || !be32_at(buf + 12)) + return -ENOENT; + *rootfs = split_round(SPLIT_FW_START + UIMAGE_HDR_SIZE + be32_at(buf + 12)); + if (*rootfs + SPLIT_BLOCK > mtd->size) + return -ENOENT; + + err = split_read(mtd, *rootfs, SQUASHFS_SB_READ, buf); + if (err) + return err; + if (le32_at(buf) != SQUASHFS_MAGIC_LE) + return -ENOENT; + /* bytes_used: 64 bits, so a corrupt high word cannot wrap past the chip */ + used = le32_at(buf + 40) | ((u64)le32_at(buf + 44) << 32); + if (!used || used > mtd->size - *rootfs) + return -ENOENT; + *data = split_round(*rootfs + used); + if (*data > mtd->size) + return -ENOENT; + return 0; +} + +/* + * master's partition named name, if add_mtd_device() took it: the root comes + * from this flash's own "rootfs", not whichever MTD device a global name + * lookup finds first. + */ +static struct mtd_info *split_part(struct mtd_info *master, const char *name) +{ + struct mtd_info *child, *found = NULL; + + mutex_lock(&master->master.partitions_lock); + list_for_each_entry(child, &master->partitions, part.node) + if (!strcmp(child->name, name) && + device_is_registered(&child->dev)) { + found = child; + break; + } + mutex_unlock(&master->master.partitions_lock); + return found; +} + +int mtd_split_layout_register(struct mtd_info *master) +{ + /* no parsers: mtd_device_parse_register() then takes parts as given */ + static const char *const no_parsers[] = { NULL }; + struct mtd_partition *parts; + struct mtd_info *root; + u64 rootfs, data; + int n = 0, err; + + err = split_find(master, &rootfs, &data); + if (err == -ENOENT) + return 1; + if (err) { + pr_warn("%s: reading the firmware area failed (%d), " + "taking the command line's partitions\n", master->name, err); + return 1; + } + + parts = kcalloc(6, sizeof(*parts), GFP_KERNEL); + if (!parts) + return -ENOMEM; + parts[n].name = "boot"; + parts[n].offset = 0; + parts[n++].size = SPLIT_BOOT_SIZE; + parts[n].name = "env"; + parts[n].offset = SPLIT_BOOT_SIZE; + parts[n++].size = SPLIT_ENV_SIZE; + parts[n].name = "firmware"; + parts[n].offset = SPLIT_FW_START; + parts[n++].size = master->size - SPLIT_FW_START; + parts[n].name = "kernel"; + parts[n].offset = SPLIT_FW_START; + parts[n++].size = rootfs - SPLIT_FW_START; + parts[n].name = "rootfs"; + parts[n].offset = rootfs; + parts[n++].size = data - rootfs; + /* a squashfs that fills the chip leaves no overlay, as mtdsplit does */ + if (data < master->size) { + parts[n].name = "rootfs_data"; + parts[n].offset = data; + parts[n++].size = master->size - data; + } + + pr_notice("%s: split NOR layout, kernel 0x%llx-0x%llx, " + "rootfs 0x%llx-0x%llx, rootfs_data from 0x%llx\n", master->name, + SPLIT_FW_START, rootfs, rootfs, data, data); + err = mtd_device_parse_register(master, no_parsers, NULL, parts, n); + kfree(parts); + if (err) + return err; + + root = split_part(master, "rootfs"); + if (!root) { + /* add_mtd_partitions() reported success; this is not expected */ + pr_warn("%s: split NOR layout registered without its rootfs\n", + master->name); + return 0; + } + /* The root is the "rootfs" just registered, as MTD_ROOTFS_ROOT_DEV does it. */ + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, root->index); + root_dev_from_flash = true; + pr_notice("split NOR layout: root on mtdblock%d\n", root->index); + return 0; +} +EXPORT_SYMBOL_GPL(mtd_split_layout_register); diff --git a/drivers/sstar/flash/nor/mtd_serflash.c b/drivers/sstar/flash/nor/mtd_serflash.c index 8a26313a66a..d194d241a27 100644 --- a/drivers/sstar/flash/nor/mtd_serflash.c +++ b/drivers/sstar/flash/nor/mtd_serflash.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -505,6 +506,18 @@ static int serflash_probe(struct platform_device *pdev) flash->mtd.name, (unsigned int)flash->mtd.size, (unsigned int)flash->mtd.size / (1024 * 1024), (unsigned int)flash->mtd.erasesize, flash->mtd.numeraseregions); platform_set_drvdata(pdev, &flash->mtd); + + /* firmware.bin at 0x50000: its own partitions, not the command line's */ + { + int split = mtd_split_layout_register(&flash->mtd); + + if (!split) + return 0; + if (split < 0) + printk(KERN_WARNING "split NOR layout not registered (%d), " + "taking the command line's partitions\n", split); + } + return mtd_device_register(&flash->mtd, NULL, 0); out: if (flash) diff --git a/include/linux/mtd/split_layout.h b/include/linux/mtd/split_layout.h new file mode 100644 index 00000000000..079cb0da572 --- /dev/null +++ b/include/linux/mtd/split_layout.h @@ -0,0 +1,21 @@ +#ifndef __MTD_SPLIT_LAYOUT_H__ +#define __MTD_SPLIT_LAYOUT_H__ + +struct mtd_info; + +/* + * Register master and its partitions from the split NOR layout (see + * drivers/mtd/split_layout.c): 0 when it did, 1 when the flash does not hold + * that layout and the caller registers master as before, a negative errno + * when the registration itself failed, with nothing of it left registered. + */ +#ifdef CONFIG_MTD_SPLIT_LAYOUT +int mtd_split_layout_register(struct mtd_info *master); +#else +static inline int mtd_split_layout_register(struct mtd_info *master) +{ + return 1; +} +#endif + +#endif diff --git a/include/linux/root_dev.h b/include/linux/root_dev.h index 4e78651371b..dfebd2141b8 100644 --- a/include/linux/root_dev.h +++ b/include/linux/root_dev.h @@ -22,4 +22,11 @@ enum { extern dev_t ROOT_DEV; +/* + * Set when the flash itself names the root filesystem (the "rootfs" of the + * split NOR layout, see drivers/mtd/split_layout.c): a root= naming an MTD + * device -- whatever layout the bootloader believes in -- then gives way to it. + */ +extern bool root_dev_from_flash; + #endif diff --git a/init/do_mounts.c b/init/do_mounts.c index b5f9604d0c9..f16a3e748e1 100644 --- a/init/do_mounts.c +++ b/init/do_mounts.c @@ -26,11 +26,16 @@ #include #include +#include /* MTD_BLOCK_MAJOR */ + #include "do_mounts.h" int root_mountflags = MS_RDONLY | MS_SILENT; static char * __initdata root_device_name; static char __initdata saved_root_name[64]; + +/* Set when the split NOR layout found the root (drivers/mtd/split_layout.c). */ +bool root_dev_from_flash; static int root_wait; dev_t ROOT_DEV; @@ -578,6 +583,22 @@ void __init mount_root(void) #endif } +/* + * Whether root= leaves the root to the flash: no root= at all, or one naming + * an MTD device, by name ("mtd...", "/dev/mtdblockN") or by number ("31:N", + * or its hex encoding). + */ +static bool __init root_names_flash(char *name) +{ + if (!name[0] || !strncmp(name, "mtd", 3) || + !strncmp(name, "/dev/mtdblock", 13)) + return true; + if (!strncmp(name, "/dev/", 5) || !strncmp(name, "PARTUUID=", 9) || + !strncmp(name, "ubi", 3)) + return false; + return MAJOR(name_to_dev_t(name)) == MTD_BLOCK_MAJOR; +} + /* * Prepare the namespace - decide what/where to mount, load ramdisks, etc. */ @@ -600,6 +621,20 @@ void __init prepare_namespace(void) md_run_setup(); + /* + * The bootloader's root= and rootfstype= describe its idea of the flash. + * When the kernel found the root filesystem on the flash itself, those + * give way for an MTD root; a root elsewhere (NFS, an SD card, a RAM disk) + * is the bootloader's to choose. + */ + if (root_dev_from_flash && root_names_flash(saved_root_name)) { + if (saved_root_name[0]) + pr_notice("VFS: root=%s ignored, the flash names its own root\n", + saved_root_name); + saved_root_name[0] = '\0'; + root_fs_names = NULL; + } + if (saved_root_name[0]) { root_device_name = saved_root_name; if (!strncmp(root_device_name, "mtd", 3) || From 63f5fd4fba57d32769bfcff0daa3d644b1f99e9e Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:15:55 +0300 Subject: [PATCH 2/2] split_layout: judge the squashfs as squashfs_fill_super() will, and let fixed slots win Review on #266: split_find() took any "hsqs" with an in-range bytes_used right after the kernel as the root. The root is mounted from what it accepts, and a root= naming the bootloader's partition gives way to it, so a false match is a camera that does not boot. - The superblock must be one squashfs mounts: version 4.0, a block size of 4K-1M equal to 1 << block_log, and bytes_used at least a superblock and inside the chip. - A fixed-slot image written over an older split one, without erasing the kernel slot's tail, can leave the old split squashfs right after the new kernel, its end overwritten by the slot's own rootfs. When the squashfs after the kernel runs on over the fixed rootfs slot (0x250000) and a valid superblock starts there, the flash is fixed slots, and the driver registers its partitions as before. Checked with split_find() built on the host over crafted 8 MB images (the real ssc377 squashfs; a 1 MiB uImage puts the boundary at 0x160000): split image (qemu-booted) split, rootfs 0x250000, data 0x760000 split, small kernel split, rootfs 0x160000, data 0x670000 stale split under fixed slots not split (was: split at 0x160000) fixed slots, gap erased not split squashfs version 3 not split (was: split) block_size != 1 << block_log not split (was: split) "hsqs" followed by noise not split And in qemu-bench -M ssc377: the ssc377_lite split image still boots from the published U-Boot, twice, root on mtdblock4, overlay kept. --- drivers/mtd/split_layout.c | 72 +++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/drivers/mtd/split_layout.c b/drivers/mtd/split_layout.c index e55219b962b..ece4b978076 100644 --- a/drivers/mtd/split_layout.c +++ b/drivers/mtd/split_layout.c @@ -39,10 +39,14 @@ #define SPLIT_FW_START (SPLIT_BOOT_SIZE + SPLIT_ENV_SIZE) #define SPLIT_BLOCK 0x10000ULL +/* where the fixed-slot layout keeps its rootfs: after a 2 MiB kernel slot */ +#define FIXED_ROOTFS_START (SPLIT_FW_START + 0x200000ULL) + #define UIMAGE_HDR_SIZE 64 #define UIMAGE_MAGIC 0x27051956 #define SQUASHFS_MAGIC_LE 0x73717368 /* "hsqs" */ #define SQUASHFS_SB_READ 48 /* through bytes_used */ +#define SQUASHFS_SB_SIZE 96 /* struct squashfs_super_block */ static u64 split_round(u64 x) { @@ -73,6 +77,45 @@ static u32 le32_at(const u8 *p) return p[0] | (p[1] << 8) | (p[2] << 16) | ((u32)p[3] << 24); } +static u16 le16_at(const u8 *p) +{ + return p[0] | (p[1] << 8); +} + +/* + * The squashfs superblock at off, as squashfs_fill_super() will judge it + * (version 4.0, a block size of 4K-1M that matches its log) and inside the + * chip: its bytes_used, 0 when it is none, -errno when reading failed. A + * bare magic is not enough: the root is mounted from whatever this accepts, + * and a root= naming the bootloader's partition gives way to it. + */ +static s64 split_squashfs_at(struct mtd_info *mtd, u64 off) +{ + u8 sb[SQUASHFS_SB_READ]; + u32 block_size; + u16 block_log; + u64 used; + int err; + + if (off + SPLIT_BLOCK > mtd->size) + return 0; + err = split_read(mtd, off, sizeof(sb), sb); + if (err) + return err; + if (le32_at(sb) != SQUASHFS_MAGIC_LE || + le16_at(sb + 28) != 4 || le16_at(sb + 30) != 0) + return 0; + block_size = le32_at(sb + 12); + block_log = le16_at(sb + 22); + if (block_log < 12 || block_log > 20 || block_size != 1U << block_log) + return 0; + /* bytes_used: 64 bits, so a corrupt high word cannot wrap past the chip */ + used = le32_at(sb + 40) | ((u64)le32_at(sb + 44) << 32); + if (used < SQUASHFS_SB_SIZE || used > mtd->size - off) + return 0; + return used; +} + /* * Where the kernel image ends and the rootfs starts, and where the rootfs * ends, when the firmware area holds this layout; -ENOENT when it does not. @@ -80,7 +123,7 @@ static u32 le32_at(const u8 *p) static int split_find(struct mtd_info *mtd, u64 *rootfs, u64 *data) { u8 buf[UIMAGE_HDR_SIZE]; - u64 used; + s64 used, fixed; int err; if (mtd->type != MTD_NORFLASH || !mtd->erasesize || @@ -97,15 +140,26 @@ static int split_find(struct mtd_info *mtd, u64 *rootfs, u64 *data) if (*rootfs + SPLIT_BLOCK > mtd->size) return -ENOENT; - err = split_read(mtd, *rootfs, SQUASHFS_SB_READ, buf); - if (err) - return err; - if (le32_at(buf) != SQUASHFS_MAGIC_LE) - return -ENOENT; - /* bytes_used: 64 bits, so a corrupt high word cannot wrap past the chip */ - used = le32_at(buf + 40) | ((u64)le32_at(buf + 44) << 32); - if (!used || used > mtd->size - *rootfs) + used = split_squashfs_at(mtd, *rootfs); + if (used < 0) + return used; + if (!used) return -ENOENT; + + /* + * A squashfs right after the kernel that runs on over the fixed rootfs + * slot, where another superblock starts: a fixed-slot image over an + * older split one, whose stale squashfs the slot's own has overwritten. + * The fixed slots are what the bootloader boots then. + */ + if (*rootfs < FIXED_ROOTFS_START && + *rootfs + used > FIXED_ROOTFS_START) { + fixed = split_squashfs_at(mtd, FIXED_ROOTFS_START); + if (fixed < 0) + return fixed; + if (fixed) + return -ENOENT; + } *data = split_round(*rootfs + used); if (*data > mtd->size) return -ENOENT;