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