make ptgen print the end offset of the last partition as well
[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
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); 
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         printf("%ld\n", ((long) (start + len) * 512));
145
146         if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) < 0) {
147                 fprintf(stderr, "Can't open output file '%s'\n",filename);
148                 return -1;
149         }
150
151         lseek(fd, 446, SEEK_SET);
152         if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
153                 fprintf(stderr, "write failed.\n");
154                 goto fail;
155         }
156         lseek(fd, 510, SEEK_SET);
157         if (write(fd, "\x55\xaa", 2) != 2) {
158                 fprintf(stderr, "write failed.\n");
159                 goto fail;
160         }
161         
162         ret = 0;
163 fail:
164         close(fd);
165         return ret;
166 }
167
168 static void usage(char *prog)
169 {
170         fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [[-t <type>] -p <size>...] \n", prog);
171         exit(1);
172 }
173
174 int main (int argc, char **argv)
175 {
176         char type = 0x83;
177         int ch;
178         int part = 0;
179
180         while ((ch = getopt(argc, argv, "h:s:p:a:t:o:v")) != -1) {
181                 switch (ch) {
182                 case 'o':
183                         filename = optarg;
184                         break;
185                 case 'v':
186                         verbose++;
187                         break;
188                 case 'h':
189                         heads = (int) strtoul(optarg, NULL, 0);
190                         break;
191                 case 's':
192                         sectors = (int) strtoul(optarg, NULL, 0);
193                         break;
194                 case 'p':
195                         if (part > 3) {
196                                 fprintf(stderr, "Too many partitions\n");
197                                 exit(1);
198                         }
199                         parts[part].size = to_kbytes(optarg);
200                         parts[part++].type = type;
201                         break;
202                 case 't':
203                         type = (char) strtoul(optarg, NULL, 16);
204                         break;
205                 case 'a':
206                         active = (int) strtoul(optarg, NULL, 0);
207                         if ((active < 0) || (active > 4))
208                                 active = 0;
209                         break;
210                 case '?':
211                 default:
212                         usage(argv[0]);
213                 }
214         }
215         argc -= optind;
216         if (argc || (heads <= 0) || (sectors <= 0) || !filename) 
217                 usage(argv[0]);
218         
219         return gen_ptable(part);
220 }