|
| 1 | +/* gpt.h |
| 2 | + * |
| 3 | + * Generic GPT (GUID Partition Table) parsing support. |
| 4 | + * |
| 5 | + * Copyright (C) 2025 wolfSSL Inc. |
| 6 | + * |
| 7 | + * This file is part of wolfBoot. |
| 8 | + * |
| 9 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * wolfBoot is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef GPT_H |
| 25 | +#define GPT_H |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +/* GPT Constants */ |
| 30 | +#define GPT_SECTOR_SIZE 0x200 |
| 31 | +#define GPT_SIGNATURE 0x5452415020494645ULL /* "EFI PART" */ |
| 32 | +#define GPT_PTYPE_PROTECTIVE 0xEE |
| 33 | +#define GPT_PART_NAME_SIZE 36 |
| 34 | +#define GPT_MBR_ENTRY_START 0x01BE |
| 35 | +#define GPT_MBR_BOOTSIG_OFFSET 0x01FE |
| 36 | +#define GPT_MBR_BOOTSIG_VALUE 0xAA55 |
| 37 | + |
| 38 | +/** |
| 39 | + * @brief MBR partition table entry structure. |
| 40 | + * |
| 41 | + * This packed structure defines the layout of an MBR partition table entry |
| 42 | + * used to identify GPT partitions (protective MBR). |
| 43 | + */ |
| 44 | +struct __attribute__((packed)) gpt_mbr_part_entry { |
| 45 | + uint8_t stat; |
| 46 | + uint8_t chs_first[3]; |
| 47 | + uint8_t ptype; |
| 48 | + uint8_t chs_last[3]; |
| 49 | + uint32_t lba_first; |
| 50 | + uint32_t lba_size; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief GPT (GUID Partition Table) header structure. |
| 55 | + */ |
| 56 | +struct __attribute__((packed)) guid_ptable { |
| 57 | + uint64_t signature; |
| 58 | + uint32_t revision; |
| 59 | + uint32_t hdr_size; |
| 60 | + uint32_t hdr_crc32; |
| 61 | + uint32_t res0; |
| 62 | + uint64_t main_lba; |
| 63 | + uint64_t backup_lba; |
| 64 | + uint64_t first_usable; |
| 65 | + uint64_t last_usable; |
| 66 | + uint64_t disk_guid[2]; |
| 67 | + uint64_t start_array; |
| 68 | + uint32_t n_part; |
| 69 | + uint32_t array_sz; |
| 70 | + uint32_t part_crc; |
| 71 | + uint8_t res1[GPT_SECTOR_SIZE - 0x5C]; |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * @brief GPT partition entry structure. |
| 76 | + * |
| 77 | + * This packed structure defines the layout of a GPT partition entry |
| 78 | + * used to describe individual partitions on the disk. |
| 79 | + */ |
| 80 | +struct __attribute__((packed)) gpt_part_entry { |
| 81 | + uint64_t type[2]; |
| 82 | + uint64_t uuid[2]; |
| 83 | + uint64_t first; |
| 84 | + uint64_t last; |
| 85 | + uint64_t flags; |
| 86 | + uint16_t name[GPT_PART_NAME_SIZE]; |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * @brief Parsed partition information. |
| 91 | + * |
| 92 | + * This structure holds parsed information about a partition extracted |
| 93 | + * from a GPT partition entry. |
| 94 | + */ |
| 95 | +struct gpt_part_info { |
| 96 | + uint64_t start; /* Start offset in bytes */ |
| 97 | + uint64_t end; /* End offset in bytes */ |
| 98 | + uint16_t name[GPT_PART_NAME_SIZE]; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * @brief Check MBR for protective GPT partition entry. |
| 103 | + * |
| 104 | + * Scans the MBR sector for a protective GPT partition entry (type 0xEE) |
| 105 | + * and validates the boot signature. |
| 106 | + * |
| 107 | + * @param[in] mbr_sector Pointer to 512-byte MBR sector data. |
| 108 | + * @param[out] gpt_lba If not NULL, receives the LBA of the GPT header. |
| 109 | + * |
| 110 | + * @return 0 on success (valid protective MBR found), -1 on error. |
| 111 | + */ |
| 112 | +int gpt_check_mbr_protective(const uint8_t *mbr_sector, uint32_t *gpt_lba); |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief Parse and validate a GPT header. |
| 116 | + * |
| 117 | + * Validates the GPT signature and copies header data to the output structure. |
| 118 | + * |
| 119 | + * @param[in] sector Pointer to 512-byte GPT header sector data. |
| 120 | + * @param[out] hdr Pointer to structure to receive parsed header. |
| 121 | + * |
| 122 | + * @return 0 on success (valid GPT header), -1 on error. |
| 123 | + */ |
| 124 | +int gpt_parse_header(const uint8_t *sector, struct guid_ptable *hdr); |
| 125 | + |
| 126 | +/** |
| 127 | + * @brief Parse a GPT partition entry. |
| 128 | + * |
| 129 | + * Parses a single partition entry and extracts partition information. |
| 130 | + * Returns success only if the partition entry is valid (non-zero type GUID). |
| 131 | + * |
| 132 | + * @param[in] entry_data Pointer to partition entry data. |
| 133 | + * @param[in] entry_size Size of the partition entry in bytes. |
| 134 | + * @param[out] part Pointer to structure to receive parsed partition info. |
| 135 | + * |
| 136 | + * @return 0 on success (valid partition entry), -1 if entry is empty/invalid. |
| 137 | + */ |
| 138 | +int gpt_parse_partition(const uint8_t *entry_data, uint32_t entry_size, |
| 139 | + struct gpt_part_info *part); |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief Compare UTF-16 partition name with ASCII string. |
| 143 | + * |
| 144 | + * Compares a GPT partition name (UTF-16LE) with an ASCII string label. |
| 145 | + * |
| 146 | + * @param[in] utf16_name UTF-16LE partition name from GPT entry. |
| 147 | + * @param[in] ascii_label ASCII string to compare against. |
| 148 | + * |
| 149 | + * @return 1 if names match, 0 if they don't match. |
| 150 | + */ |
| 151 | +int gpt_part_name_eq(const uint16_t *utf16_name, const char *ascii_label); |
| 152 | + |
| 153 | +#endif /* GPT_H */ |
| 154 | + |
0 commit comments