summaryrefslogtreecommitdiff
path: root/tools/adk/pt.c
blob: 6f2e7ce259a46dbe90065e165a930313d180b1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* 
 * pt - partition table utility
 * Copyright (C) 2010 by Waldemar Brodkorb <wbx@openadk.org>
 * 
 * just adds some required code to ptgen - partition table generator
 * Copyright (C) 2006 by Felix Fietkau <nbd@openwrt.org>
 *
 * uses parts of afdisk
 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#if defined(__linux__)
#include <linux/fs.h>
#endif

#if defined(__APPLE__)
#include <sys/disk.h>
#define BLKGETSIZE DKIOCGETBLOCKCOUNT
#endif

/* Partition table entry */
struct pte { 
	unsigned char active;
	unsigned char chs_start[3];
	unsigned char type;
	unsigned char chs_end[3];
	unsigned int start;
	unsigned int length;
};

struct partinfo {
	unsigned long size;
	int type;
};

int verbose = 0;
int active = 1;
int heads = -1;
int sectors = -1;
struct partinfo parts[4];
char *filename = NULL;

/*
 * get the sector size of the block device
 *
 * print the sector size
 */

static void getmaxsize(char *device) {
	int fd;
	unsigned long maxsectors=0;

	fd = open(device, O_RDONLY);
	ioctl(fd, BLKGETSIZE, &maxsectors);
	printf("%lu\n", maxsectors);
	close(fd);
}

/* 
 * parse the size argument, which is either
 * a simple number (K assumed) or
 * K, M or G
 *
 * returns the size in KByte
 */
static long to_kbytes(const char *string) {
	int exp = 0;
	long result;
	char *end;

	result = strtoul(string, &end, 0);
	switch (tolower(*end)) {
			case 'k' :
			case '\0' : exp = 0; break;
			case 'm' : exp = 1; break;
			case 'g' : exp = 2; break;
			default: return 0;
	}

	if (*end)
		end++;

	if (*end) {
		fprintf(stderr, "garbage after end of number\n");
		return 0;
	}

	/* result: number + 1024^(exp) */
	return result * ((2 << ((10 * exp) - 1)) ?: 1);
}

/* convert the sector number into a CHS value for the partition table */
static void to_chs(long sect, unsigned char chs[3]) {
	int c,h,s;
	
	s = (sect % sectors) + 1;
	sect = sect / sectors;
	h = sect % heads;
	sect = sect / heads;
	c = sect;

	chs[0] = h;
	chs[1] = s | ((c >> 2) & 0xC0);
	chs[2] = c & 0xFF;

	return;
}

/* round the sector number up to the next cylinder */
static inline unsigned long round_to_cyl(long sect) {
	int cyl_size = heads * sectors;

	return sect + cyl_size - (sect % cyl_size); 
}

/* check the partition sizes and write the partition table */
static int gen_ptable(int nr)
{
	struct pte pte[4];
	unsigned long sect = 0; 
	unsigned int start, len;
	int i, fd, ret = -1;

	memset(pte, 0, sizeof(struct pte) * 4);
	for (i = 0; i < nr; i++) {
		if (!parts[i].size) {
			fprintf(stderr, "Invalid size in partition %d!\n", i);
			return -1;
		}
		pte[i].active = ((i + 1) == active) ? 0x80 : 0;
		pte[i].type = parts[i].type;
		pte[i].start = start = sect + sectors;
		sect = round_to_cyl(start + parts[i].size * 2);
		pte[i].length = len = sect - start;
		to_chs(start, pte[i].chs_start);
		to_chs(start + len - 1, pte[i].chs_end);
		if (verbose)
			fprintf(stderr, "Partition %d: start=%u, end=%u, size=%u\n", i, start * 512, (start + len) * 512, len * 512);
	}

	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
		fprintf(stderr, "Can't open output file '%s'\n",filename);
		return -1;
	}

	lseek(fd, 446, SEEK_SET);
	if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
		fprintf(stderr, "write failed.\n");
		goto fail;
	}
	lseek(fd, 510, SEEK_SET);
	if (write(fd, "\x55\xaa", 2) != 2) {
		fprintf(stderr, "write failed.\n");
		goto fail;
	}
	
	ret = 0;
fail:
	close(fd);
	return ret;
}

static void usage(char *prog)
{
	fprintf(stderr,	"Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog);
	fprintf(stderr,	"Usage: %s -g <device>\n", prog);
	exit(1);
}

int main (int argc, char **argv)
{
	char type = 0x83;
	int ch;
	int part = 0;

	while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vg:")) != -1) {
		switch (ch) {
		case 'o':
			filename = optarg;
			break;
		case 'v':
			verbose++;
			break;
		case 'h':
			heads = (int) strtoul(optarg, NULL, 0);
			break;
		case 's':
			sectors = (int) strtoul(optarg, NULL, 0);
			break;
		case 'p':
			if (part > 3) {
				fprintf(stderr, "Too many partitions\n");
				exit(1);
			}
			parts[part].size = to_kbytes(optarg);
			parts[part++].type = type;
			break;
		case 't':
			type = (char) strtoul(optarg, NULL, 16);
			break;
		case 'a':
			active = (int) strtoul(optarg, NULL, 0);
			if ((active < 0) || (active > 4))
				active = 0;
			break;
		case 'g':
			getmaxsize(optarg);
			exit(0);
		case '?':
		default:
			usage(argv[0]);
		}
	}
	argc -= optind;
	if (argc || (heads <= 0) || (sectors <= 0) || !filename) 
		usage(argv[0]);
	
	return gen_ptable(part);
}