| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "device-path-util.h" |
| 4 |
#include "efi-log.h" |
| 5 |
#include "part-discovery.h" |
| 6 |
#include "proto/block-io.h" |
| 7 |
#include "proto/device-path.h" |
| 8 |
#include "proto/disk-io.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "util.h" |
| 11 |
|
| 12 |
typedef struct { |
| 13 |
EFI_GUID PartitionTypeGUID; |
| 14 |
EFI_GUID UniquePartitionGUID; |
| 15 |
EFI_LBA StartingLBA; |
| 16 |
EFI_LBA EndingLBA; |
| 17 |
uint64_t Attributes; |
| 18 |
char16_t PartitionName[36]; |
| 19 |
} EFI_PARTITION_ENTRY; |
| 20 |
|
| 21 |
typedef struct { |
| 22 |
EFI_TABLE_HEADER Header; |
| 23 |
EFI_LBA MyLBA; |
| 24 |
EFI_LBA AlternateLBA; |
| 25 |
EFI_LBA FirstUsableLBA; |
| 26 |
EFI_LBA LastUsableLBA; |
| 27 |
EFI_GUID DiskGUID; |
| 28 |
EFI_LBA PartitionEntryLBA; |
| 29 |
uint32_t NumberOfPartitionEntries; |
| 30 |
uint32_t SizeOfPartitionEntry; |
| 31 |
uint32_t PartitionEntryArrayCRC32; |
| 32 |
uint8_t _pad[420]; |
| 33 |
} _packed_ GptHeader; |
| 34 |
assert_cc(sizeof(GptHeader) == 512); |
| 35 |
|
| 36 |
static bool verify_gpt(/* const */ GptHeader *h, EFI_LBA lba_expected) { |
| 37 |
uint32_t crc32, crc32_saved; |
| 38 |
EFI_STATUS err; |
| 39 |
|
| 40 |
assert(h); |
| 41 |
|
| 42 |
/* Some superficial validation of the GPT header */ |
| 43 |
if (memcmp(&h->Header.Signature, "EFI PART", sizeof(h->Header.Signature)) != 0) |
| 44 |
return false; |
| 45 |
|
| 46 |
if (h->Header.HeaderSize < 92 || h->Header.HeaderSize > 512) |
| 47 |
return false; |
| 48 |
|
| 49 |
if (h->Header.Revision != 0x00010000U) |
| 50 |
return false; |
| 51 |
|
| 52 |
/* Calculate CRC check */ |
| 53 |
crc32_saved = h->Header.CRC32; |
| 54 |
h->Header.CRC32 = 0; |
| 55 |
err = BS->CalculateCrc32(h, h->Header.HeaderSize, &crc32); |
| 56 |
h->Header.CRC32 = crc32_saved; |
| 57 |
if (err != EFI_SUCCESS || crc32 != crc32_saved) |
| 58 |
return false; |
| 59 |
|
| 60 |
if (h->MyLBA != lba_expected) |
| 61 |
return false; |
| 62 |
|
| 63 |
if ((h->SizeOfPartitionEntry % sizeof(EFI_PARTITION_ENTRY)) != 0) |
| 64 |
return false; |
| 65 |
|
| 66 |
if (h->NumberOfPartitionEntries <= 0 || h->NumberOfPartitionEntries > 1024) |
| 67 |
return false; |
| 68 |
|
| 69 |
/* overflow check */ |
| 70 |
if (h->SizeOfPartitionEntry > SIZE_MAX / h->NumberOfPartitionEntries) |
| 71 |
return false; |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
static EFI_STATUS read_gpt_entries( |
| 77 |
EFI_DISK_IO_PROTOCOL *disk_io, |
| 78 |
uint32_t media_id, |
| 79 |
uint32_t block_size, |
| 80 |
EFI_LBA lba, |
| 81 |
EFI_LBA *reterr_backup_lba, /* May be changed even on error! */ |
| 82 |
GptHeader *ret_gpt, |
| 83 |
void **ret_entries) { |
| 84 |
|
| 85 |
GptHeader gpt; |
| 86 |
EFI_STATUS err; |
| 87 |
uint32_t crc32; |
| 88 |
size_t size; |
| 89 |
|
| 90 |
assert(disk_io); |
| 91 |
assert(ret_gpt); |
| 92 |
assert(ret_entries); |
| 93 |
|
| 94 |
uint64_t offset; |
| 95 |
if (!MUL_SAFE(&offset, lba, block_size)) |
| 96 |
return log_debug_status( |
| 97 |
EFI_INVALID_PARAMETER, |
| 98 |
"LBA %" PRIu64 " * block size %" PRIu32 " overflow: %m", |
| 99 |
lba, |
| 100 |
block_size); |
| 101 |
|
| 102 |
err = disk_io->ReadDisk(disk_io, media_id, offset, sizeof(gpt), &gpt); |
| 103 |
if (err != EFI_SUCCESS) |
| 104 |
return log_debug_status(err, "Failed to read GPT header at LBA %" PRIu64 ": %m", lba); |
| 105 |
|
| 106 |
/* Expose backup LBA even if the rest of the header is corrupt, so the caller can |
| 107 |
* try the backup GPT. */ |
| 108 |
if (reterr_backup_lba) |
| 109 |
*reterr_backup_lba = gpt.AlternateLBA; |
| 110 |
|
| 111 |
if (!verify_gpt(&gpt, lba)) |
| 112 |
return log_debug_status(EFI_NOT_FOUND, "GPT header at LBA %" PRIu64 " is not valid: %m", lba); |
| 113 |
|
| 114 |
size = (size_t) gpt.SizeOfPartitionEntry * (size_t) gpt.NumberOfPartitionEntries; |
| 115 |
if (size == SIZE_MAX) /* overflow check */ |
| 116 |
return log_debug_status(EFI_OUT_OF_RESOURCES, "GPT partition entries size overflow: %m"); |
| 117 |
|
| 118 |
_cleanup_free_ void *entries = xmalloc(size); |
| 119 |
|
| 120 |
if (!MUL_SAFE(&offset, gpt.PartitionEntryLBA, block_size)) |
| 121 |
return log_debug_status( |
| 122 |
EFI_INVALID_PARAMETER, |
| 123 |
"Partition entry LBA %" PRIu64 " * block size %" PRIu32 " overflow: %m", |
| 124 |
gpt.PartitionEntryLBA, |
| 125 |
block_size); |
| 126 |
|
| 127 |
err = disk_io->ReadDisk(disk_io, media_id, offset, size, entries); |
| 128 |
if (err != EFI_SUCCESS) |
| 129 |
return log_debug_status(err, "Failed to read GPT partition entries at LBA %" PRIu64 ": %m", gpt.PartitionEntryLBA); |
| 130 |
|
| 131 |
err = BS->CalculateCrc32(entries, size, &crc32); |
| 132 |
if (err != EFI_SUCCESS) |
| 133 |
return log_debug_status(err, "Failed to calculate CRC32 of GPT partition entries: %m"); |
| 134 |
if (crc32 != gpt.PartitionEntryArrayCRC32) |
| 135 |
return log_debug_status( |
| 136 |
EFI_CRC_ERROR, |
| 137 |
"GPT partition entries CRC32 mismatch (got 0x%08" PRIx32 ", expected 0x%08" PRIx32 "): %m", |
| 138 |
crc32, |
| 139 |
gpt.PartitionEntryArrayCRC32); |
| 140 |
|
| 141 |
*ret_gpt = gpt; |
| 142 |
*ret_entries = TAKE_PTR(entries); |
| 143 |
return EFI_SUCCESS; |
| 144 |
} |
| 145 |
|
| 146 |
static EFI_STATUS try_gpt( |
| 147 |
const EFI_GUID *type, |
| 148 |
EFI_DISK_IO_PROTOCOL *disk_io, |
| 149 |
uint32_t media_id, |
| 150 |
uint32_t block_size, |
| 151 |
EFI_LBA lba, |
| 152 |
EFI_LBA *reterr_backup_lba, /* May be changed even on error! */ |
| 153 |
HARDDRIVE_DEVICE_PATH *ret_hd) { |
| 154 |
|
| 155 |
GptHeader gpt; |
| 156 |
_cleanup_free_ void *entries = NULL; |
| 157 |
EFI_STATUS err; |
| 158 |
|
| 159 |
assert(ret_hd); |
| 160 |
|
| 161 |
err = read_gpt_entries(disk_io, media_id, block_size, lba, reterr_backup_lba, &gpt, &entries); |
| 162 |
if (err != EFI_SUCCESS) |
| 163 |
return err; |
| 164 |
|
| 165 |
for (size_t i = 0; i < gpt.NumberOfPartitionEntries; i++) { |
| 166 |
EFI_PARTITION_ENTRY *entry = |
| 167 |
(EFI_PARTITION_ENTRY *) ((uint8_t *) entries + gpt.SizeOfPartitionEntry * i); |
| 168 |
|
| 169 |
if (!efi_guid_equal(&entry->PartitionTypeGUID, type)) |
| 170 |
continue; |
| 171 |
|
| 172 |
if (entry->EndingLBA < entry->StartingLBA) /* Bogus? */ |
| 173 |
continue; |
| 174 |
|
| 175 |
*ret_hd = (HARDDRIVE_DEVICE_PATH) { |
| 176 |
.Header = { |
| 177 |
.Type = MEDIA_DEVICE_PATH, |
| 178 |
.SubType = MEDIA_HARDDRIVE_DP, |
| 179 |
.Length = sizeof(HARDDRIVE_DEVICE_PATH), |
| 180 |
}, |
| 181 |
.PartitionNumber = i + 1, |
| 182 |
.PartitionStart = entry->StartingLBA, |
| 183 |
.PartitionSize = entry->EndingLBA - entry->StartingLBA + 1, |
| 184 |
.MBRType = MBR_TYPE_EFI_PARTITION_TABLE_HEADER, |
| 185 |
.SignatureType = SIGNATURE_TYPE_GUID, |
| 186 |
}; |
| 187 |
memcpy(ret_hd->Signature, &entry->UniquePartitionGUID, sizeof(ret_hd->Signature)); |
| 188 |
|
| 189 |
return EFI_SUCCESS; |
| 190 |
} |
| 191 |
|
| 192 |
/* This GPT was fully valid, but we didn't find what we are looking for. This |
| 193 |
* means there's no reason to check the second copy of the GPT header */ |
| 194 |
return EFI_NOT_FOUND; |
| 195 |
} |
| 196 |
|
| 197 |
static EFI_STATUS find_device(const EFI_GUID *type, EFI_HANDLE *device, EFI_DEVICE_PATH **ret_device_path) { |
| 198 |
EFI_STATUS err; |
| 199 |
|
| 200 |
assert(device); |
| 201 |
assert(ret_device_path); |
| 202 |
|
| 203 |
EFI_DEVICE_PATH *partition_path; |
| 204 |
err = BS->HandleProtocol(device, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &partition_path); |
| 205 |
if (err != EFI_SUCCESS) |
| 206 |
return log_debug_status(err, "Failed to get device path: %m"); |
| 207 |
|
| 208 |
/* Find the (last) partition node itself. */ |
| 209 |
EFI_DEVICE_PATH *part_node = NULL; |
| 210 |
for (EFI_DEVICE_PATH *node = partition_path; !device_path_is_end(node); |
| 211 |
node = device_path_next_node(node)) { |
| 212 |
if (node->Type != MEDIA_DEVICE_PATH || node->SubType != MEDIA_HARDDRIVE_DP) |
| 213 |
continue; |
| 214 |
|
| 215 |
part_node = node; |
| 216 |
} |
| 217 |
|
| 218 |
if (!part_node) { |
| 219 |
log_debug("No hard drive device path node found."); |
| 220 |
return EFI_NOT_FOUND; |
| 221 |
} |
| 222 |
|
| 223 |
/* Chop off the partition part, leaving us with the full path to the disk itself. */ |
| 224 |
_cleanup_free_ EFI_DEVICE_PATH *disk_path = NULL; |
| 225 |
EFI_DEVICE_PATH *p = disk_path = device_path_replace_node(partition_path, part_node, NULL); |
| 226 |
|
| 227 |
EFI_HANDLE disk_handle; |
| 228 |
EFI_BLOCK_IO_PROTOCOL *block_io; |
| 229 |
err = BS->LocateDevicePath(MAKE_GUID_PTR(EFI_BLOCK_IO_PROTOCOL), &p, &disk_handle); |
| 230 |
if (err != EFI_SUCCESS) |
| 231 |
return log_debug_status(err, "Failed to locate disk device: %m"); |
| 232 |
|
| 233 |
/* The drivers for other partitions on this drive may not be initialized on fastboot firmware, so we |
| 234 |
* have to ask the firmware to do just that. */ |
| 235 |
(void) BS->ConnectController(disk_handle, NULL, NULL, true); |
| 236 |
|
| 237 |
err = BS->HandleProtocol(disk_handle, MAKE_GUID_PTR(EFI_BLOCK_IO_PROTOCOL), (void **) &block_io); |
| 238 |
if (err != EFI_SUCCESS) |
| 239 |
return log_debug_status(err, "Failed to get block I/O protocol: %m"); |
| 240 |
|
| 241 |
/* Filter out some block devices early. (We only care about block devices that aren't |
| 242 |
* partitions themselves — we look for GPT partition tables to parse after all —, and only |
| 243 |
* those which contain a medium and have at least 2 blocks.) */ |
| 244 |
if (block_io->Media->LogicalPartition || |
| 245 |
!block_io->Media->MediaPresent || |
| 246 |
block_io->Media->LastBlock <= 1 || |
| 247 |
block_io->Media->BlockSize < 512 || block_io->Media->BlockSize > 4096) |
| 248 |
return EFI_NOT_FOUND; |
| 249 |
|
| 250 |
EFI_DISK_IO_PROTOCOL *disk_io; |
| 251 |
err = BS->HandleProtocol(disk_handle, MAKE_GUID_PTR(EFI_DISK_IO_PROTOCOL), (void **) &disk_io); |
| 252 |
if (err != EFI_SUCCESS) |
| 253 |
return log_debug_status(err, "Failed to get disk I/O protocol: %m"); |
| 254 |
|
| 255 |
/* Try several copies of the GPT header, in case one is corrupted */ |
| 256 |
EFI_LBA backup_lba = 0; |
| 257 |
for (size_t nr = 0; nr < 3; nr++) { |
| 258 |
EFI_LBA lba; |
| 259 |
|
| 260 |
/* Read the first copy at LBA 1 and then try the backup GPT header pointed |
| 261 |
* to by the first header if that one was corrupted. As a last resort, |
| 262 |
* try the very last LBA of this block device. */ |
| 263 |
if (nr == 0) |
| 264 |
lba = 1; |
| 265 |
else if (nr == 1 && backup_lba != 0) |
| 266 |
lba = backup_lba; |
| 267 |
else if (nr == 2 && backup_lba != block_io->Media->LastBlock) |
| 268 |
lba = block_io->Media->LastBlock; |
| 269 |
else |
| 270 |
continue; |
| 271 |
|
| 272 |
HARDDRIVE_DEVICE_PATH hd; |
| 273 |
err = try_gpt(type, disk_io, block_io->Media->MediaId, block_io->Media->BlockSize, lba, |
| 274 |
nr == 0 ? &backup_lba : NULL, /* Only get backup LBA location from first GPT header. */ |
| 275 |
&hd); |
| 276 |
if (err != EFI_SUCCESS) { |
| 277 |
/* GPT was valid but no XBOOT loader partition found. */ |
| 278 |
if (err == EFI_NOT_FOUND) |
| 279 |
break; |
| 280 |
/* Bad GPT, try next one. */ |
| 281 |
continue; |
| 282 |
} |
| 283 |
|
| 284 |
/* Patch in the data we found */ |
| 285 |
*ret_device_path = device_path_replace_node(partition_path, part_node, &hd.Header); |
| 286 |
return EFI_SUCCESS; |
| 287 |
} |
| 288 |
|
| 289 |
/* No xbootloader partition found */ |
| 290 |
return EFI_NOT_FOUND; |
| 291 |
} |
| 292 |
|
| 293 |
EFI_STATUS partition_open(const EFI_GUID *type, EFI_HANDLE *device, EFI_HANDLE *ret_device, |
| 294 |
EFI_FILE **ret_root_dir) { |
| 295 |
_cleanup_free_ EFI_DEVICE_PATH *partition_path = NULL; |
| 296 |
EFI_HANDLE new_device; |
| 297 |
EFI_FILE *root_dir; |
| 298 |
EFI_STATUS err; |
| 299 |
|
| 300 |
assert(type); |
| 301 |
assert(device); |
| 302 |
assert(ret_root_dir); |
| 303 |
|
| 304 |
err = find_device(type, device, &partition_path); |
| 305 |
if (err != EFI_SUCCESS) |
| 306 |
return err; |
| 307 |
|
| 308 |
EFI_DEVICE_PATH *dp = partition_path; |
| 309 |
err = BS->LocateDevicePath(MAKE_GUID_PTR(EFI_BLOCK_IO_PROTOCOL), &dp, &new_device); |
| 310 |
if (err != EFI_SUCCESS) |
| 311 |
return err; |
| 312 |
|
| 313 |
err = open_volume(new_device, &root_dir); |
| 314 |
if (err != EFI_SUCCESS) |
| 315 |
return err; |
| 316 |
|
| 317 |
if (ret_device) |
| 318 |
*ret_device = new_device; |
| 319 |
*ret_root_dir = root_dir; |
| 320 |
return EFI_SUCCESS; |
| 321 |
} |
| 322 |
|
| 323 |
static char16_t* disk_get_part_uuid_eltorito(const EFI_DEVICE_PATH *dp) { |
| 324 |
EFI_STATUS err; |
| 325 |
|
| 326 |
assert(dp); |
| 327 |
|
| 328 |
/* When booting via El Torito, the device path contains a CDROM node instead of a HARDDRIVE |
| 329 |
* node (UEFI specification §10.3.5.2). The CDROM node doesn't carry a partition UUID, so we |
| 330 |
* need to read the GPT from the underlying disk to find it. Per §13.3.2, El Torito partition |
| 331 |
* discovery applies to any block device, not just optical media (e.g. an ISO image dd'd to a |
| 332 |
* USB stick). */ |
| 333 |
|
| 334 |
const CDROM_DEVICE_PATH *cdrom = NULL; |
| 335 |
for (const EFI_DEVICE_PATH *node = dp; !device_path_is_end(node); node = device_path_next_node(node)) |
| 336 |
if (node->Type == MEDIA_DEVICE_PATH && node->SubType == MEDIA_CDROM_DP) |
| 337 |
cdrom = (const CDROM_DEVICE_PATH *) node; |
| 338 |
if (!cdrom) { |
| 339 |
log_debug("No El Torito device path node found."); |
| 340 |
return NULL; |
| 341 |
} |
| 342 |
|
| 343 |
/* Chop off the CDROM node to get the whole-disk device path */ |
| 344 |
_cleanup_free_ EFI_DEVICE_PATH *disk_path = device_path_replace_node(dp, &cdrom->Header, /* new_node= */ NULL); |
| 345 |
|
| 346 |
EFI_DEVICE_PATH *remaining = disk_path; |
| 347 |
EFI_HANDLE disk_handle; |
| 348 |
err = BS->LocateDevicePath(MAKE_GUID_PTR(EFI_BLOCK_IO_PROTOCOL), &remaining, &disk_handle); |
| 349 |
if (err != EFI_SUCCESS) { |
| 350 |
log_debug_status(err, "Failed to locate disk device for El Torito boot: %m"); |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
|
| 354 |
(void) BS->ConnectController(disk_handle, /* DriverImageHandle= */ NULL, /* RemainingDevicePath= */ NULL, /* Recursive= */ true); |
| 355 |
|
| 356 |
EFI_BLOCK_IO_PROTOCOL *block_io; |
| 357 |
err = BS->HandleProtocol(disk_handle, MAKE_GUID_PTR(EFI_BLOCK_IO_PROTOCOL), (void **) &block_io); |
| 358 |
if (err != EFI_SUCCESS) { |
| 359 |
log_debug_status(err, "Failed to get block I/O protocol for El Torito disk: %m"); |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
|
| 363 |
if (block_io->Media->LogicalPartition || !block_io->Media->MediaPresent || |
| 364 |
block_io->Media->LastBlock <= 1) { |
| 365 |
log_debug("El Torito disk has unsuitable media (partition=%ls, present=%ls, lastblock=%" PRIu64 ").", |
| 366 |
yes_no(block_io->Media->LogicalPartition), |
| 367 |
yes_no(block_io->Media->MediaPresent), |
| 368 |
(uint64_t) block_io->Media->LastBlock); |
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
uint32_t iso9660_block_size = block_io->Media->BlockSize; |
| 373 |
if (iso9660_block_size < 512 || iso9660_block_size > 4096 || !ISPOWEROF2(iso9660_block_size)) { |
| 374 |
log_debug("Unexpected El Torito block size %" PRIu32 ", skipping.", iso9660_block_size); |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
EFI_DISK_IO_PROTOCOL *disk_io; |
| 379 |
err = BS->HandleProtocol(disk_handle, MAKE_GUID_PTR(EFI_DISK_IO_PROTOCOL), (void **) &disk_io); |
| 380 |
if (err != EFI_SUCCESS) { |
| 381 |
log_debug_status(err, "Failed to get disk I/O protocol for El Torito disk: %m"); |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
uint32_t media_id = block_io->Media->MediaId; |
| 386 |
|
| 387 |
/* Probe for the GPT header at multiple possible sector sizes (512, 1024, 2048, 4096). |
| 388 |
* The GPT header is at LBA 1, i.e. byte offset == sector_size. On El Torito media, the GPT |
| 389 |
* may use a different sector size than the media's block size (e.g. 512-byte GPT sectors |
| 390 |
* on 2048-byte blocks), so we try all possibilities. If the primary GPT header is corrupt |
| 391 |
* but contains a valid backup LBA, fall back to the backup header. */ |
| 392 |
uint32_t gpt_sector_size = 0; |
| 393 |
GptHeader gpt; |
| 394 |
_cleanup_free_ void *entries = NULL; |
| 395 |
for (uint32_t ss = 512; ss <= 4096; ss <<= 1) { |
| 396 |
EFI_LBA backup_lba = 0; |
| 397 |
|
| 398 |
err = read_gpt_entries(disk_io, media_id, ss, /* lba= */ 1, &backup_lba, &gpt, &entries); |
| 399 |
if (err == EFI_SUCCESS) { |
| 400 |
gpt_sector_size = ss; |
| 401 |
break; |
| 402 |
} |
| 403 |
if (err != EFI_NOT_FOUND) |
| 404 |
log_debug_status(err, "Failed to read primary GPT header at sector size %"PRIu32", ignoring: %m", ss); |
| 405 |
|
| 406 |
if (backup_lba != 0) { |
| 407 |
err = read_gpt_entries(disk_io, media_id, ss, backup_lba, /* reterr_backup_lba= */ NULL, &gpt, &entries); |
| 408 |
if (err == EFI_SUCCESS) { |
| 409 |
gpt_sector_size = ss; |
| 410 |
break; |
| 411 |
} |
| 412 |
if (err != EFI_NOT_FOUND) |
| 413 |
log_debug_status(err, "Failed to read backup GPT header at sector size %"PRIu32", ignoring: %m", ss); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (gpt_sector_size == 0) { |
| 418 |
log_debug("No valid GPT found on El Torito disk at any sector size."); |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
log_debug("Found GPT on El Torito disk with sector size %" PRIu32 ", %" PRIu32 " partition entries.", |
| 423 |
gpt_sector_size, gpt.NumberOfPartitionEntries); |
| 424 |
|
| 425 |
/* Find the partition whose byte offset matches the El Torito PartitionStart. |
| 426 |
* El Torito PartitionStart is in media iso9660_block_size units, GPT StartingLBA is in gpt_sector_size units. */ |
| 427 |
uint64_t cdrom_start; |
| 428 |
if (!MUL_SAFE(&cdrom_start, cdrom->PartitionStart, iso9660_block_size)) { |
| 429 |
log_debug("El Torito start offset overflow."); |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
|
| 433 |
for (size_t i = 0; i < gpt.NumberOfPartitionEntries; i++) { |
| 434 |
const EFI_PARTITION_ENTRY *entry = |
| 435 |
(const EFI_PARTITION_ENTRY *) ((const uint8_t *) entries + gpt.SizeOfPartitionEntry * i); |
| 436 |
|
| 437 |
if (!efi_guid_equal(&entry->PartitionTypeGUID, &(const EFI_GUID) ESP_GUID)) |
| 438 |
continue; |
| 439 |
|
| 440 |
uint64_t entry_start; |
| 441 |
if (MUL_SAFE(&entry_start, entry->StartingLBA, gpt_sector_size) && |
| 442 |
entry_start == cdrom_start) |
| 443 |
return xasprintf(GUID_FORMAT_STR, GUID_FORMAT_VAL(entry->UniquePartitionGUID)); |
| 444 |
} |
| 445 |
|
| 446 |
log_debug("No ESP partition matches El Torito start offset %" PRIu64 " (block size %" PRIu32 ").", |
| 447 |
cdrom->PartitionStart, iso9660_block_size); |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
char16_t *disk_get_part_uuid(EFI_HANDLE *handle) { |
| 452 |
EFI_STATUS err; |
| 453 |
EFI_DEVICE_PATH *dp; |
| 454 |
|
| 455 |
/* export the device path this image is started from */ |
| 456 |
|
| 457 |
if (!handle) |
| 458 |
return NULL; |
| 459 |
|
| 460 |
err = BS->HandleProtocol(handle, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp); |
| 461 |
if (err != EFI_SUCCESS) |
| 462 |
return NULL; |
| 463 |
|
| 464 |
for (EFI_DEVICE_PATH *node = dp; !device_path_is_end(node); node = device_path_next_node(node)) { |
| 465 |
if (node->Type != MEDIA_DEVICE_PATH || node->SubType != MEDIA_HARDDRIVE_DP) |
| 466 |
continue; |
| 467 |
|
| 468 |
HARDDRIVE_DEVICE_PATH *hd = (HARDDRIVE_DEVICE_PATH *) node; |
| 469 |
if (hd->SignatureType != SIGNATURE_TYPE_GUID) |
| 470 |
continue; |
| 471 |
|
| 472 |
return xasprintf(GUID_FORMAT_STR, GUID_FORMAT_VAL(hd->SignatureGuid)); |
| 473 |
} |
| 474 |
|
| 475 |
/* No GPT partition node found — try El Torito device path as fallback */ |
| 476 |
return disk_get_part_uuid_eltorito(dp); |
| 477 |
} |