X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/68ef0484948e6f84615c517a773846091827f07f..ea5272bc7b201fb955ca3da2ba9652b57d66de4a:/tools/firmware-utils/src/airlink.c diff --git a/tools/firmware-utils/src/airlink.c b/tools/firmware-utils/src/airlink.c index 748e84bcb..560a58df5 100644 --- a/tools/firmware-utils/src/airlink.c +++ b/tools/firmware-utils/src/airlink.c @@ -17,16 +17,17 @@ AIRLINK AR525W firmware image structure *****************************************************************************/ #include #include +#include #include #include #include #include #include +#include -typedef unsigned long u32; typedef unsigned char uchar; -u32 crctab[257] = { +uint32_t crctab[257] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, @@ -94,7 +95,7 @@ u32 crctab[257] = { 0 }; -u32 header[] = { +uint32_t header[] = { 0x00000000, 0x4e525241, 0x4b544d47, 0x00000000, 0x00000000, 0x000afd4a, 0x00000000, 0x00000005, 0x00000000, 0x00000000 @@ -105,12 +106,11 @@ static int JFFS2 = 0; int generate_image(char *kname, char *fsname, char *fname, int EHDR) { int i; - u32 lenk, lens; + uint32_t lenk, lens; uchar *bk, *bs; int fkd, ffd, fsd; - int pad = 0x1f; fkd = open(kname, O_RDONLY); - ffd = creat(fname, 0x644); + ffd = creat(fname, 0644); if ((fkd < 0) || (ffd < 0)) return -1; if (fsname) { @@ -135,12 +135,12 @@ int generate_image(char *kname, char *fsname, char *fname, int EHDR) else write(ffd, header + 2, 0x20); write(ffd, bk, lenk); - if (fsname && JFFS2) - pad = 0xffff; + lenk += 0x20; + if (!JFFS2) JFFS2 = 0x20; printf("Padding header+kernel - 0x%x + 0x%x = 0x%x\n", - lenk + 0x20, ((lenk + 0x20) | pad) + 1 - lenk - 0x20, - ((lenk + 0x20) | pad) + 1); - for (i = 0; i < ((lenk + 0x20) | pad) + 1 - lenk - 0x20; i++) + lenk, ((lenk - 1 + JFFS2) / JFFS2) * JFFS2 - lenk, + ((lenk - 1 + JFFS2) / JFFS2) * JFFS2); + for (i = 0; i < ((lenk - 1 + JFFS2) / JFFS2) * JFFS2 - lenk; i++) write(ffd, header, 1); if (fsname) { write(ffd, bs, lens); @@ -151,11 +151,11 @@ int generate_image(char *kname, char *fsname, char *fname, int EHDR) return 0; } -u32 crc32(uchar * buf, u32 len) +uint32_t crc32(uchar * buf, uint32_t len) { register int i; - u32 sum; - register u32 s0; + uint32_t sum; + register uint32_t s0; s0 = ~0; for (i = 0; i < len; i++) { s0 = (s0 >> 8) ^ crctab[(uchar) (s0 & 0xFF) ^ buf[i]]; @@ -169,13 +169,13 @@ void usage(char *prog) printf("Usage: %s [-b 0/1] image_filename \n", prog); printf(" update checksums for firmware file\n"); printf - ("Usage: %s [-b 0/1] [-j] [-e] kernel filesystem image_filename \n", + ("Usage: %s [-b 0/1] [-j erasesize_in_kibytes] [-e] kernel filesystem image_filename \n", prog); printf(" generate firmware file and update checksums\n"); printf("--------------------------------------------------\n"); printf(" -e - generate header for web upload\n"); - printf(" -j - generate header for jffs2 filesystem\n"); printf(" -b 0/1 - clear/update bootloader checksum\n"); + printf(" -j erasesize_in_kibytes - generate header for jffs2 filesystem\n"); } int main(int argc, char **argv) @@ -187,7 +187,7 @@ int main(int argc, char **argv) extern char *optarg; extern int optind, optopt; - while ((c = getopt(argc, argv, "b:e:j")) != -1) { + while ((c = getopt(argc, argv, "b:ej:")) != -1) { switch (c) { case 'b': if (optarg[0] == '1') @@ -197,7 +197,8 @@ int main(int argc, char **argv) EHDR = 1; break; case 'j': - JFFS2 = 1; + sscanf(optarg, "%i", &JFFS2); + JFFS2 *= 1024; break; case '?': fprintf(stderr, "\nError: unknown arg %c\n\n\n", @@ -241,30 +242,37 @@ int main(int argc, char **argv) lseek(fd, 0, SEEK_SET); uchar *buf = malloc(len); read(fd, buf, len); - u32 sum, l0; - u32 MagicS = 0x4b544d47; - u32 MagicE = 0x4e525241; - if (*((u32 *) buf) == MagicS) { + uint32_t sum, l0; + uint32_t MagicS = 0x474d544b; + uint32_t MagicE = 0x4152524e; + if (ntohl(*((uint32_t *) buf)) == MagicS) { fprintf(stderr, "Image without extra 8 bytes - Standard header\n"); - *((u32 *) (buf + 0x10)) = len; + buf[0x10] = len & 0xff; + buf[0x11] = (len >> 8) & 0xff; + buf[0x12] = (len >> 16) & 0xff; + buf[0x13] = (len >> 24) & 0xff; lseek(fd, 0x10, SEEK_SET); write(fd, buf + 0x10, 0x4); EHDR = 0; - } else if ((*((u32 *) (buf + 0x8)) == MagicS) - && ((*((u32 *) (buf + 0x4)) == MagicE))) { + } else if ((ntohl(*((uint32_t *) (buf + 0x8))) == MagicS) + && ((ntohl(*((uint32_t *) (buf + 0x4))) == MagicE))) { fprintf(stderr, "Image with extra 8 bytes - Extended header\n"); - *((u32 *) (buf + 0x18)) = len - 8; + *((uint32_t *) (buf + 0x18)) = len - 8; + buf[0x18] = (len - 8) & 0xff; + buf[0x19] = ((len - 8) >> 8) & 0xff; + buf[0x1a] = ((len - 8) >> 16) & 0xff; + buf[0x1b] = ((len - 8) >> 24) & 0xff; lseek(fd, 0x18, SEEK_SET); write(fd, buf + 0x18, 0x4); buf += 8; EHDR = 1; - } else if (len == *((u32 *) (buf + 0x10))) { + } else if (len == buf[0x10] | ((uint32_t)buf[0x11] << 8) | ((uint32_t)buf[0x12] << 16) | ((uint32_t)buf[0x13] << 24)) { fprintf(stderr, "Image without extra 8 bytes - Standard header\n"); EHDR = 0; - } else if (len == *((u32 *) (buf + 0x18)) + 8) { + } else if (len == (buf[0x18] | ((uint32_t)buf[0x19] << 8) | ((uint32_t)buf[0x1a] << 16) | ((uint32_t)buf[0x1b] << 24)) + 8) { fprintf(stderr, "Image with extra 8 bytes - Extended header\n"); buf += 8; @@ -273,19 +281,16 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: Wrong image size\n"); exit(-1); } - l0 = *((u32 *) & buf[0x10]); + l0 = buf[0x10] | ((uint32_t)buf[0x11] << 8) | ((uint32_t)buf[0x12] << 16) | ((uint32_t)buf[0x13] << 24); if (!BHDR) - *((u32 *) & buf[0x18]) = 0; - ulong sum0 = *((u32 *) & buf[0x18]); - ulong sum1 = *((u32 *) & buf[0x4]); - *((u32 *) & buf[0x4]) = 0x0L; - for (i = 0; i < 0x100; i++) - b[i] = buf[i]; - for (i = 0; i < 0x100; i++) - b[0x100 + i] = buf[(l0 >> 1) + i]; - for (i = 0; i < 0x200; i++) - b[0x200 + i] = buf[l0 - 0x200 + i]; - *((u32 *) & b[0x18]) = 0x0L; + *((uint32_t *) & buf[0x18]) = 0; + unsigned long sum0 = buf[0x18] | ((uint32_t)buf[0x19] << 8) | ((uint32_t)buf[0x1a] << 16) | ((uint32_t)buf[0x1b] << 24); + unsigned long sum1 = buf[0x4] | ((uint32_t)buf[0x5] << 8) | ((uint32_t)buf[0x6] << 16) | ((uint32_t)buf[0x7] << 24); + *((uint32_t *) & buf[0x4]) = 0x0L; + memcpy(b, buf, 0x100); + memcpy(b + 0x100, buf + ((l0 >> 1) - ((l0 & 0x6) >> 1)), 0x100); + memcpy(b + 0x200, buf + (l0 - 0x200), 0x200); + *((uint32_t *) & b[0x18]) = 0x0L; sum = crc32(b, 0x400); printf("CRC32 sum0 - (%x, %x, %x)\n", sum, sum0, 0x400); @@ -293,10 +298,10 @@ int main(int argc, char **argv) lseek(fd, 0x20, SEEK_SET); else lseek(fd, 0x18, SEEK_SET); - if (BHDR) - *((u32 *) & buf[0x18]) = sum; - else - *((u32 *) & buf[0x18]) = sum0; + buf[0x18] = (BHDR ? sum : sum0) & 0xff; + buf[0x19] = ((BHDR ? sum : sum0) >> 8) & 0xff; + buf[0x1a] = ((BHDR ? sum : sum0) >> 16) & 0xff; + buf[0x1b] = ((BHDR ? sum : sum0) >> 24) & 0xff; write(fd, &buf[0x18], 0x4); sum = crc32(buf, l0); @@ -305,17 +310,19 @@ int main(int argc, char **argv) lseek(fd, 0xC, SEEK_SET); else lseek(fd, 0x4, SEEK_SET); - *((u32 *) & buf[0x4]) = sum; + buf[0x4] = sum & 0xff; + buf[0x5] = (sum >> 8) & 0xff; + buf[0x6] = (sum >> 16) & 0xff; + buf[0x7] = (sum >> 24) & 0xff; write(fd, &buf[0x4], 0x4); if (EHDR) { - ulong sum2 = *((u32 *) & buf[-0x8]); - *((u32 *) & buf[-0x8]) = 0L; + unsigned long sum2 = buf[-0x8] | ((uint32_t)buf[-0x7] << 8) | ((uint32_t)buf[-0x6] << 16) | ((uint32_t)buf[-0x5] << 24); + *((uint32_t *) & buf[-0x8]) = 0L; sum = crc32(buf - 0x4, len - 0x4); - sum = htonl(sum); printf("CRC32 sum2 - (%x, %x, %x)\n", sum, sum2, len - 0x4); lseek(fd, 0, SEEK_SET); - *((u32 *) & buf[-0x8]) = sum; + *((uint32_t *) & buf[-0x8]) = htonl(sum); write(fd, &buf[-0x8], 0x4); buf -= 8; }