diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/.gitignore | 2 | ||||
-rw-r--r-- | scripts/Makefile | 2 | ||||
-rw-r--r-- | scripts/Makefile.lib | 46 | ||||
-rw-r--r-- | scripts/basic/fixdep.c | 206 | ||||
-rw-r--r-- | scripts/imx/.gitignore | 2 | ||||
-rw-r--r-- | scripts/imx/Makefile | 10 | ||||
-rw-r--r-- | scripts/imx/README | 89 | ||||
-rw-r--r-- | scripts/imx/imx-image.c | 744 | ||||
-rw-r--r-- | scripts/imx/imx-usb-loader.c | 1427 | ||||
-rw-r--r-- | scripts/kwbimage.c | 1496 | ||||
-rw-r--r-- | scripts/kwboot.c | 730 |
11 files changed, 4671 insertions, 83 deletions
diff --git a/scripts/.gitignore b/scripts/.gitignore index bff805ddf8..6518c0f076 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -2,6 +2,8 @@ bareboxenv bin2c gen_netx_image kallsyms +kwbimage +kwboot mk-am35xx-spi-image mkimage mkublheader diff --git a/scripts/Makefile b/scripts/Makefile index f062fc011b..307dc3d1a4 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -8,11 +8,13 @@ hostprogs-$(CONFIG_KALLSYMS) += kallsyms hostprogs-y += bin2c hostprogs-y += mkimage hostprogs-y += bareboxenv +hostprogs-$(CONFIG_ARCH_MVEBU) += kwbimage kwboot hostprogs-$(CONFIG_ARCH_NETX) += gen_netx_image hostprogs-$(CONFIG_ARCH_OMAP) += omap_signGP mk-am35xx-spi-image hostprogs-$(CONFIG_ARCH_S5PCxx) += s5p_cksum hostprogs-$(CONFIG_ARCH_DAVINCI) += mkublheader hostprogs-$(CONFIG_ARCH_ZYNQ) += zynq_mkimage +subdir-$(CONFIG_ARCH_IMX) += imx HOSTLOADLIBES_omap4_usbboot = -lpthread omap4_usbboot-objs := usb_linux.o omap4_usbboot.o diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index bbfd4cd2de..0b56dcc218 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -141,10 +141,10 @@ cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags) ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS) -dtc_cpp_flags = -Wp,-MD,$(depfile) -nostdinc \ - -I$(srctree)/arch/$(SRCARCH)/dts \ - -I$(srctree)/arch/$(SRCARCH)/include/dts \ - -undef -D__DTS__ +dtc_cpp_flags = -Wp,-MD,$(depfile).pre -nostdinc \ + -I$(srctree)/arch/$(SRCARCH)/dts \ + -I$(srctree)/arch/$(SRCARCH)/dts/include \ + -undef -D__DTS__ # Finds the multi-part object the current object will be linked into modname-multi = $(sort $(foreach m,$(multi-used),\ @@ -212,20 +212,17 @@ $(obj)/%.dtb.S: $(obj)/%.dtb $(call cmd,dt_S_dtb) quiet_cmd_dtc = DTC $@ -cmd_dtc = $(objtree)/scripts/dtc/dtc -O dtb -o $@ -b 0 $(DTC_FLAGS) -d $(depfile) $< +cmd_dtc = $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \ + $(objtree)/scripts/dtc/dtc -O dtb -o $@ -b 0 \ + -i $(srctree)/arch/$(SRCARCH)/dts $(DTC_FLAGS) \ + -d $(depfile).dtc $(dtc-tmp) ; \ + cat $(depfile).pre $(depfile).dtc > $(depfile) $(obj)/%.dtb: $(src)/%.dts FORCE $(call if_changed_dep,dtc) dtc-tmp = $(subst $(comma),_,$(dot-target).dts) -quiet_cmd_dtc_cpp = DTC+CPP $@ -cmd_dtc_cpp = $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \ - $(objtree)/scripts/dtc/dtc -O dtb -o $@ -b 0 $(DTC_FLAGS) $(dtc-tmp) - -$(obj)/%.dtb: $(src)/%.dtsp FORCE - $(call if_changed_dep,dtc_cpp) - # Bzip2 # --------------------------------------------------------------------------- @@ -308,3 +305,28 @@ quiet_cmd_check_file_size = CHKSIZE $@ echo "$@ size $$size > of the maximum size $$max_size" >&2; \ exit 1 ; \ fi; + +quiet_cmd_imximage__S_dcd= DCD_S $@ +cmd_imximage_S_dcd= \ +( \ + echo '\#include <asm-generic/barebox.lds.h>'; \ + echo '.balign STRUCT_ALIGNMENT'; \ + echo '.global $(subst -,_,$(*F))_start'; \ + echo '$(subst -,_,$(*F))_start:'; \ + echo '.incbin "$<" '; \ + echo '$(subst -,_,$(*F))_end:'; \ + echo '.global $(subst -,_,$(*F))_end'; \ + echo '.balign STRUCT_ALIGNMENT'; \ +) > $@ + +quiet_cmd_dcd = DCD $@ + cmd_dcd = $(objtree)/scripts/imx/imx-image -d -o $@ -c $< + +$(obj)/%.dcd: $(obj)/%.imxcfg FORCE + $(call if_changed,dcd) + +$(obj)/%.S: $(obj)/%.dcd + $(call cmd,imximage_S_dcd) + +quiet_cmd_imx_image = IMX-IMG $@ + cmd_imx_image = $(obj)/scripts/imx/imx-image -b -c $(CFG_$(@F)) -f $< -o $@ diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index ea26b23de0..078fe1d64e 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -138,38 +138,36 @@ static void print_cmdline(void) printf("cmd_%s := %s\n\n", target, cmdline); } -char * str_config = NULL; -int size_config = 0; -int len_config = 0; +struct item { + struct item *next; + unsigned int len; + unsigned int hash; + char name[0]; +}; -/* - * Grow the configuration string to a desired length. - * Usually the first growth is plenty. - */ -static void grow_config(int len) -{ - while (len_config + len > size_config) { - if (size_config == 0) - size_config = 2048; - str_config = realloc(str_config, size_config *= 2); - if (str_config == NULL) - { perror("fixdep:malloc"); exit(1); } - } -} +#define HASHSZ 256 +static struct item *hashtab[HASHSZ]; +static unsigned int strhash(const char *str, unsigned int sz) +{ + /* fnv32 hash */ + unsigned int i, hash = 2166136261U; + for (i = 0; i < sz; i++) + hash = (hash ^ str[i]) * 0x01000193; + return hash; +} /* * Lookup a value in the configuration string. */ -static int is_defined_config(const char * name, int len) +static int is_defined_config(const char *name, int len, unsigned int hash) { - const char * pconfig; - const char * plast = str_config + len_config - len; - for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) { - if (pconfig[ -1] == '\n' - && pconfig[len] == '\n' - && !memcmp(pconfig, name, len)) + struct item *aux; + + for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) { + if (aux->hash == hash && aux->len == len && + memcmp(aux->name, name, len) == 0) return 1; } return 0; @@ -178,13 +176,19 @@ static int is_defined_config(const char * name, int len) /* * Add a new value to the configuration string. */ -static void define_config(const char * name, int len) +static void define_config(const char *name, int len, unsigned int hash) { - grow_config(len + 1); + struct item *aux = malloc(sizeof(*aux) + len); - memcpy(str_config+len_config, name, len); - len_config += len; - str_config[len_config++] = '\n'; + if (!aux) { + perror("fixdep:malloc"); + exit(1); + } + memcpy(aux->name, name, len); + aux->len = len; + aux->hash = hash; + aux->next = hashtab[hash % HASHSZ]; + hashtab[hash % HASHSZ] = aux; } /* @@ -192,40 +196,49 @@ static void define_config(const char * name, int len) */ static void clear_config(void) { - len_config = 0; - define_config("", 0); + struct item *aux, *next; + unsigned int i; + + for (i = 0; i < HASHSZ; i++) { + for (aux = hashtab[i]; aux; aux = next) { + next = aux->next; + free(aux); + } + hashtab[i] = NULL; + } } /* * Record the use of a CONFIG_* word. */ -static void use_config(char *m, int slen) +static void use_config(const char *m, int slen) { - char s[PATH_MAX]; - char *p; + unsigned int hash = strhash(m, slen); + int c, i; - if (is_defined_config(m, slen)) + if (is_defined_config(m, slen, hash)) return; - define_config(m, slen); - - memcpy(s, m, slen); s[slen] = 0; + define_config(m, slen, hash); - for (p = s; p < s + slen; p++) { - if (*p == '_') - *p = '/'; + printf(" $(wildcard include/config/"); + for (i = 0; i < slen; i++) { + c = m[i]; + if (c == '_') + c = '/'; else - *p = tolower((int)*p); + c = tolower(c); + putchar(c); } - printf(" $(wildcard include/config/%s.h) \\\n", s); + printf(".h) \\\n"); } -static void parse_config_file(char *map, size_t len) +static void parse_config_file(const char *map, size_t len) { - int *end = (int *) (map + len); + const int *end = (const int *) (map + len); /* start at +1, so that p can never be < map */ - int *m = (int *) map + 1; - char *p, *q; + const int *m = (const int *) map + 1; + const char *p, *q; for (; m < end; m++) { if (*m == INT_CONF) { p = (char *) m ; goto conf; } @@ -265,7 +278,7 @@ static int strrcmp(char *s, char *sub) return memcmp(s + slen - sublen, sub, sublen); } -static void do_config_file(char *filename) +static void do_config_file(const char *filename) { struct stat st; int fd; @@ -273,7 +286,7 @@ static void do_config_file(char *filename) fd = open(filename, O_RDONLY); if (fd < 0) { - fprintf(stderr, "fixdep: "); + fprintf(stderr, "fixdep: error opening config file: "); perror(filename); exit(2); } @@ -296,42 +309,89 @@ static void do_config_file(char *filename) close(fd); } +/* + * Important: The below generated source_foo.o and deps_foo.o variable + * assignments are parsed not only by make, but also by the rather simple + * parser in scripts/mod/sumversion.c. + */ static void parse_dep_file(void *map, size_t len) { char *m = map; char *end = m + len; char *p; char s[PATH_MAX]; - - p = strchr(m, ':'); - if (!p) { - fprintf(stderr, "fixdep: parse error\n"); - exit(1); - } - memcpy(s, m, p-m); s[p-m] = 0; - printf("deps_%s := \\\n", target); - m = p+1; + int is_target; + int saw_any_target = 0; + int is_first_dep = 0; clear_config(); while (m < end) { + /* Skip any "white space" */ while (m < end && (*m == ' ' || *m == '\\' || *m == '\n')) m++; + /* Find next "white space" */ p = m; - while (p < end && *p != ' ') p++; - if (p == end) { - do p--; while (!isalnum(*p)); + while (p < end && *p != ' ' && *p != '\\' && *p != '\n') p++; + /* Is the token we found a target name? */ + is_target = (*(p-1) == ':'); + /* Don't write any target names into the dependency file */ + if (is_target) { + /* The /next/ file is the first dependency */ + is_first_dep = 1; + } else { + /* Save this token/filename */ + memcpy(s, m, p-m); + s[p - m] = 0; + + /* Ignore certain dependencies */ + if (strrcmp(s, "include/generated/autoconf.h") && + strrcmp(s, "arch/um/include/uml-config.h") && + strrcmp(s, "include/linux/kconfig.h") && + strrcmp(s, ".ver")) { + /* + * Do not list the source file as dependency, + * so that kbuild is not confused if a .c file + * is rewritten into .S or vice versa. Storing + * it in source_* is needed for modpost to + * compute srcversions. + */ + if (is_first_dep) { + /* + * If processing the concatenation of + * multiple dependency files, only + * process the first target name, which + * will be the original source name, + * and ignore any other target names, + * which will be intermediate temporary + * files. + */ + if (!saw_any_target) { + saw_any_target = 1; + printf("source_%s := %s\n\n", + target, s); + printf("deps_%s := \\\n", + target); + } + is_first_dep = 0; + } else + printf(" %s \\\n", s); + do_config_file(s); + } } - memcpy(s, m, p-m); s[p-m] = 0; - if (strrcmp(s, "include/generated/autoconf.h") && - strrcmp(s, "arch/um/include/uml-config.h") && - strrcmp(s, ".ver")) { - printf(" %s \\\n", s); - do_config_file(s); - } + /* + * Start searching for next token immediately after the first + * "whitespace" character that follows this token. + */ m = p + 1; } + + if (!saw_any_target) { + fprintf(stderr, "fixdep: parse error; no targets found\n"); + exit(1); + } + printf("\n%s: $(deps_%s)\n\n", target, target); printf("$(deps_%s):\n", target); } @@ -344,11 +404,15 @@ static void print_deps(void) fd = open(depfile, O_RDONLY); if (fd < 0) { - fprintf(stderr, "fixdep: "); + fprintf(stderr, "fixdep: error opening depfile: "); perror(depfile); exit(2); } - fstat(fd, &st); + if (fstat(fd, &st) < 0) { + fprintf(stderr, "fixdep: error fstat'ing depfile: "); + perror(depfile); + exit(2); + } if (st.st_size == 0) { fprintf(stderr,"fixdep: %s is empty\n",depfile); close(fd); @@ -374,7 +438,7 @@ static void traps(void) int *p = (int *)test; if (*p != INT_CONF) { - fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n", + fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n", *p); exit(2); } diff --git a/scripts/imx/.gitignore b/scripts/imx/.gitignore new file mode 100644 index 0000000000..84e6f2b406 --- /dev/null +++ b/scripts/imx/.gitignore @@ -0,0 +1,2 @@ +imx-usb-loader +imx-image diff --git a/scripts/imx/Makefile b/scripts/imx/Makefile new file mode 100644 index 0000000000..be0b490108 --- /dev/null +++ b/scripts/imx/Makefile @@ -0,0 +1,10 @@ +hostprogs-$(CONFIG_ARCH_IMX_IMXIMAGE) += imx-image +hostprogs-$(CONFIG_ARCH_IMX_USBLOADER) += imx-usb-loader + +always := $(hostprogs-y) + +HOSTCFLAGS_imx-usb-loader.o = `pkg-config --cflags libusb-1.0` +HOSTLOADLIBES_imx-usb-loader = `pkg-config --libs libusb-1.0` + +imx-usb-loader-objs := imx-usb-loader.o +imx-image-objs := imx-image.o diff --git a/scripts/imx/README b/scripts/imx/README new file mode 100644 index 0000000000..0d6d0d03a8 --- /dev/null +++ b/scripts/imx/README @@ -0,0 +1,89 @@ +imx-usb-loader Tools + +The Freescale i.MX SoCs support bootstrapping from USB. These are host +side utilities handling this bootstrap process. + +The imx-usb-loader tool is used to upload and start i.MX images. These +are images containing a DCD (Device Configuration Data) table. To generate +these images from raw binaries use the imx-image tool. + +imx-image +--------- + +The imx-image tool can be used to generate imximages from raw binaries. +It requires an configuration file describing how to setup the SDRAM on +a particular board. This mainly consists of a poke table. The recognized +options in this file are: + +soc <soctype> soctype can be one of imx35, imx51, imx53, imx6 +loadaddr <adr> The address the binary is uploaded to +dcdofs <ofs> The offset of the image header in the image. This should be: + 0x400 - MMC/SD, NAND, serial ROM, PATA, SATA + 0x1000 - NOR Flash + 0x100 - OneNAND +wm 8 <adr> <value> do a byte memory write +wm 16 <adr> <value> do a short memory write +wm 32 <adr> <value> do a word memory write +check <width> <cond> <addr> <mask> Poll until condition becomes true. + with <cond> being one of: + while_all_bits_clear, + while_all_bits_set, + while_any_bit_clear, + while_any_bit_set + +the i.MX SoCs support a wide range of fancy things doing with the flash header. +We limit ourselves to a very simple case, that is the flash header has a fixed +size of 0x1000 bytes. The application is expected right thereafter, so if you +specify a loadaddr of 0x80000000 in the config file, the first 0x1000 bytes +are occupied by the flash header. The raw image inside the imximage will then +end up at 0x80001000 from where it is then executed. + +Example config file, suitable for an Eukra cpuimx35: + +soc imx35 +dcdofs 0x400 +loadaddr 0x80000000 +wm 32 0x53F80004 0x00821000 +wm 32 0x53F80004 0x00821000 +wm 32 0xb8001010 0x00000004 +wm 32 0xB8001010 0x0000000C +wm 32 0xb8001004 0x0009572B +wm 32 0xb8001000 0x92220000 +wm 8 0x80000400 0xda +wm 32 0xb8001000 0xa2220000 +wm 32 0x80000000 0x12344321 +wm 32 0x80000000 0x12344321 +wm 32 0xb8001000 0xb2220000 +wm 8 0x80000033 0xda +wm 8 0x82000000 0xda +wm 32 0xb8001000 0x82224080 +wm 32 0xb8001010 0x00000004 + +example call: + +imx-image -c cpuimx35.cfg -f raw.bin -o imximage.bin + +imx-usb-loader +-------------- + +This utility is used to upload an imximage to a board. Some bootloaders directly +generate this file format, with others you can generate such an image with the +imx-image tool. The only required argument is the image file to upload. imx-usb-loader +will then look for a supported device, upload the file and execute it. + +example usage: + +imx-usb-loader imximage.bin + +Some technical notes: The i.MX SoCs USB ROM boot mode supports doing register writes +and file uploads. The files are usually uploaded to SDRAM. For this to work the SDRAM +has to be initialized first. The information necessary to do this is contained in the +imximage itself, more exactly in the DCD table. The imx-usb-loader parses this table +and translates the DCD into register writes, basically it resembles what the i.MX would +do in ROM code when the same image would be loaded from another bootsource like SD/MMC +cards. Still the i.MX needs the DCD table to be uploaded. The i.MX would execute the DCD +data again, which would result in corrupting the just configured SDRAM. The imx-usb-loader +prevents this by setting the DCD length to 0x0 before uploading the image. +The i.MX Boot ROM supports different types of images to upload. The imx-usb-loader currently +only handles the simple case of uploading a single image which is executed right after +downloading. diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c new file mode 100644 index 0000000000..2f52015ae1 --- /dev/null +++ b/scripts/imx/imx-image.c @@ -0,0 +1,744 @@ +/* + * (C) Copyright 2013 Sascha Hauer, Pengutronix + * + * See file CREDITS for list of people who contributed to this + * project. + * + * 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. + * + */ +#include <stdio.h> +#include <unistd.h> +#include <getopt.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <endian.h> + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER) + +#define MAX_DCD 1024 + +static uint32_t image_load_addr; +static uint32_t image_dcd_offset; +static uint32_t dcdtable[MAX_DCD]; +static int curdcd; +static int header_version; +static int add_barebox_header; + +/* + * ============================================================================ + * i.MX flash header v1 handling. Found on i.MX35 and i.MX51 + * ============================================================================ + */ +struct imx_flash_header { + uint32_t app_code_jump_vector; + uint32_t app_code_barker; + uint32_t app_code_csf; + uint32_t dcd_ptr_ptr; + uint32_t super_root_key; + uint32_t dcd; + uint32_t app_dest; + uint32_t dcd_barker; + uint32_t dcd_block_len; +} __attribute__((packed)); + +#define FLASH_HEADER_OFFSET 0x400 +#define DCD_BARKER 0xb17219e9 + +static uint32_t bb_header[] = { + 0xea0003fe, /* b 0x1000 */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0xeafffffe, /* 1: b 1b */ + 0x65726162, /* 'bare' */ + 0x00786f62, /* 'box\0' */ + 0x00000000, + 0x00000000, + 0x55555555, + 0x55555555, + 0x55555555, + 0x55555555, + 0x55555555, + 0x55555555, + 0x55555555, + 0x55555555, +}; + +static int add_header_v1(void *buf, int offset, uint32_t loadaddr, uint32_t imagesize) +{ + struct imx_flash_header *hdr; + int dcdsize = curdcd * sizeof(uint32_t); + + if (add_barebox_header) + memcpy(buf, bb_header, sizeof(bb_header)); + + buf += offset; + hdr = buf; + + hdr->app_code_jump_vector = loadaddr + 0x1000; + hdr->app_code_barker = 0x000000b1; + hdr->app_code_csf = 0x0; + hdr->dcd_ptr_ptr = loadaddr + offset + offsetof(struct imx_flash_header, dcd); + hdr->super_root_key = 0x0; + hdr->dcd = loadaddr + offset + offsetof(struct imx_flash_header, dcd_barker); + hdr->app_dest = loadaddr; + hdr->dcd_barker = DCD_BARKER; + hdr->dcd_block_len = dcdsize; + + buf += sizeof(struct imx_flash_header); + + memcpy(buf, dcdtable, dcdsize); + + buf += dcdsize; + + *(uint32_t *)buf = imagesize; + + return 0; +} + +static int write_mem_v1(uint32_t addr, uint32_t val, int width) +{ + if (curdcd > MAX_DCD - 3) { + fprintf(stderr, "At maximum %d dcd entried are allowed\n", MAX_DCD); + return -ENOMEM; + } + + dcdtable[curdcd++] = width; + dcdtable[curdcd++] = addr; + dcdtable[curdcd++] = val; + + return 0; +} + +/* + * ============================================================================ + * i.MX flash header v2 handling. Found on i.MX53 and i.MX6 + * ============================================================================ + */ + +struct imx_boot_data { + uint32_t start; + uint32_t size; + uint32_t plugin; +} __attribute__((packed)); + +#define TAG_IVT_HEADER 0xd1 +#define IVT_VERSION 0x40 +#define TAG_DCD_HEADER 0xd2 +#define DCD_VERSION 0x40 +#define TAG_WRITE 0xcc +#define TAG_CHECK 0xcf + +struct imx_ivt_header { + uint8_t tag; + uint16_t length; + uint8_t version; +} __attribute__((packed)); + +struct imx_flash_header_v2 { + struct imx_ivt_header header; + + uint32_t entry; + uint32_t reserved1; + uint32_t dcd_ptr; + uint32_t boot_data_ptr; + uint32_t self; + uint32_t csf; + uint32_t reserved2; + + struct imx_boot_data boot_data; + struct imx_ivt_header dcd_header; +} __attribute__((packed)); + +static int add_header_v2(void *buf, int offset, uint32_t loadaddr, uint32_t imagesize) +{ + struct imx_flash_header_v2 *hdr; + int dcdsize = curdcd * sizeof(uint32_t); + + if (add_barebox_header) + memcpy(buf, bb_header, sizeof(bb_header)); + + buf += offset; + hdr = buf; + + hdr->header.tag = TAG_IVT_HEADER; + hdr->header.length = htobe16(32); + hdr->header.version = IVT_VERSION; + + hdr->entry = loadaddr + 0x1000; + hdr->dcd_ptr = loadaddr + 0x400 + offsetof(struct imx_flash_header_v2, dcd_header); + hdr->boot_data_ptr = loadaddr + 0x400 + offsetof(struct imx_flash_header_v2, boot_data); + hdr->self = loadaddr + 0x400; + + hdr->boot_data.start = loadaddr; + hdr->boot_data.size = imagesize; + + hdr->dcd_header.tag = TAG_DCD_HEADER; + hdr->dcd_header.length = htobe16(sizeof(uint32_t) + dcdsize); + hdr->dcd_header.version = DCD_VERSION; + + buf += sizeof(*hdr); + + memcpy(buf, dcdtable, dcdsize); + + return 0; +} + +static void usage(const char *prgname) +{ + fprintf(stderr, "usage: %s [OPTIONS]\n\n" + "-c <config> specify configuration file\n" + "-f <input> input image file\n" + "-o <output> output file\n" + "-b add barebox header to image. If used, barebox recognizes\n" + " the image as regular barebox image which can be used as\n" + " second stage image\n" + "-h this help\n", prgname); + exit(1); +} + +#define MAXARGS 5 + +static int parse_line(char *line, char *argv[]) +{ + int nargs = 0; + + while (nargs < MAXARGS) { + + /* skip any white space */ + while ((*line == ' ') || (*line == '\t')) + ++line; + + if (*line == '\0') /* end of line, no more args */ + argv[nargs] = NULL; + + if (*line == '\0') { /* end of line, no more args */ + argv[nargs] = NULL; + return nargs; + } + + argv[nargs++] = line; /* begin of argument string */ + + /* find end of string */ + while (*line && (*line != ' ') && (*line != '\t')) + ++line; + + if (*line == '\0') { /* end of line, no more args */ + argv[nargs] = NULL; + return nargs; + } + + *line++ = '\0'; /* terminate current arg */ + } + + printf("** Too many args (max. %d) **\n", MAXARGS); + + return nargs; +} + +struct command { + const char *name; + int (*parse)(int argc, char *argv[]); +}; + +static uint32_t last_cmd; +static int last_cmd_len; +static uint32_t *last_dcd; + +static void check_last_dcd(uint32_t cmd) +{ + if (last_dcd) { + if (last_cmd == cmd) { + return; + } else { + uint32_t l = be32toh(*last_dcd); + + l |= last_cmd_len << 8; + + *last_dcd = htobe32(l); + + last_dcd = NULL; + } + } + + if (!cmd) + return; + + if (!last_dcd) { + last_dcd = &dcdtable[curdcd++]; + *last_dcd = htobe32(cmd); + last_cmd_len = sizeof(uint32_t); + last_cmd = cmd; + } +} + +static int write_mem_v2(uint32_t addr, uint32_t val, int width) +{ + uint32_t cmd; + + cmd = (TAG_WRITE << 24) | width; + + if (curdcd > MAX_DCD - 3) { + fprintf(stderr, "At maximum %d dcd entried are allowed\n", MAX_DCD); + return -ENOMEM; + } + + check_last_dcd(cmd); + + last_cmd_len += sizeof(uint32_t) * 2; + dcdtable[curdcd++] = htobe32(addr); + dcdtable[curdcd++] = htobe32(val); + + return 0; +} + +static const char *check_cmds[] = { + "while_all_bits_clear", /* while ((*address & mask) == 0); */ + "while_all_bits_set" , /* while ((*address & mask) == mask); */ + "while_any_bit_clear", /* while ((*address & mask) != mask); */ + "while_any_bit_set", /* while ((*address & mask) != 0); */ +}; + +static void do_cmd_check_usage(void) +{ + fprintf(stderr, + "usage: check <width> <cmd> <addr> <mask>\n" + "<width> access width in bytes [1|2|4]\n" + "with <cmd> one of:\n" + "while_all_bits_clear: while ((*addr & mask) == 0)\n" + "while_all_bits_set: while ((*addr & mask) == mask)\n" + "while_any_bit_clear: while ((*addr & mask) != mask)\n" + "while_any_bit_set: while ((*addr & mask) != 0)\n"); +} + +static int do_cmd_check(int argc, char *argv[]) +{ + uint32_t addr, mask, cmd; + int i, width; + const char *scmd; + + if (argc < 5) { + do_cmd_check_usage(); + return -EINVAL; + } + + width = strtoul(argv[1], NULL, 0) >> 3; + scmd = argv[2]; + addr = strtoul(argv[3], NULL, 0); + mask = strtoul(argv[4], NULL, 0); + + switch (width) { + case 1: + case 2: + case 4: + break; + default: + fprintf(stderr, "illegal width %d\n", width); + return -EINVAL; + }; + + if (curdcd > MAX_DCD - 3) { + fprintf(stderr, "At maximum %d dcd entried are allowed\n", MAX_DCD); + return -ENOMEM; + } + + for (i = 0; i < ARRAY_SIZE(check_cmds); i++) { + if (!strcmp(scmd, check_cmds[i])) + break; + } + + if (i == ARRAY_SIZE(check_cmds)) { + do_cmd_check_usage(); + return -EINVAL; + } + + cmd = (TAG_CHECK << 24) | (i << 3) | width; + + check_last_dcd(cmd); + + last_cmd_len += sizeof(uint32_t) * 2; + dcdtable[curdcd++] = htobe32(addr); + dcdtable[curdcd++] = htobe32(mask); + + return 0; +} + +static int do_cmd_write_mem(int argc, char *argv[]) +{ + uint32_t addr, val, width; + + if (argc != 4) { + fprintf(stderr, "usage: wm [8|16|32] <addr> <val>\n"); + return -EINVAL; + } + + width = strtoul(argv[1], NULL, 0); + addr = strtoul(argv[2], NULL, 0); + val = strtoul(argv[3], NULL, 0); + + width >>= 3; + + switch (width) { + case 1: + case 2: + case 4: + break; + default: + fprintf(stderr, "illegal width %d\n", width); + return -EINVAL; + }; + + switch (header_version) { + case 1: + return write_mem_v1(addr, val, width); + case 2: + return write_mem_v2(addr, val, width); + default: + return -EINVAL; + } +} + +static int do_loadaddr(int argc, char *argv[]) +{ + if (argc < 2) + return -EINVAL; + + image_load_addr = strtoul(argv[1], NULL, 0); + + return 0; +} + +static int do_dcd_offset(int argc, char *argv[]) +{ + if (argc < 2) + return -EINVAL; + + image_dcd_offset = strtoul(argv[1], NULL, 0); + + return 0; +} + +struct soc_type { + char *name; + int header_version; +}; + +static struct soc_type socs[] = { + { .name = "imx35", .header_version = 1, }, + { .name = "imx51", .header_version = 1, }, + { .name = "imx53", .header_version = 2, }, + { .name = "imx6", .header_version = 2, }, +}; + +static int do_soc(int argc, char *argv[]) +{ + char *soc; + int i; + + if (argc < 2) + return -EINVAL; + + soc = argv[1]; + + for (i = 0; i < ARRAY_SIZE(socs); i++) { + if (!strcmp(socs[i].name, soc)) { + header_version = socs[i].header_version; + return 0; + } + } + + fprintf(stderr, "unkown SoC type \"%s\". Known SoCs are:\n", soc); + for (i = 0; i < ARRAY_SIZE(socs); i++) + fprintf(stderr, "%s ", socs[i].name); + fprintf(stderr, "\n"); + + return -EINVAL; +} + +struct command cmds[] = { + { + .name = "wm", + .parse = do_cmd_write_mem, + }, { + .name = "check", + .parse = do_cmd_check, + }, { + .name = "loadaddr", + .parse = do_loadaddr, + }, { + .name = "dcdofs", + .parse = do_dcd_offset, + }, { + .name = "soc", + .parse = do_soc, + }, +}; + +static int parse_config(const char *filename) +{ + FILE *f; + int lineno = 0; + char *line = NULL, *tmp; + size_t len; + char *argv[MAXARGS]; + int nargs, i, ret; + + f = fopen(filename, "r"); + if (!f) { + fprintf(stderr, "Error: %s - Can't open DCD file\n", filename); + exit(1); + } + + while ((getline(&line, &len, f)) > 0) { + lineno++; + + tmp = strchr(line, '#'); + if (tmp) + *tmp = 0; + tmp = strrchr(line, '\n'); + if (tmp) + *tmp = 0; + + nargs = parse_line(line, argv); + if (!nargs) + continue; + + ret = -ENOENT; + + for (i = 0; i < ARRAY_SIZE(cmds); i++) { + if (!strcmp(cmds[i].name, argv[0])) { + ret = cmds[i].parse(nargs, argv); + if (ret) { + fprintf(stderr, "error in line %d: %s\n", + lineno, strerror(-ret)); + return ret; + } + break; + } + } + + if (ret == -ENOENT) { + fprintf(stderr, "no such command: %s\n", argv[0]); + return ret; + } + } + + return 0; +} + +static int xread(int fd, void *buf, int len) +{ + int ret; + + while (len) { + ret = read(fd, buf, len); + if (ret < 0) + return ret; + if (!ret) + return EOF; + buf += ret; + len -= ret; + } + + return 0; +} + +static int xwrite(int fd, void *buf, int len) +{ + int ret; + + while (len) { + ret = write(fd, buf, len); + if (ret < 0) + return ret; + buf += ret; + len -= ret; + } + + return 0; +} + +static int write_dcd(const char *outfile) +{ + int outfd, ret; + int dcdsize = curdcd * sizeof(uint32_t); + + outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + if (outfd < 0) { + perror("open"); + exit(1); + } + + ret = xwrite(outfd, dcdtable, dcdsize); + if (ret < 0) { + perror("write"); + exit(1); + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + int opt, ret; + char *configfile = NULL; + char *imagename = NULL; + char *outfile = NULL; + void *buf; + size_t image_size = 0; + struct stat s; + int infd, outfd; + int dcd_only = 0; + + while ((opt = getopt(argc, argv, "c:hf:o:bd")) != -1) { + switch (opt) { + case 'c': + configfile = optarg; + break; + case 'f': + imagename = optarg; + break; + case 'o': + outfile = optarg; + break; + case 'b': + add_barebox_header = 1; + break; + case 'd': + dcd_only = 1; + break; + case 'h': + usage(argv[0]); + default: + exit(1); + } + } + + if (!imagename && !dcd_only) { + fprintf(stderr, "image name not given\n"); + exit(1); + } + + if (!configfile) { + fprintf(stderr, "config file not given\n"); + exit(1); + } + + if (!outfile) { + fprintf(stderr, "output file not given\n"); + exit(1); + } + + if (!dcd_only) { + ret = stat(imagename, &s); + if (ret) { + perror("stat"); + exit(1); + } + + image_size = s.st_size; + } + + ret = parse_config(configfile); + if (ret) + exit(1); + + buf = calloc(4096, 1); + if (!buf) + exit(1); + + if (!image_dcd_offset) { + fprintf(stderr, "no dcd offset given ('dcdofs'). Defaulting to 0x400\n"); + image_dcd_offset = 0x400; + } + + if (!header_version) { + fprintf(stderr, "no SoC given. (missing 'soc' in config)\n"); + exit(1); + } + + if (header_version == 2) + check_last_dcd(0); + + if (dcd_only) { + ret = write_dcd(outfile); + if (ret) + exit(1); + exit (0); + } + + switch (header_version) { + case 1: + add_header_v1(buf, image_dcd_offset, image_load_addr, image_size + 0x1000); + break; + case 2: + add_header_v2(buf, image_dcd_offset, image_load_addr, image_size + 0x1000); + break; + default: + fprintf(stderr, "Congratulations! You're welcome to implement header version %d\n", + header_version); + exit(1); + } + + outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + if (outfd < 0) { + perror("open"); + exit(1); + } + + ret = xwrite(outfd, buf, 4096); + if (ret < 0) { + perror("write"); + exit(1); + } + + infd = open(imagename, O_RDONLY); + if (infd < 0) { + perror("open"); + exit(1); + } + + while (image_size) { + int now = image_size < 4096 ? image_size : 4096; + + ret = xread(infd, buf, now); + if (ret) { + perror("read"); + exit(1); + } + + ret = xwrite(outfd, buf, now); + if (ret) { + perror("write"); + exit(1); + } + + image_size -= now; + } + + ret = close(outfd); + if (ret) { + perror("close"); + exit(1); + } + + exit(0); +} diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c new file mode 100644 index 0000000000..d58b1da3bd --- /dev/null +++ b/scripts/imx/imx-usb-loader.c @@ -0,0 +1,1427 @@ +/* + * imx_usb: + * + * Program to download and execute an image over the USB boot protocol + * on i.MX series processors. + * + * Code originally written by Eric Nelson. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <stdio.h> +#include <sys/types.h> +#include <time.h> + +#include <unistd.h> +#include <ctype.h> +#include <sys/io.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <libusb.h> +#include <getopt.h> + +#define get_min(a, b) (((a) < (b)) ? (a) : (b)) + +int verbose; + +struct mach_id { + struct mach_id * next; + unsigned short vid; + unsigned short pid; + char file_name[256]; + char *name; +#define MODE_HID 0 +#define MODE_BULK 1 + unsigned char mode; +#define HDR_NONE 0 +#define HDR_MX51 1 +#define HDR_MX53 2 + unsigned char header_type; + unsigned short max_transfer; +}; + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#ifndef offsetof +#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER) +#endif + +struct usb_work { + char filename[256]; + unsigned char dcd; + unsigned char clear_dcd; + unsigned char plug; +#define J_ADDR 1 +#define J_HEADER 2 +#define J_HEADER2 3 + unsigned char jump_mode; + unsigned jump_addr; +}; + +struct usb_id { + struct mach_id *mach_id; + struct usb_work *work; +}; + +struct mach_id imx_ids[] = { + { + .vid = 0x066f, + .pid = 0x3780, + .name = "i.MX23", + .mode = MODE_BULK, + }, { + .vid = 0x15a2, + .pid = 0x004f, + .name = "i.MX28", + }, { + .vid = 0x15a2, + .pid = 0x0052, + .name = "i.MX50", + }, { + .vid = 0x15a2, + .pid = 0x0054, + .name = "i.MX6", + .header_type = HDR_MX53, + .mode = MODE_HID, + .max_transfer = 1024, + }, { + .vid = 0x15a2, + .pid = 0x0041, + .name = "i.MX51", + .header_type = HDR_MX51, + .mode = MODE_BULK, + .max_transfer = 64, + }, { + .vid = 0x15a2, + .pid = 0x004e, + .name = "i.MX53", + .header_type = HDR_MX53, + .mode = MODE_BULK, + .max_transfer = 512, + }, { + .vid = 0x15a2, + .pid = 0x0030, + .name = "i.MX35", + .header_type = HDR_MX51, + .mode = MODE_BULK, + .max_transfer = 64, + }, +}; + +static struct mach_id *imx_device(unsigned short vid, unsigned short pid) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(imx_ids); i++) { + struct mach_id *id = &imx_ids[i]; + if (id->vid == vid && id->pid == pid) { + fprintf(stderr, "found %s USB device [%04x:%04x]\n", + id->name, vid, pid); + return id; + } + } + + return NULL; +} + +static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id) +{ + int i = 0; + struct mach_id *p; + + for (;;) { + struct libusb_device_descriptor desc; + int r; + + libusb_device *dev = devs[i++]; + if (!dev) + break; + + r = libusb_get_device_descriptor(dev, &desc); + if (r < 0) { + fprintf(stderr, "failed to get device descriptor"); + return NULL; + } + + p = imx_device(desc.idVendor, desc.idProduct); + if (p) { + *pp_id = p; + return dev; + } + } + *pp_id = NULL; + + return NULL; +} + +static void dump_long(unsigned char *src, unsigned cnt, unsigned addr) +{ + unsigned *p = (unsigned *)src; + while (cnt >= 32) { + printf("%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n", + addr, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); + p += 8; + cnt -= 32; + addr += 32; + } + if (cnt) { + printf("%08x:", addr); + while (cnt >= 4) { + printf(" %08x", p[0]); + p++; + cnt -= 4; + } + printf("\n"); + } +} + +static void dump_bytes(unsigned char *src, unsigned cnt, unsigned addr) +{ + unsigned char *p = src; + int i; + + while (cnt >= 16) { + printf("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", + addr, + p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], + p[11], p[12], p[13], p[14], p[15]); + p += 16; + cnt -= 16; + addr += 16; + } + + if (cnt) { + printf("%08x:", addr); + i = 0; + while (cnt) { + printf(" %02x", p[0]); + p++; + cnt--; + i++; + if (cnt) if (i == 4) { + i = 0; + printf(" "); + } + } + printf("\n"); + } +} + +static long get_file_size(FILE *xfile) +{ + long size; + fseek(xfile, 0, SEEK_END); + size = ftell(xfile); + rewind(xfile); + + return size; +} + +/* + * HID Class-Specific Requests values. See section 7.2 of the HID specifications + */ +#define HID_GET_REPORT 0x01 +#define HID_GET_IDLE 0x02 +#define HID_GET_PROTOCOL 0x03 +#define HID_SET_REPORT 0x09 +#define HID_SET_IDLE 0x0A +#define HID_SET_PROTOCOL 0x0B +#define HID_REPORT_TYPE_INPUT 0x01 +#define HID_REPORT_TYPE_OUTPUT 0x02 +#define HID_REPORT_TYPE_FEATURE 0x03 +#define CTRL_IN LIBUSB_ENDPOINT_IN |LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE +#define CTRL_OUT LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE + +#define EP_IN 0x80 + +/* + * For HID class drivers, 4 reports are used to implement + * Serial Download protocol(SDP) + * Report 1 (control out endpoint) 16 byte SDP comand + * (total of 17 bytes with 1st byte report id of 0x01 + * Report 2 (control out endpoint) data associated with report 1 commands + * (max size of 1025 with 1st byte of 0x02) + * Report 3 (interrupt in endpoint) HAB security state + * (max size of 5 bytes with 1st byte of 0x03) + * (0x12343412 production) + * (0x56787856 engineering) + * Report 4 (interrupt in endpoint) date associated with report 1 commands + * (max size of 65 bytes with 1st byte of 0x04) + * + */ +/* + * For Bulk class drivers, the device is configured as + * EP0IN, EP0OUT control transfer + * EP1OUT - bulk out + * (max packet size of 512 bytes) + * EP2IN - bulk in + * (max packet size of 512 bytes) + */ +static int transfer(struct libusb_device_handle *h, int report, unsigned char *p, unsigned cnt, + int* last_trans, struct usb_id *p_id) +{ + int err; + if (cnt > p_id->mach_id->max_transfer) + cnt = p_id->mach_id->max_transfer; + + if (verbose > 4) { + printf("report=%i\n", report); + if (report < 3) + dump_bytes(p, cnt, 0); + } + + if (p_id->mach_id->mode == MODE_BULK) { + *last_trans = 0; + err = libusb_bulk_transfer(h, (report < 3) ? 1 : 2 + EP_IN, p, cnt, last_trans, 1000); + } else { + unsigned char tmp[1028]; + + tmp[0] = (unsigned char)report; + + if (report < 3) { + memcpy(&tmp[1], p, cnt); + err = libusb_control_transfer(h, + CTRL_OUT, + HID_SET_REPORT, + (HID_REPORT_TYPE_OUTPUT << 8) | report, + 0, + tmp, cnt + 1, 1000); + *last_trans = (err > 0) ? err - 1 : 0; + if (err > 0) + err = 0; + } else { + *last_trans = 0; + memset(&tmp[1], 0, cnt); + err = libusb_interrupt_transfer(h, 1 + EP_IN, tmp, cnt + 1, last_trans, 1000); + if (err >= 0) { + if (tmp[0] == (unsigned char)report) { + if (*last_trans > 1) { + *last_trans -= 1; + memcpy(p, &tmp[1], *last_trans); + } + } else { + printf("Unexpected report %i err=%i, cnt=%i, last_trans=%i, %02x %02x %02x %02x\n", + tmp[0], err, cnt, *last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + err = 0; + } + } + } + } + + if (verbose > 4 && report >= 3) + dump_bytes(p, cnt, 0); + + return err; +} + +int do_status(libusb_device_handle *h, struct usb_id *p_id) +{ + int last_trans; + unsigned char tmp[64]; + int retry = 0; + int err; + const unsigned char status_command[] = { + 5, 5, 0, 0, 0, 0, + 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0 + }; + + for (;;) { + err = transfer(h, 1, (unsigned char*)status_command, 16, &last_trans, p_id); + + if (verbose > 2) + printf("report 1, wrote %i bytes, err=%i\n", last_trans, err); + + memset(tmp, 0, sizeof(tmp)); + + err = transfer(h, 3, tmp, 64, &last_trans, p_id); + + if (verbose > 2) { + printf("report 3, read %i bytes, err=%i\n", last_trans, err); + printf("read=%02x %02x %02x %02x\n", tmp[0], tmp[1], tmp[2], tmp[3]); + } + + if (!err) + break; + + retry++; + + if (retry > 5) + break; + } + + if (p_id->mach_id->mode == MODE_HID) { + err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + if (err) + printf("4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + } + + return err; +} + +struct boot_data { + uint32_t dest; + uint32_t image_len; + uint32_t plugin; +}; + +struct imx_flash_header_v2 { +#define IVT_BARKER 0x402000d1 + uint32_t barker; + uint32_t start_addr; + uint32_t reserv1; + uint32_t dcd_ptr; + uint32_t boot_data_ptr; /* struct boot_data * */ + uint32_t self_ptr; /* struct imx_flash_header_v2 *, this - boot_data.start = offset linked at */ + uint32_t app_code_csf; + uint32_t reserv2; +}; + +/* + * MX51 header type + */ +struct imx_flash_header_v1 { + uint32_t app_start_addr; +#define APP_BARKER 0xb1 +#define DCD_BARKER 0xb17219e9 + uint32_t app_barker; + uint32_t csf_ptr; + uint32_t dcd_ptr_ptr; + uint32_t srk_ptr; + uint32_t dcd_ptr; + uint32_t app_dest_ptr; +}; + +#define V(a) (((a) >> 24) & 0xff), (((a) >> 16) & 0xff), (((a) >> 8) & 0xff), ((a) & 0xff) + +static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, + unsigned addr, unsigned char *dest, unsigned cnt) +{ + static unsigned char read_reg_command[] = { + 1, + 1, + V(0), /* address */ + 0x20, /* format */ + V(0x00000004), /* data count */ + V(0), /* data */ + 0x00, /* type */ + }; + + int retry = 0; + int last_trans; + int err; + int rem; + unsigned char tmp[64]; + read_reg_command[2] = (unsigned char)(addr >> 24); + read_reg_command[3] = (unsigned char)(addr >> 16); + read_reg_command[4] = (unsigned char)(addr >> 8); + read_reg_command[5] = (unsigned char)(addr); + + read_reg_command[7] = (unsigned char)(cnt >> 24); + read_reg_command[8] = (unsigned char)(cnt >> 16); + read_reg_command[9] = (unsigned char)(cnt >> 8); + read_reg_command[10] = (unsigned char)(cnt); + + for (;;) { + err = transfer(h, 1, read_reg_command, 16, &last_trans, p_id); + if (!err) + break; + printf("read_reg_command err=%i, last_trans=%i\n", err, last_trans); + if (retry > 5) { + return -4; + } + retry++; + } + + err = transfer(h, 3, tmp, 4, &last_trans, p_id); + if (err) { + printf("r3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + return err; + } + + rem = cnt; + + while (rem) { + tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; + err = transfer(h, 4, tmp, 64, &last_trans, p_id); + if (err) { + printf("r4 in err=%i, last_trans=%i %02x %02x %02x %02x cnt=%d rem=%d\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem); + break; + } + if ((last_trans > rem) || (last_trans > 64)) { + if ((last_trans == 64) && (cnt == rem)) { + /* Last transfer is expected to be too large for HID */ + } else { + printf("err: %02x %02x %02x %02x cnt=%d rem=%d last_trans=%i\n", + tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem, last_trans); + } + last_trans = rem; + if (last_trans > 64) + last_trans = 64; + } + memcpy(dest, tmp, last_trans); + dest += last_trans; + rem -= last_trans; + } + return err; +} + +static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id, + unsigned addr, unsigned val, int width) +{ + int retry = 0; + int last_trans; + int err = 0; + unsigned char tmp[64]; + unsigned char ds; + unsigned char write_reg_command[] = { + 2, + 2, + V(0), /* address */ + 0x0, /* format */ + V(0x00000004), /* data count */ + V(0), /* data */ + 0x00, /* type */ + }; + write_reg_command[2] = (unsigned char)(addr >> 24); + write_reg_command[3] = (unsigned char)(addr >> 16); + write_reg_command[4] = (unsigned char)(addr >> 8); + write_reg_command[5] = (unsigned char)(addr); + + switch (width) { + case 1: + ds = 0x8; + break; + case 2: + ds = 0x10; + break; + case 4: + ds = 0x20; + break; + default: + return -1; + } + + write_reg_command[6] = ds; + + write_reg_command[11] = (unsigned char)(val >> 24); + write_reg_command[12] = (unsigned char)(val >> 16); + write_reg_command[13] = (unsigned char)(val >> 8); + write_reg_command[14] = (unsigned char)(val); + + for (;;) { + err = transfer(h, 1, write_reg_command, 16, &last_trans, p_id); + if (!err) + break; + printf("write_reg_command err=%i, last_trans=%i\n", err, last_trans); + if (retry > 5) { + return -4; + } + retry++; + } + + memset(tmp, 0, sizeof(tmp)); + + err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + if (err) { + printf("w3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + printf("addr=0x%08x, val=0x%08x\n", addr, val); + } + + memset(tmp, 0, sizeof(tmp)); + + err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + if (err) + printf("w4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + return err; +} + +static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_id, + struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt) +{ + unsigned char *dcd_end; + unsigned m_length; +#define cvt_dest_to_src (((unsigned char *)hdr) - hdr->self_ptr) + unsigned char* dcd; + unsigned char* file_end = file_start + cnt; + int err = 0; + + if (!hdr->dcd_ptr) { + printf("No dcd table, barker=%x\n", hdr->barker); + return 0; /* nothing to do */ + } + + dcd = hdr->dcd_ptr + cvt_dest_to_src; + + if ((dcd < file_start) || ((dcd + 4) > file_end)) { + printf("bad dcd_ptr %08x\n", hdr->dcd_ptr); + return -1; + } + + m_length = (dcd[1] << 8) + dcd[2]; + + printf("main dcd length %x\n", m_length); + + if ((dcd[0] != 0xd2) || (dcd[3] != 0x40)) { + printf("Unknown tag\n"); + return -1; + } + + dcd_end = dcd + m_length; + + if (dcd_end > file_end) { + printf("bad dcd length %08x\n", m_length); + return -1; + } + dcd += 4; + + while (dcd < dcd_end) { + unsigned s_length = (dcd[1] << 8) + dcd[2]; + unsigned char *s_end = dcd + s_length; + + printf("sub dcd length %x\n", s_length); + + if ((dcd[0] != 0xcc) || (dcd[3] != 0x04)) { + printf("Unknown sub tag\n"); + return -1; + } + dcd += 4; + + if (s_end > dcd_end) { + printf("error s_end(%p) > dcd_end(%p)\n", s_end, dcd_end); + return -1; + } + + while (dcd < s_end) { + unsigned addr = (dcd[0] << 24) | (dcd[1] << 16) | (dcd[2] << 8) | dcd[3]; + unsigned val = (dcd[4] << 24) | (dcd[5] << 16) | (dcd[6] << 8) | dcd[7]; + + dcd += 8; + err = write_memory(h, p_id, addr, val, 4); + if (err < 0) + return err; + } + } + return err; +} + +static int get_dcd_range_old(struct imx_flash_header_v1 *hdr, + unsigned char *file_start, unsigned cnt, + unsigned char **pstart, unsigned char **pend) +{ + unsigned char *dcd_end; + unsigned m_length; +#define cvt_dest_to_src_old (((unsigned char *)&hdr->dcd_ptr) - hdr->dcd_ptr_ptr) + unsigned char* dcd; + unsigned val; + unsigned char* file_end = file_start + cnt; + + if (!hdr->dcd_ptr) { + printf("No dcd table, barker=%x\n", hdr->app_barker); + *pstart = *pend = ((unsigned char *)hdr) + sizeof(struct imx_flash_header_v1); + return 0; /* nothing to do */ + } + + dcd = hdr->dcd_ptr + cvt_dest_to_src_old; + + if ((dcd < file_start) || ((dcd + 8) > file_end)) { + printf("bad dcd_ptr %08x\n", hdr->dcd_ptr); + return -1; + } + + val = (dcd[0] << 0) | (dcd[1] << 8) | (dcd[2] << 16) | (dcd[3] << 24); + + if (val != DCD_BARKER) { + printf("Unknown tag\n"); + return -1; + } + + dcd += 4; + m_length = (dcd[0] << 0) | (dcd[1] << 8) | (dcd[2] << 16) | (dcd[3] << 24); + dcd += 4; + dcd_end = dcd + m_length; + + if (dcd_end > file_end) { + printf("bad dcd length %08x\n", m_length); + return -1; + } + + *pstart = dcd; + *pend = dcd_end; + + return 0; +} + +static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_id, + struct imx_flash_header_v1 *hdr, unsigned char *file_start, unsigned cnt) +{ + unsigned val; + unsigned char *dcd_end; + unsigned char* dcd; + int err = get_dcd_range_old(hdr, file_start, cnt, &dcd, &dcd_end); + if (err < 0) + return err; + + printf("writing DCD table...\n"); + + while (dcd < dcd_end) { + unsigned type = (dcd[0] << 0) | (dcd[1] << 8) | (dcd[2] << 16) | (dcd[3] << 24); + unsigned addr = (dcd[4] << 0) | (dcd[5] << 8) | (dcd[6] << 16) | (dcd[7] << 24); + val = (dcd[8] << 0) | (dcd[9] << 8) | (dcd[10] << 16) | (dcd[11] << 24); + dcd += 12; + + switch (type) { + case 1: + if (verbose > 1) + printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val); + err = write_memory(h, p_id, addr, val, 1); + if (err < 0) + return err; + break; + case 4: + if (verbose > 1) + printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val); + err = write_memory(h, p_id, addr, val, 4); + if (err < 0) + return err; + break; + default: + printf("!!!unknown type=%08x *0x%08x = 0x%08x\n", type, addr, val); + } + } + + if (err) + fprintf(stderr, "writing DCD table failed with %d\n", err); + else + printf("DCD table successfully written\n"); + + return err; +} + +static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id, + FILE *xfile, unsigned offset, unsigned addr, unsigned size, + unsigned char *verify_buffer, unsigned verify_cnt) +{ + int mismatch = 0; + unsigned char file_buf[1024]; + fseek(xfile, offset + verify_cnt, SEEK_SET); + + while (size) { + unsigned char mem_buf[64]; + unsigned char *p = file_buf; + int cnt = addr & 0x3f; + int request = get_min(size, sizeof(file_buf)); + + if (cnt) { + cnt = 64 - cnt; + if (request > cnt) + request = cnt; + } + + if (verify_cnt) { + p = verify_buffer; + cnt = get_min(request, verify_cnt); + verify_buffer += cnt; + verify_cnt -= cnt; + } else { + cnt = fread(p, 1, request, xfile); + if (cnt <= 0) { + printf("Unexpected end of file, request=0x%0x, size=0x%x, cnt=%i\n", + request, size, cnt); + return -1; + } + } + + size -= cnt; + + while (cnt) { + int ret; + + request = get_min(cnt, sizeof(mem_buf)); + + ret = read_memory(h, p_id, addr, mem_buf, request); + if (ret < 0) + return ret; + + if (memcmp(p, mem_buf, request)) { + unsigned char * m = mem_buf; + if (!mismatch) + printf("!!!!mismatch\n"); + mismatch++; + + while (request) { + unsigned req = get_min(request, 32); + if (memcmp(p, m, req)) { + dump_long(p, req, offset); + dump_long(m, req, addr); + printf("\n"); + } + p += req; + m+= req; + offset += req; + addr += req; + cnt -= req; + request -= req; + } + if (mismatch >= 5) + return -1; + } + p += request; + offset += request; + addr += request; + cnt -= request; + } + } + + return mismatch ? -1 : 0; +} + +static int is_header(struct usb_id *p_id, unsigned char *p) +{ + struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p; + struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; + + switch (p_id->mach_id->header_type) { + case HDR_MX51: + if (ohdr->app_barker == 0xb1) + return 1; + break; + case HDR_MX53: + if (hdr->barker == IVT_BARKER) + return 1; + } + + return 0; +} + +static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsigned char *p, + unsigned char *file_start, unsigned cnt) +{ + struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p; + struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; + int ret = 0; + + switch (p_id->mach_id->header_type) { + case HDR_MX51: + ret = write_dcd_table_old(h, p_id, ohdr, file_start, cnt); + ohdr->dcd_ptr = 0; + + break; + case HDR_MX53: + ret = write_dcd_table_ivt(h, p_id, hdr, file_start, cnt); + hdr->dcd_ptr = 0; + + break; + } + + return ret; +} + +static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id, + unsigned char *p, unsigned char *file_start, unsigned cnt) +{ + struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p; + struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; + + switch (p_id->mach_id->header_type) { + case HDR_MX51: + printf("clear dcd_ptr=0x%08x\n", ohdr->dcd_ptr); + ohdr->dcd_ptr = 0; + break; + case HDR_MX53: + printf("clear dcd_ptr=0x%08x\n", hdr->dcd_ptr); + hdr->dcd_ptr = 0; + break; + } + return 0; +} + +static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *file_start, + unsigned cnt, unsigned *dladdr, unsigned *max_length, unsigned *plugin, + unsigned *header_addr) +{ + unsigned char* file_end = file_start + cnt; + switch (p_id->mach_id->header_type) { + case HDR_MX51: + { + struct imx_flash_header_v1 *ohdr = (struct imx_flash_header_v1 *)p; + unsigned char *dcd_end; + unsigned char* dcd; + int err = get_dcd_range_old(ohdr, file_start, cnt, &dcd, &dcd_end); + + *dladdr = ohdr->app_dest_ptr; + *header_addr = ohdr->dcd_ptr_ptr - offsetof(struct imx_flash_header_v1, dcd_ptr); + *plugin = 0; + if (err >= 0) + *max_length = dcd_end[0] | (dcd_end[1] << 8) | (dcd_end[2] << 16) | (dcd_end[3] << 24); + + break; + } + case HDR_MX53: + { + unsigned char *bd; + struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; + + *dladdr = hdr->self_ptr; + *header_addr = hdr->self_ptr; + bd = hdr->boot_data_ptr + cvt_dest_to_src; + if ((bd < file_start) || ((bd + 4) > file_end)) { + printf("bad boot_data_ptr %08x\n", hdr->boot_data_ptr); + return -1; + } + + *dladdr = ((struct boot_data *)bd)->dest; + *max_length = ((struct boot_data *)bd)->image_len; + *plugin = ((struct boot_data *)bd)->plugin; + ((struct boot_data *)bd)->plugin = 0; + + hdr->boot_data_ptr = 0; + + break; + } + } + return 0; +} + +static int process_header(struct libusb_device_handle *h, struct usb_id *p_id, + struct usb_work *curr, unsigned char *buf, int cnt, + unsigned *p_dladdr, unsigned *p_max_length, unsigned *p_plugin, + unsigned *p_header_addr) +{ + int ret; + unsigned header_max = 0x800; + unsigned header_inc = 0x400; + unsigned header_offset = 0; + int header_cnt = 0; + unsigned char *p = buf; + + for (header_offset = 0; header_offset < header_max; header_offset += header_inc, p += header_inc) { + + if (!is_header(p_id, p)) + continue; + + ret = get_dl_start(p_id, p, buf, cnt, p_dladdr, p_max_length, p_plugin, p_header_addr); + if (ret < 0) { + printf("!!get_dl_start returned %i\n", ret); + return ret; + } + + if (curr->dcd) { + ret = perform_dcd(h, p_id, p, buf, cnt); + if (ret < 0) { + printf("!!perform_dcd returned %i\n", ret); + return ret; + } + curr->dcd = 0; + if ((!curr->jump_mode) && (!curr->plug)) { + printf("!!dcd done, nothing else requested\n"); + return 0; + } + } + + if (curr->clear_dcd) { + ret = clear_dcd_ptr(h, p_id, p, buf, cnt); + if (ret < 0) { + printf("!!clear_dcd returned %i\n", ret); + return ret; + } + } + + if (*p_plugin && (!curr->plug) && (!header_cnt)) { + header_cnt++; + header_max = header_offset + *p_max_length + 0x400; + if (header_max > cnt - 32) + header_max = cnt - 32; + printf("header_max=%x\n", header_max); + header_inc = 4; + } else { + if (!*p_plugin) + curr->plug = 0; + return header_offset; + } + } + + fprintf(stderr, "no DCD header found in image, run imx-image first\n"); + + return -ENODEV; +} + +static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, + unsigned char *p, int cnt, unsigned char *buf, unsigned buf_cnt, + unsigned dladdr, unsigned fsize, unsigned char type, FILE* xfile) +{ + static unsigned char dl_command[] = { + 0x04, + 0x04, + V(0), /* address */ + 0x00, /* format */ + V(0x00000020), /* data count */ + V(0), /* data */ + 0xaa, /* type */ + }; + int last_trans, err; + int retry = 0; + unsigned transfer_size = 0; + int max = p_id->mach_id->max_transfer; + unsigned char tmp[64]; + + dl_command[2] = (unsigned char)(dladdr >> 24); + dl_command[3] = (unsigned char)(dladdr >> 16); + dl_command[4] = (unsigned char)(dladdr >> 8); + dl_command[5] = (unsigned char)(dladdr); + + dl_command[7] = (unsigned char)(fsize >> 24); + dl_command[8] = (unsigned char)(fsize >> 16); + dl_command[9] = (unsigned char)(fsize >> 8); + dl_command[10] = (unsigned char)(fsize); + dl_command[15] = type; + + for (;;) { + err = transfer(h, 1, dl_command, 16, &last_trans, p_id); + if (!err) + break; + + printf("dl_command err=%i, last_trans=%i\n", err, last_trans); + + if (retry > 5) + return -4; + retry++; + } + + retry = 0; + + if (p_id->mach_id->mode == MODE_BULK) { + err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + if (err) + printf("in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + } + + while (1) { + int retry; + + if (cnt > (int)(fsize - transfer_size)) + cnt = (fsize - transfer_size); + + if (cnt <= 0) + break; + + retry = 0; + + while (cnt) { + err = transfer(h, 2, p, get_min(cnt, max), &last_trans, p_id); + if (err) { + printf("out err=%i, last_trans=%i cnt=0x%x max=0x%x transfer_size=0x%X retry=%i\n", + err, last_trans, cnt, max, transfer_size, retry); + if (retry >= 10) { + printf("Giving up\n"); + return err; + } + if (max >= 16) + max >>= 1; + else + max <<= 1; + usleep(10000); + retry++; + continue; + } + max = p_id->mach_id->max_transfer; + retry = 0; + if (cnt < last_trans) { + printf("error: last_trans=0x%x, attempted only=0%x\n", last_trans, cnt); + cnt = last_trans; + } + if (!last_trans) { + printf("Nothing last_trans, err=%i\n", err); + break; + } + p += last_trans; + cnt -= last_trans; + transfer_size += last_trans; + } + + if (!last_trans) + break; + + if (feof(xfile)) + break; + + cnt = fsize - transfer_size; + if (cnt <= 0) + break; + + cnt = fread(buf, 1 , get_min(cnt, buf_cnt), xfile); + p = buf; + } + + if (p_id->mach_id->mode == MODE_HID) { + err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + if (err) + printf("3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + if (err) + printf("4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + } else { + do_status(h, p_id); + } + + return transfer_size; +} + +#define FT_APP 0xaa +#define FT_CSF 0xcc +#define FT_DCD 0xee +#define FT_LOAD_ONLY 0x00 + +static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, + struct usb_work *curr, int verify) +{ + static unsigned char jump_command[] = {0x0b,0x0b, V(0), 0x00, V(0x00000000), V(0), 0x00}; + + int ret; + FILE* xfile; + unsigned char type; + unsigned fsize; + unsigned header_offset; + int cnt; + unsigned file_base; + int last_trans, err; +#define BUF_SIZE (1024*16) + unsigned char *buf = NULL; + unsigned char *verify_buffer = NULL; + unsigned verify_cnt; + unsigned char *p; + unsigned char tmp[64]; + unsigned dladdr = 0; + unsigned max_length; + unsigned plugin = 0; + unsigned header_addr = 0; + + unsigned skip = 0; + unsigned transfer_size=0; + int retry = 0; + + xfile = fopen(curr->filename, "rb" ); + if (!xfile) { + printf("error, can not open input file: %s\n", curr->filename); + return -5; + } + + buf = malloc(BUF_SIZE); + if (!buf) { + printf("error, out of memory\n"); + ret = -2; + goto cleanup; + } + + fsize = get_file_size(xfile); + + cnt = fread(buf, 1 , BUF_SIZE, xfile); + + if (cnt < 0x20) { + printf("error, file: %s is too small\n", curr->filename); + ret = -2; + goto cleanup; + } + + max_length = fsize; + + ret = process_header(h, p_id, curr, buf, cnt, + &dladdr, &max_length, &plugin, &header_addr); + if (ret < 0) + goto cleanup; + + header_offset = ret; + + if ((!curr->jump_mode) && (!curr->plug)) { + /* nothing else requested */ + ret = 0; + goto cleanup; + } + + if (plugin && (!curr->plug)) { + printf("Only plugin header found\n"); + ret = -1; + goto cleanup; + } + + if (!dladdr) { + printf("unknown load address\n"); + ret = -3; + goto cleanup; + } + + file_base = header_addr - header_offset; + + type = (curr->plug || curr->jump_mode) ? FT_APP : FT_LOAD_ONLY; + + if (p_id->mach_id->mode == MODE_BULK && type == FT_APP) { + /* No jump command, dladdr should point to header */ + dladdr = header_addr; + } + + if (file_base > dladdr) { + max_length -= (file_base - dladdr); + dladdr = file_base; + } + + skip = dladdr - file_base; + + if (skip > cnt) { + if (skip > fsize) { + printf("skip(0x%08x) > fsize(0x%08x) file_base=0x%08x, header_offset=0x%x\n", + skip, fsize, file_base, header_offset); + ret = -4; + goto cleanup; + } + + fseek(xfile, skip, SEEK_SET); + cnt -= skip; + fsize -= skip; + skip = 0; + cnt = fread(buf, 1 , BUF_SIZE, xfile); + } + + p = &buf[skip]; + cnt -= skip; + fsize -= skip; + + if (fsize > max_length) + fsize = max_length; + + if (verify) { + /* + * we need to save header for verification + * because some of the file is changed + * before download + */ + verify_buffer = malloc(cnt); + verify_cnt = cnt; + + if (!verify_buffer) { + printf("error, out of memory\n"); + ret = -2; + goto cleanup; + } + + memcpy(verify_buffer, p, cnt); + + if ((type == FT_APP) && (p_id->mach_id->mode != MODE_HID)) { + type = FT_LOAD_ONLY; + verify = 2; + } + } + + printf("loading binary file(%s) to %08x, skip=0x%x, fsize=%d type=%d...\n", + curr->filename, dladdr, skip, fsize, type); + + ret = load_file(h, p_id, p, cnt, buf, BUF_SIZE, + dladdr, fsize, type, xfile); + if (ret < 0) + goto cleanup; + + printf("binary file successfully loaded\n"); + + transfer_size = ret; + + if (verify) { + printf("verifying file...\n"); + + ret = verify_memory(h, p_id, xfile, skip, dladdr, fsize, verify_buffer, verify_cnt); + if (ret < 0) { + printf("verifying failed\n"); + goto cleanup; + } + + printf("file successfully verified\n"); + + if (verify == 2) { + if (verify_cnt > 64) + verify_cnt = 64; + ret = load_file(h, p_id, verify_buffer, verify_cnt, + buf, BUF_SIZE, dladdr, verify_cnt, + FT_APP, xfile); + if (ret < 0) + goto cleanup; + + } + } + + if (p_id->mach_id->mode == MODE_HID && type == FT_APP) { + printf("jumping to 0x%08x\n", header_addr); + + jump_command[2] = (unsigned char)(header_addr >> 24); + jump_command[3] = (unsigned char)(header_addr >> 16); + jump_command[4] = (unsigned char)(header_addr >> 8); + jump_command[5] = (unsigned char)(header_addr); + + /* Any command will initiate jump for mx51, jump address is ignored by mx51 */ + retry = 0; + + for (;;) { + err = transfer(h, 1, jump_command, 16, &last_trans, p_id); + if (!err) + break; + + printf("jump_command err=%i, last_trans=%i\n", err, last_trans); + + if (retry > 5) + return -4; + + retry++; + } + + memset(tmp, 0, sizeof(tmp)); + err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + + if (err) + printf("j3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", + err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); + } + + ret = (fsize == transfer_size) ? 0 : -16; +cleanup: + fclose(xfile); + free(verify_buffer); + free(buf); + + return ret; +} + +static void usage(const char *prgname) +{ + fprintf(stderr, "usage: %s [OPTIONS] [FILENAME]\n\n" + "-c check correctness of flashed image\n" + "-v verbose (give multiple times to increase)\n" + "-h this help\n", prgname); + exit(1); +} + +int main(int argc, char *argv[]) +{ + struct usb_id *p_id; + struct mach_id *mach; + libusb_device **devs; + libusb_device *dev; + int r; + int err; + int ret = 1; + ssize_t cnt; + libusb_device_handle *h = NULL; + int config = 0; + int verify = 0; + struct usb_work w = {}; + int opt; + + while ((opt = getopt(argc, argv, "cvh")) != -1) { + switch (opt) { + case 'c': + verify = 1; + break; + case 'v': + verbose++; + break; + case 'h': + usage(argv[0]); + default: + exit(1); + } + } + + if (optind == argc) { + fprintf(stderr, "no filename given\n"); + usage(argv[0]); + exit(1); + } + + w.plug = 1; + w.dcd = 1; + w.jump_mode = J_HEADER; + strncpy(w.filename, argv[optind], sizeof(w.filename) - 1); + + r = libusb_init(NULL); + if (r < 0) + goto out; + + cnt = libusb_get_device_list(NULL, &devs); + if (cnt < 0) { + fprintf(stderr, "no supported device found\n"); + goto out; + } + + dev = find_imx_dev(devs, &mach); + if (!dev) { + fprintf(stderr, "no supported device found\n"); + goto out; + } + + err = libusb_open(dev, &h); + if (err) { + fprintf(stderr, "Could not open device vid=0x%x pid=0x%x err=%d\n", + mach->vid, mach->pid, err); + goto out; + } + + libusb_free_device_list(devs, 1); + + libusb_get_configuration(h, &config); + + if (libusb_kernel_driver_active(h, 0)) + libusb_detach_kernel_driver(h, 0); + + err = libusb_claim_interface(h, 0); + if (err) { + printf("Claim failed\n"); + goto out; + } + + p_id = malloc(sizeof(*p_id)); + if (!p_id) { + perror("malloc"); + exit(1); + } + + p_id->mach_id = mach; + + err = do_status(h, p_id); + if (err) { + printf("status failed\n"); + goto out; + } + + err = do_irom_download(h, p_id, &w, verify); + if (err) { + err = do_status(h, p_id); + goto out; + } + + ret = 0; +out: + if (h) + libusb_close(h); + + libusb_exit(NULL); + + return ret; +} diff --git a/scripts/kwbimage.c b/scripts/kwbimage.c new file mode 100644 index 0000000000..4ebb07fe22 --- /dev/null +++ b/scripts/kwbimage.c @@ -0,0 +1,1496 @@ +/* + * Image manipulator for Marvell SoCs + * supports Kirkwood, Dove, Armada 370, and Armada XP + * + * (C) Copyright 2013 Thomas Petazzoni + * <thomas.petazzoni@free-electrons.com> + * + * 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. + * + * This tool allows to extract and create bootable images for Marvell + * Kirkwood, Dove, Armada 370, and Armada XP SoCs. It supports two + * versions of the bootable image format: version 0 (used on Marvell + * Kirkwood and Dove) and version 1 (used on Marvell Armada 370/XP). + * + * To extract an image, run: + * ./scripts/kwbimage -x -i <image-file> -o <some-directory> + * + * In <some-directory>, kwbimage will output 'kwbimage.cfg', the + * configuration file that describes the image, 'payload', which is + * the bootloader code itself, and it may output a 'binary.0' file + * that corresponds to a binary blob (only possible in version 1 + * images). + * + * To create an image, run: + * ./scripts/kwbimage -c -i <image-cfg-file> -o <image-file> + * + * The given configuration file is in the format of the 'kwbimage.cfg' + * file, and should reference the payload file (generally the + * bootloader code) and optionally a binary blob. + * + * Not implemented: support for the register headers and secure + * headers in v1 images + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdint.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> + +#define ALIGN_SUP(x, a) (((x) + (a - 1)) & ~(a - 1)) + +/* Structure of the main header, version 0 (Kirkwood, Dove) */ +struct main_hdr_v0 { + uint8_t blockid; /*0 */ + uint8_t nandeccmode; /*1 */ + uint16_t nandpagesize; /*2-3 */ + uint32_t blocksize; /*4-7 */ + uint32_t rsvd1; /*8-11 */ + uint32_t srcaddr; /*12-15 */ + uint32_t destaddr; /*16-19 */ + uint32_t execaddr; /*20-23 */ + uint8_t satapiomode; /*24 */ + uint8_t rsvd3; /*25 */ + uint16_t ddrinitdelay; /*26-27 */ + uint16_t rsvd2; /*28-29 */ + uint8_t ext; /*30 */ + uint8_t checksum; /*31 */ +}; + +struct ext_hdr_v0_reg { + uint32_t raddr; + uint32_t rdata; +}; + +#define EXT_HDR_V0_REG_COUNT ((0x1dc - 0x20)/sizeof(struct ext_hdr_v0_reg)) + +struct ext_hdr_v0 { + uint32_t offset; + uint8_t reserved[0x20 - sizeof(uint32_t)]; + struct ext_hdr_v0_reg rcfg[EXT_HDR_V0_REG_COUNT]; + uint8_t reserved2[7]; + uint8_t checksum; +}; + +/* Structure of the main header, version 1 (Armada 370, Armada XP) */ +struct main_hdr_v1 { + uint8_t blockid; /* 0 */ + uint8_t reserved1; /* 1 */ + uint16_t reserved2; /* 2-3 */ + uint32_t blocksize; /* 4-7 */ + uint8_t version; /* 8 */ + uint8_t headersz_msb; /* 9 */ + uint16_t headersz_lsb; /* A-B */ + uint32_t srcaddr; /* C-F */ + uint32_t destaddr; /* 10-13 */ + uint32_t execaddr; /* 14-17 */ + uint8_t reserved3; /* 18 */ + uint8_t nandblocksize; /* 19 */ + uint8_t nandbadblklocation; /* 1A */ + uint8_t reserved4; /* 1B */ + uint16_t reserved5; /* 1C-1D */ + uint8_t ext; /* 1E */ + uint8_t checksum; /* 1F */ +}; + +/* + * Header for the optional headers, version 1 (Armada 370, Armada XP) + */ +struct opt_hdr_v1 { + uint8_t headertype; + uint8_t headersz_msb; + uint16_t headersz_lsb; + char data[0]; +}; + +/* + * Various values for the opt_hdr_v1->headertype field, describing the + * different types of optional headers. The "secure" header contains + * informations related to secure boot (encryption keys, etc.). The + * "binary" header contains ARM binary code to be executed prior to + * executing the main payload (usually the bootloader). This is + * typically used to execute DDR3 training code. The "register" header + * allows to describe a set of (address, value) tuples that are + * generally used to configure the DRAM controller. + */ +#define OPT_HDR_V1_SECURE_TYPE 0x1 +#define OPT_HDR_V1_BINARY_TYPE 0x2 +#define OPT_HDR_V1_REGISTER_TYPE 0x3 + +#define KWBHEADER_V1_SIZE(hdr) \ + (((hdr)->headersz_msb << 16) | (hdr)->headersz_lsb) + +struct boot_mode { + unsigned int id; + const char *name; +}; + +struct boot_mode boot_modes[] = { + { 0x4D, "i2c" }, + { 0x5A, "spi" }, + { 0x8B, "nand" }, + { 0x78, "sata" }, + { 0x9C, "pex" }, + { 0x69, "uart" }, + {}, +}; + +struct nand_ecc_mode { + unsigned int id; + const char *name; +}; + +struct nand_ecc_mode nand_ecc_modes[] = { + { 0x00, "default" }, + { 0x01, "hamming" }, + { 0x02, "rs" }, + { 0x03, "disabled" }, + {}, +}; + +/* Used to identify an undefined execution or destination address */ +#define ADDR_INVALID ((uint32_t)-1) + +#define BINARY_MAX_ARGS 8 + +/* In-memory representation of a line of the configuration file */ +struct image_cfg_element { + enum { + IMAGE_CFG_VERSION = 0x1, + IMAGE_CFG_BOOT_FROM, + IMAGE_CFG_DEST_ADDR, + IMAGE_CFG_EXEC_ADDR, + IMAGE_CFG_NAND_BLKSZ, + IMAGE_CFG_NAND_BADBLK_LOCATION, + IMAGE_CFG_NAND_ECC_MODE, + IMAGE_CFG_NAND_PAGESZ, + IMAGE_CFG_BINARY, + IMAGE_CFG_PAYLOAD, + IMAGE_CFG_DATA, + } type; + union { + unsigned int version; + unsigned int bootfrom; + struct { + const char *file; + unsigned int args[BINARY_MAX_ARGS]; + unsigned int nargs; + } binary; + const char *payload; + unsigned int dstaddr; + unsigned int execaddr; + unsigned int nandblksz; + unsigned int nandbadblklocation; + unsigned int nandeccmode; + unsigned int nandpagesz; + struct ext_hdr_v0_reg regdata; + }; +}; + +#define IMAGE_CFG_ELEMENT_MAX 256 + +/* + * Byte 8 of the image header contains the version number. In the v0 + * header, byte 8 was reserved, and always set to 0. In the v1 header, + * byte 8 has been changed to a proper field, set to 1. + */ +static unsigned int image_version(void *header) +{ + unsigned char *ptr = header; + return ptr[8]; +} + +/* + * Utility functions to manipulate boot mode and ecc modes (convert + * them back and forth between description strings and the + * corresponding numerical identifiers). + */ + +static const char *image_boot_mode_name(unsigned int id) +{ + int i; + for (i = 0; boot_modes[i].name; i++) + if (boot_modes[i].id == id) + return boot_modes[i].name; + return NULL; +} + +int image_boot_mode_id(const char *boot_mode_name) +{ + int i; + for (i = 0; boot_modes[i].name; i++) + if (!strcmp(boot_modes[i].name, boot_mode_name)) + return boot_modes[i].id; + + return -1; +} + +static const char *image_nand_ecc_mode_name(unsigned int id) +{ + int i; + for (i = 0; nand_ecc_modes[i].name; i++) + if (nand_ecc_modes[i].id == id) + return nand_ecc_modes[i].name; + return NULL; +} + +int image_nand_ecc_mode_id(const char *nand_ecc_mode_name) +{ + int i; + for (i = 0; nand_ecc_modes[i].name; i++) + if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name)) + return nand_ecc_modes[i].id; + return -1; +} + +static struct image_cfg_element * +image_find_option(struct image_cfg_element *image_cfg, + int cfgn, unsigned int optiontype) +{ + int i; + + for (i = 0; i < cfgn; i++) { + if (image_cfg[i].type == optiontype) + return &image_cfg[i]; + } + + return NULL; +} + +static unsigned int +image_count_options(struct image_cfg_element *image_cfg, + int cfgn, unsigned int optiontype) +{ + int i; + unsigned int count = 0; + + for (i = 0; i < cfgn; i++) + if (image_cfg[i].type == optiontype) + count++; + + return count; +} + +/* + * Compute a 8-bit checksum of a memory area. This algorithm follows + * the requirements of the Marvell SoC BootROM specifications. + */ +static uint8_t image_checksum8(void *start, uint32_t len) +{ + uint8_t csum = 0; + uint8_t *p = start; + + /* check len and return zero checksum if invalid */ + if (!len) + return 0; + + do { + csum += *p; + p++; + } while (--len); + + return csum; +} + +static uint32_t image_checksum32 (void *start, uint32_t len) +{ + uint32_t csum = 0; + uint32_t *p = start; + + /* check len and return zero checksum if invalid */ + if (!len) + return 0; + + if (len % sizeof(uint32_t)) { + fprintf (stderr, "Length %d is not in multiple of %zu\n", + len, sizeof(uint32_t)); + return 0; + } + + do { + csum += *p; + p++; + len -= sizeof(uint32_t); + } while (len > 0); + + return csum; +} + +static void usage(const char *prog) +{ + printf("Usage: %s [-c | -x] -i <input> -o <output>\n", prog); + printf(" -c: create a new image\n"); + printf(" -x: extract an existing image\n"); + printf(" -i: input file\n"); + printf(" when used with -c, should point to a kwbimage.cfg file\n"); + printf(" when used with -x, should point to the image to be extracted\n"); + printf(" -o: output file/directory\n"); + printf(" when used with -c, should point to the image file to create\n"); + printf(" when used with -x, should point to a directory when the image will be extracted\n"); + printf(" -v: verbose\n"); + printf(" -h: this help text\n"); + printf(" Options specific to image creation:\n"); + printf(" -p: path to payload image. Overrides the PAYLOAD line from kwbimage.cfg\n"); + printf(" -m: boot media. Overrides the BOOT_FROM line from kwbimage.cfg\n"); + printf(" -d: load address. Overrides the DEST_ADDR line from kwbimage.cfg\n"); + printf(" -e: exec address. Overrides the EXEC_ADDR line from kwbimage.cfg\n"); +} + +static int image_extract_payload(void *payload, size_t sz, const char *output) +{ + char *imageoutname; + FILE *imageout; + int ret; + + ret = asprintf(&imageoutname, "%s/payload", output); + if (ret < 0) { + fprintf(stderr, "Cannot allocate memory\n"); + return -1; + } + + imageout = fopen(imageoutname, "w+"); + if (!imageout) { + fprintf(stderr, "Could not open output file %s\n", + imageoutname); + free(imageoutname); + return -1; + } + + ret = fwrite(payload, sz, 1, imageout); + if (ret != 1) { + fprintf(stderr, "Could not write to open file %s\n", + imageoutname); + fclose(imageout); + free(imageoutname); + return -1; + } + + fclose(imageout); + free(imageoutname); + return 0; +} + +static int image_extract_v0(void *fdimap, const char *output, FILE *focfg) +{ + struct main_hdr_v0 *main_hdr = fdimap; + struct ext_hdr_v0 *ext_hdr; + const char *boot_mode_name; + uint32_t *img_checksum; + size_t payloadsz; + int cksum, i; + + /* + * Verify checksum. When calculating the header, discard the + * last byte of the header, which itself contains the + * checksum. + */ + cksum = image_checksum8(main_hdr, sizeof(struct main_hdr_v0)-1); + if (cksum != main_hdr->checksum) { + fprintf(stderr, + "Invalid main header checksum: 0x%08x vs. 0x%08x\n", + cksum, main_hdr->checksum); + return -1; + } + + boot_mode_name = image_boot_mode_name(main_hdr->blockid); + if (!boot_mode_name) { + fprintf(stderr, "Invalid boot ID: 0x%x\n", + main_hdr->blockid); + return -1; + } + + fprintf(focfg, "VERSION 0\n"); + fprintf(focfg, "BOOT_FROM %s\n", boot_mode_name); + fprintf(focfg, "DESTADDR %08x\n", main_hdr->destaddr); + fprintf(focfg, "EXECADDR %08x\n", main_hdr->execaddr); + + if (!strcmp(boot_mode_name, "nand")) { + const char *nand_ecc_mode = + image_nand_ecc_mode_name(main_hdr->nandeccmode); + fprintf(focfg, "NAND_ECCMODE %s\n", + nand_ecc_mode); + fprintf(focfg, "NAND_PAGESZ %08x\n", + main_hdr->nandpagesize); + } + + /* No extension header, we're done */ + if (!main_hdr->ext) + return 0; + + ext_hdr = fdimap + sizeof(struct main_hdr_v0); + + for (i = 0; i < EXT_HDR_V0_REG_COUNT; i++) { + if (ext_hdr->rcfg[i].raddr == 0 && + ext_hdr->rcfg[i].rdata == 0) + break; + + fprintf(focfg, "DATA %08x %08x\n", + ext_hdr->rcfg[i].raddr, + ext_hdr->rcfg[i].rdata); + } + + /* The image is concatenated with a 32 bits checksum */ + payloadsz = main_hdr->blocksize - sizeof(uint32_t); + img_checksum = (uint32_t *) (fdimap + main_hdr->srcaddr + payloadsz); + + if (*img_checksum != image_checksum32(fdimap + main_hdr->srcaddr, + payloadsz)) { + fprintf(stderr, "The image checksum does not match\n"); + return -1; + } + + /* Finally, handle the image itself */ + fprintf(focfg, "PAYLOAD %s/payload\n", output); + return image_extract_payload(fdimap + main_hdr->srcaddr, + payloadsz, output); +} + +static int image_extract_binary_hdr_v1(const void *binary, const char *output, + FILE *focfg, int hdrnum, size_t binsz) +{ + char *binaryoutname; + FILE *binaryout; + const unsigned int *args; + unsigned int nargs; + int ret, i; + + args = binary; + nargs = args[0]; + args++; + + ret = asprintf(&binaryoutname, "%s/binary.%d", output, + hdrnum); + if (ret < 0) { + fprintf(stderr, "Couldn't not allocate memory\n"); + return ret; + } + + binaryout = fopen(binaryoutname, "w+"); + if (!binaryout) { + fprintf(stderr, "Couldn't open output file %s\n", + binaryoutname); + free(binaryoutname); + return -1; + } + + ret = fwrite(binary + (nargs + 1) * sizeof(unsigned int), + binsz - (nargs + 1) * sizeof(unsigned int), 1, + binaryout); + if (ret != 1) { + fprintf(stderr, "Could not write to output file %s\n", + binaryoutname); + fclose(binaryout); + free(binaryoutname); + return -1; + } + + fclose(binaryout); + + fprintf(focfg, "BINARY %s", binaryoutname); + for (i = 0; i < nargs; i++) + fprintf(focfg, " %08x", args[i]); + fprintf(focfg, "\n"); + + free(binaryoutname); + + return 0; +} + +static int image_extract_v1(void *fdimap, const char *output, FILE *focfg) +{ + struct main_hdr_v1 *main_hdr = fdimap; + struct opt_hdr_v1 *opt_hdr; + const char *boot_mode_name; + int headersz = KWBHEADER_V1_SIZE(main_hdr); + int hasheaders; + uint8_t cksum; + int opthdrid; + + /* + * Verify the checkum. We have to substract the checksum + * itself, because when the checksum is calculated, the + * checksum field is 0. + */ + cksum = image_checksum8(main_hdr, headersz); + cksum -= main_hdr->checksum; + + if (cksum != main_hdr->checksum) { + fprintf(stderr, + "Invalid main header checksum: 0x%08x vs. 0x%08x\n", + cksum, main_hdr->checksum); + return -1; + } + + /* First, take care of the main header */ + boot_mode_name = image_boot_mode_name(main_hdr->blockid); + if (!boot_mode_name) { + fprintf(stderr, "Invalid boot ID: 0x%x\n", + main_hdr->blockid); + return -1; + } + + fprintf(focfg, "VERSION 1\n"); + fprintf(focfg, "BOOT_FROM %s\n", boot_mode_name); + fprintf(focfg, "DESTADDR %08x\n", main_hdr->destaddr); + fprintf(focfg, "EXECADDR %08x\n", main_hdr->execaddr); + fprintf(focfg, "NAND_BLKSZ %08x\n", + main_hdr->nandblocksize * 64 * 1024); + fprintf(focfg, "NAND_BADBLK_LOCATION %02x\n", + main_hdr->nandbadblklocation); + + hasheaders = main_hdr->ext; + opt_hdr = fdimap + sizeof(struct main_hdr_v1); + opthdrid = 0; + + /* Then, go through all the extension headers */ + while (hasheaders) { + int opthdrsz = KWBHEADER_V1_SIZE(opt_hdr); + + switch (opt_hdr->headertype) { + case OPT_HDR_V1_BINARY_TYPE: + image_extract_binary_hdr_v1(opt_hdr->data, output, + focfg, opthdrid, + opthdrsz - + sizeof(struct opt_hdr_v1)); + break; + case OPT_HDR_V1_SECURE_TYPE: + case OPT_HDR_V1_REGISTER_TYPE: + fprintf(stderr, + "Support for header type 0x%x not implemented\n", + opt_hdr->headertype); + exit(1); + break; + default: + fprintf(stderr, "Invalid header type 0x%x\n", + opt_hdr->headertype); + exit(1); + } + + /* + * The first byte of the last double word of the + * current header indicates whether there is a next + * header or not. + */ + hasheaders = ((char *)opt_hdr)[opthdrsz - 4]; + + /* Move to the next header */ + opt_hdr = ((void *)opt_hdr) + opthdrsz; + opthdrid++; + } + + /* Finally, handle the image itself */ + fprintf(focfg, "PAYLOAD %s/payload\n", output); + return image_extract_payload(fdimap + main_hdr->srcaddr, + main_hdr->blocksize - 4, + output); +} + +static int image_extract(const char *input, const char *output) +{ + int fdi, ret; + struct stat fdistat, fdostat; + void *fdimap; + char *focfgname; + FILE *focfg; + + fdi = open(input, O_RDONLY); + if (fdi < 0) { + fprintf(stderr, "Cannot open input file %s: %m\n", + input); + return -1; + } + + ret = fstat(fdi, &fdistat); + if (ret < 0) { + fprintf(stderr, "Cannot stat input file %s: %m\n", + input); + close(fdi); + return -1; + } + + fdimap = mmap(NULL, fdistat.st_size, PROT_READ, MAP_PRIVATE, fdi, 0); + if (fdimap == MAP_FAILED) { + fprintf(stderr, "Cannot map input file %s: %m\n", + input); + close(fdi); + return -1; + } + + close(fdi); + + ret = stat(output, &fdostat); + if (ret < 0) { + fprintf(stderr, "Cannot stat output directory %s: %m\n", + output); + munmap(fdimap, fdistat.st_size); + return -1; + } + + if (!S_ISDIR(fdostat.st_mode)) { + fprintf(stderr, "Output %s should be a directory\n", + output); + munmap(fdimap, fdistat.st_size); + return -1; + } + + ret = asprintf(&focfgname, "%s/kwbimage.cfg", output); + if (ret < 0) { + fprintf(stderr, "Failed to allocate memory\n"); + munmap(fdimap, fdistat.st_size); + return -1; + } + + focfg = fopen(focfgname, "w+"); + if (!focfg) { + fprintf(stderr, "Output file %s could not be created\n", + focfgname); + free(focfgname); + munmap(fdimap, fdistat.st_size); + return -1; + } + + free(focfgname); + + if (image_version(fdimap) == 0) + ret = image_extract_v0(fdimap, output, focfg); + else if (image_version(fdimap) == 1) + ret = image_extract_v1(fdimap, output, focfg); + else { + fprintf(stderr, "Invalid image version %d\n", + image_version(fdimap)); + ret = -1; + } + + fclose(focfg); + munmap(fdimap, fdistat.st_size); + return ret; +} + +static int image_create_payload(void *payload_start, size_t payloadsz, + const char *payload_filename) +{ + FILE *payload; + uint32_t *payload_checksum = + (uint32_t *) (payload_start + payloadsz); + int ret; + + payload = fopen(payload_filename, "r"); + if (!payload) { + fprintf(stderr, "Cannot open payload file %s\n", + payload_filename); + return -1; + } + + ret = fread(payload_start, payloadsz, 1, payload); + if (ret != 1) { + fprintf(stderr, "Cannot read payload file %s\n", + payload_filename); + return -1; + } + + fclose(payload); + + *payload_checksum = image_checksum32(payload_start, payloadsz); + return 0; +} + +static void *image_create_v0(struct image_cfg_element *image_cfg, + int cfgn, const char *output, size_t *imagesz) +{ + struct image_cfg_element *e, *payloade; + size_t headersz, payloadsz, totalsz; + struct main_hdr_v0 *main_hdr; + struct ext_hdr_v0 *ext_hdr; + void *image; + int has_ext = 0; + int ret; + + /* Calculate the size of the header and the size of the + * payload */ + headersz = sizeof(struct main_hdr_v0); + payloadsz = 0; + + if (image_count_options(image_cfg, cfgn, IMAGE_CFG_DATA) > 0) { + has_ext = 1; + headersz += sizeof(struct ext_hdr_v0); + } + + if (image_count_options(image_cfg, cfgn, IMAGE_CFG_PAYLOAD) > 1) { + fprintf(stderr, "More than one payload, not possible\n"); + return NULL; + } + + payloade = image_find_option(image_cfg, cfgn, IMAGE_CFG_PAYLOAD); + if (payloade) { + struct stat s; + int ret; + + ret = stat(payloade->payload, &s); + if (ret < 0) { + fprintf(stderr, "Cannot stat payload file %s\n", + payloade->payload); + return NULL; + } + + payloadsz = s.st_size; + } + + /* Headers, payload and 32-bits checksum */ + totalsz = headersz + payloadsz + sizeof(uint32_t); + + image = malloc(totalsz); + if (!image) { + fprintf(stderr, "Cannot allocate memory for image\n"); + return NULL; + } + + memset(image, 0, totalsz); + + main_hdr = image; + + /* Fill in the main header */ + main_hdr->blocksize = payloadsz + sizeof(uint32_t); + main_hdr->srcaddr = headersz; + main_hdr->ext = has_ext; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_BOOT_FROM); + if (e) + main_hdr->blockid = e->bootfrom; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_DEST_ADDR); + if (e) + main_hdr->destaddr = e->dstaddr; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_EXEC_ADDR); + if (e) + main_hdr->execaddr = e->execaddr; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_NAND_ECC_MODE); + if (e) + main_hdr->nandeccmode = e->nandeccmode; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_NAND_PAGESZ); + if (e) + main_hdr->nandpagesize = e->nandpagesz; + main_hdr->checksum = image_checksum8(image, + sizeof(struct main_hdr_v0)); + + /* Generate the ext header */ + if (has_ext) { + int cfgi, datai; + + ext_hdr = image + sizeof(struct main_hdr_v0); + ext_hdr->offset = 0x40; + + for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) { + e = &image_cfg[cfgi]; + + if (e->type != IMAGE_CFG_DATA) + continue; + + ext_hdr->rcfg[datai].raddr = e->regdata.raddr; + ext_hdr->rcfg[datai].rdata = e->regdata.rdata; + datai++; + } + + ext_hdr->checksum = image_checksum8(ext_hdr, + sizeof(struct ext_hdr_v0)); + } + + if (payloade) { + ret = image_create_payload(image + headersz, payloadsz, + payloade->payload); + if (ret < 0) + return NULL; + } + + *imagesz = totalsz; + return image; +} + +static void *image_create_v1(struct image_cfg_element *image_cfg, + int cfgn, const char *output, size_t *imagesz) +{ + struct image_cfg_element *e, *payloade, *binarye; + struct main_hdr_v1 *main_hdr; + size_t headersz, payloadsz, totalsz; + void *image, *cur; + int hasext = 0; + int ret; + + /* Calculate the size of the header and the size of the + * payload */ + headersz = sizeof(struct main_hdr_v1); + payloadsz = 0; + + if (image_count_options(image_cfg, cfgn, IMAGE_CFG_BINARY) > 1) { + fprintf(stderr, "More than one binary blob, not supported\n"); + return NULL; + } + + if (image_count_options(image_cfg, cfgn, IMAGE_CFG_PAYLOAD) > 1) { + fprintf(stderr, "More than one payload, not possible\n"); + return NULL; + } + + binarye = image_find_option(image_cfg, cfgn, IMAGE_CFG_BINARY); + if (binarye) { + struct stat s; + + ret = stat(binarye->binary.file, &s); + if (ret < 0) { + char *cwd = get_current_dir_name(); + fprintf(stderr, + "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n" + "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n" + "image for your board. See 'kwbimage -x' to extract it from an existing image.\n", + binarye->binary.file, cwd); + free(cwd); + return NULL; + } + + headersz += s.st_size + + binarye->binary.nargs * sizeof(unsigned int); + hasext = 1; + } + + payloade = image_find_option(image_cfg, cfgn, IMAGE_CFG_PAYLOAD); + if (payloade) { + struct stat s; + + ret = stat(payloade->payload, &s); + if (ret < 0) { + fprintf(stderr, "Cannot stat payload file %s\n", + payloade->payload); + return NULL; + } + + payloadsz = s.st_size; + } + + /* The payload should be aligned on some reasonable + * boundary */ + headersz = ALIGN_SUP(headersz, 4096); + + /* The total size includes the headers, the payload, and the + * 32 bits checksum at the end of the payload */ + totalsz = headersz + payloadsz + sizeof(uint32_t); + + image = malloc(totalsz); + if (!image) { + fprintf(stderr, "Cannot allocate memory for image\n"); + return NULL; + } + + memset(image, 0, totalsz); + + cur = main_hdr = image; + cur += sizeof(struct main_hdr_v1); + + /* Fill the main header */ + main_hdr->blocksize = payloadsz + sizeof(uint32_t); + main_hdr->headersz_lsb = headersz & 0xFFFF; + main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16; + main_hdr->srcaddr = headersz; + main_hdr->ext = hasext; + main_hdr->version = 1; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_BOOT_FROM); + if (e) + main_hdr->blockid = e->bootfrom; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_DEST_ADDR); + if (e) + main_hdr->destaddr = e->dstaddr; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_EXEC_ADDR); + if (e) + main_hdr->execaddr = e->execaddr; + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_NAND_BLKSZ); + if (e) + main_hdr->nandblocksize = e->nandblksz / (64 * 1024); + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_NAND_BADBLK_LOCATION); + if (e) + main_hdr->nandbadblklocation = e->nandbadblklocation; + + if (binarye) { + struct opt_hdr_v1 *hdr = cur; + unsigned int *args; + size_t binhdrsz; + struct stat s; + int argi; + FILE *bin; + + hdr->headertype = OPT_HDR_V1_BINARY_TYPE; + + bin = fopen(binarye->binary.file, "r"); + if (!bin) { + fprintf(stderr, "Cannot open binary file %s\n", + binarye->binary.file); + return NULL; + } + + fstat(fileno(bin), &s); + + binhdrsz = sizeof(struct opt_hdr_v1) + + (binarye->binary.nargs + 1) * sizeof(unsigned int) + + s.st_size; + hdr->headersz_lsb = binhdrsz & 0xFFFF; + hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16; + + cur += sizeof(struct opt_hdr_v1); + + args = cur; + *args = binarye->binary.nargs; + args++; + for (argi = 0; argi < binarye->binary.nargs; argi++) + args[argi] = binarye->binary.args[argi]; + + cur += (binarye->binary.nargs + 1) * sizeof(unsigned int); + + ret = fread(cur, s.st_size, 1, bin); + if (ret != 1) { + fprintf(stderr, + "Could not read binary image %s\n", + binarye->binary.file); + return NULL; + } + + fclose(bin); + + cur += s.st_size; + + /* + * For now, we don't support more than one binary + * header, and no other header types are + * supported. So, the binary header is necessarily the + * last one + */ + *((unsigned char *) cur) = 0; + + cur += sizeof(uint32_t); + } + + /* Calculate and set the header checksum */ + main_hdr->checksum = image_checksum8(main_hdr, headersz); + + if (payloade) { + ret = image_create_payload(image + headersz, payloadsz, + payloade->payload); + if (ret < 0) + return NULL; + } + + *imagesz = totalsz; + return image; +} + +static int image_create_config_parse_oneline(char *line, + struct image_cfg_element *el) +{ + char *keyword, *saveptr; + + keyword = strtok_r(line, " ", &saveptr); + if (!strcmp(keyword, "VERSION")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_VERSION; + el->version = atoi(value); + } else if (!strcmp(keyword, "BOOT_FROM")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_BOOT_FROM; + el->bootfrom = image_boot_mode_id(value); + if (el->bootfrom < 0) { + fprintf(stderr, + "Invalid boot media '%s'\n", value); + return -1; + } + } else if (!strcmp(keyword, "DESTADDR")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_DEST_ADDR; + el->dstaddr = strtol(value, NULL, 16); + } else if (!strcmp(keyword, "EXECADDR")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_EXEC_ADDR; + el->execaddr = strtol(value, NULL, 16); + } else if (!strcmp(keyword, "NAND_BLKSZ")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_NAND_BLKSZ; + el->nandblksz = strtol(value, NULL, 16); + } else if (!strcmp(keyword, "NAND_BADBLK_LOCATION")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_NAND_BADBLK_LOCATION; + el->nandbadblklocation = + strtol(value, NULL, 16); + } else if (!strcmp(keyword, "NAND_ECCMODE")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_NAND_ECC_MODE; + el->nandeccmode = image_nand_ecc_mode_id(value); + if (el->nandeccmode < 0) { + fprintf(stderr, + "Invalid NAND ECC mode '%s'\n", value); + return -1; + } + } else if (!strcmp(keyword, "NAND_PAGESZ")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_NAND_PAGESZ; + el->nandpagesz = strtol(value, NULL, 16); + } else if (!strcmp(keyword, "BINARY")) { + char *value = strtok_r(NULL, " ", &saveptr); + int argi = 0; + + el->type = IMAGE_CFG_BINARY; + el->binary.file = strdup(value); + while (1) { + value = strtok_r(NULL, " ", &saveptr); + if (!value) + break; + el->binary.args[argi] = strtol(value, NULL, 16); + argi++; + if (argi >= BINARY_MAX_ARGS) { + fprintf(stderr, + "Too many argument for binary\n"); + return -1; + } + } + el->binary.nargs = argi; + } else if (!strcmp(keyword, "DATA")) { + char *value1 = strtok_r(NULL, " ", &saveptr); + char *value2 = strtok_r(NULL, " ", &saveptr); + + if (!value1 || !value2) { + fprintf(stderr, "Invalid number of arguments for DATA\n"); + return -1; + } + + el->type = IMAGE_CFG_DATA; + el->regdata.raddr = strtol(value1, NULL, 16); + el->regdata.rdata = strtol(value2, NULL, 16); + } else if (!strcmp(keyword, "PAYLOAD")) { + char *value = strtok_r(NULL, " ", &saveptr); + el->type = IMAGE_CFG_PAYLOAD; + el->payload = strdup(value); + } else { + fprintf(stderr, "Ignoring unknown line '%s'\n", line); + } + + return 0; +} + +/* + * Parse the configuration file 'fcfg' into the array of configuration + * elements 'image_cfg', and return the number of configuration + * elements in 'cfgn'. + */ +static int image_create_config_parse(FILE *fcfg, + struct image_cfg_element *image_cfg, + int *cfgn) +{ + int ret; + int cfgi = 0; + + /* Parse the configuration file */ + while (!feof(fcfg)) { + char *line; + char buf[256]; + + /* Read the current line */ + memset(buf, 0, sizeof(buf)); + line = fgets(buf, sizeof(buf), fcfg); + if (!line) + break; + + /* Ignore useless lines */ + if (line[0] == '\n' || line[0] == '#') + continue; + + /* Strip final newline */ + if (line[strlen(line) - 1] == '\n') + line[strlen(line) - 1] = 0; + + /* Parse the current line */ + ret = image_create_config_parse_oneline(line, + &image_cfg[cfgi]); + if (ret) + return ret; + + cfgi++; + + if (cfgi >= IMAGE_CFG_ELEMENT_MAX) { + fprintf(stderr, "Too many configuration elements in .cfg file\n"); + return -1; + } + } + + *cfgn = cfgi; + return 0; +} + +static int image_override_payload(struct image_cfg_element *image_cfg, + int *cfgn, const char *payload) +{ + struct image_cfg_element *e; + int cfgi = *cfgn; + + if (!payload) + return 0; + + e = image_find_option(image_cfg, *cfgn, IMAGE_CFG_PAYLOAD); + if (e) { + e->payload = payload; + return 0; + } + + image_cfg[cfgi].type = IMAGE_CFG_PAYLOAD; + image_cfg[cfgi].payload = payload; + cfgi++; + + *cfgn = cfgi; + return 0; +} + +static int image_override_bootmedia(struct image_cfg_element *image_cfg, + int *cfgn, const char *bootmedia) +{ + struct image_cfg_element *e; + int bootfrom; + int cfgi = *cfgn; + + if (!bootmedia) + return 0; + + bootfrom = image_boot_mode_id(bootmedia); + if (!bootfrom) { + fprintf(stderr, + "Invalid boot media '%s'\n", bootmedia); + return -1; + } + + e = image_find_option(image_cfg, *cfgn, IMAGE_CFG_BOOT_FROM); + if (e) { + e->bootfrom = bootfrom; + return 0; + } + + image_cfg[cfgi].type = IMAGE_CFG_BOOT_FROM; + image_cfg[cfgi].bootfrom = bootfrom; + cfgi++; + + *cfgn = cfgi; + return 0; +} + +static int image_override_dstaddr(struct image_cfg_element *image_cfg, + int *cfgn, uint32_t dstaddr) +{ + struct image_cfg_element *e; + int cfgi = *cfgn; + + if (dstaddr == ADDR_INVALID) + return 0; + + e = image_find_option(image_cfg, *cfgn, IMAGE_CFG_DEST_ADDR); + if (e) { + e->dstaddr = dstaddr; + return 0; + } + + image_cfg[cfgi].type = IMAGE_CFG_DEST_ADDR; + image_cfg[cfgi].dstaddr = dstaddr; + cfgi++; + + *cfgn = cfgi; + return 0; +} + +static int image_override_execaddr(struct image_cfg_element *image_cfg, + int *cfgn, uint32_t execaddr) +{ + struct image_cfg_element *e; + int cfgi = *cfgn; + + if (execaddr == ADDR_INVALID) + return 0; + + e = image_find_option(image_cfg, *cfgn, IMAGE_CFG_EXEC_ADDR); + if (e) { + e->execaddr = execaddr; + return 0; + } + + image_cfg[cfgi].type = IMAGE_CFG_EXEC_ADDR; + image_cfg[cfgi].execaddr = execaddr; + cfgi++; + + *cfgn = cfgi; + return 0; +} + +static int image_get_version(struct image_cfg_element *image_cfg, + int cfgn) +{ + struct image_cfg_element *e; + + e = image_find_option(image_cfg, cfgn, IMAGE_CFG_VERSION); + if (!e) + return -1; + + return e->version; +} + +static void image_dump_config(struct image_cfg_element *image_cfg, + int cfgn) +{ + int cfgi; + + printf("== configuration dump\n"); + + for (cfgi = 0; cfgi < cfgn; cfgi++) { + struct image_cfg_element *e = &image_cfg[cfgi]; + switch (e->type) { + case IMAGE_CFG_VERSION: + printf("VERSION %d\n", e->version); + break; + case IMAGE_CFG_BOOT_FROM: + printf("BOOTFROM %s\n", + image_boot_mode_name(e->bootfrom)); + break; + case IMAGE_CFG_DEST_ADDR: + printf("DESTADDR 0x%x\n", e->dstaddr); + break; + case IMAGE_CFG_EXEC_ADDR: + printf("EXECADDR 0x%x\n", e->execaddr); + break; + case IMAGE_CFG_NAND_BLKSZ: + printf("NANDBLKSZ 0x%x\n", e->nandblksz); + break; + case IMAGE_CFG_NAND_BADBLK_LOCATION: + printf("NANDBADBLK 0x%x\n", e->nandbadblklocation); + break; + case IMAGE_CFG_NAND_ECC_MODE: + printf("NAND_ECCMODE 0x%x\n", e->nandeccmode); + break; + case IMAGE_CFG_NAND_PAGESZ: + printf("NAND_PAGESZ 0x%x\n", e->nandpagesz); + break; + case IMAGE_CFG_BINARY: + printf("BINARY %s (%d args)\n", e->binary.file, + e->binary.nargs); + break; + case IMAGE_CFG_PAYLOAD: + printf("PAYLOAD %s\n", e->payload); + break; + case IMAGE_CFG_DATA: + printf("DATA 0x%x 0x%x\n", + e->regdata.raddr, + e->regdata.rdata); + break; + default: + printf("Error, unknown type\n"); + break; + } + } + + printf("== end configuration dump\n"); +} + +static int image_create(const char *input, const char *output, + const char *payload, const char *bootmedia, + uint32_t dstaddr, uint32_t execaddr, + int verbose) +{ + struct image_cfg_element *image_cfg; + FILE *fcfg, *outputimg; + void *image = NULL; + int version; + size_t imagesz; + int cfgn; + int ret; + + fcfg = fopen(input, "r"); + if (!fcfg) { + fprintf(stderr, "Could not open input file %s\n", + input); + return -1; + } + + image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * + sizeof(struct image_cfg_element)); + if (!image_cfg) { + fprintf(stderr, "Cannot allocate memory\n"); + fclose(fcfg); + return -1; + } + + memset(image_cfg, 0, + IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); + rewind(fcfg); + + ret = image_create_config_parse(fcfg, image_cfg, &cfgn); + if (ret) { + free(image_cfg); + return -1; + } + + image_override_payload(image_cfg, &cfgn, payload); + image_override_bootmedia(image_cfg, &cfgn, bootmedia); + image_override_dstaddr(image_cfg, &cfgn, dstaddr); + image_override_execaddr(image_cfg, &cfgn, execaddr); + + if (!image_find_option(image_cfg, cfgn, IMAGE_CFG_BOOT_FROM) || + !image_find_option(image_cfg, cfgn, IMAGE_CFG_DEST_ADDR) || + !image_find_option(image_cfg, cfgn, IMAGE_CFG_EXEC_ADDR)) { + fprintf(stderr, + "Missing information (either boot media, exec addr or dest addr)\n"); + free(image_cfg); + return -1; + } + + if (verbose) + image_dump_config(image_cfg, cfgn); + + version = image_get_version(image_cfg, cfgn); + + if (version == 0) + image = image_create_v0(image_cfg, cfgn, output, &imagesz); + else if (version == 1) + image = image_create_v1(image_cfg, cfgn, output, &imagesz); + else if (version == -1) { + fprintf(stderr, "File %s does not have the VERSION field\n", + input); + free(image_cfg); + return -1; + } + + if (!image) { + fprintf(stderr, "Could not create image\n"); + free(image_cfg); + return -1; + } + + free(image_cfg); + + outputimg = fopen(output, "w"); + if (!outputimg) { + fprintf(stderr, "Cannot open output image %s for writing\n", + output); + free(image); + return -1; + } + + ret = fwrite(image, imagesz, 1, outputimg); + if (ret != 1) { + fprintf(stderr, "Cannot write to output image %s\n", + output); + fclose(outputimg); + free(image); + return -1; + } + + fclose(outputimg); + free(image); + + return 0; +} + +enum { + ACTION_CREATE, + ACTION_EXTRACT, + ACTION_DUMP, + ACTION_HELP, +}; + +int main(int argc, char *argv[]) +{ + int action = -1, opt, verbose = 0; + const char *input = NULL, *output = NULL, + *payload = NULL, *bootmedia = NULL; + uint32_t execaddr = ADDR_INVALID, dstaddr = ADDR_INVALID; + + while ((opt = getopt(argc, argv, "hxci:o:p:m:e:d:v")) != -1) { + switch (opt) { + case 'x': + action = ACTION_EXTRACT; + break; + case 'c': + action = ACTION_CREATE; + break; + case 'i': + input = optarg; + break; + case 'o': + output = optarg; + break; + case 'p': + payload = optarg; + break; + case 'm': + bootmedia = optarg; + break; + case 'e': + execaddr = strtol(optarg, NULL, 0); + break; + case 'd': + dstaddr = strtol(optarg, NULL, 0); + break; + case 'v': + verbose = 1; + break; + case 'h': + action = ACTION_HELP; + break; + } + } + + /* We should have consumed all arguments */ + if (optind != argc) { + usage(argv[0]); + exit(1); + } + + if (action != ACTION_HELP && !input) { + fprintf(stderr, "Missing input file\n"); + usage(argv[0]); + exit(1); + } + + if ((action == ACTION_EXTRACT || action == ACTION_CREATE) && + !output) { + fprintf(stderr, "Missing output file\n"); + usage(argv[0]); + exit(1); + } + + if (action == ACTION_EXTRACT && + (bootmedia || payload || + (execaddr != ADDR_INVALID) || (dstaddr != ADDR_INVALID))) { + fprintf(stderr, + "-m, -p, -e or -d do not make sense when extracting an image"); + usage(argv[0]); + exit(1); + } + + switch (action) { + case ACTION_EXTRACT: + return image_extract(input, output); + case ACTION_CREATE: + return image_create(input, output, payload, + bootmedia, dstaddr, execaddr, + verbose); + case ACTION_HELP: + usage(argv[0]); + return 0; + default: + fprintf(stderr, "No action specified\n"); + usage(argv[0]); + exit(1); + } + + return 0; +} diff --git a/scripts/kwboot.c b/scripts/kwboot.c new file mode 100644 index 0000000000..33c94b3a8b --- /dev/null +++ b/scripts/kwboot.c @@ -0,0 +1,730 @@ +/* + * Boot a Marvell SoC, with Xmodem over UART0. + * supports Kirkwood, Dove, Armada 370, Armada XP + * + * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com> + * + * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281 + * Integrated Controller: Functional Specifications" December 2, + * 2008. Chapter 24.2 "BootROM Firmware". + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <libgen.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <stdint.h> +#include <termios.h> +#include <sys/mman.h> +#include <sys/stat.h> + +/* + * Marvell BootROM UART Sensing + */ + +static unsigned char kwboot_msg_boot[] = { + 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 +}; + +static unsigned char kwboot_msg_debug[] = { + 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 +}; + +#define KWBOOT_MSG_REQ_DELAY 1000 /* ms */ +#define KWBOOT_MSG_RSP_TIMEO 1000 /* ms */ + +/* + * Xmodem Transfers + */ + +#define SOH 1 /* sender start of block header */ +#define EOT 4 /* sender end of block transfer */ +#define ACK 6 /* target block ack */ +#define NAK 21 /* target block negative ack */ +#define CAN 24 /* target/sender transfer cancellation */ + +struct kwboot_block { + uint8_t soh; + uint8_t pnum; + uint8_t _pnum; + uint8_t data[128]; + uint8_t csum; +} __attribute((packed)); + +#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */ + +static int kwboot_verbose; + +static void +kwboot_printv(const char *fmt, ...) +{ + va_list ap; + + if (kwboot_verbose) { + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + fflush(stdout); + } +} + +static void +__spinner(void) +{ + const char seq[] = { '-', '\\', '|', '/' }; + const int div = 8; + static int state, bs; + + if (state % div == 0) { + fputc(bs, stdout); + fputc(seq[state / div % sizeof(seq)], stdout); + fflush(stdout); + } + + bs = '\b'; + state++; +} + +static void +kwboot_spinner(void) +{ + if (kwboot_verbose) + __spinner(); +} + +static void +__progress(int pct, char c) +{ + const int width = 70; + static const char *nl = ""; + static int pos; + + if (pos % width == 0) + printf("%s%3d %% [", nl, pct); + + fputc(c, stdout); + + nl = "]\n"; + pos++; + + if (pct == 100) { + while (pos++ < width) + fputc(' ', stdout); + fputs(nl, stdout); + } + + fflush(stdout); + +} + +static void +kwboot_progress(int _pct, char c) +{ + static int pct; + + if (_pct != -1) + pct = _pct; + + if (kwboot_verbose) + __progress(pct, c); +} + +static int +kwboot_tty_recv(int fd, void *buf, size_t len, int timeo) +{ + int rc, nfds; + fd_set rfds; + struct timeval tv; + ssize_t n; + + rc = -1; + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + tv.tv_sec = 0; + tv.tv_usec = timeo * 1000; + if (tv.tv_usec > 1000000) { + tv.tv_sec += tv.tv_usec / 1000000; + tv.tv_usec %= 1000000; + } + + do { + nfds = select(fd + 1, &rfds, NULL, NULL, &tv); + if (nfds < 0) + goto out; + if (!nfds) { + errno = ETIMEDOUT; + goto out; + } + + n = read(fd, buf, len); + if (n < 0) + goto out; + + buf = (char *)buf + n; + len -= n; + } while (len > 0); + + rc = 0; +out: + return rc; +} + +static int +kwboot_tty_send(int fd, const void *buf, size_t len) +{ + int rc; + ssize_t n; + + if (!buf) + return 0; + + rc = -1; + + do { + n = write(fd, buf, len); + if (n < 0) + goto out; + + buf = (char *)buf + n; + len -= n; + } while (len > 0); + + rc = tcdrain(fd); +out: + return rc; +} + +static int +kwboot_tty_send_char(int fd, unsigned char c) +{ + return kwboot_tty_send(fd, &c, 1); +} + +static speed_t +kwboot_tty_speed(int baudrate) +{ + switch (baudrate) { + case 115200: + return B115200; + case 57600: + return B57600; + case 38400: + return B38400; + case 19200: + return B19200; + case 9600: + return B9600; + } + + return -1; +} + +static int +kwboot_open_tty(const char *path, speed_t speed) +{ + int rc, fd; + struct termios tio; + + rc = -1; + + fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY); + if (fd < 0) + goto out; + + memset(&tio, 0, sizeof(tio)); + + tio.c_iflag = 0; + tio.c_cflag = CREAD|CLOCAL|CS8; + + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 10; + + cfsetospeed(&tio, speed); + cfsetispeed(&tio, speed); + + rc = tcsetattr(fd, TCSANOW, &tio); + if (rc) + goto out; + + rc = fd; +out: + if (rc < 0) { + if (fd >= 0) + close(fd); + } + + return rc; +} + +static int +kwboot_bootmsg(int tty, void *msg) +{ + int rc; + char c; + + if (msg == NULL) + kwboot_printv("Please reboot the target into UART boot mode..."); + else + kwboot_printv("Sending boot message. Please reboot the target..."); + + do { + rc = tcflush(tty, TCIOFLUSH); + if (rc) + break; + + rc = kwboot_tty_send(tty, msg, 8); + if (rc) { + usleep(KWBOOT_MSG_REQ_DELAY * 1000); + continue; + } + + rc = kwboot_tty_recv(tty, &c, 1, KWBOOT_MSG_RSP_TIMEO); + + kwboot_spinner(); + + } while (rc || c != NAK); + + kwboot_printv("\n"); + + return rc; +} + +static int +kwboot_debugmsg(int tty, void *msg) +{ + int rc; + + kwboot_printv("Sending debug message. Please reboot the target..."); + + do { + char buf[16]; + + rc = tcflush(tty, TCIOFLUSH); + if (rc) + break; + + rc = kwboot_tty_send(tty, msg, 8); + if (rc) { + usleep(KWBOOT_MSG_REQ_DELAY * 1000); + continue; + } + + rc = kwboot_tty_recv(tty, buf, 16, KWBOOT_MSG_RSP_TIMEO); + + kwboot_spinner(); + + } while (rc); + + kwboot_printv("\n"); + + return rc; +} + +static int +kwboot_xm_makeblock(struct kwboot_block *block, const void *data, + size_t size, int pnum) +{ + const size_t blksz = sizeof(block->data); + size_t n; + int i; + + block->pnum = pnum; + block->_pnum = ~block->pnum; + + n = size < blksz ? size : blksz; + memcpy(&block->data[0], data, n); + memset(&block->data[n], 0, blksz - n); + + block->csum = 0; + for (i = 0; i < n; i++) + block->csum += block->data[i]; + + return n; +} + +static int +kwboot_xm_sendblock(int fd, struct kwboot_block *block) +{ + int rc, retries; + char c; + + retries = 16; + do { + rc = kwboot_tty_send(fd, block, sizeof(*block)); + if (rc) + break; + + do { + rc = kwboot_tty_recv(fd, &c, 1, KWBOOT_BLK_RSP_TIMEO); + if (rc) + break; + + if (c != ACK && c!= NAK && c != CAN) + printf("%c", c); + + } while (c != ACK && c != NAK && c != CAN); + + if (c != ACK) + kwboot_progress(-1, '+'); + + } while (c == NAK && retries-- > 0); + + rc = -1; + + switch (c) { + case ACK: + rc = 0; + break; + case NAK: + errno = EBADMSG; + break; + case CAN: + errno = ECANCELED; + break; + default: + errno = EPROTO; + break; + } + + return rc; +} + +static int +kwboot_xmodem(int tty, const void *_data, size_t size) +{ + const uint8_t *data = _data; + int rc, pnum, N, err; + + pnum = 1; + N = 0; + + kwboot_printv("Sending boot image...\n"); + + do { + struct kwboot_block block; + int n; + + n = kwboot_xm_makeblock(&block, + data + N, size - N, + pnum++); + if (n < 0) + goto can; + + if (!n) + break; + + rc = kwboot_xm_sendblock(tty, &block); + if (rc) + goto out; + + N += n; + kwboot_progress(N * 100 / size, '.'); + } while (1); + + rc = kwboot_tty_send_char(tty, EOT); + +out: + return rc; + +can: + err = errno; + kwboot_tty_send_char(tty, CAN); + errno = err; + goto out; +} + +static int +kwboot_term_pipe(int in, int out, char *quit, int *s) +{ + ssize_t nin, nout; + char _buf[128], *buf = _buf; + + nin = read(in, buf, sizeof(buf)); + if (nin < 0) + return -1; + + if (quit) { + int i; + + for (i = 0; i < nin; i++) { + if (*buf == quit[*s]) { + (*s)++; + if (!quit[*s]) + return 0; + buf++; + nin--; + } else + while (*s > 0) { + nout = write(out, quit, *s); + if (nout <= 0) + return -1; + (*s) -= nout; + } + } + } + + while (nin > 0) { + nout = write(out, buf, nin); + if (nout <= 0) + return -1; + nin -= nout; + } + + return 0; +} + +static int +kwboot_terminal(int tty) +{ + int rc, in, s; + char *quit = "\34c"; + struct termios otio, tio; + + rc = -1; + + in = STDIN_FILENO; + if (isatty(in)) { + rc = tcgetattr(in, &otio); + if (!rc) { + tio = otio; + cfmakeraw(&tio); + rc = tcsetattr(in, TCSANOW, &tio); + } + if (rc) { + perror("tcsetattr"); + goto out; + } + + kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", + quit[0]|0100, quit[1]); + } else + in = -1; + + rc = 0; + s = 0; + + do { + fd_set rfds; + int nfds = 0; + + FD_SET(tty, &rfds); + nfds = nfds < tty ? tty : nfds; + + if (in >= 0) { + FD_SET(in, &rfds); + nfds = nfds < in ? in : nfds; + } + + nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); + if (nfds < 0) + break; + + if (FD_ISSET(tty, &rfds)) { + rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); + if (rc) + break; + } + + if (FD_ISSET(in, &rfds)) { + rc = kwboot_term_pipe(in, tty, quit, &s); + if (rc) + break; + } + } while (quit[s] != 0); + + tcsetattr(in, TCSANOW, &otio); +out: + return rc; +} + +static void * +kwboot_mmap_image(const char *path, size_t *size, int prot) +{ + int rc, fd, flags; + struct stat st; + void *img; + + rc = -1; + fd = -1; + img = NULL; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto out; + + rc = fstat(fd, &st); + if (rc) + goto out; + + flags = (prot & PROT_WRITE) ? MAP_PRIVATE : MAP_SHARED; + + img = mmap(NULL, st.st_size, prot, flags, fd, 0); + if (img == MAP_FAILED) { + img = NULL; + goto out; + } + + rc = 0; + *size = st.st_size; +out: + if (rc && img) { + munmap(img, st.st_size); + img = NULL; + } + if (fd >= 0) + close(fd); + + return img; +} + +static void +kwboot_usage(FILE *stream, char *progname) +{ + fprintf(stream, + "Usage: %s [-d | -b <image> | -D <image> ] [ -t ] [-B <baud> ] <TTY>\n", + progname); + fprintf(stream, "\n"); + fprintf(stream, + " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); + fprintf(stream, + " -D <image>: boot <image> without preamble (Dove)\n"); + fprintf(stream, " -d: enter debug mode\n"); + fprintf(stream, "\n"); + fprintf(stream, " -t: mini terminal\n"); + fprintf(stream, "\n"); + fprintf(stream, " -B <baud>: set baud rate\n"); + fprintf(stream, "\n"); +} + +int +main(int argc, char **argv) +{ + const char *ttypath, *imgpath; + int rv, rc, tty, term; + void *bootmsg; + void *debugmsg; + void *img; + size_t size; + speed_t speed; + + rv = 1; + tty = -1; + bootmsg = NULL; + debugmsg = NULL; + imgpath = NULL; + img = NULL; + term = 0; + size = 0; + speed = B115200; + + kwboot_verbose = isatty(STDOUT_FILENO); + + do { + int c = getopt(argc, argv, "hb:dtB:D:"); + if (c < 0) + break; + + switch (c) { + case 'b': + bootmsg = kwboot_msg_boot; + imgpath = optarg; + break; + + case 'D': + bootmsg = NULL; + imgpath = optarg; + break; + + case 'd': + debugmsg = kwboot_msg_debug; + break; + + case 't': + term = 1; + break; + + case 'B': + speed = kwboot_tty_speed(atoi(optarg)); + if (speed == -1) + goto usage; + break; + + case 'h': + rv = 0; + default: + goto usage; + } + } while (1); + + if (!bootmsg && !term && !debugmsg) + goto usage; + + if (argc - optind < 1) + goto usage; + + ttypath = argv[optind++]; + + tty = kwboot_open_tty(ttypath, speed); + if (tty < 0) { + perror(ttypath); + goto out; + } + + if (imgpath) { + img = kwboot_mmap_image(imgpath, &size, PROT_READ); + if (!img) { + perror(imgpath); + goto out; + } + } + + if (debugmsg) { + rc = kwboot_debugmsg(tty, debugmsg); + if (rc) { + perror("debugmsg"); + goto out; + } + } else { + rc = kwboot_bootmsg(tty, bootmsg); + if (rc) { + perror("bootmsg"); + goto out; + } + } + + if (img) { + rc = kwboot_xmodem(tty, img, size); + if (rc) { + perror("xmodem"); + goto out; + } + } + + if (term) { + rc = kwboot_terminal(tty); + if (rc && !(errno == EINTR)) { + perror("terminal"); + goto out; + } + } + + rv = 0; +out: + if (tty >= 0) + close(tty); + + if (img) + munmap(img, size); + + return rv; + +usage: + kwboot_usage(rv ? stderr : stdout, basename(argv[0])); + goto out; +} |