bcm47xxpart.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <uapi/linux/magic.h>
  17. /*
  18. * NAND flash on Netgear R6250 was verified to contain 15 partitions.
  19. * This will result in allocating too big array for some old devices, but the
  20. * memory will be freed soon anyway (see mtd_device_parse_register).
  21. */
  22. #define BCM47XXPART_MAX_PARTS 20
  23. /*
  24. * Amount of bytes we read when analyzing each block of flash memory.
  25. * Set it big enough to allow detecting partition and reading important data.
  26. */
  27. #define BCM47XXPART_BYTES_TO_READ 0x4e8
  28. /* Magics */
  29. #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
  30. #define BOARD_DATA_MAGIC2 0xBD0D0BBD
  31. #define CFE_MAGIC 0x43464531 /* 1EFC */
  32. #define FACTORY_MAGIC 0x59544346 /* FCTY */
  33. #define NVRAM_HEADER 0x48534C46 /* FLSH */
  34. #define POT_MAGIC1 0x54544f50 /* POTT */
  35. #define POT_MAGIC2 0x504f /* OP */
  36. #define ML_MAGIC1 0x39685a42
  37. #define ML_MAGIC2 0x26594131
  38. #define TRX_MAGIC 0x30524448
  39. #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
  40. #define UBI_EC_MAGIC 0x23494255 /* UBI# */
  41. struct trx_header {
  42. uint32_t magic;
  43. uint32_t length;
  44. uint32_t crc32;
  45. uint16_t flags;
  46. uint16_t version;
  47. uint32_t offset[3];
  48. } __packed;
  49. static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
  50. u64 offset, uint32_t mask_flags)
  51. {
  52. part->name = name;
  53. part->offset = offset;
  54. part->mask_flags = mask_flags;
  55. }
  56. static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
  57. size_t offset)
  58. {
  59. uint32_t buf;
  60. size_t bytes_read;
  61. int err;
  62. err = mtd_read(master, offset, sizeof(buf), &bytes_read,
  63. (uint8_t *)&buf);
  64. if (err && !mtd_is_bitflip(err)) {
  65. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  66. offset, err);
  67. goto out_default;
  68. }
  69. if (buf == UBI_EC_MAGIC)
  70. return "ubi";
  71. out_default:
  72. return "rootfs";
  73. }
  74. static int bcm47xxpart_parse(struct mtd_info *master,
  75. 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. struct trx_header *trx;
  85. int trx_part = -1;
  86. int last_trx_part = -1;
  87. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  88. int err;
  89. /*
  90. * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
  91. * partitions were aligned to at least 0x1000 anyway.
  92. */
  93. if (blocksize < 0x1000)
  94. blocksize = 0x1000;
  95. /* Alloc */
  96. parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
  97. GFP_KERNEL);
  98. if (!parts)
  99. return -ENOMEM;
  100. buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
  101. if (!buf) {
  102. kfree(parts);
  103. return -ENOMEM;
  104. }
  105. /* Parse block by block looking for magics */
  106. for (offset = 0; offset <= master->size - blocksize;
  107. offset += blocksize) {
  108. /* Nothing more in higher memory on BCM47XX (MIPS) */
  109. if (config_enabled(CONFIG_BCM47XX) && offset >= 0x2000000)
  110. break;
  111. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  112. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  113. break;
  114. }
  115. /* Read beginning of the block */
  116. err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
  117. &bytes_read, (uint8_t *)buf);
  118. if (err && !mtd_is_bitflip(err)) {
  119. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  120. offset, err);
  121. continue;
  122. }
  123. /* Magic or small NVRAM at 0x400 */
  124. if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
  125. (buf[0x400 / 4] == NVRAM_HEADER)) {
  126. bcm47xxpart_add_part(&parts[curr_part++], "boot",
  127. offset, MTD_WRITEABLE);
  128. continue;
  129. }
  130. /*
  131. * board_data starts with board_id which differs across boards,
  132. * but we can use 'MPFR' (hopefully) magic at 0x100
  133. */
  134. if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
  135. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  136. offset, MTD_WRITEABLE);
  137. continue;
  138. }
  139. /* Found on Huawei E970 */
  140. if (buf[0x000 / 4] == FACTORY_MAGIC) {
  141. bcm47xxpart_add_part(&parts[curr_part++], "factory",
  142. offset, MTD_WRITEABLE);
  143. continue;
  144. }
  145. /* POT(TOP) */
  146. if (buf[0x000 / 4] == POT_MAGIC1 &&
  147. (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
  148. bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
  149. MTD_WRITEABLE);
  150. continue;
  151. }
  152. /* ML */
  153. if (buf[0x010 / 4] == ML_MAGIC1 &&
  154. buf[0x014 / 4] == ML_MAGIC2) {
  155. bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
  156. MTD_WRITEABLE);
  157. continue;
  158. }
  159. /* TRX */
  160. if (buf[0x000 / 4] == TRX_MAGIC) {
  161. if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
  162. pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
  163. break;
  164. }
  165. trx = (struct trx_header *)buf;
  166. trx_part = curr_part;
  167. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  168. offset, 0);
  169. i = 0;
  170. /* We have LZMA loader if offset[2] points to sth */
  171. if (trx->offset[2]) {
  172. bcm47xxpart_add_part(&parts[curr_part++],
  173. "loader",
  174. offset + trx->offset[i],
  175. 0);
  176. i++;
  177. }
  178. if (trx->offset[i]) {
  179. bcm47xxpart_add_part(&parts[curr_part++],
  180. "linux",
  181. offset + trx->offset[i],
  182. 0);
  183. i++;
  184. }
  185. /*
  186. * Pure rootfs size is known and can be calculated as:
  187. * trx->length - trx->offset[i]. We don't fill it as
  188. * we want to have jffs2 (overlay) in the same mtd.
  189. */
  190. if (trx->offset[i]) {
  191. const char *name;
  192. name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
  193. bcm47xxpart_add_part(&parts[curr_part++],
  194. name,
  195. offset + trx->offset[i],
  196. 0);
  197. i++;
  198. }
  199. last_trx_part = curr_part - 1;
  200. /* Jump to the end of TRX */
  201. offset = roundup(offset + trx->length, blocksize);
  202. /* Next loop iteration will increase the offset */
  203. offset -= blocksize;
  204. continue;
  205. }
  206. /* Squashfs on devices not using TRX */
  207. if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
  208. buf[0x000 / 4] == SHSQ_MAGIC) {
  209. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  210. offset, 0);
  211. continue;
  212. }
  213. /*
  214. * New (ARM?) devices may have NVRAM in some middle block. Last
  215. * block will be checked later, so skip it.
  216. */
  217. if (offset != master->size - blocksize &&
  218. buf[0x000 / 4] == NVRAM_HEADER) {
  219. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  220. offset, 0);
  221. continue;
  222. }
  223. /* Read middle of the block */
  224. err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
  225. (uint8_t *)buf);
  226. if (err && !mtd_is_bitflip(err)) {
  227. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  228. offset, err);
  229. continue;
  230. }
  231. /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
  232. if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
  233. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  234. offset, MTD_WRITEABLE);
  235. continue;
  236. }
  237. }
  238. /* Look for NVRAM at the end of the last block. */
  239. for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
  240. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  241. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  242. break;
  243. }
  244. offset = master->size - possible_nvram_sizes[i];
  245. err = mtd_read(master, offset, 0x4, &bytes_read,
  246. (uint8_t *)buf);
  247. if (err && !mtd_is_bitflip(err)) {
  248. pr_err("mtd_read error while reading (offset 0x%X): %d\n",
  249. offset, err);
  250. continue;
  251. }
  252. /* Standard NVRAM */
  253. if (buf[0] == NVRAM_HEADER) {
  254. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  255. master->size - blocksize, 0);
  256. break;
  257. }
  258. }
  259. kfree(buf);
  260. /*
  261. * Assume that partitions end at the beginning of the one they are
  262. * followed by.
  263. */
  264. for (i = 0; i < curr_part; i++) {
  265. u64 next_part_offset = (i < curr_part - 1) ?
  266. parts[i + 1].offset : master->size;
  267. parts[i].size = next_part_offset - parts[i].offset;
  268. if (i == last_trx_part && trx_part >= 0)
  269. parts[trx_part].size = next_part_offset -
  270. parts[trx_part].offset;
  271. }
  272. *pparts = parts;
  273. return curr_part;
  274. };
  275. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  276. .owner = THIS_MODULE,
  277. .parse_fn = bcm47xxpart_parse,
  278. .name = "bcm47xxpart",
  279. };
  280. static int __init bcm47xxpart_init(void)
  281. {
  282. register_mtd_parser(&bcm47xxpart_mtd_parser);
  283. return 0;
  284. }
  285. static void __exit bcm47xxpart_exit(void)
  286. {
  287. deregister_mtd_parser(&bcm47xxpart_mtd_parser);
  288. }
  289. module_init(bcm47xxpart_init);
  290. module_exit(bcm47xxpart_exit);
  291. MODULE_LICENSE("GPL");
  292. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");