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: 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 diff --git a/src/pu-emmc.c b/src/pu-emmc.c index 1100c172..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,6 +992,29 @@ 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", ""); + 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); part->size = pu_hash_table_lookup_sector(v->data.mapping, emmc->device, "size", 0); diff --git a/src/pu-utils.c b/src/pu-utils.c index 863e3a3b..19fc96a9 100644 --- a/src/pu-utils.c +++ b/src/pu-utils.c @@ -392,6 +392,49 @@ 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) +{ + 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..365dfeef 100644 --- a/src/pu-utils.h +++ b/src/pu-utils.h @@ -46,6 +46,11 @@ 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); gboolean pu_wait_for_partitions(GError **error); 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/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: 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(); } 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(); } 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(); }