firmware-tools/ptgen: use portable exit codes
[oweals/openwrt.git] / tools / firmware-utils / src / ptgen.c
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 */
43 struct 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
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 int kb_align = 0;
62 struct partinfo parts[4];
63 char *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  */
73 static long to_kbytes(const char *string)
74 {
75         int exp = 0;
76         long result;
77         char *end;
78
79         result = strtoul(string, &end, 0);
80         switch (tolower(*end)) {
81                 case 'k' :
82                 case '\0' : exp = 0; break;
83                 case 'm' : exp = 1; break;
84                 case 'g' : exp = 2; break;
85                 default: return 0;
86         }
87
88         if (*end)
89                 end++;
90
91         if (*end) {
92                 fprintf(stderr, "garbage after end of number\n");
93                 return 0;
94         }
95
96         /* result: number + 1024^(exp) */
97         if (exp == 0)
98                 return result;
99         return result * (2 << ((10 * exp) - 1));
100 }
101
102 /* convert the sector number into a CHS value for the partition table */
103 static void to_chs(long sect, unsigned char chs[3])
104 {
105         int c,h,s;
106
107         s = (sect % sectors) + 1;
108         sect = sect / sectors;
109         h = sect % heads;
110         sect = sect / heads;
111         c = sect;
112
113         chs[0] = h;
114         chs[1] = s | ((c >> 2) & 0xC0);
115         chs[2] = c & 0xFF;
116
117         return;
118 }
119
120 /* round the sector number up to the next cylinder */
121 static inline unsigned long round_to_cyl(long sect)
122 {
123         int cyl_size = heads * sectors;
124
125         return sect + cyl_size - (sect % cyl_size);
126 }
127
128 /* round the sector number up to the kb_align boundary */
129 static inline unsigned long round_to_kb(long sect) {
130         return ((sect - 1) / kb_align + 1) * kb_align;
131 }
132
133 /* check the partition sizes and write the partition table */
134 static int gen_ptable(uint32_t signature, int nr)
135 {
136         struct pte pte[4];
137         unsigned long sect = 0;
138         int i, fd, ret = -1, start, len;
139
140         memset(pte, 0, sizeof(struct pte) * 4);
141         for (i = 0; i < nr; i++) {
142                 if (!parts[i].size) {
143                         fprintf(stderr, "Invalid size in partition %d!\n", i);
144                         return -1;
145                 }
146
147                 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
148                 pte[i].type = parts[i].type;
149
150                 start = sect + sectors;
151                 if (kb_align != 0)
152                         start = round_to_kb(start);
153                 pte[i].start = cpu_to_le32(start);
154
155                 sect = start + parts[i].size * 2;
156                 if (kb_align == 0)
157                         sect = round_to_cyl(sect);
158                 pte[i].length = cpu_to_le32(len = sect - start);
159
160                 to_chs(start, pte[i].chs_start);
161                 to_chs(start + len - 1, pte[i].chs_end);
162
163                 if (verbose)
164                         fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long)start * 512, ((long)start + (long)len) * 512, (long)len * 512);
165                 printf("%ld\n", (long)start * 512);
166                 printf("%ld\n", (long)len * 512);
167         }
168
169         if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
170                 fprintf(stderr, "Can't open output file '%s'\n",filename);
171                 return -1;
172         }
173
174         lseek(fd, 440, SEEK_SET);
175         if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
176                 fprintf(stderr, "write failed.\n");
177                 goto fail;
178         }
179
180         lseek(fd, 446, SEEK_SET);
181         if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
182                 fprintf(stderr, "write failed.\n");
183                 goto fail;
184         }
185         lseek(fd, 510, SEEK_SET);
186         if (write(fd, "\x55\xaa", 2) != 2) {
187                 fprintf(stderr, "write failed.\n");
188                 goto fail;
189         }
190
191         ret = 0;
192 fail:
193         close(fd);
194         return ret;
195 }
196
197 static void usage(char *prog)
198 {
199         fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
200         exit(EXIT_FAILURE);
201 }
202
203 int main (int argc, char **argv)
204 {
205         char type = 0x83;
206         int ch;
207         int part = 0;
208         uint32_t signature = 0x5452574F; /* 'OWRT' */
209
210         while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vl:S:")) != -1) {
211                 switch (ch) {
212                 case 'o':
213                         filename = optarg;
214                         break;
215                 case 'v':
216                         verbose++;
217                         break;
218                 case 'h':
219                         heads = (int)strtoul(optarg, NULL, 0);
220                         break;
221                 case 's':
222                         sectors = (int)strtoul(optarg, NULL, 0);
223                         break;
224                 case 'p':
225                         if (part > 3) {
226                                 fprintf(stderr, "Too many partitions\n");
227                                 exit(EXIT_FAILURE);
228                         }
229                         parts[part].size = to_kbytes(optarg);
230                         parts[part++].type = type;
231                         break;
232                 case 't':
233                         type = (char)strtoul(optarg, NULL, 16);
234                         break;
235                 case 'a':
236                         active = (int)strtoul(optarg, NULL, 0);
237                         if ((active < 0) || (active > 4))
238                                 active = 0;
239                         break;
240                 case 'l':
241                         kb_align = (int)strtoul(optarg, NULL, 0) * 2;
242                         break;
243                 case 'S':
244                         signature = strtoul(optarg, NULL, 0);
245                         break;
246                 case '?':
247                 default:
248                         usage(argv[0]);
249                 }
250         }
251         argc -= optind;
252         if (argc || (heads <= 0) || (sectors <= 0) || !filename)
253                 usage(argv[0]);
254
255         return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
256 }