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