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 * 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>
35 #if __BYTE_ORDER == __BIG_ENDIAN
36 #define cpu_to_le32(x) bswap_32(x)
37 #elif __BYTE_ORDER == __LITTLE_ENDIAN
38 #define cpu_to_le32(x) (x)
40 #error unknown endianness!
43 /* Partition table entry */
63 bool ignore_null_sized_partition
= false;
64 struct partinfo parts
[4];
65 char *filename
= NULL
;
69 * parse the size argument, which is either
70 * a simple number (K assumed) or
73 * returns the size in KByte
75 static long to_kbytes(const char *string
)
81 result
= strtoul(string
, &end
, 0);
82 switch (tolower(*end
)) {
84 case '\0' : exp
= 0; break;
85 case 'm' : exp
= 1; break;
86 case 'g' : exp
= 2; break;
94 fprintf(stderr
, "garbage after end of number\n");
98 /* result: number + 1024^(exp) */
101 return result
* (2 << ((10 * exp
) - 1));
104 /* convert the sector number into a CHS value for the partition table */
105 static void to_chs(long sect
, unsigned char chs
[3])
109 s
= (sect
% sectors
) + 1;
110 sect
= sect
/ sectors
;
116 chs
[1] = s
| ((c
>> 2) & 0xC0);
122 /* round the sector number up to the next cylinder */
123 static inline unsigned long round_to_cyl(long sect
)
125 int cyl_size
= heads
* sectors
;
127 return sect
+ cyl_size
- (sect
% cyl_size
);
130 /* round the sector number up to the kb_align boundary */
131 static inline unsigned long round_to_kb(long sect
) {
132 return ((sect
- 1) / kb_align
+ 1) * kb_align
;
135 /* check the partition sizes and write the partition table */
136 static int gen_ptable(uint32_t signature
, int nr
)
139 unsigned long sect
= 0;
140 int i
, fd
, ret
= -1, start
, len
;
142 memset(pte
, 0, sizeof(struct pte
) * 4);
143 for (i
= 0; i
< nr
; i
++) {
144 if (!parts
[i
].size
) {
145 if (ignore_null_sized_partition
)
147 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
151 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
152 pte
[i
].type
= parts
[i
].type
;
154 start
= sect
+ sectors
;
156 start
= round_to_kb(start
);
157 pte
[i
].start
= cpu_to_le32(start
);
159 sect
= start
+ parts
[i
].size
* 2;
161 sect
= round_to_cyl(sect
);
162 pte
[i
].length
= cpu_to_le32(len
= sect
- start
);
164 to_chs(start
, pte
[i
].chs_start
);
165 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
168 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n", i
, (long)start
* 512, ((long)start
+ (long)len
) * 512, (long)len
* 512);
169 printf("%ld\n", (long)start
* 512);
170 printf("%ld\n", (long)len
* 512);
173 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
174 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
178 lseek(fd
, 440, SEEK_SET
);
179 if (write(fd
, &signature
, sizeof(signature
)) != sizeof(signature
)) {
180 fprintf(stderr
, "write failed.\n");
184 lseek(fd
, 446, SEEK_SET
);
185 if (write(fd
, pte
, sizeof(struct pte
) * 4) != sizeof(struct pte
) * 4) {
186 fprintf(stderr
, "write failed.\n");
189 lseek(fd
, 510, SEEK_SET
);
190 if (write(fd
, "\x55\xaa", 2) != 2) {
191 fprintf(stderr
, "write failed.\n");
201 static void usage(char *prog
)
203 fprintf(stderr
, "Usage: %s [-v] [-n] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog
);
207 int main (int argc
, char **argv
)
212 uint32_t signature
= 0x5452574F; /* 'OWRT' */
214 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:o:vnl:S:")) != -1) {
223 ignore_null_sized_partition
= true;
226 heads
= (int)strtoul(optarg
, NULL
, 0);
229 sectors
= (int)strtoul(optarg
, NULL
, 0);
233 fprintf(stderr
, "Too many partitions\n");
236 parts
[part
].size
= to_kbytes(optarg
);
237 parts
[part
++].type
= type
;
240 type
= (char)strtoul(optarg
, NULL
, 16);
243 active
= (int)strtoul(optarg
, NULL
, 0);
244 if ((active
< 0) || (active
> 4))
248 kb_align
= (int)strtoul(optarg
, NULL
, 0) * 2;
251 signature
= strtoul(optarg
, NULL
, 0);
259 if (argc
|| (heads
<= 0) || (sectors
<= 0) || !filename
)
262 return gen_ptable(signature
, part
) ? EXIT_FAILURE
: EXIT_SUCCESS
;