bcm47xxpart.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. if (mtd_read(master, offset, sizeof(buf), &bytes_read,
  62. (uint8_t *)&buf) < 0) {
  63. pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
  64. offset);
  65. goto out_default;
  66. }
  67. if (buf == UBI_EC_MAGIC)
  68. return "ubi";
  69. out_default:
  70. return "rootfs";
  71. }
  72. static int bcm47xxpart_parse(struct mtd_info *master,
  73. struct mtd_partition **pparts,
  74. struct mtd_part_parser_data *data)
  75. {
  76. struct mtd_partition *parts;
  77. uint8_t i, curr_part = 0;
  78. uint32_t *buf;
  79. size_t bytes_read;
  80. uint32_t offset;
  81. uint32_t blocksize = master->erasesize;
  82. struct trx_header *trx;
  83. int trx_part = -1;
  84. int last_trx_part = -1;
  85. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  86. /*
  87. * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
  88. * partitions were aligned to at least 0x1000 anyway.
  89. */
  90. if (blocksize < 0x1000)
  91. blocksize = 0x1000;
  92. /* Alloc */
  93. parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
  94. GFP_KERNEL);
  95. if (!parts)
  96. return -ENOMEM;
  97. buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
  98. if (!buf) {
  99. kfree(parts);
  100. return -ENOMEM;
  101. }
  102. /* Parse block by block looking for magics */
  103. for (offset = 0; offset <= master->size - blocksize;
  104. offset += blocksize) {
  105. /* Nothing more in higher memory */
  106. if (offset >= 0x2000000)
  107. break;
  108. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  109. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  110. break;
  111. }
  112. /* Read beginning of the block */
  113. if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
  114. &bytes_read, (uint8_t *)buf) < 0) {
  115. pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
  116. offset);
  117. continue;
  118. }
  119. /* Magic or small NVRAM at 0x400 */
  120. if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
  121. (buf[0x400 / 4] == NVRAM_HEADER)) {
  122. bcm47xxpart_add_part(&parts[curr_part++], "boot",
  123. offset, MTD_WRITEABLE);
  124. continue;
  125. }
  126. /*
  127. * board_data starts with board_id which differs across boards,
  128. * but we can use 'MPFR' (hopefully) magic at 0x100
  129. */
  130. if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
  131. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  132. offset, MTD_WRITEABLE);
  133. continue;
  134. }
  135. /* Found on Huawei E970 */
  136. if (buf[0x000 / 4] == FACTORY_MAGIC) {
  137. bcm47xxpart_add_part(&parts[curr_part++], "factory",
  138. offset, MTD_WRITEABLE);
  139. continue;
  140. }
  141. /* POT(TOP) */
  142. if (buf[0x000 / 4] == POT_MAGIC1 &&
  143. (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
  144. bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
  145. MTD_WRITEABLE);
  146. continue;
  147. }
  148. /* ML */
  149. if (buf[0x010 / 4] == ML_MAGIC1 &&
  150. buf[0x014 / 4] == ML_MAGIC2) {
  151. bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
  152. MTD_WRITEABLE);
  153. continue;
  154. }
  155. /* TRX */
  156. if (buf[0x000 / 4] == TRX_MAGIC) {
  157. if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
  158. pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
  159. break;
  160. }
  161. trx = (struct trx_header *)buf;
  162. trx_part = curr_part;
  163. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  164. offset, 0);
  165. i = 0;
  166. /* We have LZMA loader if offset[2] points to sth */
  167. if (trx->offset[2]) {
  168. bcm47xxpart_add_part(&parts[curr_part++],
  169. "loader",
  170. offset + trx->offset[i],
  171. 0);
  172. i++;
  173. }
  174. if (trx->offset[i]) {
  175. bcm47xxpart_add_part(&parts[curr_part++],
  176. "linux",
  177. offset + trx->offset[i],
  178. 0);
  179. i++;
  180. }
  181. /*
  182. * Pure rootfs size is known and can be calculated as:
  183. * trx->length - trx->offset[i]. We don't fill it as
  184. * we want to have jffs2 (overlay) in the same mtd.
  185. */
  186. if (trx->offset[i]) {
  187. const char *name;
  188. name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
  189. bcm47xxpart_add_part(&parts[curr_part++],
  190. name,
  191. offset + trx->offset[i],
  192. 0);
  193. i++;
  194. }
  195. last_trx_part = curr_part - 1;
  196. /*
  197. * We have whole TRX scanned, skip to the next part. Use
  198. * roundown (not roundup), as the loop will increase
  199. * offset in next step.
  200. */
  201. offset = rounddown(offset + trx->length, blocksize);
  202. continue;
  203. }
  204. /* Squashfs on devices not using TRX */
  205. if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
  206. buf[0x000 / 4] == SHSQ_MAGIC) {
  207. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  208. offset, 0);
  209. continue;
  210. }
  211. /*
  212. * New (ARM?) devices may have NVRAM in some middle block. Last
  213. * block will be checked later, so skip it.
  214. */
  215. if (offset != master->size - blocksize &&
  216. buf[0x000 / 4] == NVRAM_HEADER) {
  217. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  218. offset, 0);
  219. continue;
  220. }
  221. /* Read middle of the block */
  222. if (mtd_read(master, offset + 0x8000, 0x4,
  223. &bytes_read, (uint8_t *)buf) < 0) {
  224. pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
  225. offset);
  226. continue;
  227. }
  228. /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
  229. if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
  230. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  231. offset, MTD_WRITEABLE);
  232. continue;
  233. }
  234. }
  235. /* Look for NVRAM at the end of the last block. */
  236. for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
  237. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  238. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  239. break;
  240. }
  241. offset = master->size - possible_nvram_sizes[i];
  242. if (mtd_read(master, offset, 0x4, &bytes_read,
  243. (uint8_t *)buf) < 0) {
  244. pr_err("mtd_read error while reading at offset 0x%X!\n",
  245. offset);
  246. continue;
  247. }
  248. /* Standard NVRAM */
  249. if (buf[0] == NVRAM_HEADER) {
  250. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  251. master->size - blocksize, 0);
  252. break;
  253. }
  254. }
  255. kfree(buf);
  256. /*
  257. * Assume that partitions end at the beginning of the one they are
  258. * followed by.
  259. */
  260. for (i = 0; i < curr_part; i++) {
  261. u64 next_part_offset = (i < curr_part - 1) ?
  262. parts[i + 1].offset : master->size;
  263. parts[i].size = next_part_offset - parts[i].offset;
  264. if (i == last_trx_part && trx_part >= 0)
  265. parts[trx_part].size = next_part_offset -
  266. parts[trx_part].offset;
  267. }
  268. *pparts = parts;
  269. return curr_part;
  270. };
  271. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  272. .owner = THIS_MODULE,
  273. .parse_fn = bcm47xxpart_parse,
  274. .name = "bcm47xxpart",
  275. };
  276. static int __init bcm47xxpart_init(void)
  277. {
  278. register_mtd_parser(&bcm47xxpart_mtd_parser);
  279. return 0;
  280. }
  281. static void __exit bcm47xxpart_exit(void)
  282. {
  283. deregister_mtd_parser(&bcm47xxpart_mtd_parser);
  284. }
  285. module_init(bcm47xxpart_init);
  286. module_exit(bcm47xxpart_exit);
  287. MODULE_LICENSE("GPL");
  288. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");