bcm63xxpart.c 6.5 KB

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