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