gpt.c (7179B)
1 /* see LICENSE file for copyright and license details */ 2 #include "gpt.h" 3 #include "../util.h" 4 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <strings.h> 9 #include <unistd.h> 10 11 static uint32_t crc32_table[256]; 12 static int crc32_table_initialized = 0; 13 14 static void 15 init_crc32_table(void) 16 { 17 uint32_t i, j, c; 18 for (i = 0; i < 256; i++) { 19 c = i; 20 for (j = 0; j < 8; j++) { 21 if (c & 1) 22 c = 0xedb88320U ^ (c >> 1); 23 else 24 c = c >> 1; 25 } 26 crc32_table[i] = c; 27 } 28 crc32_table_initialized = 1; 29 } 30 31 uint32_t 32 disk_crc32(uint32_t crc, const void *buf, size_t len) 33 { 34 const uint8_t *p = buf; 35 if (!crc32_table_initialized) 36 init_crc32_table(); 37 while (len--) 38 crc = (crc >> 8) ^ crc32_table[(crc ^ *p++) & 0xFF]; 39 return crc; 40 } 41 42 const struct GptType gpt_types[] = { 43 /* short two-byte codes match gdisk/sgdisk's own convention, since 44 * every caller already writing "-t ef00" etc against those tools 45 * expects the same codes here. the last is the default */ 46 {"ef00", "EFI system", "c12a7328-f81f-11d2-ba4b-00a0c93ec93b", 0xef}, 47 {"ef02", "BIOS boot", "21686148-6449-6e6f-744e-656564454649", 0x00}, 48 {"8200", "Linux swap", "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f", 0x82}, 49 {"0700", "Basic data", "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", 0x07}, 50 {"8e00", "Linux LVM", "e6d6d379-f507-44c2-a23c-238f2a3df928", 0x8e}, 51 {"fd00", "Linux RAID", "a19d880f-05fc-4d3b-a006-743f0f84911e", 0xfd}, 52 {"8302", "Linux home", "933ac7e1-2eb4-4f13-b844-0e14e2aef915", 0x83}, 53 {"8300", "Linux filesystem", "0fc63daf-8483-4772-8e79-3d69d8477de4", 0x83}, 54 }; 55 const int gpt_ntypes = (int)(sizeof(gpt_types) / sizeof(gpt_types[0])); 56 57 /* canonical XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX form: the first three 58 * groups store little-endian, the last two store big-endian, the same 59 * mixed layout every other GUID field in this file already uses */ 60 int 61 parse_guid_string(const char *s, uint8_t out[16]) 62 { 63 static const int group_len[5] = {8, 4, 4, 4, 12}; 64 static const int group_le[5] = {1, 1, 1, 0, 0}; 65 int g, i, hi, lo, byte_idx = 0; 66 const char *p = s; 67 68 for (g = 0; g < 5; g++) { 69 uint8_t bytes[6]; 70 int nbytes = group_len[g] / 2; 71 for (i = 0; i < nbytes; i++) { 72 hi = hexval(p[i * 2]); 73 lo = hexval(p[i * 2 + 1]); 74 if (hi < 0 || lo < 0) 75 return -1; 76 bytes[i] = (uint8_t)((hi << 4) | lo); 77 } 78 if (group_le[g]) { 79 for (i = nbytes - 1; i >= 0; i--) 80 out[byte_idx++] = bytes[i]; 81 } else { 82 for (i = 0; i < nbytes; i++) 83 out[byte_idx++] = bytes[i]; 84 } 85 p += group_len[g]; 86 if (g < 4) { 87 if (*p != '-') 88 return -1; 89 p++; 90 } 91 } 92 return *p == '\0' ? 0 : -1; 93 } 94 95 /* a GUID as text, in the canonical form parse_guid_string reads */ 96 void 97 format_guid(const uint8_t g[16], char out[37]) 98 { 99 snprintf( 100 out, 101 37, 102 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 103 g[3], 104 g[2], 105 g[1], 106 g[0], 107 g[5], 108 g[4], 109 g[7], 110 g[6], 111 g[8], 112 g[9], 113 g[10], 114 g[11], 115 g[12], 116 g[13], 117 g[14], 118 g[15] 119 ); 120 } 121 122 const struct GptType * 123 gpt_type_of(const uint8_t guid[16]) 124 { 125 uint8_t want[16]; 126 int i; 127 128 for (i = 0; i < gpt_ntypes; i++) 129 if (parse_guid_string(gpt_types[i].guid, want) == 0 && memcmp(want, guid, 16) == 0) 130 return &gpt_types[i]; 131 return NULL; 132 } 133 134 /* type is a short gdisk-style code (see gpt_types above), a full 135 * canonical GUID string, or unset: unset (or unrecognized, with a 136 * warning) keeps the long-standing default of "linux filesystem data", 137 * matching what every caller got before -t existed */ 138 void 139 gpt_type_guid(const char *type, uint8_t out[16]) 140 { 141 int i; 142 143 if (type) { 144 for (i = 0; i < gpt_ntypes; i++) { 145 if (strcasecmp(type, gpt_types[i].code) == 0) { 146 parse_guid_string(gpt_types[i].guid, out); 147 return; 148 } 149 } 150 if (parse_guid_string(type, out) == 0) 151 return; 152 weprintf("unrecognized GPT type '%s', using linux filesystem data\n", type); 153 } 154 parse_guid_string(gpt_types[gpt_ntypes - 1].guid, out); 155 } 156 157 /* a random GUID, version 4: the disk and each partition must have a 158 * GUID of their own, and a loader such as limine ignores a partition 159 * whose GUID is zero */ 160 int 161 gpt_random_guid(uint8_t out[16]) 162 { 163 int fd = open(ARUU_PATH_DEVURANDOM, O_RDONLY); 164 ssize_t n = fd < 0 ? -1 : read(fd, out, 16); 165 166 if (fd >= 0) 167 close(fd); 168 if (n != 16) 169 return -1; 170 out[6] = (out[6] & 0x0f) | 0x40; 171 out[8] = (out[8] & 0x3f) | 0x80; 172 return 0; 173 } 174 175 /* GPT partition names are UTF-16LE, 36 code units, null-padded: every 176 * caller so far only ever needs a plain ASCII label, so this widens 177 * each byte rather than pulling in a real UTF-8 decoder for it */ 178 void 179 gpt_set_name(uint16_t name[36], const char *s) 180 { 181 size_t i; 182 183 memset(name, 0, 36 * sizeof(uint16_t)); 184 if (!s) 185 return; 186 for (i = 0; i < 35 && s[i]; i++) 187 name[i] = (uint16_t)(unsigned char)s[i]; 188 } 189 190 int 191 gpt_read(struct BlockDev *dev, struct GptHeader *hdr, struct GptPartition *parts) 192 { 193 unsigned char sector[512]; 194 size_t part_sectors; 195 196 if (blockdev_read(dev, 1, sector, 1) < 0) 197 return -1; 198 199 memcpy(hdr, sector, sizeof(*hdr)); 200 201 if (memcmp(hdr->sig, "EFI PART", 8) != 0) 202 return -1; 203 204 part_sectors = (hdr->num_parts * hdr->part_size + dev->sec_size - 1) / dev->sec_size; 205 if (blockdev_read(dev, hdr->partition_lba, parts, part_sectors) < 0) 206 return -1; 207 208 return 0; 209 } 210 211 int 212 gpt_write(struct BlockDev *dev, struct GptHeader *hdr, struct GptPartition *parts) 213 { 214 unsigned char sector[512]; 215 struct MbrHeader pmbr; 216 struct GptHeader backup_hdr; 217 uint64_t limit; 218 size_t part_sectors; 219 220 part_sectors = (hdr->num_parts * hdr->part_size + dev->sec_size - 1) / dev->sec_size; 221 222 hdr->parts_crc = disk_crc32(~0U, parts, hdr->num_parts * hdr->part_size) ^ ~0U; 223 224 hdr->crc = 0; 225 hdr->crc = disk_crc32(~0U, hdr, hdr->size) ^ ~0U; 226 227 memset(&pmbr, 0, sizeof(pmbr)); 228 pmbr.parts[0].type = 0xEE; 229 pmbr.parts[0].start_lba = 1; 230 limit = dev->size / dev->sec_size - 1; 231 pmbr.parts[0].size = limit > 0xFFFFFFFFU ? 0xFFFFFFFFU : (uint32_t)limit; 232 pmbr.sig[0] = 0x55; 233 pmbr.sig[1] = 0xAA; 234 if (blockdev_write(dev, 0, &pmbr, 1) < 0) 235 return -1; 236 237 memset(sector, 0, sizeof(sector)); 238 memcpy(sector, hdr, sizeof(*hdr)); 239 if (blockdev_write(dev, 1, sector, 1) < 0) 240 return -1; 241 if (blockdev_write(dev, hdr->partition_lba, parts, part_sectors) < 0) 242 return -1; 243 244 backup_hdr = *hdr; 245 backup_hdr.current_lba = hdr->backup_lba; 246 backup_hdr.backup_lba = hdr->current_lba; 247 backup_hdr.partition_lba = backup_hdr.current_lba - part_sectors; 248 249 backup_hdr.crc = 0; 250 backup_hdr.crc = disk_crc32(~0U, &backup_hdr, backup_hdr.size) ^ ~0U; 251 252 if (blockdev_write(dev, backup_hdr.partition_lba, parts, part_sectors) < 0) 253 return -1; 254 255 memset(sector, 0, sizeof(sector)); 256 memcpy(sector, &backup_hdr, sizeof(backup_hdr)); 257 if (blockdev_write(dev, backup_hdr.current_lba, sector, 1) < 0) 258 return -1; 259 260 return 0; 261 }