OpenWrt – Diff between revs 2 and 3

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 2 Rev 3
1 /* 1 /*
2 * ptgen - partition table generator 2 * ptgen - partition table generator
3 * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name> 3 * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
4 * 4 *
5 * uses parts of afdisk 5 * uses parts of afdisk
6 * Copyright (C) 2002 by David Roetzel <david@roetzel.de> 6 * Copyright (C) 2002 by David Roetzel <david@roetzel.de>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 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 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 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 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 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 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */ 21 */
22   22  
23 #include <sys/types.h> 23 #include <sys/types.h>
24 #include <sys/stat.h> 24 #include <sys/stat.h>
25 #include <string.h> 25 #include <string.h>
26 #include <unistd.h> 26 #include <unistd.h>
27 #include <stdlib.h> 27 #include <stdlib.h>
28 #include <stdio.h> 28 #include <stdio.h>
29 #include <stdint.h> 29 #include <stdint.h>
30 #include <stdbool.h> -  
31 #include <ctype.h> 30 #include <ctype.h>
32 #include <fcntl.h> 31 #include <fcntl.h>
33 #include <stdint.h> 32 #include <stdint.h>
34   33  
35 #if __BYTE_ORDER == __BIG_ENDIAN 34 #if __BYTE_ORDER == __BIG_ENDIAN
36 #define cpu_to_le32(x) bswap_32(x) 35 #define cpu_to_le32(x) bswap_32(x)
37 #elif __BYTE_ORDER == __LITTLE_ENDIAN 36 #elif __BYTE_ORDER == __LITTLE_ENDIAN
38 #define cpu_to_le32(x) (x) 37 #define cpu_to_le32(x) (x)
39 #else 38 #else
40 #error unknown endianness! 39 #error unknown endianness!
41 #endif 40 #endif
42   41  
43 /* Partition table entry */ 42 /* Partition table entry */
44 struct pte { 43 struct pte {
45 uint8_t active; 44 uint8_t active;
46 uint8_t chs_start[3]; 45 uint8_t chs_start[3];
47 uint8_t type; 46 uint8_t type;
48 uint8_t chs_end[3]; 47 uint8_t chs_end[3];
49 uint32_t start; 48 uint32_t start;
50 uint32_t length; 49 uint32_t length;
51 }; 50 };
52   51  
53 struct partinfo { 52 struct partinfo {
54 unsigned long size; 53 unsigned long size;
55 int type; 54 int type;
56 }; 55 };
57   56  
58 int verbose = 0; 57 int verbose = 0;
59 int active = 1; 58 int active = 1;
60 int heads = -1; 59 int heads = -1;
61 int sectors = -1; 60 int sectors = -1;
62 int kb_align = 0; 61 int kb_align = 0;
63 bool ignore_null_sized_partition = false; -  
64 struct partinfo parts[4]; 62 struct partinfo parts[4];
65 char *filename = NULL; 63 char *filename = NULL;
66   64  
67   65  
68 /* 66 /*
69 * parse the size argument, which is either 67 * parse the size argument, which is either
70 * a simple number (K assumed) or 68 * a simple number (K assumed) or
71 * K, M or G 69 * K, M or G
72 * 70 *
73 * returns the size in KByte 71 * returns the size in KByte
74 */ 72 */
75 static long to_kbytes(const char *string) 73 static long to_kbytes(const char *string)
76 { 74 {
77 int exp = 0; 75 int exp = 0;
78 long result; 76 long result;
79 char *end; 77 char *end;
80   78  
81 result = strtoul(string, &end, 0); 79 result = strtoul(string, &end, 0);
82 switch (tolower(*end)) { 80 switch (tolower(*end)) {
83 case 'k' : 81 case 'k' :
84 case '\0' : exp = 0; break; 82 case '\0' : exp = 0; break;
85 case 'm' : exp = 1; break; 83 case 'm' : exp = 1; break;
86 case 'g' : exp = 2; break; 84 case 'g' : exp = 2; break;
87 default: return 0; 85 default: return 0;
88 } 86 }
89   87  
90 if (*end) 88 if (*end)
91 end++; 89 end++;
92   90  
93 if (*end) { 91 if (*end) {
94 fprintf(stderr, "garbage after end of number\n"); 92 fprintf(stderr, "garbage after end of number\n");
95 return 0; 93 return 0;
96 } 94 }
97   95  
98 /* result: number + 1024^(exp) */ 96 /* result: number + 1024^(exp) */
99 if (exp == 0) 97 if (exp == 0)
100 return result; 98 return result;
101 return result * (2 << ((10 * exp) - 1)); 99 return result * (2 << ((10 * exp) - 1));
102 } 100 }
103   101  
104 /* convert the sector number into a CHS value for the partition table */ 102 /* convert the sector number into a CHS value for the partition table */
105 static void to_chs(long sect, unsigned char chs[3]) 103 static void to_chs(long sect, unsigned char chs[3])
106 { 104 {
107 int c,h,s; 105 int c,h,s;
108   106  
109 s = (sect % sectors) + 1; 107 s = (sect % sectors) + 1;
110 sect = sect / sectors; 108 sect = sect / sectors;
111 h = sect % heads; 109 h = sect % heads;
112 sect = sect / heads; 110 sect = sect / heads;
113 c = sect; 111 c = sect;
114   112  
115 chs[0] = h; 113 chs[0] = h;
116 chs[1] = s | ((c >> 2) & 0xC0); 114 chs[1] = s | ((c >> 2) & 0xC0);
117 chs[2] = c & 0xFF; 115 chs[2] = c & 0xFF;
118   116  
119 return; 117 return;
120 } 118 }
121   119  
122 /* round the sector number up to the next cylinder */ 120 /* round the sector number up to the next cylinder */
123 static inline unsigned long round_to_cyl(long sect) 121 static inline unsigned long round_to_cyl(long sect)
124 { 122 {
125 int cyl_size = heads * sectors; 123 int cyl_size = heads * sectors;
126   124  
127 return sect + cyl_size - (sect % cyl_size); 125 return sect + cyl_size - (sect % cyl_size);
128 } 126 }
129   127  
130 /* round the sector number up to the kb_align boundary */ 128 /* round the sector number up to the kb_align boundary */
131 static inline unsigned long round_to_kb(long sect) { 129 static inline unsigned long round_to_kb(long sect) {
132 return ((sect - 1) / kb_align + 1) * kb_align; 130 return ((sect - 1) / kb_align + 1) * kb_align;
133 } 131 }
134   132  
135 /* check the partition sizes and write the partition table */ 133 /* check the partition sizes and write the partition table */
136 static int gen_ptable(uint32_t signature, int nr) 134 static int gen_ptable(uint32_t signature, int nr)
137 { 135 {
138 struct pte pte[4]; 136 struct pte pte[4];
139 unsigned long sect = 0; 137 unsigned long sect = 0;
140 int i, fd, ret = -1, start, len; 138 int i, fd, ret = -1, start, len;
141   139  
142 memset(pte, 0, sizeof(struct pte) * 4); 140 memset(pte, 0, sizeof(struct pte) * 4);
143 for (i = 0; i < nr; i++) { 141 for (i = 0; i < nr; i++) {
144 if (!parts[i].size) { 142 if (!parts[i].size) {
145 if (ignore_null_sized_partition) -  
146 continue; -  
147 fprintf(stderr, "Invalid size in partition %d!\n", i); 143 fprintf(stderr, "Invalid size in partition %d!\n", i);
148 return -1; 144 return -1;
149 } 145 }
150   146  
151 pte[i].active = ((i + 1) == active) ? 0x80 : 0; 147 pte[i].active = ((i + 1) == active) ? 0x80 : 0;
152 pte[i].type = parts[i].type; 148 pte[i].type = parts[i].type;
153   149  
154 start = sect + sectors; 150 start = sect + sectors;
155 if (kb_align != 0) 151 if (kb_align != 0)
156 start = round_to_kb(start); 152 start = round_to_kb(start);
157 pte[i].start = cpu_to_le32(start); 153 pte[i].start = cpu_to_le32(start);
158   154  
159 sect = start + parts[i].size * 2; 155 sect = start + parts[i].size * 2;
160 if (kb_align == 0) 156 if (kb_align == 0)
161 sect = round_to_cyl(sect); 157 sect = round_to_cyl(sect);
162 pte[i].length = cpu_to_le32(len = sect - start); 158 pte[i].length = cpu_to_le32(len = sect - start);
163   159  
164 to_chs(start, pte[i].chs_start); 160 to_chs(start, pte[i].chs_start);
165 to_chs(start + len - 1, pte[i].chs_end); 161 to_chs(start + len - 1, pte[i].chs_end);
166   162  
167 if (verbose) 163 if (verbose)
168 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long)start * 512, ((long)start + (long)len) * 512, (long)len * 512); 164 fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long)start * 512, ((long)start + (long)len) * 512, (long)len * 512);
169 printf("%ld\n", (long)start * 512); 165 printf("%ld\n", (long)start * 512);
170 printf("%ld\n", (long)len * 512); 166 printf("%ld\n", (long)len * 512);
171 } 167 }
172   168  
173 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { 169 if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
174 fprintf(stderr, "Can't open output file '%s'\n",filename); 170 fprintf(stderr, "Can't open output file '%s'\n",filename);
175 return -1; 171 return -1;
176 } 172 }
177   173  
178 lseek(fd, 440, SEEK_SET); 174 lseek(fd, 440, SEEK_SET);
179 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) { 175 if (write(fd, &signature, sizeof(signature)) != sizeof(signature)) {
180 fprintf(stderr, "write failed.\n"); 176 fprintf(stderr, "write failed.\n");
181 goto fail; 177 goto fail;
182 } 178 }
183   179  
184 lseek(fd, 446, SEEK_SET); 180 lseek(fd, 446, SEEK_SET);
185 if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) { 181 if (write(fd, pte, sizeof(struct pte) * 4) != sizeof(struct pte) * 4) {
186 fprintf(stderr, "write failed.\n"); 182 fprintf(stderr, "write failed.\n");
187 goto fail; 183 goto fail;
188 } 184 }
189 lseek(fd, 510, SEEK_SET); 185 lseek(fd, 510, SEEK_SET);
190 if (write(fd, "\x55\xaa", 2) != 2) { 186 if (write(fd, "\x55\xaa", 2) != 2) {
191 fprintf(stderr, "write failed.\n"); 187 fprintf(stderr, "write failed.\n");
192 goto fail; 188 goto fail;
193 } 189 }
194   190  
195 ret = 0; 191 ret = 0;
196 fail: 192 fail:
197 close(fd); 193 close(fd);
198 return ret; 194 return ret;
199 } 195 }
200   196  
201 static void usage(char *prog) 197 static void usage(char *prog)
202 { 198 {
203 fprintf(stderr, "Usage: %s [-v] [-n] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog); 199 fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
204 exit(EXIT_FAILURE); 200 exit(EXIT_FAILURE);
205 } 201 }
206   202  
207 int main (int argc, char **argv) 203 int main (int argc, char **argv)
208 { 204 {
209 char type = 0x83; 205 char type = 0x83;
210 int ch; 206 int ch;
211 int part = 0; 207 int part = 0;
212 uint32_t signature = 0x5452574F; /* 'OWRT' */ 208 uint32_t signature = 0x5452574F; /* 'OWRT' */
213   209  
214 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vnl:S:")) != -1) { 210 while ((ch = getopt(argc, argv, "h:s:p:a:t:o:vl:S:")) != -1) {
215 switch (ch) { 211 switch (ch) {
216 case 'o': 212 case 'o':
217 filename = optarg; 213 filename = optarg;
218 break; 214 break;
219 case 'v': 215 case 'v':
220 verbose++; 216 verbose++;
221 break; 217 break;
222 case 'n': -  
223 ignore_null_sized_partition = true; -  
224 break; -  
225 case 'h': 218 case 'h':
226 heads = (int)strtoul(optarg, NULL, 0); 219 heads = (int)strtoul(optarg, NULL, 0);
227 break; 220 break;
228 case 's': 221 case 's':
229 sectors = (int)strtoul(optarg, NULL, 0); 222 sectors = (int)strtoul(optarg, NULL, 0);
230 break; 223 break;
231 case 'p': 224 case 'p':
232 if (part > 3) { 225 if (part > 3) {
233 fprintf(stderr, "Too many partitions\n"); 226 fprintf(stderr, "Too many partitions\n");
234 exit(EXIT_FAILURE); 227 exit(EXIT_FAILURE);
235 } 228 }
236 parts[part].size = to_kbytes(optarg); 229 parts[part].size = to_kbytes(optarg);
237 parts[part++].type = type; 230 parts[part++].type = type;
238 break; 231 break;
239 case 't': 232 case 't':
240 type = (char)strtoul(optarg, NULL, 16); 233 type = (char)strtoul(optarg, NULL, 16);
241 break; 234 break;
242 case 'a': 235 case 'a':
243 active = (int)strtoul(optarg, NULL, 0); 236 active = (int)strtoul(optarg, NULL, 0);
244 if ((active < 0) || (active > 4)) 237 if ((active < 0) || (active > 4))
245 active = 0; 238 active = 0;
246 break; 239 break;
247 case 'l': 240 case 'l':
248 kb_align = (int)strtoul(optarg, NULL, 0) * 2; 241 kb_align = (int)strtoul(optarg, NULL, 0) * 2;
249 break; 242 break;
250 case 'S': 243 case 'S':
251 signature = strtoul(optarg, NULL, 0); 244 signature = strtoul(optarg, NULL, 0);
252 break; 245 break;
253 case '?': 246 case '?':
254 default: 247 default:
255 usage(argv[0]); 248 usage(argv[0]);
256 } 249 }
257 } 250 }
258 argc -= optind; 251 argc -= optind;
259 if (argc || (heads <= 0) || (sectors <= 0) || !filename) 252 if (argc || (heads <= 0) || (sectors <= 0) || !filename)
260 usage(argv[0]); 253 usage(argv[0]);
261   254  
262 return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS; 255 return gen_ptable(signature, part) ? EXIT_FAILURE : EXIT_SUCCESS;
263 } 256 }
264   257