From e4dedebc448238156872dd6a81b5cb87753f3d4a Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:41:32 +0100 Subject: [PATCH 01/10] src: utils: Add pu_is_valid_uuid() Configuration values like partuuid are handed to sfdisk unchecked, so a typo only shows up late as an sfdisk failure. Add a helper that checks the canonical 8-4-4-4-12 hexadecimal UUID format, so such values can be validated while parsing the layout configuration. Signed-off-by: Martin Schwan --- src/pu-utils.c | 18 ++++++++++++++++++ src/pu-utils.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/pu-utils.c b/src/pu-utils.c index 863e3a3b..a7127a51 100644 --- a/src/pu-utils.c +++ b/src/pu-utils.c @@ -392,6 +392,24 @@ pu_partition_set_partuuid(const gchar *device, return TRUE; } +gboolean +pu_is_valid_uuid(const gchar *uuid) +{ + if (uuid == NULL || strlen(uuid) != 36) + return FALSE; + + for (gsize i = 0; i < 36; i++) { + if (i == 8 || i == 13 || i == 18 || i == 23) { + if (uuid[i] != '-') + return FALSE; + } else if (!g_ascii_isxdigit(uuid[i])) { + return FALSE; + } + } + + return TRUE; +} + gboolean pu_is_drive(const gchar *device) { diff --git a/src/pu-utils.h b/src/pu-utils.h index 8b488a98..a921de88 100644 --- a/src/pu-utils.h +++ b/src/pu-utils.h @@ -46,6 +46,7 @@ gboolean pu_partition_set_partuuid(const gchar *device, guint index, const gchar *partuuid, GError **error); +gboolean pu_is_valid_uuid(const gchar *uuid); gboolean pu_is_drive(const gchar *device); gboolean pu_is_ext234_image(const gchar *path); gboolean pu_wait_for_partitions(GError **error); From 0e6d1c10e281a922ef1911dc24d0191ac2840925 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:41:39 +0100 Subject: [PATCH 02/10] src: emmc: Reject malformed partuuid on GPT disks A malformed partuuid used to be passed to sfdisk, which failed only while writing to the device. Validate the value when parsing the layout configuration, so the error is reported early and names the partition. Values on non-GPT disks are still ignored with a warning as before. Signed-off-by: Martin Schwan --- src/pu-emmc.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/pu-emmc.c b/src/pu-emmc.c index 1100c172..97477c51 100644 --- a/src/pu-emmc.c +++ b/src/pu-emmc.c @@ -981,6 +981,17 @@ pu_emmc_parse_partitions(PuEmmc *emmc, PuEmmcPartition *part = g_new0(PuEmmcPartition, 1); part->label = pu_hash_table_lookup_string(v->data.mapping, "label", NULL); part->partuuid = pu_hash_table_lookup_string(v->data.mapping, "partuuid", ""); + if (g_strcmp0(part->partuuid, "") > 0 && + g_str_equal(emmc->disktype->name, "gpt") && + !pu_is_valid_uuid(part->partuuid)) { + g_set_error(error, PU_ERROR, PU_ERROR_EMMC_PARSE, + "Invalid partuuid '%s' of partition '%s'", + part->partuuid, part->label ? part->label : ""); + g_free(part->partuuid); + g_free(part->label); + g_free(part); + return FALSE; + } part->filesystem = pu_hash_table_lookup_string(v->data.mapping, "filesystem", NULL); part->mkfs_extra_args = pu_hash_table_lookup_string(v->data.mapping, "mkfs-extra-args", NULL); part->size = pu_hash_table_lookup_sector(v->data.mapping, emmc->device, "size", 0); From b59bfaaac39bef4d4f2e23cbd55a2a1d024fff0d Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:41:48 +0100 Subject: [PATCH 03/10] src: utils: Add pu_partition_set_type_guid() Partition flags only map to a fixed set of GPT partition type GUIDs. Add a helper that sets an arbitrary type GUID using sfdisk --part-type, mirroring pu_partition_set_partuuid(). Signed-off-by: Martin Schwan --- src/pu-utils.c | 25 +++++++++++++++++++++++++ src/pu-utils.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/src/pu-utils.c b/src/pu-utils.c index a7127a51..19fc96a9 100644 --- a/src/pu-utils.c +++ b/src/pu-utils.c @@ -392,6 +392,31 @@ pu_partition_set_partuuid(const gchar *device, return TRUE; } +gboolean +pu_partition_set_type_guid(const gchar *device, + guint index, + const gchar *type_guid, + GError **error) +{ + g_autofree gchar *cmd = NULL; + + g_return_val_if_fail(g_strcmp0(device, "") > 0, FALSE); + g_return_val_if_fail(index > 0, FALSE); + g_return_val_if_fail(pu_is_valid_uuid(type_guid), FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + + cmd = g_strdup_printf("sfdisk --part-type %s %d %s", + device, index, type_guid); + + if (!pu_spawn_command_line_sync(cmd, error)) { + g_prefix_error(error, "Failed setting type GUID on %s partition %d: ", + device, index); + return FALSE; + } + + return TRUE; +} + gboolean pu_is_valid_uuid(const gchar *uuid) { diff --git a/src/pu-utils.h b/src/pu-utils.h index a921de88..365dfeef 100644 --- a/src/pu-utils.h +++ b/src/pu-utils.h @@ -46,6 +46,10 @@ gboolean pu_partition_set_partuuid(const gchar *device, guint index, const gchar *partuuid, GError **error); +gboolean pu_partition_set_type_guid(const gchar *device, + guint index, + const gchar *type_guid, + GError **error); gboolean pu_is_valid_uuid(const gchar *uuid); gboolean pu_is_drive(const gchar *device); gboolean pu_is_ext234_image(const gchar *path); From 110ddf91abfcbb2a2d45aa14a7d2f53e596655b9 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:41:57 +0100 Subject: [PATCH 04/10] src: emmc: Add type-guid partition option Partition flags cannot express arbitrary GPT partition type GUIDs, such as the ones of the Discoverable Partitions Specification. Add the type-guid option to set any type GUID on a partition. Like partuuid, it is validated while parsing on GPT disks and ignored with a warning on other disk labels. Signed-off-by: Martin Schwan --- src/pu-emmc.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/pu-emmc.c b/src/pu-emmc.c index 97477c51..bed43617 100644 --- a/src/pu-emmc.c +++ b/src/pu-emmc.c @@ -31,6 +31,7 @@ typedef struct _PuEmmcInput { typedef struct _PuEmmcPartition { gchar *label; gchar *partuuid; + gchar *type_guid; PedPartitionType type; gchar *filesystem; gchar *mkfs_extra_args; @@ -328,6 +329,15 @@ pu_emmc_write_data(PuFlash *flash, } } + if (g_strcmp0(part->type_guid, "") > 0) { + if (g_str_equal(self->disktype->name, "gpt")) { + if (!pu_partition_set_type_guid(self->device->path, idx, part->type_guid, error)) + return FALSE; + } else { + g_warning("Setting type GUID is only supported on GPT partitioned devices"); + } + } + part_path = pu_device_get_partition_path(self->device->path, idx, error); if (part_path == NULL) return FALSE; @@ -593,6 +603,7 @@ pu_emmc_class_finalize(GObject *object) PuEmmcPartition *part = p->data; g_free(part->label); g_free(part->partuuid); + g_free(part->type_guid); g_free(part->filesystem); g_free(part->mkfs_extra_args); g_list_free(g_steal_pointer(&part->flags)); @@ -981,16 +992,28 @@ pu_emmc_parse_partitions(PuEmmc *emmc, PuEmmcPartition *part = g_new0(PuEmmcPartition, 1); part->label = pu_hash_table_lookup_string(v->data.mapping, "label", NULL); part->partuuid = pu_hash_table_lookup_string(v->data.mapping, "partuuid", ""); - if (g_strcmp0(part->partuuid, "") > 0 && - g_str_equal(emmc->disktype->name, "gpt") && - !pu_is_valid_uuid(part->partuuid)) { - g_set_error(error, PU_ERROR, PU_ERROR_EMMC_PARSE, - "Invalid partuuid '%s' of partition '%s'", - part->partuuid, part->label ? part->label : ""); - g_free(part->partuuid); - g_free(part->label); - g_free(part); - return FALSE; + part->type_guid = pu_hash_table_lookup_string(v->data.mapping, "type-guid", ""); + if (g_str_equal(emmc->disktype->name, "gpt")) { + const gchar *invalid = NULL; + const gchar *name = NULL; + + if (g_strcmp0(part->partuuid, "") > 0 && !pu_is_valid_uuid(part->partuuid)) { + invalid = part->partuuid; + name = "partuuid"; + } else if (g_strcmp0(part->type_guid, "") > 0 && !pu_is_valid_uuid(part->type_guid)) { + invalid = part->type_guid; + name = "type-guid"; + } + if (invalid != NULL) { + g_set_error(error, PU_ERROR, PU_ERROR_EMMC_PARSE, + "Invalid %s '%s' of partition '%s'", + name, invalid, part->label ? part->label : ""); + g_free(part->partuuid); + g_free(part->type_guid); + g_free(part->label); + g_free(part); + return FALSE; + } } part->filesystem = pu_hash_table_lookup_string(v->data.mapping, "filesystem", NULL); part->mkfs_extra_args = pu_hash_table_lookup_string(v->data.mapping, "mkfs-extra-args", NULL); From 549e86c2d15b0aaf0af0b29639a3fba969a35b93 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:30 +0100 Subject: [PATCH 05/10] tests: utils: Test pu_is_valid_uuid() Signed-off-by: Martin Schwan --- tests/utils.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/utils.c b/tests/utils.c index 3e4431b0..c301fc06 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -205,6 +205,19 @@ test_is_ext234_image(void) g_assert_false(pu_is_ext234_image("data/lorem.txt")); } +static void +test_is_valid_uuid(void) +{ + g_assert_true(pu_is_valid_uuid("6a69db6b-0858-404c-b42c-a58f5fb498ff")); + g_assert_true(pu_is_valid_uuid("6A69DB6B-0858-404C-B42C-A58F5FB498FF")); + g_assert_false(pu_is_valid_uuid(NULL)); + g_assert_false(pu_is_valid_uuid("")); + g_assert_false(pu_is_valid_uuid("6a69db6b-0858-404c-b42c-a58f5fb498f")); + g_assert_false(pu_is_valid_uuid("6a69db6b-0858-404c-b42c-a58f5fb498ff0")); + g_assert_false(pu_is_valid_uuid("6a69db6b085804c-b42c-a58f5fb498ff-0")); + g_assert_false(pu_is_valid_uuid("6a69db6b-0858-404c-b42c-a58f5fb498fg")); +} + int main(int argc, char *argv[]) @@ -235,6 +248,7 @@ main(int argc, g_test_add_func("/utils/str_pre_remove", test_str_pre_remove); g_test_add_func("/utils/device_get_partition_pattern", test_device_get_partition_pattern); g_test_add_func("/utils/is_ext234_image", test_is_ext234_image); + g_test_add_func("/utils/is_valid_uuid", test_is_valid_uuid); return g_test_run(); } From ffb71a14b784ed071e6c5f90aa58bec7032b0af2 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:30 +0100 Subject: [PATCH 06/10] tests: emmc: Test partuuid and type-guid validation Cover valid values on GPT, malformed values failing with a parse error on GPT, and the same malformed values being ignored on msdos disks, so existing msdos configurations keep working. Signed-off-by: Martin Schwan --- tests/config/gpt-guids.yaml | 12 +++++ tests/config/gpt-invalid-partuuid.yaml | 8 ++++ tests/config/gpt-invalid-type-guid.yaml | 8 ++++ tests/config/msdos-gpt-options.yaml | 9 ++++ tests/emmc.c | 60 +++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 tests/config/gpt-guids.yaml create mode 100644 tests/config/gpt-invalid-partuuid.yaml create mode 100644 tests/config/gpt-invalid-type-guid.yaml create mode 100644 tests/config/msdos-gpt-options.yaml diff --git a/tests/config/gpt-guids.yaml b/tests/config/gpt-guids.yaml new file mode 100644 index 00000000..34815a32 --- /dev/null +++ b/tests/config/gpt-guids.yaml @@ -0,0 +1,12 @@ +api-version: 1 +disklabel: gpt + +partitions: + - label: BOOT + partuuid: "6a69db6b-0858-404c-b42c-a58f5fb498ff" + type-guid: "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" + size: 32MiB + offset: 1MiB + - label: ROOT + type-guid: "b921b045-1df0-41c3-af44-4c6f280d3fae" + expand: true diff --git a/tests/config/gpt-invalid-partuuid.yaml b/tests/config/gpt-invalid-partuuid.yaml new file mode 100644 index 00000000..e1577a09 --- /dev/null +++ b/tests/config/gpt-invalid-partuuid.yaml @@ -0,0 +1,8 @@ +api-version: 1 +disklabel: gpt + +partitions: + - label: BOOT + partuuid: "not-a-uuid" + size: 32MiB + offset: 1MiB diff --git a/tests/config/gpt-invalid-type-guid.yaml b/tests/config/gpt-invalid-type-guid.yaml new file mode 100644 index 00000000..105e389d --- /dev/null +++ b/tests/config/gpt-invalid-type-guid.yaml @@ -0,0 +1,8 @@ +api-version: 1 +disklabel: gpt + +partitions: + - label: BOOT + type-guid: "6a69db6b-0858-404c-b42c-a58f5fb498f" + size: 32MiB + offset: 1MiB diff --git a/tests/config/msdos-gpt-options.yaml b/tests/config/msdos-gpt-options.yaml new file mode 100644 index 00000000..e410f4dc --- /dev/null +++ b/tests/config/msdos-gpt-options.yaml @@ -0,0 +1,9 @@ +api-version: 1 +disklabel: msdos + +partitions: + - label: BOOT + partuuid: "not-a-uuid" + type-guid: "not-a-guid" + size: 32MiB + offset: 1MiB diff --git a/tests/emmc.c b/tests/emmc.c index c52a6264..d91a2d73 100644 --- a/tests/emmc.c +++ b/tests/emmc.c @@ -118,6 +118,58 @@ test_raw_overwrite_fail_raw(EmptyFileFixture *fixture, g_clear_error(&fixture->error); } +static void +check_parse(EmptyFileFixture *fixture, + const gchar *path, + gboolean expect_ok) +{ + g_autoptr(PuConfig) config = NULL; + g_autoptr(PuEmmc) emmc = NULL; + + config = pu_config_new_from_file(path, &fixture->error); + g_assert_no_error(fixture->error); + g_assert_nonnull(config); + + emmc = pu_emmc_new(g_file_get_path(fixture->file), config, NULL, FALSE, + &fixture->error); + if (expect_ok) { + g_assert_no_error(fixture->error); + g_assert_nonnull(emmc); + } else { + g_assert_error(fixture->error, PU_ERROR, PU_ERROR_EMMC_PARSE); + g_assert_null(emmc); + g_clear_error(&fixture->error); + } +} + +static void +test_gpt_guids(EmptyFileFixture *fixture, + G_GNUC_UNUSED gconstpointer user_data) +{ + check_parse(fixture, "config/gpt-guids.yaml", TRUE); +} + +static void +test_gpt_invalid_partuuid(EmptyFileFixture *fixture, + G_GNUC_UNUSED gconstpointer user_data) +{ + check_parse(fixture, "config/gpt-invalid-partuuid.yaml", FALSE); +} + +static void +test_gpt_invalid_type_guid(EmptyFileFixture *fixture, + G_GNUC_UNUSED gconstpointer user_data) +{ + check_parse(fixture, "config/gpt-invalid-type-guid.yaml", FALSE); +} + +static void +test_msdos_gpt_options(EmptyFileFixture *fixture, + G_GNUC_UNUSED gconstpointer user_data) +{ + check_parse(fixture, "config/msdos-gpt-options.yaml", TRUE); +} + int main(int argc, char *argv[]) @@ -140,6 +192,14 @@ main(int argc, g_test_add("/emmc/raw_overwrite_fail_raw", EmptyFileFixture, "mmcblk0", empty_file_set_up, test_raw_overwrite_fail_raw, empty_file_tear_down); + g_test_add("/emmc/gpt_guids", EmptyFileFixture, "mmcblk0", + empty_file_set_up, test_gpt_guids, empty_file_tear_down); + g_test_add("/emmc/gpt_invalid_partuuid", EmptyFileFixture, "mmcblk0", + empty_file_set_up, test_gpt_invalid_partuuid, empty_file_tear_down); + g_test_add("/emmc/gpt_invalid_type_guid", EmptyFileFixture, "mmcblk0", + empty_file_set_up, test_gpt_invalid_type_guid, empty_file_tear_down); + g_test_add("/emmc/msdos_ignores_gpt_options", EmptyFileFixture, "mmcblk0", + empty_file_set_up, test_msdos_gpt_options, empty_file_tear_down); return g_test_run(); } From 3984cb698be260d6c03685a1a7aef08ea4e43b72 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:37 +0100 Subject: [PATCH 07/10] tests: utils-root: Test pu_partition_set_type_guid() The test needs root privileges for the loop device and was not run as part of the user suite. Signed-off-by: Martin Schwan --- tests/utils-root.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/utils-root.c b/tests/utils-root.c index 38325bbb..28f1f140 100644 --- a/tests/utils-root.c +++ b/tests/utils-root.c @@ -13,6 +13,7 @@ #include "pu-error.h" #define PARTUUID "428957a5-839d-45ae-adaf-4108c98a087b" +#define TYPE_GUID "b921b045-1df0-41c3-af44-4c6f280d3fae" static void test_is_drive(EmptyDeviceFixture *fixture, @@ -59,6 +60,36 @@ test_partition_set_partuuid(EmptyDeviceFixture *fixture, g_assert_cmpstr(partuuid, ==, PARTUUID); } +static void +test_partition_set_type_guid(EmptyDeviceFixture *fixture, + G_GNUC_UNUSED gconstpointer user_data) +{ + blkid_probe pr; + blkid_partlist ls; + blkid_partition par; + const gchar *type; + gint wait_status; + g_autofree gchar *cmd = g_strdup_printf("sh scripts/create-partition %s", + fixture->loop_dev); + g_assert_true(g_spawn_command_line_sync(cmd, NULL, NULL, + &wait_status, &fixture->error)); + g_assert_no_error(fixture->error); + g_assert_true(g_spawn_check_wait_status(wait_status, &fixture->error)); + g_assert_no_error(fixture->error); + g_assert_true(pu_partition_set_type_guid(fixture->loop_dev, 1, + TYPE_GUID, &fixture->error)); + g_assert_no_error(fixture->error); + + pr = blkid_new_probe_from_filename(fixture->loop_dev); + g_assert_nonnull(pr); + ls = blkid_probe_get_partitions(pr); + g_assert_nonnull(ls); + par = blkid_partlist_get_partition(ls, 0); + g_assert_nonnull(par); + type = blkid_partition_get_type_string(par); + g_assert_cmpstr(type, ==, TYPE_GUID); +} + int main(int argc, char *argv[]) @@ -78,6 +109,8 @@ main(int argc, g_test_add_func("/utils/wait_for_partitions", test_wait_for_partitions); g_test_add("/utils/part_set_partuuid", EmptyDeviceFixture, NULL, empty_device_set_up, test_partition_set_partuuid, empty_device_tear_down); + g_test_add("/utils/part_set_type_guid", EmptyDeviceFixture, NULL, empty_device_set_up, + test_partition_set_type_guid, empty_device_tear_down); return g_test_run(); } From 2a5746c32af8c1841bab060f98b3f46d0f666f3e Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:37 +0100 Subject: [PATCH 08/10] tests: system-tests: Use type-guid in gpt.yaml Signed-off-by: Martin Schwan --- tests/config/system-tests/gpt.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/config/system-tests/gpt.yaml b/tests/config/system-tests/gpt.yaml index a72ad979..c091a085 100644 --- a/tests/config/system-tests/gpt.yaml +++ b/tests/config/system-tests/gpt.yaml @@ -16,6 +16,7 @@ partitions: input: - filename: lorem.txt - label: ROOT1 + type-guid: "0fc63daf-8483-4772-8e79-3d69d8477de4" filesystem: ext4 expand: true input: From a3a306a0b38f6c8e808aabafb3989c4e62d32e09 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:52 +0100 Subject: [PATCH 09/10] doc: layout-config-reference: Document type-guid and partuuid validation Describe the new type-guid option and that malformed partuuid and type-guid values are rejected on GPT partitioned devices. Signed-off-by: Martin Schwan --- doc/layout-config-reference.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/layout-config-reference.rst b/doc/layout-config-reference.rst index 053737c2..ce4225eb 100644 --- a/doc/layout-config-reference.rst +++ b/doc/layout-config-reference.rst @@ -133,6 +133,22 @@ options: The PARTUUID of the partition. Only supported on GPT partitioned devices. A random UUID is used by default. + The value must be a UUID in the format + ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``. A malformed value on a GPT + partitioned device results in an error when the layout configuration is + parsed. On other disklabels, the option is ignored with a warning. + +``type-guid`` :badge-type:`string` :badge-since:`5.0.0` + The GPT partition type GUID of the partition, e.g. + ``b921b045-1df0-41c3-af44-4c6f280d3fae``. Only supported on GPT partitioned + devices. The value must be a UUID in the format + ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``. A malformed value results in an + error when the layout configuration is parsed. On other disklabels, the + option is ignored with a warning. + + The type GUID is set after the partition ``flags`` have been applied and + thus overrides any type implied by them. + ``type`` :badge-type:`string` The partition type. May be one of ``primary`` or ``logical``. Note, that with the first occurrence of a logical partition the following ones must be From ced688663c030f0e7627408a42ebee1e41bb5abb Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 30 Oct 2026 10:42:52 +0100 Subject: [PATCH 10/10] CHANGELOG: Add type-guid option and partuuid validation Signed-off-by: Martin Schwan --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5c97a8f6..0ac310a5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,11 @@ - 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 the ``type-guid`` partition option for setting an arbitrary GPT partition + type GUID. +- A malformed ``partuuid`` on GPT partitioned devices now fails with an error + when parsing the layout configuration instead of failing later while writing + to the device. .. _release-4.0.0: