141-0002-mtd-bcm47xxpart-support-layouts-with-multiple-TRX-pa.patch 3.2 KB

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