2 * ptgen - partition table generator
3 * Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
32 #if __BYTE_ORDER == __BIG_ENDIAN
33 #define cpu_to_le16(x) bswap_16(x)
34 #elif __BYTE_ORDER == __LITTLE_ENDIAN
35 #define cpu_to_le16(x) (x)
37 #error unknown endianness!
40 /* Partition table entry */
43 unsigned char chs_start
[3];
45 unsigned char chs_end
[3];
60 struct partinfo parts
[4];
61 char *filename
= NULL
;
65 * parse the size argument, which is either
66 * a simple number (K assumed) or
69 * returns the size in KByte
71 static long to_kbytes(const char *string
) {
76 result
= strtoul(string
, &end
, 0);
77 switch (tolower(*end
)) {
79 case '\0' : exp
= 0; break;
80 case 'm' : exp
= 1; break;
81 case 'g' : exp
= 2; break;
89 fprintf(stderr
, "garbage after end of number\n");
93 /* result: number + 1024^(exp) */
94 return result
* ((2 << ((10 * exp
) - 1)) ?: 1);
97 /* convert the sector number into a CHS value for the partition table */
98 static void to_chs(long sect
, unsigned char chs
[3]) {
101 s
= (sect
% sectors
) + 1;
102 sect
= sect
/ sectors
;
108 chs
[1] = s
| ((c
>> 2) & 0xC0);
114 /* round the sector number up to the next cylinder */
115 static inline unsigned long round_to_cyl(long sect
) {
116 int cyl_size
= heads
* sectors
;
118 return sect
+ cyl_size
- (sect
% cyl_size
);
121 /* round the sector number up to the kb_align boundary */
122 static inline unsigned long round_to_kb(long sect
) {
123 return ((sect
- 1) / kb_align
+ 1) * kb_align
;
126 /* check the partition sizes and write the partition table */
127 static int gen_ptable(int nr
)
130 unsigned long sect
= 0;
131 int i
, fd
, ret
= -1, start
, len
;
133 memset(pte
, 0, sizeof(struct pte
) * 4);
134 for (i
= 0; i
< nr
; i
++) {
135 if (!parts
[i
].size
) {
136 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
139 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
140 pte
[i
].type
= parts
[i
].type
;
141 start
= sect
+ sectors
;
143 start
= round_to_kb(start
);
144 pte
[i
].start
= cpu_to_le16(start
);
145 sect
= start
+ parts
[i
].size
* 2;
147 sect
= round_to_cyl(sect
);
148 pte
[i
].length
= cpu_to_le16(len
= sect
- start
);
149 to_chs(start
, pte
[i
].chs_start
);
150 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
152 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n", i
, (long) start
* 512, ((long) start
+ (long) len
) * 512, (long) len
* 512);
153 printf("%ld\n", ((long) start
* 512));
154 printf("%ld\n", ((long) len
* 512));
157 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
158 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
162 lseek(fd
, 446, SEEK_SET
);
163 if (write(fd
, pte
, sizeof(struct pte
) * 4) != sizeof(struct pte
) * 4) {
164 fprintf(stderr
, "write failed.\n");
167 lseek(fd
, 510, SEEK_SET
);
168 if (write(fd
, "\x55\xaa", 2) != 2) {
169 fprintf(stderr
, "write failed.\n");
179 static void usage(char *prog
)
181 fprintf(stderr
, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog
);
185 int main (int argc
, char **argv
)
191 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:o:vl:")) != -1) {
200 heads
= (int) strtoul(optarg
, NULL
, 0);
203 sectors
= (int) strtoul(optarg
, NULL
, 0);
207 fprintf(stderr
, "Too many partitions\n");
210 parts
[part
].size
= to_kbytes(optarg
);
211 parts
[part
++].type
= type
;
214 type
= (char) strtoul(optarg
, NULL
, 16);
217 active
= (int) strtoul(optarg
, NULL
, 0);
218 if ((active
< 0) || (active
> 4))
222 kb_align
= (int) strtoul(optarg
, NULL
, 0) * 2;
230 if (argc
|| (heads
<= 0) || (sectors
<= 0) || !filename
)
233 return gen_ptable(part
);