[U-Boot] [PATCH 1/3 v2] common/armflash: Support for ARM flash images

The ARM reference designs all use a special flash image format that stores a footer (two versions exist) at the end of the last erase block of the image in flash memory.
Version one of the footer is indicated by the magic number 0xA0FFFF9F at 12 bytes before the end of the flash block and version two is indicated by the magic number 0x464F4F54 0x464C5348 (ASCII for "FLSHFOOT") in the very last 8 bytes of the erase block.
This command driver implements support for both versions of the AFS images (the name comes from the Linux driver in drivers/mtd/afs.c) and makes it possible to list images and load an image by name into the memory with these commands:
afs - lists flash contents afs load <image> - loads image to address indicated in the image afs load <image> <addres> - loads image to a specified address
This image scheme is used on the ARM Integrator family, ARM Versatile family, ARM RealView family (not yet supported in U-Boot) and ARM Versatile Express family up to and including the new Juno board for 64 bit development.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org --- ChangeLog v1->v2: - Fix a small compiler warning about unused "secstart" variable - Add Tom's ACK --- common/Kconfig | 6 ++ common/Makefile | 1 + common/cmd_armflash.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 common/cmd_armflash.c
diff --git a/common/Kconfig b/common/Kconfig index e66277430459..4cde4b004880 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -193,6 +193,12 @@ config CMD_FLASH erase - FLASH memory protect - enable or disable FLASH write protection
+config CMD_ARMFLASH + depends on FLASH_CFI_DRIVER + bool "armflash" + help + ARM Ltd reference designs flash partition access + config CMD_NAND bool "nand" help diff --git a/common/Makefile b/common/Makefile index 7216a1392230..252fbf194b0e 100644 --- a/common/Makefile +++ b/common/Makefile @@ -60,6 +60,7 @@ obj-$(CONFIG_ENV_IS_NOWHERE) += env_nowhere.o # command obj-$(CONFIG_CMD_AES) += cmd_aes.o obj-$(CONFIG_CMD_AMBAPP) += cmd_ambapp.o +obj-$(CONFIG_CMD_ARMFLASH) += cmd_armflash.o obj-$(CONFIG_SOURCE) += cmd_source.o obj-$(CONFIG_CMD_SOURCE) += cmd_source.o obj-$(CONFIG_CMD_BDI) += cmd_bdinfo.o diff --git a/common/cmd_armflash.c b/common/cmd_armflash.c new file mode 100644 index 000000000000..1db92b05992a --- /dev/null +++ b/common/cmd_armflash.c @@ -0,0 +1,278 @@ +/* + * (C) Copyright 2015 + * Linus Walleij, Linaro + * + * Support for ARM Flash Partitions + * + * SPDX-License-Identifier: GPL-2.0+ + */ +#include <common.h> +#include <command.h> +#include <asm/io.h> + +#define MAX_REGIONS 4 +#define MAX_IMAGES 32 + +struct afs_region { + u32 load_address; + u32 size; + u32 offset; +}; + +struct afs_image { + flash_info_t *flinfo; + const char *name; + u32 version; + u32 entrypoint; + u32 attributes; + u32 region_count; + struct afs_region regions[MAX_REGIONS]; + ulong flash_mem_start; + ulong flash_mem_end; +}; + +static struct afs_image afs_images[MAX_IMAGES]; +static int num_afs_images; + +static u32 compute_crc(ulong start, u32 len) +{ + u32 sum = 0; + int i; + + if (len % 4 != 0) { + printf("bad checksumming\n"); + return 0; + } + + for (i = 0; i < len; i += 4) { + u32 val; + + val = readl((void *)start + i); + if (val > ~sum) + sum++; + sum += val; + } + return ~sum; +} + +static void parse_bank(ulong bank) +{ + int i; + ulong flstart, flend; + flash_info_t *info; + + info = &flash_info[bank]; + if (info->flash_id != FLASH_MAN_CFI) { + printf("Bank %lu: missing or unknown FLASH type\n", bank); + return; + } + if (!info->sector_count) { + printf("Bank %lu: no FLASH sectors\n", bank); + return; + } + + flstart = info->start[0]; + flend = flstart + info->size; + + for (i = 0; i < info->sector_count; ++i) { + ulong secend; + u32 foot1, foot2; + + if (ctrlc()) + break; + + if (i == info->sector_count-1) + secend = flend; + else + secend = info->start[i+1]; + + /* Check for v1 header */ + foot1 = readl((void *)secend - 0x0c); + if (foot1 == 0xA0FFFF9FU) { + struct afs_image *afi = &afs_images[num_afs_images]; + ulong imginfo; + + afi->flinfo = info; + afi->version = 1; + afi->flash_mem_start = readl((void *)secend - 0x10); + afi->flash_mem_end = readl((void *)secend - 0x14); + afi->attributes = readl((void *)secend - 0x08); + /* Adjust to even address */ + imginfo = afi->flash_mem_end + afi->flash_mem_end % 4; + /* Record as a single region */ + afi->region_count = 1; + afi->regions[0].offset = readl((void *)imginfo + 0x04); + afi->regions[0].load_address = + readl((void *)imginfo + 0x08); + afi->regions[0].size = readl((void *)imginfo + 0x0C); + afi->entrypoint = readl((void *)imginfo + 0x10); + afi->name = (const char *)imginfo + 0x14; + num_afs_images++; + } + + /* Check for v2 header */ + foot1 = readl((void *)secend - 0x04); + foot2 = readl((void *)secend - 0x08); + /* This makes up the string "HSLFTOOF" flash footer */ + if (foot1 == 0x464F4F54U && foot2 == 0x464C5348U) { + struct afs_image *afi = &afs_images[num_afs_images]; + ulong imginfo; + u32 block_start, block_end; + int j; + + afi->flinfo = info; + afi->version = readl((void *)secend - 0x0c); + imginfo = secend - 0x30 - readl((void *)secend - 0x10); + afi->name = (const char *)secend - 0x30; + + afi->entrypoint = readl((void *)imginfo+0x08); + afi->attributes = readl((void *)imginfo+0x0c); + afi->region_count = readl((void *)imginfo+0x10); + block_start = readl((void *)imginfo+0x54); + block_end = readl((void *)imginfo+0x58); + afi->flash_mem_start = afi->flinfo->start[block_start]; + afi->flash_mem_end = afi->flinfo->start[block_end]; + + /* + * Check footer CRC, the algorithm saves the inverse + * checksum as part of the summed words, and thus + * the result should be zero. + */ + if (compute_crc(imginfo + 8, 0x88) != 0) { + printf("BAD CRC on ARM image info\n"); + printf("(continuing anyway)\n"); + } + + /* Parse regions */ + for (j = 0; j < afi->region_count; j++) { + afi->regions[j].load_address = + readl((void *)imginfo+0x14 + j*0x10); + afi->regions[j].size = + readl((void *)imginfo+0x18 + j*0x10); + afi->regions[j].offset = + readl((void *)imginfo+0x1c + j*0x10); + /* + * At offset 0x20 + j*0x10 there is a region + * checksum which seems to be the running + * sum + 3, however since we anyway checksum + * the entire footer this is skipped over for + * checking here. + */ + } + num_afs_images++; + } + } +} + +static void parse_flash(void) +{ + ulong bank; + + /* We have already parsed the images in flash */ + if (num_afs_images > 0) + return; + for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) + parse_bank(bank); +} + +static void load_image(const char * const name, const ulong address) +{ + struct afs_image *afi = NULL; + int i; + + parse_flash(); + for (i = 0; i < num_afs_images; i++) { + struct afs_image *tmp = &afs_images[i]; + + if (!strcmp(tmp->name, name)) { + afi = tmp; + break; + } + } + if (!afi) { + printf("image "%s" not found in flash\n", name); + return; + } + + for (i = 0; i < afi->region_count; i++) { + ulong from, to; + + from = afi->flash_mem_start + afi->regions[i].offset; + if (address) { + to = address; + } else if (afi->regions[i].load_address) { + to = afi->regions[i].load_address; + } else { + printf("no valid load address\n"); + return; + } + + memcpy((void *)to, (void *)from, afi->regions[i].size); + + printf("loaded region %d from %08lX to %08lX, %08X bytes\n", + i, + from, + to, + afi->regions[i].size); + } +} + +static void print_images(void) +{ + int i; + + parse_flash(); + for (i = 0; i < num_afs_images; i++) { + struct afs_image *afi = &afs_images[i]; + int j; + + printf("Image: "%s" (v%d):\n", afi->name, afi->version); + printf(" Entry point: 0x%08X\n", afi->entrypoint); + printf(" Attributes: 0x%08X: ", afi->attributes); + if (afi->attributes == 0x01) + printf("ARM executable"); + if (afi->attributes == 0x08) + printf("ARM backup"); + printf("\n"); + printf(" Flash mem start: 0x%08lX\n", + afi->flash_mem_start); + printf(" Flash mem end: 0x%08lX\n", + afi->flash_mem_end); + for (j = 0; j < afi->region_count; j++) { + printf(" region %d\n" + " load address: %08X\n" + " size: %08X\n" + " offset: %08X\n", + j, + afi->regions[j].load_address, + afi->regions[j].size, + afi->regions[j].offset); + } + } +} + +static int do_afs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + if (argc == 1) { + print_images(); + } else if (argc == 3 && !strcmp(argv[1], "load")) { + load_image(argv[2], 0x0); + } else if (argc == 4 && !strcmp(argv[1], "load")) { + ulong load_addr; + + load_addr = simple_strtoul(argv[3], NULL, 16); + load_image(argv[2], load_addr); + } else { + return CMD_RET_USAGE; + } + + return 0; +} + +U_BOOT_CMD(afs, 4, 0, do_afs, "show AFS partitions", + "no arguments\n" + " - list images in flash\n" + "load <image>\n" + " - load an image to the location indicated in the header\n" + "load <image> 0x<address>\n" + " - load an image to the location specified\n");

