bcm47xxpart.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BCM47XX MTD partitioning
  4. *
  5. * Copyright © 2012 Rafał Miłecki <[email protected]>
  6. */
  7. #include <linux/bcm47xx_nvram.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/mtd/mtd.h>
  12. #include <linux/mtd/partitions.h>
  13. #include <uapi/linux/magic.h>
  14. /*
  15. * NAND flash on Netgear R6250 was verified to contain 15 partitions.
  16. * This will result in allocating too big array for some old devices, but the
  17. * memory will be freed soon anyway (see mtd_device_parse_register).
  18. */
  19. #define BCM47XXPART_MAX_PARTS 20
  20. /*
  21. * Amount of bytes we read when analyzing each block of flash memory.
  22. * Set it big enough to allow detecting partition and reading important data.
  23. */
  24. #define BCM47XXPART_BYTES_TO_READ 0x4e8
  25. /* Magics */
  26. #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
  27. #define BOARD_DATA_MAGIC2 0xBD0D0BBD
  28. #define CFE_MAGIC 0x43464531 /* 1EFC */
  29. #define FACTORY_MAGIC 0x59544346 /* FCTY */
  30. #define NVRAM_HEADER 0x48534C46 /* FLSH */
  31. #define POT_MAGIC1 0x54544f50 /* POTT */
  32. #define POT_MAGIC2 0x504f /* OP */
  33. #define ML_MAGIC1 0x39685a42
  34. #define ML_MAGIC2 0x26594131
  35. #define TRX_MAGIC 0x30524448
  36. #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
  37. static const char * const trx_types[] = { "trx", NULL };
  38. struct trx_header {
  39. uint32_t magic;
  40. uint32_t length;
  41. uint32_t crc32;
  42. uint16_t flags;
  43. uint16_t version;
  44. uint32_t offset[3];
  45. } __packed;
  46. static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
  47. u64 offset, uint32_t mask_flags)
  48. {
  49. part->name = name;
  50. part->offset = offset;
  51. part->mask_flags = mask_flags;
  52. }
  53. /**
  54. * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
  55. *
  56. * Some devices may have more than one TRX partition. In such case one of them
  57. * is the main one and another a failsafe one. Bootloader may fallback to the
  58. * failsafe firmware if it detects corruption of the main image.
  59. *
  60. * This function provides info about currently used TRX partition. It's the one
  61. * containing kernel started by the bootloader.
  62. */
  63. static int bcm47xxpart_bootpartition(void)
  64. {
  65. char buf[4];
  66. int bootpartition;
  67. /* Check CFE environment variable */
  68. if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
  69. if (!kstrtoint(buf, 0, &bootpartition))
  70. return bootpartition;
  71. }
  72. return 0;
  73. }
  74. static int bcm47xxpart_parse(struct mtd_info *master,
  75. const struct mtd_partition **pparts,
  76. struct mtd_part_parser_data *data)
  77. {
  78. struct mtd_partition *parts;
  79. uint8_t i, curr_part = 0;
  80. uint32_t *buf;
  81. size_t bytes_read;
  82. uint32_t offset;
  83. uint32_t blocksize = master->erasesize;
  84. int trx_parts[2]; /* Array with indexes of TRX partitions */
  85. int trx_num = 0; /* Number of found TRX partitions */
  86. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  87. int err;
  88. /*
  89. * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
  90. * partitions were aligned to at least 0x1000 anyway.
  91. */
  92. if (blocksize < 0x1000)
  93. blocksize = 0x1000;
  94. /* Alloc */
  95. parts = kcalloc(BCM47XXPART_MAX_PARTS, sizeof(struct mtd_partition),
  96. GFP_KERNEL);
  97. if (!parts)
  98. return -ENOMEM;
  99. buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
  100. if (!buf) {
  101. kfree(parts);
  102. return -ENOMEM;
  103. }
  104. /* Parse block by block looking for magics */
  105. for (offset = 0; offset <= master->size - blocksize;
  106. offset += blocksize) {
  107. /* Nothing more in higher memory on BCM47XX (MIPS) */
  108. if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
  109. break;
  110. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  111. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  112. break;
  113. }
  114. /* Read beginning of the block */
  115. err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
  116. &bytes_read, (uint8_t *)buf);
  117. if (err && !mtd_is_bitflip(err)) {
  118. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  119. offset, err);
  120. continue;
  121. }
  122. /* Magic or small NVRAM at 0x400 */
  123. if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
  124. (buf[0x400 / 4] == NVRAM_HEADER)) {
  125. bcm47xxpart_add_part(&parts[curr_part++], "boot",
  126. offset, MTD_WRITEABLE);
  127. continue;
  128. }
  129. /*
  130. * board_data starts with board_id which differs across boards,
  131. * but we can use 'MPFR' (hopefully) magic at 0x100
  132. */
  133. if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
  134. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  135. offset, MTD_WRITEABLE);
  136. continue;
  137. }
  138. /* Found on Huawei E970 */
  139. if (buf[0x000 / 4] == FACTORY_MAGIC) {
  140. bcm47xxpart_add_part(&parts[curr_part++], "factory",
  141. offset, MTD_WRITEABLE);
  142. continue;
  143. }
  144. /* POT(TOP) */
  145. if (buf[0x000 / 4] == POT_MAGIC1 &&
  146. (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
  147. bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
  148. MTD_WRITEABLE);
  149. continue;
  150. }
  151. /* ML */
  152. if (buf[0x010 / 4] == ML_MAGIC1 &&
  153. buf[0x014 / 4] == ML_MAGIC2) {
  154. bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
  155. MTD_WRITEABLE);
  156. continue;
  157. }
  158. /* TRX */
  159. if (buf[0x000 / 4] == TRX_MAGIC) {
  160. struct trx_header *trx;
  161. uint32_t last_subpart;
  162. uint32_t trx_size;
  163. if (trx_num >= ARRAY_SIZE(trx_parts))
  164. pr_warn("No enough space to store another TRX found at 0x%X\n",
  165. offset);
  166. else
  167. trx_parts[trx_num++] = curr_part;
  168. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  169. offset, 0);
  170. /*
  171. * Try to find TRX size. The "length" field isn't fully
  172. * reliable as it could be decreased to make CRC32 cover
  173. * only part of TRX data. It's commonly used as checksum
  174. * can't cover e.g. ever-changing rootfs partition.
  175. * Use offsets as helpers for assuming min TRX size.
  176. */
  177. trx = (struct trx_header *)buf;
  178. last_subpart = max3(trx->offset[0], trx->offset[1],
  179. trx->offset[2]);
  180. trx_size = max(trx->length, last_subpart + blocksize);
  181. /*
  182. * Skip the TRX data. Decrease offset by block size as
  183. * the next loop iteration will increase it.
  184. */
  185. offset += roundup(trx_size, blocksize) - blocksize;
  186. continue;
  187. }
  188. /* Squashfs on devices not using TRX */
  189. if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
  190. buf[0x000 / 4] == SHSQ_MAGIC) {
  191. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  192. offset, 0);
  193. continue;
  194. }
  195. /*
  196. * New (ARM?) devices may have NVRAM in some middle block. Last
  197. * block will be checked later, so skip it.
  198. */
  199. if (offset != master->size - blocksize &&
  200. buf[0x000 / 4] == NVRAM_HEADER) {
  201. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  202. offset, 0);
  203. continue;
  204. }
  205. /* Read middle of the block */
  206. err = mtd_read(master, offset + (blocksize / 2), 0x4, &bytes_read,
  207. (uint8_t *)buf);
  208. if (err && !mtd_is_bitflip(err)) {
  209. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  210. offset + (blocksize / 2), err);
  211. continue;
  212. }
  213. /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
  214. if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
  215. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  216. offset, MTD_WRITEABLE);
  217. continue;
  218. }
  219. }
  220. /* Look for NVRAM at the end of the last block. */
  221. for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
  222. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  223. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  224. break;
  225. }
  226. offset = master->size - possible_nvram_sizes[i];
  227. err = mtd_read(master, offset, 0x4, &bytes_read,
  228. (uint8_t *)buf);
  229. if (err && !mtd_is_bitflip(err)) {
  230. pr_err("mtd_read error while reading (offset 0x%X): %d\n",
  231. offset, err);
  232. continue;
  233. }
  234. /* Standard NVRAM */
  235. if (buf[0] == NVRAM_HEADER) {
  236. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  237. master->size - blocksize, 0);
  238. break;
  239. }
  240. }
  241. kfree(buf);
  242. /*
  243. * Assume that partitions end at the beginning of the one they are
  244. * followed by.
  245. */
  246. for (i = 0; i < curr_part; i++) {
  247. u64 next_part_offset = (i < curr_part - 1) ?
  248. parts[i + 1].offset : master->size;
  249. parts[i].size = next_part_offset - parts[i].offset;
  250. }
  251. /* If there was TRX parse it now */
  252. for (i = 0; i < trx_num; i++) {
  253. struct mtd_partition *trx = &parts[trx_parts[i]];
  254. if (i == bcm47xxpart_bootpartition())
  255. trx->types = trx_types;
  256. else
  257. trx->name = "failsafe";
  258. }
  259. *pparts = parts;
  260. return curr_part;
  261. };
  262. static const struct of_device_id bcm47xxpart_of_match_table[] = {
  263. { .compatible = "brcm,bcm947xx-cfe-partitions" },
  264. {},
  265. };
  266. MODULE_DEVICE_TABLE(of, bcm47xxpart_of_match_table);
  267. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  268. .parse_fn = bcm47xxpart_parse,
  269. .name = "bcm47xxpart",
  270. .of_match_table = bcm47xxpart_of_match_table,
  271. };
  272. module_mtd_part_parser(bcm47xxpart_mtd_parser);
  273. MODULE_LICENSE("GPL");
  274. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");