4 * Copyright (C) 2005 Mike Baker
5 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
6 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
7 * Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
8 * Copyright (C) 2017 George Hopkins <george-hopkins@null.net>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <arpa/inet.h>
38 #include <sys/ioctl.h>
39 #include <mtd/mtd-user.h>
43 #if !defined(__BYTE_ORDER)
44 #error "Unknown byte order"
47 #if __BYTE_ORDER == __BIG_ENDIAN
48 #define cpu_to_le32(x) bswap_32(x)
49 #define le32_to_cpu(x) bswap_32(x)
50 #elif __BYTE_ORDER == __LITTLE_ENDIAN
51 #define cpu_to_le32(x) (x)
52 #define le32_to_cpu(x) (x)
54 #error "Unsupported endianness"
57 #define WRG_MAGIC 0x20040220
67 } __attribute__ ((packed
));
69 ssize_t
pread(int fd
, void *buf
, size_t count
, off_t offset
);
70 ssize_t
pwrite(int fd
, const void *buf
, size_t count
, off_t offset
);
73 wrg_fix_md5(struct wrg_header
*shdr
, int fd
, size_t data_offset
, size_t data_size
)
78 unsigned char digest
[16];
82 buf
= malloc(data_size
);
88 res
= pread(fd
, buf
, data_size
, data_offset
);
89 if (res
!= data_size
) {
96 MD5_Update(&ctx
, (char *)&shdr
->offset
, sizeof(shdr
->offset
));
97 MD5_Update(&ctx
, (char *)&shdr
->devname
, sizeof(shdr
->devname
));
98 MD5_Update(&ctx
, buf
, data_size
);
99 MD5_Final(digest
, &ctx
);
101 if (!memcmp(digest
, shdr
->digest
, sizeof(digest
))) {
103 fprintf(stderr
, "the header is fixed already\n");
108 fprintf(stderr
, "new size: %u, new MD5: ", data_size
);
109 for (i
= 0; i
< sizeof(digest
); i
++)
110 fprintf(stderr
, "%02x", digest
[i
]);
112 fprintf(stderr
, "\n");
115 /* update the size in the image */
116 shdr
->size
= cpu_to_le32(data_size
);
118 /* update the checksum in the image */
119 memcpy(shdr
->digest
, digest
, sizeof(digest
));
128 mtd_fixwrg(const char *mtd
, size_t offset
, size_t data_size
)
135 struct wrg_header
*shdr
;
138 fprintf(stderr
, "Trying to fix WRG header in %s at 0x%x...\n",
141 block_offset
= offset
& ~(erasesize
- 1);
142 offset
-= block_offset
;
144 fd
= mtd_check_open(mtd
);
146 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
150 if (block_offset
+ erasesize
> mtdsize
) {
151 fprintf(stderr
, "Offset too large, device size 0x%x\n",
156 first_block
= malloc(erasesize
);
162 res
= pread(fd
, first_block
, erasesize
, block_offset
);
163 if (res
!= erasesize
) {
168 shdr
= (struct wrg_header
*)(first_block
+ offset
);
169 if (le32_to_cpu(shdr
->magic1
) != WRG_MAGIC
) {
170 fprintf(stderr
, "No WRG header found (%08x != %08x)\n",
171 le32_to_cpu(shdr
->magic1
), WRG_MAGIC
);
173 } else if (!le32_to_cpu(shdr
->size
)) {
174 fprintf(stderr
, "WRG entity with empty image\n");
178 data_offset
= offset
+ sizeof(struct wrg_header
);
180 data_size
= mtdsize
- data_offset
;
181 if (data_size
> le32_to_cpu(shdr
->size
))
182 data_size
= le32_to_cpu(shdr
->size
);
183 if (wrg_fix_md5(shdr
, fd
, data_offset
, data_size
))
186 if (mtd_erase_block(fd
, block_offset
)) {
187 fprintf(stderr
, "Can't erease block at 0x%x (%s)\n",
188 block_offset
, strerror(errno
));
193 fprintf(stderr
, "Rewriting block at 0x%x\n", block_offset
);
195 if (pwrite(fd
, first_block
, erasesize
, block_offset
) != erasesize
) {
196 fprintf(stderr
, "Error writing block (%s)\n", strerror(errno
));
201 fprintf(stderr
, "Done.\n");