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 GPT_HEADER_SIZE 92
86 #define GPT_ENTRY_SIZE 128
87 #define GPT_ENTRY_MAX 128
88 #define GPT_ENTRY_NAME_SIZE 72
89 #define GPT_SIZE GPT_ENTRY_SIZE * GPT_ENTRY_MAX / DISK_SECTOR_SIZE
91 #define GPT_ATTR_PLAT_REQUIRED BIT(0)
92 #define GPT_ATTR_EFI_IGNORE BIT(1)
93 #define GPT_ATTR_LEGACY_BOOT BIT(2)
95 #define GPT_HEADER_SECTOR 1
96 #define GPT_FIRST_ENTRY_SECTOR 2
98 #define MBR_ENTRY_MAX 4
99 #define MBR_DISK_SIGNATURE_OFFSET 440
100 #define MBR_PARTITION_ENTRY_OFFSET 446
101 #define MBR_BOOT_SIGNATURE_OFFSET 510
103 #define DISK_SECTOR_SIZE 512
105 /* Partition table entry */
108 uint8_t chs_start
[3];
116 unsigned long actual_start
;
125 uint64_t gattr
; /* GPT partition attributes */
128 /* GPT Partition table header */
137 uint64_t first_usable
;
138 uint64_t last_usable
;
140 uint64_t first_entry
;
143 uint32_t entry_crc32
;
144 } __attribute__((packed
));
146 /* GPT Partition table entry */
153 char name
[GPT_ENTRY_NAME_SIZE
];
154 } __attribute__((packed
));
162 bool ignore_null_sized_partition
= false;
163 bool use_guid_partition_table
= false;
164 struct partinfo parts
[GPT_ENTRY_MAX
];
165 char *filename
= NULL
;
169 * parse the size argument, which is either
170 * a simple number (K assumed) or
173 * returns the size in KByte
175 static long to_kbytes(const char *string
)
181 result
= strtoul(string
, &end
, 0);
182 switch (tolower(*end
)) {
184 case '\0' : exp
= 0; break;
185 case 'm' : exp
= 1; break;
186 case 'g' : exp
= 2; break;
194 fputs("garbage after end of number\n", stderr
);
198 /* result: number + 1024^(exp) */
201 return result
* (2 << ((10 * exp
) - 1));
204 /* convert the sector number into a CHS value for the partition table */
205 static void to_chs(long sect
, unsigned char chs
[3])
209 s
= (sect
% sectors
) + 1;
210 sect
= sect
/ sectors
;
216 chs
[1] = s
| ((c
>> 2) & 0xC0);
222 /* round the sector number up to the next cylinder */
223 static inline unsigned long round_to_cyl(long sect
)
225 int cyl_size
= heads
* sectors
;
227 return sect
+ cyl_size
- (sect
% cyl_size
);
230 /* round the sector number up to the kb_align boundary */
231 static inline unsigned long round_to_kb(long sect
) {
232 return ((sect
- 1) / kb_align
+ 1) * kb_align
;
235 /* Compute a CRC for guid partition table */
236 static inline unsigned long gpt_crc32(void *buf
, unsigned long len
)
238 return cyg_crc32_accumulate(~0L, buf
, len
) ^ ~0L;
241 /* Parse a guid string to guid_t struct */
242 static inline int guid_parse(char *buf
, guid_t
*guid
)
247 if (strnlen(buf
, GUID_STRING_LENGTH
) != GUID_STRING_LENGTH
)
249 for (i
= 0; i
< sizeof(guid_t
); i
++) {
255 guid
->b
[i
] = strtol(b
, 0, 16);
258 swap(guid
->b
[0], guid
->b
[3]);
259 swap(guid
->b
[1], guid
->b
[2]);
260 swap(guid
->b
[4], guid
->b
[5]);
261 swap(guid
->b
[6], guid
->b
[7]);
266 * Map GPT partition types to partition GUIDs.
267 * NB: not all GPT partition types have an equivalent MBR type.
269 static inline bool parse_gpt_parttype(const char *type
, struct partinfo
*part
)
271 if (!strcmp(type
, "cros_kernel")) {
272 part
->has_guid
= true;
273 part
->guid
= GUID_PARTITION_CHROME_OS_KERNEL
;
274 /* Default attributes: bootable kernel. */
275 part
->gattr
= (1ULL << 48) | /* priority=1 */
276 (1ULL << 56); /* success=1 */
282 /* init an utf-16 string from utf-8 string */
283 static inline void init_utf16(char *str
, uint16_t *buf
, unsigned bufsize
)
286 for (i
= 0; i
< bufsize
; i
++) {
287 if (str
[n
] == 0x00) {
290 } else if ((str
[n
] & 0x80) == 0x00) {//0xxxxxxx
291 buf
[i
] = cpu_to_le16(str
[n
++]);
292 } else if ((str
[n
] & 0xE0) == 0xC0) {//110xxxxx
293 buf
[i
] = cpu_to_le16((str
[n
] & 0x1F) << 6 | (str
[n
+ 1] & 0x3F));
295 } else if ((str
[n
] & 0xF0) == 0xE0) {//1110xxxx
296 buf
[i
] = cpu_to_le16((str
[n
] & 0x0F) << 12 | (str
[n
+ 1] & 0x3F) << 6 | (str
[n
+ 2] & 0x3F));
299 buf
[i
] = cpu_to_le16('?');
305 /* check the partition sizes and write the partition table */
306 static int gen_ptable(uint32_t signature
, int nr
)
308 struct pte pte
[MBR_ENTRY_MAX
];
309 unsigned long start
, len
, sect
= 0;
312 memset(pte
, 0, sizeof(struct pte
) * MBR_ENTRY_MAX
);
313 for (i
= 0; i
< nr
; i
++) {
314 if (!parts
[i
].size
) {
315 if (ignore_null_sized_partition
)
317 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
321 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
322 pte
[i
].type
= parts
[i
].type
;
324 start
= sect
+ sectors
;
325 if (parts
[i
].start
!= 0) {
326 if (parts
[i
].start
* 2 < start
) {
327 fprintf(stderr
, "Invalid start %ld for partition %d!\n",
331 start
= parts
[i
].start
* 2;
332 } else if (kb_align
!= 0) {
333 start
= round_to_kb(start
);
335 pte
[i
].start
= cpu_to_le32(start
);
337 sect
= start
+ parts
[i
].size
* 2;
339 sect
= round_to_cyl(sect
);
340 pte
[i
].length
= cpu_to_le32(len
= sect
- start
);
342 to_chs(start
, pte
[i
].chs_start
);
343 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
346 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n",
348 (long)start
* DISK_SECTOR_SIZE
,
349 (long)(start
+ len
) * DISK_SECTOR_SIZE
,
350 (long)len
* DISK_SECTOR_SIZE
);
351 printf("%ld\n", (long)start
* DISK_SECTOR_SIZE
);
352 printf("%ld\n", (long)len
* DISK_SECTOR_SIZE
);
355 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
356 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
360 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
361 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
362 fputs("write failed.\n", stderr
);
366 lseek(fd
, MBR_PARTITION_ENTRY_OFFSET
, SEEK_SET
);
367 if (write(fd
, pte
, sizeof(struct pte
) * MBR_ENTRY_MAX
) != sizeof(struct pte
) * MBR_ENTRY_MAX
) {
368 fputs("write failed.\n", stderr
);
371 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
372 if (write(fd
, "\x55\xaa", 2) != 2) {
373 fputs("write failed.\n", stderr
);
383 /* check the partition sizes and write the guid partition table */
384 static int gen_gptable(uint32_t signature
, guid_t guid
, unsigned nr
)
386 struct pte pte
[MBR_ENTRY_MAX
];
388 .signature
= cpu_to_le64(GPT_SIGNATURE
),
389 .revision
= cpu_to_le32(GPT_REVISION
),
390 .size
= cpu_to_le32(GPT_HEADER_SIZE
),
391 .self
= cpu_to_le64(GPT_HEADER_SECTOR
),
392 .first_usable
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
+ GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
/ DISK_SECTOR_SIZE
),
393 .first_entry
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
),
395 .entry_num
= cpu_to_le32(GPT_ENTRY_MAX
),
396 .entry_size
= cpu_to_le32(GPT_ENTRY_SIZE
),
398 struct gpte gpte
[GPT_ENTRY_MAX
];
400 uint64_t sect
= GPT_SIZE
+ GPT_FIRST_ENTRY_SECTOR
;
402 unsigned i
, pmbr
= 1;
404 memset(pte
, 0, sizeof(struct pte
) * MBR_ENTRY_MAX
);
405 memset(gpte
, 0, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
);
406 for (i
= 0; i
< nr
; i
++) {
407 if (!parts
[i
].size
) {
408 if (ignore_null_sized_partition
)
410 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
414 if (parts
[i
].start
!= 0) {
415 if (parts
[i
].start
* 2 < start
) {
416 fprintf(stderr
, "Invalid start %ld for partition %d!\n",
420 start
= parts
[i
].start
* 2;
421 } else if (kb_align
!= 0) {
422 start
= round_to_kb(start
);
424 parts
[i
].actual_start
= start
;
425 gpte
[i
].start
= cpu_to_le64(start
);
427 sect
= start
+ parts
[i
].size
* 2;
428 gpte
[i
].end
= cpu_to_le64(sect
-1);
430 gpte
[i
].guid
.b
[sizeof(guid_t
) -1] += i
+ 1;
431 gpte
[i
].type
= parts
[i
].guid
;
433 if (parts
[i
].hybrid
&& pmbr
< MBR_ENTRY_MAX
) {
434 pte
[pmbr
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
435 pte
[pmbr
].type
= parts
[i
].type
;
436 pte
[pmbr
].start
= cpu_to_le32(start
);
437 pte
[pmbr
].length
= cpu_to_le32(sect
- start
);
438 to_chs(start
, pte
[1].chs_start
);
439 to_chs(sect
- 1, pte
[1].chs_end
);
442 gpte
[i
].attr
= parts
[i
].gattr
;
445 init_utf16(parts
[i
].name
, (uint16_t *)gpte
[i
].name
, GPT_ENTRY_NAME_SIZE
/ sizeof(uint16_t));
447 if ((i
+ 1) == (unsigned)active
)
448 gpte
[i
].attr
|= GPT_ATTR_LEGACY_BOOT
;
450 if (parts
[i
].required
)
451 gpte
[i
].attr
|= GPT_ATTR_PLAT_REQUIRED
;
454 fprintf(stderr
, "Partition %d: start=%" PRIu64
", end=%" PRIu64
", size=%" PRIu64
"\n",
456 start
* DISK_SECTOR_SIZE
, sect
* DISK_SECTOR_SIZE
,
457 (sect
- start
) * DISK_SECTOR_SIZE
);
458 printf("%" PRIu64
"\n", start
* DISK_SECTOR_SIZE
);
459 printf("%" PRIu64
"\n", (sect
- start
) * DISK_SECTOR_SIZE
);
462 if (parts
[0].actual_start
> GPT_FIRST_ENTRY_SECTOR
+ GPT_SIZE
) {
463 gpte
[GPT_ENTRY_MAX
- 1].start
= cpu_to_le64(GPT_FIRST_ENTRY_SECTOR
+ GPT_SIZE
);
464 gpte
[GPT_ENTRY_MAX
- 1].end
= cpu_to_le64(parts
[0].actual_start
- 1);
465 gpte
[GPT_ENTRY_MAX
- 1].type
= GUID_PARTITION_BIOS_BOOT
;
466 gpte
[GPT_ENTRY_MAX
- 1].guid
= guid
;
467 gpte
[GPT_ENTRY_MAX
- 1].guid
.b
[sizeof(guid_t
) -1] += GPT_ENTRY_MAX
;
470 end
= sect
+ GPT_SIZE
;
473 pte
[0].start
= cpu_to_le32(GPT_HEADER_SECTOR
);
474 pte
[0].length
= cpu_to_le32(end
- GPT_HEADER_SECTOR
);
475 to_chs(GPT_HEADER_SECTOR
, pte
[0].chs_start
);
476 to_chs(end
, pte
[0].chs_end
);
478 gpth
.last_usable
= cpu_to_le64(end
- GPT_SIZE
- 1);
479 gpth
.alternate
= cpu_to_le64(end
);
480 gpth
.entry_crc32
= cpu_to_le32(gpt_crc32(gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
));
481 gpth
.crc32
= cpu_to_le32(gpt_crc32((char *)&gpth
, GPT_HEADER_SIZE
));
483 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
484 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
488 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
489 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
490 fputs("write failed.\n", stderr
);
494 lseek(fd
, MBR_PARTITION_ENTRY_OFFSET
, SEEK_SET
);
495 if (write(fd
, pte
, sizeof(struct pte
) * MBR_ENTRY_MAX
) != sizeof(struct pte
) * MBR_ENTRY_MAX
) {
496 fputs("write failed.\n", stderr
);
500 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
501 if (write(fd
, "\x55\xaa", 2) != 2) {
502 fputs("write failed.\n", stderr
);
506 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
507 fputs("write failed.\n", stderr
);
511 lseek(fd
, GPT_FIRST_ENTRY_SECTOR
* DISK_SECTOR_SIZE
, SEEK_SET
);
512 if (write(fd
, &gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) != GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) {
513 fputs("write failed.\n", stderr
);
517 #ifdef WANT_ALTERNATE_PTABLE
518 /* The alternate partition table (We omit it by default) */
519 swap(gpth
.self
, gpth
.alternate
);
520 gpth
.first_entry
= cpu_to_le64(end
- GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
/ DISK_SECTOR_SIZE
),
522 gpth
.crc32
= cpu_to_le32(gpt_crc32(&gpth
, GPT_HEADER_SIZE
));
524 lseek(fd
, end
* DISK_SECTOR_SIZE
- GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
, SEEK_SET
);
525 if (write(fd
, &gpte
, GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) != GPT_ENTRY_SIZE
* GPT_ENTRY_MAX
) {
526 fputs("write failed.\n", stderr
);
530 lseek(fd
, end
* DISK_SECTOR_SIZE
, SEEK_SET
);
531 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
532 fputs("write failed.\n", stderr
);
535 lseek(fd
, (end
+ 1) * DISK_SECTOR_SIZE
-1, SEEK_SET
);
536 if (write(fd
, "\x00", 1) != 1) {
537 fputs("write failed.\n", stderr
);
548 static void usage(char *prog
)
550 fprintf(stderr
, "Usage: %s [-v] [-n] [-g] -h <heads> -s <sectors> -o <outputfile>\n"
551 " [-a 0..4] [-l <align kB>] [-G <guid>]\n"
552 " [[-t <type> | -T <GPT part type>] [-r] [-N <name>] -p <size>[@<start>]...] \n", prog
);
556 static guid_t
type_to_guid_and_name(unsigned char type
, char **name
)
558 guid_t guid
= GUID_PARTITION_BASIC_DATA
;
563 *name
= "EFI System Partition";
564 guid
= GUID_PARTITION_SYSTEM
;
567 guid
= GUID_PARTITION_LINUX_FS_GUID
;
570 guid
= GUID_PARTITION_LINUX_FIT_GUID
;
577 int main (int argc
, char **argv
)
579 unsigned char type
= 0x83;
584 unsigned short int hybrid
= 0, required
= 0;
585 uint32_t signature
= 0x5452574F; /* 'OWRT' */
586 guid_t guid
= GUID_INIT( signature
, 0x2211, 0x4433, \
587 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
589 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:T:o:vnHN:gl:rS:G:")) != -1) {
598 ignore_null_sized_partition
= true;
601 use_guid_partition_table
= 1;
607 heads
= (int)strtoul(optarg
, NULL
, 0);
610 sectors
= (int)strtoul(optarg
, NULL
, 0);
613 if (part
> GPT_ENTRY_MAX
- 1 || (!use_guid_partition_table
&& part
> 3)) {
614 fputs("Too many partitions\n", stderr
);
617 p
= strchr(optarg
, '@');
620 parts
[part
].start
= to_kbytes(p
);
622 if (!parts
[part
].has_guid
)
623 parts
[part
].guid
= type_to_guid_and_name(type
, &name
);
625 parts
[part
].size
= to_kbytes(optarg
);
626 parts
[part
].required
= required
;
627 parts
[part
].name
= name
;
628 parts
[part
].hybrid
= hybrid
;
629 fprintf(stderr
, "part %ld %ld\n", parts
[part
].start
, parts
[part
].size
);
630 parts
[part
++].type
= type
;
632 * reset 'name','required' and 'hybrid'
633 * 'type' is deliberately inherited from the previous delcaration
646 type
= (char)strtoul(optarg
, NULL
, 16);
649 active
= (int)strtoul(optarg
, NULL
, 0);
650 if ((active
< 0) || (active
> 4))
654 kb_align
= (int)strtoul(optarg
, NULL
, 0) * 2;
657 signature
= strtoul(optarg
, NULL
, 0);
660 if (!parse_gpt_parttype(optarg
, &parts
[part
])) {
662 "Invalid GPT partition type \"%s\"\n",
668 if (guid_parse(optarg
, &guid
)) {
669 fputs("Invalid guid string\n", stderr
);
679 if (argc
|| (!use_guid_partition_table
&& ((heads
<= 0) || (sectors
<= 0))) || !filename
)
682 if (use_guid_partition_table
) {
685 return gen_gptable(signature
, guid
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
688 return gen_ptable(signature
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;