bcm63xxpart.c 6.3 KB

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