060-0005-mtd-bcm47xxpart-support-layouts-with-multiple-TRX-pa.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. From 89a0d9a9f1941a086a82bc7cd73d275cec98ba14 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Tue, 10 Jan 2017 23:15:25 +0100
  4. Subject: [PATCH] mtd: bcm47xxpart: support layouts with multiple TRX
  5. partitions
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. Some devices may have an extra TRX partition used as failsafe one. If
  10. we detect such partition we should set a proper name for it and don't
  11. parse it.
  12. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  13. Acked-by: Marek Vasut <marek.vasut@gmail.com>
  14. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
  15. ---
  16. drivers/mtd/bcm47xxpart.c | 56 ++++++++++++++++++++++++++++++++++++++---------
  17. 1 file changed, 46 insertions(+), 10 deletions(-)
  18. --- a/drivers/mtd/bcm47xxpart.c
  19. +++ b/drivers/mtd/bcm47xxpart.c
  20. @@ -9,6 +9,7 @@
  21. *
  22. */
  23. +#include <linux/bcm47xx_nvram.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. @@ -144,6 +145,30 @@ static int bcm47xxpart_parse_trx(struct
  28. return curr_part;
  29. }
  30. +/**
  31. + * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
  32. + *
  33. + * Some devices may have more than one TRX partition. In such case one of them
  34. + * is the main one and another a failsafe one. Bootloader may fallback to the
  35. + * failsafe firmware if it detects corruption of the main image.
  36. + *
  37. + * This function provides info about currently used TRX partition. It's the one
  38. + * containing kernel started by the bootloader.
  39. + */
  40. +static int bcm47xxpart_bootpartition(void)
  41. +{
  42. + char buf[4];
  43. + int bootpartition;
  44. +
  45. + /* Check CFE environment variable */
  46. + if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
  47. + if (!kstrtoint(buf, 0, &bootpartition))
  48. + return bootpartition;
  49. + }
  50. +
  51. + return 0;
  52. +}
  53. +
  54. static int bcm47xxpart_parse(struct mtd_info *master,
  55. const struct mtd_partition **pparts,
  56. struct mtd_part_parser_data *data)
  57. @@ -154,7 +179,8 @@ static int bcm47xxpart_parse(struct mtd_
  58. size_t bytes_read;
  59. uint32_t offset;
  60. uint32_t blocksize = master->erasesize;
  61. - int trx_part = -1;
  62. + int trx_parts[2]; /* Array with indexes of TRX partitions */
  63. + int trx_num = 0; /* Number of found TRX partitions */
  64. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  65. int err;
  66. @@ -243,7 +269,11 @@ static int bcm47xxpart_parse(struct mtd_
  67. if (buf[0x000 / 4] == TRX_MAGIC) {
  68. struct trx_header *trx;
  69. - trx_part = curr_part;
  70. + if (trx_num >= ARRAY_SIZE(trx_parts))
  71. + pr_warn("No enough space to store another TRX found at 0x%X\n",
  72. + offset);
  73. + else
  74. + trx_parts[trx_num++] = curr_part;
  75. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  76. offset, 0);
  77. @@ -329,14 +359,20 @@ static int bcm47xxpart_parse(struct mtd_
  78. }
  79. /* If there was TRX parse it now */
  80. - if (trx_part >= 0) {
  81. - int num_parts;
  82. + for (i = 0; i < trx_num; i++) {
  83. + struct mtd_partition *trx = &parts[trx_parts[i]];
  84. - num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
  85. - parts + curr_part,
  86. - BCM47XXPART_MAX_PARTS - curr_part);
  87. - if (num_parts > 0)
  88. - curr_part += num_parts;
  89. + if (i == bcm47xxpart_bootpartition()) {
  90. + int num_parts;
  91. +
  92. + num_parts = bcm47xxpart_parse_trx(master, trx,
  93. + parts + curr_part,
  94. + BCM47XXPART_MAX_PARTS - curr_part);
  95. + if (num_parts > 0)
  96. + curr_part += num_parts;
  97. + } else {
  98. + trx->name = "failsafe";
  99. + }
  100. }
  101. *pparts = parts;