bcm47xxpart.c 5.0 KB

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