summaryrefslogtreecommitdiff
path: root/src/shared/gpt.c
blob: a2e39dc1a15720af448c04c88d152e6bb968546f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "gpt.h"
#include "string-util.h"
#include "utf8.h"

const GptPartitionType gpt_partition_type_table[] = {
        { GPT_ROOT_X86,                    "root-x86"                    },
        { GPT_ROOT_X86_VERITY,             "root-x86-verity"             },
        { GPT_ROOT_X86_VERITY_SIG,         "root-x86-verity-sig"         },
        { GPT_ROOT_X86_64,                 "root-x86-64"                 },
        { GPT_ROOT_X86_64_VERITY,          "root-x86-64-verity"          },
        { GPT_ROOT_X86_64_VERITY_SIG,      "root-x86-64-verity-sig"      },
        { GPT_ROOT_ARM,                    "root-arm"                    },
        { GPT_ROOT_ARM_VERITY,             "root-arm-verity"             },
        { GPT_ROOT_ARM_VERITY_SIG,         "root-arm-verity-sig"         },
        { GPT_ROOT_ARM_64,                 "root-arm64"                  },
        { GPT_ROOT_ARM_64_VERITY,          "root-arm64-verity"           },
        { GPT_ROOT_ARM_64_VERITY_SIG,      "root-arm64-verity-sig"       },
        { GPT_ROOT_IA64,                   "root-ia64"                   },
        { GPT_ROOT_IA64_VERITY,            "root-ia64-verity"            },
        { GPT_ROOT_IA64_VERITY_SIG,        "root-ia64-verity-sig"        },
        { GPT_ROOT_LOONGARCH64,            "root-loongarch64"            },
        { GPT_ROOT_LOONGARCH64_VERITY,     "root-loongarch64-verity"     },
        { GPT_ROOT_LOONGARCH64_VERITY_SIG, "root-loongarch64-verity-sig" },
        { GPT_ROOT_RISCV32,                "root-riscv32"                },
        { GPT_ROOT_RISCV32_VERITY,         "root-riscv32-verity"         },
        { GPT_ROOT_RISCV32_VERITY_SIG,     "root-riscv32-verity-sig"     },
        { GPT_ROOT_RISCV64,                "root-riscv64"                },
        { GPT_ROOT_RISCV64_VERITY,         "root-riscv64-verity"         },
        { GPT_ROOT_RISCV64_VERITY_SIG,     "root-riscv64-verity-sig"     },
#ifdef GPT_ROOT_NATIVE
        { GPT_ROOT_NATIVE,                 "root"                        },
        { GPT_ROOT_NATIVE_VERITY,          "root-verity"                 },
        { GPT_ROOT_NATIVE_VERITY_SIG,      "root-verity-sig"             },
#endif
#ifdef GPT_ROOT_SECONDARY
        { GPT_ROOT_SECONDARY,              "root-secondary"              },
        { GPT_ROOT_SECONDARY_VERITY,       "root-secondary-verity"       },
        { GPT_ROOT_SECONDARY_VERITY_SIG,   "root-secondary-verity-sig"   },
#endif
        { GPT_USR_X86,                     "usr-x86"                     },
        { GPT_USR_X86_VERITY,              "usr-x86-verity"              },
        { GPT_USR_X86_VERITY_SIG,          "usr-x86-verity-sig"          },
        { GPT_USR_X86_64,                  "usr-x86-64"                  },
        { GPT_USR_X86_64_VERITY,           "usr-x86-64-verity"           },
        { GPT_USR_X86_64_VERITY_SIG,       "usr-x86-64-verity-sig"       },
        { GPT_USR_ARM,                     "usr-arm"                     },
        { GPT_USR_ARM_VERITY,              "usr-arm-verity"              },
        { GPT_USR_ARM_VERITY_SIG,          "usr-arm-verity-sig"          },
        { GPT_USR_ARM_64,                  "usr-arm64"                   },
        { GPT_USR_ARM_64_VERITY,           "usr-arm64-verity"            },
        { GPT_USR_ARM_64_VERITY_SIG,       "usr-arm64-verity-sig"        },
        { GPT_USR_IA64,                    "usr-ia64"                    },
        { GPT_USR_IA64_VERITY,             "usr-ia64-verity"             },
        { GPT_USR_IA64_VERITY_SIG,         "usr-ia64-verity-sig"         },
        { GPT_USR_LOONGARCH64,             "usr-loongarch64"             },
        { GPT_USR_LOONGARCH64_VERITY,      "usr-loongarch64-verity"      },
        { GPT_USR_LOONGARCH64_VERITY_SIG,  "usr-loongarch64-verity-sig"  },
        { GPT_USR_RISCV32,                 "usr-riscv32"                 },
        { GPT_USR_RISCV32_VERITY,          "usr-riscv32-verity"          },
        { GPT_USR_RISCV32_VERITY_SIG,      "usr-riscv32-verity-sig"      },
        { GPT_USR_RISCV64,                 "usr-riscv64"                 },
        { GPT_USR_RISCV64_VERITY,          "usr-riscv64-verity"          },
        { GPT_USR_RISCV64_VERITY_SIG,      "usr-riscv64-verity-sig"      },
#ifdef GPT_USR_NATIVE
        { GPT_USR_NATIVE,                  "usr"                         },
        { GPT_USR_NATIVE_VERITY,           "usr-verity"                  },
        { GPT_USR_NATIVE_VERITY_SIG,       "usr-verity-sig"              },
#endif
#ifdef GPT_USR_SECONDARY
        { GPT_USR_SECONDARY,               "usr-secondary"               },
        { GPT_USR_SECONDARY_VERITY,        "usr-secondary-verity"        },
        { GPT_USR_SECONDARY_VERITY_SIG,    "usr-secondary-verity-sig"    },
#endif
        { GPT_ESP,                         "esp"                         },
        { GPT_XBOOTLDR,                    "xbootldr"                    },
        { GPT_SWAP,                        "swap"                        },
        { GPT_HOME,                        "home"                        },
        { GPT_SRV,                         "srv"                         },
        { GPT_VAR,                         "var"                         },
        { GPT_TMP,                         "tmp"                         },
        { GPT_USER_HOME,                   "user-home"                   },
        { GPT_LINUX_GENERIC,               "linux-generic"               },
        {}
};

