bcm47xxpart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <bcm47xx_nvram.h>
  17. /* 10 parts were found on sflash on Netgear WNDR4500 */
  18. #define BCM47XXPART_MAX_PARTS 12
  19. /*
  20. * Amount of bytes we read when analyzing each block of flash memory.
  21. * Set it big enough to allow detecting partition and reading important data.
  22. */
  23. #define BCM47XXPART_BYTES_TO_READ 0x4e8
  24. /* Magics */
  25. #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
  26. #define BOARD_DATA_MAGIC2 0xBD0D0BBD
  27. #define CFE_MAGIC 0x43464531 /* 1EFC */
  28. #define FACTORY_MAGIC 0x59544346 /* FCTY */
  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. trx = (struct trx_header *)buf;
  132. trx_part = curr_part;
  133. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  134. offset, 0);
  135. i = 0;
  136. /* We have LZMA loader if offset[2] points to sth */
  137. if (trx->offset[2]) {
  138. bcm47xxpart_add_part(&parts[curr_part++],
  139. "loader",
  140. offset + trx->offset[i],
  141. 0);
  142. i++;
  143. }
  144. bcm47xxpart_add_part(&parts[curr_part++], "linux",
  145. offset + trx->offset[i], 0);
  146. i++;
  147. /*
  148. * Pure rootfs size is known and can be calculated as:
  149. * trx->length - trx->offset[i]. We don't fill it as
  150. * we want to have jffs2 (overlay) in the same mtd.
  151. */
  152. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  153. offset + trx->offset[i], 0);
  154. i++;
  155. last_trx_part = curr_part - 1;
  156. /*
  157. * We have whole TRX scanned, skip to the next part. Use
  158. * roundown (not roundup), as the loop will increase
  159. * offset in next step.
  160. */
  161. offset = rounddown(offset + trx->length, blocksize);
  162. continue;
  163. }
  164. /* Squashfs on devices not using TRX */
  165. if (buf[0x000 / 4] == SQSH_MAGIC) {
  166. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  167. offset, 0);
  168. continue;
  169. }
  170. /* Read middle of the block */
  171. if (mtd_read(master, offset + 0x8000, 0x4,
  172. &bytes_read, (uint8_t *)buf) < 0) {
  173. pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
  174. offset);
  175. continue;
  176. }
  177. /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
  178. if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
  179. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  180. offset, MTD_WRITEABLE);
  181. continue;
  182. }
  183. }
  184. /* Look for NVRAM at the end of the last block. */
  185. for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
  186. if (curr_part > BCM47XXPART_MAX_PARTS) {
  187. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  188. break;
  189. }
  190. offset = master->size - possible_nvram_sizes[i];
  191. if (mtd_read(master, offset, 0x4, &bytes_read,
  192. (uint8_t *)buf) < 0) {
  193. pr_err("mtd_read error while reading at offset 0x%X!\n",
  194. offset);
  195. continue;
  196. }
  197. /* Standard NVRAM */
  198. if (buf[0] == NVRAM_HEADER) {
  199. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  200. master->size - blocksize, 0);
  201. break;
  202. }
  203. }
  204. kfree(buf);
  205. /*
  206. * Assume that partitions end at the beginning of the one they are
  207. * followed by.
  208. */
  209. for (i = 0; i < curr_part; i++) {
  210. u64 next_part_offset = (i < curr_part - 1) ?
  211. parts[i + 1].offset : master->size;
  212. parts[i].size = next_part_offset - parts[i].offset;
  213. if (i == last_trx_part && trx_part >= 0)
  214. parts[trx_part].size = next_part_offset -
  215. parts[trx_part].offset;
  216. }
  217. *pparts = parts;
  218. return curr_part;
  219. };
  220. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  221. .owner = THIS_MODULE,
  222. .parse_fn = bcm47xxpart_parse,
  223. .name = "bcm47xxpart",
  224. };
  225. static int __init bcm47xxpart_init(void)
  226. {
  227. register_mtd_parser(&bcm47xxpart_mtd_parser);
  228. return 0;
  229. }
  230. static void __exit bcm47xxpart_exit(void)
  231. {
  232. deregister_mtd_parser(&bcm47xxpart_mtd_parser);
  233. }
  234. module_init(bcm47xxpart_init);
  235. module_exit(bcm47xxpart_exit);
  236. MODULE_LICENSE("GPL");
  237. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");