source: freewrt/package/mtd/mtd.c@ 6fc4520e

freewrt_1_0 freewrt_2_0
Last change on this file since 6fc4520e was 20d0d4e, checked in by Thorsten Glaser <tg@…>, 19 years ago

allow -F to force to not check, for non-TRX partitions

git-svn-id: svn://www.freewrt.org/trunk/freewrt@695 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * mtd - simple memory technology device manipulation tool
3 *
4 * Copyright (c) 2006 Thorsten Glaser <tg@freewrt.org>
5 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
6 * Felix Fietkau <nbd@openwrt.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * $Id: mtd.c 3588 2006-04-05 02:09:22Z nbd $
23 *
24 * The code is based on the linux-mtd examples.
25 */
26
27#include <sys/param.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/reboot.h>
33#include <limits.h>
34#include <unistd.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <stdint.h>
38#include <fcntl.h>
39#include <errno.h>
40#include <error.h>
41#include <time.h>
42#include <string.h>
43
44#include <linux/mtd/mtd.h>
45
46#define TRX_MAGIC 0x30524448 /* "HDR0" */
47#define BUFSIZE (16 * 1024)
48#define MAX_ARGS 8
49
50#define DEBUG
51
52#define SYSTYPE_UNKNOWN 0
53#define SYSTYPE_BROADCOM 1
54/* to be continued */
55
56struct trx_header {
57 uint32_t magic; /* "HDR0" */
58 uint32_t len; /* Length of file including header */
59 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
60 uint32_t flag_version; /* 0:15 flags, 16:31 version */
61 uint32_t offsets[3]; /* Offsets of partitions from start of header */
62};
63
64char buf[BUFSIZE];
65int buflen;
66
67int
68image_check_bcom(int imagefd, const char *mtd)
69{
70 struct trx_header *trx = (struct trx_header *) buf;
71 struct mtd_info_user mtdInfo;
72 int fd;
73
74 buflen = read(imagefd, buf, 32);
75 if (buflen < 32) {
76 fprintf(stdout, "Could not get image header, file too small (%ld bytes)\n", buflen);
77 return 0;
78 }
79
80 switch(trx->magic) {
81 case 0x47343557: /* W54G */
82 case 0x53343557: /* W54S */
83 case 0x73343557: /* W54s */
84 case 0x46343557: /* W54F */
85 case 0x55343557: /* W54U */
86 /* ignore the first 32 bytes */
87 buflen = read(imagefd, buf, sizeof(struct trx_header));
88 break;
89 }
90
91 if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
92 fprintf(stderr, "Bad trx header\n");
93 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
94 "original firmware files are, use following command to convert to trx:\n"
95 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
96 return 0;
97 }
98
99 /* check if image fits to mtd device */
100 fd = mtd_open(mtd, O_RDWR | O_SYNC);
101 if(fd < 0) {
102 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
103 exit(1);
104 }
105
106 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
107 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
108 exit(1);
109 }
110
111 if(mtdInfo.size < trx->len) {
112 fprintf(stderr, "Image too big for partition: %s\n", mtd);
113 close(fd);
114 return 0;
115 }
116
117 close(fd);
118 return 1;
119}
120
121int
122image_check(int imagefd, const char *mtd)
123{
124 int fd, systype;
125 size_t count;
126 char *c;
127 FILE *f;
128
129 systype = SYSTYPE_UNKNOWN;
130 f = fopen("/proc/cpuinfo", "r");
131 while (!feof(f) && (fgets(buf, BUFSIZE - 1, f) != NULL)) {
132 if ((strncmp(buf, "system type", 11) == 0) && (c = strchr(buf, ':'))) {
133 c += 2;
134 if (strncmp(c, "Broadcom BCM947XX", 17) == 0)
135 systype = SYSTYPE_BROADCOM;
136 }
137 }
138 fclose(f);
139
140 switch(systype) {
141 case SYSTYPE_BROADCOM:
142 return image_check_bcom(imagefd, mtd);
143 default:
144 return 1;
145 }
146}
147
148int mtd_check(char *mtd)
149{
150 struct mtd_info_user mtdInfo;
151 int fd;
152
153 fd = mtd_open(mtd, O_RDWR | O_SYNC);
154 if(fd < 0) {
155 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
156 return 0;
157 }
158
159 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
160 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
161 close(fd);
162 return 0;
163 }
164
165 close(fd);
166 return 1;
167}
168
169int
170mtd_unlock(const char *mtd)
171{
172 int fd;
173 struct mtd_info_user mtdInfo;
174 struct erase_info_user mtdLockInfo;
175
176 fd = mtd_open(mtd, O_RDWR | O_SYNC);
177 if(fd < 0) {
178 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
179 exit(1);
180 }
181
182 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
183 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
184 close(fd);
185 exit(1);
186 }
187
188 mtdLockInfo.start = 0;
189 mtdLockInfo.length = mtdInfo.size;
190 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
191 close(fd);
192 return 0;
193 }
194
195 close(fd);
196 return 0;
197}
198
199int
200mtd_open(const char *mtd, int flags)
201{
202 FILE *fp;
203 char dev[PATH_MAX];
204 int i;
205
206 if ((fp = fopen("/proc/mtd", "r"))) {
207 while (fgets(dev, sizeof(dev), fp)) {
208 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
209 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
210 fclose(fp);
211 return open(dev, flags);
212 }
213 }
214 fclose(fp);
215 }
216
217 return open(mtd, flags);
218}
219
220int
221mtd_erase(const char *mtd)
222{
223 int fd;
224 struct mtd_info_user mtdInfo;
225 struct erase_info_user mtdEraseInfo;
226
227 fd = mtd_open(mtd, O_RDWR | O_SYNC);
228 if(fd < 0) {
229 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
230 exit(1);
231 }
232
233 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
234 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
235 close(fd);
236 exit(1);
237 }
238
239 mtdEraseInfo.length = mtdInfo.erasesize;
240
241 for (mtdEraseInfo.start = 0;
242 mtdEraseInfo.start < mtdInfo.size;
243 mtdEraseInfo.start += mtdInfo.erasesize) {
244
245 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
246 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
247 fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
248 close(fd);
249 exit(1);
250 }
251 }
252
253 close(fd);
254 return 0;
255
256}
257
258int
259mtd_write(int imagefd, const char *mtd, int quiet)
260{
261 int fd, i, result;
262 size_t r, w, e;
263 struct mtd_info_user mtdInfo;
264 struct erase_info_user mtdEraseInfo;
265 int ret = 0;
266
267 fd = mtd_open(mtd, O_RDWR | O_SYNC);
268 if(fd < 0) {
269 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
270 exit(1);
271 }
272
273 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
274 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
275 close(fd);
276 exit(1);
277 }
278
279 r = w = e = 0;
280 if (!quiet)
281 fprintf(stderr, " [ ]");
282
283 for (;;) {
284 /* buffer may contain data already (from trx check) */
285 r = buflen;
286 r += read(imagefd, buf + buflen, BUFSIZE - buflen);
287 w += r;
288
289 /* EOF */
290 if (r <= 0) break;
291
292 /* need to erase the next block before writing data to it */
293 while (w > e) {
294 mtdEraseInfo.start = e;
295 mtdEraseInfo.length = mtdInfo.erasesize;
296
297 if (!quiet)
298 fprintf(stderr, "\b\b\b[e]");
299 /* erase the chunk */
300 if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
301 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
302 exit(1);
303 }
304 e += mtdInfo.erasesize;
305 }
306
307 if (!quiet)
308 fprintf(stderr, "\b\b\b[w]");
309
310 if ((result = write(fd, buf, r)) < r) {
311 if (result < 0) {
312 fprintf(stderr, "Error writing image.\n");
313 exit(1);
314 } else {
315 fprintf(stderr, "Insufficient space.\n");
316 exit(1);
317 }
318 }
319
320 buflen = 0;
321 }
322 if (!quiet)
323 fprintf(stderr, "\b\b\b\b");
324
325 close(fd);
326 return 0;
327}
328
329void usage(void)
330{
331 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
332 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
333 "mtd recognizes these commands:\n"
334 " unlock unlock the device\n"
335 " erase erase all data on device\n"
336 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
337 "Following options are available:\n"
338 " -q quiet mode (once: no [w] on writing,\n"
339 " twice: no status messages)\n"
340 " -r reboot after successful command\n"
341 " -f force write without trx checks\n"
342 " -e <device> erase <device> before executing the command\n\n"
343 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
344 " mtd -r write linux.trx linux\n\n");
345 exit(1);
346}
347
348int main (int argc, char **argv)
349{
350 int ch, i, boot, unlock, imagefd, force, quiet, unlocked;
351 char *erase[MAX_ARGS], *device, *imagefile;
352 enum {
353 CMD_ERASE,
354 CMD_WRITE,
355 CMD_UNLOCK
356 } cmd;
357
358 erase[0] = NULL;
359 boot = 0;
360 force = 0;
361 buflen = 0;
362 quiet = 0;
363
364 while ((ch = getopt(argc, argv, "Ffrqe:")) != -1)
365 switch (ch) {
366 case 'F':
367 quiet = 1;
368 /* FALLTHROUGH */
369 case 'f':
370 force = 1;
371 break;
372 case 'r':
373 boot = 1;
374 break;
375 case 'q':
376 quiet++;
377 break;
378 case 'e':
379 i = 0;
380 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
381 i++;
382
383 erase[i++] = optarg;
384 erase[i] = NULL;
385 break;
386
387 case '?':
388 default:
389 usage();
390 }
391 argc -= optind;
392 argv += optind;
393
394 if (argc < 2)
395 usage();
396
397 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
398 cmd = CMD_UNLOCK;
399 device = argv[1];
400 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
401 cmd = CMD_ERASE;
402 device = argv[1];
403 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
404 cmd = CMD_WRITE;
405 device = argv[2];
406
407 if (strcmp(argv[1], "-") == 0) {
408 imagefile = "<stdin>";
409 imagefd = 0;
410 } else {
411 imagefile = argv[1];
412 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
413 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
414 exit(1);
415 }
416 }
417
418 /* check trx file before erasing or writing anything */
419 if (!(force && quiet)) if (!image_check(imagefd, device)) {
420 if ((quiet < 2) || !force)
421 fprintf(stderr, "TRX check failed!\n");
422 if (!force)
423 exit(1);
424 }
425 if (!mtd_check(device)) {
426 fprintf(stderr, "Can't open device for writing!\n");
427 exit(1);
428 }
429 } else {
430 usage();
431 }
432
433 sync();
434
435 i = 0;
436 unlocked = 0;
437 while (erase[i] != NULL) {
438 if (quiet < 2)
439 fprintf(stderr, "Unlocking %s ...\n", erase[i]);
440 mtd_unlock(erase[i]);
441 if (quiet < 2)
442 fprintf(stderr, "Erasing %s ...\n", erase[i]);
443 mtd_erase(erase[i]);
444 if (strcmp(erase[i], device) == 0)
445 unlocked = 1;
446 i++;
447 }
448
449 if (!unlocked) {
450 if (quiet < 2)
451 fprintf(stderr, "Unlocking %s ...\n", device);
452 mtd_unlock(device);
453 }
454
455 switch (cmd) {
456 case CMD_UNLOCK:
457 break;
458 case CMD_ERASE:
459 if (quiet < 2)
460 fprintf(stderr, "Erasing %s ...\n", device);
461 mtd_erase(device);
462 break;
463 case CMD_WRITE:
464 if (quiet < 2)
465 fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
466 mtd_write(imagefd, device, quiet);
467 if (quiet < 2)
468 fprintf(stderr, "\n");
469 break;
470 }
471
472 sync();
473
474 if (boot)
475 kill(1, 15); // send SIGTERM to init for reboot
476
477 return 0;
478}
Note: See TracBrowser for help on using the repository browser.