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