Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 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 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/module.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | #include <linux/mtd/mtd.h> |
| 32 | #include <linux/mtd/partitions.h> |
| 33 | |
| 34 | #include <asm/mach-bcm63xx/bcm963xx_tag.h> |
Jonas Gorski | f2d9739 | 2011-12-17 13:58:14 +0100 | [diff] [blame^] | 35 | #include <asm/mach-bcm63xx/board_bcm963xx.h> |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 36 | |
| 37 | #define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */ |
| 38 | |
Jonas Gorski | f2d9739 | 2011-12-17 13:58:14 +0100 | [diff] [blame^] | 39 | #define BCM63XX_CFE_MAGIC_OFFSET 0x4e0 |
| 40 | |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 41 | static int bcm63xx_detect_cfe(struct mtd_info *master) |
| 42 | { |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 43 | char buf[9]; |
| 44 | int ret; |
| 45 | size_t retlen; |
| 46 | |
Jonas Gorski | f2d9739 | 2011-12-17 13:58:14 +0100 | [diff] [blame^] | 47 | ret = master->read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen, |
| 48 | (void *)buf); |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 49 | buf[retlen] = 0; |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 50 | |
Jonas Gorski | f2d9739 | 2011-12-17 13:58:14 +0100 | [diff] [blame^] | 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | if (strncmp("cfe-v", buf, 5) == 0) |
| 55 | return 0; |
| 56 | |
| 57 | /* very old CFE's do not have the cfe-v string, so check for magic */ |
| 58 | ret = master->read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen, |
| 59 | (void *)buf); |
| 60 | buf[retlen] = 0; |
| 61 | |
| 62 | return strncmp("CFE1CFE1", buf, 8); |
Jonas Gorski | 70a3c16 | 2011-12-05 16:08:08 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int bcm63xx_parse_cfe_partitions(struct mtd_info *master, |
| 66 | struct mtd_partition **pparts, |
| 67 | struct mtd_part_parser_data *data) |
| 68 | { |
| 69 | /* CFE, NVRAM and global Linux are always present */ |
| 70 | int nrparts = 3, curpart = 0; |
| 71 | struct bcm_tag *buf; |
| 72 | struct mtd_partition *parts; |
| 73 | int ret; |
| 74 | size_t retlen; |
| 75 | unsigned int rootfsaddr, kerneladdr, spareaddr; |
| 76 | unsigned int rootfslen, kernellen, sparelen, totallen; |
| 77 | int namelen = 0; |
| 78 | int i; |
| 79 | char *boardid; |
| 80 | char *tagversion; |
| 81 | |
| 82 | if (bcm63xx_detect_cfe(master)) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | /* Allocate memory for buffer */ |
| 86 | buf = vmalloc(sizeof(struct bcm_tag)); |
| 87 | if (!buf) |
| 88 | return -ENOMEM; |
| 89 | |
| 90 | /* Get the tag */ |
| 91 | ret = master->read(master, master->erasesize, sizeof(struct bcm_tag), |
| 92 | &retlen, (void *)buf); |
| 93 | if (retlen != sizeof(struct bcm_tag)) { |
| 94 | vfree(buf); |
| 95 | return -EIO; |
| 96 | } |
| 97 | |
| 98 | sscanf(buf->kernel_address, "%u", &kerneladdr); |
| 99 | sscanf(buf->kernel_length, "%u", &kernellen); |
| 100 | sscanf(buf->total_length, "%u", &totallen); |
| 101 | tagversion = &(buf->tag_version[0]); |
| 102 | boardid = &(buf->board_id[0]); |
| 103 | |
| 104 | pr_info("CFE boot tag found with version %s and board type %s\n", |
| 105 | tagversion, boardid); |
| 106 | |
| 107 | kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE; |
| 108 | rootfsaddr = kerneladdr + kernellen; |
| 109 | spareaddr = roundup(totallen, master->erasesize) + master->erasesize; |
| 110 | sparelen = master->size - spareaddr - master->erasesize; |
| 111 | rootfslen = spareaddr - rootfsaddr; |
| 112 | |
| 113 | /* Determine number of partitions */ |
| 114 | namelen = 8; |
| 115 | if (rootfslen > 0) { |
| 116 | nrparts++; |
| 117 | namelen += 6; |
| 118 | } |
| 119 | if (kernellen > 0) { |
| 120 | nrparts++; |
| 121 | namelen += 6; |
| 122 | } |
| 123 | |
| 124 | /* Ask kernel for more memory */ |
| 125 | parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL); |
| 126 | if (!parts) { |
| 127 | vfree(buf); |
| 128 | return -ENOMEM; |
| 129 | } |
| 130 | |
| 131 | /* Start building partition list */ |
| 132 | parts[curpart].name = "CFE"; |
| 133 | parts[curpart].offset = 0; |
| 134 | parts[curpart].size = master->erasesize; |
| 135 | curpart++; |
| 136 | |
| 137 | if (kernellen > 0) { |
| 138 | parts[curpart].name = "kernel"; |
| 139 | parts[curpart].offset = kerneladdr; |
| 140 | parts[curpart].size = kernellen; |
| 141 | curpart++; |
| 142 | } |
| 143 | |
| 144 | if (rootfslen > 0) { |
| 145 | parts[curpart].name = "rootfs"; |
| 146 | parts[curpart].offset = rootfsaddr; |
| 147 | parts[curpart].size = rootfslen; |
| 148 | if (sparelen > 0) |
| 149 | parts[curpart].size += sparelen; |
| 150 | curpart++; |
| 151 | } |
| 152 | |
| 153 | parts[curpart].name = "nvram"; |
| 154 | parts[curpart].offset = master->size - master->erasesize; |
| 155 | parts[curpart].size = master->erasesize; |
| 156 | |
| 157 | /* Global partition "linux" to make easy firmware upgrade */ |
| 158 | curpart++; |
| 159 | parts[curpart].name = "linux"; |
| 160 | parts[curpart].offset = parts[0].size; |
| 161 | parts[curpart].size = master->size - parts[0].size - parts[3].size; |
| 162 | |
| 163 | for (i = 0; i < nrparts; i++) |
| 164 | pr_info("Partition %d is %s offset %lx and length %lx\n", i, |
| 165 | parts[i].name, (long unsigned int)(parts[i].offset), |
| 166 | (long unsigned int)(parts[i].size)); |
| 167 | |
| 168 | pr_info("Spare partition is offset %x and length %x\n", spareaddr, |
| 169 | sparelen); |
| 170 | |
| 171 | *pparts = parts; |
| 172 | vfree(buf); |
| 173 | |
| 174 | return nrparts; |
| 175 | }; |
| 176 | |
| 177 | static struct mtd_part_parser bcm63xx_cfe_parser = { |
| 178 | .owner = THIS_MODULE, |
| 179 | .parse_fn = bcm63xx_parse_cfe_partitions, |
| 180 | .name = "bcm63xxpart", |
| 181 | }; |
| 182 | |
| 183 | static int __init bcm63xx_cfe_parser_init(void) |
| 184 | { |
| 185 | return register_mtd_parser(&bcm63xx_cfe_parser); |
| 186 | } |
| 187 | |
| 188 | static void __exit bcm63xx_cfe_parser_exit(void) |
| 189 | { |
| 190 | deregister_mtd_parser(&bcm63xx_cfe_parser); |
| 191 | } |
| 192 | |
| 193 | module_init(bcm63xx_cfe_parser_init); |
| 194 | module_exit(bcm63xx_cfe_parser_exit); |
| 195 | |
| 196 | MODULE_LICENSE("GPL"); |
| 197 | MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>"); |
| 198 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 199 | MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>"); |
| 200 | MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com"); |
| 201 | MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders"); |