diff options
author | cshore <cshore@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2010-12-26 04:18:01 +0000 |
---|---|---|
committer | cshore <cshore@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2010-12-26 04:18:01 +0000 |
commit | 40e684229764c2de974108aeca87715c6707e8c7 (patch) | |
tree | f9b73ee98969191e3b03ed8d924a9caa120eddfb /package/mtd/src | |
parent | cafdb5f9427a8aeea9de3efc1256a88f9cdeaa82 (diff) |
mtd: Added fixtrx for brcm63xx imagetag. This allows brcm63xx boards which experience a CRC on second boot due to the JFFS2 creation / DEADC0DE deletion to work correctly when mtd fixtrx is run on first boot
Signed-off-by: Daniel Dickinson <daniel@cshore.neomailbox.net>
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@24835 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/mtd/src')
-rw-r--r-- | package/mtd/src/Makefile | 1 | ||||
l--------- | package/mtd/src/bcm_tag.h | 1 | ||||
-rw-r--r-- | package/mtd/src/imagetag.c | 244 | ||||
-rw-r--r-- | package/mtd/src/mtd.c | 2 |
4 files changed, 247 insertions, 1 deletions
diff --git a/package/mtd/src/Makefile b/package/mtd/src/Makefile index bb509be8ba..fa2e486f92 100644 --- a/package/mtd/src/Makefile +++ b/package/mtd/src/Makefile @@ -4,6 +4,7 @@ CFLAGS += -Wall obj = mtd.o jffs2.o crc32.o obj.brcm = trx.o obj.brcm47xx = $(obj.brcm) +obj.brcm63xx = imagetag.o ifdef FIS_SUPPORT obj += fis.o diff --git a/package/mtd/src/bcm_tag.h b/package/mtd/src/bcm_tag.h new file mode 120000 index 0000000000..2e977a2d7a --- /dev/null +++ b/package/mtd/src/bcm_tag.h @@ -0,0 +1 @@ +../../../target/linux/brcm63xx/files/arch/mips/include/asm/mach-bcm63xx/bcm_tag.h
\ No newline at end of file diff --git a/package/mtd/src/imagetag.c b/package/mtd/src/imagetag.c new file mode 100644 index 0000000000..71d88ea1a4 --- /dev/null +++ b/package/mtd/src/imagetag.c @@ -0,0 +1,244 @@ +/* + * imagetag.c + * + * Copyright (C) 2005 Mike Baker + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org> + * Copyrigth (C) 2010 Daniel Dickinson <openwrt@cshore.neomailbox.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <string.h> +#include <errno.h> + +#include <sys/ioctl.h> +#include "mtd-api.h" +#include "mtd.h" +#include "crc32.h" +#include "bcm_tag.h" + +ssize_t pread(int fd, void *buf, size_t count, off_t offset); +ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset); + +#define CRC_START 0xFFFFFFFF + +static uint32_t strntoul(char *str, char **endptr, int base, size_t len) { + char *newstr; + uint32_t res = 0; + + newstr = calloc(len + 1, sizeof(char)); + if (newstr) { + strncpy(newstr, str, len); + res = strtoul(newstr, endptr, base); + free(newstr); + } + return res; +} + +uint32_t compute_crc32(uint32_t crc, off_t start, size_t compute_len, int fd) +{ + uint8_t readbuf[1024]; + ssize_t res; + off_t offset = start; + + /* Read a buffer's worth of bytes */ + while (fd && (compute_len >= sizeof(readbuf))) { + res = pread(fd, readbuf, sizeof(readbuf), offset); + crc = crc32(crc, readbuf, res); + compute_len = compute_len - res; + offset += res; + } + + /* Less than buffer-size bytes remains, read compute_len bytes */ + if (fd && (compute_len > 0)) { + res = pread(fd, readbuf, compute_len, offset); + crc = crc32(crc, readbuf, res); + } + + return crc; +} + +int +trx_check(int imagefd, const char *mtd, char *buf, int *len) +{ + struct bcm_tag *tag = (const struct bcm_tag *) buf; + int fd; + uint32_t headerCRC; + uint32_t imageLen; + + if (strcmp(mtd, "linux") != 0) + return 1; + + *len = read(imagefd, buf, sizeof(struct bcm_tag)); + if (*len < sizeof(struct bcm_tag)) { + fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len); + return 0; + } + headerCRC = crc32buf(buf, offsetof(struct bcm_tag, headerCRC)); + if (*(uint32_t *)(&tag->headerCRC[0]) != headerCRC) { + + if (quiet < 2) { + fprintf(stderr, "Bad header CRC got %08lx, calculated %08lx\n", + *(uint32_t *)(&tag->headerCRC[0]), headerCRC); + fprintf(stderr, "This is not the correct file format; refusing to flash.\n" + "Please specify the correct file or use -f to force.\n"); + } + return 0; + } + + /* check if image fits to mtd device */ + fd = mtd_check_open(mtd); + if(fd < 0) { + fprintf(stderr, "Could not open mtd device: %s\n", mtd); + exit(1); + } + + imageLen = strntoul(&tag->totalLength[0], NULL, 10, IMAGE_LEN); + + if(mtdsize < imageLen) { + fprintf(stderr, "Image too big for partition: %s\n", mtd); + close(fd); + return 0; + } + + close(fd); + return 1; +} + +int +mtd_fixtrx(const char *mtd, size_t offset) +{ + int fd; + struct bcm_tag *tag; + char *buf; + ssize_t res; + size_t block_offset; + uint32_t cfelen, imagelen, imagestart, rootfslen; + uint32_t imagecrc, rootfscrc, headercrc; + cfelen = imagelen = imagestart = imagecrc = rootfscrc = headercrc = rootfslen = 0; + + if (quiet < 2) + fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset); + + block_offset = offset & ~(erasesize - 1); + offset -= block_offset; + + fd = mtd_check_open(mtd); + if(fd < 0) { + fprintf(stderr, "Could not open mtd device: %s\n", mtd); + exit(1); + } + + if (block_offset + erasesize > mtdsize) { + fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize); + exit(1); + } + + buf = malloc(erasesize); + if (!buf) { + perror("malloc"); + exit(1); + } + + res = pread(fd, buf, erasesize, block_offset); + if (res != erasesize) { + perror("pread"); + exit(1); + } + + tag = (struct bcm_tag *) (buf + offset); + + cfelen = strntoul(&tag->cfeLength[0], NULL, 10, IMAGE_LEN); + if (cfelen) { + fprintf(stderr, "Non-zero CFE length. This is currently unsupported.\n"); + exit(1); + } + + if (quiet < 2) { + fprintf(stderr, "Verifying we actually have an imagetag.\n"); + } + + headercrc = compute_crc32(CRC_START, offset, offsetof(struct bcm_tag, headerCRC), fd); + if (headercrc != *(uint32_t *)(&tag->headerCRC[0])) { + fprintf(stderr, "Tag verify failed. This may not be a valid image.\n"); + exit(1); + } + + if (quiet < 2) { + fprintf(stderr, "Checking current fixed status.\n"); + } + + rootfslen = strntoul(&tag->rootLength[0], NULL, 10, IMAGE_LEN); + if (rootfslen == 0) { + if (quiet < 2) + fprintf(stderr, "Header already fixed, exiting\n"); + close(fd); + return 0; + } + + if (quiet < 2) { + fprintf(stderr, "Setting root length to 0.\n"); + } + + sprintf(&tag->rootLength[0], "%lu", 0); + strncpy(&tag->totalLength[0], &tag->kernelLength[0], IMAGE_LEN); + + if (quiet < 2) { + fprintf(stderr, "Recalculating CRCs.\n"); + } + + imagestart = sizeof(tag); + memcpy(&tag->imageCRC[0], &tag->kernelCRC[0], CRC_LEN); + memcpy(&tag->fskernelCRC[0], &tag->kernelCRC[0], CRC_LEN); + rootfscrc = CRC_START; + memcpy(&tag->rootfsCRC[0], &rootfscrc, sizeof(uint32_t)); + headercrc = crc32(CRC_START, tag, offsetof(struct bcm_tag, headerCRC)); + memcpy(&tag->headerCRC[0], &headercrc, sizeof(uint32_t)); + + if (quiet < 2) { + fprintf(stderr, "Erasing imagetag block\n"); + } + + if (mtd_erase_block(fd, block_offset)) { + fprintf(stderr, "Can't erase block at 0x%x (%s)\n", block_offset, strerror(errno)); + exit(1); + } + + if (quiet < 2) { + fprintf(stderr, "New image crc32: 0x%x, rewriting block\n", + *(uint32_t *)(&tag->imageCRC[0])); + fprintf(stderr, "New header crc32: 0x%x, rewriting block\n", headercrc); + } + + if (pwrite(fd, buf, erasesize, block_offset) != erasesize) { + fprintf(stderr, "Error writing block (%s)\n", strerror(errno)); + exit(1); + } + + if (quiet < 2) + fprintf(stderr, "Done.\n"); + + close (fd); + sync(); + return 0; + +} diff --git a/package/mtd/src/mtd.c b/package/mtd/src/mtd.c index d14b196bc0..8b87bada3f 100644 --- a/package/mtd/src/mtd.c +++ b/package/mtd/src/mtd.c @@ -598,7 +598,7 @@ int main (int argc, char **argv) break; case 'o': if (!mtd_fixtrx) { - fprintf(stderr, "-o: Only for brcm47xx\n"); + fprintf(stderr, "-o: Only for brcm47xx/brcm63xx\n"); usage(); } errno = 0; |