diff --git a/arch/arm/mach-mvebu/common.c b/arch/arm/mach-mvebu/common.c index fa971da..83aeb41 100644 --- a/arch/arm/mach-mvebu/common.c +++ b/arch/arm/mach-mvebu/common.c @@ -37,6 +37,10 @@ #define DOVE_SDRAM_MAP_VALID BIT(0) #define DOVE_SDRAM_LENGTH_SHIFT 16 #define DOVE_SDRAM_LENGTH_MASK (0x00f << DOVE_SDRAM_LENGTH_SHIFT) +#define DOVE_SDRAM_REGS_BASE_DECODE 0x10 + +#define DOVE_CPU_CTRL (MVEBU_REMAP_INT_REG_BASE + 0xd025c) +#define DOVE_AXI_CTRL (MVEBU_REMAP_INT_REG_BASE + 0xd0224) #define KIRKWOOD_SDRAM_BASE (IOMEM(MVEBU_REMAP_INT_REG_BASE) + 0x00000) #define KIRKWOOD_DDR_BASE_CSn(n) (0x1500 + ((n) * 0x8)) @@ -192,8 +196,33 @@ void __naked __noreturn dove_barebox_entry(void *boarddata) { + uint32_t val; + void __iomem *mcbase = mvebu_get_initial_int_reg_base() + 0x800000; + mvebu_remap_registers(); + /* + * On dove there is an additional register window that is expected to be + * located 0x800000 after the main register window. This contains the + * DDR registers. + */ + val = readl(mcbase + DOVE_SDRAM_REGS_BASE_DECODE) & 0x0000ffff; + val |= (unsigned long)DOVE_SDRAM_BASE & 0xffff0000; + writel(val, mcbase + DOVE_SDRAM_REGS_BASE_DECODE); + + /* tell the axi controller about where to find the DDR controller */ + val = readl(DOVE_AXI_CTRL) & 0x007fffff; + val |= (unsigned long)DOVE_SDRAM_BASE & 0xff800000; + writel(val, DOVE_AXI_CTRL); + + /* + * The AXI units internal space base starts at the same address as the + * DDR controller. + */ + val = readl(DOVE_CPU_CTRL) & 0xfff007ff; + val |= ((unsigned long)DOVE_SDRAM_BASE & 0xff800000) >> 12; + writel(val, DOVE_CPU_CTRL); + barebox_arm_entry(0, dove_memory_find(), boarddata); } diff --git a/arch/arm/mach-mvebu/dove.c b/arch/arm/mach-mvebu/dove.c index 1cdb7e1..37fde63 100644 --- a/arch/arm/mach-mvebu/dove.c +++ b/arch/arm/mach-mvebu/dove.c @@ -22,27 +22,6 @@ #include #include -static inline void dove_remap_mc_regs(void) -{ - void __iomem *mcboot = IOMEM(DOVE_BOOTUP_MC_REGS); - uint32_t val; - - /* remap ahb slave base */ - val = readl(DOVE_CPU_CTRL) & 0xffff0000; - val |= (DOVE_REMAP_MC_REGS & 0xffff0000) >> 16; - writel(val, DOVE_CPU_CTRL); - - /* remap axi bridge address */ - val = readl(DOVE_AXI_CTRL) & 0x007fffff; - val |= DOVE_REMAP_MC_REGS & 0xff800000; - writel(val, DOVE_AXI_CTRL); - - /* remap memory controller base address */ - val = readl(mcboot + SDRAM_REGS_BASE_DECODE) & 0x0000ffff; - val |= DOVE_REMAP_MC_REGS & 0xffff0000; - writel(val, mcboot + SDRAM_REGS_BASE_DECODE); -} - static void __noreturn dove_restart_soc(struct restart_handler *rst) { /* enable and assert RSTOUTn */ @@ -62,7 +41,6 @@ barebox_set_model("Marvell Dove"); barebox_set_hostname("dove"); - dove_remap_mc_regs(); mvebu_mbus_init(); return 0; diff --git a/arch/arm/mach-mvebu/kwbootimage.c b/arch/arm/mach-mvebu/kwbootimage.c index 8d364ce..e379d73 100644 --- a/arch/arm/mach-mvebu/kwbootimage.c +++ b/arch/arm/mach-mvebu/kwbootimage.c @@ -10,7 +10,7 @@ #include #include -static int do_bootm_kwbimage_v1(struct image_data *data) +static int do_bootm_kwbimage_v0_v1(struct image_data *data) { int fd, ret; loff_t offset; @@ -69,14 +69,21 @@ return ret; } +static struct image_handler image_handler_kwbimage_v0_handler = { + .name = "MVEBU kwbimage v0", + .bootm = do_bootm_kwbimage_v0_v1, + .filetype = filetype_kwbimage_v0, +}; + static struct image_handler image_handler_kwbimage_v1_handler = { .name = "MVEBU kwbimage v1", - .bootm = do_bootm_kwbimage_v1, + .bootm = do_bootm_kwbimage_v0_v1, .filetype = filetype_kwbimage_v1, }; static int mvebu_register_kwbimage_image_handler(void) { + register_image_handler(&image_handler_kwbimage_v0_handler); register_image_handler(&image_handler_kwbimage_v1_handler); return 0; diff --git a/arch/arm/mach-omap/am33xx_bbu_nand.c b/arch/arm/mach-omap/am33xx_bbu_nand.c index 7785d40..6fc6e7e 100644 --- a/arch/arm/mach-omap/am33xx_bbu_nand.c +++ b/arch/arm/mach-omap/am33xx_bbu_nand.c @@ -20,6 +20,7 @@ #include #include #include +#include #include struct nand_bbu_handler { @@ -28,39 +29,6 @@ int num_devicefiles; }; -static int write_image(const char *devfile, const void *image, size_t size) -{ - int fd = 0; - int ret = 0; - - fd = open(devfile, O_WRONLY); - if (fd < 0) { - pr_err("could not open %s: %s\n", devfile, - errno_str()); - return fd; - } - - ret = erase(fd, ERASE_SIZE_ALL, 0); - if (ret < 0) { - pr_err("could not erase %s: %s\n", devfile, - errno_str()); - close(fd); - return ret; - } - - ret = write(fd, image, size); - if (ret < 0) { - pr_err("could not write to fd %s: %s\n", devfile, - errno_str()); - close(fd); - return ret; - } - - close(fd); - - return 0; -} - /* * Upate given nand partitions with an image */ @@ -80,12 +48,12 @@ /* check if the devicefile has been overwritten */ if (strcmp(data->devicefile, nh->devicefile[0]) != 0) { - ret = write_image(data->devicefile, image, size); + ret = write_file_flash(data->devicefile, image, size); if (ret != 0) return ret; } else { for (i = 0; i < nh->num_devicefiles; i++) { - ret = write_image(nh->devicefile[i], image, size); + ret = write_file_flash(nh->devicefile[i], image, size); if (ret != 0) return ret; } diff --git a/common/filetype.c b/common/filetype.c index 444ec14..bb807df 100644 --- a/common/filetype.c +++ b/common/filetype.c @@ -65,7 +65,8 @@ [filetype_exe] = { "MS-DOS executable", "exe" }, [filetype_mxs_bootstream] = { "Freescale MXS bootstream", "mxsbs" }, [filetype_socfpga_xload] = { "SoCFPGA prebootloader image", "socfpga-xload" }, - [filetype_kwbimage_v1] = { "MVEBU kwbimage (v1)", "kwb" }, + [filetype_kwbimage_v0] = { "MVEBU kwbimage (v0)", "kwb0" }, + [filetype_kwbimage_v1] = { "MVEBU kwbimage (v1)", "kwb1" }, [filetype_android_sparse] = { "Android sparse image", "sparse" }, [filetype_arm64_linux_image] = { "ARM aarch64 Linux image", "aarch64-linux" }, }; @@ -302,10 +303,21 @@ if ((buf8[0] == 0x5a || buf8[0] == 0x69 || buf8[0] == 0x78 || buf8[0] == 0x8b || buf8[0] == 0x9c) && buf8[0x1] == 0 && buf8[0x2] == 0 && buf8[0x3] == 0 && - buf8[0x8] == 1 && buf8[0x18] == 0 && buf8[0x1b] == 0 && - buf8[0x1c] == 0 && buf8[0x1d] == 0 && - (buf8[0x1e] == 0 || buf8[0x1e] == 1)) - return filetype_kwbimage_v1; + buf8[0x18] == 0 && buf8[0x1b] == 0 && buf8[0x1c] == 0) { + unsigned char sum = 0; + int i; + + for (i = 0; i <= 0x1e; ++i) + sum += buf8[i]; + + if (sum == buf8[0x1f] && buf8[0x8] == 0) + return filetype_kwbimage_v0; + + if (sum == buf8[0x1f] && + buf8[0x8] == 1 && buf8[0x1d] == 0 && + (buf8[0x1e] == 0 || buf8[0x1e] == 1)) + return filetype_kwbimage_v1; + } if (is_sparse_image(_buf)) return filetype_android_sparse; diff --git a/include/filetype.h b/include/filetype.h index 9986938..d9963a2 100644 --- a/include/filetype.h +++ b/include/filetype.h @@ -38,6 +38,7 @@ filetype_xz_compressed, filetype_mxs_bootstream, filetype_socfpga_xload, + filetype_kwbimage_v0, filetype_kwbimage_v1, filetype_android_sparse, filetype_arm64_linux_image, diff --git a/include/libfile.h b/include/libfile.h index fd2fade..2c5eef7 100644 --- a/include/libfile.h +++ b/include/libfile.h @@ -12,6 +12,7 @@ loff_t max_size); int write_file(const char *filename, const void *buf, size_t size); +int write_file_flash(const char *filename, const void *buf, size_t size); int copy_file(const char *src, const char *dst, int verbose); diff --git a/lib/libfile.c b/lib/libfile.c index b7db22d..d22519b 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -251,6 +251,39 @@ EXPORT_SYMBOL(write_file); /** + * write_file_flash - write a buffer to a file backed by flash + * @filename: The filename to write + * @size: The size of the buffer + * + * Functional this is identical to write_file but calls erase() before writing. + * + * Return: 0 for success or negative error value + */ +int write_file_flash(const char *filename, const void *buf, size_t size) +{ + int fd, ret; + + fd = open(filename, O_WRONLY); + if (fd < 0) + return fd; + + ret = erase(fd, size, 0); + if (ret < 0) + goto out_close; + + ret = write_full(fd, buf, size); + +out_close: + close(fd); + + if (ret < 0) + return ret; + + return 0; +} +EXPORT_SYMBOL(write_file_flash); + +/** * copy_file - Copy a file * @src: The source filename * @dst: The destination filename diff --git a/scripts/kwboot.c b/scripts/kwboot.c index db177ce..df52144 100644 --- a/scripts/kwboot.c +++ b/scripts/kwboot.c @@ -602,12 +602,24 @@ return rc; } +static unsigned char crc(const unsigned char *img, size_t len) +{ + unsigned char ret = 0; + size_t i; + + for (i = 0; i < len; ++i) + ret += img[i]; + + return ret; +} + static int kwboot_check_image(unsigned char *img, size_t size) { size_t i; size_t header_size, image_size, image_offset; unsigned char csum = 0; + unsigned char imgversion; if (size < 0x20) { fprintf(stderr, @@ -636,7 +648,8 @@ return 1; } - if (img[0x8] != 1) { + imgversion = img[0x8]; + if (imgversion > 1) { fprintf(stderr, "Unknown version: 0x%hhx\n", img[0x8]); return 1; } @@ -646,7 +659,15 @@ image_offset = img[0xc] | (img[0xd] << 8) | (img[0xe] << 16) | (img[0xf] << 24); - header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8); + if (imgversion == 0) { + /* Image format 0 */ + header_size = + img[0x1e] * 0x200 /* header extensions */ + + img[0x1d] * 0x800 /* binary header extensions */; + } else { + /* Image format 1 */ + header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8); + } if (header_size > image_offset) { fprintf(stderr, "Header (%zu) expands over image start (%zu)\n", @@ -660,16 +681,38 @@ return 1; } - for (i = 0; i < header_size; ++i) - csum += img[i]; - csum -= img[0x1f]; + if (imgversion == 0) { + /* check Main Header */ + csum = crc(img, 0x1f); + if (csum != img[0x1f]) { + fprintf(stderr, + "Main Header checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n", + img[0x1f], csum); + return 1; + } - if (csum != img[0x1f]) { - fprintf(stderr, - "Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n", - img[0x1f], csum); - return 1; + /* check Header Extensions */ + for (i = 0; i < img[0x1e]; ++i) { + csum = crc(img + 0x20 + i * 0x200, 0x1df); + if (csum != img[i * 0x200 + 0x1ff]) { + fprintf(stderr, + "Extension Header #%zu checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n", + i, img[i * 0x200 + 0x1ff], csum); + return 1; + } + } + } else { + csum = crc(img, header_size); + csum -= img[0x1f]; + + if (csum != img[0x1f]) { + fprintf(stderr, + "Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n", + img[0x1f], csum); + return 1; + } + } return 0;