bcm47xxpart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <asm/mach-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 = 0x10000;
  56. struct trx_header *trx;
  57. int trx_part = -1;
  58. int last_trx_part = -1;
  59. /* Alloc */
  60. parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
  61. GFP_KERNEL);
  62. buf = kzalloc(BCM47XXPART_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, BCM47XXPART_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. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  89. offset, 0);
  90. continue;
  91. }
  92. /*
  93. * board_data starts with board_id which differs across boards,
  94. * but we can use 'MPFR' (hopefully) magic at 0x100
  95. */
  96. if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
  97. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  98. offset, MTD_WRITEABLE);
  99. continue;
  100. }
  101. /* POT(TOP) */
  102. if (buf[0x000 / 4] == POT_MAGIC1 &&
  103. (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
  104. bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
  105. MTD_WRITEABLE);
  106. continue;
  107. }
  108. /* ML */
  109. if (buf[0x010 / 4] == ML_MAGIC1 &&
  110. buf[0x014 / 4] == ML_MAGIC2) {
  111. bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
  112. MTD_WRITEABLE);
  113. continue;
  114. }
  115. /* TRX */
  116. if (buf[0x000 / 4] == TRX_MAGIC) {
  117. trx = (struct trx_header *)buf;
  118. trx_part = curr_part;
  119. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  120. offset, 0);
  121. i = 0;
  122. /* We have LZMA loader if offset[2] points to sth */
  123. if (trx->offset[2]) {
  124. bcm47xxpart_add_part(&parts[curr_part++],
  125. "loader",
  126. offset + trx->offset[i],
  127. 0);
  128. i++;
  129. }
  130. bcm47xxpart_add_part(&parts[curr_part++], "linux",
  131. offset + trx->offset[i], 0);
  132. i++;
  133. /*
  134. * Pure rootfs size is known and can be calculated as:
  135. * trx->length - trx->offset[i]. We don't fill it as
  136. * we want to have jffs2 (overlay) in the same mtd.
  137. */
  138. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  139. offset + trx->offset[i], 0);
  140. i++;
  141. last_trx_part = curr_part - 1;
  142. /*
  143. * We have whole TRX scanned, skip to the next part. Use
  144. * roundown (not roundup), as the loop will increase
  145. * offset in next step.
  146. */
  147. offset = rounddown(offset + trx->length, blocksize);
  148. continue;
  149. }
  150. }
  151. kfree(buf);
  152. /*
  153. * Assume that partitions end at the beginning of the one they are
  154. * followed by.
  155. */
  156. for (i = 0; i < curr_part; i++) {
  157. u64 next_part_offset = (i < curr_part - 1) ?
  158. parts[i + 1].offset : master->size;
  159. parts[i].size = next_part_offset - parts[i].offset;
  160. if (i == last_trx_part && trx_part >= 0)
  161. parts[trx_part].size = next_part_offset -
  162. parts[trx_part].offset;
  163. }
  164. *pparts = parts;
  165. return curr_part;
  166. };
  167. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  168. .owner = THIS_MODULE,
  169. .parse_fn = bcm47xxpart_parse,
  170. .name = "bcm47xxpart",
  171. };
  172. static int __init bcm47xxpart_init(void)
  173. {
  174. return register_mtd_parser(&bcm47xxpart_mtd_parser);
  175. }
  176. static void __exit bcm47xxpart_exit(void)
  177. {
  178. deregister_mtd_parser(&bcm47xxpart_mtd_parser);
  179. }
  180. module_init(bcm47xxpart_init);
  181. module_exit(bcm47xxpart_exit);
  182. MODULE_LICENSE("GPL");
  183. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");