bcm47xxpart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/bcm47xx_nvram.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <uapi/linux/magic.h>
  18. /*
  19. * NAND flash on Netgear R6250 was verified to contain 15 partitions.
  20. * This will result in allocating too big array for some old devices, but the
  21. * memory will be freed soon anyway (see mtd_device_parse_register).
  22. */
  23. #define BCM47XXPART_MAX_PARTS 20
  24. /*
  25. * Amount of bytes we read when analyzing each block of flash memory.
  26. * Set it big enough to allow detecting partition and reading important data.
  27. */
  28. #define BCM47XXPART_BYTES_TO_READ 0x4e8
  29. /* Magics */
  30. #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
  31. #define BOARD_DATA_MAGIC2 0xBD0D0BBD
  32. #define CFE_MAGIC 0x43464531 /* 1EFC */
  33. #define FACTORY_MAGIC 0x59544346 /* FCTY */
  34. #define NVRAM_HEADER 0x48534C46 /* FLSH */
  35. #define POT_MAGIC1 0x54544f50 /* POTT */
  36. #define POT_MAGIC2 0x504f /* OP */
  37. #define ML_MAGIC1 0x39685a42
  38. #define ML_MAGIC2 0x26594131
  39. #define TRX_MAGIC 0x30524448
  40. #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
  41. #define UBI_EC_MAGIC 0x23494255 /* UBI# */
  42. struct trx_header {
  43. uint32_t magic;
  44. uint32_t length;
  45. uint32_t crc32;
  46. uint16_t flags;
  47. uint16_t version;
  48. uint32_t offset[3];
  49. } __packed;
  50. static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
  51. u64 offset, uint32_t mask_flags)
  52. {
  53. part->name = name;
  54. part->offset = offset;
  55. part->mask_flags = mask_flags;
  56. }
  57. static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
  58. size_t offset)
  59. {
  60. uint32_t buf;
  61. size_t bytes_read;
  62. int err;
  63. err = mtd_read(master, offset, sizeof(buf), &bytes_read,
  64. (uint8_t *)&buf);
  65. if (err && !mtd_is_bitflip(err)) {
  66. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  67. offset, err);
  68. goto out_default;
  69. }
  70. if (buf == UBI_EC_MAGIC)
  71. return "ubi";
  72. out_default:
  73. return "rootfs";
  74. }
  75. static int bcm47xxpart_parse_trx(struct mtd_info *master,
  76. struct mtd_partition *trx,
  77. struct mtd_partition *parts,
  78. size_t parts_len)
  79. {
  80. struct trx_header header;
  81. size_t bytes_read;
  82. int curr_part = 0;
  83. int i, err;
  84. if (parts_len < 3) {
  85. pr_warn("No enough space to add TRX partitions!\n");
  86. return -ENOMEM;
  87. }
  88. err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
  89. (uint8_t *)&header);
  90. if (err && !mtd_is_bitflip(err)) {
  91. pr_err("mtd_read error while reading TRX header: %d\n", err);
  92. return err;
  93. }
  94. i = 0;
  95. /* We have LZMA loader if offset[2] points to sth */
  96. if (header.offset[2]) {
  97. bcm47xxpart_add_part(&parts[curr_part++], "loader",
  98. trx->offset + header.offset[i], 0);
  99. i++;
  100. }
  101. if (header.offset[i]) {
  102. bcm47xxpart_add_part(&parts[curr_part++], "linux",
  103. trx->offset + header.offset[i], 0);
  104. i++;
  105. }
  106. if (header.offset[i]) {
  107. size_t offset = trx->offset + header.offset[i];
  108. const char *name = bcm47xxpart_trx_data_part_name(master,
  109. offset);
  110. bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
  111. i++;
  112. }
  113. /*
  114. * Assume that every partition ends at the beginning of the one it is
  115. * followed by.
  116. */
  117. for (i = 0; i < curr_part; i++) {
  118. u64 next_part_offset = (i < curr_part - 1) ?
  119. parts[i + 1].offset :
  120. trx->offset + trx->size;
  121. parts[i].size = next_part_offset - parts[i].offset;
  122. }
  123. return curr_part;
  124. }
  125. /**
  126. * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
  127. *
  128. * Some devices may have more than one TRX partition. In such case one of them
  129. * is the main one and another a failsafe one. Bootloader may fallback to the
  130. * failsafe firmware if it detects corruption of the main image.
  131. *
  132. * This function provides info about currently used TRX partition. It's the one
  133. * containing kernel started by the bootloader.
  134. */
  135. static int bcm47xxpart_bootpartition(void)
  136. {
  137. char buf[4];
  138. int bootpartition;
  139. /* Check CFE environment variable */
  140. if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
  141. if (!kstrtoint(buf, 0, &bootpartition))
  142. return bootpartition;
  143. }
  144. return 0;
  145. }
  146. static int bcm47xxpart_parse(struct mtd_info *master,
  147. const struct mtd_partition **pparts,
  148. struct mtd_part_parser_data *data)
  149. {
  150. struct mtd_partition *parts;
  151. uint8_t i, curr_part = 0;
  152. uint32_t *buf;
  153. size_t bytes_read;
  154. uint32_t offset;
  155. uint32_t blocksize = master->erasesize;
  156. int trx_parts[2]; /* Array with indexes of TRX partitions */
  157. int trx_num = 0; /* Number of found TRX partitions */
  158. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  159. int err;
  160. /*
  161. * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
  162. * partitions were aligned to at least 0x1000 anyway.
  163. */
  164. if (blocksize < 0x1000)
  165. blocksize = 0x1000;
  166. /* Alloc */
  167. parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
  168. GFP_KERNEL);
  169. if (!parts)
  170. return -ENOMEM;
  171. buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
  172. if (!buf) {
  173. kfree(parts);
  174. return -ENOMEM;
  175. }
  176. /* Parse block by block looking for magics */
  177. for (offset = 0; offset <= master->size - blocksize;
  178. offset += blocksize) {
  179. /* Nothing more in higher memory on BCM47XX (MIPS) */
  180. if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
  181. break;
  182. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  183. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  184. break;
  185. }
  186. /* Read beginning of the block */
  187. err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
  188. &bytes_read, (uint8_t *)buf);
  189. if (err && !mtd_is_bitflip(err)) {
  190. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  191. offset, err);
  192. continue;
  193. }
  194. /* Magic or small NVRAM at 0x400 */
  195. if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
  196. (buf[0x400 / 4] == NVRAM_HEADER)) {
  197. bcm47xxpart_add_part(&parts[curr_part++], "boot",
  198. offset, MTD_WRITEABLE);
  199. continue;
  200. }
  201. /*
  202. * board_data starts with board_id which differs across boards,
  203. * but we can use 'MPFR' (hopefully) magic at 0x100
  204. */
  205. if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
  206. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  207. offset, MTD_WRITEABLE);
  208. continue;
  209. }
  210. /* Found on Huawei E970 */
  211. if (buf[0x000 / 4] == FACTORY_MAGIC) {
  212. bcm47xxpart_add_part(&parts[curr_part++], "factory",
  213. offset, MTD_WRITEABLE);
  214. continue;
  215. }
  216. /* POT(TOP) */
  217. if (buf[0x000 / 4] == POT_MAGIC1 &&
  218. (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
  219. bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
  220. MTD_WRITEABLE);
  221. continue;
  222. }
  223. /* ML */
  224. if (buf[0x010 / 4] == ML_MAGIC1 &&
  225. buf[0x014 / 4] == ML_MAGIC2) {
  226. bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
  227. MTD_WRITEABLE);
  228. continue;
  229. }
  230. /* TRX */
  231. if (buf[0x000 / 4] == TRX_MAGIC) {
  232. struct trx_header *trx;
  233. if (trx_num >= ARRAY_SIZE(trx_parts))
  234. pr_warn("No enough space to store another TRX found at 0x%X\n",
  235. offset);
  236. else
  237. trx_parts[trx_num++] = curr_part;
  238. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  239. offset, 0);
  240. /* Jump to the end of TRX */
  241. trx = (struct trx_header *)buf;
  242. offset = roundup(offset + trx->length, blocksize);
  243. /* Next loop iteration will increase the offset */
  244. offset -= blocksize;
  245. continue;
  246. }
  247. /* Squashfs on devices not using TRX */
  248. if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
  249. buf[0x000 / 4] == SHSQ_MAGIC) {
  250. bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
  251. offset, 0);
  252. continue;
  253. }
  254. /*
  255. * New (ARM?) devices may have NVRAM in some middle block. Last
  256. * block will be checked later, so skip it.
  257. */
  258. if (offset != master->size - blocksize &&
  259. buf[0x000 / 4] == NVRAM_HEADER) {
  260. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  261. offset, 0);
  262. continue;
  263. }
  264. /* Read middle of the block */
  265. err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
  266. (uint8_t *)buf);
  267. if (err && !mtd_is_bitflip(err)) {
  268. pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
  269. offset, err);
  270. continue;
  271. }
  272. /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
  273. if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
  274. bcm47xxpart_add_part(&parts[curr_part++], "board_data",
  275. offset, MTD_WRITEABLE);
  276. continue;
  277. }
  278. }
  279. /* Look for NVRAM at the end of the last block. */
  280. for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
  281. if (curr_part >= BCM47XXPART_MAX_PARTS) {
  282. pr_warn("Reached maximum number of partitions, scanning stopped!\n");
  283. break;
  284. }
  285. offset = master->size - possible_nvram_sizes[i];
  286. err = mtd_read(master, offset, 0x4, &bytes_read,
  287. (uint8_t *)buf);
  288. if (err && !mtd_is_bitflip(err)) {
  289. pr_err("mtd_read error while reading (offset 0x%X): %d\n",
  290. offset, err);
  291. continue;
  292. }
  293. /* Standard NVRAM */
  294. if (buf[0] == NVRAM_HEADER) {
  295. bcm47xxpart_add_part(&parts[curr_part++], "nvram",
  296. master->size - blocksize, 0);
  297. break;
  298. }
  299. }
  300. kfree(buf);
  301. /*
  302. * Assume that partitions end at the beginning of the one they are
  303. * followed by.
  304. */
  305. for (i = 0; i < curr_part; i++) {
  306. u64 next_part_offset = (i < curr_part - 1) ?
  307. parts[i + 1].offset : master->size;
  308. parts[i].size = next_part_offset - parts[i].offset;
  309. }
  310. /* If there was TRX parse it now */
  311. for (i = 0; i < trx_num; i++) {
  312. struct mtd_partition *trx = &parts[trx_parts[i]];
  313. if (i == bcm47xxpart_bootpartition()) {
  314. int num_parts;
  315. num_parts = bcm47xxpart_parse_trx(master, trx,
  316. parts + curr_part,
  317. BCM47XXPART_MAX_PARTS - curr_part);
  318. if (num_parts > 0)
  319. curr_part += num_parts;
  320. } else {
  321. trx->name = "failsafe";
  322. }
  323. }
  324. *pparts = parts;
  325. return curr_part;
  326. };
  327. static struct mtd_part_parser bcm47xxpart_mtd_parser = {
  328. .parse_fn = bcm47xxpart_parse,
  329. .name = "bcm47xxpart",
  330. };
  331. module_mtd_part_parser(bcm47xxpart_mtd_parser);
  332. MODULE_LICENSE("GPL");
  333. MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");