f48317589c220ebbc723cde4791564e3d56b9672
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>
36 #define TRX_MAGIC 0x30524448 /* "HDR0" */
38 unsigned magic
; /* "HDR0" */
39 unsigned len
; /* Length of file including header */
40 unsigned crc32
; /* 32-bit CRC from flag_version to end of file */
41 unsigned flag_version
; /* 0:15 flags, 16:31 version */
42 unsigned offsets
[3]; /* Offsets of partitions from start of header */
46 trx_fixup(int fd
, const char *name
)
48 struct mtd_info_user mtdInfo
;
50 struct trx_header
*trx
;
54 if (ioctl(fd
, MEMGETINFO
, &mtdInfo
) < 0) {
55 fprintf(stderr
, "Failed to get mtd info\n");
60 if (mtdInfo
.size
<= 0) {
61 fprintf(stderr
, "Invalid MTD device size\n");
65 bfd
= mtd_open(name
, true);
66 ptr
= mmap(NULL
, len
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, bfd
, 0);
67 if (!ptr
|| (ptr
== (void *) -1)) {
73 if (trx
->magic
!= TRX_MAGIC
) {
74 fprintf(stderr
, "TRX header not found\n");
79 scan
= ptr
+ offsetof(struct trx_header
, flag_version
);
80 trx
->crc32
= crc32buf(scan
, trx
->len
- (scan
- ptr
));
81 msync(ptr
, sizeof(struct trx_header
), MS_SYNC
|MS_INVALIDATE
);
89 fprintf(stderr
, "Error fixing up TRX header\n");
94 trx_check(int imagefd
, const char *mtd
, char *buf
, int *len
)
96 const struct trx_header
*trx
= (const struct trx_header
*) buf
;
99 if (strcmp(mtd
, "linux") != 0)
102 *len
= read(imagefd
, buf
, 32);
104 fprintf(stdout
, "Could not get image header, file too small (%d bytes)\n", *len
);
108 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
110 fprintf(stderr
, "Bad trx header\n");
111 fprintf(stderr
, "This is not the correct file format; refusing to flash.\n"
112 "Please specify the correct file or use -f to force.\n");
117 /* check if image fits to mtd device */
118 fd
= mtd_check_open(mtd
);
120 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
124 if(mtdsize
< trx
->len
) {
125 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);