bcm47xxpart.c 7.7 KB

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