From patchwork Sat Apr 4 23:48:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 46776 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 108BE216D1 for ; Sat, 4 Apr 2015 23:48:51 +0000 (UTC) Received: by wghm4 with SMTP id m4sf271981wgh.2 for ; Sat, 04 Apr 2015 16:48:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:cc:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=nwfSjpvidj8FBp5RBPp7DKduwbbrRt4RiuVKl72gZ64=; b=j2u4k09qlvdFntVt1VeXXPfi74U+6AOzWpdfSnyHfw6iwOibOUwMfvu4J2QugFRzvv D4C34Mx+lIoe8v7C7s8RPPGPJnYjS90xZ6gl/nyrdRHOdkcPJt7vBkpUJCWN8qfG0MV6 9Q1orGl0geRlnVUwOrIuZmaOTjI06AB+1/eSDeP0P6wTS/BcH73b8OjPZVGF3K0ulWmv 2gwxLtXofZOccEI5qHWfnQ75S5An3kDZFuAppYLke1una1KGX7AS77blv8fsOJwmG62+ h0ySzOC9/JstqIX247g1cCB7PmtQlkhEyMTx+bTv1DjnBV6nqrRwwX7eowna7K9dhKxo 4icQ== X-Gm-Message-State: ALoCoQntXy1NRQYrVULcky8aUoXmQeOFhAb5qT4yoBW0JmV5hOTQMGjxtPUoOmwdL7r3bOaXhx3q X-Received: by 10.112.142.1 with SMTP id rs1mr1991009lbb.19.1428191330014; Sat, 04 Apr 2015 16:48:50 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.5.2 with SMTP id ci2ls403773lad.38.gmail; Sat, 04 Apr 2015 16:48:49 -0700 (PDT) X-Received: by 10.112.14.208 with SMTP id r16mr7698198lbc.6.1428191329729; Sat, 04 Apr 2015 16:48:49 -0700 (PDT) Received: from mail-la0-f42.google.com (mail-la0-f42.google.com. [209.85.215.42]) by mx.google.com with ESMTPS id yl9si79943lbb.93.2015.04.04.16.48.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Apr 2015 16:48:49 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.42 as permitted sender) client-ip=209.85.215.42; Received: by lagg8 with SMTP id g8so1061736lag.1 for ; Sat, 04 Apr 2015 16:48:49 -0700 (PDT) X-Received: by 10.152.116.11 with SMTP id js11mr7794955lab.106.1428191329617; Sat, 04 Apr 2015 16:48:49 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.57.201 with SMTP id k9csp2637137lbq; Sat, 4 Apr 2015 16:48:48 -0700 (PDT) X-Received: by 10.194.157.39 with SMTP id wj7mr17986288wjb.57.1428191328550; Sat, 04 Apr 2015 16:48:48 -0700 (PDT) Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id p6si251854wic.104.2015.04.04.16.48.47; Sat, 04 Apr 2015 16:48:48 -0700 (PDT) Received-SPF: none (google.com: u-boot-bounces@lists.denx.de does not designate permitted sender hosts) client-ip=85.214.87.163; Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id F3C914A03B; Sun, 5 Apr 2015 01:48:46 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nKarPsPBuXfN; Sun, 5 Apr 2015 01:48:46 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 60787B37B1; Sun, 5 Apr 2015 01:48:46 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C8389B37B1 for ; Sun, 5 Apr 2015 01:48:41 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FybgrRu8z5UM for ; Sun, 5 Apr 2015 01:48:41 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com [209.85.215.48]) by theia.denx.de (Postfix) with ESMTPS id 32D004A01F for ; Sun, 5 Apr 2015 01:48:38 +0200 (CEST) Received: by lahf3 with SMTP id f3so1048054lah.2 for ; Sat, 04 Apr 2015 16:48:37 -0700 (PDT) X-Received: by 10.152.5.36 with SMTP id p4mr7958211lap.7.1428191317768; Sat, 04 Apr 2015 16:48:37 -0700 (PDT) Received: from localhost.lan ([80.202.93.41]) by mx.google.com with ESMTPSA id lv10sm20262lac.24.2015.04.04.16.48.35 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Apr 2015 16:48:36 -0700 (PDT) From: Linus Walleij To: u-boot@lists.denx.de, Albert Aribaud , Tom Rini Date: Sun, 5 Apr 2015 01:48:31 +0200 Message-Id: <1428191313-3945-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 1.9.3 Cc: Steve Rae , u-boot-review@google.com Subject: [U-Boot] [PATCH 1/3 v2] common/armflash: Support for ARM flash images X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.42 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 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 - loads image to address indicated in the image afs load - 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 Signed-off-by: Linus Walleij --- 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 +#include +#include + +#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 \n" + " - load an image to the location indicated in the header\n" + "load 0x
\n" + " - load an image to the location specified\n");