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];
59 struct partinfo parts
[4];
60 char *filename
= NULL
;
64 * parse the size argument, which is either
65 * a simple number (K assumed) or
68 * returns the size in KByte
70 static long to_kbytes(const char *string
) {
75 result
= strtoul(string
, &end
, 0);
76 switch (tolower(*end
)) {
78 case '\0' : exp
= 0; break;
79 case 'm' : exp
= 1; break;
80 case 'g' : exp
= 2; break;
88 fprintf(stderr
, "garbage after end of number\n");
92 /* result: number + 1024^(exp) */
93 return result
* ((2 << ((10 * exp
) - 1)) ?: 1);
96 /* convert the sector number into a CHS value for the partition table */
97 static void to_chs(long sect
, unsigned char chs
[3]) {
100 s
= (sect
% sectors
) + 1;
101 sect
= sect
/ sectors
;
107 chs
[1] = s
| ((c
>> 2) & 0xC0);
113 /* round the sector number up to the next cylinder */
114 static inline unsigned long round_to_cyl(long sect
) {
115 int cyl_size
= heads
* sectors
;
117 return sect
+ cyl_size
- (sect
% cyl_size
);
120 /* check the partition sizes and write the partition table */
121 static int gen_ptable(int nr
)
124 unsigned long sect
= 0;
125 int i
, fd
, ret
= -1, start
, len
;
127 memset(pte
, 0, sizeof(struct pte
) * 4);
128 for (i
= 0; i
< nr
; i
++) {
129 if (!parts
[i
].size
) {
130 fprintf(stderr
, "Invalid size in partition %d!\n", i
);
133 pte
[i
].active
= ((i
+ 1) == active
) ? 0x80 : 0;
134 pte
[i
].type
= parts
[i
].type
;
135 pte
[i
].start
= cpu_to_le16(start
= sect
+ sectors
);
136 sect
= round_to_cyl(start
+ parts
[i
].size
* 2);
137 pte
[i
].length
= cpu_to_le16(len
= sect
- start
);
138 to_chs(start
, pte
[i
].chs_start
);
139 to_chs(start
+ len
- 1, pte
[i
].chs_end
);
141 fprintf(stderr
, "Partition %d: start=%ld, end=%ld, size=%ld\n", i
, (long) start
* 512, ((long) start
+ (long) len
) * 512, (long) len
* 512);
142 printf("%ld\n", ((long) start
* 512));
143 printf("%ld\n", ((long) len
* 512));
146 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) < 0) {
147 fprintf(stderr
, "Can't open output file '%s'\n",filename
);
151 lseek(fd
, 446, SEEK_SET
);
152 if (write(fd
, pte
, sizeof(struct pte
) * 4) != sizeof(struct pte
) * 4) {
153 fprintf(stderr
, "write failed.\n");
156 lseek(fd
, 510, SEEK_SET
);
157 if (write(fd
, "\x55\xaa", 2) != 2) {
158 fprintf(stderr
, "write failed.\n");
168 static void usage(char *prog
)
170 fprintf(stderr
, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog
);
174 int main (int argc
, char **argv
)
180 while ((ch
= getopt(argc
, argv
, "h:s:p:a:t:o:v")) != -1) {
189 heads
= (int) strtoul(optarg
, NULL
, 0);
192 sectors
= (int) strtoul(optarg
, NULL
, 0);
196 fprintf(stderr
, "Too many partitions\n");
199 parts
[part
].size
= to_kbytes(optarg
);
200 parts
[part
++].type
= type
;
203 type
= (char) strtoul(optarg
, NULL
, 16);
206 active
= (int) strtoul(optarg
, NULL
, 0);
207 if ((active
< 0) || (active
> 4))
216 if (argc
|| (heads
<= 0) || (sectors
<= 0) || !filename
)
219 return gen_ptable(part
);
This page took 0.061863 seconds and 5 git commands to generate.