1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ptgen - partition table generator 4 * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name> 5 * 6 * uses parts of afdisk 7 * Copyright (C) 2002 by David Roetzel <david@roetzel.de> 8 * 9 * UUID/GUID definition stolen from kernel/include/uapi/linux/uuid.h 10 * Copyright (C) 2010, Intel Corp. Huang Ying <ying.huang@intel.com> 11 */ 12 13 #include <byteswap.h> 14 #include <sys/types.h> 15 #include <sys/stat.h> 16 #include <string.h> 17 #include <unistd.h> 18 #include <stdlib.h> 19 #include <stdio.h> 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include <ctype.h> 23 #include <inttypes.h> 24 #include <fcntl.h> 25 #include <stdint.h> 26 #include "cyg_crc.h" 27 28 #if __BYTE_ORDER == __BIG_ENDIAN 29 #define cpu_to_le16(x) bswap_16(x) 30 #define cpu_to_le32(x) bswap_32(x) 31 #define cpu_to_le64(x) bswap_64(x) 32 #elif __BYTE_ORDER == __LITTLE_ENDIAN 33 #define cpu_to_le16(x) (x) 34 #define cpu_to_le32(x) (x) 35 #define cpu_to_le64(x) (x) 36 #else 37 #error unknown endianness! 38 #endif 39 40 #define swap(a, b) \ 41 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 42 43 #define BIT(_x) (1UL << (_x)) 44 45 typedef struct { 46 uint8_t b[16]; 47 } guid_t; 48 49 #define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ 50 ((guid_t) \ 51 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ 52 (b) & 0xff, ((b) >> 8) & 0xff, \ 53 (c) & 0xff, ((c) >> 8) & 0xff, \ 54 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) 55 56 #define GUID_STRING_LENGTH 36 57 58 #define GPT_SIGNATURE 0x5452415020494645ULL 59 #define GPT_REVISION 0x00010000 60 61 #define GUID_PARTITION_SYSTEM \ 62 GUID_INIT( 0xC12A7328, 0xF81F, 0x11d2, \ 63 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B) 64 65 #define GUID_PARTITION_BASIC_DATA \ 66 GUID_INIT( 0xEBD0A0A2, 0xB9E5, 0x4433, \ 67 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7) 68 69 #define GUID_PARTITION_BIOS_BOOT \ 70 GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \ 71 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49) 72 73 #define GUID_PARTITION_CHROME_OS_KERNEL \ 74 GUID_INIT( 0xFE3A2A5D, 0x4F32, 0x41A7, \ 75 0xB7, 0x25, 0xAC, 0xCC, 0x32, 0x85, 0xA3, 0x09) 76 77 #define GUID_PARTITION_LINUX_FIT_GUID \ 78 GUID_INIT( 0xcae9be83, 0xb15f, 0x49cc, \ 79 0x86, 0x3f, 0x08, 0x1b, 0x74, 0x4a, 0x2d, 0x93) 80 81 #define GUID_PARTITION_LINUX_FS_GUID \ 82 GUID_INIT( 0x0fc63daf, 0x8483, 0x4772, \ 83 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4) 84 85 #define GUID_PARTITION_SIFIVE_SPL \ 86 GUID_INIT( 0x5b193300, 0xfc78, 0x40cd, \ 87 0x80, 0x02, 0xe8, 0x6c, 0x45, 0x58, 0x0b, 0x47) 88 89 #define GUID_PARTITION_SIFIVE_UBOOT \ 90 GUID_INIT( 0x2e54b353, 0x1271, 0x4842, \ 91 0x80, 0x6f, 0xe4, 0x36, 0xd6, 0xaf, 0x69, 0x85) 92 93 #define GPT_HEADER_SIZE 92 94 #define GPT_ENTRY_SIZE 128 95 #define GPT_ENTRY_MAX 128 96 #define GPT_ENTRY_NAME_SIZE 72 97 #define GPT_SIZE GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE 98 99 #define GPT_ATTR_PLAT_REQUIRED BIT(0) 100 #define GPT_ATTR_EFI_IGNORE BIT(1) 101 #define GPT_ATTR_LEGACY_BOOT BIT(2) 102 103 #define GPT_HEADER_SECTOR 1 104 #define GPT_FIRST_ENTRY_SECTOR 2 105 106 #define MBR_ENTRY_MAX 4 107 #define MBR_DISK_SIGNATURE_OFFSET 440 108 #define MBR_PARTITION_ENTRY_OFFSET 446 109 #define MBR_BOOT_SIGNATURE_OFFSET 510 110 111 #define DISK_SECTOR_SIZE 512 112 113 /* Partition table entry */ 114 struct pte { 115 uint8_t active; 116 uint8_t chs_start[3]; 117 uint8_t type; 118 uint8_t chs_end[3]; 119 uint32_t start; 120 uint32_t length; 121 }; 122 123 struct partinfo { 124 unsigned long actual_start; 125 unsigned long start; 126 unsigned long size; 127 int type; 128 int hybrid; 129 char *name; 130 short int required; 131 bool has_guid; 132 guid_t guid; 133 uint64_t gattr; /* GPT partition attributes */ 134 }; 135 136 /* GPT Partition table header */ 137 struct gpth { 138 uint64_t signature; 139 uint32_t revision; 140 uint32_t size; 141 uint32_t crc32; 142 uint32_t reserved; 143 uint64_t self; 144 uint64_t alternate; 145 uint64_t first_usable; 146 uint64_t last_usable; 147 guid_t disk_guid; 148 uint64_t first_entry; 149 uint32_t entry_num; 150 uint32_t entry_size; 151 uint32_t entry_crc32; 152 } __attribute__((packed)); 153 154 /* GPT Partition table entry */ 155 struct gpte { 156 guid_t type; 157 guid_t guid; 158 uint64_t start; 159 uint64_t end; 160 uint64_t attr; 161 char name[GPT_ENTRY_NAME_SIZE]; 162 } __attribute__((packed)); 163 164 165 int verbose = 0; 166 int active = 1; 167 int heads = -1; 168 int sectors = -1; 169 int kb_align = 0; 170 bool ignore_null_sized_partition = false; 171 bool use_guid_partition_table = false; 172 struct partinfo parts[GPT_ENTRY_MAX]; 173 char *filename = NULL; 174 175 176 /* 177 * parse the size argument, which is either 178 * a simple number (K assumed) or 179 * K, M or G 180 * 181 * returns the size in KByte 182 */ 183 static long to_kbytes(const char *string) 184 { 185 int exp = 0; 186 long result; 187 char *end; 188 189 result = strtoul(string, &end, 0); 190 switch (tolower(*end)) { 191 case 'k' : 192 case '\0' : exp = 0; break; 193 case 'm' : exp = 1; break; 194 case 'g' : exp = 2; break; 195 default: return 0; 196 } 197 198 if (*end) 199 end++; 200 201 if (*end) { 202 fputs("garbage after end of number\n", stderr); 203 return 0; 204 } 205 206 /* result: number + 1024^(exp) */ 207 if (exp == 0) 208 return result; 209 return result * (2 << ((10 * exp) - 1)); 210 } 211 212 /* convert the sector number into a CHS value for the partition table */ 213 static void to_chs(long sect, unsigned char chs[3]) 214 { 215 int c,h,s; 216 217 s = (sect % sectors) + 1; 218 sect = sect / sectors; 219 h = sect % heads; 220 sect = sect / heads; 221 c = sect; 222 223 chs[0] = h; 224 chs[1] = s | ((c >> 2) & 0xC0); 225 chs[2] = c & 0xFF; 226 227 return; 228 } 229 230 /* round the sector number up to the next cylinder */ 231 static inline unsigned long round_to_cyl(long sect) 232 { 233 int cyl_size = heads * sectors; 234 235 return sect + cyl_size - (sect % cyl_size); 236 } 237 238 /* round the sector number up to the kb_align boundary */ 239 static inline unsigned long round_to_kb(long sect) { 240 return ((sect - 1) / kb_align + 1) * kb_align; 241 } 242 243 /* Compute a CRC for guid partition table */ 244 static inline unsigned long gpt_crc32(void *buf, unsigned long len) 245 { 246 return cyg_crc32_accumulate(~0L, buf, len) ^ ~0L; 247 } 248 249 /* Parse a guid string to guid_t struct */ 250 static inline int guid_parse(char *buf, guid_t *guid) 251 { 252 char b[4] = {0}; 253 char *p = buf; 254 unsigned i = 0; 255 if (strnlen(buf, GUID_STRING_LENGTH) != GUID_STRING_LENGTH) 256 return -1; 257 for (i = 0; i < sizeof(guid_t); i++) { 258 if (*p == '-') 259 p++; 260 if (*p == '\0') 261 return -1; 262 memcpy(b, p, 2); 263 guid->b[i] = strtol(b, 0, 16); 264 p += 2; 265 } 266 swap(guid->b[0], guid->b[3]); 267 swap(guid->b[1], guid->b[2]); 268 swap(guid->b[4], guid->b[5]); 269 swap(guid->b[6], guid->b[7]); 270 return 0; 271 } 272 273 /* 274 * Map GPT partition types to partition GUIDs. 275 * NB: not all GPT partition types have an equivalent MBR type. 276 */ 277 static inline bool parse_gpt_parttype(const char *type, struct partinfo *part) 278 { 279 if (!strcmp(type, "cros_kernel")) { 280 part->has_guid = true; 281 part->guid = GUID_PARTITION_CHROME_OS_KERNEL; 282 /* Default attributes: bootable kernel. */ 283 part->gattr = (1ULL << 48) | /* priority=1 */ 284 (1ULL << 56); /* success=1 */ 285 return true; 286 } 287 288 if (!strcmp(type, "sifiveu_spl")) { 289 part->has_guid = true; 290 part->guid = GUID_PARTITION_SIFIVE_SPL; 291 return true; 292 } 293 294 if (!strcmp(type, "sifiveu_uboot")) { 295 part->has_guid = true; 296 part->guid = GUID_PARTITION_SIFIVE_UBOOT; 297 return true; 298 } 299 300 return false; 301 } 302 303 /* init an utf-16 string from utf-8 string */ 304 static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize) 305 { 306 unsigned i, n = 0; 307 for (i = 0; i < bufsize; i++) { 308 if (str[n] == 0x00) { 309 buf[i] = 0x00; 310 return ; 311 } else if ((str[n] & 0x80) == 0x00) {//0xxxxxxx 312 buf[i] = cpu_to_le16(str[n++]); 313 } else if ((str[n] & 0xE0) == 0xC0) {//110xxxxx 314 buf[i] = cpu_to_le16((str[n] & 0x1F) << 6 | (str[n + 1] & 0x3F)); 315 n += 2; 316 } else if ((str[n] & 0xF0) == 0xE0) {//1110xxxx 317 buf[i] = cpu_to_le16((str[n] & 0x0F) << 12 | (str[n + 1] & 0x3F) << 6 | (str[n + 2] & 0x3F)); 318 n += 3; 319 } else { 320 buf[i] = cpu_to_le16('?'); 321 n++; 322 } 323 } 324 } 325 326 /* check the partition sizes and write the partition table */ 327 static int gen_ptable(uint32_t signature, int nr) 328 { 329 struct pte pte[MBR_ENTRY_MAX]; 330 unsigned long start, len, sect = 0; 331 int i, fd, ret = -1; 332 333 memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX); 334 for (i = 0; i < nr; i++) { 335 if (!parts[i].size) { 336 if (ignore_null_sized_partition) 337 continue; 338 fprintf(stderr, "Invalid size in partition %d!\n", i); 339 return ret; 340 } 341 342 pte[i].active = ((i + 1) == active) ? 0x80 : 0; 343 pte[i].type = parts[i].type; 344 345 start = sect + sectors; 346 if (parts[i].start != 0) { 347 if (parts[i].start * 2 < start) { 348 fprintf(stderr, "Invalid start %ld for partition %d!\n", 349 parts[i].start, i); 350 return ret; 351 } 352 start = parts[i].start * 2; 353 } else if (kb_align != 0) { 354 start = round_to_kb(start); 355 } 356 pte[i].start = cpu_to_le32(start); 357 358 sect = start + parts[i].size * 2; 359 if (kb_align == 0) 360 sect = round_to_cyl(sect); 361 pte[i].length = cpu_to_le32(len = sect - start); 362 363 to_chs(start, pte[i].chs_start); 364 to_chs(start + len - 1, pte[i].chs_end); 365 366 if (verbose) 367 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", 368 i, 369 (long)start * DISK_SECTOR_SIZE, 370 (long)(start + len) * DISK_SECTOR_SIZE, 371 (long)len * DISK_SECTOR_SIZE); 372 printf("%ld\n", (long)start * DISK_SECTOR_SIZE); 373 printf("%ld\n", (long)len * DISK_SECTOR_SIZE); 374 } 375 376 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { 377 fprintf(stderr, "Can't open output file '%s'\n",filename); 378 return ret; 379 } 380 381 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET); 382 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) { 383 fputs("write failed.\n", stderr); 384 goto fail; 385 } 386 387 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET); 388 if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) { 389 fputs("write failed.\n", stderr); 390 goto fail; 391 } 392 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET); 393 if (write(fd, "\x55\xaa", 2) != 2) { 394 fputs("write failed.\n", stderr); 395 goto fail; 396 } 397 398 ret = 0; 399 fail: 400 close(fd); 401 return ret; 402 } 403 404 /* check the partition sizes and write the guid partition table */ 405 static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr) 406 { 407 struct pte pte[MBR_ENTRY_MAX]; 408 struct gpth gpth = { 409 .signature = cpu_to_le64(GPT_SIGNATURE), 410 .revision = cpu_to_le32(GPT_REVISION), 411 .size = cpu_to_le32(GPT_HEADER_SIZE), 412 .self = cpu_to_le64(GPT_HEADER_SECTOR), 413 .first_usable = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE), 414 .first_entry = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR), 415 .disk_guid = guid, 416 .entry_num = cpu_to_le32(GPT_ENTRY_MAX), 417 .entry_size = cpu_to_le32(GPT_ENTRY_SIZE), 418 }; 419 struct gpte gpte[GPT_ENTRY_MAX]; 420 uint64_t start, end; 421 uint64_t sect = GPT_SIZE + GPT_FIRST_ENTRY_SECTOR; 422 int fd, ret = -1; 423 unsigned i, pmbr = 1; 424 425 memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX); 426 memset(gpte, 0, GPT_ENTRY_SIZE * GPT_ENTRY_MAX); 427 for (i = 0; i < nr; i++) { 428 if (!parts[i].size) { 429 if (ignore_null_sized_partition) 430 continue; 431 fprintf(stderr, "Invalid size in partition %d!\n", i); 432 return ret; 433 } 434 start = sect; 435 if (parts[i].start != 0) { 436 if (parts[i].start * 2 < start) { 437 fprintf(stderr, "Invalid start %ld for partition %d!\n", 438 parts[i].start, i); 439 return ret; 440 } 441 start = parts[i].start * 2; 442 } else if (kb_align != 0) { 443 start = round_to_kb(start); 444 } 445 parts[i].actual_start = start; 446 gpte[i].start = cpu_to_le64(start); 447 448 sect = start + parts[i].size * 2; 449 gpte[i].end = cpu_to_le64(sect -1); 450 gpte[i].guid = guid; 451 gpte[i].guid.b[sizeof(guid_t) -1] += i + 1; 452 gpte[i].type = parts[i].guid; 453 454 if (parts[i].hybrid && pmbr < MBR_ENTRY_MAX) { 455 pte[pmbr].active = ((i + 1) == active) ? 0x80 : 0; 456 pte[pmbr].type = parts[i].type; 457 pte[pmbr].start = cpu_to_le32(start); 458 pte[pmbr].length = cpu_to_le32(sect - start); 459 to_chs(start, pte[1].chs_start); 460 to_chs(sect - 1, pte[1].chs_end); 461 pmbr++; 462 } 463 gpte[i].attr = parts[i].gattr; 464 465 if (parts[i].name) 466 init_utf16(parts[i].name, (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t)); 467 468 if ((i + 1) == (unsigned)active) 469 gpte[i].attr |= GPT_ATTR_LEGACY_BOOT; 470 471 if (parts[i].required) 472 gpte[i].attr |= GPT_ATTR_PLAT_REQUIRED; 473 474 if (verbose) 475 fprintf(stderr, "Partition %d: start=%" PRIu64 ", end=%" PRIu64 ", size=%" PRIu64 "\n", 476 i, 477 start * DISK_SECTOR_SIZE, sect * DISK_SECTOR_SIZE, 478 (sect - start) * DISK_SECTOR_SIZE); 479 printf("%" PRIu64 "\n", start * DISK_SECTOR_SIZE); 480 printf("%" PRIu64 "\n", (sect - start) * DISK_SECTOR_SIZE); 481 } 482 483 if (parts[0].actual_start > GPT_FIRST_ENTRY_SECTOR + GPT_SIZE) { 484 gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_SIZE); 485 gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64(parts[0].actual_start - 1); 486 gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT; 487 gpte[GPT_ENTRY_MAX - 1].guid = guid; 488 gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX; 489 } 490 491 end = sect + GPT_SIZE; 492 493 pte[0].type = 0xEE; 494 pte[0].start = cpu_to_le32(GPT_HEADER_SECTOR); 495 pte[0].length = cpu_to_le32(end - GPT_HEADER_SECTOR); 496 to_chs(GPT_HEADER_SECTOR, pte[0].chs_start); 497 to_chs(end, pte[0].chs_end); 498 499 gpth.last_usable = cpu_to_le64(end - GPT_SIZE - 1); 500 gpth.alternate = cpu_to_le64(end); 501 gpth.entry_crc32 = cpu_to_le32(gpt_crc32(gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX)); 502 gpth.crc32 = cpu_to_le32(gpt_crc32((char *)&gpth, GPT_HEADER_SIZE)); 503 504 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { 505 fprintf(stderr, "Can't open output file '%s'\n",filename); 506 return ret; 507 } 508 509 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET); 510 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) { 511 fputs("write failed.\n", stderr); 512 goto fail; 513 } 514 515 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET); 516 if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) { 517 fputs("write failed.\n", stderr); 518 goto fail; 519 } 520 521 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET); 522 if (write(fd, "\x55\xaa", 2) != 2) { 523 fputs("write failed.\n", stderr); 524 goto fail; 525 } 526 527 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) { 528 fputs("write failed.\n", stderr); 529 goto fail; 530 } 531 532 lseek(fd, GPT_FIRST_ENTRY_SECTOR * DISK_SECTOR_SIZE, SEEK_SET); 533 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) { 534 fputs("write failed.\n", stderr); 535 goto fail; 536 } 537 538 #ifdef WANT_ALTERNATE_PTABLE 539 /* The alternate partition table (We omit it by default) */ 540 swap(gpth.self, gpth.alternate); 541 gpth.first_entry = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE), 542 gpth.crc32 = 0; 543 gpth.crc32 = cpu_to_le32(gpt_crc32(&gpth, GPT_HEADER_SIZE)); 544 545 lseek(fd, end * DISK_SECTOR_SIZE - GPT_ENTRY_SIZE * GPT_ENTRY_MAX, SEEK_SET); 546 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) { 547 fputs("write failed.\n", stderr); 548 goto fail; 549 } 550 551 lseek(fd, end * DISK_SECTOR_SIZE, SEEK_SET); 552 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) { 553 fputs("write failed.\n", stderr); 554 goto fail; 555 } 556 lseek(fd, (end + 1) * DISK_SECTOR_SIZE -1, SEEK_SET); 557 if (write(fd, "\x00", 1) != 1) { 558 fputs("write failed.\n", stderr); 559 goto fail; 560 } 561 #endif 562 563 ret = 0; 564 fail: 565 close(fd); 566 return ret; 567 } 568 569 static void usage(char *prog) 570 { 571 fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile>\n" 572 " [-a <part number>] [-l <align kB>] [-G <guid>]\n" 573 " [[-t <type> | -T <GPT part type>] [-r] [-N <name>] -p <size>[@<start>]...] \n", prog); 574 exit(EXIT_FAILURE); 575 } 576 577 static guid_t type_to_guid_and_name(unsigned char type, char **name) 578 { 579 guid_t guid = GUID_PARTITION_BASIC_DATA; 580 581 switch (type) { 582 case 0xef: 583 if(*name == NULL) 584 *name = "EFI System Partition"; 585 guid = GUID_PARTITION_SYSTEM; 586 break; 587 case 0x83: 588 guid = GUID_PARTITION_LINUX_FS_GUID; 589 break; 590 case 0x2e: 591 guid = GUID_PARTITION_LINUX_FIT_GUID; 592 break; 593 } 594 595 return guid; 596 } 597 598 int main (int argc, char **argv) 599 { 600 unsigned char type = 0x83; 601 char *p; 602 int ch; 603 int part = 0; 604 char *name = NULL; 605 unsigned short int hybrid = 0, required = 0; 606 uint32_t signature = 0x5452574F; /* 'OWRT' */ 607 guid_t guid = GUID_INIT( signature, 0x2211, 0x4433, \ 608 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00); 609 610 while ((ch = getopt(argc, argv, "h:s:p:a:t:T:o:vnHN:gl:rS:G:")) != -1) { 611 switch (ch) { 612 case 'o': 613 filename = optarg; 614 break; 615 case 'v': 616 verbose++; 617 break; 618 case 'n': 619 ignore_null_sized_partition = true; 620 break; 621 case 'g': 622 use_guid_partition_table = 1; 623 break; 624 case 'H': 625 hybrid = 1; 626 break; 627 case 'h': 628 heads = (int)strtoul(optarg, NULL, 0); 629 break; 630 case 's': 631 sectors = (int)strtoul(optarg, NULL, 0); 632 break; 633 case 'p': 634 if (part > GPT_ENTRY_MAX - 1 || (!use_guid_partition_table && part > 3)) { 635 fputs("Too many partitions\n", stderr); 636 exit(EXIT_FAILURE); 637 } 638 p = strchr(optarg, '@'); 639 if (p) { 640 *(p++) = 0; 641 parts[part].start = to_kbytes(p); 642 } 643 if (!parts[part].has_guid) 644 parts[part].guid = type_to_guid_and_name(type, &name); 645 646 parts[part].size = to_kbytes(optarg); 647 parts[part].required = required; 648 parts[part].name = name; 649 parts[part].hybrid = hybrid; 650 fprintf(stderr, "part %ld %ld\n", parts[part].start, parts[part].size); 651 parts[part++].type = type; 652 /* 653 * reset 'name','required' and 'hybrid' 654 * 'type' is deliberately inherited from the previous delcaration 655 */ 656 name = NULL; 657 required = 0; 658 hybrid = 0; 659 break; 660 case 'N': 661 name = optarg; 662 break; 663 case 'r': 664 required = 1; 665 break; 666 case 't': 667 type = (char)strtoul(optarg, NULL, 16); 668 break; 669 case 'a': 670 active = (int)strtoul(optarg, NULL, 0); 671 break; 672 case 'l': 673 kb_align = (int)strtoul(optarg, NULL, 0) * 2; 674 break; 675 case 'S': 676 signature = strtoul(optarg, NULL, 0); 677 break; 678 case 'T': 679 if (!parse_gpt_parttype(optarg, &parts[part])) { 680 fprintf(stderr, 681 "Invalid GPT partition type \"%s\"\n", 682 optarg); 683 exit(EXIT_FAILURE); 684 } 685 break; 686 case 'G': 687 if (guid_parse(optarg, &guid)) { 688 fputs("Invalid guid string\n", stderr); 689 exit(EXIT_FAILURE); 690 } 691 break; 692 case '?': 693 default: 694 usage(argv[0]); 695 } 696 } 697 argc -= optind; 698 if (argc || (!use_guid_partition_table && ((heads <= 0) || (sectors <= 0))) || !filename) 699 usage(argv[0]); 700 701 if ((use_guid_partition_table && active > GPT_ENTRY_MAX) || 702 (!use_guid_partition_table && active > MBR_ENTRY_MAX) || 703 active < 0) 704 active = 0; 705 706 if (use_guid_partition_table) { 707 heads = 254; 708 sectors = 63; 709 return gen_gptable(signature, guid, part) ? EXIT_FAILURE : EXIT_SUCCESS; 710 } 711 712 return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS; 713 } 714
This page was automatically generated by LXR 0.3.1. • OpenWrt