This modifies the vexpress64 Juno configuration so that it will by default load and boot a kernel and a device tree from the images stored in the NOR flash. When we are at it, also define the proper command line for the Juno and indicate that the USB stick (/dev/sda1) is the default root file system.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org --- ChangeLog v1->v2: - Rebase on the upstream baseline - Add Tom's ACK --- include/configs/vexpress_aemv8a.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/include/configs/vexpress_aemv8a.h b/include/configs/vexpress_aemv8a.h index 9ddb594f9c54..3fda20a8f6c8 100644 --- a/include/configs/vexpress_aemv8a.h +++ b/include/configs/vexpress_aemv8a.h @@ -198,7 +198,34 @@ #define CONFIG_SYS_SDRAM_BASE PHYS_SDRAM_1
/* Initial environment variables */ -#ifdef CONFIG_TARGET_VEXPRESS64_BASE_FVP +#ifdef CONFIG_TARGET_VEXPRESS64_JUNO +/* + * Defines where the kernel and FDT exist in NOR flash and where it will + * be copied into DRAM + */ +#define CONFIG_EXTRA_ENV_SETTINGS \ + "kernel_name=Image\0" \ + "kernel_addr=0x80000000\0" \ + "fdt_name=juno\0" \ + "fdt_addr=0x83000000\0" \ + "fdt_high=0xffffffffffffffff\0" \ + "initrd_high=0xffffffffffffffff\0" \ + +/* Assume we boot with root on the first partition of a USB stick */ +#define CONFIG_BOOTARGS "console=ttyAMA0,115200n8 " \ + "root=/dev/sda1 rw " \ + "earlyprintk=pl011,0x7ff80000 debug user_debug=31 "\ + "loglevel=9" + +/* Copy the kernel and FDT to DRAM memory and boot */ +#define CONFIG_BOOTCOMMAND "afs load ${kernel_name} ${kernel_addr} ; " \ + "afs load ${fdt_name} ${fdt_addr} ; " \ + "fdt addr ${fdt_addr}; fdt resize; " \ + "booti ${kernel_addr} - ${fdt_addr}" + +#define CONFIG_BOOTDELAY 1 + +#elif CONFIG_TARGET_VEXPRESS64_BASE_FVP #define CONFIG_EXTRA_ENV_SETTINGS \ "kernel_name=uImage\0" \ "kernel_addr=0x80000000\0" \ @@ -246,6 +273,7 @@ #define CONFIG_SYS_NO_FLASH #else #define CONFIG_CMD_FLASH +#define CONFIG_CMD_ARMFLASH #define CONFIG_SYS_FLASH_CFI 1 #define CONFIG_FLASH_CFI_DRIVER 1 #define CONFIG_SYS_FLASH_BASE 0x08000000

