Skip to content

Commit 237ea0d

Browse files
Rafał MiłeckiBoris Brezillon
authored andcommitted
mtd: bcm47xxpart: improve handling TRX partition size
When bcm47xxpart finds a TRX partition (container) it's supposed to jump to the end of it and keep looking for more partitions. TRX and its subpartitions are handled by a separate parser. The problem with old code was relying on the length specified in a TRX header. That isn't reliable as TRX is commonly modified to have checksum cover only non-changing subpartitions. Otherwise modifying e.g. a rootfs would result in CRC32 mismatch and bootloader refusing to boot a firmware. Fix it by trying better to figure out a real TRX size. We can securely assume that TRX has to cover all subpartitions and the last one is at least of a block size in size. Then compare it with a length field. This makes code more optimal & reliable thanks to skipping data that shouldn't be parsed. Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
1 parent 5ac67ce commit 237ea0d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

drivers/mtd/bcm47xxpart.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ static int bcm47xxpart_parse(struct mtd_info *master,
186186
/* TRX */
187187
if (buf[0x000 / 4] == TRX_MAGIC) {
188188
struct trx_header *trx;
189+
uint32_t last_subpart;
190+
uint32_t trx_size;
189191

190192
if (trx_num >= ARRAY_SIZE(trx_parts))
191193
pr_warn("No enough space to store another TRX found at 0x%X\n",
@@ -195,11 +197,23 @@ static int bcm47xxpart_parse(struct mtd_info *master,
195197
bcm47xxpart_add_part(&parts[curr_part++], "firmware",
196198
offset, 0);
197199

198-
/* Jump to the end of TRX */
200+
/*
201+
* Try to find TRX size. The "length" field isn't fully
202+
* reliable as it could be decreased to make CRC32 cover
203+
* only part of TRX data. It's commonly used as checksum
204+
* can't cover e.g. ever-changing rootfs partition.
205+
* Use offsets as helpers for assuming min TRX size.
206+
*/
199207
trx = (struct trx_header *)buf;
200-
offset = roundup(offset + trx->length, blocksize);
201-
/* Next loop iteration will increase the offset */
202-
offset -= blocksize;
208+
last_subpart = max3(trx->offset[0], trx->offset[1],
209+
trx->offset[2]);
210+
trx_size = max(trx->length, last_subpart + blocksize);
211+
212+
/*
213+
* Skip the TRX data. Decrease offset by block size as
214+
* the next loop iteration will increase it.
215+
*/
216+
offset += roundup(trx_size, blocksize) - blocksize;
203217
continue;
204218
}
205219

0 commit comments

Comments
 (0)