4 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
6 * Based on the trx fixup code:
7 * Copyright (C) 2005 Mike Baker
8 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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.
34 #include <arpa/inet.h>
36 #include <sys/ioctl.h>
37 #include <mtd/mtd-user.h>
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 #define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
44 #elif __BYTE_ORDER == __LITTLE_ENDIAN
45 #define STORE32_LE(X) (X)
47 #error unknown endianness!
50 ssize_t
pread(int fd
, void *buf
, size_t count
, off_t offset
);
51 ssize_t
pwrite(int fd
, const void *buf
, size_t count
, off_t offset
);
54 seama_fix_md5(char *buf
, size_t len
)
56 struct seama_hdr
*shdr
;
61 unsigned char digest
[16];
64 if (len
< sizeof(struct seama_hdr
))
67 shdr
= (struct seama_hdr
*) buf
;
68 if (shdr
->magic
!= htonl(SEAMA_MAGIC
)) {
69 fprintf(stderr
, "no SEAMA header found\n");
73 isize
= ntohl(shdr
->size
);
74 msize
= ntohs(shdr
->metasize
);
76 /* the image contains no checksum */
80 len
-= sizeof(struct seama_hdr
) + sizeof(digest
) + msize
;
84 data
= buf
+ sizeof(struct seama_hdr
) + sizeof(digest
) + msize
;
87 MD5_Update(&ctx
, data
, isize
);
88 MD5_Final(digest
, &ctx
);
90 if (!memcmp(digest
, &buf
[sizeof(struct seama_hdr
)], sizeof(digest
))) {
92 fprintf(stderr
, "the header is fixed already\n");
97 fprintf(stderr
, "new size:%u, new MD5: ", isize
);
98 for (i
= 0; i
< sizeof(digest
); i
++)
99 fprintf(stderr
, "%02x", digest
[i
]);
101 fprintf(stderr
, "\n");
104 /* update the size in the image */
105 shdr
->size
= htonl(isize
);
107 /* update the checksum in the image */
108 for (i
= 0; i
< sizeof(digest
); i
++)
109 buf
[sizeof(struct seama_hdr
) + i
] = digest
[i
];
115 mtd_fixseama(const char *mtd
, size_t offset
)
123 fprintf(stderr
, "Trying to fix SEAMA header in %s at 0x%x...\n",
126 block_offset
= offset
& ~(erasesize
- 1);
127 offset
-= block_offset
;
129 fd
= mtd_check_open(mtd
);
131 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
135 if (block_offset
+ erasesize
> mtdsize
) {
136 fprintf(stderr
, "Offset too large, device size 0x%x\n",
141 buf
= malloc(mtdsize
);
147 res
= pread(fd
, buf
, mtdsize
, block_offset
);
148 if (res
!= mtdsize
) {
153 if (seama_fix_md5(buf
, mtdsize
))
156 if (mtd_erase_block(fd
, block_offset
)) {
157 fprintf(stderr
, "Can't erease block at 0x%x (%s)\n",
158 block_offset
, strerror(errno
));
163 fprintf(stderr
, "Rewriting block at 0x%x\n", block_offset
);
165 if (pwrite(fd
, buf
, erasesize
, block_offset
) != erasesize
) {
166 fprintf(stderr
, "Error writing block (%s)\n", strerror(errno
));
171 fprintf(stderr
, "Done.\n");