141-0001-mtd-bcm47xxpart-move-TRX-parsing-code-to-separated-f.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
  2. Subject: [PATCH 1/2] mtd: bcm47xxpart: move TRX parsing code to separated
  3. function
  4. MIME-Version: 1.0
  5. Content-Type: text/plain; charset=UTF-8
  6. Content-Transfer-Encoding: 8bit
  7. This change simplifies main parsing loop logic a bit. In future it may
  8. be useful for moving TRX support to separated module / parser (if we
  9. implement support for them at some point).
  10. Finally parsing TRX at the end puts us in a better position as we have
  11. better flash layout knowledge. It may be useful e.g. if it appears there
  12. is more than 1 TRX partition.
  13. Signed-off-by: Rafał Miłecki <[email protected]>
  14. ---
  15. drivers/mtd/bcm47xxpart.c | 124 ++++++++++++++++++++++++++++------------------
  16. 1 file changed, 77 insertions(+), 47 deletions(-)
  17. --- a/drivers/mtd/bcm47xxpart.c
  18. +++ b/drivers/mtd/bcm47xxpart.c
  19. @@ -83,6 +83,70 @@ out_default:
  20. return "rootfs";
  21. }
  22. +static int bcm47xxpart_parse_trx(struct mtd_info *master,
  23. + struct mtd_partition *trx,
  24. + struct mtd_partition *parts,
  25. + size_t parts_len)
  26. +{
  27. + struct mtd_partition *part;
  28. + struct trx_header header;
  29. + size_t bytes_read;
  30. + int curr_part = 0;
  31. + int i, err;
  32. +
  33. + if (parts_len < 3) {
  34. + pr_warn("No enough space to add TRX partitions!\n");
  35. + return -ENOMEM;
  36. + }
  37. +
  38. + err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
  39. + (uint8_t *)&header);
  40. + if (err && !mtd_is_bitflip(err)) {
  41. + pr_err("mtd_read error while reading TRX header: %d\n", err);
  42. + return err;
  43. + }
  44. +
  45. + i = 0;
  46. +
  47. + /* We have LZMA loader if offset[2] points to sth */
  48. + if (header.offset[2]) {
  49. + part = &parts[curr_part++];
  50. + part->name = "loader";
  51. + part->offset = trx->offset + header.offset[i];
  52. + i++;
  53. + }
  54. +
  55. + if (header.offset[i]) {
  56. + part = &parts[curr_part++];
  57. + part->name = "linux";
  58. + part->offset = trx->offset + header.offset[i];
  59. + i++;
  60. + }
  61. +
  62. + if (header.offset[i]) {
  63. + size_t offset = trx->offset + header.offset[i];
  64. +
  65. + part = &parts[curr_part++];
  66. + part->name = bcm47xxpart_trx_data_part_name(master, offset);
  67. + part->offset = offset;
  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. struct mtd_partition **pparts,
  88. struct mtd_part_parser_data *data)
  89. @@ -93,9 +157,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 +244,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 +329,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;