bcm47xxpart.c 6.0 KB

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