firmware-utils: ptgen: add GPT support
[openwrt/staging/stintel.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 typedef struct {
56 uint8_t b[16];
57 } guid_t;
58
59 #define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
60 ((guid_t) \
61 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
62 (b) & 0xff, ((b) >> 8) & 0xff, \
63 (c) & 0xff, ((c) >> 8) & 0xff, \
64 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
65
66 #define GUID_STRING_LENGTH 36
67
68 #define GPT_SIGNATURE 0x5452415020494645ULL
69 #define GPT_REVISION 0x00010000
70
71 #define GUID_PARTITION_SYSTEM \
72 GUID_INIT( 0xC12A7328, 0xF81F, 0x11d2, \
73 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
74
75 #define GUID_PARTITION_BASIC_DATA \
76 GUID_INIT( 0xEBD0A0A2, 0xB9E5, 0x4433, \
77 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
78
79 #define GUID_PARTITION_BIOS_BOOT \
80 GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \
81 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49)
82
83 #define GPT_HEADER_SIZE 92
84 #define GPT_ENTRY_SIZE 128
85 #define GPT_ENTRY_MAX 128
86 #define GPT_ENTRY_NAME_SIZE 72
87
88 #define GPT_HEADER_SECTOR 1
89 #define GPT_FIRST_ENTRY_SECTOR 2
90
91 #define MBR_ENTRY_MAX 4
92 #define MBR_DISK_SIGNATURE_OFFSET 440
93 #define MBR_PARTITION_ENTRY_OFFSET 446
94 #define MBR_BOOT_SIGNATURE_OFFSET 510
95
96 #define DISK_SECTOR_SIZE 512
97
98 /* Partition table entry */
99 struct pte {
100 uint8_t active;
101 uint8_t chs_start[3];
102 uint8_t type;
103 uint8_t chs_end[3];
104 uint32_t start;
105 uint32_t length;
106 };
107
108 struct partinfo {
109 unsigned long size;
110 int type;
111 };
112
113 /* GPT Partition table header */
114 struct gpth {
115 uint64_t signature;
116 uint32_t revision;
117 uint32_t size;
118 uint32_t crc32;
119 uint32_t reserved;
120 uint64_t self;
121 uint64_t alternate;
122 uint64_t first_usable;
123 uint64_t last_usable;
124 guid_t disk_guid;
125 uint64_t first_entry;
126 uint32_t entry_num;
127 uint32_t entry_size;
128 uint32_t entry_crc32;
129 } __attribute__((packed));
130
131 /* GPT Partition table entry */
132 struct gpte {
133 guid_t type;
134 guid_t guid;
135 uint64_t start;
136 uint64_t end;
137 uint64_t attr;
138 char name[GPT_ENTRY_NAME_SIZE];
139 } __attribute__((packed));
140
141
142 int verbose = 0;
143 int active = 1;
144 int heads = -1;
145 int sectors = -1;
146 int kb_align = 0;
147 bool ignore_null_sized_partition = false;
148 bool use_guid_partition_table = false;
149 struct partinfo parts[GPT_ENTRY_MAX];
150 char *filename = NULL;
151
152
153 /*
154 * parse the size argument, which is either
155 * a simple number (K assumed) or
156 * K, M or G
157 *
158 * returns the size in KByte
159 */
160 static long to_kbytes(const char *string)
161 {
162 int exp = 0;
163 long result;
164 char *end;
165
166 result = strtoul(string, &end, 0);
167 switch (tolower(*end)) {
168 case 'k' :
169 case '\0' : exp = 0; break;
170 case 'm' : exp = 1; break;
171 case 'g' : exp = 2; break;
172 default: return 0;
173 }
174
175 if (*end)
176 end++;
177
178 if (*end) {
179 fputs("garbage after end of number\n", stderr);
180 return 0;
181 }
182
183 /* result: number + 1024^(exp) */
184 if (exp == 0)
185 return result;
186 return result * (2 << ((10 * exp) - 1));
187 }
188
189 /* convert the sector number into a CHS value for the partition table */
190 static void to_chs(long sect, unsigned char chs[3])
191 {
192 int c,h,s;
193
194 s = (sect % sectors) + 1;
195 sect = sect / sectors;
196 h = sect % heads;
197 sect = sect / heads;
198 c = sect;
199
200 chs[0] = h;
201 chs[1] = s | ((c >> 2) & 0xC0);
202 chs[2] = c & 0xFF;
203
204 return;
205 }
206
207 /* round the sector number up to the next cylinder */
208 static inline unsigned long round_to_cyl(long sect)
209 {
210 int cyl_size = heads * sectors;
211
212 return sect + cyl_size - (sect % cyl_size);
213 }
214
215 /* round the sector number up to the kb_align boundary */
216 static inline unsigned long round_to_kb(long sect) {
217 return ((sect - 1) / kb_align + 1) * kb_align;
218 }
219
220 /* Compute a CRC for guid partition table */
221 static inline unsigned long gpt_crc32(void *buf, unsigned long len)
222 {
223 return cyg_crc32_accumulate(~0L, buf, len) ^ ~0L;
224 }
225
226 /* Parse a guid string to guid_t struct */
227 static inline int guid_parse(char *buf, guid_t *guid)
228 {
229 char b[4] = {0};
230 char *p = buf;
231 unsigned i = 0;
232 if (strnlen(buf, GUID_STRING_LENGTH) != GUID_STRING_LENGTH)
233 return -1;
234 for (i = 0; i < sizeof(guid_t); i++) {
235 if (*p == '-')
236 p++;
237 if (*p == '\0')
238 return -1;
239 memcpy(b, p, 2);
240 guid->b[i] = strtol(b, 0, 16);
241 p += 2;
242 }
243 swap(guid->b[0], guid->b[3]);
244 swap(guid->b[1], guid->b[2]);
245 swap(guid->b[4], guid->b[5]);
246 swap(guid->b[6], guid->b[7]);
247 return 0;
248 }
249
250 /* init an utf-16 string from utf-8 string */
251 static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize)
252 {
253 unsigned i, n = 0;
254 for (i = 0; i < bufsize; i++) {
255 if (str[n] == 0x00) {
256 buf[i] = 0x00;
257 return ;
258 } else if ((str[n] & 0x80) == 0x00) {//0xxxxxxx
259 buf[i] = cpu_to_le16(str[n++]);
260 } else if ((str[n] & 0xE0) == 0xC0) {//110xxxxx
261 buf[i] = cpu_to_le16((str[n] & 0x1F) << 6 | (str[n + 1] & 0x3F));
262 n += 2;
263 } else if ((str[n] & 0xF0) == 0xE0) {//1110xxxx
264 buf[i] = cpu_to_le16((str[n] & 0x0F) << 12 | (str[n + 1] & 0x3F) << 6 | (str[n + 2] & 0x3F));
265 n += 3;
266 } else {
267 buf[i] = cpu_to_le16('?');
268 n++;
269 }
270 }
271 }
272
273 /* check the partition sizes and write the partition table */
274 static int gen_ptable(uint32_t signature, int nr)
275 {
276 struct pte pte[MBR_ENTRY_MAX];
277 unsigned long start, len, sect = 0;
278 int i, fd, ret = -1;
279
280 memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
281 for (i = 0; i < nr; i++) {
282 if (!parts[i].size) {
283 if (ignore_null_sized_partition)
284 continue;
285 fprintf(stderr, "Invalid size in partition %d!\n", i);
286 return ret;
287 }
288
289 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
290 pte[i].type = parts[i].type;
291
292 start = sect + sectors;
293 if (kb_align != 0)
294 start = round_to_kb(start);
295 pte[i].start = cpu_to_le32(start);
296
297 sect = start + parts[i].size * 2;
298 if (kb_align == 0)
299 sect = round_to_cyl(sect);
300 pte[i].length = cpu_to_le32(len = sect - start);
301
302 to_chs(start, pte[i].chs_start);
303 to_chs(start + len - 1, pte[i].chs_end);
304
305 if (verbose)
306 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n",
307 i,
308 (long)start * DISK_SECTOR_SIZE,
309 (long)(start + len) * DISK_SECTOR_SIZE,
310 (long)len * DISK_SECTOR_SIZE);
311 printf("%ld\n", (long)start * DISK_SECTOR_SIZE);
312 printf("%ld\n", (long)len * DISK_SECTOR_SIZE);
313 }
314
315 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
316 fprintf(stderr, "Can't open output file '%s'\n",filename);
317 return ret;
318 }
319
320 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
321 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
322 fputs("write failed.\n", stderr);
323 goto fail;
324 }
325
326 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
327 if (write(fd, pte, sizeof(struct pte) * MBR_ENTRY_MAX) != sizeof(struct pte) * MBR_ENTRY_MAX) {
328 fputs("write failed.\n", stderr);
329 goto fail;
330 }
331 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
332 if (write(fd, "\x55\xaa", 2) != 2) {
333 fputs("write failed.\n", stderr);
334 goto fail;
335 }
336
337 ret = 0;
338 fail:
339 close(fd);
340 return ret;
341 }
342
343 /* check the partition sizes and write the guid partition table */
344 static int gen_gptable(uint32_t signature, guid_t guid, unsigned nr)
345 {
346 struct pte pte;
347 struct gpth gpth = {
348 .signature = cpu_to_le64(GPT_SIGNATURE),
349 .revision = cpu_to_le32(GPT_REVISION),
350 .size = cpu_to_le32(GPT_HEADER_SIZE),
351 .self = cpu_to_le64(GPT_HEADER_SECTOR),
352 .first_usable = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
353 .first_entry = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR),
354 .disk_guid = guid,
355 .entry_num = cpu_to_le32(GPT_ENTRY_MAX),
356 .entry_size = cpu_to_le32(GPT_ENTRY_SIZE),
357 };
358 struct gpte gpte[GPT_ENTRY_MAX];
359 uint64_t start, end, sect = 0;
360 int fd, ret = -1;
361 unsigned i;
362
363 memset(gpte, 0, GPT_ENTRY_SIZE * GPT_ENTRY_MAX);
364 for (i = 0; i < nr; i++) {
365 if (!parts[i].size) {
366 if (ignore_null_sized_partition)
367 continue;
368 fprintf(stderr, "Invalid size in partition %d!\n", i);
369 return ret;
370 }
371 start = sect + sectors;
372 if (kb_align != 0)
373 start = round_to_kb(start);
374 gpte[i].start = cpu_to_le64(start);
375
376 sect = start + parts[i].size * 2;
377 if (kb_align == 0)
378 sect = round_to_cyl(sect);
379 gpte[i].end = cpu_to_le64(sect -1);
380 gpte[i].guid = guid;
381 gpte[i].guid.b[sizeof(guid_t) -1] += i + 1;
382 if (parts[i].type == 0xEF || (i + 1) == (unsigned)active) {
383 gpte[i].type = GUID_PARTITION_SYSTEM;
384 init_utf16("EFI System Partition", (uint16_t *)gpte[i].name, GPT_ENTRY_NAME_SIZE / sizeof(uint16_t));
385 } else {
386 gpte[i].type = GUID_PARTITION_BASIC_DATA;
387 }
388
389 if (verbose)
390 fprintf(stderr, "Partition %d: start=%" PRIu64 ", end=%" PRIu64 ", size=%" PRIu64 "\n",
391 i,
392 start * DISK_SECTOR_SIZE, sect * DISK_SECTOR_SIZE,
393 (sect - start) * DISK_SECTOR_SIZE);
394 printf("%" PRIu64 "\n", start * DISK_SECTOR_SIZE);
395 printf("%" PRIu64 "\n", (sect - start) * DISK_SECTOR_SIZE);
396 }
397
398 gpte[GPT_ENTRY_MAX - 1].start = cpu_to_le64(GPT_FIRST_ENTRY_SECTOR + GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE);
399 gpte[GPT_ENTRY_MAX - 1].end = cpu_to_le64((kb_align ? round_to_kb(sectors) : (unsigned long)sectors) - 1);
400 gpte[GPT_ENTRY_MAX - 1].type = GUID_PARTITION_BIOS_BOOT;
401 gpte[GPT_ENTRY_MAX - 1].guid = guid;
402 gpte[GPT_ENTRY_MAX - 1].guid.b[sizeof(guid_t) -1] += GPT_ENTRY_MAX;
403
404 end = sect + sectors - 1;
405
406 pte.type = 0xEE;
407 pte.start = cpu_to_le32(GPT_HEADER_SECTOR);
408 pte.length = cpu_to_le32(end);
409 to_chs(GPT_HEADER_SECTOR, pte.chs_start);
410 to_chs(end, pte.chs_end);
411
412 gpth.last_usable = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE - 1);
413 gpth.alternate = cpu_to_le64(end);
414 gpth.entry_crc32 = cpu_to_le32(gpt_crc32(gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX));
415 gpth.crc32 = cpu_to_le32(gpt_crc32((char *)&gpth, GPT_HEADER_SIZE));
416
417 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
418 fprintf(stderr, "Can't open output file '%s'\n",filename);
419 return ret;
420 }
421
422 lseek(fd, MBR_DISK_SIGNATURE_OFFSET, SEEK_SET);
423 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
424 fputs("write failed.\n", stderr);
425 goto fail;
426 }
427
428 lseek(fd, MBR_PARTITION_ENTRY_OFFSET, SEEK_SET);
429 if (write(fd, &pte, sizeof(struct pte)) != sizeof(struct pte)) {
430 fputs("write failed.\n", stderr);
431 goto fail;
432 }
433
434 lseek(fd, MBR_BOOT_SIGNATURE_OFFSET, SEEK_SET);
435 if (write(fd, "\x55\xaa", 2) != 2) {
436 fputs("write failed.\n", stderr);
437 goto fail;
438 }
439
440 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
441 fputs("write failed.\n", stderr);
442 goto fail;
443 }
444
445 lseek(fd, GPT_FIRST_ENTRY_SECTOR * DISK_SECTOR_SIZE, SEEK_SET);
446 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
447 fputs("write failed.\n", stderr);
448 goto fail;
449 }
450
451 #ifdef WANT_ALTERNATE_PTABLE
452 /* The alternate partition table (We omit it by default) */
453 swap(gpth.self, gpth.alternate);
454 gpth.first_entry = cpu_to_le64(end - GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE),
455 gpth.crc32 = 0;
456 gpth.crc32 = cpu_to_le32(gpt_crc32(&gpth, GPT_HEADER_SIZE));
457
458 lseek(fd, end * DISK_SECTOR_SIZE - GPT_ENTRY_SIZE * GPT_ENTRY_MAX, SEEK_SET);
459 if (write(fd, &gpte, GPT_ENTRY_SIZE * GPT_ENTRY_MAX) != GPT_ENTRY_SIZE * GPT_ENTRY_MAX) {
460 fputs("write failed.\n", stderr);
461 goto fail;
462 }
463
464 lseek(fd, end * DISK_SECTOR_SIZE, SEEK_SET);
465 if (write(fd, &gpth, GPT_HEADER_SIZE) != GPT_HEADER_SIZE) {
466 fputs("write failed.\n", stderr);
467 goto fail;
468 }
469 lseek(fd, (end + 1) * DISK_SECTOR_SIZE -1, SEEK_SET);
470 if (write(fd, "\x00", 1) != 1) {
471 fputs("write failed.\n", stderr);
472 goto fail;
473 }
474 #endif
475
476 ret = 0;
477 fail:
478 close(fd);
479 return ret;
480 }
481
482 static void usage(char *prog)
483 {
484 fprintf(stderr, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [-G <guid>] [[-t <type>] -p <size>...] \n", prog);
485 exit(EXIT_FAILURE);
486 }
487
488 int main (int argc, char **argv)
489 {
490 unsigned char type = 0x83;
491 int ch;
492 int part = 0;
493 uint32_t signature = 0x5452574F; /* 'OWRT' */
494 guid_t guid = GUID_INIT( signature, 0x2211, 0x4433, \
495 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
496
497 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vngl:S:G:")) != -1) {
498 switch (ch) {
499 case 'o':
500 filename = optarg;
501 break;
502 case 'v':
503 verbose++;
504 break;
505 case 'n':
506 ignore_null_sized_partition = true;
507 break;
508 case 'g':
509 use_guid_partition_table = 1;
510 break;
511 case 'h':
512 heads = (int)strtoul(optarg, NULL, 0);
513 break;
514 case 's':
515 sectors = (int)strtoul(optarg, NULL, 0);
516 break;
517 case 'p':
518 if (part > GPT_ENTRY_MAX - 1 || (!use_guid_partition_table && part > 3)) {
519 fputs("Too many partitions\n", stderr);
520 exit(EXIT_FAILURE);
521 }
522 parts[part].size = to_kbytes(optarg);
523 parts[part++].type = type;
524 break;
525 case 't':
526 type = (char)strtoul(optarg, NULL, 16);
527 break;
528 case 'a':
529 active = (int)strtoul(optarg, NULL, 0);
530 if ((active < 0) || (active > 4))
531 active = 0;
532 break;
533 case 'l':
534 kb_align = (int)strtoul(optarg, NULL, 0) * 2;
535 break;
536 case 'S':
537 signature = strtoul(optarg, NULL, 0);
538 break;
539 case 'G':
540 if (guid_parse(optarg, &guid)) {
541 fputs("Invalid guid string\n", stderr);
542 exit(EXIT_FAILURE);
543 }
544 break;
545 case '?':
546 default:
547 usage(argv[0]);
548 }
549 }
550 argc -= optind;
551 if (argc || (heads <= 0) || (sectors <= 0) || !filename)
552 usage(argv[0]);
553
554 if (use_guid_partition_table)
555 return gen_gptable(signature, guid, part) ? EXIT_FAILURE : EXIT_SUCCESS;
556
557 return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
558 }