diff --git a/brcm_fw_tool/Makefile b/brcm_fw_tool/Makefile index d11f2f004af2f804412de6b22b4e72313fed0473..c4533b7489f3746cde8b959866397aae930389d9 100644 --- a/brcm_fw_tool/Makefile +++ b/brcm_fw_tool/Makefile @@ -9,9 +9,9 @@ CFLAGS += -I$(STAGING_DIR)/usr/include/bcm963xx/bcmdrivers/opensource/include/bc %.o: %.c $(CC) -c $(CFLAGS) -o $@ $< -obj = brcm_fw_tool.o jffs2.o crc32.o +obj = brcm_fw_tool.o jffs2.o crc32.o image-jffs2.o obj.brcm47xx = $(obj.brcm) brcm_fw_tool: $(obj) $(obj.$(TARGET)) clean: - rm -f *.o jffs2 + rm -f *.o jffs2 brcm_fw_tool diff --git a/brcm_fw_tool/brcm_fw_tool.c b/brcm_fw_tool/brcm_fw_tool.c index d64ee67852173505805e4dffdbf23456fad8e999..c6f9a28c81725b556647634c8ce4423dc0886ac0 100644 --- a/brcm_fw_tool/brcm_fw_tool.c +++ b/brcm_fw_tool/brcm_fw_tool.c @@ -44,6 +44,7 @@ #include "bcmTag.h" #include "bcmnet.h" #include <bcm_sdk_version.h> +#include "image-jffs2.h" #define MAP_SIZE (4096) #define MAP_MASK 0xFFF @@ -68,11 +69,6 @@ void *mapped_base; #endif -int verbose = 0; -long start_offset = 0; -long fsimg_size = 0; -#define BLOCKSIZE 131072 -#define VERSION_NAME "cferam.000" #define READ_BUF 1024 #define WFI_VERSION 0x00005732 @@ -371,121 +367,7 @@ ret_pos: static int generate_image(const char *in_file, char *sequence_number) { - int in_fp = 0; - int rb; - uint8_t readbuf[BLOCKSIZE]; - int read_bytes = 0; - struct jffs2_raw_dirent *pdir = NULL; - unsigned long version = 0; - uint8_t *p; - int possibly_jffs2 = 0; - int sequence; - char fixed_sequence[16]; - int ver_name_len = strlen(VERSION_NAME); - - if ((in_fp = open(in_file, O_RDWR)) < 0){ - fprintf(stderr, "failed to open: %s\n", in_file); - exit(1); - } - - if (verbose) - fprintf(stderr, "|%s|\n", sequence_number); - - if (!sequence_number) { - fprintf(stderr, "No sequence number\n"); - exit(1); - } - - sequence = atoi(sequence_number); - sprintf(fixed_sequence, "%03d", sequence+1); - - if (verbose) - fprintf(stderr, "New sequence: %s\n",fixed_sequence); - - if (start_offset) { - lseek(in_fp, start_offset, SEEK_SET); - if (verbose) - fprintf(stderr, "Skipping %lu bytes\n", start_offset); - } - - if (fsimg_size && verbose) - fprintf(stderr, "Image size limited to %lu bytes\n", fsimg_size); - - for (;;) { - rb = read(in_fp, &readbuf, sizeof(readbuf)); - if (rb <= 0) - break; - - if (fsimg_size && (read_bytes + rb) > fsimg_size) - rb = fsimg_size - read_bytes; - read_bytes += rb; - - p = (uint8_t*)&readbuf; - - while (p < (&readbuf[rb-1])) { - pdir = (struct jffs2_raw_dirent *)p; - - if( pdir->magic == JFFS2_MAGIC_BITMASK ) { - /* Ignore current sequence number (-3) */ - if( pdir->nodetype == JFFS2_NODETYPE_DIRENT && - ver_name_len == pdir->nsize && - !memcmp(VERSION_NAME, pdir->name, ver_name_len-3) ) { - if( pdir->version > version ) { - if( pdir->ino != 0 ) { - if (verbose) { - printf("Found cferam structure at offset %x\n", (unsigned int)p); - printf("name : %s\n",pdir->name); - printf("name_crc: %x\n",pdir->name_crc); - printf("nsize : %d\n",pdir->nsize); - printf("totlen : %d\n",pdir->totlen); - } - - goto end; - } - } - } - p += (pdir->totlen + 3) & ~0x03; - } else { - /* Skip the rest of this block */ - p = &readbuf[BLOCKSIZE]; - if (possibly_jffs2++ > 100) - goto error; - } - } - - if (fsimg_size && read_bytes >= fsimg_size) - break; - } - -error: - printf("JFFS2 image corrupt or not JFFS2 image\n"); - return 1; - -end: - if(sequence == -1) { - memcpy(fixed_sequence, pdir->name + (ver_name_len-3), 3); - fixed_sequence[3] = '\0'; - printf("%s\n", fixed_sequence); - goto out; - } - - memcpy(pdir->name+7, fixed_sequence, 3); - pdir->name_crc = crc32(0, pdir->name, strlen((char*)pdir->name)); - if (verbose) { - printf("new name : %s sn: %s\n",pdir->name, fixed_sequence); - printf("new name_crc: %x\n",pdir->name_crc); - } - - if (lseek(in_fp, -BLOCKSIZE, SEEK_CUR) < 0) { - fprintf(stderr, "seeking of file %s failed\n", in_file); - exit(1); - } - - write(in_fp, &readbuf, BLOCKSIZE); -out: - close(in_fp); - - return 0; + return gen_image_jffs2(in_file, sequence_number); } static int diff --git a/brcm_fw_tool/brcm_fw_tool.h b/brcm_fw_tool/brcm_fw_tool.h index ad98016af30b193988a44c173f09aa5ce7f560f6..09c070de0b17f9d697cfc419b39583185c89ccef 100644 --- a/brcm_fw_tool/brcm_fw_tool.h +++ b/brcm_fw_tool/brcm_fw_tool.h @@ -3,6 +3,10 @@ #define JFFS2_EOF "\xde\xad\xc0\xde" +int verbose; +long start_offset; +long fsimg_size; + extern int quiet; extern int mtd_replace_jffs2(const char *mtd, int fd, int ofs, const char *filename, int nand_erasesize); diff --git a/brcm_fw_tool/image-jffs2.c b/brcm_fw_tool/image-jffs2.c new file mode 100644 index 0000000000000000000000000000000000000000..ace0b9a84580ccc473680684d7f52620c66cda98 --- /dev/null +++ b/brcm_fw_tool/image-jffs2.c @@ -0,0 +1,145 @@ + +#include <limits.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <time.h> +#include <string.h> +#include <err.h> +#include "brcm_fw_tool.h" +#include "crc32.h" +#include "jffs2.h" +#include <board.h> +#include "bcmTag.h" +#include <bcm_sdk_version.h> +#include "image-jffs2.h" +#include "brcm_fw_tool.h" + + +#define VERSION_NAME "cferam.000" +#define BLOCKSIZE 131072 + + +int gen_image_jffs2(const char *in_file, char *sequence_number) +{ + int in_fp = 0; + int rb; + uint8_t readbuf[BLOCKSIZE]; + int read_bytes = 0; + struct jffs2_raw_dirent *pdir = NULL; + unsigned long version = 0; + uint8_t *p; + int possibly_jffs2 = 0; + int sequence; + char fixed_sequence[16]; + int ver_name_len = strlen(VERSION_NAME); + + if ((in_fp = open(in_file, O_RDWR)) < 0){ + fprintf(stderr, "failed to open: %s\n", in_file); + exit(1); + } + + if (verbose) + fprintf(stderr, "|%s|\n", sequence_number); + + if (!sequence_number) { + fprintf(stderr, "No sequence number\n"); + exit(1); + } + + sequence = atoi(sequence_number); + sprintf(fixed_sequence, "%03d", sequence+1); + + if (verbose) + fprintf(stderr, "New sequence: %s\n",fixed_sequence); + + if (start_offset) { + lseek(in_fp, start_offset, SEEK_SET); + if (verbose) + fprintf(stderr, "Skipping %lu bytes\n", start_offset); + } + + if (fsimg_size && verbose) + fprintf(stderr, "Image size limited to %lu bytes\n", fsimg_size); + + for (;;) { + rb = read(in_fp, &readbuf, sizeof(readbuf)); + if (rb <= 0) + break; + + if (fsimg_size && (read_bytes + rb) > fsimg_size) + rb = fsimg_size - read_bytes; + read_bytes += rb; + + p = (uint8_t*)&readbuf; + + while (p < (&readbuf[rb-1])) { + pdir = (struct jffs2_raw_dirent *)p; + + if( pdir->magic == JFFS2_MAGIC_BITMASK ) { + /* Ignore current sequence number (-3) */ + if( pdir->nodetype == JFFS2_NODETYPE_DIRENT && + ver_name_len == pdir->nsize && + !memcmp(VERSION_NAME, pdir->name, ver_name_len-3) ) { + if( pdir->version > version ) { + if( pdir->ino != 0 ) { + if (verbose) { + printf("Found cferam structure at offset %x\n", (unsigned int)p); + printf("name : %s\n",pdir->name); + printf("name_crc: %x\n",pdir->name_crc); + printf("nsize : %d\n",pdir->nsize); + printf("totlen : %d\n",pdir->totlen); + } + + goto end; + } + } + } + p += (pdir->totlen + 3) & ~0x03; + } else { + /* Skip the rest of this block */ + p = &readbuf[BLOCKSIZE]; + if (possibly_jffs2++ > 100) + goto error; + } + } + + if (fsimg_size && read_bytes >= fsimg_size) + break; + } + +error: + printf("JFFS2 image corrupt or not JFFS2 image\n"); + return 1; + +end: + if(sequence == -1) { + memcpy(fixed_sequence, pdir->name + (ver_name_len-3), 3); + fixed_sequence[3] = '\0'; + printf("%s\n", fixed_sequence); + goto out; + } + + memcpy(pdir->name+7, fixed_sequence, 3); + pdir->name_crc = crc32(0, pdir->name, strlen((char*)pdir->name)); + if (verbose) { + printf("new name : %s sn: %s\n",pdir->name, fixed_sequence); + printf("new name_crc: %x\n",pdir->name_crc); + } + + if (lseek(in_fp, -BLOCKSIZE, SEEK_CUR) < 0) { + fprintf(stderr, "seeking of file %s failed\n", in_file); + exit(1); + } + + write(in_fp, &readbuf, BLOCKSIZE); +out: + close(in_fp); + + return 0; +} + diff --git a/brcm_fw_tool/image-jffs2.h b/brcm_fw_tool/image-jffs2.h new file mode 100644 index 0000000000000000000000000000000000000000..e418fcb4d23b3a67e25bd79648b796f2efb62c88 --- /dev/null +++ b/brcm_fw_tool/image-jffs2.h @@ -0,0 +1,8 @@ +#ifndef IMAGE_JFFS2_H +#define IMAGE_JFFS2_H + + + +int gen_image_jffs2(const char *in_file, char *sequence_number); + +#endif