4 * Copyright (C) 2005 Mike Baker
5 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <sys/ioctl.h>
35 #define TRX_MAGIC 0x30524448 /* "HDR0" */
37 unsigned magic
; /* "HDR0" */
38 unsigned len
; /* Length of file including header */
39 unsigned crc32
; /* 32-bit CRC from flag_version to end of file */
40 unsigned flag_version
; /* 0:15 flags, 16:31 version */
41 unsigned offsets
[3]; /* Offsets of partitions from start of header */
44 static unsigned long *crc32
= NULL
;
46 static void init_crc32()
49 unsigned long poly
= 0xEDB88320L
;
55 crc32
= (unsigned long *) malloc(256 * sizeof(unsigned long));
61 for (n
= 0; n
< 256; n
++) {
62 crc
= (unsigned long) n
;
63 for (bit
= 0; bit
< 8; bit
++)
64 crc
= (crc
& 1) ? (poly
^ (crc
>> 1)) : (crc
>> 1);
69 static unsigned int crc32buf(char *buf
, size_t len
)
71 unsigned int crc
= 0xFFFFFFFF;
72 for (; len
; len
--, buf
++)
73 crc
= crc32
[(crc
^ *buf
) & 0xff] ^ (crc
>> 8);
78 trx_fixup(int fd
, const char *name
)
80 struct mtd_info_user mtdInfo
;
82 struct trx_header
*trx
;
86 if (ioctl(fd
, MEMGETINFO
, &mtdInfo
) < 0) {
87 fprintf(stderr
, "Failed to get mtd info\n");
92 if (mtdInfo
.size
<= 0) {
93 fprintf(stderr
, "Invalid MTD device size\n");
97 bfd
= mtd_open(name
, true);
98 ptr
= mmap(NULL
, len
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, bfd
, 0);
99 if (!ptr
|| (ptr
== (void *) -1)) {
105 if (trx
->magic
!= TRX_MAGIC
) {
106 fprintf(stderr
, "TRX header not found\n");
111 scan
= ptr
+ offsetof(struct trx_header
, flag_version
);
112 trx
->crc32
= crc32buf(scan
, trx
->len
- (scan
- ptr
));
113 msync(ptr
, sizeof(struct trx_header
), MS_SYNC
|MS_INVALIDATE
);
121 fprintf(stderr
, "Error fixing up TRX header\n");
126 trx_check(int imagefd
, const char *mtd
, char *buf
, int *len
)
128 const struct trx_header
*trx
= (const struct trx_header
*) buf
;
131 if (strcmp(mtd
, "linux") != 0)
134 *len
= read(imagefd
, buf
, 32);
136 fprintf(stdout
, "Could not get image header, file too small (%d bytes)\n", *len
);
140 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
142 fprintf(stderr
, "Bad trx header\n");
143 fprintf(stderr
, "This is not the correct file format; refusing to flash.\n"
144 "Please specify the correct file or use -f to force.\n");
149 /* check if image fits to mtd device */
150 fd
= mtd_check_open(mtd
);
152 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
156 if(mtdsize
< trx
->len
) {
157 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
This page took 0.049318 seconds and 5 git commands to generate.