#include "dos.h"
#define GPT_PRIMARY_LBA 1
-#define GPT_BLOCK_SIZE 512
/* Signature - “EFI PART” */
#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
uint32_t sizeof_partition_entry;
uint32_t partition_entry_array_crc32;
- uint8_t reserved2[GPT_BLOCK_SIZE - 92];
+ /*
+ * The rest of the block is reserved by UEFI and must be zero. EFI
+ * standard handles this by:
+ *
+ * uint8_t reserved2[ BLKSSZGET - 92 ];
+ *
+ * This definition is useless in practice. It is necessary to read
+ * whole block from the device rather than sizeof(struct gpt_header)
+ * only.
+ */
} __attribute__ ((packed));
/*** not used
uint64_t lba, size_t bytes)
{
return blkid_probe_get_buffer(pr,
- GPT_BLOCK_SIZE * lba, bytes);
+ blkid_probe_get_sectorsize(pr) * lba, bytes);
}
static inline int guidcmp(efi_guid_t left, efi_guid_t right)
static int last_lba(blkid_probe pr, uint64_t *lba)
{
blkid_loff_t sz = blkid_probe_get_size(pr);
- if (sz < GPT_BLOCK_SIZE)
+ if (sz < blkid_probe_get_sectorsize(pr))
return -1;
*lba = (sz >> 9) - 1;
uint32_t crc, orgcrc;
uint64_t lu, fu;
size_t esz;
+ uint32_t hsz, ssz;
+
+ ssz = blkid_probe_get_sectorsize(pr);
- h = (struct gpt_header *) get_lba_buffer(pr, lba, sizeof(*h));
+ /* whole sector is allocated for GPT header */
+ h = (struct gpt_header *) get_lba_buffer(pr, lba, ssz);
if (!h)
return NULL;
if (le64_to_cpu(h->signature) != GPT_HEADER_SIGNATURE)
return NULL;
+ hsz = le32_to_cpu(h->header_size);
+
+ /* EFI: The HeaderSize must be greater than 92 and must be less
+ * than or equal to the logical block size.
+ */
+ if (hsz > ssz || hsz < sizeof(*h))
+ return NULL;
+
/* Header has to be verified when header_crc32 is zero */
orgcrc = le32_to_cpu(h->header_crc32);
h->header_crc32 = 0;
- crc = count_crc32((unsigned char *) h, le32_to_cpu(h->header_size));
+ crc = count_crc32((unsigned char *) h, hsz);
if (crc != orgcrc) {
DBG(DEBUG_LOWPROBE, printf("GPT header corrupted\n"));
return NULL;
return NULL;
}
- /* The header seems valid, save it */
+ /* The header seems valid, save it
+ * (we don't care about zeros in hdr->reserved2 area) */
memcpy(hdr, h, sizeof(*h));
h = hdr;
* skip the legacy MBR. We follows this behavior and MBR is optional.
* See is_valid_pmbr().
*
- * It would be possible to check for "EFI PART" at begin of the disk,
- * but the primary GPT is not required (in force mode).
- *
* It means we have to always call probe_gpt_pt().
*/
.magics = BLKID_NONE_MAGIC