From 7640f8904590b4810ef7a104fd69fc5b35caee4e Mon Sep 17 00:00:00 2001 From: Hauke Mehrtens Date: Fri, 6 Aug 2010 20:21:32 +0000 Subject: [PATCH] brcm47xx: Add new image generation and fix leds/reset button on WNR834B V2 Thank you realopty for the patch. tools/firmware-utils/src/mkchkimg.c is from http://www.myopenrouter.com/download/10611/mkchkimg/ This closes #7702. SVN-Revision: 22516 --- package/broadcom-diag/src/diag.c | 18 ++ target/linux/brcm47xx/image/Makefile | 6 + tools/firmware-utils/Makefile | 1 + tools/firmware-utils/src/mkchkimg.c | 327 +++++++++++++++++++++++++++ 4 files changed, 352 insertions(+) create mode 100644 tools/firmware-utils/src/mkchkimg.c diff --git a/package/broadcom-diag/src/diag.c b/package/broadcom-diag/src/diag.c index 6a95a8afb3..620f29171c 100644 --- a/package/broadcom-diag/src/diag.c +++ b/package/broadcom-diag/src/diag.c @@ -110,6 +110,7 @@ enum { /* Netgear */ WGT634U, + WNR834BV2, /* Trendware */ TEW411BRPP, @@ -754,6 +755,17 @@ static struct platform_t __initdata platforms[] = { { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, }, }, + [WNR834BV2] = { + .name = "Netgear WNR834B V2", + .buttons = { + { .name = "reset", .gpio = 1 << 6 }, + }, + .leds = { + { .name = "power", .gpio = 1 << 2, .polarity = NORMAL }, + { .name = "diag", .gpio = 1 << 3, .polarity = NORMAL }, + { .name = "connected", .gpio = 1 << 7, .polarity = NORMAL }, + }, + }, /* Trendware */ [TEW411BRPP] = { .name = "Trendware TEW411BRP+", @@ -1035,6 +1047,12 @@ static struct platform_t __init *platform_detect(void) return &platforms[WDNetCenter]; } + if (!strcmp(boardnum, "08") || !strcmp(boardnum, "01") && + !strcmp(boardtype,"0x0472") && !strcmp(getvar("cardbus"), "1")) { /* Netgear WNR834B V1 and V2*/ + /* TODO: Check for version. Default platform is V2 for now. */ + return &platforms[WNR834BV2]; + } + } else { /* PMON based - old stuff */ if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) && (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) { diff --git a/target/linux/brcm47xx/image/Makefile b/target/linux/brcm47xx/image/Makefile index 69a31e7554..daee392da9 100644 --- a/target/linux/brcm47xx/image/Makefile +++ b/target/linux/brcm47xx/image/Makefile @@ -98,6 +98,10 @@ define Image/Build/Initramfs $(STAGING_DIR_HOST)/bin/trx -o $(BIN_DIR)/$(IMG_PREFIX)-initramfs.trx -f $(KDIR)/loader.gz -f $(KDIR)/vmlinux.lzma endef +define Image/Build/Chk + $(STAGING_DIR_HOST)/bin/mkchkimg -o $(BIN_DIR)/openwrt-$(2)-$(5).chk -k $(BIN_DIR)/$(IMG_PREFIX)-$(1).trx -b $(3) -r $(4) +endef + define Image/Build $(STAGING_DIR_HOST)/bin/trx -o $(BIN_DIR)/$(IMG_PREFIX)-$(1).trx \ -f $(KDIR)/loader.gz -f $(KDIR)/vmlinux.lzma \ @@ -105,6 +109,8 @@ define Image/Build $(call Image/Build/$(1),$(1)) $(call Image/Build/Motorola,$(1),wr850g,1,$(1)) $(call Image/Build/USR,$(1),usr5461,$(1)) + $(call Image/Build/Chk,$(1),wnr834b_v2,U12H081T00_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1))) + $(call Image/Build/Chk,$(1),wgr614_v8,U12H072T00_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1))) endef $(eval $(call BuildImage)) diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index d4a633c509..37bbc0301f 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -46,6 +46,7 @@ define Host/Compile $(call cc,wndr3700) $(call cc,mkdniimg) $(call cc,mktitanimg) + $(call cc,mkchkimg) endef define Host/Install diff --git a/tools/firmware-utils/src/mkchkimg.c b/tools/firmware-utils/src/mkchkimg.c new file mode 100644 index 0000000000..e152f7d468 --- /dev/null +++ b/tools/firmware-utils/src/mkchkimg.c @@ -0,0 +1,327 @@ +/* + * Make CHK Image + * + * This utility creates Netgear .chk files. + * + * Copyright (C) 2008 Dave C. Reeve + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include +#include +#include +#include +#include +#include +#include + +#define BUF_LEN (2048) + +#define MAX_BOARD_ID_LEN (64) + +struct chk_header { + uint32_t magic; + uint32_t header_len; + uint8_t reserved[8]; + uint32_t kernel_chksum; + uint32_t rootfs_chksum; + uint32_t kernel_len; + uint32_t rootfs_len; + uint32_t image_chksum; + uint32_t header_chksum; + /* char board_id[] - upto MAX_BOARD_ID_LEN */ +}; + +static void __attribute__ ((format (printf, 2, 3))) +fatal_error (int maybe_errno, const char * format, ...) +{ + va_list ap; + + fprintf (stderr, "mkchkimg: "); + va_start (ap, format); + vfprintf (stderr, format, ap); + va_end (ap); + + if (maybe_errno) { + fprintf (stderr, ": %s\n", strerror (maybe_errno)); + } else { + fprintf (stderr, "\n"); + } + + exit (EXIT_FAILURE); +} + +static void __attribute__ ((format (printf, 1, 2))) +message (const char * format, ...) +{ + va_list ap; + + fprintf (stderr, "mkchkimg: "); + va_start (ap, format); + vfprintf (stderr, format, ap); + va_end (ap); + fprintf (stderr, "\n"); +} + +struct ngr_checksum { + uint32_t c0; + uint32_t c1; +}; + +static inline void +netgear_checksum_init (struct ngr_checksum * c) +{ + c->c0 = c->c1 = 0; +} + +static inline void +netgear_checksum_add (struct ngr_checksum * c, unsigned char * buf, size_t len) +{ + size_t i; + + for (i=0; ic0 += buf[i] & 0xff; + c->c1 += c->c0; + } +} + +static inline unsigned long +netgear_checksum_fini (struct ngr_checksum * c) +{ + uint32_t b, checksum; + + b = (c->c0 & 65535) + ((c->c0 >> 16) & 65535); + c->c0 = ((b >> 16) + b) & 65535; + b = (c->c1 & 65535) + ((c->c1 >> 16) & 65535); + c->c1 = ((b >> 16) + b) & 65535; + checksum = ((c->c1 << 16) | c->c0); + return checksum; +} + +static void +print_help (void) +{ + fprintf (stderr, "Usage: mkchkimg -o output -k kernel [-f filesys] [-b board_id] [-r region]\n"); +} + +int +main (int argc, char * argv[]) +{ + int opt; + char * ptr; + size_t len; + size_t header_len; + struct chk_header * hdr; + struct ngr_checksum chk_part, chk_whole; + char buf[BUF_LEN]; + char * output_file, * kern_file, * fs_file; + FILE * out_fp, * kern_fp, * fs_fp; + char * board_id; + unsigned long region; + + /* Default values */ + board_id = "U12H072T00_NETGEAR"; + region = 1; /* 1=WW, 2=NA */ + output_file = NULL; + kern_file = NULL; + fs_file = NULL; + fs_fp = NULL; + + while ((opt = getopt (argc, argv, ":b:r:k:f:o:h")) != -1) { + switch (opt) { + case 'b': + /* Board Identity */ + if (strlen (optarg) > MAX_BOARD_ID_LEN) { + fatal_error (0, "Board lenght exceeds %d", + MAX_BOARD_ID_LEN); + } + board_id = optarg; + break; + + case 'r': + /* Region */ + errno = 0; + region = strtoul (optarg, &ptr, 0); + if (errno || ptr==optarg || *ptr!='\0') { + fatal_error (0, "Cannot parse region %s", optarg); + } + if (region > 0xff) { + fatal_error (0, "Region cannot exceed 0xff"); + } + break; + + case 'k': + /* Kernel */ + kern_file = optarg; + break; + + case 'f': + /* Filing System */ + fs_file = optarg; + break; + + case 'o': + /* Output file */ + output_file = optarg; + break; + + case 'h': + print_help (); + return EXIT_SUCCESS; + + case ':': + print_help (); + fatal_error (0, "Option -%c missing argument", optopt); + break; + + case '?': + print_help (); + fatal_error (0, "Unknown argument -%c", optopt); + break; + + default: + break; + } + } + + /* Check we have all the options expected */ + if (!kern_file) { + print_help (); + fatal_error (0, "Kernel file expected"); + } + if (!output_file) { + print_help (); + fatal_error (0, "Output file required"); + } + message ("Netgear CHK writer - v0.1"); + + /* Open the input file */ + kern_fp = fopen (kern_file, "r"); + if (!kern_fp) { + fatal_error (errno, "Cannot open %s", kern_file); + } + + /* Open the fs file, if specified */ + if (fs_file) { + fs_fp = fopen (fs_file, "r"); + if (!fs_fp) { + fatal_error (errno, "Cannot open %s", fs_file); + } + } + + /* Open the output file */ + out_fp = fopen (output_file, "w+"); + if (!out_fp) { + fatal_error (errno, "Cannot open %s", output_file); + } + + /* Write zeros when the chk header will be */ + buf[0] = '\0'; + header_len = sizeof (struct chk_header) + strlen (board_id); + if (fwrite (buf, 1, header_len, out_fp) != header_len) { + fatal_error (errno, "Cannot write header"); + } + + /* Allocate storage for header, we fill in as we go */ + hdr = malloc (sizeof (struct chk_header)); + if (!hdr) { + fatal_error (0, "malloc failed"); + } + bzero (hdr, sizeof (struct chk_header)); + + /* Fill in known values */ + hdr->magic = htonl (0x2a23245e); + hdr->header_len = htonl(header_len); + hdr->reserved[0] = (unsigned char)(region & 0xff); + hdr->reserved[1] = 1; /* Major */ + hdr->reserved[2] = 1; /* Minor */ + hdr->reserved[3] = 99; /* Build */ + hdr->reserved[4] = 0; /* Unknown t1 ? was 1 */ + hdr->reserved[5] = 0; /* Unknonw t2 ? was 0 */ + hdr->reserved[6] = 0; /* Unknonw t3 ? was 1 */ + hdr->reserved[7] = 0; /* Unused ? */ + message (" Board Id: %s", board_id); + message (" Region: %s", region == 1 ? "World Wide (WW)" + : (region == 2 ? "North America (NA)" : "Unknown")); + + /* Copy the trx file, calculating the checksum as we go */ + netgear_checksum_init (&chk_part); + netgear_checksum_init (&chk_whole); + while (!feof (kern_fp)) { + len = fread (buf, 1, BUF_LEN, kern_fp); + if (len < 1) { + break; + } + if (fwrite (buf, len, 1, out_fp) != 1) { + fatal_error (errno, "Write error"); + } + hdr->kernel_len += len; + netgear_checksum_add (&chk_part, (unsigned char *)buf, len); + netgear_checksum_add (&chk_whole, (unsigned char *)buf, len); + } + hdr->kernel_chksum = netgear_checksum_fini (&chk_part); + message (" Kernel Len: %u", hdr->kernel_len); + message ("Kernel Checksum: 0x%08x", hdr->kernel_chksum); + hdr->kernel_len = htonl (hdr->kernel_len); + hdr->kernel_chksum = htonl (hdr->kernel_chksum); + + /* Now copy the root fs, calculating the checksum as we go */ + if (fs_fp) { + netgear_checksum_init (&chk_part); + while (!feof (fs_fp)) { + len = fread (buf, 1, BUF_LEN, fs_fp); + if (len < 1) { + break; + } + if (fwrite (buf, len, 1, out_fp) != 1) { + fatal_error (errno, "Write error"); + } + hdr->rootfs_len += len; + netgear_checksum_add (&chk_part, (unsigned char *)buf, len); + netgear_checksum_add (&chk_whole, (unsigned char *)buf, len); + } + hdr->rootfs_chksum = (netgear_checksum_fini (&chk_part)); + message (" Rootfs Len: %u", hdr->rootfs_len); + message ("Rootfs Checksum: 0x%08x", hdr->rootfs_chksum); + hdr->rootfs_len = htonl (hdr->rootfs_len); + hdr->rootfs_chksum = htonl (hdr->rootfs_chksum); + } + + /* Calcautate the image checksum */ + hdr->image_chksum = netgear_checksum_fini (&chk_whole); + message (" Image Checksum: 0x%08x", hdr->image_chksum); + hdr->image_chksum = htonl (hdr->image_chksum); + + /* Calculate the header checksum */ + netgear_checksum_init (&chk_part); + netgear_checksum_add (&chk_part, (unsigned char *)hdr, + sizeof (struct chk_header)); + netgear_checksum_add (&chk_part, (unsigned char *)board_id, + strlen (board_id)); + hdr->header_chksum = htonl (netgear_checksum_fini (&chk_part)); + + /* Finally rewind the output and write headers */ + rewind (out_fp); + if (fwrite (hdr, sizeof (struct chk_header), 1, out_fp) != 1) { + fatal_error (errno, "Cannot write header"); + } + if (fwrite (board_id, strlen (board_id), 1, out_fp) != 1) { + fatal_error (errno, "Cannot write board id"); + } + + /* Success */ + return EXIT_SUCCESS; +} +