remove custom partition table hack, add ptgen utility for generating partition tables
[openwrt/staging/mkresin.git] / tools / firmware-utils / src / ptgen.c
1 /*
2 * ptgen - partition table generator
3 * Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
4 *
5 * uses parts of afdisk
6 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31
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)
36 #else
37 #error unknown endianness!
38 #endif
39
40 /* Partition table entry */
41 struct pte {
42 unsigned char active;
43 unsigned char chs_start[3];
44 unsigned char type;
45 unsigned char chs_end[3];
46 unsigned int start;
47 unsigned int length;
48 };
49
50 struct partinfo {
51 unsigned long size;
52 int type;
53 };
54
55 int verbose = 0;
56 int active = 1;
57 int heads = -1;
58 int sectors = -1;
59 struct partinfo parts[4];
60 char *filename = NULL;
61
62
63 /*
64 * parse the size argument, which is either
65 * a simple number (K assumed) or
66 * K, M or G
67 *
68 * returns the size in KByte
69 */
70 static long to_kbytes(const char *string) {
71 int exp = 0;
72 long result;
73 char *end;
74
75 result = strtoul(string, &end, 0);
76 switch (tolower(*end)) {
77 case 'k' :
78 case '\0' : exp = 0; break;
79 case 'm' : exp = 1; break;
80 case 'g' : exp = 2; break;
81 default: return 0;
82 }
83
84 if (*end)
85 end++;
86
87 if (*end) {
88 fprintf(stderr, "garbage after end of number\n");
89 return 0;
90 }
91
92 /* result: number + 1024^(exp) */
93 return result * ((2 << ((10 * exp) - 1)) ?: 1);
94 }
95
96 /* convert the sector number into a CHS value for the partition table */
97 static void to_chs(long sect, unsigned char chs[3]) {
98 int c,h,s;
99
100 s = (sect % sectors) + 1;
101 sect = sect / sectors;
102 h = sect % heads;
103 sect = sect / heads;
104 c = sect;
105
106 chs[0] = h;
107 chs[1] = s | ((c >> 2) & 0xC0);
108 chs[2] = c & 0xFF;
109
110 return;
111 }
112
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;
116
117 return sect + cyl_size - ((sect % cyl_size) ?: cyl_size);
118 }
119
120 /* check the partition sizes and write the partition table */
121 static int gen_ptable(int nr)
122 {
123 struct pte pte[4];
124 unsigned long sect = 0;
125 int i, fd, ret = -1, start, len;
126
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);
131 return -1;
132 }
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);
140 if (verbose)
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 }
144
145 if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) < 0) {
146 fprintf(stderr, "Can't open output file '%s'\n",filename);
147 return -1;
148 }
149
150 lseek(fd, 446, SEEK_SET);
151 if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
152 fprintf(stderr, "write failed.\n");
153 goto fail;
154 }
155 lseek(fd, 510, SEEK_SET);
156 if (write(fd, "\x55\xaa", 2) != 2) {
157 fprintf(stderr, "write failed.\n");
158 goto fail;
159 }
160
161 ret = 0;
162 fail:
163 close(fd);
164 return ret;
165 }
166
167 static void usage(char *prog)
168 {
169 fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog);
170 exit(1);
171 }
172
173 int main (int argc, char **argv)
174 {
175 char type = 0x83;
176 int ch;
177 int part = 0;
178
179 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:v")) != -1) {
180 switch (ch) {
181 case 'o':
182 filename = optarg;
183 break;
184 case 'v':
185 verbose++;
186 break;
187 case 'h':
188 heads = (int) strtoul(optarg, NULL, 0);
189 break;
190 case 's':
191 sectors = (int) strtoul(optarg, NULL, 0);
192 break;
193 case 'p':
194 if (part > 3) {
195 fprintf(stderr, "Too many partitions\n");
196 exit(1);
197 }
198 parts[part].size = to_kbytes(optarg);
199 parts[part++].type = type;
200 break;
201 case 't':
202 type = (char) strtoul(optarg, NULL, 16);
203 break;
204 case 'a':
205 active = (int) strtoul(optarg, NULL, 0);
206 if ((active < 0) || (active > 4))
207 active = 0;
208 break;
209 case '?':
210 default:
211 usage(argv[0]);
212 }
213 }
214 argc -= optind;
215 if (argc || (heads <= 0) || (sectors <= 0) || !filename)
216 usage(argv[0]);
217
218 return gen_ptable(part);
219 }