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];
114 /* GPT Partition table header */
123 uint64_t first_usable
;
124 uint64_t last_usable
;
126 uint64_t first_entry
;
129 uint32_t entry_crc32
;
130 } __attribute__((packed
));
132 /* GPT Partition table entry */
139 char name
[GPT_ENTRY_NAME_SIZE
];
140 } __attribute__((packed
));
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
;
155 * parse the size argument, which is either
156 * a simple number (K assumed) or
159 * returns the size in KByte
161 static long to_kbytes(const char *string
)
167 result
= strtoul(string
, &end
, 0);
168 switch (tolower(*end
)) {
170 case '\0' : exp
= 0; break;
171 case 'm' : exp
= 1; break;
172 case 'g' : exp
= 2; break;
180 fputs("garbage after end of number\n", stderr
);
184 /* result: number + 1024^(exp) */
187 return result
* (2 << ((10 * exp
) - 1));
190 /* convert the sector number into a CHS value for the partition table */
191 static void to_chs(long sect
, unsigned char chs
[3])
195 s
= (sect
% sectors
) + 1;
196 sect
= sect
/ sectors
;
202 chs
[1] = s
| ((c
>> 2) & 0xC0);
208 /* round the sector number up to the next cylinder */
209 static inline unsigned long round_to_cyl(long sect
)
211 int cyl_size
= heads
* sectors
;
213 return sect
+ cyl_size
- (sect
% cyl_size
);
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
;
221 /* Compute a CRC for guid partition table */
222 static inline unsigned long gpt_crc32(void *buf
, unsigned long len
)
224 return cyg_crc32_accumulate(~0L, buf
, len
) ^ ~0L;
227 /* Parse a guid string to guid_t struct */
228 static inline int guid_parse(char *buf
, guid_t
*guid
)
233 if (strnlen(buf
, GUID_STRING_LENGTH
) != GUID_STRING_LENGTH
)
235 for (i
= 0; i
< sizeof(guid_t
); i
++) {
241 guid
->b
[i
] = strtol(b
, 0, 16);
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]);
251 /* init an utf-16 string from utf-8 string */
252 static inline void init_utf16(char *str
, uint16_t *buf
, unsigned bufsize
)
255 for (i
= 0; i
< bufsize
; i
++) {
256 if (str
[n
] == 0x00) {
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));
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));
268 buf
[i
] = cpu_to_le16('?');
274 /* check the partition sizes and write the partition table */
275 static int gen_ptable(uint32_t signature
, int nr
)
277 struct pte pte
[MBR_ENTRY_MAX
];
278 unsigned long start
, len
, sect
= 0;
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
)
286 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
290 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
291 pte
[i
].type
= parts
[i
].type
;
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",
300 start
= parts
[i
].start
* 2;
301 } else if (kb_align
!= 0) {
302 start
= round_to_kb(start
);
304 pte
[i
].start
= cpu_to_le32(start
);
306 sect
= start
+ parts
[i
].size
* 2;
308 sect
= round_to_cyl(sect
);
309 pte
[i
].length
= cpu_to_le32(len
= sect
- start
);
311 to_chs(start
, pte
[i
].chs_start
);
312 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
315 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n",
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
);
324 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
325 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
329 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
330 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
331 fputs("write failed.\n", stderr
);
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
);
340 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
341 if (write(fd
, "\x55\xaa", 2) != 2) {
342 fputs("write failed.\n", stderr
);
352 /* check the partition sizes and write the guid partition table */
353 static int gen_gptable(uint32_t signature
, guid_t guid
, unsigned nr
)
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
),
364 .entry_num
= cpu_to_le32(GPT_ENTRY_MAX
),
365 .entry_size
= cpu_to_le32(GPT_ENTRY_SIZE
),
367 struct gpte gpte
[GPT_ENTRY_MAX
];
368 uint64_t start
, end
, sect
= 0;
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
)
377 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
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",
387 start
= parts
[i
].start
* 2;
388 } else if (kb_align
!= 0) {
389 start
= round_to_kb(start
);
391 gpte
[i
].start
= cpu_to_le64(start
);
393 sect
= start
+ parts
[i
].size
* 2;
395 sect
= round_to_cyl(sect
);
396 gpte
[i
].end
= cpu_to_le64(sect
-1);
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));
403 gpte
[i
].type
= GUID_PARTITION_BASIC_DATA
;
407 fprintf(stderr
, "Partition %d: start=%" PRIu64
", end=%" PRIu64
", size=%" PRIu64
"\n",
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
);
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
;
421 end
= sect
+ sectors
- 1;
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
);
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
));
434 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
435 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
439 lseek(fd
, MBR_DISK_SIGNATURE_OFFSET
, SEEK_SET
);
440 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
441 fputs("write failed.\n", stderr
);
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
);
451 lseek(fd
, MBR_BOOT_SIGNATURE_OFFSET
, SEEK_SET
);
452 if (write(fd
, "\x55\xaa", 2) != 2) {
453 fputs("write failed.\n", stderr
);
457 if (write(fd
, &gpth
, GPT_HEADER_SIZE
) != GPT_HEADER_SIZE
) {
458 fputs("write failed.\n", stderr
);
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
);
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
),
473 gpth
.crc32
= cpu_to_le32(gpt_crc32(&gpth
, GPT_HEADER_SIZE
));
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
);
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
);
486 lseek(fd
, (end
+ 1) * DISK_SECTOR_SIZE
-1, SEEK_SET
);
487 if (write(fd
, "\x00", 1) != 1) {
488 fputs("write failed.\n", stderr
);
499 static void usage(char *prog
)
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
);
505 int main (int argc
, char **argv
)
507 unsigned char type
= 0x83;
511 uint32_t signature
= 0x5452574F; /* 'OWRT' */
512 guid_t guid
= GUID_INIT( signature
, 0x2211, 0x4433, \
513 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0x00);
515 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:o:vngl:S:G:")) != -1) {
524 ignore_null_sized_partition
= true;
527 use_guid_partition_table
= 1;
530 heads
= (int)strtoul(optarg
, NULL
, 0);
533 sectors
= (int)strtoul(optarg
, NULL
, 0);
536 if (part
> GPT_ENTRY_MAX
- 1 || (!use_guid_partition_table
&& part
> 3)) {
537 fputs("Too many partitions\n", stderr
);
540 p
= strchr(optarg
, '@');
543 parts
[part
].start
= to_kbytes(p
);
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
;
550 type
= (char)strtoul(optarg
, NULL
, 16);
553 active
= (int)strtoul(optarg
, NULL
, 0);
554 if ((active
< 0) || (active
> 4))
558 kb_align
= (int)strtoul(optarg
, NULL
, 0) * 2;
561 signature
= strtoul(optarg
, NULL
, 0);
564 if (guid_parse(optarg
, &guid
)) {
565 fputs("Invalid guid string\n", stderr
);
575 if (argc
|| (heads
<= 0) || (sectors
<= 0) || !filename
)
578 if (use_guid_partition_table
)
579 return gen_gptable(signature
, guid
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;
581 return gen_ptable(signature
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;