bcm63xxpart.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #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/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <asm/mach-bcm63xx/bcm963xx_tag.h>
  33. #include <asm/mach-bcm63xx/board_bcm963xx.h>
  34. #define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
  35. #define BCM63XX_MIN_CFE_SIZE 0x10000 /* always at least 64KiB */
  36. #define BCM63XX_MIN_NVRAM_SIZE 0x10000 /* 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. int namelen = 0;
  70. int i;
  71. u32 computed_crc;
  72. if (bcm63xx_detect_cfe(master))
  73. return -EINVAL;
  74. cfelen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_CFE_SIZE);
  75. nvramlen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_NVRAM_SIZE);
  76. /* Allocate memory for buffer */
  77. buf = vmalloc(sizeof(struct bcm_tag));
  78. if (!buf)
  79. return -ENOMEM;
  80. /* Get the tag */
  81. ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
  82. (void *)buf);
  83. if (retlen != sizeof(struct bcm_tag)) {
  84. vfree(buf);
  85. return -EIO;
  86. }
  87. computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
  88. offsetof(struct bcm_tag, header_crc));
  89. if (computed_crc == buf->header_crc) {
  90. char *boardid = &(buf->board_id[0]);
  91. char *tagversion = &(buf->tag_version[0]);
  92. sscanf(buf->kernel_address, "%u", &kerneladdr);
  93. sscanf(buf->kernel_length, "%u", &kernellen);
  94. sscanf(buf->total_length, "%u", &totallen);
  95. pr_info("CFE boot tag found with version %s and board type %s\n",
  96. tagversion, boardid);
  97. kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
  98. rootfsaddr = kerneladdr + kernellen;
  99. spareaddr = roundup(totallen, master->erasesize) + cfelen;
  100. sparelen = master->size - spareaddr - nvramlen;
  101. rootfslen = spareaddr - rootfsaddr;
  102. } else {
  103. pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
  104. buf->header_crc, computed_crc);
  105. kernellen = 0;
  106. rootfslen = 0;
  107. rootfsaddr = 0;
  108. spareaddr = cfelen;
  109. sparelen = master->size - cfelen - nvramlen;
  110. }
  111. /* Determine number of partitions */
  112. namelen = 8;
  113. if (rootfslen > 0) {
  114. nrparts++;
  115. namelen += 6;
  116. }
  117. if (kernellen > 0) {
  118. nrparts++;
  119. namelen += 6;
  120. }
  121. /* Ask kernel for more memory */
  122. parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
  123. if (!parts) {
  124. vfree(buf);
  125. return -ENOMEM;
  126. }
  127. /* Start building partition list */
  128. parts[curpart].name = "CFE";
  129. parts[curpart].offset = 0;
  130. parts[curpart].size = cfelen;
  131. curpart++;
  132. if (kernellen > 0) {
  133. parts[curpart].name = "kernel";
  134. parts[curpart].offset = kerneladdr;
  135. parts[curpart].size = kernellen;
  136. curpart++;
  137. }
  138. if (rootfslen > 0) {
  139. parts[curpart].name = "rootfs";
  140. parts[curpart].offset = rootfsaddr;
  141. parts[curpart].size = rootfslen;
  142. if (sparelen > 0)
  143. parts[curpart].size += sparelen;
  144. curpart++;
  145. }
  146. parts[curpart].name = "nvram";
  147. parts[curpart].offset = master->size - nvramlen;
  148. parts[curpart].size = nvramlen;
  149. /* Global partition "linux" to make easy firmware upgrade */
  150. curpart++;
  151. parts[curpart].name = "linux";
  152. parts[curpart].offset = cfelen;
  153. parts[curpart].size = master->size - cfelen - nvramlen;
  154. for (i = 0; i < nrparts; i++)
  155. pr_info("Partition %d is %s offset %lx and length %lx\n", i,
  156. parts[i].name, (long unsigned int)(parts[i].offset),
  157. (long unsigned int)(parts[i].size));
  158. pr_info("Spare partition is offset %x and length %x\n", spareaddr,
  159. sparelen);
  160. *pparts = parts;
  161. vfree(buf);
  162. return nrparts;
  163. };
  164. static struct mtd_part_parser bcm63xx_cfe_parser = {
  165. .owner = THIS_MODULE,
  166. .parse_fn = bcm63xx_parse_cfe_partitions,
  167. .name = "bcm63xxpart",
  168. };
  169. static int __init bcm63xx_cfe_parser_init(void)
  170. {
  171. return register_mtd_parser(&bcm63xx_cfe_parser);
  172. }
  173. static void __exit bcm63xx_cfe_parser_exit(void)
  174. {
  175. deregister_mtd_parser(&bcm63xx_cfe_parser);
  176. }
  177. module_init(bcm63xx_cfe_parser_init);
  178. module_exit(bcm63xx_cfe_parser_exit);
  179. MODULE_LICENSE("GPL");
  180. MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
  181. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  182. MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
  183. MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
  184. MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");