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