]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
gpt: Add gpt_partition_type_override_architecture()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 31 May 2023 11:44:00 +0000 (13:44 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 31 May 2023 11:48:13 +0000 (13:48 +0200)
Let's add a function that allows changing the architecture of a given
partition type.

src/shared/gpt.c
src/shared/gpt.h
src/test/test-gpt.c

index 4df8724f41e1e45bebfdefe1c80a3df1a69c94bb..dd96261888c07f5fddfb4e4c5c169c4c5cd5731c 100644 (file)
@@ -248,6 +248,18 @@ int gpt_partition_type_from_string(const char *s, GptPartitionType *ret) {
         return 0;
 }
 
+GptPartitionType gpt_partition_type_override_architecture(GptPartitionType type, Architecture arch) {
+        assert(arch >= 0);
+
+        FOREACH_ARRAY(t, gpt_partition_type_table, ELEMENTSOF(gpt_partition_type_table) - 1)
+                if (t->designator == type.designator && t->arch == arch)
+                        return *t;
+
+        /* If we can't find an entry with the same designator and the requested architecture, just return the
+         * original partition type. */
+        return type;
+}
+
 Architecture gpt_partition_type_uuid_to_arch(sd_id128_t id) {
         const GptPartitionType *pt;
 
index bebfbc61167437fb29531c5d3fc10b142ffe384d..8623a8664e5413bdb368bfc449e8b68f81d80ae2 100644 (file)
@@ -62,6 +62,8 @@ int gpt_partition_label_valid(const char *s);
 GptPartitionType gpt_partition_type_from_uuid(sd_id128_t id);
 int gpt_partition_type_from_string(const char *s, GptPartitionType *ret);
 
+GptPartitionType gpt_partition_type_override_architecture(GptPartitionType type, Architecture arch);
+
 const char *gpt_partition_type_mountpoint_nulstr(GptPartitionType type);
 
 bool gpt_partition_type_knows_read_only(GptPartitionType type);
index 5ad30fab446a8db33b59fa93aec62ff9dd13ac00..fa5923eb2777212aba948e4480312524bd28f4a0 100644 (file)
@@ -81,4 +81,31 @@ TEST(type_alias_same) {
         }
 }
 
+TEST(override_architecture) {
+        GptPartitionType x, y;
+
+        assert_se(gpt_partition_type_from_string("root-x86-64", &x) >= 0);
+        assert_se(x.arch == ARCHITECTURE_X86_64);
+
+        assert_se(gpt_partition_type_from_string("root-arm64", &y) >= 0);
+        assert(y.arch == ARCHITECTURE_ARM64);
+
+        x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64);
+        assert_se(x.arch == y.arch);
+        assert_se(x.designator == y.designator);
+        assert_se(sd_id128_equal(x.uuid, y.uuid));
+        assert_se(streq(x.name, y.name));
+
+        /* If the partition type does not have an architecture, nothing should change. */
+
+        assert_se(gpt_partition_type_from_string("esp", &x) >= 0);
+        y = x;
+
+        x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64);
+        assert_se(x.arch == y.arch);
+        assert_se(x.designator == y.designator);
+        assert_se(sd_id128_equal(x.uuid, y.uuid));
+        assert_se(streq(x.name, y.name));
+}
+
 DEFINE_TEST_MAIN(LOG_INFO);