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