1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ptgen - partition table generator
4 * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
7 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
9 * UUID/GUID definition stolen from kernel/include/uapi/linux/uuid.h
10 * Copyright (C) 2010, Intel Corp. Huang Ying <ying.huang@intel.com>
14 #include <sys/types.h>
28 #if __BYTE_ORDER == __BIG_ENDIAN
29 #define cpu_to_le16(x) bswap_16(x)
30 #define cpu_to_le32(x) bswap_32(x)
31 #define cpu_to_le64(x) bswap_64(x)
32 #elif __BYTE_ORDER == __LITTLE_ENDIAN
33 #define cpu_to_le16(x) (x)
34 #define cpu_to_le32(x) (x)
35 #define cpu_to_le64(x) (x)
37 #error unknown endianness!
41 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
43 #define BIT(_x) (1UL << (_x))
49 #define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
51 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
52 (b) & 0xff, ((b) >> 8) & 0xff, \
53 (c) & 0xff, ((c) >> 8) & 0xff, \
54 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
56 #define GUID_STRING_LENGTH 36
58 #define GPT_SIGNATURE 0x5452415020494645ULL
59 #define GPT_REVISION 0x00010000
61 #define GUID_PARTITION_SYSTEM \
62 GUID_INIT( 0xC12A7328, 0xF81F, 0x11d2, \
63 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
65 #define GUID_PARTITION_BASIC_DATA \
66 GUID_INIT( 0xEBD0A0A2, 0xB9E5, 0x4433, \
67 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
69 #define GUID_PARTITION_BIOS_BOOT \
70 GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \
71 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49)
73 #define GUID_PARTITION_CHROME_OS_KERNEL \
74 GUID_INIT( 0xFE3A2A5D, 0x4F32, 0x41A7, \
75 0xB7, 0x25, 0xAC, 0xCC, 0x32, 0x85, 0xA3, 0x09)
77 #define GUID_PARTITION_LINUX_FIT_GUID \
78 GUID_INIT( 0xcae9be83, 0xb15f, 0x49cc, \
79 0x86, 0x3f, 0x08, 0x1b, 0x74, 0x4a, 0x2d, 0x93)
81 #define GUID_PARTITION_LINUX_FS_GUID \
82 GUID_INIT( 0x0fc63daf, 0x8483, 0x4772, \
83 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4)
85 #define GUID_PARTITION_SIFIVE_SPL \
86 GUID_INIT( 0x5b193300, 0xfc78, 0x40cd, \
87 0x80, 0x02, 0xe8, 0x6c, 0x45, 0x58, 0x0b, 0x47)
89 #define GUID_PARTITION_SIFIVE_UBOOT \
90 GUID_INIT( 0x2e54b353, 0x1271, 0x4842, \
91 0x80, 0x6f, 0xe4, 0x36, 0xd6, 0xaf, 0x69, 0x85)
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
99 #define GPT_ATTR_PLAT_REQUIRED BIT(0)
100 #define GPT_ATTR_EFI_IGNORE BIT(1)
101 #define GPT_ATTR_LEGACY_BOOT BIT(2)
103 #define GPT_HEADER_SECTOR 1
104 #define GPT_FIRST_ENTRY_SECTOR 2
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
111 #define DISK_SECTOR_SIZE 512
113 /* Partition table entry */
116 uint8_t chs_start
[3];
124 unsigned long actual_start
;
133 uint64_t gattr
; /* GPT partition attributes */
136 /* GPT Partition table header */
145 uint64_t first_usable
;
146 uint64_t last_usable
;
148 uint64_t first_entry
;
151 uint32_t entry_crc32
;
152 } __attribute__((packed
));
154 /* GPT Partition table entry */
161 char name
[GPT_ENTRY_NAME_SIZE
];
162 } __attribute__((packed
));
170 bool ignore_null_sized_partition
= false;
171 bool use_guid_partition_table
= false;
172 struct partinfo parts
[GPT_ENTRY_MAX
];
173 char *filename
= NULL
;
177 * parse the size argument, which is either
178 * a simple number (K assumed) or
181 * returns the size in KByte
183 static long to_kbytes(const char *string
)
189 result
= strtoul(string
, &end
, 0);
190 switch (tolower(*end
)) {
192 case '\0' : exp
= 0; break;
193 case 'm' : exp
= 1; break;
194 case 'g' : exp
= 2; break;
202 fputs("garbage after end of number\n", stderr
);
206 /* result: number + 1024^(exp) */
209 return result
* (2 << ((10 * exp
) - 1));
212 /* convert the sector number into a CHS value for the partition table */
213 static void to_chs(long sect
, unsigned char chs
[3])
217 s
= (sect
% sectors
) + 1;
218 sect
= sect
/ sectors
;
224 chs
[1] = s
| ((c
>> 2) & 0xC0);
230 /* round the sector number up to the next cylinder */
231 static inline unsigned long round_to_cyl(long sect
)
233 int cyl_size
= heads
* sectors
;
235 return sect
+ cyl_size
- (sect
% cyl_size
);
238 /* round the sector number up to the kb_align boundary */
239 static inline unsigned long round_to_kb(long sect
) {
240 return ((sect
- 1) / kb_align
+ 1) * kb_align
;
243 /* Compute a CRC for guid partition table */
244 static inline unsigned long gpt_crc32(void *buf
, unsigned long len
)
246 return cyg_crc32_accumulate(~0L, buf
, len
) ^ ~0L;
249 /* Parse a guid string to guid_t struct */
250 static inline int guid_parse(char *buf
, guid_t
*guid
)
255 if (strnlen(buf
, GUID_STRING_LENGTH
) != GUID_STRING_LENGTH
)
257 for (i
= 0; i
< sizeof(guid_t
); i
++) {
263 guid
->b
[i
] = strtol(b
, 0, 16);
266 swap(guid
->b
[0], guid
->b
[3]);
267 swap(guid
->b
[1], guid
->b
[2]);
268 swap(guid
->b
[4], guid
->b
[5]);
269 swap(guid
->b
[6], guid
->b
[7]);
274 * Map GPT partition types to partition GUIDs.
275 * NB: not all GPT partition types have an equivalent MBR type.
277 static inline bool parse_gpt_parttype(const char *type
, struct partinfo
*part
)
279 if (!strcmp(type
, "cros_kernel")) {
280 part
->has_guid
= true;
281 part
->guid
= GUID_PARTITION_CHROME_OS_KERNEL
;
282 /* Default attributes: bootable kernel. */
283 part
->gattr
= (1ULL << 48) | /* priority=1 */
284 (1ULL << 56); /* success=1 */
288 if (!strcmp(type
, "sifiveu_spl")) {
289 part
->has_guid
= true;
290 part
->guid
= GUID_PARTITION_SIFIVE_SPL
;
294 if (!strcmp(type
, "sifiveu_uboot")) {
295 part
->has_guid
= true;
296 part
->guid
= GUID_PARTITION_SIFIVE_UBOOT
;
303 /* init an utf-16 string from utf-8 string */
304 static inline void init_utf16(char *str
, uint16_t *buf
, unsigned bufsize
)
307 for (i
= 0; i
< bufsize
; i
++) {
308 if (str
[n
] == 0x00) {
311 } else if ((str
[n
] & 0x80) == 0x00) {//0xxxxxxx
312 buf
[i
] = cpu_to_le16(str
[n
++]);
313 } else if ((str
[n
] & 0xE0) == 0xC0) {//110xxxxx
314 buf
[i
] = cpu_to_le16((str
[n
] & 0x1F) << 6 | (str
[n
+ 1] & 0x3F));
316 } else if ((str
[n
] & 0xF0) == 0xE0) {//1110xxxx
317 buf
[i
] = cpu_to_le16((str
[n
] & 0x0F) << 12 | (str
[n
+ 1] & 0x3F) << 6 | (str
[n
+ 2] & 0x3F));
320 buf
[i
] = cpu_to_le16('?');
326 /* check the partition sizes and write the partition table */
327 static int gen_ptable(uint32_t signature
, int nr
)
329 struct pte pte
[MBR_ENTRY_MAX
];
330 unsigned long start
, len
, sect
= 0;
333 memset(pte
, 0, sizeof(struct pte
) * MBR_ENTRY_MAX
);
334 for (i
= 0; i
< nr
; i
++) {
335 if (!parts
[i
].size
) {
336 if (ignore_null_sized_partition
)
338 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
342 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
343 pte
[i
].type
= parts
[i
].type
;
345 start
= sect
+ sectors
;
346 if (parts
[i
].start
!= 0) {
347 if (parts
[i
].start
* 2 < start
) {
348 fprintf(stderr
, "Invalid start %ld for partition %d!\n",
352 start
= parts
[i
].start
* 2;
353 } else if (kb_align
!= 0) {
354 start
= round_to_kb(start
);
356 pte
[i
].start
= cpu_to_le32(start
);
358 sect
= start
+ parts
[i
].size
* 2;
360 sect
= round_to_cyl(sect
);
361 pte
[i
].length
= cpu_to_le32(len
= sect
- start
);
363 to_chs(start
, pte
[i
].chs_start
);
364 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
367 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n",
369 (long)start
* DISK_SECTOR_SIZE
,
370 (long)(start
+ len
) * DISK_SECTOR_SIZE
,
371 (long)len
* DISK_SECTOR_SIZE
);
372 printf("%ld\n", (long)start
* DISK_SECTOR_SIZE
);
373 printf("%ld\n", (long)len
* DISK_SECTOR_SIZE
);
376 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
377 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
381 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
382 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
383 fputs("write failed.\n", stderr
);
387 lseek(fd
, MBR_PARTITION_ENTRY_OFFSET
, SEEK_SET
);
388 if (write(fd
, pte
, sizeof(struct pte
) * MBR_ENTRY_MAX
) != sizeof(struct pte
) * MBR_ENTRY_MAX
) {
389 fputs("write failed.\n", stderr
);
392 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
393 if (write(fd
, "\x55\xaa", 2) != 2) {
394 fputs("write failed.\n", stderr
);
404 /* check the partition sizes and write the guid partition table */
405 static int gen_gptable(uint32_t signature
, guid_t guid
, unsigned nr
)
407 struct pte pte
[MBR_ENTRY_MAX
];
409 .signature
= cpu_to_le64(GPT_SIGNATURE
),
410 .revision
= cpu_to_le32(GPT_REVISION
),
411 .size
= cpu_to_le32(GPT_HEADER_SIZE
),
412 .self
= cpu_to_le64(GPT_HEADER_SECTOR
),
413 .first_usable
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
+ GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
/ DISK_SECTOR_SIZE
),
414 .first_entry
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
),
416 .entry_num
= cpu_to_le32(GPT_ENTRY_MAX
),
417 .entry_size
= cpu_to_le32(GPT_ENTRY_SIZE
),
419 struct gpte gpte
[GPT_ENTRY_MAX
];
421 uint64_t sect
= GPT_SIZE
+ GPT_FIRST_ENTRY_SECTOR
;
423 unsigned i
, pmbr
= 1;
425 memset(pte
, 0, sizeof(struct pte
) * MBR_ENTRY_MAX
);
426 memset(gpte
, 0, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
);
427 for (i
= 0; i
< nr
; i
++) {
428 if (!parts
[i
].size
) {
429 if (ignore_null_sized_partition
)
431 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
435 if (parts
[i
].start
!= 0) {
436 if (parts
[i
].start
* 2 < start
) {
437 fprintf(stderr
, "Invalid start %ld for partition %d!\n",
441 start
= parts
[i
].start
* 2;
442 } else if (kb_align
!= 0) {
443 start
= round_to_kb(start
);
445 parts
[i
].actual_start
= start
;
446 gpte
[i
].start
= cpu_to_le64(start
);
448 sect
= start
+ parts
[i
].size
* 2;
449 gpte
[i
].end
= cpu_to_le64(sect
-1);
451 gpte
[i
].guid
.b
[sizeof(guid_t
) -1] += i
+ 1;
452 gpte
[i
].type
= parts
[i
].guid
;
454 if (parts
[i
].hybrid
&& pmbr
< MBR_ENTRY_MAX
) {
455 pte
[pmbr
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
456 pte
[pmbr
].type
= parts
[i
].type
;
457 pte
[pmbr
].start
= cpu_to_le32(start
);
458 pte
[pmbr
].length
= cpu_to_le32(sect
- start
);
459 to_chs(start
, pte
[1].chs_start
);
460 to_chs(sect
- 1, pte
[1].chs_end
);
463 gpte
[i
].attr
= parts
[i
].gattr
;
466 init_utf16(parts
[i
].name
, (uint16_t *)gpte
[i
].name
, GPT_ENTRY_NAME_SIZE
/ sizeof(uint16_t));
468 if ((i
+ 1) == (unsigned)active
)
469 gpte
[i
].attr
|= GPT_ATTR_LEGACY_BOOT
;
471 if (parts
[i
].required
)
472 gpte
[i
].attr
|= GPT_ATTR_PLAT_REQUIRED
;
475 fprintf(stderr
, "Partition %d: start=%" PRIu64
", end=%" PRIu64
", size=%" PRIu64
"\n",
477 start
* DISK_SECTOR_SIZE
, sect
* DISK_SECTOR_SIZE
,
478 (sect
- start
) * DISK_SECTOR_SIZE
);
479 printf("%" PRIu64
"\n", start
* DISK_SECTOR_SIZE
);
480 printf("%" PRIu64
"\n", (sect
- start
) * DISK_SECTOR_SIZE
);
483 if (parts
[0].actual_start
> GPT_FIRST_ENTRY_SECTOR
+ GPT_SIZE
) {
484 gpte
[GPT_ENTRY_MAX
- 1].start
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
+ GPT_SIZE
);
485 gpte
[GPT_ENTRY_MAX
- 1].end
= cpu_to_le64(parts
[0].actual_start
- 1);
486 gpte
[GPT_ENTRY_MAX
- 1].type
= GUID_PARTITION_BIOS_BOOT
;
487 gpte
[GPT_ENTRY_MAX
- 1].guid
= guid
;
488 gpte
[GPT_ENTRY_MAX
- 1].guid
.b
[sizeof(guid_t
) -1] += GPT_ENTRY_MAX
;
491 end
= sect
+ GPT_SIZE
;
494 pte
[0].start
= cpu_to_le32(GPT_HEADER_SECTOR
);
495 pte
[0].length
= cpu_to_le32(end
- GPT_HEADER_SECTOR
);
496 to_chs(GPT_HEADER_SECTOR
, pte
[0].chs_start
);
497 to_chs(end
, pte
[0].chs_end
);
499 gpth
.last_usable
= cpu_to_le64(end
- GPT_SIZE
- 1);
500 gpth
.alternate
= cpu_to_le64(end
);
501 gpth
.entry_crc32
= cpu_to_le32(gpt_crc32(gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
));
502 gpth
.crc32
= cpu_to_le32(gpt_crc32((char *)&gpth
, GPT_HEADER_SIZE
));
504 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
505 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
509 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
510 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
511 fputs("write failed.\n", stderr
);
515 lseek(fd
, MBR_PARTITION_ENTRY_OFFSET
, SEEK_SET
);
516 if (write(fd
, pte
, sizeof(struct pte
) * MBR_ENTRY_MAX
) != sizeof(struct pte
) * MBR_ENTRY_MAX
) {
517 fputs("write failed.\n", stderr
);
521 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
522 if (write(fd
, "\x55\xaa", 2) != 2) {
523 fputs("write failed.\n", stderr
);
527 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
528 fputs("write failed.\n", stderr
);
532 lseek(fd
, GPT_FIRST_ENTRY_SECTOR
* DISK_SECTOR_SIZE
, SEEK_SET
);
533 if (write(fd
, &gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) != GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) {
534 fputs("write failed.\n", stderr
);
538 #ifdef WANT_ALTERNATE_PTABLE
539 /* The alternate partition table (We omit it by default) */
540 swap(gpth
.self
, gpth
.alternate
);
541 gpth
.first_entry
= cpu_to_le64(end
- GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
/ DISK_SECTOR_SIZE
),
543 gpth
.crc32
= cpu_to_le32(gpt_crc32(&gpth
, GPT_HEADER_SIZE
));
545 lseek(fd
, end
* DISK_SECTOR_SIZE
- GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
, SEEK_SET
);
546 if (write(fd
, &gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) != GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) {
547 fputs("write failed.\n", stderr
);
551 lseek(fd
, end
* DISK_SECTOR_SIZE
, SEEK_SET
);
552 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
553 fputs("write failed.\n", stderr
);
556 lseek(fd
, (end
+ 1) * DISK_SECTOR_SIZE
-1, SEEK_SET
);
557 if (write(fd
, "\x00", 1) != 1) {
558 fputs("write failed.\n", stderr
);
569 static void usage(char *prog
)
571 fprintf(stderr
, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile>\n"
572 " [-a <part number>] [-l <align kB>] [-G <guid>]\n"
573 " [[-t <type> | -T <GPT part type>] [-r] [-N <name>] -p <size>[@<start>]...] \n", prog
);
577 static guid_t
type_to_guid_and_name(unsigned char type
, char **name
)
579 guid_t guid
= GUID_PARTITION_BASIC_DATA
;
584 *name
= "EFI System Partition";
585 guid
= GUID_PARTITION_SYSTEM
;
588 guid
= GUID_PARTITION_LINUX_FS_GUID
;
591 guid
= GUID_PARTITION_LINUX_FIT_GUID
;
598 int main (int argc
, char **argv
)
600 unsigned char type
= 0x83;
605 unsigned short int hybrid
= 0, required
= 0;
606 uint32_t signature
= 0x5452574F; /* 'OWRT' */
607 guid_t guid
= GUID_INIT( signature
, 0x2211, 0x4433, \
608 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
610 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:T:o:vnHN:gl:rS:G:")) != -1) {
619 ignore_null_sized_partition
= true;
622 use_guid_partition_table
= 1;
628 heads
= (int)strtoul(optarg
, NULL
, 0);
631 sectors
= (int)strtoul(optarg
, NULL
, 0);
634 if (part
> GPT_ENTRY_MAX
- 1 || (!use_guid_partition_table
&& part
> 3)) {
635 fputs("Too many partitions\n", stderr
);
638 p
= strchr(optarg
, '@');
641 parts
[part
].start
= to_kbytes(p
);
643 if (!parts
[part
].has_guid
)
644 parts
[part
].guid
= type_to_guid_and_name(type
, &name
);
646 parts
[part
].size
= to_kbytes(optarg
);
647 parts
[part
].required
= required
;
648 parts
[part
].name
= name
;
649 parts
[part
].hybrid
= hybrid
;
650 fprintf(stderr
, "part %ld %ld\n", parts
[part
].start
, parts
[part
].size
);
651 parts
[part
++].type
= type
;
653 * reset 'name','required' and 'hybrid'
654 * 'type' is deliberately inherited from the previous delcaration
667 type
= (char)strtoul(optarg
, NULL
, 16);
670 active
= (int)strtoul(optarg
, NULL
, 0);
673 kb_align
= (int)strtoul(optarg
, NULL
, 0) * 2;
676 signature
= strtoul(optarg
, NULL
, 0);
679 if (!parse_gpt_parttype(optarg
, &parts
[part
])) {
681 "Invalid GPT partition type \"%s\"\n",
687 if (guid_parse(optarg
, &guid
)) {
688 fputs("Invalid guid string\n", stderr
);
698 if (argc
|| (!use_guid_partition_table
&& ((heads
<= 0) || (sectors
<= 0))) || !filename
)
701 if ((use_guid_partition_table
&& active
> GPT_ENTRY_MAX
) ||
702 (!use_guid_partition_table
&& active
> MBR_ENTRY_MAX
) ||
706 if (use_guid_partition_table
) {
709 return gen_gptable(signature
, guid
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
712 return gen_ptable(signature
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;