2 * ptgen - partition table generator
3 * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
6 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
8 * UUID/GUID definition stolen from kernel/include/uapi/linux/uuid.h
9 * Copyright (C) 2010, Intel Corp. Huang Ying <ying.huang@intel.com>
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.
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.
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
26 #include <sys/types.h>
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)
49 #error unknown endianness!
53 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
59 #define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
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) }})
66 #define GUID_STRING_LENGTH 36
68 #define GPT_SIGNATURE 0x5452415020494645ULL
69 #define GPT_REVISION 0x00010000
71 #define GUID_PARTITION_SYSTEM \
72 GUID_INIT( 0xC12A7328, 0xF81F, 0x11d2, \
73 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
75 #define GUID_PARTITION_BASIC_DATA \
76 GUID_INIT( 0xEBD0A0A2, 0xB9E5, 0x4433, \
77 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
79 #define GUID_PARTITION_BIOS_BOOT \
80 GUID_INIT( 0x21686148, 0x6449, 0x6E6F, \
81 0x74, 0x4E, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49)
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
88 #define GPT_HEADER_SECTOR 1
89 #define GPT_FIRST_ENTRY_SECTOR 2
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
96 #define DISK_SECTOR_SIZE 512
98 /* Partition table entry */
101 uint8_t chs_start
[3];
113 /* GPT Partition table header */
122 uint64_t first_usable
;
123 uint64_t last_usable
;
125 uint64_t first_entry
;
128 uint32_t entry_crc32
;
129 } __attribute__((packed
));
131 /* GPT Partition table entry */
138 char name
[GPT_ENTRY_NAME_SIZE
];
139 } __attribute__((packed
));
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
;
154 * parse the size argument, which is either
155 * a simple number (K assumed) or
158 * returns the size in KByte
160 static long to_kbytes(const char *string
)
166 result
= strtoul(string
, &end
, 0);
167 switch (tolower(*end
)) {
169 case '\0' : exp
= 0; break;
170 case 'm' : exp
= 1; break;
171 case 'g' : exp
= 2; break;
179 fputs("garbage after end of number\n", stderr
);
183 /* result: number + 1024^(exp) */
186 return result
* (2 << ((10 * exp
) - 1));
189 /* convert the sector number into a CHS value for the partition table */
190 static void to_chs(long sect
, unsigned char chs
[3])
194 s
= (sect
% sectors
) + 1;
195 sect
= sect
/ sectors
;
201 chs
[1] = s
| ((c
>> 2) & 0xC0);
207 /* round the sector number up to the next cylinder */
208 static inline unsigned long round_to_cyl(long sect
)
210 int cyl_size
= heads
* sectors
;
212 return sect
+ cyl_size
- (sect
% cyl_size
);
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
;
220 /* Compute a CRC for guid partition table */
221 static inline unsigned long gpt_crc32(void *buf
, unsigned long len
)
223 return cyg_crc32_accumulate(~0L, buf
, len
) ^ ~0L;
226 /* Parse a guid string to guid_t struct */
227 static inline int guid_parse(char *buf
, guid_t
*guid
)
232 if (strnlen(buf
, GUID_STRING_LENGTH
) != GUID_STRING_LENGTH
)
234 for (i
= 0; i
< sizeof(guid_t
); i
++) {
240 guid
->b
[i
] = strtol(b
, 0, 16);
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]);
250 /* init an utf-16 string from utf-8 string */
251 static inline void init_utf16(char *str
, uint16_t *buf
, unsigned bufsize
)
254 for (i
= 0; i
< bufsize
; i
++) {
255 if (str
[n
] == 0x00) {
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));
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));
267 buf
[i
] = cpu_to_le16('?');
273 /* check the partition sizes and write the partition table */
274 static int gen_ptable(uint32_t signature
, int nr
)
276 struct pte pte
[MBR_ENTRY_MAX
];
277 unsigned long start
, len
, sect
= 0;
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
)
285 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
289 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
290 pte
[i
].type
= parts
[i
].type
;
292 start
= sect
+ sectors
;
294 start
= round_to_kb(start
);
295 pte
[i
].start
= cpu_to_le32(start
);
297 sect
= start
+ parts
[i
].size
* 2;
299 sect
= round_to_cyl(sect
);
300 pte
[i
].length
= cpu_to_le32(len
= sect
- start
);
302 to_chs(start
, pte
[i
].chs_start
);
303 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
306 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n",
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
);
315 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
316 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
320 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
321 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
322 fputs("write failed.\n", stderr
);
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
);
331 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
332 if (write(fd
, "\x55\xaa", 2) != 2) {
333 fputs("write failed.\n", stderr
);
343 /* check the partition sizes and write the guid partition table */
344 static int gen_gptable(uint32_t signature
, guid_t guid
, unsigned nr
)
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
),
355 .entry_num
= cpu_to_le32(GPT_ENTRY_MAX
),
356 .entry_size
= cpu_to_le32(GPT_ENTRY_SIZE
),
358 struct gpte gpte
[GPT_ENTRY_MAX
];
359 uint64_t start
, end
, sect
= 0;
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
)
368 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
371 start
= sect
+ sectors
;
373 start
= round_to_kb(start
);
374 gpte
[i
].start
= cpu_to_le64(start
);
376 sect
= start
+ parts
[i
].size
* 2;
378 sect
= round_to_cyl(sect
);
379 gpte
[i
].end
= cpu_to_le64(sect
-1);
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));
386 gpte
[i
].type
= GUID_PARTITION_BASIC_DATA
;
390 fprintf(stderr
, "Partition %d: start=%" PRIu64
", end=%" PRIu64
", size=%" PRIu64
"\n",
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
);
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
;
404 end
= sect
+ sectors
- 1;
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
);
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
));
417 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
418 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
422 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
423 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
424 fputs("write failed.\n", stderr
);
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
);
434 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
435 if (write(fd
, "\x55\xaa", 2) != 2) {
436 fputs("write failed.\n", stderr
);
440 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
441 fputs("write failed.\n", stderr
);
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
);
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
),
456 gpth
.crc32
= cpu_to_le32(gpt_crc32(&gpth
, GPT_HEADER_SIZE
));
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
);
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
);
469 lseek(fd
, (end
+ 1) * DISK_SECTOR_SIZE
-1, SEEK_SET
);
470 if (write(fd
, "\x00", 1) != 1) {
471 fputs("write failed.\n", stderr
);
482 static void usage(char *prog
)
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
);
488 int main (int argc
, char **argv
)
490 unsigned char type
= 0x83;
493 uint32_t signature
= 0x5452574F; /* 'OWRT' */
494 guid_t guid
= GUID_INIT( signature
, 0x2211, 0x4433, \
495 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
497 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:o:vngl:S:G:")) != -1) {
506 ignore_null_sized_partition
= true;
509 use_guid_partition_table
= 1;
512 heads
= (int)strtoul(optarg
, NULL
, 0);
515 sectors
= (int)strtoul(optarg
, NULL
, 0);
518 if (part
> GPT_ENTRY_MAX
- 1 || (!use_guid_partition_table
&& part
> 3)) {
519 fputs("Too many partitions\n", stderr
);
522 parts
[part
].size
= to_kbytes(optarg
);
523 parts
[part
++].type
= type
;
526 type
= (char)strtoul(optarg
, NULL
, 16);
529 active
= (int)strtoul(optarg
, NULL
, 0);
530 if ((active
< 0) || (active
> 4))
534 kb_align
= (int)strtoul(optarg
, NULL
, 0) * 2;
537 signature
= strtoul(optarg
, NULL
, 0);
540 if (guid_parse(optarg
, &guid
)) {
541 fputs("Invalid guid string\n", stderr
);
551 if (argc
|| (heads
<= 0) || (sectors
<= 0) || !filename
)
554 if (use_guid_partition_table
)
555 return gen_gptable(signature
, guid
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
557 return gen_ptable(signature
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;