On Sun, Apr 05, 2015 at 01:48:32AM +0200, Linus Walleij wrote:
This modifies the vexpress64 Juno configuration so that it will by default load and boot a kernel and a device tree from the images stored in the NOR flash. When we are at it, also define the proper command line for the Juno and indicate that the USB stick (/dev/sda1) is the default root file system.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org
Applied to u-boot/master, thanks!

This consolidates the flash settings for the Integrator and activates the new ARM flash image support for them so images can be loaded by name from flash.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org --- ChangeLog v1->v2: - Rebase on the upstream baseline - Add Tom's ACK --- include/configs/integrator-common.h | 22 ++++++++++++++++++++++ include/configs/integratorap.h | 16 +++------------- include/configs/integratorcp.h | 22 ++-------------------- 3 files changed, 27 insertions(+), 33 deletions(-)
diff --git a/include/configs/integrator-common.h b/include/configs/integrator-common.h index eac517aeeb1c..4362925ae1e1 100644 --- a/include/configs/integrator-common.h +++ b/include/configs/integrator-common.h @@ -86,3 +86,25 @@ CONFIG_SYS_INIT_RAM_SIZE - \ GENERATED_GBL_DATA_SIZE) #define CONFIG_SYS_INIT_SP_ADDR CONFIG_SYS_GBL_DATA_OFFSET + +/* + * FLASH and environment organization + * Top varies according to amount fitted + * Reserve top 4 blocks of flash + * - ARM Boot Monitor + * - Unused + * - SIB block + * - U-Boot environment + */ +#define CONFIG_CMD_FLASH +#define CONFIG_CMD_ARMFLASH +#define CONFIG_SYS_FLASH_CFI 1 +#define CONFIG_FLASH_CFI_DRIVER 1 +#define CONFIG_SYS_FLASH_BASE 0x24000000 +#define CONFIG_SYS_MAX_FLASH_BANKS 1 + +/* Timeout values in ticks */ +#define CONFIG_SYS_FLASH_ERASE_TOUT (2 * CONFIG_SYS_HZ) /* Erase Timeout */ +#define CONFIG_SYS_FLASH_WRITE_TOUT (2 * CONFIG_SYS_HZ) /* Write Timeout */ +#define CONFIG_SYS_FLASH_PROTECTION /* The devices have real protection */ +#define CONFIG_SYS_FLASH_EMPTY_INFO /* flinfo indicates empty blocks */ diff --git a/include/configs/integratorap.h b/include/configs/integratorap.h index edea769a921d..e168c8c9ba57 100644 --- a/include/configs/integratorap.h +++ b/include/configs/integratorap.h @@ -55,22 +55,12 @@ */ #define CONFIG_SYS_PROMPT "Integrator-AP # " /* Monitor Command Prompt */
-#define CONFIG_SYS_FLASH_BASE 0x24000000 - -/*----------------------------------------------------------------------- - * FLASH and environment organization - */ -#define CONFIG_SYS_FLASH_CFI 1 -#define CONFIG_FLASH_CFI_DRIVER 1 -#define CONFIG_ENV_IS_NOWHERE -#define CONFIG_SYS_MAX_FLASH_BANKS 1 /* max number of memory banks */ -/* timeout values are in ticks */ -#define CONFIG_SYS_FLASH_ERASE_TOUT (2*CONFIG_SYS_HZ) /* Timeout for Flash Erase */ -#define CONFIG_SYS_FLASH_WRITE_TOUT (2*CONFIG_SYS_HZ) /* Timeout for Flash Write */ +/* Flash settings */ +#define CONFIG_SYS_FLASH_SIZE 0x02000000 /* 32 MiB */ #define CONFIG_SYS_MAX_FLASH_SECT 128 +#define CONFIG_ENV_IS_NOWHERE 1 #define CONFIG_ENV_SIZE 32768
- /*----------------------------------------------------------------------- * PCI definitions */ diff --git a/include/configs/integratorcp.h b/include/configs/integratorcp.h index 608719a7eacb..7c1ef2483ea2 100644 --- a/include/configs/integratorcp.h +++ b/include/configs/integratorcp.h @@ -55,28 +55,10 @@ */ #define CONFIG_SYS_PROMPT "Integrator-CP # " /* Monitor Command Prompt */
-/* - * FLASH and environment organization - * Top varies according to amount fitted - * Reserve top 4 blocks of flash - * - ARM Boot Monitor - * - Unused - * - SIB block - * - U-Boot environment - * - * Base is always 0x24000000 - */ -#define CONFIG_SYS_FLASH_BASE 0x24000000 -#define CONFIG_SYS_FLASH_CFI 1 -#define CONFIG_FLASH_CFI_DRIVER 1 -#define CONFIG_SYS_MAX_FLASH_SECT 64 -#define CONFIG_SYS_MAX_FLASH_BANKS 1 /* max number of memory banks */ #define PHYS_FLASH_SIZE 0x01000000 /* 16MB */ -#define CONFIG_SYS_FLASH_ERASE_TOUT (2*CONFIG_SYS_HZ) /* Timeout for Flash Erase */ -#define CONFIG_SYS_FLASH_WRITE_TOUT (2*CONFIG_SYS_HZ) /* Timeout for Flash Write */ - -#define CONFIG_SYS_MONITOR_LEN 0x00100000 +#define CONFIG_SYS_MAX_FLASH_SECT 64 #define CONFIG_ENV_IS_IN_FLASH 1 +#define CONFIG_SYS_MONITOR_LEN 0x00100000
/* * Move up the U-Boot & monitor area if more flash is fitted.

On Sun, Apr 05, 2015 at 01:48:33AM +0200, Linus Walleij wrote:
This consolidates the flash settings for the Integrator and activates the new ARM flash image support for them so images can be loaded by name from flash.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org
Applied to u-boot/master, thanks!

On Sun, Apr 05, 2015 at 01:48:31AM +0200, Linus Walleij wrote:
The ARM reference designs all use a special flash image format that stores a footer (two versions exist) at the end of the last erase block of the image in flash memory.
Version one of the footer is indicated by the magic number 0xA0FFFF9F at 12 bytes before the end of the flash block and version two is indicated by the magic number 0x464F4F54 0x464C5348 (ASCII for "FLSHFOOT") in the very last 8 bytes of the erase block.
This command driver implements support for both versions of the AFS images (the name comes from the Linux driver in drivers/mtd/afs.c) and makes it possible to list images and load an image by name into the memory with these commands:
afs - lists flash contents afs load <image> - loads image to address indicated in the image afs load <image> <addres> - loads image to a specified address
This image scheme is used on the ARM Integrator family, ARM Versatile family, ARM RealView family (not yet supported in U-Boot) and ARM Versatile Express family up to and including the new Juno board for 64 bit development.
Reviewed-by: Tom Rini trini@konsulko.com Signed-off-by: Linus Walleij linus.walleij@linaro.org
Applied to u-boot/master, since Juno fails to build without this series, thanks!
participants (2)
-
Linus Walleij
-
Tom Rini