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 #include "freebsd.h"
33
34 #if __BYTE_ORDER == __BIG_ENDIAN
35 #define cpu_to_le16(x) bswap_16(x)
36 #elif __BYTE_ORDER == __LITTLE_ENDIAN
37 #define cpu_to_le16(x) (x)
38 #else
39 #error unknown endianness!
40 #endif
41
42 /* Partition table entry */
43 struct pte {
44 unsigned char active;
45 unsigned char chs_start[3];
46 unsigned char type;
47 unsigned char chs_end[3];
48 unsigned int start;
49 unsigned int length;
50 };
51
52 struct partinfo {
53 unsigned long size;
54 int type;
55 };
56
57 int verbose = 0;
58 int active = 1;
59 int heads = -1;
60 int sectors = -1;
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 */
to_kbytes(const char * string)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 */
to_chs(long sect,unsigned char chs[3])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 */
round_to_cyl(long sect)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 /* check the partition sizes and write the partition table */
gen_ptable(int nr)123 static int gen_ptable(int nr)
124 {
125 struct pte pte[4];
126 unsigned long sect = 0;
127 int i, fd, ret = -1, start, len;
128
129 memset(pte, 0, sizeof(struct pte) * 4);
130 for (i = 0; i < nr; i++) {
131 if (!parts[i].size) {
132 fprintf(stderr, "Invalid size in partition %d!\n", i);
133 return -1;
134 }
135 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
136 pte[i].type = parts[i].type;
137 pte[i].start = cpu_to_le16(start = sect + sectors);
138 sect = round_to_cyl(start + parts[i].size * 2);
139 pte[i].length = cpu_to_le16(len = sect - start);
140 to_chs(start, pte[i].chs_start);
141 to_chs(start + len - 1, pte[i].chs_end);
142 if (verbose)
143 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long) start * 512, ((long) start + (long) len) * 512, (long) len * 512);
144 printf("%ld\n", ((long) start * 512));
145 printf("%ld\n", ((long) len * 512));
146 }
147
148 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
149 fprintf(stderr, "Can't open output file '%s'\n",filename);
150 return -1;
151 }
152
153 lseek(fd, 446, SEEK_SET);
154 if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
155 fprintf(stderr, "write failed.\n");
156 goto fail;
157 }
158 lseek(fd, 510, SEEK_SET);
159 if (write(fd, "\x55\xaa", 2) != 2) {
160 fprintf(stderr, "write failed.\n");
161 goto fail;
162 }
163
164 ret = 0;
165 fail:
166 close(fd);
167 return ret;
168 }
169
usage(char * prog)170 static void usage(char *prog)
171 {
172 fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog);
173 exit(1);
174 }
175
main(int argc,char ** argv)176 int main (int argc, char **argv)
177 {
178 char type = 0x83;
179 int ch;
180 int part = 0;
181
182 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:v")) != -1) {
183 switch (ch) {
184 case 'o':
185 filename = optarg;
186 break;
187 case 'v':
188 verbose++;
189 break;
190 case 'h':
191 heads = (int) strtoul(optarg, NULL, 0);
192 break;
193 case 's':
194 sectors = (int) strtoul(optarg, NULL, 0);
195 break;
196 case 'p':
197 if (part > 3) {
198 fprintf(stderr, "Too many partitions\n");
199 exit(1);
200 }
201 parts[part].size = to_kbytes(optarg);
202 parts[part++].type = type;
203 break;
204 case 't':
205 type = (char) strtoul(optarg, NULL, 16);
206 break;
207 case 'a':
208 active = (int) strtoul(optarg, NULL, 0);
209 if ((active < 0) || (active > 4))
210 active = 0;
211 break;
212 case '?':
213 default:
214 usage(argv[0]);
215 }
216 }
217 argc -= optind;
218 if (argc || (heads <= 0) || (sectors <= 0) || !filename)
219 usage(argv[0]);
220
221 return gen_ptable(part);
222 }
223