diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 5c97a8f6..a3defb57 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -13,6 +13,19 @@
- Add a FAQ section to the documentation explaining the ``Filesystem has
unsupported feature(s)`` error of ``resize2fs``, caused by the ext4
``orphan_file`` feature and old e2fsprogs versions.
+- Add support for UBI (Unsorted Block Images) volumes inside an MTD partition.
+ Raw NAND flashes have no partition table but use the UBI layer to manage
+ volumes within an MTD. A partition with an ``ubi`` section is formatted with
+ UBI, wiping all of its previous content, and the specified volumes are
+ created on top of it. Volumes are either dynamic, holding a UBIFS filesystem,
+ or static for raw data, which is how a bootloader usually accesses them.
+
+ Input files of volumes with a filesystem are copied into the mounted
+ filesystem or extracted into it, if they are archives. UBIFS images are
+ written directly to the volume. Volumes without a filesystem are written
+ directly and are verified by reading the volume back.
+
+ This needs the kernel's UBI and UBIFS modules to be available.
.. _release-4.0.0:
diff --git a/doc/data/layout-ubi-nand.yaml b/doc/data/layout-ubi-nand.yaml
new file mode 100644
index 00000000..16fe568e
--- /dev/null
+++ b/doc/data/layout-ubi-nand.yaml
@@ -0,0 +1,45 @@
+api-version: 5
+
+supported-device-types:
+ - mtd
+
+partitions:
+ # Barebox environment, kept intact by not erasing it.
+ - name: barebox-environment
+ size: 512kiB
+ offset: 512kiB
+ erase: false
+
+ # Barebox, written raw to the partition with flashcp.
+ - name: barebox
+ size: 1MiB
+ offset: 512kiB
+ input:
+ filename: barebox.bin
+
+ # The rest of the NAND is formatted with UBI.
+ - name: ubi
+ size: 300MiB
+ ubi:
+ volumes:
+ # Static volumes are accessed directly by the bootloader.
+ - name: dtb
+ size: 128kiB
+ type: static
+ input:
+ - filename: oftree
+
+ - name: kernel
+ size: 8MiB
+ type: static
+ input:
+ - filename: Image
+
+ # Dynamic volume with a UBIFS rootfs, using the rest of the UBI device.
+ - name: rootfs
+ expand: true
+ filesystem: ubifs
+ # -F makes the kernel fix up free space on first mount.
+ mkfs-extra-args: -F
+ input:
+ - filename: rootfs.tar
diff --git a/doc/data/meson.build b/doc/data/meson.build
index ac7b7511..083da6e3 100644
--- a/doc/data/meson.build
+++ b/doc/data/meson.build
@@ -1,6 +1,7 @@
static_data = [
'layout-ab-emmc.yaml',
'layout-simple.yaml',
+ 'layout-ubi-nand.yaml',
'partup-logo-2x.svg',
'partup-logo-2x-white.svg'
]
diff --git a/doc/layout-config-examples.rst b/doc/layout-config-examples.rst
index 451687f7..9264e7d0 100644
--- a/doc/layout-config-examples.rst
+++ b/doc/layout-config-examples.rst
@@ -18,3 +18,9 @@ eMMC Layout With Raw WIC Image As Input
.. literalinclude:: data/layout-raw-wic.yaml
:language: yaml
+
+MTD Layout With UBI Volumes
+---------------------------
+
+.. literalinclude:: data/layout-ubi-nand.yaml
+ :language: yaml
diff --git a/doc/layout-config-reference.rst b/doc/layout-config-reference.rst
index 053737c2..15dbfd2b 100644
--- a/doc/layout-config-reference.rst
+++ b/doc/layout-config-reference.rst
@@ -307,6 +307,90 @@ contained partitions of an MTD. Each entry may contain the following options:
given as a runtime argument. Note, that this checksum is independent from the
input's ``sha256sum`` option. See :ref:`checksum-verification` for details.
+``ubi`` :badge-type:`mapping` :badge-since:`5.0.0`
+ A mapping with the UBI volumes to create on top of this partition. This
+ option is mutually exclusive with ``input``, as a partition either holds raw
+ data or UBI volumes. See :ref:`ubi-volumes`.
+
+.. _ubi-volumes:
+
+UBI Volumes
+............
+
+UBI (Unsorted Block Images) volume support is available since:
+:ref:`release-5.0.0`
+
+Raw NAND flash has no partition table. Instead, the UBI layer manages volumes
+inside an MTD. These volumes commonly hold a UBIFS rootfs or raw files for the
+bootloader. A partition with an ``ubi`` mapping is formatted with UBI and the
+volumes out of its ``volumes`` sequence are created on top of it.
+
+Formatting is always done with ``ubiformat``, which erases all previous content
+of the partition and writes fresh erase counters and an empty volume table.
+
+The ``ubi`` mapping contains the following option:
+
+``volumes`` :badge-type:`sequence` :badge-state:`required` :badge-since:`5.0.0`
+ A sequence of mappings describing the UBI volumes. Each mapping may contain
+ the following options:
+
+ ``name`` :badge-type:`string` :badge-state:`required` :badge-since:`5.0.0`
+ The volume name. UBI requires volume names to be unique within a UBI
+ device and to be at most 63 characters long.
+
+ ``size`` :badge-type:`integer` :badge-type:`string` :badge-since:`5.0.0`
+ The size of the volume. This scalar is mandatory, unless the volume is
+ expanded. The same units as for the partition's ``size`` are supported,
+ e.g. ``12kiB`` or ``34MB``. When no unit is specified, the default is
+ sectors. UBI rounds the size up to whole logical eraseblocks (LEB), so a
+ volume may become slightly larger than specified.
+
+ ``type`` :badge-type:`string` :badge-since:`5.0.0`
+ The volume type. Possible options are:
+
+ - ``dynamic``: Volume that can be used as a UBI character device and can
+ hold a UBIFS filesystem. UBI assigns the volume's data to logical
+ eraseblocks dynamically.
+ - ``static``: Volume with a fixed amount of logical eraseblocks, meant
+ for raw data only. This is what a bootloader usually uses to access
+ files directly.
+
+ The default value is ``dynamic``. A static volume cannot be expanded and
+ cannot have a filesystem.
+
+ ``filesystem`` :badge-type:`string` :badge-since:`5.0.0`
+ The filesystem type of the volume. The only supported filesystem is
+ ``ubifs``, which is also the default value of dynamic volumes. Use
+ ``null`` to write raw data into the volume. Static volumes have no
+ filesystem by default.
+
+ The filesystem is created with ``mkfs.ubifs`` before any input file is
+ written, replacing any filesystem that was on the volume before.
+
+ ``mkfs-extra-args`` :badge-type:`string` :badge-since:`5.0.0`
+ Extra arguments to be passed to ``mkfs.ubifs``. See ``man mkfs.ubifs``
+ for allowed arguments.
+
+ ``expand`` :badge-type:`boolean` :badge-since:`5.0.0`
+ Expand the volume to the rest of the UBI device. Only the last volume can
+ be expanded. The default is ``false`` for all volumes.
+
+ ``input`` :badge-type:`sequence` :badge-since:`5.0.0`
+ A sequence of input mappings. See :ref:`input-files`.
+
+ Volumes with a filesystem get their input files copied into the mounted
+ filesystem, or extracted if they are archives. UBIFS images are written
+ directly to the volume and are then mounted once, so that UBIFS can
+ resize the filesystem to the whole volume. Volumes without a filesystem
+ are written directly with ``ubiupdatevol``. Such a volume takes only one
+ input file, because writing replaces the whole volume's content.
+
+ Raw volume writes are always being verified by reading the volume back and
+ checking it against the input's SHA1 sum. The checksum is not being
+ verified when ``--skip-checksums`` is given as a runtime argument. Note,
+ that this checksum is independent from the input's ``sha256sum`` option.
+ See :ref:`checksum-verification` for details.
+
.. _input-files:
Input Files
@@ -356,3 +440,9 @@ types, they are treated as following.
any existing filesystem, so it should be specified as ``filesystem: null`` or
not be specified at all. Additionally ext filesystems are resized to utilize
the whole partition.
+
+``ubifs``
+ Raw filesystem images are written directly to the UBI volume. This overrides
+ any existing filesystem. Additionally, the UBIFS filesystem is mounted after
+ writing, so that it resizes itself to utilize the whole volume. Only works on
+ :ref:`ubi-volumes`.
diff --git a/doc/usage.rst b/doc/usage.rst
index 7d24f51a..dba9f802 100644
--- a/doc/usage.rst
+++ b/doc/usage.rst
@@ -71,14 +71,17 @@ translation layer. This includes:
- SSD
- SD cards
- eMMC devices and their eMMC boot partitions
+- Memory Technology Devices (MTD) (since :ref:`release-3.0.0`), including raw
+ NAND flash with UBI volumes (since :ref:`release-5.0.0`)
-The device must be named ``mmcblk*`` or ``sd*``, e.g. the following device names
-are valid::
+The device must be named ``mmcblk*``, ``sd*``, or ``mtd*``, e.g. the following
+device names are valid::
/dev/mmcblk0
/dev/mmcblk9
/dev/sda
/dev/sdf
+ /dev/mtd0
.. warning::
@@ -87,9 +90,10 @@ are valid::
.. note::
- Devices with raw access to memory, not incorporating a flash translation
- layer, like those accessible through the `Linux MTD interface
- `_, are currently not supported.
+ MTDs must be specified as their master device, like ``/dev/mtd0``. partup
+ creates the MTD's partitions itself. For that, the kernel must be configured
+ with ``CONFIG_MTD_PARTITIONED_MASTER``, so that the master device is exposed
+ even if the MTD is already partitioned.
.. _example-usage:
@@ -199,10 +203,12 @@ the raw device, namely:
- the ``raw`` section of MMC and HD devices (since :ref:`release-2.1.0`),
- eMMC boot partition ``binaries`` -- verified on both boot partitions
(``boot0`` and ``boot1``) (since :ref:`release-3.0.0`), and
-- MTD ``partitions`` (since :ref:`release-3.0.0`).
+- MTD ``partitions`` (since :ref:`release-3.0.0`), and
+- UBI ``volumes`` without a filesystem (since :ref:`release-5.0.0`).
Read-back verification is *not* performed for data that is written through a
mounted filesystem, i.e. files copied into a partition, archives extracted into
-a partition, or raw ext[234] filesystem images written to a partition.
+a partition, or raw ext[234] or UBIFS filesystem images written to a partition
+or volume.
The output verification is skipped when ``--skip-checksums`` is given.
diff --git a/meson.build b/meson.build
index d873bd5f..23e282c5 100644
--- a/meson.build
+++ b/meson.build
@@ -45,6 +45,7 @@ src = [
'src/pu-mount.c',
'src/pu-mtd.c',
'src/pu-package.c',
+ 'src/pu-ubi.c',
'src/pu-unit.c',
'src/pu-utils.c'
]
diff --git a/src/pu-error.h b/src/pu-error.h
index 2df9eba3..c20a243c 100644
--- a/src/pu-error.h
+++ b/src/pu-error.h
@@ -25,6 +25,11 @@ typedef enum {
/* MTD errors */
PU_ERROR_MTD_PARSE,
+ /* UBI errors */
+ PU_ERROR_UBI_PARSE,
+ PU_ERROR_UBI_FORMAT,
+ PU_ERROR_UBI_VERIFY,
+
/* Config parsing errors */
PU_ERROR_CONFIG_INIT_FAILED,
PU_ERROR_CONFIG_INVALID_ROOT,
diff --git a/src/pu-mtd.c b/src/pu-mtd.c
index ea35e991..2efd3d9f 100644
--- a/src/pu-mtd.c
+++ b/src/pu-mtd.c
@@ -19,6 +19,7 @@
#include "pu-hashtable.h"
#include "pu-utils.h"
#include "pu-mtd.h"
+#include "pu-ubi.h"
typedef struct _PuMtdInput {
gchar *filename;
@@ -35,6 +36,7 @@ typedef struct _PuMtdPartition {
gboolean erase;
gboolean expand;
PuMtdInput *input;
+ GList *volumes;
} PuMtdPartition;
struct _PuMtd {
@@ -191,6 +193,16 @@ pu_mtd_partition_enumerator_iterate(PuMtdPartitionEnumerator *part_enum,
return TRUE;
}
+static guint
+get_mtd_number(const gchar *device_path)
+{
+ g_autofree gchar *basename = g_path_get_basename(device_path);
+
+ g_return_val_if_fail(g_str_has_prefix(basename, "mtd"), 0);
+
+ return (guint) g_ascii_strtoll(basename + 3, NULL, 10);
+}
+
static gboolean
pu_mtd_init_device(PuFlash *flash,
GError **error)
@@ -217,6 +229,11 @@ pu_mtd_init_device(PuFlash *flash,
return FALSE;
}
+ /* Detach any UBI device from the master device, so that its partitions
+ * can be deleted. */
+ if (!pu_ubi_detach(get_mtd_number(device_path), error))
+ return FALSE;
+
/* Delete device's old partitions */
part_enum = pu_mtd_enumerate_partitions(device_path, error);
if (!part_enum)
@@ -233,6 +250,12 @@ pu_mtd_init_device(PuFlash *flash,
if (!part_info)
break;
+ if (!pu_ubi_detach(part_info->devnum, error)) {
+ g_prefix_error(error, "Failed detaching UBI device from partition "
+ "%u of device '%s'", part_info->devnum, device_path);
+ return FALSE;
+ }
+
cmd = g_strdup_printf("mtdpart del %s %u", device_path, part_info->devnum);
if (!pu_spawn_command_line_sync(cmd, error)) {
g_prefix_error(error, "Failed deleting partition %u of device '%s'",
@@ -373,6 +396,28 @@ pu_mtd_setup_layout(PuFlash *flash,
p = find_partition(self, part_info->name, part_info->offset, error);
if (!p)
return FALSE;
+
+ /* A partition holding UBI volumes is formatted with UBI instead of
+ * being erased with the other partitions. ubiformat writes fresh UBI
+ * erase counters and volume tables, so no flash_erase is needed. */
+ if (p->volumes) {
+ g_autofree gchar *ubi_node = NULL;
+ g_autofree gchar *part_dev = NULL;
+
+ part_dev = g_strdup_printf("/dev/mtd%u", part_info->devnum);
+
+ if (!pu_ubi_format(part_dev, error))
+ return FALSE;
+
+ if (!pu_ubi_attach(part_info->devnum, &ubi_node, error))
+ return FALSE;
+
+ if (!pu_ubi_create_volumes(ubi_node, p->volumes, error))
+ return FALSE;
+
+ continue;
+ }
+
if (!p->erase)
continue;
@@ -427,6 +472,25 @@ pu_mtd_write_data(PuFlash *flash,
p = find_partition(self, part_info->name, part_info->offset, error);
if (!p)
return FALSE;
+
+ if (p->volumes) {
+ g_autofree gchar *ubi_node = NULL;
+
+ ubi_node = pu_ubi_get_ubi_node(part_info->devnum);
+ if (!ubi_node) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_VERIFY,
+ "Failed finding UBI device of partition '%s'",
+ part_info->name);
+ return FALSE;
+ }
+
+ if (!pu_ubi_write_volumes(ubi_node, p->volumes, prefix,
+ skip_checksums, error))
+ return FALSE;
+
+ continue;
+ }
+
if (!p->input)
continue;
@@ -474,6 +538,14 @@ pu_mtd_write_data(PuFlash *flash,
static void
pu_mtd_class_finalize(GObject *object)
{
+ PuMtd *self = PU_MTD(object);
+
+ for (GList *p = self->partitions; p != NULL; p = p->next) {
+ PuMtdPartition *part = p->data;
+ pu_ubi_volumes_free(part->volumes);
+ part->volumes = NULL;
+ }
+
G_OBJECT_CLASS(pu_mtd_parent_class)->finalize(object);
}
@@ -594,6 +666,24 @@ pu_mtd_parse_partitions(PuMtd *mtd,
}
}
+ PuConfigValue *value_ubi = g_hash_table_lookup(v->data.mapping, "ubi");
+ if (value_ubi) {
+ if (part->input) {
+ g_set_error(error, PU_ERROR, PU_ERROR_MTD_PARSE,
+ "Partition '%s' has an input and UBI volumes. "
+ "Only one of both is supported", part->name);
+ return FALSE;
+ }
+ if (value_ubi->type != PU_CONFIG_VALUE_TYPE_MAPPING) {
+ g_set_error(error, PU_ERROR, PU_ERROR_MTD_PARSE,
+ "'ubi' of partition does not contain a mapping");
+ return FALSE;
+ }
+ if (!pu_ubi_parse_volumes(value_ubi->data.mapping, prefix,
+ &part->volumes, error))
+ return FALSE;
+ }
+
mtd->partitions = g_list_prepend(mtd->partitions, part);
}
diff --git a/src/pu-ubi.c b/src/pu-ubi.c
new file mode 100644
index 00000000..62e49c2b
--- /dev/null
+++ b/src/pu-ubi.c
@@ -0,0 +1,698 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ * Copyright (c) 2026 PHYTEC Messtechnik GmbH
+ */
+
+#define G_LOG_DOMAIN "partup-ubi"
+
+#include
+#include
+#include "pu-checksum.h"
+#include "pu-error.h"
+#include "pu-file.h"
+#include "pu-hashtable.h"
+#include "pu-mount.h"
+#include "pu-utils.h"
+#include "pu-ubi.h"
+
+static void
+pu_ubi_volume_input_free(PuUbiVolumeInput *input)
+{
+ if (!input)
+ return;
+
+ g_free(input->filename);
+ g_free(input->md5sum);
+ g_free(input->sha256sum);
+ g_free(input);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(PuUbiVolumeInput, pu_ubi_volume_input_free)
+
+static void
+pu_ubi_volume_free(PuUbiVolume *volume)
+{
+ if (!volume)
+ return;
+
+ g_free(volume->name);
+ g_free(volume->filesystem);
+ g_free(volume->mkfs_extra_args);
+ for (GList *i = volume->input; i != NULL; i = i->next)
+ pu_ubi_volume_input_free(i->data);
+ g_list_free(g_steal_pointer(&volume->input));
+ g_free(volume);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(PuUbiVolume, pu_ubi_volume_free)
+
+void
+pu_ubi_volumes_free(GList *volumes)
+{
+ for (GList *v = volumes; v != NULL; v = v->next)
+ pu_ubi_volume_free(v->data);
+
+ g_list_free(volumes);
+}
+
+static gboolean
+pu_ubi_parse_volume_input(GHashTable *volume,
+ const gchar *prefix,
+ GList **input,
+ GError **error)
+{
+ PuConfigValue *value_input = g_hash_table_lookup(volume, "input");
+ GList *input_list;
+ g_autofree gchar *name = NULL;
+
+ g_return_val_if_fail(volume != NULL, FALSE);
+ g_return_val_if_fail(input != NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ name = pu_hash_table_lookup_string(volume, "name", NULL);
+
+ if (!value_input) {
+ g_debug("No entry 'input' found. Skipping...");
+ return TRUE;
+ }
+
+ if (value_input->type != PU_CONFIG_VALUE_TYPE_SEQUENCE) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'input' of volume '%s' does not contain a sequence",
+ name);
+ return FALSE;
+ }
+ input_list = value_input->data.sequence;
+
+ for (GList *i = input_list; i != NULL; i = i->next) {
+ PuConfigValue *v = i->data;
+ g_autoptr(PuUbiVolumeInput) in = NULL;
+ g_autofree gchar *path = NULL;
+ goffset size;
+
+ if (v->type != PU_CONFIG_VALUE_TYPE_MAPPING) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'input' of volume '%s' does not contain sequence "
+ "of mappings", name);
+ return FALSE;
+ }
+
+ in = g_new0(PuUbiVolumeInput, 1);
+ in->filename = pu_hash_table_lookup_string(v->data.mapping, "filename", "");
+ in->md5sum = pu_hash_table_lookup_string(v->data.mapping, "md5sum", "");
+ in->sha256sum = pu_hash_table_lookup_string(v->data.mapping, "sha256sum", "");
+
+ path = pu_path_from_filename(in->filename, prefix, error);
+ if (path == NULL) {
+ g_prefix_error(error, "Failed parsing input filename for volume '%s': ",
+ name);
+ return FALSE;
+ }
+
+ size = pu_file_get_size(path, error);
+ if (size == 0) {
+ g_prefix_error(error, "Failed retrieving file size of input '%s' "
+ "for volume '%s': ", in->filename, name);
+ return FALSE;
+ }
+ in->_size = size;
+
+ g_debug("Parsed volume input: filename=%s md5sum=%s sha256sum=%s",
+ in->filename, in->md5sum, in->sha256sum);
+
+ *input = g_list_prepend(*input, g_steal_pointer(&in));
+ }
+
+ *input = g_list_reverse(*input);
+
+ return TRUE;
+}
+
+gboolean
+pu_ubi_parse_volumes(GHashTable *ubi,
+ const gchar *prefix,
+ GList **volumes,
+ GError **error)
+{
+ PuConfigValue *value_volumes = g_hash_table_lookup(ubi, "volumes");
+ GList *volumes_list;
+
+ g_return_val_if_fail(ubi != NULL, FALSE);
+ g_return_val_if_fail(volumes != NULL, FALSE);
+ g_return_val_if_fail(*volumes == NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ if (!value_volumes) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'ubi' is missing a 'volumes' sequence");
+ return FALSE;
+ }
+
+ if (value_volumes->type != PU_CONFIG_VALUE_TYPE_SEQUENCE) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'volumes' is not a sequence");
+ return FALSE;
+ }
+ volumes_list = value_volumes->data.sequence;
+
+ if (!volumes_list) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'volumes' does not contain any volumes");
+ return FALSE;
+ }
+
+ for (GList *p = volumes_list; p != NULL; p = p->next) {
+ PuConfigValue *v = p->data;
+ g_autoptr(PuUbiVolume) volume = NULL;
+ g_autofree gchar *type_str = NULL;
+
+ if (v->type != PU_CONFIG_VALUE_TYPE_MAPPING) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "'volumes' does not contain sequence of mappings");
+ return FALSE;
+ }
+
+ volume = g_new0(PuUbiVolume, 1);
+ volume->name = pu_hash_table_lookup_string(v->data.mapping, "name", NULL);
+ volume->size = pu_hash_table_lookup_bytes(v->data.mapping, "size", 0);
+ volume->filesystem = NULL;
+ volume->mkfs_extra_args = pu_hash_table_lookup_string(v->data.mapping,
+ "mkfs-extra-args", NULL);
+ volume->expand = pu_hash_table_lookup_boolean(v->data.mapping, "expand", FALSE);
+
+ if (!volume->name) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Volume is missing a name");
+ return FALSE;
+ }
+
+ type_str = pu_hash_table_lookup_string(v->data.mapping, "type", "dynamic");
+ if (g_str_equal(type_str, "dynamic")) {
+ volume->type = PU_UBI_VOLUME_TYPE_DYNAMIC;
+ } else if (g_str_equal(type_str, "static")) {
+ volume->type = PU_UBI_VOLUME_TYPE_STATIC;
+ } else {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Volume '%s' of invalid type '%s' specified",
+ volume->name, type_str);
+ return FALSE;
+ }
+
+ /* Only dynamic volumes can hold a filesystem. Their default
+ * filesystem is UBIFS, static volumes have none by default. */
+ if (g_hash_table_contains(v->data.mapping, "filesystem"))
+ volume->filesystem = pu_hash_table_lookup_string(v->data.mapping,
+ "filesystem", NULL);
+ else if (volume->type == PU_UBI_VOLUME_TYPE_DYNAMIC)
+ volume->filesystem = g_strdup("ubifs");
+
+ if (volume->filesystem && !g_str_equal(volume->filesystem, "ubifs")) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Filesystem type '%s' of volume '%s' is not supported",
+ volume->filesystem, volume->name);
+ return FALSE;
+ }
+
+ if (volume->type == PU_UBI_VOLUME_TYPE_STATIC) {
+ if (volume->expand) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Static volume '%s' cannot be expanded", volume->name);
+ return FALSE;
+ }
+
+ if (volume->filesystem) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Static volume '%s' cannot have a filesystem",
+ volume->name);
+ return FALSE;
+ }
+ }
+
+ if (volume->size == 0 && !volume->expand) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Volume '%s' is missing a size", volume->name);
+ return FALSE;
+ }
+
+ if (volume->expand && p->next) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Only the last volume can be expanded");
+ return FALSE;
+ }
+
+ if (!pu_ubi_parse_volume_input(v->data.mapping, prefix, &volume->input, error))
+ return FALSE;
+
+ /* Expanding volumes have no size yet and are not checked. */
+ if (volume->size > 0) {
+ for (GList *i = volume->input; i != NULL; i = i->next) {
+ const PuUbiVolumeInput *in = i->data;
+
+ if ((gint64) in->_size > volume->size) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_PARSE,
+ "Input file '%s' (%" G_GSIZE_FORMAT " bytes) "
+ "exceeds size of volume '%s' "
+ "(%" G_GINT64_FORMAT " bytes)",
+ in->filename, in->_size, volume->name,
+ volume->size);
+ return FALSE;
+ }
+ }
+ }
+
+ g_debug("Parsed volume: name=%s type=%s filesystem=%s "
+ "mkfs-extra-args=%s size=%" G_GINT64_FORMAT " expand=%s",
+ volume->name, type_str,
+ volume->filesystem ? volume->filesystem : "null",
+ volume->mkfs_extra_args, volume->size,
+ volume->expand ? "true" : "false");
+
+ *volumes = g_list_prepend(*volumes, g_steal_pointer(&volume));
+ }
+
+ *volumes = g_list_reverse(*volumes);
+
+ return TRUE;
+}
+
+static gchar *
+find_ubi_node(guint mtd_devnum)
+{
+ g_autoptr(GDir) dir = NULL;
+ const gchar *name;
+
+ dir = g_dir_open("/sys/class/ubi", 0, NULL);
+ if (!dir)
+ return NULL;
+
+ while ((name = g_dir_read_name(dir)) != NULL) {
+ g_autofree gchar *filename = NULL;
+ gint64 value = 0;
+
+ /* skip UBI volume devices, they are named ubi_ */
+ if (!g_str_has_prefix(name, "ubi") || strchr(name, '_'))
+ continue;
+
+ filename = g_build_filename("/sys/class/ubi", name, "mtd_dev", NULL);
+ if (!pu_file_read_int64(filename, &value, NULL))
+ continue;
+
+ if (value == (gint64) mtd_devnum)
+ return g_strdup_printf("/dev/%s", name);
+ }
+
+ return NULL;
+}
+
+static gchar *
+find_volume_node(const gchar *ubi_node,
+ const gchar *volume_name)
+{
+ g_autoptr(GDir) dir = NULL;
+ g_autofree gchar *pattern = NULL;
+ g_autofree gchar *basename = NULL;
+ const gchar *name;
+
+ dir = g_dir_open("/sys/class/ubi", 0, NULL);
+ if (!dir)
+ return NULL;
+
+ basename = g_path_get_basename(ubi_node);
+ pattern = g_strdup_printf("%s_", basename);
+
+ while ((name = g_dir_read_name(dir)) != NULL) {
+ g_autofree gchar *filename = NULL;
+ g_autofree gchar *contents = NULL;
+
+ if (!g_str_has_prefix(name, pattern))
+ continue;
+
+ filename = g_build_filename("/sys/class/ubi", name, "name", NULL);
+ if (!g_file_get_contents(filename, &contents, NULL, NULL))
+ continue;
+
+ g_strchomp(contents);
+ if (g_str_equal(contents, volume_name))
+ return g_strdup_printf("/dev/%s", name);
+ }
+
+ return NULL;
+}
+
+gchar *
+pu_ubi_get_ubi_node(guint mtd_devnum)
+{
+ return find_ubi_node(mtd_devnum);
+}
+
+gboolean
+pu_ubi_detach(guint mtd_devnum,
+ GError **error)
+{
+ g_autofree gchar *ubi_node = NULL;
+ g_autofree gchar *cmd = NULL;
+
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ ubi_node = find_ubi_node(mtd_devnum);
+ if (!ubi_node) {
+ g_debug("No UBI device attached to MTD %u", mtd_devnum);
+ return TRUE;
+ }
+
+ g_debug("Detaching UBI device '%s' from MTD %u", ubi_node, mtd_devnum);
+
+ cmd = g_strdup_printf("ubidetach /dev/ubi_ctrl -m %u", mtd_devnum);
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed detaching UBI device from MTD %u: ",
+ mtd_devnum);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+pu_ubi_format(const gchar *mtd_path,
+ GError **error)
+{
+ g_autofree gchar *cmd = NULL;
+
+ g_return_val_if_fail(mtd_path != NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ g_debug("Formatting MTD '%s' with UBI", mtd_path);
+
+ cmd = g_strdup_printf("ubiformat %s -y -q", mtd_path);
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed formatting MTD '%s' with UBI: ",
+ mtd_path);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+pu_ubi_attach(guint mtd_devnum,
+ gchar **ubi_node,
+ GError **error)
+{
+ g_autofree gchar *cmd = NULL;
+
+ g_return_val_if_fail(ubi_node != NULL, FALSE);
+ g_return_val_if_fail(*ubi_node == NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ g_debug("Attaching MTD %u to UBI", mtd_devnum);
+
+ cmd = g_strdup_printf("ubiattach /dev/ubi_ctrl -m %u", mtd_devnum);
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed attaching MTD %u to UBI: ", mtd_devnum);
+ return FALSE;
+ }
+
+ *ubi_node = find_ubi_node(mtd_devnum);
+ if (*ubi_node == NULL) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_FORMAT,
+ "Failed finding UBI device of MTD %u", mtd_devnum);
+ return FALSE;
+ }
+
+ g_debug("MTD %u is now UBI device '%s'", mtd_devnum, *ubi_node);
+
+ return TRUE;
+}
+
+gboolean
+pu_ubi_create_volumes(const gchar *ubi_node,
+ GList *volumes,
+ GError **error)
+{
+ g_return_val_if_fail(ubi_node != NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ for (GList *v = volumes; v != NULL; v = v->next) {
+ PuUbiVolume *volume = v->data;
+ g_autofree gchar *cmd = NULL;
+ g_autofree gchar *name = NULL;
+ g_autofree gchar *size = NULL;
+
+ name = g_shell_quote(volume->name);
+
+ if (volume->expand)
+ size = g_strdup("-m");
+ else
+ size = g_strdup_printf("-s %" G_GINT64_FORMAT, volume->size);
+
+ if (volume->type == PU_UBI_VOLUME_TYPE_STATIC)
+ cmd = g_strdup_printf("ubimkvol %s -N %s -t static %s",
+ ubi_node, name, size);
+ else
+ cmd = g_strdup_printf("ubimkvol %s -N %s %s", ubi_node, name, size);
+
+ g_debug("Creating UBI volume '%s'", volume->name);
+
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed creating UBI volume '%s': ",
+ volume->name);
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_mkfs_ubifs(const gchar *volume_node,
+ const gchar *extra_args,
+ GError **error)
+{
+ g_autofree gchar *cmd = NULL;
+
+ g_debug("Making filesystem ubifs on UBI volume '%s'", volume_node);
+
+ if (extra_args)
+ cmd = g_strdup_printf("mkfs.ubifs %s %s", extra_args, volume_node);
+ else
+ cmd = g_strdup_printf("mkfs.ubifs %s", volume_node);
+
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed creating filesystem on UBI volume '%s': ",
+ volume_node);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_update_volume(const gchar *volume_node,
+ const gchar *path,
+ GError **error)
+{
+ g_autofree gchar *cmd = NULL;
+
+ g_debug("Writing file '%s' to UBI volume '%s'", path, volume_node);
+
+ cmd = g_strdup_printf("ubiupdatevol %s %s", volume_node, path);
+ if (!pu_spawn_command_line_sync(cmd, error)) {
+ g_prefix_error(error, "Failed writing '%s' to UBI volume '%s': ",
+ path, volume_node);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_verify_input(PuUbiVolumeInput *input,
+ const gchar *path,
+ gboolean skip_checksums,
+ GError **error)
+{
+ if (skip_checksums)
+ return TRUE;
+
+ if (!g_str_equal(input->md5sum, "")) {
+ g_debug("Checking MD5 sum of input file '%s'", path);
+ if (!pu_checksum_verify_file(path, input->md5sum, G_CHECKSUM_MD5, error))
+ return FALSE;
+ }
+
+ if (!g_str_equal(input->sha256sum, "")) {
+ g_debug("Checking SHA256 sum of input file '%s'", path);
+ if (!pu_checksum_verify_file(path, input->sha256sum, G_CHECKSUM_SHA256,
+ error))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_write_volume_data(PuUbiVolume *volume,
+ const gchar *volume_node,
+ const gchar *prefix,
+ gboolean skip_checksums,
+ GError **error)
+{
+ for (GList *i = volume->input; i != NULL; i = i->next) {
+ PuUbiVolumeInput *input = i->data;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *sha1 = NULL;
+
+ path = pu_path_from_filename(input->filename, prefix, error);
+ if (!path) {
+ g_prefix_error(error, "Failed parsing input filename for UBI volume "
+ "'%s': ", volume->name);
+ return FALSE;
+ }
+
+ if (!pu_ubi_verify_input(input, path, skip_checksums, error))
+ return FALSE;
+
+ if (!pu_ubi_update_volume(volume_node, path, error))
+ return FALSE;
+
+ if (skip_checksums)
+ continue;
+
+ sha1 = pu_checksum_new_from_file(path, 0, G_CHECKSUM_SHA1, error);
+ if (!sha1) {
+ g_prefix_error(error, "Failed calculating SHA1 sum of input file '%s': ",
+ input->filename);
+ return FALSE;
+ }
+
+ g_debug("Verifying SHA1 sum of UBI volume '%s'", volume->name);
+ if (!pu_checksum_verify_raw(volume_node, 0, input->_size, sha1,
+ G_CHECKSUM_SHA1, error))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_write_volume_filesystem(PuUbiVolume *volume,
+ const gchar *volume_node,
+ const gchar *prefix,
+ gboolean skip_checksums,
+ GError **error)
+{
+ g_autofree gchar *mount_point = NULL;
+
+ if (!pu_ubi_mkfs_ubifs(volume_node, volume->mkfs_extra_args, error))
+ return FALSE;
+
+ if (!volume->input) {
+ g_debug("No input specified. Leaving UBI volume '%s' empty", volume->name);
+ return TRUE;
+ }
+
+ mount_point = pu_create_mount_point(volume->name, error);
+ if (!mount_point)
+ return FALSE;
+
+ for (GList *i = volume->input; i != NULL; i = i->next) {
+ PuUbiVolumeInput *input = i->data;
+ g_autofree gchar *path = NULL;
+
+ path = pu_path_from_filename(input->filename, prefix, error);
+ if (!path) {
+ g_prefix_error(error, "Failed parsing input filename for UBI volume "
+ "'%s': ", volume->name);
+ return FALSE;
+ }
+
+ if (!pu_ubi_verify_input(input, path, skip_checksums, error))
+ return FALSE;
+
+ if (g_regex_match_simple(".tar", path, G_REGEX_CASELESS, 0)) {
+ if (!pu_mount(volume_node, mount_point, volume->filesystem, NULL, error))
+ return FALSE;
+ if (!pu_archive_extract(path, mount_point, error)) {
+ g_prefix_error(error, "Failed extracting file '%s' to UBI volume "
+ "'%s': ", input->filename, volume->name);
+ return FALSE;
+ }
+ if (!pu_umount(mount_point, error))
+ return FALSE;
+ } else if (g_regex_match_simple(".ubifs$", path, 0, 0)) {
+ if (!pu_ubi_update_volume(volume_node, path, error))
+ return FALSE;
+
+ /* Mount to let UBIFS resize the filesystem to the volume size. */
+ if (!pu_mount(volume_node, mount_point, volume->filesystem, NULL, error))
+ return FALSE;
+ if (!pu_umount(mount_point, error))
+ return FALSE;
+ } else {
+ if (!pu_mount(volume_node, mount_point, volume->filesystem, NULL, error))
+ return FALSE;
+ if (!pu_file_copy(path, mount_point, error)) {
+ g_prefix_error(error, "Failed copying file '%s' to UBI volume "
+ "'%s': ", input->filename, volume->name);
+ return FALSE;
+ }
+ if (!pu_umount(mount_point, error))
+ return FALSE;
+ }
+ }
+
+ g_rmdir(mount_point);
+
+ return TRUE;
+}
+
+static gboolean
+pu_ubi_write_volume(PuUbiVolume *volume,
+ const gchar *ubi_node,
+ const gchar *prefix,
+ gboolean skip_checksums,
+ GError **error)
+{
+ g_autofree gchar *volume_node = NULL;
+
+ g_return_val_if_fail(volume != NULL, FALSE);
+ g_return_val_if_fail(ubi_node != NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ volume_node = find_volume_node(ubi_node, volume->name);
+ if (!volume_node) {
+ g_set_error(error, PU_ERROR, PU_ERROR_UBI_VERIFY,
+ "Failed finding UBI volume node of volume '%s'", volume->name);
+ return FALSE;
+ }
+
+ if (volume->filesystem)
+ return pu_ubi_write_volume_filesystem(volume, volume_node, prefix,
+ skip_checksums, error);
+
+ if (!volume->input) {
+ g_debug("No input specified. Skipping UBI volume '%s'", volume->name);
+ return TRUE;
+ }
+
+ return pu_ubi_write_volume_data(volume, volume_node, prefix, skip_checksums,
+ error);
+}
+
+gboolean
+pu_ubi_write_volumes(const gchar *ubi_node,
+ GList *volumes,
+ const gchar *prefix,
+ gboolean skip_checksums,
+ GError **error)
+{
+ g_return_val_if_fail(ubi_node != NULL, FALSE);
+ g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+ for (GList *v = volumes; v != NULL; v = v->next) {
+ if (!pu_ubi_write_volume(v->data, ubi_node, prefix, skip_checksums, error))
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/src/pu-ubi.h b/src/pu-ubi.h
new file mode 100644
index 00000000..dbe5291f
--- /dev/null
+++ b/src/pu-ubi.h
@@ -0,0 +1,60 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ * Copyright (c) 2026 PHYTEC Messtechnik GmbH
+ */
+
+#ifndef PARTUP_UBI_H
+#define PARTUP_UBI_H
+
+#include "pu-config.h"
+
+typedef enum {
+ /* Dynamic volumes are the default UBI volume type and may be resized. */
+ PU_UBI_VOLUME_TYPE_DYNAMIC = 0,
+ /* Static volumes have a fixed size and may be read directly by a
+ * bootloader, without mapping the whole volume into memory first. */
+ PU_UBI_VOLUME_TYPE_STATIC
+} PuUbiVolumeType;
+
+typedef struct _PuUbiVolumeInput {
+ gchar *filename;
+ gchar *md5sum;
+ gchar *sha256sum;
+
+ /* Internal members */
+ gsize _size;
+} PuUbiVolumeInput;
+typedef struct _PuUbiVolume {
+ gchar *name;
+ gint64 size;
+ PuUbiVolumeType type;
+ gchar *filesystem;
+ gchar *mkfs_extra_args;
+ gboolean expand;
+ GList *input;
+} PuUbiVolume;
+
+gboolean pu_ubi_parse_volumes(GHashTable *ubi,
+ const gchar *prefix,
+ GList **volumes,
+ GError **error);
+void pu_ubi_volumes_free(GList *volumes);
+
+gchar * pu_ubi_get_ubi_node(guint mtd_devnum);
+gboolean pu_ubi_detach(guint mtd_devnum,
+ GError **error);
+gboolean pu_ubi_format(const gchar *mtd_path,
+ GError **error);
+gboolean pu_ubi_attach(guint mtd_devnum,
+ gchar **ubi_node,
+ GError **error);
+gboolean pu_ubi_create_volumes(const gchar *ubi_node,
+ GList *volumes,
+ GError **error);
+gboolean pu_ubi_write_volumes(const gchar *ubi_node,
+ GList *volumes,
+ const gchar *prefix,
+ gboolean skip_checksums,
+ GError **error);
+
+#endif /* PARTUP_UBI_H */
diff --git a/tests/config.c b/tests/config.c
index 33e625a9..c445a2d2 100644
--- a/tests/config.c
+++ b/tests/config.c
@@ -67,6 +67,42 @@ config_mtd(void)
g_clear_error(&error);
}
+static void
+config_ubi(void)
+{
+ g_autoptr(GError) error = NULL;
+ g_autoptr(PuConfig) config = NULL;
+ GHashTable *root;
+ PuConfigValue *value;
+ PuConfigDeviceType device_type;
+
+ config = pu_config_new_from_file("config/ubi.yaml", &error);
+ g_assert_nonnull(config);
+ g_assert_false(pu_config_is_version_compatible(config, 4, NULL));
+ g_assert_true(pu_config_is_version_compatible(config, 5, &error));
+ g_assert_true(pu_config_is_device_supported(config, "/dev/mtd0", &device_type, &error));
+ g_assert_cmpint(device_type, ==, PU_CONFIG_DEVICE_TYPE_MTD);
+ g_assert_false(pu_config_is_device_supported(config, "/dev/mmcblk1", &device_type, NULL));
+ g_assert_cmpint(device_type, ==, PU_CONFIG_DEVICE_TYPE_NONE);
+ root = pu_config_get_root(config);
+ g_assert_nonnull(root);
+ value = g_hash_table_lookup(root, "partitions");
+ g_assert_nonnull(value);
+ g_assert_cmpint(value->type, ==, PU_CONFIG_VALUE_TYPE_SEQUENCE);
+
+ /* second partition holds the UBI volumes */
+ PuConfigValue *partition = g_list_nth_data(value->data.sequence, 1);
+ g_assert_nonnull(partition);
+ value = g_hash_table_lookup(partition->data.mapping, "ubi");
+ g_assert_nonnull(value);
+ g_assert_cmpint(value->type, ==, PU_CONFIG_VALUE_TYPE_MAPPING);
+ value = g_hash_table_lookup(value->data.mapping, "volumes");
+ g_assert_nonnull(value);
+ g_assert_cmpint(value->type, ==, PU_CONFIG_VALUE_TYPE_SEQUENCE);
+ g_assert_cmpuint(g_list_length(value->data.sequence), ==, 2);
+ g_clear_error(&error);
+}
+
static void
config_types(void)
{
@@ -122,6 +158,7 @@ main(int argc,
g_test_add_func("/config/simple", config_simple);
g_test_add_func("/config/mtd", config_mtd);
+ g_test_add_func("/config/ubi", config_ubi);
g_test_add_func("/config/types", config_types);
return g_test_run();
diff --git a/tests/config/ubi-expand-not-last.yaml b/tests/config/ubi-expand-not-last.yaml
new file mode 100644
index 00000000..1ee5f779
--- /dev/null
+++ b/tests/config/ubi-expand-not-last.yaml
@@ -0,0 +1,13 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: rootfs
+ size: 8MiB
+ expand: true
+ - name: data
+ size: 16MiB
diff --git a/tests/config/ubi-input-and-partition.yaml b/tests/config/ubi-input-and-partition.yaml
new file mode 100644
index 00000000..1a3e337f
--- /dev/null
+++ b/tests/config/ubi-input-and-partition.yaml
@@ -0,0 +1,12 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ input:
+ filename: lorem.txt
+ ubi:
+ volumes:
+ - name: data
+ size: 8MiB
diff --git a/tests/config/ubi-input-too-big.yaml b/tests/config/ubi-input-too-big.yaml
new file mode 100644
index 00000000..06d4dcb9
--- /dev/null
+++ b/tests/config/ubi-input-too-big.yaml
@@ -0,0 +1,13 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: kernel
+ size: 8kiB
+ filesystem: null
+ input:
+ - filename: random.bin
diff --git a/tests/config/ubi-missing-name.yaml b/tests/config/ubi-missing-name.yaml
new file mode 100644
index 00000000..ecf265c6
--- /dev/null
+++ b/tests/config/ubi-missing-name.yaml
@@ -0,0 +1,9 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - size: 8MiB
diff --git a/tests/config/ubi-missing-size.yaml b/tests/config/ubi-missing-size.yaml
new file mode 100644
index 00000000..4049a55a
--- /dev/null
+++ b/tests/config/ubi-missing-size.yaml
@@ -0,0 +1,9 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: rootfs
diff --git a/tests/config/ubi-missing-volumes.yaml b/tests/config/ubi-missing-volumes.yaml
new file mode 100644
index 00000000..bbc96781
--- /dev/null
+++ b/tests/config/ubi-missing-volumes.yaml
@@ -0,0 +1,8 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ mkfs-extra-args: -F
diff --git a/tests/config/ubi-static-expand.yaml b/tests/config/ubi-static-expand.yaml
new file mode 100644
index 00000000..7aba81da
--- /dev/null
+++ b/tests/config/ubi-static-expand.yaml
@@ -0,0 +1,11 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: dtb
+ type: static
+ expand: true
diff --git a/tests/config/ubi-static-filesystem.yaml b/tests/config/ubi-static-filesystem.yaml
new file mode 100644
index 00000000..2f541178
--- /dev/null
+++ b/tests/config/ubi-static-filesystem.yaml
@@ -0,0 +1,12 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: dtb
+ size: 8MiB
+ type: static
+ filesystem: ubifs
diff --git a/tests/config/ubi-unknown-filesystem.yaml b/tests/config/ubi-unknown-filesystem.yaml
new file mode 100644
index 00000000..bbb5be82
--- /dev/null
+++ b/tests/config/ubi-unknown-filesystem.yaml
@@ -0,0 +1,11 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: rootfs
+ size: 8MiB
+ filesystem: ext4
diff --git a/tests/config/ubi-unknown-type.yaml b/tests/config/ubi-unknown-type.yaml
new file mode 100644
index 00000000..31459f2c
--- /dev/null
+++ b/tests/config/ubi-unknown-type.yaml
@@ -0,0 +1,11 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: rootfs
+ size: 8MiB
+ type: magnetic
diff --git a/tests/config/ubi.yaml b/tests/config/ubi.yaml
new file mode 100644
index 00000000..24af1795
--- /dev/null
+++ b/tests/config/ubi.yaml
@@ -0,0 +1,19 @@
+api-version: 5
+supported-device-types:
+ - mtd
+partitions:
+ - name: barebox
+ size: 1MiB
+ - name: rootfs
+ size: 400MiB
+ ubi:
+ volumes:
+ - name: kernel
+ size: 8MiB
+ type: static
+ - name: rootfs
+ expand: true
+ filesystem: ubifs
+ mkfs-extra-args: -F
+ input:
+ - filename: lorem.tar
diff --git a/tests/meson.build b/tests/meson.build
index dd1db7df..1cf831df 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -4,6 +4,7 @@ tests = [
'config',
'emmc',
'file',
+ 'mtd',
'package',
'unit',
'utils'
diff --git a/tests/mtd.c b/tests/mtd.c
new file mode 100644
index 00000000..74a05fa0
--- /dev/null
+++ b/tests/mtd.c
@@ -0,0 +1,134 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ * Copyright (c) 2026 PHYTEC Messtechnik GmbH
+ */
+
+#include
+#include
+#include "pu-error.h"
+#include "pu-mtd.h"
+
+/* The device path is only needed to construct the object. Parsing a layout
+ * configuration never accesses the device itself, so no real MTD device is
+ * ever touched by these tests. */
+static const gchar *device_path = "/dev/mtd0";
+
+static void
+mtd_ubi(void)
+{
+ g_autoptr(GError) error = NULL;
+ g_autoptr(PuConfig) config = NULL;
+ g_autoptr(PuMtd) mtd = NULL;
+
+ config = pu_config_new_from_file("config/ubi.yaml", &error);
+ g_assert_no_error(error);
+ g_assert_nonnull(config);
+
+ mtd = pu_mtd_new(device_path, config, "data", FALSE, &error);
+ g_assert_no_error(error);
+ g_assert_nonnull(mtd);
+}
+
+static void
+mtd_ubi_fail(const gchar *config_file,
+ gint expected_code)
+{
+ g_autoptr(GError) error = NULL;
+ g_autoptr(PuConfig) config = NULL;
+ g_autoptr(PuMtd) mtd = NULL;
+
+ config = pu_config_new_from_file(config_file, &error);
+ g_assert_no_error(error);
+ g_assert_nonnull(config);
+
+ mtd = pu_mtd_new(device_path, config, "data", FALSE, &error);
+ g_assert_error(error, PU_ERROR, expected_code);
+ g_assert_null(mtd);
+
+ g_clear_error(&error);
+}
+
+static void
+mtd_ubi_missing_name(void)
+{
+ mtd_ubi_fail("config/ubi-missing-name.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_missing_size(void)
+{
+ mtd_ubi_fail("config/ubi-missing-size.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_missing_volumes(void)
+{
+ mtd_ubi_fail("config/ubi-missing-volumes.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_expand_not_last(void)
+{
+ mtd_ubi_fail("config/ubi-expand-not-last.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_unknown_type(void)
+{
+ mtd_ubi_fail("config/ubi-unknown-type.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_unknown_filesystem(void)
+{
+ mtd_ubi_fail("config/ubi-unknown-filesystem.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_input_too_big(void)
+{
+ mtd_ubi_fail("config/ubi-input-too-big.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_static_filesystem(void)
+{
+ mtd_ubi_fail("config/ubi-static-filesystem.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_static_expand(void)
+{
+ mtd_ubi_fail("config/ubi-static-expand.yaml", PU_ERROR_UBI_PARSE);
+}
+
+static void
+mtd_ubi_input_and_partition(void)
+{
+ mtd_ubi_fail("config/ubi-input-and-partition.yaml", PU_ERROR_MTD_PARSE);
+}
+
+int
+main(int argc,
+ char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+#ifdef PARTUP_TEST_SRCDIR
+ g_chdir(PARTUP_TEST_SRCDIR);
+#endif
+
+ g_test_add_func("/mtd/ubi", mtd_ubi);
+ g_test_add_func("/mtd/ubi/missing-name", mtd_ubi_missing_name);
+ g_test_add_func("/mtd/ubi/missing-size", mtd_ubi_missing_size);
+ g_test_add_func("/mtd/ubi/missing-volumes", mtd_ubi_missing_volumes);
+ g_test_add_func("/mtd/ubi/expand-not-last", mtd_ubi_expand_not_last);
+ g_test_add_func("/mtd/ubi/unknown-type", mtd_ubi_unknown_type);
+ g_test_add_func("/mtd/ubi/unknown-filesystem", mtd_ubi_unknown_filesystem);
+ g_test_add_func("/mtd/ubi/input-too-big", mtd_ubi_input_too_big);
+ g_test_add_func("/mtd/ubi/static-filesystem", mtd_ubi_static_filesystem);
+ g_test_add_func("/mtd/ubi/static-expand", mtd_ubi_static_expand);
+ g_test_add_func("/mtd/ubi/input-and-partition", mtd_ubi_input_and_partition);
+
+ return g_test_run();
+}