| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "gpt.h" |
| 7 |
#include "string-table.h" |
| 8 |
#include "string-util.h" |
| 9 |
#include "utf8.h" |
| 10 |
|
| 11 |
/* Gently push people towards defining GPT type UUIDs for all architectures we know */ |
| 12 |
#if !defined(SD_GPT_ROOT_NATIVE) || \ |
| 13 |
!defined(SD_GPT_ROOT_NATIVE_VERITY) || \ |
| 14 |
!defined(SD_GPT_ROOT_NATIVE_VERITY_SIG) || \ |
| 15 |
!defined(SD_GPT_USR_NATIVE) || \ |
| 16 |
!defined(SD_GPT_USR_NATIVE_VERITY) || \ |
| 17 |
!defined(SD_GPT_USR_NATIVE_VERITY_SIG) |
| 18 |
#pragma message "Please define GPT partition types for your architecture." |
| 19 |
#endif |
| 20 |
|
| 21 |
bool partition_designator_is_versioned(PartitionDesignator d) { |
| 22 |
/* Returns true for all designators where we want to support a concept of "versioning", i.e. which |
| 23 |
* likely contain software binaries (or hashes thereof) that make sense to be versioned as a |
| 24 |
* whole. We use this check to automatically pick the newest version of these partitions, by version |
| 25 |
* comparing the partition labels. */ |
| 26 |
|
| 27 |
return IN_SET(d, |
| 28 |
PARTITION_ROOT, |
| 29 |
PARTITION_USR, |
| 30 |
PARTITION_ROOT_VERITY, |
| 31 |
PARTITION_USR_VERITY, |
| 32 |
PARTITION_ROOT_VERITY_SIG, |
| 33 |
PARTITION_USR_VERITY_SIG); |
| 34 |
} |
| 35 |
|
| 36 |
PartitionDesignator partition_verity_hash_of(PartitionDesignator p) { |
| 37 |
switch (p) { |
| 38 |
|
| 39 |
case PARTITION_ROOT: |
| 40 |
return PARTITION_ROOT_VERITY; |
| 41 |
|
| 42 |
case PARTITION_USR: |
| 43 |
return PARTITION_USR_VERITY; |
| 44 |
|
| 45 |
default: |
| 46 |
return _PARTITION_DESIGNATOR_INVALID; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
PartitionDesignator partition_verity_sig_of(PartitionDesignator p) { |
| 51 |
switch (p) { |
| 52 |
|
| 53 |
case PARTITION_ROOT: |
| 54 |
return PARTITION_ROOT_VERITY_SIG; |
| 55 |
|
| 56 |
case PARTITION_USR: |
| 57 |
return PARTITION_USR_VERITY_SIG; |
| 58 |
|
| 59 |
default: |
| 60 |
return _PARTITION_DESIGNATOR_INVALID; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
PartitionDesignator partition_verity_hash_to_data(PartitionDesignator d) { |
| 65 |
switch (d) { |
| 66 |
|
| 67 |
case PARTITION_ROOT_VERITY: |
| 68 |
return PARTITION_ROOT; |
| 69 |
|
| 70 |
case PARTITION_USR_VERITY: |
| 71 |
return PARTITION_USR; |
| 72 |
|
| 73 |
default: |
| 74 |
return _PARTITION_DESIGNATOR_INVALID; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
PartitionDesignator partition_verity_sig_to_data(PartitionDesignator d) { |
| 79 |
switch (d) { |
| 80 |
|
| 81 |
case PARTITION_ROOT_VERITY_SIG: |
| 82 |
return PARTITION_ROOT; |
| 83 |
|
| 84 |
case PARTITION_USR_VERITY_SIG: |
| 85 |
return PARTITION_USR; |
| 86 |
|
| 87 |
default: |
| 88 |
return _PARTITION_DESIGNATOR_INVALID; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
PartitionDesignator partition_verity_to_data(PartitionDesignator d) { |
| 93 |
PartitionDesignator e = partition_verity_hash_to_data(d); |
| 94 |
if (e >= 0) |
| 95 |
return e; |
| 96 |
|
| 97 |
return partition_verity_sig_to_data(d); |
| 98 |
} |
| 99 |
|
| 100 |
static const char *const partition_designator_table[_PARTITION_DESIGNATOR_MAX] = { |
| 101 |
[PARTITION_ROOT] = "root", |
| 102 |
[PARTITION_USR] = "usr", |
| 103 |
[PARTITION_HOME] = "home", |
| 104 |
[PARTITION_SRV] = "srv", |
| 105 |
[PARTITION_ESP] = "esp", |
| 106 |
[PARTITION_XBOOTLDR] = "xbootldr", |
| 107 |
[PARTITION_SWAP] = "swap", |
| 108 |
[PARTITION_ROOT_VERITY] = "root-verity", |
| 109 |
[PARTITION_USR_VERITY] = "usr-verity", |
| 110 |
[PARTITION_ROOT_VERITY_SIG] = "root-verity-sig", |
| 111 |
[PARTITION_USR_VERITY_SIG] = "usr-verity-sig", |
| 112 |
[PARTITION_TMP] = "tmp", |
| 113 |
[PARTITION_VAR] = "var", |
| 114 |
}; |
| 115 |
|
| 116 |
DEFINE_STRING_TABLE_LOOKUP(partition_designator, PartitionDesignator); |
| 117 |
|
| 118 |
static const char *const partition_mountpoint_table[_PARTITION_DESIGNATOR_MAX] = { |
| 119 |
[PARTITION_ROOT] = "/\0", |
| 120 |
[PARTITION_USR] = "/usr\0", |
| 121 |
[PARTITION_HOME] = "/home\0", |
| 122 |
[PARTITION_SRV] = "/srv\0", |
| 123 |
[PARTITION_ESP] = "/efi\0/boot\0", |
| 124 |
[PARTITION_XBOOTLDR] = "/boot\0", |
| 125 |
[PARTITION_TMP] = "/var/tmp\0", |
| 126 |
[PARTITION_VAR] = "/var\0", |
| 127 |
}; |
| 128 |
|
| 129 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(partition_mountpoint, PartitionDesignator); |
| 130 |
|
| 131 |
#define _GPT_ARCH_SEXTET(arch, name) \ |
| 132 |
{ SD_GPT_ROOT_##arch, "root-" name, ARCHITECTURE_##arch, .designator = PARTITION_ROOT }, \ |
| 133 |
{ SD_GPT_ROOT_##arch##_VERITY, "root-" name "-verity", ARCHITECTURE_##arch, .designator = PARTITION_ROOT_VERITY }, \ |
| 134 |
{ SD_GPT_ROOT_##arch##_VERITY_SIG, "root-" name "-verity-sig", ARCHITECTURE_##arch, .designator = PARTITION_ROOT_VERITY_SIG }, \ |
| 135 |
{ SD_GPT_USR_##arch, "usr-" name, ARCHITECTURE_##arch, .designator = PARTITION_USR }, \ |
| 136 |
{ SD_GPT_USR_##arch##_VERITY, "usr-" name "-verity", ARCHITECTURE_##arch, .designator = PARTITION_USR_VERITY }, \ |
| 137 |
{ SD_GPT_USR_##arch##_VERITY_SIG, "usr-" name "-verity-sig", ARCHITECTURE_##arch, .designator = PARTITION_USR_VERITY_SIG } |
| 138 |
|
| 139 |
/* Two special cases: alias aarch64 to arm64, and amd64 to x86-64. The DSP mixes debianisms and CPUisms: for |
| 140 |
* x86, it uses x86 and x86_64, but for aarch64 it uses arm64. This is confusing, and leads to issues for |
| 141 |
* callers that have to know which -ism to use for which architecture. But we also don't really want to |
| 142 |
* change the spec and add new partition labels, so add a user-friendly aliasing here, so that both are |
| 143 |
* accepted but the end result on disk (ie: the partition label). |
| 144 |
* So always list the canonical name FIRST, and then any aliases later, so that we can match on aliases, |
| 145 |
* but always return the canonical name. And never return directly a match on the name, always re-resolve |
| 146 |
* by UUID so that the canonical entry is always found. */ |
| 147 |
|
| 148 |
const GptPartitionType gpt_partition_type_table[] = { |
| 149 |
_GPT_ARCH_SEXTET(ALPHA, "alpha"), |
| 150 |
_GPT_ARCH_SEXTET(ARC, "arc"), |
| 151 |
_GPT_ARCH_SEXTET(ARM, "arm"), |
| 152 |
_GPT_ARCH_SEXTET(ARM, "armv7l"), /* Alias: must be listed after arm */ |
| 153 |
_GPT_ARCH_SEXTET(ARM64, "arm64"), |
| 154 |
_GPT_ARCH_SEXTET(ARM64, "aarch64"), /* Alias: must be listed after arm64 */ |
| 155 |
_GPT_ARCH_SEXTET(IA64, "ia64"), |
| 156 |
_GPT_ARCH_SEXTET(LOONGARCH64, "loongarch64"), |
| 157 |
_GPT_ARCH_SEXTET(LOONGARCH64, "loong64"), /* Alias: must be listed after loongarch64 */ |
| 158 |
_GPT_ARCH_SEXTET(MIPS, "mips"), |
| 159 |
_GPT_ARCH_SEXTET(MIPS64, "mips64"), |
| 160 |
_GPT_ARCH_SEXTET(MIPS_LE, "mips-le"), |
| 161 |
_GPT_ARCH_SEXTET(MIPS_LE, "mipsel"), /* Alias: must be listed after mips-le */ |
| 162 |
_GPT_ARCH_SEXTET(MIPS64_LE, "mips64-le"), |
| 163 |
_GPT_ARCH_SEXTET(MIPS64_LE, "mips64el"), /* Alias: must be listed after mips64-le */ |
| 164 |
_GPT_ARCH_SEXTET(PARISC, "parisc"), |
| 165 |
_GPT_ARCH_SEXTET(PARISC, "hppa"), /* Alias: must be listed after parisc */ |
| 166 |
_GPT_ARCH_SEXTET(PPC, "ppc"), |
| 167 |
_GPT_ARCH_SEXTET(PPC64, "ppc64"), |
| 168 |
_GPT_ARCH_SEXTET(PPC64_LE, "ppc64-le"), |
| 169 |
_GPT_ARCH_SEXTET(PPC64_LE, "ppc64le"), /* Alias: must be listed after ppc64-le */ |
| 170 |
_GPT_ARCH_SEXTET(PPC64_LE, "ppc64el"), /* Alias: must be listed after ppc64-le */ |
| 171 |
_GPT_ARCH_SEXTET(RISCV32, "riscv32"), |
| 172 |
_GPT_ARCH_SEXTET(RISCV64, "riscv64"), |
| 173 |
_GPT_ARCH_SEXTET(S390, "s390"), |
| 174 |
_GPT_ARCH_SEXTET(S390X, "s390x"), |
| 175 |
_GPT_ARCH_SEXTET(TILEGX, "tilegx"), |
| 176 |
_GPT_ARCH_SEXTET(X86, "x86"), |
| 177 |
_GPT_ARCH_SEXTET(X86, "i386"), /* Alias: must be listed after x86 */ |
| 178 |
_GPT_ARCH_SEXTET(X86, "i486"), /* Alias: must be listed after x86 */ |
| 179 |
_GPT_ARCH_SEXTET(X86, "i586"), /* Alias: must be listed after x86 */ |
| 180 |
_GPT_ARCH_SEXTET(X86, "i686"), /* Alias: must be listed after x86 */ |
| 181 |
_GPT_ARCH_SEXTET(X86_64, "x86-64"), |
| 182 |
_GPT_ARCH_SEXTET(X86_64, "x86_64"), /* Alias: must be listed after x86-64 */ |
| 183 |
_GPT_ARCH_SEXTET(X86_64, "amd64"), /* Alias: must be listed after x86-64 */ |
| 184 |
#ifdef SD_GPT_ROOT_NATIVE |
| 185 |
{ SD_GPT_ROOT_NATIVE, "root", native_architecture(), .designator = PARTITION_ROOT }, |
| 186 |
{ SD_GPT_ROOT_NATIVE_VERITY, "root-verity", native_architecture(), .designator = PARTITION_ROOT_VERITY }, |
| 187 |
{ SD_GPT_ROOT_NATIVE_VERITY_SIG, "root-verity-sig", native_architecture(), .designator = PARTITION_ROOT_VERITY_SIG }, |
| 188 |
{ SD_GPT_USR_NATIVE, "usr", native_architecture(), .designator = PARTITION_USR }, |
| 189 |
{ SD_GPT_USR_NATIVE_VERITY, "usr-verity", native_architecture(), .designator = PARTITION_USR_VERITY }, |
| 190 |
{ SD_GPT_USR_NATIVE_VERITY_SIG, "usr-verity-sig", native_architecture(), .designator = PARTITION_USR_VERITY_SIG }, |
| 191 |
#endif |
| 192 |
#ifdef SD_GPT_ROOT_SECONDARY |
| 193 |
{ SD_GPT_ROOT_SECONDARY, "root-secondary", ARCHITECTURE_SECONDARY, .designator = PARTITION_ROOT }, |
| 194 |
{ SD_GPT_ROOT_SECONDARY_VERITY, "root-secondary-verity", ARCHITECTURE_SECONDARY, .designator = PARTITION_ROOT_VERITY }, |
| 195 |
{ SD_GPT_ROOT_SECONDARY_VERITY_SIG, "root-secondary-verity-sig", ARCHITECTURE_SECONDARY, .designator = PARTITION_ROOT_VERITY_SIG }, |
| 196 |
{ SD_GPT_USR_SECONDARY, "usr-secondary", ARCHITECTURE_SECONDARY, .designator = PARTITION_USR }, |
| 197 |
{ SD_GPT_USR_SECONDARY_VERITY, "usr-secondary-verity", ARCHITECTURE_SECONDARY, .designator = PARTITION_USR_VERITY }, |
| 198 |
{ SD_GPT_USR_SECONDARY_VERITY_SIG, "usr-secondary-verity-sig", ARCHITECTURE_SECONDARY, .designator = PARTITION_USR_VERITY_SIG }, |
| 199 |
#endif |
| 200 |
|
| 201 |
{ SD_GPT_ESP, "esp", _ARCHITECTURE_INVALID, .designator = PARTITION_ESP }, |
| 202 |
{ SD_GPT_XBOOTLDR, "xbootldr", _ARCHITECTURE_INVALID, .designator = PARTITION_XBOOTLDR }, |
| 203 |
{ SD_GPT_SWAP, "swap", _ARCHITECTURE_INVALID, .designator = PARTITION_SWAP }, |
| 204 |
{ SD_GPT_HOME, "home", _ARCHITECTURE_INVALID, .designator = PARTITION_HOME }, |
| 205 |
{ SD_GPT_SRV, "srv", _ARCHITECTURE_INVALID, .designator = PARTITION_SRV }, |
| 206 |
{ SD_GPT_VAR, "var", _ARCHITECTURE_INVALID, .designator = PARTITION_VAR }, |
| 207 |
{ SD_GPT_TMP, "tmp", _ARCHITECTURE_INVALID, .designator = PARTITION_TMP }, |
| 208 |
{ SD_GPT_USER_HOME, "user-home", _ARCHITECTURE_INVALID, .designator = _PARTITION_DESIGNATOR_INVALID }, |
| 209 |
{ SD_GPT_LINUX_GENERIC, "linux-generic", _ARCHITECTURE_INVALID, .designator = _PARTITION_DESIGNATOR_INVALID }, |
| 210 |
{} |
| 211 |
}; |
| 212 |
|
| 213 |
static const GptPartitionType *gpt_partition_type_find_by_uuid(sd_id128_t id) { |
| 214 |
|
| 215 |
FOREACH_ARRAY(t, gpt_partition_type_table, ELEMENTSOF(gpt_partition_type_table) - 1) |
| 216 |
if (sd_id128_equal(id, t->uuid)) |
| 217 |
return t; |
| 218 |
|
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
const char* gpt_partition_type_uuid_to_string(sd_id128_t id) { |
| 223 |
const GptPartitionType *pt; |
| 224 |
|
| 225 |
pt = gpt_partition_type_find_by_uuid(id); |
| 226 |
if (!pt) |
| 227 |
return NULL; |
| 228 |
|
| 229 |
return pt->name; |
| 230 |
} |
| 231 |
|
| 232 |
const char* gpt_partition_type_uuid_to_string_harder( |
| 233 |
sd_id128_t id, |
| 234 |
char buffer[static SD_ID128_UUID_STRING_MAX]) { |
| 235 |
|
| 236 |
const char *s; |
| 237 |
|
| 238 |
assert(buffer); |
| 239 |
|
| 240 |
s = gpt_partition_type_uuid_to_string(id); |
| 241 |
if (s) |
| 242 |
return s; |
| 243 |
|
| 244 |
return sd_id128_to_uuid_string(id, buffer); |
| 245 |
} |
| 246 |
|
| 247 |
int gpt_partition_type_from_string(const char *s, GptPartitionType *ret) { |
| 248 |
sd_id128_t id = SD_ID128_NULL; |
| 249 |
int r; |
| 250 |
|
| 251 |
assert(s); |
| 252 |
|
| 253 |
FOREACH_ARRAY(t, gpt_partition_type_table, ELEMENTSOF(gpt_partition_type_table) - 1) |
| 254 |
if (streq(s, t->name)) { |
| 255 |
/* Don't return immediately, instead re-resolve by UUID so that we can support |
| 256 |
* aliases like aarch64 -> arm64 transparently. */ |
| 257 |
id = t->uuid; |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
if (sd_id128_is_null(id)) { |
| 262 |
r = sd_id128_from_string(s, &id); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
} |
| 266 |
|
| 267 |
if (ret) |
| 268 |
*ret = gpt_partition_type_from_uuid(id); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
GptPartitionType gpt_partition_type_override_architecture(GptPartitionType type, Architecture arch) { |
| 274 |
assert(arch >= 0); |
| 275 |
|
| 276 |
FOREACH_ARRAY(t, gpt_partition_type_table, ELEMENTSOF(gpt_partition_type_table) - 1) |
| 277 |
if (t->designator == type.designator && t->arch == arch) |
| 278 |
return *t; |
| 279 |
|
| 280 |
/* If we can't find an entry with the same designator and the requested architecture, just return the |
| 281 |
* original partition type. */ |
| 282 |
return type; |
| 283 |
} |
| 284 |
|
| 285 |
Architecture gpt_partition_type_uuid_to_arch(sd_id128_t id) { |
| 286 |
const GptPartitionType *pt; |
| 287 |
|
| 288 |
pt = gpt_partition_type_find_by_uuid(id); |
| 289 |
if (!pt) |
| 290 |
return _ARCHITECTURE_INVALID; |
| 291 |
|
| 292 |
return pt->arch; |
| 293 |
} |
| 294 |
|
| 295 |
int gpt_partition_label_valid(const char *s) { |
| 296 |
_cleanup_free_ char16_t *recoded = NULL; |
| 297 |
|
| 298 |
recoded = utf8_to_utf16(s, SIZE_MAX); |
| 299 |
if (!recoded) |
| 300 |
return -ENOMEM; |
| 301 |
|
| 302 |
return char16_strlen(recoded) <= GPT_LABEL_MAX; |
| 303 |
} |
| 304 |
|
| 305 |
GptPartitionType gpt_partition_type_from_uuid(sd_id128_t id) { |
| 306 |
const GptPartitionType *pt; |
| 307 |
|
| 308 |
pt = gpt_partition_type_find_by_uuid(id); |
| 309 |
if (pt) |
| 310 |
return *pt; |
| 311 |
|
| 312 |
return (GptPartitionType) { |
| 313 |
.uuid = id, |
| 314 |
.arch = _ARCHITECTURE_INVALID, |
| 315 |
.designator = _PARTITION_DESIGNATOR_INVALID, |
| 316 |
}; |
| 317 |
} |
| 318 |
|
| 319 |
const char* gpt_partition_type_mountpoint_nulstr(GptPartitionType type) { |
| 320 |
return partition_mountpoint_to_string(type.designator); |
| 321 |
} |
| 322 |
|
| 323 |
bool gpt_partition_type_knows_read_only(GptPartitionType type) { |
| 324 |
return IN_SET(type.designator, |
| 325 |
PARTITION_ROOT, |
| 326 |
PARTITION_USR, |
| 327 |
/* pretty much implied, but let's set the bit to make things really clear */ |
| 328 |
PARTITION_ROOT_VERITY, |
| 329 |
PARTITION_USR_VERITY, |
| 330 |
PARTITION_ROOT_VERITY_SIG, |
| 331 |
PARTITION_USR_VERITY_SIG, |
| 332 |
PARTITION_HOME, |
| 333 |
PARTITION_SRV, |
| 334 |
PARTITION_VAR, |
| 335 |
PARTITION_TMP, |
| 336 |
PARTITION_XBOOTLDR); |
| 337 |
} |
| 338 |
|
| 339 |
bool gpt_partition_type_knows_growfs(GptPartitionType type) { |
| 340 |
return IN_SET(type.designator, |
| 341 |
PARTITION_ROOT, |
| 342 |
PARTITION_USR, |
| 343 |
PARTITION_HOME, |
| 344 |
PARTITION_SRV, |
| 345 |
PARTITION_VAR, |
| 346 |
PARTITION_TMP, |
| 347 |
PARTITION_XBOOTLDR); |
| 348 |
} |
| 349 |
|
| 350 |
bool gpt_partition_type_knows_no_auto(GptPartitionType type) { |
| 351 |
return IN_SET(type.designator, |
| 352 |
PARTITION_ROOT, |
| 353 |
PARTITION_ROOT_VERITY, |
| 354 |
PARTITION_USR, |
| 355 |
PARTITION_USR_VERITY, |
| 356 |
PARTITION_HOME, |
| 357 |
PARTITION_SRV, |
| 358 |
PARTITION_VAR, |
| 359 |
PARTITION_TMP, |
| 360 |
PARTITION_XBOOTLDR, |
| 361 |
PARTITION_SWAP); |
| 362 |
} |
| 363 |
|
| 364 |
bool gpt_partition_type_has_filesystem(GptPartitionType type) { |
| 365 |
return IN_SET(type.designator, |
| 366 |
PARTITION_ROOT, |
| 367 |
PARTITION_USR, |
| 368 |
PARTITION_HOME, |
| 369 |
PARTITION_SRV, |
| 370 |
PARTITION_ESP, |
| 371 |
PARTITION_XBOOTLDR, |
| 372 |
PARTITION_TMP, |
| 373 |
PARTITION_VAR); |
| 374 |
} |
| 375 |
|
| 376 |
bool gpt_header_has_signature(const GptHeader *p) { |
| 377 |
assert(p); |
| 378 |
|
| 379 |
if (memcmp(p->signature, (const char[8]) { 'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T' }, 8) != 0) |
| 380 |
return false; |
| 381 |
|
| 382 |
if (le32toh(p->revision) != UINT32_C(0x00010000)) /* the only known revision of the spec: 1.0 */ |
| 383 |
return false; |
| 384 |
|
| 385 |
if (le32toh(p->header_size) < sizeof(GptHeader)) |
| 386 |
return false; |
| 387 |
|
| 388 |
if (le32toh(p->header_size) > 4096) /* larger than a sector? something is off… */ |
| 389 |
return false; |
| 390 |
|
| 391 |
if (le64toh(p->my_lba) != 1) /* this sector must claim to be at sector offset 1 */ |
| 392 |
return false; |
| 393 |
|
| 394 |
return true; |
| 395 |
} |
| 396 |
|
| 397 |
ssize_t gpt_probe( |
| 398 |
int fd, |
| 399 |
GptHeader *ret_header, |
| 400 |
void **ret_entries, |
| 401 |
uint32_t *ret_n_entries, |
| 402 |
uint32_t *ret_entry_size) { |
| 403 |
|
| 404 |
assert(fd >= 0); |
| 405 |
|
| 406 |
/* Disk images might be for 512B or for 4096 sector sizes, let's try to auto-detect that by searching |
| 407 |
* for the GPT headers at the relevant byte offsets. */ |
| 408 |
|
| 409 |
assert_cc(sizeof(GptHeader) == 92); |
| 410 |
|
| 411 |
/* We expect a sector size in the range 512…4096. The GPT header is located in the second |
| 412 |
* sector. Hence it could be at byte 512 at the earliest, and at byte 4096 at the latest. And we must |
| 413 |
* read with granularity of the largest sector size we care about. Which means 8K. */ |
| 414 |
uint8_t sectors[2 * 4096]; |
| 415 |
|
| 416 |
ssize_t n = pread(fd, sectors, sizeof(sectors), 0); |
| 417 |
if (n < 0) |
| 418 |
return -errno; |
| 419 |
if ((size_t) n < sizeof(sectors)) |
| 420 |
return 0; /* too short */ |
| 421 |
|
| 422 |
/* Let's see if we find the GPT partition header with various expected sector sizes */ |
| 423 |
uint32_t found = 0; |
| 424 |
for (uint32_t sz = 512; sz <= 4096; sz <<= 1) { |
| 425 |
const GptHeader *p = (const GptHeader *) (sectors + sz); |
| 426 |
|
| 427 |
if (!gpt_header_has_signature(p)) |
| 428 |
continue; |
| 429 |
|
| 430 |
if (found != 0) |
| 431 |
return -ENOTUNIQ; |
| 432 |
|
| 433 |
found = sz; |
| 434 |
} |
| 435 |
|
| 436 |
if (found == 0) |
| 437 |
return 0; |
| 438 |
|
| 439 |
const GptHeader *h = (const GptHeader *) (sectors + found); |
| 440 |
|
| 441 |
uint32_t entry_sz = le32toh(h->size_of_partition_entry); |
| 442 |
uint32_t entry_count = le32toh(h->number_of_partition_entries); |
| 443 |
|
| 444 |
if (ret_entries) { |
| 445 |
uint64_t entry_lba = le64toh(h->partition_entry_lba); |
| 446 |
if (entry_lba > (uint64_t) INT64_MAX / found) |
| 447 |
return -EBADMSG; |
| 448 |
|
| 449 |
uint64_t entry_offset = entry_lba * found; |
| 450 |
|
| 451 |
if (entry_sz < sizeof(GptPartitionEntry) || entry_count == 0 || entry_count > 1024) |
| 452 |
return -EBADMSG; |
| 453 |
if (entry_sz > SIZE_MAX / entry_count) |
| 454 |
return -EBADMSG; |
| 455 |
|
| 456 |
size_t entries_size = (size_t) entry_sz * entry_count; |
| 457 |
_cleanup_free_ void *entries = malloc(entries_size); |
| 458 |
if (!entries) |
| 459 |
return -ENOMEM; |
| 460 |
|
| 461 |
n = pread(fd, entries, entries_size, entry_offset); |
| 462 |
if (n < 0) |
| 463 |
return -errno; |
| 464 |
if ((size_t) n < entries_size) |
| 465 |
return -EBADMSG; |
| 466 |
|
| 467 |
*ret_entries = TAKE_PTR(entries); |
| 468 |
} |
| 469 |
|
| 470 |
if (ret_header) |
| 471 |
*ret_header = *h; |
| 472 |
if (ret_n_entries) |
| 473 |
*ret_n_entries = entry_count; |
| 474 |
if (ret_entry_size) |
| 475 |
*ret_entry_size = entry_sz; |
| 476 |
|
| 477 |
return found; /* sector size */ |
| 478 |
} |