label.c (11950B)
1 /* see LICENSE file for copyright and license details */ 2 #include "label.h" 3 #include "gpt.h" 4 #include "../util.h" 5 6 #include <errno.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <string.h> 10 #include <strings.h> 11 12 #define MBR_SLOTS 4 13 14 static const struct label_type *types_cache; 15 16 static void 17 set_type(struct label_part *p, const char *code) 18 { 19 const struct GptType *t = NULL; 20 int i; 21 22 for (i = 0; i < gpt_ntypes; i++) 23 if (strcasecmp(gpt_types[i].code, code) == 0) 24 t = &gpt_types[i]; 25 memset(p->type, 0, sizeof p->type); 26 if (!t) 27 return; 28 snprintf(p->type, sizeof p->type, "%s", t->code); 29 parse_guid_string(t->guid, p->type_guid); 30 p->mbr_type = t->mbr; 31 } 32 33 const char * 34 label_type_name(const struct label_part *p) 35 { 36 int i; 37 38 for (i = 0; i < gpt_ntypes; i++) 39 if (p->type[0] && strcasecmp(gpt_types[i].code, p->type) == 0) 40 return gpt_types[i].name; 41 return "unknown"; 42 } 43 44 int 45 label_types(const struct label_type **out) 46 { 47 static struct label_type table[16]; 48 int i; 49 50 if (!types_cache) { 51 for (i = 0; i < gpt_ntypes && i < 16; i++) { 52 table[i].code = gpt_types[i].code; 53 table[i].name = gpt_types[i].name; 54 table[i].gpt_only = gpt_types[i].mbr == 0; 55 } 56 types_cache = table; 57 } 58 *out = types_cache; 59 return gpt_ntypes; 60 } 61 62 static uint64_t 63 gpt_array_sectors(size_t sec_size) 64 { 65 return (uint64_t)(GPT_ENTRIES * GPT_ENTRY_SZ + sec_size - 1) / sec_size; 66 } 67 68 int 69 label_init(struct label *l, const struct BlockDev *dev, enum label_kind kind) 70 { 71 memset(l, 0, sizeof *l); 72 l->kind = kind; 73 l->sec_size = dev->sec_size; 74 l->sectors = dev->size / dev->sec_size; 75 if (kind == LABEL_GPT) { 76 uint64_t arr = gpt_array_sectors(l->sec_size); 77 78 /* the primary header and array in front, the backup behind */ 79 if (l->sectors < 2 + arr + 1 + arr + 1 + LABEL_ALIGN) 80 return -1; 81 l->first = 2 + arr; 82 l->last = l->sectors - 2 - arr; 83 l->slots = LABEL_SLOTS; 84 if (gpt_random_guid(l->disk_guid) < 0) 85 return -1; 86 } else if (kind == LABEL_MBR) { 87 if (l->sectors <= LABEL_ALIGN) 88 return -1; 89 l->first = LABEL_ALIGN; 90 l->last = l->sectors - 1; 91 l->slots = MBR_SLOTS; 92 { 93 uint8_t r[16]; 94 95 if (gpt_random_guid(r) < 0) 96 return -1; 97 memcpy(&l->mbr_id, r, sizeof l->mbr_id); 98 } 99 } 100 return 0; 101 } 102 103 static int 104 read_gpt(struct BlockDev *dev, struct label *l) 105 { 106 struct GptHeader hdr; 107 struct GptPartition *parts; 108 uint32_t i; 109 int n = 0; 110 111 if (!(parts = calloc(GPT_ENTRIES, GPT_ENTRY_SZ))) 112 return -1; 113 if (gpt_read(dev, &hdr, parts) < 0 || hdr.part_size != GPT_ENTRY_SZ 114 || hdr.num_parts > GPT_ENTRIES) { 115 free(parts); 116 return 0; /* no GPT */ 117 } 118 l->kind = LABEL_GPT; 119 l->slots = LABEL_SLOTS; 120 l->first = hdr.first_usable; 121 l->last = hdr.last_usable; 122 memcpy(l->disk_guid, hdr.disk_guid, 16); 123 for (i = 0; i < hdr.num_parts; i++) { 124 struct label_part *p = &l->part[i]; 125 const struct GptType *t; 126 int c; 127 128 if (parts[i].start_lba == 0 && parts[i].end_lba == 0) 129 continue; 130 p->used = 1; 131 p->start = parts[i].start_lba; 132 p->end = parts[i].end_lba; 133 memcpy(p->type_guid, parts[i].type_guid, 16); 134 memcpy(p->guid, parts[i].part_guid, 16); 135 if ((t = gpt_type_of(parts[i].type_guid))) { 136 snprintf(p->type, sizeof p->type, "%s", t->code); 137 p->mbr_type = t->mbr; 138 } 139 /* names are UTF-16, narrowed: what the installer writes is ASCII */ 140 for (c = 0; c < 36 && c < (int)sizeof p->name - 1 && parts[i].name[c]; c++) 141 p->name[c] = parts[i].name[c] < 128 ? (char)parts[i].name[c] : '?'; 142 n++; 143 } 144 free(parts); 145 return n >= 0 ? 1 : 0; 146 } 147 148 static int 149 read_mbr(struct BlockDev *dev, struct label *l) 150 { 151 struct MbrHeader mbr; 152 int i; 153 154 if (blockdev_read(dev, 0, &mbr, 1) < 0) 155 return -1; 156 if (mbr.sig[0] != 0x55 || mbr.sig[1] != 0xAA) 157 return 0; 158 /* a protective MBR over a GPT that did not read is no table of its own */ 159 if (mbr.parts[0].type == 0xEE) 160 return 0; 161 l->kind = LABEL_MBR; 162 l->slots = MBR_SLOTS; 163 l->first = LABEL_ALIGN; 164 l->last = l->sectors - 1; 165 memcpy(l->mbr_code, mbr.code, sizeof l->mbr_code); 166 memcpy(&l->mbr_id, mbr.code + 440, 4); 167 for (i = 0; i < MBR_SLOTS; i++) { 168 struct label_part *p = &l->part[i]; 169 int t; 170 171 if (mbr.parts[i].type == 0 || mbr.parts[i].size == 0) 172 continue; 173 p->used = 1; 174 p->start = mbr.parts[i].start_lba; 175 p->end = (uint64_t)mbr.parts[i].start_lba + mbr.parts[i].size - 1; 176 p->mbr_type = mbr.parts[i].type; 177 p->active = mbr.parts[i].boot == 0x80; 178 /* 0x83 stands for both Linux types: the plain one's code, not the home */ 179 for (t = 0; t < gpt_ntypes; t++) { 180 if (gpt_types[t].mbr == p->mbr_type && strcmp(gpt_types[t].code, "8302") != 0) { 181 snprintf(p->type, sizeof p->type, "%s", gpt_types[t].code); 182 parse_guid_string(gpt_types[t].guid, p->type_guid); 183 break; 184 } 185 } 186 } 187 return 1; 188 } 189 190 int 191 label_read(struct BlockDev *dev, struct label *l) 192 { 193 int got; 194 195 memset(l, 0, sizeof *l); 196 l->sec_size = dev->sec_size; 197 l->sectors = dev->size / dev->sec_size; 198 if ((got = read_gpt(dev, l)) < 0) 199 return -1; 200 if (got > 0) 201 return 0; 202 if ((got = read_mbr(dev, l)) < 0) 203 return -1; 204 return 0; 205 } 206 207 static int 208 overlaps(const struct label *l, uint64_t start, uint64_t end, int except) 209 { 210 int i; 211 212 for (i = 0; i < l->slots; i++) 213 if (i != except && l->part[i].used && start <= l->part[i].end && end >= l->part[i].start) 214 return 1; 215 return 0; 216 } 217 218 static int 219 by_start(const void *a, const void *b) 220 { 221 const struct label_part *x = *(const struct label_part *const *)a, 222 *y = *(const struct label_part *const *)b; 223 224 return x->start < y->start ? -1 : x->start > y->start; 225 } 226 227 int 228 label_gaps(const struct label *l, struct label_gap *out, int max) 229 { 230 const struct label_part *order[LABEL_SLOTS]; 231 int i, n = 0, g = 0; 232 uint64_t from = l->first; 233 234 if (l->kind == LABEL_NONE) 235 return 0; 236 for (i = 0; i < l->slots; i++) 237 if (l->part[i].used) 238 order[n++] = &l->part[i]; 239 qsort(order, (size_t)n, sizeof order[0], by_start); 240 for (i = 0; i <= n && g < max; i++) { 241 uint64_t to = i < n ? order[i]->start : l->last + 1; 242 uint64_t start = (from + LABEL_ALIGN - 1) / LABEL_ALIGN * LABEL_ALIGN; 243 244 /* the end is the sector before the next partition, rounded down so the next starts aligned */ 245 if (to > start && to - start >= LABEL_ALIGN) { 246 out[g].start = start; 247 out[g].end = to - 1; 248 g++; 249 } 250 if (i < n && order[i]->end + 1 > from) 251 from = order[i]->end + 1; 252 } 253 return g; 254 } 255 256 int 257 label_add(struct label *l, uint64_t start, uint64_t sectors, const char *type, const char *name) 258 { 259 struct label_gap gaps[LABEL_SLOTS + 1]; 260 struct label_part *p; 261 int n, i, slot = -1, pick = -1; 262 uint64_t end; 263 264 if (l->kind == LABEL_NONE) 265 return -EINVAL; 266 for (i = 0; i < l->slots && slot < 0; i++) 267 if (!l->part[i].used) 268 slot = i; 269 if (slot < 0) 270 return -EEXIST; 271 n = label_gaps(l, gaps, LABEL_SLOTS + 1); 272 if (start == 0) { 273 /* the first gap that holds it, or with no size the biggest */ 274 for (i = 0; i < n; i++) { 275 uint64_t room = gaps[i].end - gaps[i].start + 1; 276 277 if (sectors && room >= sectors) { 278 pick = i; 279 break; 280 } 281 if (!sectors && (pick < 0 || room > gaps[pick].end - gaps[pick].start + 1)) 282 pick = i; 283 } 284 if (pick < 0) 285 return -ENOSPC; 286 start = gaps[pick].start; 287 end = sectors ? start + sectors - 1 : gaps[pick].end; 288 } else if (sectors) { 289 end = start + sectors - 1; 290 } else { 291 /* to the end of the gap the start is in */ 292 end = 0; 293 for (i = 0; i < n; i++) 294 if (start >= gaps[i].start && start <= gaps[i].end) 295 end = gaps[i].end; 296 if (!end) 297 return -EINVAL; 298 } 299 if (start < l->first || end > l->last || end < start || overlaps(l, start, end, -1)) 300 return -EINVAL; 301 p = &l->part[slot]; 302 memset(p, 0, sizeof *p); 303 p->used = 1; 304 p->start = start; 305 p->end = end; 306 set_type(p, type ? type : "8300"); 307 if (!p->type[0]) 308 set_type(p, "8300"); 309 if (name) 310 snprintf(p->name, sizeof p->name, "%s", name); 311 if (l->kind == LABEL_GPT && gpt_random_guid(p->guid) < 0) { 312 memset(p, 0, sizeof *p); 313 return -EIO; 314 } 315 return slot; 316 } 317 318 int 319 label_delete(struct label *l, int slot) 320 { 321 if (slot < 0 || slot >= l->slots || !l->part[slot].used) 322 return -EINVAL; 323 memset(&l->part[slot], 0, sizeof l->part[slot]); 324 return 0; 325 } 326 327 int 328 label_set_type(struct label *l, int slot, const char *type) 329 { 330 if (slot < 0 || slot >= l->slots || !l->part[slot].used) 331 return -EINVAL; 332 set_type(&l->part[slot], type); 333 return l->part[slot].type[0] ? 0 : -EINVAL; 334 } 335 336 int 337 label_set_name(struct label *l, int slot, const char *name) 338 { 339 if (slot < 0 || slot >= l->slots || !l->part[slot].used) 340 return -EINVAL; 341 snprintf(l->part[slot].name, sizeof l->part[slot].name, "%s", name ? name : ""); 342 return 0; 343 } 344 345 static int 346 write_gpt(struct BlockDev *dev, const struct label *l) 347 { 348 struct GptHeader hdr; 349 struct GptPartition *parts; 350 uint64_t arr = gpt_array_sectors(l->sec_size); 351 int i, c, rc; 352 353 if (!(parts = calloc(GPT_ENTRIES, GPT_ENTRY_SZ))) 354 return -1; 355 memset(&hdr, 0, sizeof hdr); 356 memcpy(hdr.sig, "EFI PART", 8); 357 hdr.rev = 0x00010000; 358 hdr.size = 92; 359 hdr.current_lba = 1; 360 hdr.backup_lba = l->sectors - 1; 361 hdr.first_usable = 2 + arr; 362 hdr.last_usable = l->sectors - 2 - arr; 363 memcpy(hdr.disk_guid, l->disk_guid, 16); 364 hdr.partition_lba = 2; 365 hdr.num_parts = GPT_ENTRIES; 366 hdr.part_size = GPT_ENTRY_SZ; 367 for (i = 0; i < LABEL_SLOTS; i++) { 368 const struct label_part *p = &l->part[i]; 369 370 if (!p->used) 371 continue; 372 memcpy(parts[i].type_guid, p->type_guid, 16); 373 memcpy(parts[i].part_guid, p->guid, 16); 374 parts[i].start_lba = p->start; 375 parts[i].end_lba = p->end; 376 for (c = 0; c < 35 && p->name[c]; c++) 377 parts[i].name[c] = (uint16_t)(unsigned char)p->name[c]; 378 } 379 rc = gpt_write(dev, &hdr, parts); 380 free(parts); 381 return rc; 382 } 383 384 /* the GPT headers of dev, primary and backup, zeroed when there are any */ 385 static void 386 drop_gpt(struct BlockDev *dev) 387 { 388 unsigned char sector[4096]; 389 uint64_t at[2] = {1, dev->size / dev->sec_size - 1}; 390 int i; 391 392 if (dev->sec_size > sizeof sector) 393 return; 394 for (i = 0; i < 2; i++) { 395 if (blockdev_read(dev, at[i], sector, 1) < 0 || memcmp(sector, "EFI PART", 8) != 0) 396 continue; 397 memset(sector, 0, sizeof sector); 398 blockdev_write(dev, at[i], sector, 1); 399 } 400 } 401 402 static int 403 write_mbr(struct BlockDev *dev, const struct label *l) 404 { 405 struct MbrHeader mbr; 406 int i; 407 408 memset(&mbr, 0, sizeof mbr); 409 memcpy(mbr.code, l->mbr_code, sizeof mbr.code); 410 memcpy(mbr.code + 440, &l->mbr_id, 4); 411 for (i = 0; i < MBR_SLOTS; i++) { 412 const struct label_part *p = &l->part[i]; 413 414 if (!p->used) 415 continue; 416 mbr.parts[i].boot = p->active ? 0x80 : 0; 417 mbr.parts[i].type = p->mbr_type; 418 mbr.parts[i].start_lba = (uint32_t)p->start; 419 mbr.parts[i].size = (uint32_t)(p->end - p->start + 1); 420 /* CHS is not used: the largest value says "ask the LBA" */ 421 memset(mbr.parts[i].start_chs, 0xff, 3); 422 memset(mbr.parts[i].end_chs, 0xff, 3); 423 } 424 mbr.sig[0] = 0x55; 425 mbr.sig[1] = 0xAA; 426 /* a GPT left behind would be read in preference to this table */ 427 drop_gpt(dev); 428 return blockdev_write(dev, 0, &mbr, 1); 429 } 430 431 int 432 label_write(struct BlockDev *dev, const struct label *l) 433 { 434 int rc; 435 436 if (l->kind == LABEL_GPT) 437 rc = write_gpt(dev, l); 438 else if (l->kind == LABEL_MBR) 439 rc = write_mbr(dev, l); 440 else 441 return -1; 442 if (rc == 0) 443 dev->dirty = 1; 444 return rc; 445 }