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