Skip to contentSkip to conversation

Handling ubinized image flashed on not erased flash space

DISCUSSION Rafał Miłecki 1 participant 2 messages Mar 11, 2022 – Mar 18, 2022 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

Déjà vu? Yes! Check:
[PATCH RFC 2/2] ubi: add support for UBI_EC_FLAG_ERASE_FROM_HERE
https://patchwork.ozlabs.org/project/linux-mtd/patch/[email protected]/

My problem is that some bootloaders don't erase flash partition before writing a new firmware image. UBI won't like such flash garbage:

ubi0: attaching mtd2
ubi0 error: ubi_attach: bad image sequence number 1646161998 in PEB 119, expected 524503983 Erase counter header dump:

        magic          0x55424923
        version        1
        ec             1
        vid_hdr_offset 2048
        data_offset    4096
        image_seq      1646161998
        hdr_crc        0x1eb28994

erase counter header hexdump:
ubi0 error: ubi_attach_mtd_dev: failed to attach mtd2, error -22 UBI error: cannot attach mtd2

This problem affects e.g. Broadcom's CFE bootloader. There is no always a way to workaround that or it may require some user-unavailable tricks (not every user has a serial console to start with).

OpenWrt handles that by:
1. Appending EOF marker to the ubinized image
2. Modifying UBI kernel code to erase everything past EOF marker

I'm looking for a Buildroot solution now. Last time I was told that upstream kernel is no place for such hacks. It was suggested to use initramfs with user-space tool to deal with it instead.

My question is: how to do that? I need to erase flash partition starting with some middle block (the first after ubinized image). I need to get that offset somehow.

I *can't* scan block after block looking for the first one without a matching EC magic. That wouldn't work with the same ubinized image flashed twice.

Reading volumes table, each volume size, trying to calculate ubinized image size on that data - sounds like overkill for user-space app.

Any suggestions, please?

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#1 lore raw
Quoted 4 lines · Rafał Miłecki

My question is: how to do that? I need to erase flash partition starting with some middle block (the first after ubinized image). I need to get that offset somehow.

Quoted 8 lines

I *can't* scan block after block looking for the first one without a matching EC magic. That wouldn't work with the same ubinized image flashed twice.

Reading volumes table, each volume size, trying to calculate ubinized image size on that data - sounds like overkill for user-space app.

Any suggestions, please?

I'd like to provide some summary for further researchers.

I had some discussion on this with Richard on #mtd on Wednesday and it seems there isn't a reliable way for finding end of ubinized image on flash.

I was suggested to use a custom flash block as an EOF (End Of UBI?) marker. Then detect such a marker from either: user-space or kernel:

[2022-03-16] [12:54:39 CET] <derRichard> one more thing: i don't have a problem with in-kernel cleanup code. i have a problem with a changed ubi disk format.

Sadly it seems a kernel solution for the whole issue won't get accepted as upstream code:

[2022-03-16] [14:27:05 CET] <rmilecki> derRichard: you wrote "i don't have a problem with in-kernel cleanup code" [2022-03-16] [14:27:11 CET] <rmilecki> derRichard: did you mean downstream or upstream? [2022-03-16] [14:27:19 CET] <derRichard> downstream
[2022-03-16] [14:27:33 CET] <derRichard> look, we cannot add such hacks upstream
[2022-03-16] [14:28:14 CET] <rmilecki> we also can't figure out any non-hack way to support those case [2022-03-16] [14:28:16 CET] <rmilecki> *cases
[2022-03-16] [14:28:24 CET] <rmilecki> which is disappointing
[2022-03-16] [14:28:36 CET] <derRichard> since the bootloader hates you...

So basically we just circled back. The best solution seems to be the one implemented & used in OpenWrt for decades.

Personally I'm a bit disappointed. I'll need to carry on a workaround for those poor bootloaders in every project (OpenWrt / Buildroot / Yocto / whatever).

Few MTD guys kept nagging OpenWrt for its downstream mtd hacks but it appears there is no idea or place for an upstream solution. Unless we reverse engineer those poor bootloaders and replace them with U-Boot.

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/