bcm47xxpart.c 5.9 KB

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