431-mtd-bcm47xxpart-check-for-bad-blocks-when-calculatin.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
  2. Date: Sat, 2 Jan 2016 01:04:52 +0100
  3. Subject: [PATCH] mtd: bcm47xxpart: check for bad blocks when calculating
  4. offsets
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
  9. ---
  10. drivers/mtd/bcm47xxpart.c | 50 +++++++++++++++++++++++++++++++++++++----------
  11. 1 file changed, 40 insertions(+), 10 deletions(-)
  12. --- a/drivers/mtd/bcm47xxpart.c
  13. +++ b/drivers/mtd/bcm47xxpart.c
  14. @@ -61,6 +61,34 @@ static void bcm47xxpart_add_part(struct
  15. part->mask_flags = mask_flags;
  16. }
  17. +/*
  18. + * Calculate real end offset (address) for a given amount of data. It checks
  19. + * all blocks skipping bad ones.
  20. + */
  21. +static size_t bcm47xxpart_real_offset(struct mtd_info *master, size_t offset,
  22. + size_t bytes)
  23. +{
  24. + size_t real_offset = offset;
  25. +
  26. + if (mtd_block_isbad(master, real_offset))
  27. + pr_warn("Base offset shouldn't be at bad block");
  28. +
  29. + while (bytes >= master->erasesize) {
  30. + bytes -= master->erasesize;
  31. + real_offset += master->erasesize;
  32. + while (mtd_block_isbad(master, real_offset)) {
  33. + real_offset += master->erasesize;
  34. +
  35. + if (real_offset >= master->size)
  36. + return real_offset - master->erasesize;
  37. + }
  38. + }
  39. +
  40. + real_offset += bytes;
  41. +
  42. + return real_offset;
  43. +}
  44. +
  45. static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
  46. size_t offset)
  47. {
  48. @@ -182,6 +210,8 @@ static int bcm47xxpart_parse(struct mtd_
  49. /* TRX */
  50. if (buf[0x000 / 4] == TRX_MAGIC) {
  51. + uint32_t tmp;
  52. +
  53. if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
  54. pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
  55. break;
  56. @@ -196,18 +226,18 @@ static int bcm47xxpart_parse(struct mtd_
  57. i = 0;
  58. /* We have LZMA loader if offset[2] points to sth */
  59. if (trx->offset[2]) {
  60. + tmp = bcm47xxpart_real_offset(master, offset,
  61. + trx->offset[i]);
  62. bcm47xxpart_add_part(&parts[curr_part++],
  63. - "loader",
  64. - offset + trx->offset[i],
  65. - 0);
  66. + "loader", tmp, 0);
  67. i++;
  68. }
  69. if (trx->offset[i]) {
  70. + tmp = bcm47xxpart_real_offset(master, offset,
  71. + trx->offset[i]);
  72. bcm47xxpart_add_part(&parts[curr_part++],
  73. - "linux",
  74. - offset + trx->offset[i],
  75. - 0);
  76. + "linux", tmp, 0);
  77. i++;
  78. }
  79. @@ -219,11 +249,11 @@ static int bcm47xxpart_parse(struct mtd_
  80. if (trx->offset[i]) {
  81. const char *name;
  82. - name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
  83. + tmp = bcm47xxpart_real_offset(master, offset,
  84. + trx->offset[i]);
  85. + name = bcm47xxpart_trx_data_part_name(master, tmp);
  86. bcm47xxpart_add_part(&parts[curr_part++],
  87. - name,
  88. - offset + trx->offset[i],
  89. - 0);
  90. + name, tmp, 0);
  91. i++;
  92. }