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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  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 <rafal@milecki.pl>
  14. ---
  15. drivers/mtd/bcm47xxpart.c | 121 ++++++++++++++++++++++++++++------------------
  16. 1 file changed, 74 insertions(+), 47 deletions(-)
  17. --- a/drivers/mtd/bcm47xxpart.c
  18. +++ b/drivers/mtd/bcm47xxpart.c
  19. @@ -83,6 +83,67 @@ 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 trx_header header;
  28. + size_t bytes_read;
  29. + int curr_part = 0;
  30. + int i, err;
  31. +
  32. + if (parts_len < 3) {
  33. + pr_warn("No enough space to add TRX partitions!\n");
  34. + return -ENOMEM;
  35. + }
  36. +
  37. + err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
  38. + (uint8_t *)&header);
  39. + if (err && !mtd_is_bitflip(err)) {
  40. + pr_err("mtd_read error while reading TRX header: %d\n", err);
  41. + return err;
  42. + }
  43. +
  44. + i = 0;
  45. +
  46. + /* We have LZMA loader if offset[2] points to sth */
  47. + if (header.offset[2]) {
  48. + bcm47xxpart_add_part(&parts[curr_part++], "loader",
  49. + trx->offset + header.offset[i], 0);
  50. + i++;
  51. + }
  52. +
  53. + if (header.offset[i]) {
  54. + bcm47xxpart_add_part(&parts[curr_part++], "linux",
  55. + trx->offset + header.offset[i], 0);
  56. + i++;
  57. + }
  58. +
  59. + if (header.offset[i]) {
  60. + size_t offset = trx->offset + header.offset[i];
  61. + const char *name = bcm47xxpart_trx_data_part_name(master,
  62. + offset);
  63. +
  64. + bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
  65. + i++;
  66. + }
  67. +
  68. + /*
  69. + * Assume that every partition ends at the beginning of the one it is
  70. + * followed by.
  71. + */
  72. + for (i = 0; i < curr_part; i++) {
  73. + u64 next_part_offset = (i < curr_part - 1) ?
  74. + parts[i + 1].offset :
  75. + trx->offset + trx->size;
  76. +
  77. + parts[i].size = next_part_offset - parts[i].offset;
  78. + }
  79. +
  80. + return curr_part;
  81. +}
  82. +
  83. static int bcm47xxpart_parse(struct mtd_info *master,
  84. struct mtd_partition **pparts,
  85. struct mtd_part_parser_data *data)
  86. @@ -93,9 +154,7 @@ static int bcm47xxpart_parse(struct mtd_
  87. size_t bytes_read;
  88. uint32_t offset;
  89. uint32_t blocksize = master->erasesize;
  90. - struct trx_header *trx;
  91. int trx_part = -1;
  92. - int last_trx_part = -1;
  93. int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
  94. int err;
  95. @@ -182,54 +241,14 @@ static int bcm47xxpart_parse(struct mtd_
  96. /* TRX */
  97. if (buf[0x000 / 4] == TRX_MAGIC) {
  98. - if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
  99. - pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
  100. - break;
  101. - }
  102. -
  103. - trx = (struct trx_header *)buf;
  104. + struct trx_header *trx;
  105. trx_part = curr_part;
  106. bcm47xxpart_add_part(&parts[curr_part++], "firmware",
  107. offset, 0);
  108. - i = 0;
  109. - /* We have LZMA loader if offset[2] points to sth */
  110. - if (trx->offset[2]) {
  111. - bcm47xxpart_add_part(&parts[curr_part++],
  112. - "loader",
  113. - offset + trx->offset[i],
  114. - 0);
  115. - i++;
  116. - }
  117. -
  118. - if (trx->offset[i]) {
  119. - bcm47xxpart_add_part(&parts[curr_part++],
  120. - "linux",
  121. - offset + trx->offset[i],
  122. - 0);
  123. - i++;
  124. - }
  125. -
  126. - /*
  127. - * Pure rootfs size is known and can be calculated as:
  128. - * trx->length - trx->offset[i]. We don't fill it as
  129. - * we want to have jffs2 (overlay) in the same mtd.
  130. - */
  131. - if (trx->offset[i]) {
  132. - const char *name;
  133. -
  134. - name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
  135. - bcm47xxpart_add_part(&parts[curr_part++],
  136. - name,
  137. - offset + trx->offset[i],
  138. - 0);
  139. - i++;
  140. - }
  141. -
  142. - last_trx_part = curr_part - 1;
  143. -
  144. /* Jump to the end of TRX */
  145. + trx = (struct trx_header *)buf;
  146. offset = roundup(offset + trx->length, blocksize);
  147. /* Next loop iteration will increase the offset */
  148. offset -= blocksize;
  149. @@ -307,9 +326,17 @@ static int bcm47xxpart_parse(struct mtd_
  150. parts[i + 1].offset : master->size;
  151. parts[i].size = next_part_offset - parts[i].offset;
  152. - if (i == last_trx_part && trx_part >= 0)
  153. - parts[trx_part].size = next_part_offset -
  154. - parts[trx_part].offset;
  155. + }
  156. +
  157. + /* If there was TRX parse it now */
  158. + if (trx_part >= 0) {
  159. + int num_parts;
  160. +
  161. + num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
  162. + parts + curr_part,
  163. + BCM47XXPART_MAX_PARTS - curr_part);
  164. + if (num_parts > 0)
  165. + curr_part += num_parts;
  166. }
  167. *pparts = parts;