bcm47xxpart.c 6.4 KB

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