bcm47xxpart.c 7.1 KB

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