linux/drivers/mtd/bcm63xxpart.c
<<
>>
Prefs
   1/*
   2 * BCM63XX CFE image tag parser
   3 *
   4 * Copyright © 2006-2008  Florian Fainelli <florian@openwrt.org>
   5 *                        Mike Albon <malbon@openwrt.org>
   6 * Copyright © 2009-2010  Daniel Dickinson <openwrt@cshore.neomailbox.net>
   7 * Copyright © 2011-2012  Jonas Gorski <jonas.gorski@gmail.com>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22 *
  23 */
  24
  25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26
  27#include <linux/crc32.h>
  28#include <linux/module.h>
  29#include <linux/kernel.h>
  30#include <linux/slab.h>
  31#include <linux/vmalloc.h>
  32#include <linux/mtd/mtd.h>
  33#include <linux/mtd/partitions.h>
  34
  35#include <asm/mach-bcm63xx/bcm963xx_tag.h>
  36#include <asm/mach-bcm63xx/board_bcm963xx.h>
  37
  38#define BCM63XX_EXTENDED_SIZE   0xBFC00000      /* Extended flash address */
  39
  40#define BCM63XX_CFE_BLOCK_SIZE  0x10000         /* always at least 64KiB */
  41
  42#define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
  43
  44static int bcm63xx_detect_cfe(struct mtd_info *master)
  45{
  46        char buf[9];
  47        int ret;
  48        size_t retlen;
  49
  50        ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
  51                       (void *)buf);
  52        buf[retlen] = 0;
  53
  54        if (ret)
  55                return ret;
  56
  57        if (strncmp("cfe-v", buf, 5) == 0)
  58                return 0;
  59
  60        /* very old CFE's do not have the cfe-v string, so check for magic */
  61        ret = mtd_read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
  62                       (void *)buf);
  63        buf[retlen] = 0;
  64
  65        return strncmp("CFE1CFE1", buf, 8);
  66}
  67
  68static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
  69                                        struct mtd_partition **pparts,
  70                                        struct mtd_part_parser_data *data)
  71{
  72        /* CFE, NVRAM and global Linux are always present */
  73        int nrparts = 3, curpart = 0;
  74        struct bcm_tag *buf;
  75        struct mtd_partition *parts;
  76        int ret;
  77        size_t retlen;
  78        unsigned int rootfsaddr, kerneladdr, spareaddr;
  79        unsigned int rootfslen, kernellen, sparelen, totallen;
  80        unsigned int cfelen, nvramlen;
  81        unsigned int cfe_erasesize;
  82        int i;
  83        u32 computed_crc;
  84        bool rootfs_first = false;
  85
  86        if (bcm63xx_detect_cfe(master))
  87                return -EINVAL;
  88
  89        cfe_erasesize = max_t(uint32_t, master->erasesize,
  90                              BCM63XX_CFE_BLOCK_SIZE);
  91
  92        cfelen = cfe_erasesize;
  93        nvramlen = cfe_erasesize;
  94
  95        /* Allocate memory for buffer */
  96        buf = vmalloc(sizeof(struct bcm_tag));
  97        if (!buf)
  98                return -ENOMEM;
  99
 100        /* Get the tag */
 101        ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
 102                       (void *)buf);
 103
 104        if (retlen != sizeof(struct bcm_tag)) {
 105                vfree(buf);
 106                return -EIO;
 107        }
 108
 109        computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
 110                                offsetof(struct bcm_tag, header_crc));
 111        if (computed_crc == buf->header_crc) {
 112                char *boardid = &(buf->board_id[0]);
 113                char *tagversion = &(buf->tag_version[0]);
 114
 115                sscanf(buf->flash_image_start, "%u", &rootfsaddr);
 116                sscanf(buf->kernel_address, "%u", &kerneladdr);
 117                sscanf(buf->kernel_length, "%u", &kernellen);
 118                sscanf(buf->total_length, "%u", &totallen);
 119
 120                pr_info("CFE boot tag found with version %s and board type %s\n",
 121                        tagversion, boardid);
 122
 123                kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
 124                rootfsaddr = rootfsaddr - BCM63XX_EXTENDED_SIZE;
 125                spareaddr = roundup(totallen, master->erasesize) + cfelen;
 126
 127                if (rootfsaddr < kerneladdr) {
 128                        /* default Broadcom layout */
 129                        rootfslen = kerneladdr - rootfsaddr;
 130                        rootfs_first = true;
 131                } else {
 132                        /* OpenWrt layout */
 133                        rootfsaddr = kerneladdr + kernellen;
 134                        rootfslen = spareaddr - rootfsaddr;
 135                }
 136        } else {
 137                pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
 138                        buf->header_crc, computed_crc);
 139                kernellen = 0;
 140                rootfslen = 0;
 141                rootfsaddr = 0;
 142                spareaddr = cfelen;
 143        }
 144        sparelen = master->size - spareaddr - nvramlen;
 145
 146        /* Determine number of partitions */
 147        if (rootfslen > 0)
 148                nrparts++;
 149
 150        if (kernellen > 0)
 151                nrparts++;
 152
 153        /* Ask kernel for more memory */
 154        parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
 155        if (!parts) {
 156                vfree(buf);
 157                return -ENOMEM;
 158        }
 159
 160        /* Start building partition list */
 161        parts[curpart].name = "CFE";
 162        parts[curpart].offset = 0;
 163        parts[curpart].size = cfelen;
 164        curpart++;
 165
 166        if (kernellen > 0) {
 167                int kernelpart = curpart;
 168
 169                if (rootfslen > 0 && rootfs_first)
 170                        kernelpart++;
 171                parts[kernelpart].name = "kernel";
 172                parts[kernelpart].offset = kerneladdr;
 173                parts[kernelpart].size = kernellen;
 174                curpart++;
 175        }
 176
 177        if (rootfslen > 0) {
 178                int rootfspart = curpart;
 179
 180                if (kernellen > 0 && rootfs_first)
 181                        rootfspart--;
 182                parts[rootfspart].name = "rootfs";
 183                parts[rootfspart].offset = rootfsaddr;
 184                parts[rootfspart].size = rootfslen;
 185                if (sparelen > 0  && !rootfs_first)
 186                        parts[rootfspart].size += sparelen;
 187                curpart++;
 188        }
 189
 190        parts[curpart].name = "nvram";
 191        parts[curpart].offset = master->size - nvramlen;
 192        parts[curpart].size = nvramlen;
 193        curpart++;
 194
 195        /* Global partition "linux" to make easy firmware upgrade */
 196        parts[curpart].name = "linux";
 197        parts[curpart].offset = cfelen;
 198        parts[curpart].size = master->size - cfelen - nvramlen;
 199
 200        for (i = 0; i < nrparts; i++)
 201                pr_info("Partition %d is %s offset %llx and length %llx\n", i,
 202                        parts[i].name, parts[i].offset, parts[i].size);
 203
 204        pr_info("Spare partition is offset %x and length %x\n", spareaddr,
 205                sparelen);
 206
 207        *pparts = parts;
 208        vfree(buf);
 209
 210        return nrparts;
 211};
 212
 213static struct mtd_part_parser bcm63xx_cfe_parser = {
 214        .owner = THIS_MODULE,
 215        .parse_fn = bcm63xx_parse_cfe_partitions,
 216        .name = "bcm63xxpart",
 217};
 218
 219static int __init bcm63xx_cfe_parser_init(void)
 220{
 221        return register_mtd_parser(&bcm63xx_cfe_parser);
 222}
 223
 224static void __exit bcm63xx_cfe_parser_exit(void)
 225{
 226        deregister_mtd_parser(&bcm63xx_cfe_parser);
 227}
 228
 229module_init(bcm63xx_cfe_parser_init);
 230module_exit(bcm63xx_cfe_parser_exit);
 231
 232MODULE_LICENSE("GPL");
 233MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
 234MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
 235MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
 236MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
 237MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");
 238
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.