Add firmware generation for other Compex boards based on myloader, thanks to Juhos...
authorFlorian Fainelli <florian@openwrt.org>
Thu, 22 Mar 2007 21:36:15 +0000 (21:36 +0000)
committerFlorian Fainelli <florian@openwrt.org>
Thu, 22 Mar 2007 21:36:15 +0000 (21:36 +0000)
SVN-Revision: 6640

target/linux/adm5120-2.6/image/Makefile
tools/firmware-utils/Makefile
tools/firmware-utils/src/mkmylofw.c [new file with mode: 0644]
tools/firmware-utils/src/myloader.h [new file with mode: 0644]

index a41fde93306e4d64975006eadfabbb91ab30c967..29c400728a5b6d445e1da50e265ddfc192c26043 100644 (file)
@@ -43,11 +43,29 @@ define Image/Build/Edimax
        cat csys $(KDIR)/vmlinux $(KDIR)/root.$(1) > $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(3)-$(2).img
 endef
 
+define Image/Build/MyLoader
+       $(CP) $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(1)-noloader.trx $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(3)-$(2).trx
+       $(STAGING_DIR)/bin/mkmylofw -B $(4) \
+               -p0x20000:0x10000:ahp:0x80001000 \
+               -p0x30000:0 \
+               -b0x20000:0x10000:h:$(KDIR)/loader.gz \
+               -b0x30000:0::$(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(3)-$(2).trx \
+               $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(3)-$(2).bin
+endef
+
 define Image/Build
        $(STAGING_DIR)/bin/trx -o $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(1).trx -f $(KDIR)/loader.gz -f $(KDIR)/vmlinux.lzma $(call trxalign/$(1)) -f $(KDIR)/root.$(1)
+       $(STAGING_DIR)/bin/trx -o $(BIN_DIR)/openwrt-$(BOARD)-$(KERNEL)-$(1)-noloader.trx -f $(KDIR)/vmlinux.lzma $(call trxalign/$(1)) -f $(KDIR)/root.$(1)
 ifneq ($(1),jffs2-128K)
-       $(call Image/Build/Compex,$(1),wp54g,$(patsubst jffs2-%,jffs2,$(1)))
+       $(call Image/Build/Compex,$(1),wp54g-wrt,$(patsubst jffs2-%,jffs2,$(1)))
        $(call Image/Build/Edimax,$(1),br6104,$(patsubst jffs2-%,jffs2,$(1)))
+       $(call Image/Build/MyLoader,$(1),np27g,$(patsubst jffs2-%,jffs2,$(1)),NP27G)
+       $(call Image/Build/MyLoader,$(1),np28g,$(patsubst jffs2-%,jffs2,$(1)),NP28G)
+       $(call Image/Build/MyLoader,$(1),np28ghs,$(patsubst jffs2-%,jffs2,$(1)),NP28GHS)
+       $(call Image/Build/MyLoader,$(1),wp54g,$(patsubst jffs2-%,jffs2,$(1)),WP54G)
+       $(call Image/Build/MyLoader,$(1),wp54ag,$(patsubst jffs2-%,jffs2,$(1)),WP54AG)
+       $(call Image/Build/MyLoader,$(1),wpp54g,$(patsubst jffs2-%,jffs2,$(1)),WPP54G)
+       $(call Image/Build/MyLoader,$(1),wpp54ag,$(patsubst jffs2-%,jffs2,$(1)),WPP54AG)
 endif
 endef
 
index 88e3a4964c4f988bd7ed6db087bcc96667609f20..c9d9bae1e1ffc6dfcc6552557dff11bcf634e415 100644 (file)
@@ -27,6 +27,7 @@ define Build/Compile
        $(call cc,airlink)
        $(call cc,srec2bin)
        $(call cc,mksyshdr)
+       $(call cc,mkmylofw)
 endef
 
 define Build/Install
diff --git a/tools/firmware-utils/src/mkmylofw.c b/tools/firmware-utils/src/mkmylofw.c
new file mode 100644 (file)
index 0000000..b432cbc
--- /dev/null
@@ -0,0 +1,1187 @@
+/*
+ *  Copyright (C) 2006,2007 Gabor Juhos
+ *
+ *  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.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>     /* for unlink() */
+#include <libgen.h>
+#include <getopt.h>     /* for getopt() */
+#include <stdarg.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <endian.h>     /* for __BYTE_ORDER */
+
+#if defined(__CYGWIN__)
+#  include <byteswap.h>
+#endif
+
+#if (__BYTE_ORDER == __LITTLE_ENDIAN)
+#  define HOST_TO_LE16(x)      (x)
+#  define HOST_TO_LE32(x)      (x)
+#else
+#  define HOST_TO_LE16(x)      bswap_16(x)
+#  define HOST_TO_LE32(x)      bswap_32(x)
+#endif
+
+#include "myloader.h"
+
+#define MAX_FW_BLOCKS          32
+#define MAX_ARG_COUNT   32
+#define MAX_ARG_LEN     1024
+#define FILE_BUF_LEN    (16*1024)
+
+struct fw_block {
+       uint32_t        addr;
+       uint32_t        blocklen; /* length of the block */
+       uint32_t        flags;
+
+       char            *name;  /* name of the file */
+       uint32_t        size;   /* length of the file */
+       uint32_t        crc;    /* crc value of the file */
+};
+
+#define BLOCK_FLAG_HAVEHDR    0x0001
+
+struct cpx_board {
+       char            *model; /* model number*/
+       char            *name;  /* model name*/
+       char            *desc;  /* description */
+       uint16_t        vid;    /* vendor id */
+       uint16_t        did;    /* device id */
+       uint16_t        svid;   /* sub vendor id */
+       uint16_t        sdid;   /* sub device id */
+       uint32_t        flash_size;     /* size of flash */
+};
+
+#define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc) {          \
+       .model = _mod, .name = _name, .desc = _desc,                            \
+       .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid,         \
+       .flash_size = (_flash << 20)    }
+
+#define CPX_BOARD(_did, _flash, _mod, _name, _desc) \
+       BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc)
+
+char   *progname;
+char   *ofname = NULL;
+
+uint32_t flash_size = 0;
+int    fw_num_partitions = 0;
+int    fw_num_blocks = 0;
+int    verblevel = 0;
+
+struct mylo_fw_header fw_header;
+struct mylo_partition fw_partitions[MYLO_MAX_PARTITIONS];
+struct fw_block fw_blocks[MAX_FW_BLOCKS];
+
+struct cpx_board boards[] = {
+       CPX_BOARD(DEVID_COMPEX_NP18A, 4,
+               "NP18A", "Compex NetPassage 18A",
+               "Dualband Wireless A+G Internet Gateway"),
+       CPX_BOARD(DEVID_COMPEX_NP26G8M, 2,
+               "NP26G8M", "Compex NetPassage 26G (8M)",
+               "Wireless-G Broadband Multimedia Gateway"),
+       CPX_BOARD(DEVID_COMPEX_NP26G16M, 4,
+               "NP26G16M", "Compex NetPassage 26G (16M)",
+               "Wireless-G Broadband Multimedia Gateway"),
+       CPX_BOARD(DEVID_COMPEX_NP27G, 4,
+               "NP27G", "Compex NetPassage 27G",
+               "Wireless-G 54Mbps eXtended Range Router"),
+       CPX_BOARD(DEVID_COMPEX_NP28G, 4,
+               "NP28G", "Compex NetPassage 28G",
+               "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
+       CPX_BOARD(DEVID_COMPEX_NP28GHS, 4,
+               "NP28GHS", "Compex NetPassage 28G (HotSpot)",
+               "HotSpot Solution"),
+       CPX_BOARD(DEVID_COMPEX_WP18, 4,
+               "WP18", "Compex NetPassage WP18",
+               "Wireless-G 54Mbps A+G Dualband Access Point"),
+       CPX_BOARD(DEVID_COMPEX_WP54G, 4,
+               "WP54G", "Compex WP54G",
+               "Wireless-G 54Mbps XR Access Point"),
+       CPX_BOARD(DEVID_COMPEX_WP54Gv1C, 2,
+               "WP54Gv1C", "Compex WP54G rev.1C",
+               "Wireless-G 54Mbps XR Access Point"),
+       CPX_BOARD(DEVID_COMPEX_WP54AG, 4,
+               "WP54AG", "Compex WP54AG",
+               "Wireless-AG 54Mbps XR Access Point"),
+       CPX_BOARD(DEVID_COMPEX_WPP54G, 4,
+               "WPP54G", "Compex WPP54G",
+               "Outdoor Access Point"),
+       CPX_BOARD(DEVID_COMPEX_WPP54AG, 4,
+               "WPP54AG", "Compex WPP54AG",
+               "Outdoor Access Point"),
+       {.model = NULL}
+};
+
+void
+errmsgv(int syserr, const char *fmt, va_list arg_ptr)
+{
+       int save = errno;
+
+       fflush(0);
+       fprintf(stderr, "[%s] Error: ", progname);
+       vfprintf(stderr, fmt, arg_ptr);
+       if (syserr != 0) {
+               fprintf(stderr, ": %s", strerror(save));
+       }
+       fprintf(stderr, "\n");
+}
+
+void
+errmsg(int syserr, const char *fmt, ...)
+{
+       va_list arg_ptr;
+       va_start(arg_ptr, fmt);
+       errmsgv(syserr, fmt, arg_ptr);
+       va_end(arg_ptr);
+}
+
+void
+dbgmsg(int level, const char *fmt, ...)
+{
+       va_list arg_ptr;
+       if (verblevel >= level) {
+               fflush(0);
+               va_start(arg_ptr, fmt);
+               vfprintf(stderr, fmt, arg_ptr);
+               fprintf(stderr, "\n");
+               va_end(arg_ptr);
+       }
+}
+
+
+void
+usage(int status)
+{
+       FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
+       struct cpx_board *board;
+
+       fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
+       fprintf(stream,
+"\n"
+"  <file>          write output to the <file>\n"
+"\n"
+"Options:\n"
+"  -B <board>      create firmware for the board specified with <board>.\n"
+"                  This option set vendor id, device id, subvendor id,\n"
+"                  subdevice id, and flash size options to the right value.\n"
+"                  valid <board> values:\n");
+       for (board = boards; board->model != NULL; board++){
+               fprintf(stream,
+"                      %-12s: %s\n",
+                board->model, board->name);
+       };
+       fprintf(stream,
+"  -i <vid>:<did>[:<svid>[:<sdid>]]\n"
+"                  create firmware for board with vendor id <vid>, device\n"
+"                  id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
+"  -r <rev>        set board revision to <rev>.\n"
+"  -s <size>       set flash size to <size>\n"
+"  -b <addr>:<len>[:[<flags>]:<file>]\n"
+"                  define block at <addr> with length of <len>.\n"
+"                  valid <flag> values:\n"
+"                      h : add crc header before the file data.\n"
+"  -p <addr>:<len>[:<flags>[:<param>[:<file>]]]\n"
+"                  add partition at <addr>, with size of <len> to the\n"
+"                  partition table, set partition flags to <flags> and\n"
+"                  partition parameter to <param>. If the <file> is specified\n"
+"                  content of the file is also added to the firmware image.\n"
+"                  valid <flag> values:\n"
+"                      a:  this is the active partition. The bootloader loads\n"
+"                          the firmware from this partition.\n"
+"                      h:  the partition data have a header.\n"
+"                      p:  the bootloader loads data from this partition to\n"
+"                          the RAM before decompress it.\n"
+"  -h              show this screen\n"
+       );
+
+       exit(status);
+}
+
+/*
+ * Code to compute the CRC-32 table. Borrowed from
+ * gzip-1.0.3/makecrc.c.
+ */
+
+static uint32_t crc_32_tab[256];
+
+void
+init_crc_table(void)
+{
+       /* Not copyrighted 1990 Mark Adler      */
+
+       uint32_t c;      /* crc shift register */
+       uint32_t e;      /* polynomial exclusive-or pattern */
+       int i;           /* counter for all possible eight bit values */
+       int k;           /* byte being shifted into crc apparatus */
+
+       /* terms of polynomial defining this crc (except x^32): */
+       static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+       /* Make exclusive-or pattern from polynomial */
+       e = 0;
+       for (i = 0; i < sizeof(p)/sizeof(int); i++)
+               e |= 1L << (31 - p[i]);
+
+       crc_32_tab[0] = 0;
+
+       for (i = 1; i < 256; i++) {
+               c = 0;
+               for (k = i | 256; k != 1; k >>= 1) {
+                       c = c & 1 ? (c >> 1) ^ e : c >> 1;
+                       if (k & 1)
+                               c ^= e;
+               }
+               crc_32_tab[i] = c;
+       }
+}
+
+
+void
+update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
+{
+       uint32_t t;
+
+       t = *crc ^ 0xFFFFFFFFUL;
+       while (len--) {
+               t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
+       }
+       *crc = t ^ 0xFFFFFFFFUL;
+}
+
+
+uint32_t
+get_crc(uint8_t *p, uint32_t len)
+{
+       uint32_t crc;
+       
+       crc = 0;
+       update_crc(p ,len , &crc);
+       return crc;
+}
+
+
+int
+str2u32(char *arg, uint32_t *val)
+{
+       char *err = NULL;
+       uint32_t t;
+       
+       errno=0;
+       t = strtoul(arg, &err, 0);
+       if (errno || (err==arg) || ((err != NULL) && *err)) {
+               return -1;
+       }
+
+       *val = t;
+       return 0;
+}
+
+
+int
+str2u16(char *arg, uint16_t *val)
+{
+       char *err = NULL;
+       uint32_t t;
+
+       errno=0;
+       t = strtoul(arg, &err, 0);
+       if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
+               return -1;
+       }
+       
+       *val = t & 0xFFFF;
+       return 0;
+}
+
+
+struct cpx_board *
+find_board(char *model){
+       struct cpx_board *board;
+       struct cpx_board *tmp;
+
+       board = NULL;
+       for (tmp = boards; tmp->model != NULL; tmp++){
+               if (strcmp(model, tmp->model) == 0) {
+                       board = tmp;
+                       break;
+               }
+       };
+
+       return board;
+}
+
+
+int
+get_file_crc(struct fw_block *ff)
+{
+       FILE *f;
+       uint8_t buf[FILE_BUF_LEN];
+       uint32_t readlen = sizeof(buf);
+       int res = -1;
+       size_t len;
+       
+       if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
+               res = 0;
+               goto out;
+       }
+       
+       errno = 0;
+       f = fopen(ff->name,"r");
+       if (errno) {
+               errmsg(1,"unable to open file %s", ff->name);
+               goto out;
+       }
+       
+       ff->crc = 0;
+       len = ff->size;
+       while (len > 0) {
+               if (len < readlen)
+                       readlen = len;
+
+               errno = 0;
+               fread(buf, readlen, 1, f);
+               if (errno) {
+                       errmsg(1,"unable to read from file %s", ff->name);
+                       goto out_close;
+               }
+
+               update_crc(buf, readlen, &ff->crc);
+               len -= readlen;
+       }
+
+       res = 0;
+       
+out_close:
+       fclose(f);
+out:
+       return res;
+}
+
+
+int
+process_files(void)
+{
+       struct fw_block *b;
+       struct stat st;
+       int i;
+
+       for (i = 0; i < fw_num_blocks; i++) {
+               b = &fw_blocks[i];
+               if ((b->addr + b->blocklen) > flash_size) {
+                       errmsg(0, "block at 0x%08X is too big", b->addr);
+                       return -1;
+               }
+               if (b->name == NULL)
+                       continue;
+
+               if (stat(b->name, &st) < 0) {
+                       errmsg(0, "stat failed on %s",b->name);
+                       return -1;
+               }
+               if (b->blocklen == 0) {
+                       b->blocklen = flash_size - b->addr;
+               }
+               if (st.st_size > b->blocklen) {
+                       errmsg(0,"file %s is too big",b->name);
+                       return -1;
+               }
+
+               b->size = st.st_size;
+       }
+
+       return 0;
+}
+
+
+int
+process_partitions(void)
+{
+       struct mylo_partition *part;
+       int i;
+
+       for (i = 0; i < fw_num_partitions; i++) {
+               part = &fw_partitions[i];
+
+               if (part->addr > flash_size) {
+                       errmsg(0, "invalid partition at 0x%08X", part->addr);
+                       return -1;
+               }
+
+               if ((part->addr + part->size) > flash_size) {
+                       errmsg(0, "partition at 0x%08X is too big", part->addr);
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+
+/*
+ * routines to write data to the output file
+ */
+int
+write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
+{
+       errno = 0;
+
+       fwrite(data, len, 1, outfile);
+       if (errno) {
+               errmsg(1,"unable to write output file");
+               return -1;
+       }
+
+       if (crc) {
+               update_crc(data, len, crc);
+       }
+
+       return 0;
+}
+
+
+inline int
+write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
+{
+       return write_out_data(outfile, (uint8_t *)desc,
+               sizeof(*desc), crc);
+}
+
+
+int
+write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
+{
+       char buff[FILE_BUF_LEN];
+       size_t  buflen = sizeof(buff);
+       FILE *f;
+       size_t len;
+
+       errno = 0;
+       
+       if (block->name == NULL) {
+               return 0;
+       }
+       
+       if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
+               struct mylo_partition_header ph;
+               
+               if (get_file_crc(block) != 0)
+                       return -1;
+               
+               ph.crc = HOST_TO_LE32(block->crc);
+               ph.len = HOST_TO_LE32(block->size);
+               
+               if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
+                       return -1;
+       }
+       
+       f = fopen(block->name,"r");
+       if (errno) {
+               errmsg(1,"unable to open file: %s", block->name);
+               return -1;
+       }
+
+       len = block->size;
+       while (len > 0) {
+               if (len < buflen)
+                       buflen = len;
+
+               // read data from source file
+               errno = 0;
+               fread(buff, buflen, 1, f);
+               if (errno != 0) {
+                       errmsg(1,"unable to read from file: %s",block->name);
+                       return -1;
+               }
+
+               if (write_out_data(outfile, buff, buflen, crc) != 0)
+                       return -1;
+                       
+               len -= buflen;
+       }
+
+       fclose(f);
+       
+       dbgmsg(1,"file %s written out", block->name);
+       return 0;
+}
+
+
+int
+write_out_header(FILE *outfile, uint32_t *crc)
+{
+       struct mylo_fw_header hdr;
+
+       memset(&hdr, 0, sizeof(hdr));
+
+       hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
+       hdr.crc = HOST_TO_LE32(fw_header.crc);
+       hdr.vid = HOST_TO_LE16(fw_header.vid);
+       hdr.did = HOST_TO_LE16(fw_header.did);
+       hdr.svid = HOST_TO_LE16(fw_header.svid);
+       hdr.sdid = HOST_TO_LE16(fw_header.sdid);
+       hdr.rev = HOST_TO_LE32(fw_header.rev);
+       hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
+       hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
+       hdr.flags = HOST_TO_LE32(fw_header.flags);
+
+       if (fseek(outfile, 0, SEEK_SET) != 0) {
+               errmsg(1,"fseek failed on output file");
+               return -1;
+       }
+
+       return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
+}
+
+
+int
+write_out_partitions(FILE *outfile, uint32_t *crc)
+{
+       struct mylo_partition_table p;
+       struct mylo_partition *p1, *p2;
+       int i;
+
+       if (fw_num_partitions == 0)
+               return 0;
+
+       memset(&p, 0, sizeof(p));
+
+       p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
+       for (i = 0; i < fw_num_partitions; i++) {
+               p1 = &p.partitions[i];
+               p2 = &fw_partitions[i];
+               p1->flags = HOST_TO_LE16(p2->flags);
+               p1->type = HOST_TO_LE16(PARTITION_TYPE_USED);
+               p1->addr = HOST_TO_LE32(p2->addr);
+               p1->size = HOST_TO_LE32(p2->size);
+               p1->param = HOST_TO_LE32(p2->param);
+       }
+
+       return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
+}
+
+
+int
+write_out_blocks(FILE *outfile, uint32_t *crc)
+{
+       struct mylo_fw_blockdesc desc;
+       struct fw_block *b;
+       int i;
+
+       /*
+        * if at least one partition specified, write out block descriptor
+        * for the partition table
+        */
+       if (fw_num_partitions > 0) {
+
+               desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
+               desc.addr = HOST_TO_LE32(0x10000);
+               desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
+               desc.blen = HOST_TO_LE32(0x10000);
+
+               if (write_out_desc(outfile, &desc, crc) != 0)
+                       return -1;
+       }
+
+       /*
+        * write out block descriptors for each files
+        */
+       for (i = 0; i < fw_num_blocks; i++) {
+               b = &fw_blocks[i];
+               desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
+               desc.addr = HOST_TO_LE32(b->addr);
+               desc.blen = HOST_TO_LE32(b->blocklen);
+               if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
+                       desc.dlen = HOST_TO_LE32(b->size +
+                               sizeof(struct mylo_partition_header));
+               } else {
+                       desc.dlen = HOST_TO_LE32(b->size);
+               }
+               if (write_out_desc(outfile, &desc, crc) != 0)
+                       return -1;
+       }
+
+       /*
+        * write out the null block descriptor
+        */
+       memset(&desc, 0, sizeof(desc));
+       if (write_out_desc(outfile, &desc, crc) != 0)
+               return -1;
+
+       if (write_out_partitions(outfile, crc) != 0)
+               return -1;
+
+       /*
+        * write out data for each blocks
+        */
+       for (i = 0; i < fw_num_blocks; i++) {
+               b = &fw_blocks[i];
+               if (write_out_file(outfile, b, crc) != 0)
+                       return -1;
+       }
+
+       return 0;
+}
+
+
+/*
+ * argument parsing
+ */
+int
+parse_arg(char *arg, char *buf, char *argv[])
+{
+       int res = 0;
+       size_t argl;
+       char *tok;
+       char **ap = &buf;
+       int i;
+
+       if ((arg == NULL)) {
+               /* invalid argument string */
+               return -1;
+       }
+
+       argl = strlen(arg);
+       if (argl == 0) {
+               /* no arguments */
+               return res;
+       }
+
+       if (argl >= MAX_ARG_LEN) {
+               /* argument is too long */
+               argl = MAX_ARG_LEN-1;
+       }
+
+       memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
+       memcpy(buf, arg, argl);
+       buf[argl] = '\0';
+
+       for (i = 0; i < MAX_ARG_COUNT; i++) {
+               tok = strsep(ap, ":");
+               if (tok == NULL) {
+                       break;
+               }
+#if 0
+               else if (tok[0] == '\0') {
+                       break;
+               }
+#endif
+               argv[i] = tok;
+               res++;
+       }
+
+       return res;
+}
+
+
+int
+required_arg(char c, char *arg)
+{
+       if ((optarg != NULL) && (*arg == '-')){
+               errmsg(0,"option %c requires an argument\n", c);
+               return -1;
+       }
+       
+       return 0;
+}
+
+
+int
+is_empty_arg(char *arg)
+{
+       int ret = 1;
+       if (arg != NULL) {
+               if (*arg) ret = 0;
+       };
+       return ret;
+}
+
+
+int
+parse_opt_flags(char ch, char *arg)
+{
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+
+       if (str2u32(arg, &fw_header.flags) != 0) {
+               errmsg(0,"invalid firmware flags: %s", arg);
+               goto err_out;
+       }
+       
+       dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
+
+       return 0;
+
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_size(char ch, char *arg)
+{
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+
+       if (str2u32(arg, &flash_size) != 0) {
+               errmsg(0,"invalid flash size: %s", arg);
+               goto err_out;
+       }
+       
+       dbgmsg(1, "flash size set to %d bytes", flash_size);
+
+       return 0;
+
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_id(char ch, char *arg)
+{
+       char buf[MAX_ARG_LEN];
+       char *argv[MAX_ARG_COUNT];
+       int argc;
+       char *p;
+
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+
+       argc = parse_arg(arg, buf, argv);
+
+       /* processing vendor ID*/
+       p = argv[0];
+       if (is_empty_arg(p)) {
+               errmsg(0,"vendor id is missing from -%c %s",ch, arg);
+               goto err_out;
+       } else if (str2u16(p, &fw_header.vid) != 0) {
+               errmsg(0,"invalid vendor id: %s", p);
+               goto err_out;
+       }
+
+       dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
+
+       /* processing device ID*/
+       p = argv[1];
+       if (is_empty_arg(p)) {
+               errmsg(0,"device id is missing from -%c %s",ch, arg);
+               goto err_out;
+       } else if (str2u16(p, &fw_header.did) != 0) {
+               errmsg(0,"invalid device id: %s", p);
+               goto err_out;
+       }
+
+       dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
+
+       /* processing sub vendor ID*/
+       p = argv[2];
+       if (is_empty_arg(p)) {
+               fw_header.svid = fw_header.vid;
+       } else if (str2u16(p, &fw_header.svid) != 0) {
+               errmsg(0,"invalid sub vendor id: %s", p);
+               goto err_out;
+       }
+
+       dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
+
+       /* processing device ID*/
+       p = argv[3];
+       if (is_empty_arg(p)) {
+               fw_header.sdid = fw_header.did;
+       } else if (str2u16(p, &fw_header.sdid) != 0) {
+               errmsg(0,"invalid sub device id: %s", p);
+               goto err_out;
+       }
+
+       dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
+
+       /* processing revision */
+       p = argv[4];
+       if (is_empty_arg(p)) {
+               fw_header.rev = 0;
+       } else if (str2u32(arg, &fw_header.rev) != 0) {
+               errmsg(0,"invalid revision number: %s", p);
+               goto err_out;
+       }
+
+       dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
+
+       return 0;
+
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_block(char ch, char *arg)
+{
+       char buf[MAX_ARG_LEN];
+       char *argv[MAX_ARG_COUNT];
+       int argc;
+       struct fw_block *b;
+       char *p;
+
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+
+       if (fw_num_blocks >= MAX_FW_BLOCKS) {
+               errmsg(0,"too many blocks specified");
+               goto err_out;
+       }
+
+       argc = parse_arg(arg, buf, argv);
+       dbgmsg(1,"processing block option %s, count %d", arg, argc);
+
+       b = &fw_blocks[fw_num_blocks++];
+
+       /* processing block address */
+       p = argv[0];
+       if (is_empty_arg(p)) {
+               errmsg(0,"no block address specified in %s", arg);
+               goto err_out;
+       } else if (str2u32(p, &b->addr) != 0) {
+               errmsg(0,"invalid block address: %s", p);
+               goto err_out;
+       }
+
+       /* processing block length */
+       p = argv[1];
+       if (is_empty_arg(p)) {
+               errmsg(0,"no block length specified in %s", arg);
+               goto err_out;
+       } else if (str2u32(p, &b->blocklen) != 0) {
+               errmsg(0,"invalid block length: %s", p);
+               goto err_out;
+       }
+       
+       if (argc < 3) {
+               dbgmsg(1,"empty block %s", arg);
+               goto success;
+       }
+       
+       /* processing flags */
+       p = argv[2];
+       if (is_empty_arg(p) == 0) {
+               for ( ; *p != '\0'; p++) {
+                       switch (*p) {
+                       case 'h':
+                               b->flags |= BLOCK_FLAG_HAVEHDR;
+                               break;
+                       default:
+                               errmsg(0, "invalid block flag \"%c\"", *p);
+                               goto err_out;
+                       }
+               }
+       }
+
+       /* processing file name */
+       p = argv[3];
+       if (is_empty_arg(p)) {
+               errmsg(0,"file name missing in %s", arg);
+               goto err_out;
+       }
+       
+       b->name = strdup(p);
+       if (b->name == NULL) {
+               errmsg(0,"not enough memory");
+               goto err_out;
+       }
+       
+success:
+
+       return 0;
+       
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_partition(char ch, char *arg)
+{
+       char buf[MAX_ARG_LEN];
+       char *argv[MAX_ARG_COUNT];
+       int argc;
+       char *p;
+
+       struct mylo_partition *part;
+
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+
+       if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
+               errmsg(0, "too many partitions specified");
+               goto err_out;
+       }
+       
+       part = &fw_partitions[fw_num_partitions++];
+
+       argc = parse_arg(arg, buf, argv);
+
+       /* processing partition address */
+       p = argv[0];
+       if (is_empty_arg(p)) {
+               errmsg(0,"partition address missing in -%c %s",ch, arg);
+               goto err_out;
+       } else if (str2u32(p, &part->addr) != 0) {
+               errmsg(0,"invalid partition address: %s", p);
+               goto err_out;
+       }
+
+       /* processing partition size */
+       p = argv[1];
+       if (is_empty_arg(p)) {
+               errmsg(0,"partition size missing in -%c %s",ch, arg);
+               goto err_out;
+       } else if (str2u32(p, &part->size) != 0) {
+               errmsg(0,"invalid partition size: %s", p);
+               goto err_out;
+       }
+
+       /* processing partition flags */
+       p = argv[2];
+       if (is_empty_arg(p) == 0) {
+               for ( ; *p != '\0'; p++) {
+                       switch (*p) {
+                       case 'a':
+                               part->flags |= PARTITION_FLAG_ACTIVE;
+                               break;
+                       case 'p':
+                               part->flags |= PARTITION_FLAG_PRELOAD;
+                               break;
+                       case 'h':
+                               part->flags |= PARTITION_FLAG_HAVEHDR;
+                               break;
+                       default:
+                               errmsg(0, "invalid partition flag \"%c\"", *p);
+                               goto err_out;
+                       }
+               }
+       }
+
+       /* processing partition parameter */
+       p = argv[3];
+       if (is_empty_arg(p)) {
+               /* set default partition parameter */
+               part->param = 0;
+       } else if (str2u32(p, &part->param) != 0) {
+               errmsg(0,"invalid partition parameter: %s", p);
+               goto err_out;
+       }
+
+#if 1
+       if (part->size == 0) {
+               part->size = flash_size - part->addr;
+       }
+
+       /* processing file parameter */
+       p = argv[4];
+       if (is_empty_arg(p) == 0) {
+               struct fw_block *b;
+               
+               if (fw_num_blocks == MAX_FW_BLOCKS) {
+                       errmsg(0,"too many blocks specified", p);
+                       goto err_out;
+               }
+               b = &fw_blocks[fw_num_blocks++];
+               b->name = strdup(p);
+               b->addr = part->addr;
+               b->blocklen = part->size;
+               if (part->flags & PARTITION_FLAG_HAVEHDR) {
+                       b->flags |= BLOCK_FLAG_HAVEHDR;
+               }
+       }
+#endif
+
+       return 0;
+
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_board(char ch, char *arg)
+{
+       struct cpx_board *board;
+       
+       if (required_arg(ch, arg)) {
+               goto err_out;
+       }
+       
+       board = find_board(arg);
+       if (board == NULL){
+               errmsg(0,"invalid/unknown board specified: %s", arg);
+               goto err_out;
+       }
+       
+       fw_header.vid = board->vid;
+       fw_header.did = board->did;
+       fw_header.svid = board->svid;
+       fw_header.sdid = board->sdid;
+       
+       flash_size = board->flash_size;
+
+       return 0;
+       
+err_out:
+       return -1;
+}
+
+
+int
+parse_opt_rev(char ch, char *arg)
+{
+       if (required_arg(ch, arg)) {
+               return -1;
+       }
+
+       if (str2u32(arg, &fw_header.rev) != 0) {
+               errmsg(0,"invalid revision number: %s", arg);
+               return -1;
+       }
+
+       return 0;
+}
+
+
+/*
+ * main
+ */
+int
+main(int argc, char *argv[])
+{
+       int optinvalid = 0;   /* flag for invalid option */
+       int c;
+       int res = EXIT_FAILURE;
+
+       FILE  *outfile;
+       uint32_t crc;
+       
+       progname=basename(argv[0]);
+       
+       memset(&fw_header, 0, sizeof(fw_header));
+       
+       /* init header defaults */
+       fw_header.vid = VENID_COMPEX;
+       fw_header.did = DEVID_COMPEX_WP54G;
+       fw_header.svid = VENID_COMPEX;
+       fw_header.sdid = DEVID_COMPEX_WP54G;
+       fw_header.fwhi = 0x20000;
+       fw_header.fwlo = 0x20000;
+       fw_header.flags = 0;
+
+       opterr = 0;  /* could not print standard getopt error messages */
+       while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
+               optinvalid = 0;
+               switch (c) {
+               case 'b':
+                       optinvalid = parse_opt_block(c,optarg);
+                       break;
+               case 'B':
+                       optinvalid = parse_opt_board(c,optarg);
+                       break;
+               case 'f':
+                       optinvalid = parse_opt_flags(c,optarg);
+                       break;
+               case 'h':
+                       usage(EXIT_SUCCESS);
+                       break;
+               case 'i':
+                       optinvalid = parse_opt_id(c,optarg);
+                       break;
+               case 'p':
+                       optinvalid = parse_opt_partition(c,optarg);
+                       break;
+               case 'r':
+                       optinvalid = parse_opt_rev(c,optarg);
+                       break;
+               case 's':
+                       optinvalid = parse_opt_size(c,optarg);
+                       break;
+               case 'v':
+                       verblevel++;
+                       break;
+               default:
+                       optinvalid = 1;
+                       break;
+               }
+               if (optinvalid != 0 ){
+                       errmsg(0, "invalid option: -%c", optopt);
+                       goto out;
+               }
+       }
+       
+       if (optind == argc) {
+               errmsg(0, "no output file specified");
+               goto out;
+       }
+
+       ofname = argv[optind++];
+       
+       if (optind < argc) {
+               errmsg(0, "invalid option: %s", argv[optind]);
+               goto out;
+       }
+       
+       if (flash_size == 0) {
+               errmsg(0, "no flash size specified");
+               goto out;
+       }
+       
+       if (process_files() != 0) {
+               goto out;
+       }
+
+       if (process_partitions() != 0) {
+               goto out;
+       }
+
+       outfile = fopen(ofname, "w");
+       if (outfile == NULL) {
+               errmsg(1, "could not open \"%s\" for writing", ofname);
+               goto out;
+       }
+
+       crc = 0;
+       init_crc_table();
+
+       if (write_out_header(outfile, &crc) != 0)
+               goto out_flush;
+               
+       if (write_out_blocks(outfile, &crc) != 0)
+               goto out_flush;
+
+       fw_header.crc = crc;
+       if (write_out_header(outfile, NULL) != 0)
+               goto out_flush;
+
+       dbgmsg(1,"Firmware file %s completed.", ofname);
+
+       res = EXIT_SUCCESS;
+
+out_flush:
+       fflush(outfile);
+       fclose(outfile);
+       if (res != EXIT_SUCCESS) {
+               unlink(ofname);
+       }
+out:
+       return res;
+}
diff --git a/tools/firmware-utils/src/myloader.h b/tools/firmware-utils/src/myloader.h
new file mode 100644 (file)
index 0000000..2c8b50e
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ *  Copyright (C) 2006,2007 Gabor Juhos
+ *
+ *  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.
+ *
+ */
+
+#ifndef _MYLOADER_H_
+#define _MYLOADER_H_
+
+/*
+ * Firmware file format:
+ *
+ *     <header>
+ *     [<block descriptor 0>]
+ *     ...
+ *     [<block descriptor n>]
+ *     <null block descriptor>
+ *     [<block data 0>]
+ *     ...
+ *     [<block data n>]
+ *
+ *
+ */
+
+#define MYLO_MAGIC_FIRMWARE    0x4C594D00
+#define MYLO_MAGIC_20021103    0x20021103
+#define MYLO_MAGIC_20021107    0x20021107
+
+#define MYLO_MAGIC_SYSSETUP    MYLO_MAGIC_20021107
+#define MYLO_MAGIC_PARTITIONS  MYLO_MAGIC_20021103
+
+/* Vendor ID's (seems to be same as the PCI vendor ID's) */
+#define VENID_COMPEX           0x11F6
+
+/* Devices based on the ADM5120 */
+#define DEVID_COMPEX_NP27G     0x0078
+#define DEVID_COMPEX_NP28G     0x044C
+#define DEVID_COMPEX_NP28GHS   0x044E
+#define DEVID_COMPEX_WP54Gv1C  0x0514
+#define DEVID_COMPEX_WP54G     0x0515
+#define DEVID_COMPEX_WP54AG    0x0546
+#define DEVID_COMPEX_WPP54AG   0x0550
+#define DEVID_COMPEX_WPP54G    0x0555
+
+/* Devices based on the IXP422 */
+#define DEVID_COMPEX_WP18      0x047E
+#define DEVID_COMPEX_NP18A     0x0489
+
+/* Other devices */
+#define DEVID_COMPEX_NP26G8M   0x03E8
+#define DEVID_COMPEX_NP26G16M  0x03E9
+
+struct mylo_fw_header {
+       uint32_t        magic;  /* must be MYLO_MAGIC_MYLO */
+       uint32_t        crc;    /* CRC of the whole firmware */
+       uint32_t        res0;   /* unknown/unused */
+       uint32_t        res1;   /* unknown/unused */
+       uint16_t        vid;    /* vendor ID */
+       uint16_t        did;    /* device ID */
+       uint16_t        svid;   /* sub vendor ID */
+       uint16_t        sdid;   /* sub device ID */
+       uint32_t        rev;    /* device revision */
+       uint32_t        fwhi;   /* FIXME: firmware version high? */
+       uint32_t        fwlo;   /* FIXME: firmware version low? */
+       uint32_t        flags;  /* firmware flags */
+};
+
+#define FW_FLAG_BOARD_PARAMS_WP        0x01 /* board parameters are write protected */
+#define FW_FLAG_BOOT_SECTOR_WE 0x02 /* enable of write boot sectors (below 64K) */
+
+struct mylo_fw_blockdesc {
+       uint32_t        type;   /* block type */
+       uint32_t        addr;   /* relative address to flash start */
+       uint32_t        dlen;   /* size of block data in bytes */
+       uint32_t        blen;   /* total size of block in bytes */
+};
+
+#define FW_DESC_TYPE_UNUSED    0
+#define FW_DESC_TYPE_USED      1
+
+struct mylo_partition {
+       uint16_t        flags;  /* partition flags */
+       uint16_t        type;   /* type of the partition */
+       uint32_t        addr;   /* relative address of the partition from the
+                                  flash start */
+       uint32_t        size;   /* size of the partition in bytes */
+       uint32_t        param;  /* if this is the active partition, the
+                                  MyLoader load code to this address */
+};
+
+#define PARTITION_FLAG_ACTIVE  0x8000 /* this is the active partition,
+                                       * MyLoader loads firmware from here */
+#define PARTITION_FLAG_ISRAM   0x2000 /* FIXME: this is a RAM partition? */
+#define PARTIIION_FLAG_RAMLOAD 0x1000 /* FIXME: load this partition into the RAM? */
+#define PARTITION_FLAG_PRELOAD 0x0800 /* the partition data preloaded to RAM
+                                       * before decompression */
+#define PARTITION_FLAG_HAVEHDR  0x0002 /* the partition data have a header */
+
+#define PARTITION_TYPE_FREE    0
+#define PARTITION_TYPE_USED    1
+
+#define MYLO_MAX_PARTITIONS    8       /* maximum number of partitions in the
+                                          partition table */
+
+struct mylo_partition_table {
+       uint32_t        magic;  /* must be 0x20021103 */
+       uint32_t        res0;   /* unknown/unused */
+       uint32_t        res1;   /* unknown/unused */
+       uint32_t        res2;   /* unknown/unused */
+       struct mylo_partition partitions[MYLO_MAX_PARTITIONS];
+};
+
+struct mylo_partition_header {
+       uint32_t        len;    /* length of the partition data */
+       uint32_t        crc;    /* CRC value of the partition data */
+};
+
+struct mylo_system_params {
+       uint32_t        magic;
+       uint32_t        res0;
+       uint32_t        res1;
+       uint32_t        mylo_ver;
+       uint16_t        vid;    /* Vendor ID */
+       uint16_t        did;    /* Device ID */
+       uint16_t        svid;   /* Sub Vendor ID */
+       uint16_t        sdid;   /* Sub Device ID */
+       uint32_t        rev;    /* device revision */
+       uint32_t        fwhi;
+       uint32_t        fwlo;
+       uint32_t        tftp_addr;
+       uint32_t        prog_start;
+       uint32_t        flash_size;
+       uint32_t        dram_size;
+};
+
+
+struct mylo_eth_addr {
+       uint8_t mac[6];
+       uint8_t csum[2];
+};
+
+#define MYLO_ETHADDR_COUNT     8       /* maximum number of ethernet address
+                                          in the board parameters */
+
+struct mylo_board_params {
+       uint32_t        magic;
+       uint32_t        res0;
+       uint32_t        res1;
+       uint32_t        res2;
+       struct mylo_eth_addr addr[MYLO_ETHADDR_COUNT];
+};
+
+#endif /* _MYLOADER_H_*/