bcm63xxpart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * BCM63XX CFE image tag parser
  4. *
  5. * Copyright © 2006-2008 Florian Fainelli <[email protected]>
  6. * Mike Albon <[email protected]>
  7. * Copyright © 2009-2010 Daniel Dickinson <[email protected]>
  8. * Copyright © 2011-2013 Jonas Gorski <[email protected]>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/bcm963xx_nvram.h>
  12. #include <linux/bcm963xx_tag.h>
  13. #include <linux/crc32.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sizes.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/of.h>
  22. #ifdef CONFIG_MIPS
  23. #include <asm/bootinfo.h>
  24. #include <asm/fw/cfe/cfe_api.h>
  25. #endif /* CONFIG_MIPS */
  26. #define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
  27. #define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
  28. #define BCM963XX_CFE_VERSION_OFFSET 0x570
  29. #define BCM963XX_NVRAM_OFFSET 0x580
  30. /* Ensure strings read from flash structs are null terminated */
  31. #define STR_NULL_TERMINATE(x) \
  32. do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
  33. static inline int bcm63xx_detect_cfe(void)
  34. {
  35. int ret = 0;
  36. #ifdef CONFIG_MIPS
  37. ret = (fw_arg3 == CFE_EPTSEAL);
  38. #endif /* CONFIG_MIPS */
  39. return ret;
  40. }
  41. static int bcm63xx_read_nvram(struct mtd_info *master,
  42. struct bcm963xx_nvram *nvram)
  43. {
  44. u32 actual_crc, expected_crc;
  45. size_t retlen;
  46. int ret;
  47. /* extract nvram data */
  48. ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
  49. &retlen, (void *)nvram);
  50. if (ret)
  51. return ret;
  52. ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
  53. if (ret)
  54. pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
  55. expected_crc, actual_crc);
  56. if (!nvram->psi_size)
  57. nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
  58. return 0;
  59. }
  60. static const char * const bcm63xx_cfe_part_types[] = {
  61. "bcm963xx-imagetag",
  62. NULL,
  63. };
  64. static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
  65. const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
  66. {
  67. struct mtd_partition *parts;
  68. int nrparts = 3, curpart = 0;
  69. unsigned int cfelen, nvramlen;
  70. unsigned int cfe_erasesize;
  71. int i;
  72. cfe_erasesize = max_t(uint32_t, master->erasesize,
  73. BCM963XX_CFE_BLOCK_SIZE);
  74. cfelen = cfe_erasesize;
  75. nvramlen = nvram->psi_size * SZ_1K;
  76. nvramlen = roundup(nvramlen, cfe_erasesize);
  77. parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
  78. if (!parts)
  79. return -ENOMEM;
  80. /* Start building partition list */
  81. parts[curpart].name = "CFE";
  82. parts[curpart].offset = 0;
  83. parts[curpart].size = cfelen;
  84. curpart++;
  85. parts[curpart].name = "nvram";
  86. parts[curpart].offset = master->size - nvramlen;
  87. parts[curpart].size = nvramlen;
  88. curpart++;
  89. /* Global partition "linux" to make easy firmware upgrade */
  90. parts[curpart].name = "linux";
  91. parts[curpart].offset = cfelen;
  92. parts[curpart].size = master->size - cfelen - nvramlen;
  93. parts[curpart].types = bcm63xx_cfe_part_types;
  94. for (i = 0; i < nrparts; i++)
  95. pr_info("Partition %d is %s offset %llx and length %llx\n", i,
  96. parts[i].name, parts[i].offset, parts[i].size);
  97. *pparts = parts;
  98. return nrparts;
  99. }
  100. static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
  101. const struct mtd_partition **pparts,
  102. struct mtd_part_parser_data *data)
  103. {
  104. struct bcm963xx_nvram *nvram = NULL;
  105. int ret;
  106. if (!bcm63xx_detect_cfe())
  107. return -EINVAL;
  108. nvram = vzalloc(sizeof(*nvram));
  109. if (!nvram)
  110. return -ENOMEM;
  111. ret = bcm63xx_read_nvram(master, nvram);
  112. if (ret)
  113. goto out;
  114. if (!mtd_type_is_nand(master))
  115. ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
  116. else
  117. ret = -EINVAL;
  118. out:
  119. vfree(nvram);
  120. return ret;
  121. };
  122. static const struct of_device_id parse_bcm63xx_cfe_match_table[] = {
  123. { .compatible = "brcm,bcm963xx-cfe-nor-partitions" },
  124. {},
  125. };
  126. MODULE_DEVICE_TABLE(of, parse_bcm63xx_cfe_match_table);
  127. static struct mtd_part_parser bcm63xx_cfe_parser = {
  128. .parse_fn = bcm63xx_parse_cfe_partitions,
  129. .name = "bcm63xxpart",
  130. .of_match_table = parse_bcm63xx_cfe_match_table,
  131. };
  132. module_mtd_part_parser(bcm63xx_cfe_parser);
  133. MODULE_LICENSE("GPL");
  134. MODULE_AUTHOR("Daniel Dickinson <[email protected]>");
  135. MODULE_AUTHOR("Florian Fainelli <[email protected]>");
  136. MODULE_AUTHOR("Mike Albon <[email protected]>");
  137. MODULE_AUTHOR("Jonas Gorski <[email protected]");
  138. MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");