58066fef71038eb239df1553ca3d8ed206b599b1
[project/firmware-utils.git] / src / ptgen.c
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 GPT_HEADER_SIZE 92
86 #define GPT_ENTRY_SIZE 128
87 #define GPT_ENTRY_MAX 128
88 #define GPT_ENTRY_NAME_SIZE 72
89 #define GPT_SIZE GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE
90
91 #define GPT_ATTR_PLAT_REQUIRED BIT(0)
92 #define GPT_ATTR_EFI_IGNORE BIT(1)
93 #define GPT_ATTR_LEGACY_BOOT BIT(2)
94
95 #define GPT_HEADER_SECTOR 1
96 #define GPT_FIRST_ENTRY_SECTOR 2
97
98 #define MBR_ENTRY_MAX 4
99 #define MBR_DISK_SIGNATURE_OFFSET 440
100 #define MBR_PARTITION_ENTRY_OFFSET 446
101 #define MBR_BOOT_SIGNATURE_OFFSET 510
102
103 #define DISK_SECTOR_SIZE 512
104
105 /* Partition table entry */
106 struct pte {
107 uint8_t active;
108 uint8_t chs_start[3];
109 uint8_t type;
110 uint8_t chs_end[3];
111 uint32_t start;
112 uint32_t length;
113 };
114
115 struct partinfo {
116 unsigned long actual_start;
117 unsigned long start;
118 unsigned long size;
119 int type;
120 int hybrid;
121 char *name;
122 short int required;
123 bool has_guid;
124 guid_t guid;
125 uint64_t gattr; /* GPT partition attributes */
126 };
127
128 /* GPT Partition table header */
129 struct gpth {
130 uint64_t signature;
131 uint32_t revision;
132 uint32_t size;
133 uint32_t crc32;
134 uint32_t reserved;
135 uint64_t self;
136 uint64_t alternate;
137 uint64_t first_usable;
138 uint64_t last_usable;
139 guid_t disk_guid;
140 uint64_t first_entry;
141 uint32_t entry_num;
142 uint32_t entry_size;
143 uint32_t entry_crc32;
144 } __attribute__((packed));
145
146 /* GPT Partition table entry */
147 struct gpte {
148 guid_t type;
149 guid_t guid;
150 uint64_t start;
151 uint64_t end;
152 uint64_t attr;
153 char name[GPT_ENTRY_NAME_SIZE];
154 } __attribute__((packed));
155
156
157 int verbose = 0;
158 int active = 1;
159 int heads = -1;
160 int sectors = -1;
161 int kb_align = 0;
162 bool ignore_null_sized_partition = false;
163 bool use_guid_partition_table = false;
164 struct partinfo parts[GPT_ENTRY_MAX];
165 char *filename = NULL;
166
167
168 /*
169 * parse the size argument, which is either
170 * a simple number (K assumed) or
171 * K, M or G
172 *
173 * returns the size in KByte
174 */
175 static long to_kbytes(const char *string)
176 {
177 int exp = 0;
178 long result;
179 char *end;
180
181 result = strtoul(string, &end, 0);
182 switch (tolower(*end)) {
183 case 'k' :
184 case '\0' : exp = 0; break;
185 case 'm' : exp = 1; break;
186 case 'g' : exp = 2; break;
187 default: return 0;
188 }
189
190 if (*end)
191 end++;
192
193 if (*end) {
194 fputs("garbage after end of number\n", stderr);
195 return 0;
196 }
197
198 /* result: number + 1024^(exp) */
199 if (exp == 0)
200 return result;
201 return result * (2 << ((10 * exp) - 1));
202 }
203
204 /* convert the sector number into a CHS value for the partition table */
205 static void to_chs(long sect, unsigned char chs[3])
206 {
207 int c,h,s;
208
209 s = (sect % sectors) + 1;
210 sect = sect / sectors;
211 h = sect % heads;
212 sect = sect / heads;
213 c = sect;
214
215 chs[0] = h;
216 chs[1] = s | ((c >> 2) & 0xC0);
217 chs[2] = c & 0xFF;
218
219 return;
220 }
221
222 /* round the sector number up to the next cylinder */
223 static inline unsigned long round_to_cyl(long sect)
224 {
225 int cyl_size = heads * sectors;
226
227 return sect + cyl_size - (sect % cyl_size);
228 }
229
230 /* round the sector number up to the kb_align boundary */
231 static inline unsigned long round_to_kb(long sect) {
232 return ((sect - 1) / kb_align + 1) * kb_align;
233 }
234
235 /* Compute a CRC for guid partition table */
236 static inline unsigned long gpt_crc32(void *buf, unsigned long len)
237 {
238 return cyg_crc32_accumulate(~0L, buf, len) ^ ~0L;
239 }
240
241 /* Parse a guid string to guid_t struct */
242 static inline int guid_parse(char *buf, guid_t *guid)
243 {
244 char b[4] = {0};
245 char *p = buf;
246 unsigned i = 0;
247 if (strnlen(buf, GUID_STRING_LENGTH) != GUID_STRING_LENGTH)
248 return -1;
249 for (i = 0; i < sizeof(guid_t); i++) {
250 if (*p == '-')
251 p++;
252 if (*p == '\0')
253 return -1;
254 memcpy(b, p, 2);
255 guid->b[i] = strtol(b, 0, 16);
256 p += 2;
257 }
258 swap(guid->b[0], guid->b[3]);
259 swap(guid->b[1], guid->b[2]);
260 swap(guid->b[4], guid->b[5]);
261 swap(guid->b[6], guid->b[7]);
262 return 0;
263 }
264
265 /*
266 * Map GPT partition types to partition GUIDs.
267 * NB: not all GPT partition types have an equivalent MBR type.
268 */
269 static inline bool parse_gpt_parttype(const char *type, struct partinfo *part)
270 {
271 if (!strcmp(type, "cros_kernel")) {
272 part->has_guid = true;
273 part->guid = GUID_PARTITION_CHROME_OS_KERNEL;
274 /* Default attributes: bootable kernel. */
275 part->gattr = (1ULL << 48) | /* priority=1 */
276 (1ULL << 56); /* success=1 */
277 return true;
278 }
279 return false;
280 }
281
282 /* init an utf-16 string from utf-8 string */
283 static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize)
284 {
285 unsigned i, n = 0;
286 for (i = 0; i < bufsize; i++) {
287 if (str[n] == 0x00) {
288 buf[i] = 0x00;
289 return ;
290 } else if ((str[n] & 0x80) == 0x00) {//0xxxxxxx
291 buf[i] = cpu_to_le16(str[n++]);
292 } else if ((str[n] & 0xE0) == 0xC0) {//110xxxxx
293 buf[i] = cpu_to_le16((str[n] & 0x1F) << 6 | (str[n + 1] & 0x3F));
294 n += 2;
295 } else if ((str[n] & 0xF0) == 0xE0) {//1110xxxx
296 buf[i] = cpu_to_le16((str[n] & 0x0F) << 12 | (str[n + 1] & 0x3F) << 6 | (str[n + 2] & 0x3F));
297 n += 3;
298 } else {
299 buf[i] = cpu_to_le16('?');
300 n++;
301 }
302 }
303 }
304
305 /* check the partition sizes and write the partition table */
306 static int gen_ptable(uint32_t signature, int nr)
307 {
308 struct pte pte[MBR_ENTRY_MAX];
309 unsigned long start, len, sect = 0;
310 int i, fd, ret = -1;
311
312 memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
313 for (i = 0; i < nr; i++) {
314 if (!parts[i].size) {
315 if (ignore_null_sized_partition)
316 continue;
317 fprintf(stderr, "Invalid size in partition %d!\n", i);
318 return ret;
319 }
320
321 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
322 pte[i].type = parts[i].type;
323
324 start = sect + sectors;
325 if (parts[i].start != 0) {
326 if (parts[i].start * 2 < start) {
327 fprintf(stderr, "Invalid start %ld for partition %d!\n",
328 parts[i].start, i);
329 return ret;
330 }
331 start = parts[i].start * 2;
332 } else if (kb_align != 0) {
333 start = round_to_kb(start);
334 }
335 pte[i].start = cpu_to_le32(start);
336
337 sect = start + parts[i].size * 2;
338 if (kb_align == 0)
339 sect = round_to_cyl(sect);
340 pte[i].length = cpu_to_le32(len = sect - start);
341
342 to_chs(start, pte[i].chs_start);
343 to_chs(start + len - 1, pte[i].chs_end);
344
345 if (verbose)
346 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n",
347 i,
348 (long)start * DISK_SECTOR_SIZE,
349 (long)(start + len) * DISK_SECTOR_SIZE,
350 (long)len * DISK_SECTOR_SIZE);
351 printf("%ld\n", (long)start * DISK_SECTOR_SIZE);
352 printf("%ld\n", (long)len * DISK_SECTOR_SIZE);
353 }
354
355 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
356 fprintf(stderr, "Can't open output file '%s'\n",filename);
357 return ret;
358 }
359
360 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
361 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
362 fputs("write failed.\n", stderr);
363 goto fail;
364 }
365
366 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
367 if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) {
368 fputs("write failed.\n", stderr);
369 goto fail;
370 }
371 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
372 if (write(fd, "\x55\xaa", 2) != 2) {
373 fputs("write failed.\n", stderr);
374 goto fail;
375 }
376
377 ret = 0;
378 fail:
379 close(fd);
380 return ret;
381 }
382
383 /* check the partition sizes and write the guid partition table */
384 static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr)
385 {
386 struct pte pte[MBR_ENTRY_MAX];
387 struct gpth gpth = {
388 .signature = cpu_to_le64(GPT_SIGNATURE),
389 .revision = cpu_to_le32(GPT_REVISION),
390 .size = cpu_to_le32(GPT_HEADER_SIZE),
391 .self = cpu_to_le64(GPT_HEADER_SECTOR),
392 .first_usable = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
393 .first_entry = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR),
394 .disk_guid = guid,
395 .entry_num = cpu_to_le32(GPT_ENTRY_MAX),
396 .entry_size = cpu_to_le32(GPT_ENTRY_SIZE),
397 };
398 struct gpte gpte[GPT_ENTRY_MAX];
399 uint64_t start, end;
400 uint64_t sect = GPT_SIZE + GPT_FIRST_ENTRY_SECTOR;
401 int fd, ret = -1;
402 unsigned i, pmbr = 1;
403
404 memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
405 memset(gpte, 0, GPT_ENTRY_SIZE * GPT_ENTRY_MAX);
406 for (i = 0; i < nr; i++) {
407 if (!parts[i].size) {
408 if (ignore_null_sized_partition)
409 continue;
410 fprintf(stderr, "Invalid size in partition %d!\n", i);
411 return ret;
412 }
413 start = sect;
414 if (parts[i].start != 0) {
415 if (parts[i].start * 2 < start) {
416 fprintf(stderr, "Invalid start %ld for partition %d!\n",
417 parts[i].start, i);
418 return ret;
419 }
420 start = parts[i].start * 2;
421 } else if (kb_align != 0) {
422 start = round_to_kb(start);
423 }
424 parts[i].actual_start = start;
425 gpte[i].start = cpu_to_le64(start);
426
427 sect = start + parts[i].size * 2;
428 gpte[i].end = cpu_to_le64(sect -1);
429 gpte[i].guid = guid;
430 gpte[i].guid.b[sizeof(guid_t) -1] += i + 1;
431 gpte[i].type = parts[i].guid;
432
433 if (parts[i].hybrid && pmbr < MBR_ENTRY_MAX) {
434 pte[pmbr].active = ((i + 1) == active) ? 0x80 : 0;
435 pte[pmbr].type = parts[i].type;
436 pte[pmbr].start = cpu_to_le32(start);
437 pte[pmbr].length = cpu_to_le32(sect - start);
438 to_chs(start, pte[1].chs_start);
439 to_chs(sect - 1, pte[1].chs_end);
440 pmbr++;
441 }
442 gpte[i].attr = parts[i].gattr;
443
444 if (parts[i].name)
445 init_utf16(parts[i].name, (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t));
446
447 if ((i + 1) == (unsigned)active)
448 gpte[i].attr |= GPT_ATTR_LEGACY_BOOT;
449
450 if (parts[i].required)
451 gpte[i].attr |= GPT_ATTR_PLAT_REQUIRED;
452
453 if (verbose)
454 fprintf(stderr, "Partition %d: start=%" PRIu64 ", end=%" PRIu64 ", size=%" PRIu64 "\n",
455 i,
456 start * DISK_SECTOR_SIZE, sect * DISK_SECTOR_SIZE,
457 (sect - start) * DISK_SECTOR_SIZE);
458 printf("%" PRIu64 "\n", start * DISK_SECTOR_SIZE);
459 printf("%" PRIu64 "\n", (sect - start) * DISK_SECTOR_SIZE);
460 }
461
462 if (parts[0].actual_start > GPT_FIRST_ENTRY_SECTOR + GPT_SIZE) {
463 gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_SIZE);
464 gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64(parts[0].actual_start - 1);
465 gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT;
466 gpte[GPT_ENTRY_MAX - 1].guid = guid;
467 gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX;
468 }
469
470 end = sect + GPT_SIZE;
471
472 pte[0].type = 0xEE;
473 pte[0].start = cpu_to_le32(GPT_HEADER_SECTOR);
474 pte[0].length = cpu_to_le32(end - GPT_HEADER_SECTOR);
475 to_chs(GPT_HEADER_SECTOR, pte[0].chs_start);
476 to_chs(end, pte[0].chs_end);
477
478 gpth.last_usable = cpu_to_le64(end - GPT_SIZE - 1);
479 gpth.alternate = cpu_to_le64(end);
480 gpth.entry_crc32 = cpu_to_le32(gpt_crc32(gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX));
481 gpth.crc32 = cpu_to_le32(gpt_crc32((char *)&gpth, GPT_HEADER_SIZE));
482
483 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
484 fprintf(stderr, "Can't open output file '%s'\n",filename);
485 return ret;
486 }
487
488 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
489 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
490 fputs("write failed.\n", stderr);
491 goto fail;
492 }
493
494 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
495 if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) {
496 fputs("write failed.\n", stderr);
497 goto fail;
498 }
499
500 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
501 if (write(fd, "\x55\xaa", 2) != 2) {
502 fputs("write failed.\n", stderr);
503 goto fail;
504 }
505
506 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
507 fputs("write failed.\n", stderr);
508 goto fail;
509 }
510
511 lseek(fd, GPT_FIRST_ENTRY_SECTOR * DISK_SECTOR_SIZE, SEEK_SET);
512 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
513 fputs("write failed.\n", stderr);
514 goto fail;
515 }
516
517 #ifdef WANT_ALTERNATE_PTABLE
518 /* The alternate partition table (We omit it by default) */
519 swap(gpth.self, gpth.alternate);
520 gpth.first_entry = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
521 gpth.crc32 = 0;
522 gpth.crc32 = cpu_to_le32(gpt_crc32(&gpth, GPT_HEADER_SIZE));
523
524 lseek(fd, end * DISK_SECTOR_SIZE - GPT_ENTRY_SIZE * GPT_ENTRY_MAX, SEEK_SET);
525 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
526 fputs("write failed.\n", stderr);
527 goto fail;
528 }
529
530 lseek(fd, end * DISK_SECTOR_SIZE, SEEK_SET);
531 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
532 fputs("write failed.\n", stderr);
533 goto fail;
534 }
535 lseek(fd, (end + 1) * DISK_SECTOR_SIZE -1, SEEK_SET);
536 if (write(fd, "\x00", 1) != 1) {
537 fputs("write failed.\n", stderr);
538 goto fail;
539 }
540 #endif
541
542 ret = 0;
543 fail:
544 close(fd);
545 return ret;
546 }
547
548 static void usage(char *prog)
549 {
550 fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile>\n"
551 " [-a 0..4] [-l <align kB>] [-G <guid>]\n"
552 " [[-t <type> | -T <GPT part type>] [-r] [-N <name>] -p <size>[@<start>]...] \n", prog);
553 exit(EXIT_FAILURE);
554 }
555
556 static guid_t type_to_guid_and_name(unsigned char type, char **name)
557 {
558 guid_t guid = GUID_PARTITION_BASIC_DATA;
559
560 switch (type) {
561 case 0xef:
562 if(*name == NULL)
563 *name = "EFI System Partition";
564 guid = GUID_PARTITION_SYSTEM;
565 break;
566 case 0x83:
567 guid = GUID_PARTITION_LINUX_FS_GUID;
568 break;
569 case 0x2e:
570 guid = GUID_PARTITION_LINUX_FIT_GUID;
571 break;
572 }
573
574 return guid;
575 }
576
577 int main (int argc, char **argv)
578 {
579 unsigned char type = 0x83;
580 char *p;
581 int ch;
582 int part = 0;
583 char *name = NULL;
584 unsigned short int hybrid = 0, required = 0;
585 uint32_t signature = 0x5452574F; /* 'OWRT' */
586 guid_t guid = GUID_INIT( signature, 0x2211, 0x4433, \
587 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
588
589 while ((ch = getopt(argc, argv, "h:s:p:a:t:T:o:vnHN:gl:rS:G:")) != -1) {
590 switch (ch) {
591 case 'o':
592 filename = optarg;
593 break;
594 case 'v':
595 verbose++;
596 break;
597 case 'n':
598 ignore_null_sized_partition = true;
599 break;
600 case 'g':
601 use_guid_partition_table = 1;
602 break;
603 case 'H':
604 hybrid = 1;
605 break;
606 case 'h':
607 heads = (int)strtoul(optarg, NULL, 0);
608 break;
609 case 's':
610 sectors = (int)strtoul(optarg, NULL, 0);
611 break;
612 case 'p':
613 if (part > GPT_ENTRY_MAX - 1 || (!use_guid_partition_table && part > 3)) {
614 fputs("Too many partitions\n", stderr);
615 exit(EXIT_FAILURE);
616 }
617 p = strchr(optarg, '@');
618 if (p) {
619 *(p++) = 0;
620 parts[part].start = to_kbytes(p);
621 }
622 if (!parts[part].has_guid)
623 parts[part].guid = type_to_guid_and_name(type, &name);
624
625 parts[part].size = to_kbytes(optarg);
626 parts[part].required = required;
627 parts[part].name = name;
628 parts[part].hybrid = hybrid;
629 fprintf(stderr, "part %ld %ld\n", parts[part].start, parts[part].size);
630 parts[part++].type = type;
631 /*
632 * reset 'name','required' and 'hybrid'
633 * 'type' is deliberately inherited from the previous delcaration
634 */
635 name = NULL;
636 required = 0;
637 hybrid = 0;
638 break;
639 case 'N':
640 name = optarg;
641 break;
642 case 'r':
643 required = 1;
644 break;
645 case 't':
646 type = (char)strtoul(optarg, NULL, 16);
647 break;
648 case 'a':
649 active = (int)strtoul(optarg, NULL, 0);
650 if ((active < 0) || (active > 4))
651 active = 0;
652 break;
653 case 'l':
654 kb_align = (int)strtoul(optarg, NULL, 0) * 2;
655 break;
656 case 'S':
657 signature = strtoul(optarg, NULL, 0);
658 break;
659 case 'T':
660 if (!parse_gpt_parttype(optarg, &parts[part])) {
661 fprintf(stderr,
662 "Invalid GPT partition type \"%s\"\n",
663 optarg);
664 exit(EXIT_FAILURE);
665 }
666 break;
667 case 'G':
668 if (guid_parse(optarg, &guid)) {
669 fputs("Invalid guid string\n", stderr);
670 exit(EXIT_FAILURE);
671 }
672 break;
673 case '?':
674 default:
675 usage(argv[0]);
676 }
677 }
678 argc -= optind;
679 if (argc || (!use_guid_partition_table && ((heads <= 0) || (sectors <= 0))) || !filename)
680 usage(argv[0]);
681
682 if (use_guid_partition_table) {
683 heads = 254;
684 sectors = 63;
685 return gen_gptable(signature, guid, part) ? EXIT_FAILURE : EXIT_SUCCESS;
686 }
687
688 return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
689 }