linux/drivers/mtd/parsers/bcm63xxpart.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * BCM63XX CFE image tag parser
   4 *
   5 * Copyright © 2006-2008  Florian Fainelli <florian@openwrt.org>
   6 *                        Mike Albon <malbon@openwrt.org>
   7 * Copyright © 2009-2010  Daniel Dickinson <openwrt@cshore.neomailbox.net>
   8 * Copyright © 2011-2013  Jonas Gorski <jonas.gorski@gmail.com>
   9 */
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/bcm963xx_nvram.h>
  14#include <linux/bcm963xx_tag.h>
  15#include <linux/crc32.h>
  16#include <linux/module.h>
  17#include <linux/kernel.h>
  18#include <linux/sizes.h>
  19#include <linux/slab.h>
  20#include <linux/vmalloc.h>
  21#include <linux/mtd/mtd.h>
  22#include <linux/mtd/partitions.h>
  23#include <linux/of.h>
  24
  25#define BCM963XX_CFE_BLOCK_SIZE         SZ_64K  /* always at least 64KiB */
  26
  27#define BCM963XX_CFE_MAGIC_OFFSET       0x4e0
  28#define BCM963XX_CFE_VERSION_OFFSET     0x570
  29#define BCM963XX_NVRAM_OFFSET           0x580
  30
  31/* Ensure strings read from flash structs are null terminated */
  32#define STR_NULL_TERMINATE(x) \
  33        do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
  34
  35static int bcm63xx_detect_cfe(struct mtd_info *master)
  36{
  37        char buf[9];
  38        int ret;
  39        size_t retlen;
  40
  41        ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
  42                       (void *)buf);
  43        buf[retlen] = 0;
  44
  45        if (ret)
  46                return ret;
  47
  48        if (strncmp("cfe-v", buf, 5) == 0)
  49                return 0;
  50
  51        /* very old CFE's do not have the cfe-v string, so check for magic */
  52        ret = mtd_read(master, BCM963XX_CFE_MAGIC_OFFSET, 8, &retlen,
  53                       (void *)buf);
  54        buf[retlen] = 0;
  55
  56        return strncmp("CFE1CFE1", buf, 8);
  57}
  58
  59static int bcm63xx_read_nvram(struct mtd_info *master,
  60        struct bcm963xx_nvram *nvram)
  61{
  62        u32 actual_crc, expected_crc;
  63        size_t retlen;
  64        int ret;
  65
  66        /* extract nvram data */
  67        ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
  68                        &retlen, (void *)nvram);
  69        if (ret)
  70                return ret;
  71
  72        ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
  73        if (ret)
  74                pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
  75                        expected_crc, actual_crc);
  76
  77        if (!nvram->psi_size)
  78                nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
  79
  80        return 0;
  81}
  82
  83static const char * const bcm63xx_cfe_part_types[] = {
  84        "bcm963xx-imagetag",
  85        NULL,
  86};
  87
  88static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
  89        const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
  90{
  91        struct mtd_partition *parts;
  92        int nrparts = 3, curpart = 0;
  93        unsigned int cfelen, nvramlen;
  94        unsigned int cfe_erasesize;
  95        int i;
  96
  97        cfe_erasesize = max_t(uint32_t, master->erasesize,
  98                              BCM963XX_CFE_BLOCK_SIZE);
  99
 100        cfelen = cfe_erasesize;
 101        nvramlen = nvram->psi_size * SZ_1K;
 102        nvramlen = roundup(nvramlen, cfe_erasesize);
 103
 104        parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
 105        if (!parts)
 106                return -ENOMEM;
 107
 108        /* Start building partition list */
 109        parts[curpart].name = "CFE";
 110        parts[curpart].offset = 0;
 111        parts[curpart].size = cfelen;
 112        curpart++;
 113
 114        parts[curpart].name = "nvram";
 115        parts[curpart].offset = master->size - nvramlen;
 116        parts[curpart].size = nvramlen;
 117        curpart++;
 118
 119        /* Global partition "linux" to make easy firmware upgrade */
 120        parts[curpart].name = "linux";
 121        parts[curpart].offset = cfelen;
 122        parts[curpart].size = master->size - cfelen - nvramlen;
 123        parts[curpart].types = bcm63xx_cfe_part_types;
 124
 125        for (i = 0; i < nrparts; i++)
 126                pr_info("Partition %d is %s offset %llx and length %llx\n", i,
 127                        parts[i].name, parts[i].offset, parts[i].size);
 128
 129        *pparts = parts;
 130
 131        return nrparts;
 132}
 133
 134static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
 135                                        const struct mtd_partition **pparts,
 136                                        struct mtd_part_parser_data *data)
 137{
 138        struct bcm963xx_nvram *nvram = NULL;
 139        int ret;
 140
 141        if (bcm63xx_detect_cfe(master))
 142                return -EINVAL;
 143
 144        nvram = vzalloc(sizeof(*nvram));
 145        if (!nvram)
 146                return -ENOMEM;
 147
 148        ret = bcm63xx_read_nvram(master, nvram);
 149        if (ret)
 150                goto out;
 151
 152        if (!mtd_type_is_nand(master))
 153                ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
 154        else
 155                ret = -EINVAL;
 156
 157out:
 158        vfree(nvram);
 159        return ret;
 160};
 161
 162static const struct of_device_id parse_bcm63xx_cfe_match_table[] = {
 163        { .compatible = "brcm,bcm963xx-cfe-nor-partitions" },
 164        {},
 165};
 166MODULE_DEVICE_TABLE(of, parse_bcm63xx_cfe_match_table);
 167
 168static struct mtd_part_parser bcm63xx_cfe_parser = {
 169        .parse_fn = bcm63xx_parse_cfe_partitions,
 170        .name = "bcm63xxpart",
 171        .of_match_table = parse_bcm63xx_cfe_match_table,
 172};
 173module_mtd_part_parser(bcm63xx_cfe_parser);
 174
 175MODULE_LICENSE("GPL");
 176MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
 177MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
 178MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
 179MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
 180MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");
 181