Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * BCM63XX CFE image tag parser
0004  *
0005  * Copyright © 2006-2008  Florian Fainelli <florian@openwrt.org>
0006  *            Mike Albon <malbon@openwrt.org>
0007  * Copyright © 2009-2010  Daniel Dickinson <openwrt@cshore.neomailbox.net>
0008  * Copyright © 2011-2013  Jonas Gorski <jonas.gorski@gmail.com>
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/bcm963xx_nvram.h>
0014 #include <linux/bcm963xx_tag.h>
0015 #include <linux/crc32.h>
0016 #include <linux/module.h>
0017 #include <linux/kernel.h>
0018 #include <linux/sizes.h>
0019 #include <linux/slab.h>
0020 #include <linux/vmalloc.h>
0021 #include <linux/mtd/mtd.h>
0022 #include <linux/mtd/partitions.h>
0023 #include <linux/of.h>
0024 
0025 #ifdef CONFIG_MIPS
0026 #include <asm/bootinfo.h>
0027 #include <asm/fw/cfe/cfe_api.h>
0028 #endif /* CONFIG_MIPS */
0029 
0030 #define BCM963XX_CFE_BLOCK_SIZE     SZ_64K  /* always at least 64KiB */
0031 
0032 #define BCM963XX_CFE_MAGIC_OFFSET   0x4e0
0033 #define BCM963XX_CFE_VERSION_OFFSET 0x570
0034 #define BCM963XX_NVRAM_OFFSET       0x580
0035 
0036 /* Ensure strings read from flash structs are null terminated */
0037 #define STR_NULL_TERMINATE(x) \
0038     do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
0039 
0040 static inline int bcm63xx_detect_cfe(void)
0041 {
0042     int ret = 0;
0043 
0044 #ifdef CONFIG_MIPS
0045     ret = (fw_arg3 == CFE_EPTSEAL);
0046 #endif /* CONFIG_MIPS */
0047 
0048     return ret;
0049 }
0050 
0051 static int bcm63xx_read_nvram(struct mtd_info *master,
0052     struct bcm963xx_nvram *nvram)
0053 {
0054     u32 actual_crc, expected_crc;
0055     size_t retlen;
0056     int ret;
0057 
0058     /* extract nvram data */
0059     ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
0060             &retlen, (void *)nvram);
0061     if (ret)
0062         return ret;
0063 
0064     ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
0065     if (ret)
0066         pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
0067             expected_crc, actual_crc);
0068 
0069     if (!nvram->psi_size)
0070         nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
0071 
0072     return 0;
0073 }
0074 
0075 static const char * const bcm63xx_cfe_part_types[] = {
0076     "bcm963xx-imagetag",
0077     NULL,
0078 };
0079 
0080 static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
0081     const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
0082 {
0083     struct mtd_partition *parts;
0084     int nrparts = 3, curpart = 0;
0085     unsigned int cfelen, nvramlen;
0086     unsigned int cfe_erasesize;
0087     int i;
0088 
0089     cfe_erasesize = max_t(uint32_t, master->erasesize,
0090                   BCM963XX_CFE_BLOCK_SIZE);
0091 
0092     cfelen = cfe_erasesize;
0093     nvramlen = nvram->psi_size * SZ_1K;
0094     nvramlen = roundup(nvramlen, cfe_erasesize);
0095 
0096     parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
0097     if (!parts)
0098         return -ENOMEM;
0099 
0100     /* Start building partition list */
0101     parts[curpart].name = "CFE";
0102     parts[curpart].offset = 0;
0103     parts[curpart].size = cfelen;
0104     curpart++;
0105 
0106     parts[curpart].name = "nvram";
0107     parts[curpart].offset = master->size - nvramlen;
0108     parts[curpart].size = nvramlen;
0109     curpart++;
0110 
0111     /* Global partition "linux" to make easy firmware upgrade */
0112     parts[curpart].name = "linux";
0113     parts[curpart].offset = cfelen;
0114     parts[curpart].size = master->size - cfelen - nvramlen;
0115     parts[curpart].types = bcm63xx_cfe_part_types;
0116 
0117     for (i = 0; i < nrparts; i++)
0118         pr_info("Partition %d is %s offset %llx and length %llx\n", i,
0119             parts[i].name, parts[i].offset, parts[i].size);
0120 
0121     *pparts = parts;
0122 
0123     return nrparts;
0124 }
0125 
0126 static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
0127                     const struct mtd_partition **pparts,
0128                     struct mtd_part_parser_data *data)
0129 {
0130     struct bcm963xx_nvram *nvram = NULL;
0131     int ret;
0132 
0133     if (!bcm63xx_detect_cfe())
0134         return -EINVAL;
0135 
0136     nvram = vzalloc(sizeof(*nvram));
0137     if (!nvram)
0138         return -ENOMEM;
0139 
0140     ret = bcm63xx_read_nvram(master, nvram);
0141     if (ret)
0142         goto out;
0143 
0144     if (!mtd_type_is_nand(master))
0145         ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
0146     else
0147         ret = -EINVAL;
0148 
0149 out:
0150     vfree(nvram);
0151     return ret;
0152 };
0153 
0154 static const struct of_device_id parse_bcm63xx_cfe_match_table[] = {
0155     { .compatible = "brcm,bcm963xx-cfe-nor-partitions" },
0156     {},
0157 };
0158 MODULE_DEVICE_TABLE(of, parse_bcm63xx_cfe_match_table);
0159 
0160 static struct mtd_part_parser bcm63xx_cfe_parser = {
0161     .parse_fn = bcm63xx_parse_cfe_partitions,
0162     .name = "bcm63xxpart",
0163     .of_match_table = parse_bcm63xx_cfe_match_table,
0164 };
0165 module_mtd_part_parser(bcm63xx_cfe_parser);
0166 
0167 MODULE_LICENSE("GPL");
0168 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
0169 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
0170 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
0171 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
0172 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");