1
0

060-0004-mtd-bcm47xxpart-move-TRX-parsing-code-to-separated-f.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. From b522d7b0ebe3539340c2a6d46d787ae3d33bcb92 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:24 +0100
  4. Subject: [PATCH] mtd: bcm47xxpart: move TRX parsing code to separated function
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This change simplifies main parsing loop logic a bit. In future it may
  9. be useful for moving TRX support to separated module / parser (if we
  10. implement support for them at some point).
  11. Finally parsing TRX at the end puts us in a better position as we have
  12. better flash layout knowledge. It may be useful e.g. if it appears there
  13. is more than 1 TRX partition.
  14. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  15. Acked-by: Marek Vasut <marek.vasut@gmail.com>
  16. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
  17. ---
  18. drivers/mtd/bcm47xxpart.c | 121 ++++++++++++++++++++++++++++------------------
  19. 1 file changed, 74 insertions(+), 47 deletions(-)
  20. --- a/drivers/mtd/bcm47xxpart.c
  21. +++ b/drivers/mtd/bcm47xxpart.c
  22. @@ -83,6 +83,67 @@ out_default:
  23. return "rootfs";
  24. }
  25. +static int bcm47xxpart_parse_trx(struct mtd_info *master,
  26. + struct mtd_partition *trx,
  27. + struct mtd_partition *parts,
  28. + size_t parts_len)
  29. +{
  30. + struct trx_header header;
  31. + size_t bytes_read;
  32. + int curr_part = 0;
  33. + int i, err;
  34. +
  35. + if (parts_len < 3) {
  36. + pr_warn("No enough space to add TRX partitions!\n");
  37. + return -ENOMEM;
  38. + }
  39. +
  40. + err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
  41. + (uint8_t *)&header);
  42. + if (err && !mtd_is_bitflip(err)) {
  43. + pr_err("mtd_read error while reading TRX header: %d\n", err);
  44. + return err;
  45. + }
  46. +
  47. + i = 0;
  48. +
  49. + /* We have LZMA loader if offset[2] points to sth */
  50. + if (header.offset[2]) {
  51. + bcm47xxpart_add_part(&parts[curr_part++], "loader",
  52. + trx->offset + header.offset[i], 0);
  53. + i++;
  54. + }
  55. +
  56. + if (header.offset[i]) {
  57. + bcm47xxpart_add_part(&parts[curr_part++], "linux",
  58. + trx->offset + header.offset[i], 0);
  59. + i++;
  60. + }
  61. +
  62. + if (header.offset[i]) {
  63. + size_t offset = trx->offset + header.offset[i];
  64. + const char *name = bcm47xxpart_trx_data_part_name(master,
  65. + offset);
  66. +
  67. + bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
  68. + i++;
  69. + }
  70. +
  71. + /*
  72. + * Assume that every partition ends at the beginning of the one it is
  73. + * followed by.
  74. + */
  75. + for (i = 0; i < curr_part; i++) {
  76. + u64 next_part_offset = (i < curr_part - 1) ?
  77. + parts[i + 1].offset :
  78. + trx->offset + trx->size;
  79. +
  80. + parts[i].size = next_part_offset - parts[i].offset;
  81. + }
  82. +
  83. + return curr_part;
  84. +}
  85. +
  86. static int bcm47xxpart_parse(struct mtd_info *master,
  87. const struct mtd_partition **pparts,
  88. struct mtd_part_parser_data *data)
  89. @@ -93,9 +154,7 @@ static int bcm47xxpart_parse(struct mtd_
  90. size_t bytes_read;
  91. uint32_t offset;
  92. uint32_t blocksize = master->erasesize;
  93. - struct trx_header *trx;
  94. int trx_part = -1;
  95. - int last_trx_part = -1;
  96. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  97. int err;
  98. @@ -182,54 +241,14 @@ static int bcm47xxpart_parse(struct mtd_
  99. /* TRX */
  100. if (buf[0x000 / 4] == TRX_MAGIC) {
  101. - if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
  102. - pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
  103. - break;
  104. - }
  105. -
  106. - trx = (struct trx_header *)buf;
  107. + struct trx_header *trx;
  108. trx_part = curr_part;
  109. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  110. offset, 0);
  111. - i = 0;
  112. - /* We have LZMA loader if offset[2] points to sth */
  113. - if (trx->offset[2]) {
  114. - bcm47xxpart_add_part(&parts[curr_part++],
  115. - "loader",
  116. - offset + trx->offset[i],
  117. - 0);
  118. - i++;
  119. - }
  120. -
  121. - if (trx->offset[i]) {
  122. - bcm47xxpart_add_part(&parts[curr_part++],
  123. - "linux",
  124. - offset + trx->offset[i],
  125. - 0);
  126. - i++;
  127. - }
  128. -
  129. - /*
  130. - * Pure rootfs size is known and can be calculated as:
  131. - * trx->length - trx->offset[i]. We don't fill it as
  132. - * we want to have jffs2 (overlay) in the same mtd.
  133. - */
  134. - if (trx->offset[i]) {
  135. - const char *name;
  136. -
  137. - name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
  138. - bcm47xxpart_add_part(&parts[curr_part++],
  139. - name,
  140. - offset + trx->offset[i],
  141. - 0);
  142. - i++;
  143. - }
  144. -
  145. - last_trx_part = curr_part - 1;
  146. -
  147. /* Jump to the end of TRX */
  148. + trx = (struct trx_header *)buf;
  149. offset = roundup(offset + trx->length, blocksize);
  150. /* Next loop iteration will increase the offset */
  151. offset -= blocksize;
  152. @@ -307,9 +326,17 @@ static int bcm47xxpart_parse(struct mtd_
  153. parts[i + 1].offset : master->size;
  154. parts[i].size = next_part_offset - parts[i].offset;
  155. - if (i == last_trx_part && trx_part >= 0)
  156. - parts[trx_part].size = next_part_offset -
  157. - parts[trx_part].offset;
  158. + }
  159. +
  160. + /* If there was TRX parse it now */
  161. + if (trx_part >= 0) {
  162. + int num_parts;
  163. +
  164. + num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
  165. + parts + curr_part,
  166. + BCM47XXPART_MAX_PARTS - curr_part);
  167. + if (num_parts > 0)
  168. + curr_part += num_parts;
  169. }
  170. *pparts = parts;