bcm47xxpart.c 5.5 KB

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