bcm63xxpart.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/bcm963xx_nvram.h>
  26. #include <linux/bcm963xx_tag.h>
  27. #include <linux/crc32.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sizes.h>
  31. #include <linux/slab.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/partitions.h>
  35. #define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
  36. #define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
  37. #define BCM963XX_CFE_VERSION_OFFSET 0x570
  38. #define BCM963XX_NVRAM_OFFSET 0x580
  39. /* Ensure strings read from flash structs are null terminated */
  40. #define STR_NULL_TERMINATE(x) \
  41. do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
  42. static int bcm63xx_detect_cfe(struct mtd_info *master)
  43. {
  44. char buf[9];
  45. int ret;
  46. size_t retlen;
  47. ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
  48. (void *)buf);
  49. buf[retlen] = 0;
  50. if (ret)
  51. return ret;
  52. if (strncmp("cfe-v", buf, 5) == 0)
  53. return 0;
  54. /* very old CFE's do not have the cfe-v string, so check for magic */
  55. ret = mtd_read(master, BCM963XX_CFE_MAGIC_OFFSET, 8, &retlen,
  56. (void *)buf);
  57. buf[retlen] = 0;
  58. return strncmp("CFE1CFE1", buf, 8);
  59. }
  60. static int bcm63xx_read_nvram(struct mtd_info *master,
  61. struct bcm963xx_nvram *nvram)
  62. {
  63. u32 actual_crc, expected_crc;
  64. size_t retlen;
  65. int ret;
  66. /* extract nvram data */
  67. ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
  68. &retlen, (void *)nvram);
  69. if (ret)
  70. return ret;
  71. ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
  72. if (ret)
  73. pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
  74. expected_crc, actual_crc);
  75. if (!nvram->psi_size)
  76. nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
  77. return 0;
  78. }
  79. static int bcm63xx_read_image_tag(struct mtd_info *master, const char *name,
  80. loff_t tag_offset, struct bcm_tag *buf)
  81. {
  82. int ret;
  83. size_t retlen;
  84. u32 computed_crc;
  85. ret = mtd_read(master, tag_offset, sizeof(*buf), &retlen, (void *)buf);
  86. if (ret)
  87. return ret;
  88. if (retlen != sizeof(*buf))
  89. return -EIO;
  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. STR_NULL_TERMINATE(buf->board_id);
  94. STR_NULL_TERMINATE(buf->tag_version);
  95. pr_info("%s: CFE image tag found at 0x%llx with version %s, board type %s\n",
  96. name, tag_offset, buf->tag_version, buf->board_id);
  97. return 0;
  98. }
  99. pr_warn("%s: CFE image tag at 0x%llx CRC invalid (expected %08x, actual %08x)\n",
  100. name, tag_offset, buf->header_crc, computed_crc);
  101. return 1;
  102. }
  103. static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
  104. const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
  105. {
  106. /* CFE, NVRAM and global Linux are always present */
  107. int nrparts = 3, curpart = 0;
  108. struct bcm_tag *buf = NULL;
  109. struct mtd_partition *parts;
  110. int ret;
  111. unsigned int rootfsaddr, kerneladdr, spareaddr;
  112. unsigned int rootfslen, kernellen, sparelen, totallen;
  113. unsigned int cfelen, nvramlen;
  114. unsigned int cfe_erasesize;
  115. int i;
  116. bool rootfs_first = false;
  117. cfe_erasesize = max_t(uint32_t, master->erasesize,
  118. BCM963XX_CFE_BLOCK_SIZE);
  119. cfelen = cfe_erasesize;
  120. nvramlen = nvram->psi_size * SZ_1K;
  121. nvramlen = roundup(nvramlen, cfe_erasesize);
  122. buf = vmalloc(sizeof(struct bcm_tag));
  123. if (!buf)
  124. return -ENOMEM;
  125. /* Get the tag */
  126. ret = bcm63xx_read_image_tag(master, "rootfs", cfelen, buf);
  127. if (!ret) {
  128. STR_NULL_TERMINATE(buf->flash_image_start);
  129. if (kstrtouint(buf->flash_image_start, 10, &rootfsaddr) ||
  130. rootfsaddr < BCM963XX_EXTENDED_SIZE) {
  131. pr_err("invalid rootfs address: %*ph\n",
  132. (int)sizeof(buf->flash_image_start),
  133. buf->flash_image_start);
  134. goto invalid_tag;
  135. }
  136. STR_NULL_TERMINATE(buf->kernel_address);
  137. if (kstrtouint(buf->kernel_address, 10, &kerneladdr) ||
  138. kerneladdr < BCM963XX_EXTENDED_SIZE) {
  139. pr_err("invalid kernel address: %*ph\n",
  140. (int)sizeof(buf->kernel_address),
  141. buf->kernel_address);
  142. goto invalid_tag;
  143. }
  144. STR_NULL_TERMINATE(buf->kernel_length);
  145. if (kstrtouint(buf->kernel_length, 10, &kernellen)) {
  146. pr_err("invalid kernel length: %*ph\n",
  147. (int)sizeof(buf->kernel_length),
  148. buf->kernel_length);
  149. goto invalid_tag;
  150. }
  151. STR_NULL_TERMINATE(buf->total_length);
  152. if (kstrtouint(buf->total_length, 10, &totallen)) {
  153. pr_err("invalid total length: %*ph\n",
  154. (int)sizeof(buf->total_length),
  155. buf->total_length);
  156. goto invalid_tag;
  157. }
  158. kerneladdr = kerneladdr - BCM963XX_EXTENDED_SIZE;
  159. rootfsaddr = rootfsaddr - BCM963XX_EXTENDED_SIZE;
  160. spareaddr = roundup(totallen, master->erasesize) + cfelen;
  161. if (rootfsaddr < kerneladdr) {
  162. /* default Broadcom layout */
  163. rootfslen = kerneladdr - rootfsaddr;
  164. rootfs_first = true;
  165. } else {
  166. /* OpenWrt layout */
  167. rootfsaddr = kerneladdr + kernellen;
  168. rootfslen = spareaddr - rootfsaddr;
  169. }
  170. } else if (ret > 0) {
  171. invalid_tag:
  172. kernellen = 0;
  173. rootfslen = 0;
  174. rootfsaddr = 0;
  175. spareaddr = cfelen;
  176. } else {
  177. goto out;
  178. }
  179. sparelen = master->size - spareaddr - nvramlen;
  180. /* Determine number of partitions */
  181. if (rootfslen > 0)
  182. nrparts++;
  183. if (kernellen > 0)
  184. nrparts++;
  185. parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
  186. if (!parts) {
  187. ret = -ENOMEM;
  188. goto out;
  189. }
  190. /* Start building partition list */
  191. parts[curpart].name = "CFE";
  192. parts[curpart].offset = 0;
  193. parts[curpart].size = cfelen;
  194. curpart++;
  195. if (kernellen > 0) {
  196. int kernelpart = curpart;
  197. if (rootfslen > 0 && rootfs_first)
  198. kernelpart++;
  199. parts[kernelpart].name = "kernel";
  200. parts[kernelpart].offset = kerneladdr;
  201. parts[kernelpart].size = kernellen;
  202. curpart++;
  203. }
  204. if (rootfslen > 0) {
  205. int rootfspart = curpart;
  206. if (kernellen > 0 && rootfs_first)
  207. rootfspart--;
  208. parts[rootfspart].name = "rootfs";
  209. parts[rootfspart].offset = rootfsaddr;
  210. parts[rootfspart].size = rootfslen;
  211. if (sparelen > 0 && !rootfs_first)
  212. parts[rootfspart].size += sparelen;
  213. curpart++;
  214. }
  215. parts[curpart].name = "nvram";
  216. parts[curpart].offset = master->size - nvramlen;
  217. parts[curpart].size = nvramlen;
  218. curpart++;
  219. /* Global partition "linux" to make easy firmware upgrade */
  220. parts[curpart].name = "linux";
  221. parts[curpart].offset = cfelen;
  222. parts[curpart].size = master->size - cfelen - nvramlen;
  223. for (i = 0; i < nrparts; i++)
  224. pr_info("Partition %d is %s offset %llx and length %llx\n", i,
  225. parts[i].name, parts[i].offset, parts[i].size);
  226. pr_info("Spare partition is offset %x and length %x\n", spareaddr,
  227. sparelen);
  228. *pparts = parts;
  229. ret = 0;
  230. out:
  231. vfree(buf);
  232. if (ret)
  233. return ret;
  234. return nrparts;
  235. }
  236. static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
  237. const struct mtd_partition **pparts,
  238. struct mtd_part_parser_data *data)
  239. {
  240. struct bcm963xx_nvram *nvram = NULL;
  241. int ret;
  242. if (bcm63xx_detect_cfe(master))
  243. return -EINVAL;
  244. nvram = vzalloc(sizeof(*nvram));
  245. if (!nvram)
  246. return -ENOMEM;
  247. ret = bcm63xx_read_nvram(master, nvram);
  248. if (ret)
  249. goto out;
  250. if (!mtd_type_is_nand(master))
  251. ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
  252. else
  253. ret = -EINVAL;
  254. out:
  255. vfree(nvram);
  256. return ret;
  257. };
  258. static struct mtd_part_parser bcm63xx_cfe_parser = {
  259. .parse_fn = bcm63xx_parse_cfe_partitions,
  260. .name = "bcm63xxpart",
  261. };
  262. module_mtd_part_parser(bcm63xx_cfe_parser);
  263. MODULE_LICENSE("GPL");
  264. MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
  265. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  266. MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
  267. MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
  268. MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");