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