6379ed7a5904b7ddc48316ce52549c06f3c50d6e
[openwrt/openwrt.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 #include <stdint.h>
32
33 #if __BYTE_ORDER == __BIG_ENDIAN
34 #define cpu_to_le16(x) bswap_16(x)
35 #elif __BYTE_ORDER == __LITTLE_ENDIAN
36 #define cpu_to_le16(x) (x)
37 #else
38 #error unknown endianness!
39 #endif
40
41 /* Partition table entry */
42 struct pte {
43 unsigned char active;
44 unsigned char chs_start[3];
45 unsigned char type;
46 unsigned char chs_end[3];
47 unsigned int start;
48 unsigned int length;
49 };
50
51 struct partinfo {
52 unsigned long size;
53 int type;
54 };
55
56 int verbose = 0;
57 int active = 1;
58 int heads = -1;
59 int sectors = -1;
60 int kb_align = 0;
61 struct partinfo parts[4];
62 char *filename = NULL;
63
64
65 /*
66 * parse the size argument, which is either
67 * a simple number (K assumed) or
68 * K, M or G
69 *
70 * returns the size in KByte
71 */
72 static long to_kbytes(const char *string) {
73 int exp = 0;
74 long result;
75 char *end;
76
77 result = strtoul(string, &end, 0);
78 switch (tolower(*end)) {
79 case 'k' :
80 case '\0' : exp = 0; break;
81 case 'm' : exp = 1; break;
82 case 'g' : exp = 2; break;
83 default: return 0;
84 }
85
86 if (*end)
87 end++;
88
89 if (*end) {
90 fprintf(stderr, "garbage after end of number\n");
91 return 0;
92 }
93
94 /* result: number + 1024^(exp) */
95 return result * ((2 << ((10 * exp) - 1)) ?: 1);
96 }
97
98 /* convert the sector number into a CHS value for the partition table */
99 static void to_chs(long sect, unsigned char chs[3]) {
100 int c,h,s;
101
102 s = (sect % sectors) + 1;
103 sect = sect / sectors;
104 h = sect % heads;
105 sect = sect / heads;
106 c = sect;
107
108 chs[0] = h;
109 chs[1] = s | ((c >> 2) & 0xC0);
110 chs[2] = c & 0xFF;
111
112 return;
113 }
114
115 /* round the sector number up to the next cylinder */
116 static inline unsigned long round_to_cyl(long sect) {
117 int cyl_size = heads * sectors;
118
119 return sect + cyl_size - (sect % cyl_size);
120 }
121
122 /* round the sector number up to the kb_align boundary */
123 static inline unsigned long round_to_kb(long sect) {
124 return ((sect - 1) / kb_align + 1) * kb_align;
125 }
126
127 /* check the partition sizes and write the partition table */
128 static int gen_ptable(uint32_t signature, int nr)
129 {
130 struct pte pte[4];
131 unsigned long sect = 0;
132 int i, fd, ret = -1, start, len;
133
134 memset(pte, 0, sizeof(struct pte) * 4);
135 for (i = 0; i < nr; i++) {
136 if (!parts[i].size) {
137 fprintf(stderr, "Invalid size in partition %d!\n", i);
138 return -1;
139 }
140 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
141 pte[i].type = parts[i].type;
142 start = sect + sectors;
143 if (kb_align != 0)
144 start = round_to_kb(start);
145 pte[i].start = cpu_to_le16(start);
146 sect = start + parts[i].size * 2;
147 if (kb_align == 0)
148 sect = round_to_cyl(sect);
149 pte[i].length = cpu_to_le16(len = sect - start);
150 to_chs(start, pte[i].chs_start);
151 to_chs(start + len - 1, pte[i].chs_end);
152 if (verbose)
153 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long) start * 512, ((long) start + (long) len) * 512, (long) len * 512);
154 printf("%ld\n", ((long) start * 512));
155 printf("%ld\n", ((long) len * 512));
156 }
157
158 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
159 fprintf(stderr, "Can't open output file '%s'\n",filename);
160 return -1;
161 }
162
163 lseek(fd, 440, SEEK_SET);
164 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
165 fprintf(stderr, "write failed.\n");
166 goto fail;
167 }
168
169 lseek(fd, 446, SEEK_SET);
170 if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
171 fprintf(stderr, "write failed.\n");
172 goto fail;
173 }
174 lseek(fd, 510, SEEK_SET);
175 if (write(fd, "\x55\xaa", 2) != 2) {
176 fprintf(stderr, "write failed.\n");
177 goto fail;
178 }
179
180 ret = 0;
181 fail:
182 close(fd);
183 return ret;
184 }
185
186 static void usage(char *prog)
187 {
188 fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
189 exit(1);
190 }
191
192 int main (int argc, char **argv)
193 {
194 char type = 0x83;
195 int ch;
196 int part = 0;
197 uint32_t signature = 0x5452574F; /* 'OWRT' */
198
199 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vl:S:")) != -1) {
200 switch (ch) {
201 case 'o':
202 filename = optarg;
203 break;
204 case 'v':
205 verbose++;
206 break;
207 case 'h':
208 heads = (int) strtoul(optarg, NULL, 0);
209 break;
210 case 's':
211 sectors = (int) strtoul(optarg, NULL, 0);
212 break;
213 case 'p':
214 if (part > 3) {
215 fprintf(stderr, "Too many partitions\n");
216 exit(1);
217 }
218 parts[part].size = to_kbytes(optarg);
219 parts[part++].type = type;
220 break;
221 case 't':
222 type = (char) strtoul(optarg, NULL, 16);
223 break;
224 case 'a':
225 active = (int) strtoul(optarg, NULL, 0);
226 if ((active < 0) || (active > 4))
227 active = 0;
228 break;
229 case 'l':
230 kb_align = (int) strtoul(optarg, NULL, 0) * 2;
231 break;
232 case 'S':
233 signature = strtoul(optarg, NULL, 0);
234 break;
235 case '?':
236 default:
237 usage(argv[0]);
238 }
239 }
240 argc -= optind;
241 if (argc || (heads <= 0) || (sectors <= 0) || !filename)
242 usage(argv[0]);
243
244 return gen_ptable(signature, part);
245 }