const char *gpt_partition_type_uuid_to_string(sd_id128_t id) {
        for (size_t i = 0; i < ELEMENTSOF(gpt_partition_type_table) - 1; i++)
                if (sd_id128_equal(id, gpt_partition_type_table[i].uuid))
                        return gpt_partition_type_table[i].name;

        return NULL;
}

const char *gpt_partition_type_uuid_to_string_harder(
                sd_id128_t id,
                char buffer[static ID128_UUID_STRING_MAX]) {

        const char *s;

        assert(buffer);

        s = gpt_partition_type_uuid_to_string(id);
        if (s)
                return s;

        return id128_to_uuid_string(id, buffer);
}

int gpt_partition_type_uuid_from_string(const char *s, sd_id128_t *ret) {
        assert(s);
        assert(ret);

        for (size_t i = 0; i < ELEMENTSOF(gpt_partition_type_table) - 1; i++)
                if (streq(s, gpt_partition_type_table[i].name)) {
                        *ret = gpt_partition_type_table[i].uuid;
                        return 0;
                }

        return sd_id128_from_string(s, ret);
}

int gpt_partition_label_valid(const char *s) {
        _cleanup_free_ char16_t *recoded = NULL;

        recoded = utf8_to_utf16(s, strlen(s));
        if (!recoded)
                return -ENOMEM;

        return char16_strlen(recoded) <= GPT_LABEL_MAX;
}

bool gpt_partition_type_is_root(sd_id128_t id) {
        return sd_id128_in_set(id,
                               GPT_ROOT_X86,
                               GPT_ROOT_X86_64,
                               GPT_ROOT_ARM,
                               GPT_ROOT_ARM_64,
                               GPT_ROOT_IA64,
                               GPT_ROOT_LOONGARCH64,
                               GPT_ROOT_RISCV32,
                               GPT_ROOT_RISCV64);
}

bool gpt_partition_type_is_root_verity(sd_id128_t id) {
        return sd_id128_in_set(id,
                               GPT_ROOT_X86_VERITY,
                               GPT_ROOT_X86_64_VERITY,
                               GPT_ROOT_ARM_VERITY,
                               GPT_ROOT_ARM_64_VERITY,
                               GPT_ROOT_IA64_VERITY,
                               GPT_ROOT_LOONGARCH64_VERITY,
                               GPT_ROOT_RISCV32_VERITY,
                               GPT_ROOT_RISCV64_VERITY);
}

bool gpt_partition_type_is_usr(sd_id128_t id) {
        return sd_id128_in_set(id,
                               GPT_USR_X86,
                               GPT_USR_X86_64,
                               GPT_USR_ARM,
                               GPT_USR_ARM_64,
                               GPT_USR_IA64,
                               GPT_USR_LOONGARCH64,
                               GPT_USR_RISCV32,
                               GPT_USR_RISCV64);
}

bool gpt_partition_type_is_usr_verity(sd_id128_t id) {
        return sd_id128_in_set(id,
                               GPT_USR_X86_VERITY,
                               GPT_USR_X86_64_VERITY,
                               GPT_USR_ARM_VERITY,
                               GPT_USR_ARM_64_VERITY,
                               GPT_USR_IA64_VERITY,
                               GPT_USR_LOONGARCH64_VERITY,
                               GPT_USR_RISCV32_VERITY,
                               GPT_USR_RISCV64_VERITY);
}

bool gpt_partition_type_knows_read_only(sd_id128_t id) {
        return gpt_partition_type_is_root(id) ||
                gpt_partition_type_is_usr(id) ||
                sd_id128_in_set(id,
                                GPT_HOME,
                                GPT_SRV,
                                GPT_VAR,
                                GPT_TMP,
                                GPT_XBOOTLDR) ||
                gpt_partition_type_is_root_verity(id) || /* pretty much implied, but let's set the bit to make things really clear */
                gpt_partition_type_is_usr_verity(id);    /* ditto */
}

bool gpt_partition_type_knows_growfs(sd_id128_t id) {
        return gpt_partition_type_is_root(id) ||
                gpt_partition_type_is_usr(id) ||
                sd_id128_in_set(id,
                                GPT_HOME,
                                GPT_SRV,
                                GPT_VAR,
                                GPT_TMP,
                                GPT_XBOOTLDR);
}

bool gpt_partition_type_knows_no_auto(sd_id128_t id) {
        return gpt_partition_type_is_root(id) ||
                gpt_partition_type_is_root_verity(id) ||
                gpt_partition_type_is_usr(id) ||
                gpt_partition_type_is_usr_verity(id) ||
                sd_id128_in_set(id,
                                GPT_HOME,
                                GPT_SRV,
                                GPT_VAR,
                                GPT_TMP,
                                GPT_XBOOTLDR,
                                GPT_SWAP);
}