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>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <arpa/inet.h>
36 #include <sys/ioctl.h>
37 #include <mtd/mtd-user.h>
42 static inline uint32_t le32_to_cpu(uint8_t *buf)
44 return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
47 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
48 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
51 wrgg_fix_md5(struct wrgg03_header *shdr, int fd, size_t data_offset, size_t data_size)
56 unsigned char digest[16];
60 buf = malloc(data_size);
66 res = pread(fd, buf, data_size, data_offset);
67 if (res != data_size) {
74 MD5_Update(&ctx, (char *)&shdr->offset, sizeof(shdr->offset));
75 MD5_Update(&ctx, (char *)&shdr->dev_name, sizeof(shdr->dev_name));
76 MD5_Update(&ctx, buf, data_size);
77 MD5_Final(digest, &ctx);
79 if (!memcmp(digest, shdr->digest, sizeof(digest))) {
81 fprintf(stderr, "the header is fixed already\n");
86 fprintf(stderr, "new size:%u, new MD5: ", data_size);
87 for (i = 0; i < sizeof(digest); i++)
88 fprintf(stderr, "%02x", digest[i]);
90 fprintf(stderr, "\n");
93 /* update the size in the image */
94 shdr->size = data_size;
96 /* update the checksum in the image */
97 memcpy(shdr->digest, digest, sizeof(digest));
106 mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size)
113 struct wrgg03_header *shdr;
116 fprintf(stderr, "Trying to fix WRGG header in %s at 0x%x...\n",
119 block_offset = offset & ~(erasesize - 1);
120 offset -= block_offset;
122 fd = mtd_check_open(mtd);
124 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
128 if (block_offset + erasesize > mtdsize) {
129 fprintf(stderr, "Offset too large, device size 0x%x\n",
134 first_block = malloc(erasesize);
140 res = pread(fd, first_block, erasesize, block_offset);
141 if (res != erasesize) {
146 shdr = (struct wrgg03_header *)(first_block + offset);
148 /* The magic is always stored in little-endian byte order */
149 if (le32_to_cpu((uint8_t *)&shdr->magic1) != WRGG03_MAGIC) {
150 fprintf(stderr, "magic1 = %x\n", shdr->magic1);
151 fprintf(stderr, "WRGG03_MAGIC = %x\n", WRGG03_MAGIC);
152 fprintf(stderr, "No WRGG header found\n");
154 } else if (!shdr->size) {
155 fprintf(stderr, "WRGG entity with empty image\n");
159 data_offset = offset + sizeof(struct wrgg03_header);
161 data_size = mtdsize - data_offset;
162 if (data_size > shdr->size)
163 data_size = shdr->size;
164 if (wrgg_fix_md5(shdr, fd, data_offset, data_size))
167 if (mtd_erase_block(fd, block_offset)) {
168 fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
169 block_offset, strerror(errno));
174 fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
176 if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
177 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
182 fprintf(stderr, "Done.\n");