bcm47xxpart.c 8.8 KB

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