am335x: failsafe bootloader update
Subsystems (3)
v2 cover am335x: failsafe bootloader update
Make it possible to update the (whole) bootloader on am335x robustly, i.e. so that a power failure or random OOM killing of the update process or other interruptions do not result in a bricked board.
The order the trial bits gets set is somewhat odd, but is clearly what happens, and somebody else trying to reverse engineer the ROM code has observed the same thing: See the TracingVectors.ods in
https://github.com/sjgallagher2/am335xbootrom .
v2: Add patch updating documentation, including a sketch of shell commands that could be used for the actual update procedure.
v1: https://lore.kernel.org/u-boot/[email protected]/
Rasmus Villemoes (2):
am335x: add support for loading u-boot from multiple offsets
doc: am335x: document use of CONFIG_SPL_AM33XX_MMCSD_MULTIPLE
arch/arm/mach-omap2/Kconfig | 37 ++++++++++++++ arch/arm/mach-omap2/boot-common.c | 31 ++++++++++++ doc/board/ti/am335x_evm.rst | 80 +++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+)
Signature · 1 line
2.52.0
PATCH v2 1/2 am335x: add support for loading u-boot from multiple offsets
The am335x knows nothing about eMMC boot partitions, so in order to implement an update procedure of the bootloader which is robust against power failure or other interruptions, one must make use of the fact that ROM code on the am335x looks for a valid first stage bootloader at several different offsets. Updating that can then be implemented by (assume we put MLO at offsets 128K and 256K):
(1) At least one of the two slots must contain a valid header, since we successfully booted. Pick the other one.
(2) Overwrite the first sector of the slot chosen in step (a) with all zeroes.
(3) Write everything but the first sector to the chosen slot.
(4) Write the first sector (containing the magic signature that the boot ROM uses to identify a valid image) to the chosen slot.
(5) Repeat steps (2)-(4) for the other slot.
It's not possible to simply write the whole MLO in one go, especially not when updating the 128K slot, because an interruption after the first sector is written would make the ROM code believe the image is valid.
But this only caters for SPL itself; regardless of where SPL was loaded from, it would go on to load U-Boot proper from SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR. So in order to update the whole bootloader, we need to teach SPL to use a different offset for U-Boot proper depending on where SPL itself was loaded from (*). With that, the update procedure is just amended by a step
(3a) Write U-Boot proper to the offset corresponding to the SPL slot being updated.
We can know (*) because the ROM code sets a new bit in a certain "trace vector" before each successive attempt.
--- arch/arm/mach-omap2/Kconfig | 37 +++++++++++++++++++++++++++++++ arch/arm/mach-omap2/boot-common.c | 31 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
arch/arm/mach-omap2/Kconfig +37 -0
@@ -156,6 +156,43 @@ config SYS_DEFAULT_LPDDR2_TIMINGS endchoice +config SPL_AM33XX_MMCSD_MULTIPLE + bool "Support multiple locations of next boot phase" + depends on AM33XX + depends on SPL_SYS_MMCSD_RAW_MODE + depends on SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR + help + The boot ROM on the am33xx looks for the first stage + bootloader at several hard-coded offsets in the mmc device + (0, 128K, 256K, 384K) and uses the first location which has + a valid header. This can be used to implement failsafe + update of that first stage (SPL). But in order for the + update of the whole bootloader to be failsafe, SPL must load + U-Boot proper from a location dependent on where SPL itself + was loaded from. This option allows you to specify four + different offsets corresponding to the different places + where SPL could have been loaded from. + +if SPL_AM33XX_MMCSD_MULTIPLE + +config SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_0K + hex "Address on the MMC to load U-Boot from when SPL was loaded from offset 0K" + default SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR + +config SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_128K + hex "Address on the MMC to load U-Boot from when SPL was loaded from offset 128K" + default SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR + +config SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_256K + hex "Address on the MMC to load U-Boot from when SPL was loaded from offset 256K" + default SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR + +config SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_384K + hex "Address on the MMC to load U-Boot from when SPL was loaded from offset 384K" + default SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR + +endif + source "arch/arm/mach-omap2/omap3/Kconfig" source "arch/arm/mach-omap2/omap5/Kconfig"
arch/arm/mach-omap2/boot-common.c +31 -0
@@ -318,3 +318,34 @@ static void tee_image_process(ulong tee_image, size_t tee_size) }U_BOOT_FIT_LOADABLE_HANDLER (IH_TYPE_TEE , tee_image_process);#endif + +#ifdef CONFIG_SPL_AM33XX_MMCSD_MULTIPLE + +#define AM335X_TRACE_VECTOR2 0x4030CE44 + +unsigned long arch_spl_mmc_get_uboot_raw_sector (struct mmc *mmc,unsigned long raw_sect) +{ +u32 bits = *(u32 *)AM335X_TRACE_VECTOR2 ; + + bits &=0xf000 ; + +/* +* The ROM code sets the "trial bit 3", bit 15, first, when +* attempting offset 0, then "trial bit 2", bit 14, when +* attempting offset 128K, and so on. If the tracing vector +* has completely unexpected contents, fall back to the +* raw_sect we were given. +*/ +switch (bits) { +case 0x8000 : raw_sect =CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_0K ;break ; +case 0xc000 : raw_sect =CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_128K ;break ; +case 0xe000 : raw_sect =CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_256K ;break ; +case 0xf000 : raw_sect =CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_384K ;break ; +default : +printf ("Warning: Unexpected trial bits 0x%04x in trace vector 2, falling back to 0x%lx\n" , + bits, raw_sect); + } + +return raw_sect; +} +#endif
Signature · 1 line
2.52.0
PATCH v2 2/2 doc: am335x: document use of CONFIG_SPL_AM33XX_MMCSD_MULTIPLE
In reply to Rasmus VillemoesDocument how one can use the CONFIG_SPL_AM33XX_MMCSD_MULTIPLE option to implement a scheme for failsafe updating of the whole bootloader on the am335x.
--- doc/board/ti/am335x_evm.rst | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
doc/board/ti/am335x_evm.rst +80 -0
@@ -481,3 +481,83 @@ bind with it: misc 0 [ + ] ti-musb-wrapper | |-- usb@47400000 usb 0 [ ] ti-musb-peripheral | | |-- usb@47401000 usb 0 [ ] ti-musb-host | | `-- usb@47401800 + +Failsafe bootloader update +-------------------------- + +As indicated above, the ROM code on the AM335x supports loading SPL +from one of several different locations. It looks at offsets 0, +128KiB, 256KiB and 384KiB (sectors 0, 0x100, 0x200, 0x300) for a +sector containing a valid "TOC structure" (see the reference manual +for details). + +This can be used to implement a scheme for updating SPL which is +robust against power failure or other interruptions: Suppose we store +copies of SPL (wrapped in the "MLO" image, which is what includes that +TOC structure) at offsets 128KiB and 256KiB, and let us refer to those +two locations as slot 1 and slot 2. + +The whole procedure maintains the invariant that at any time, at +least one of the slots contains a complete and valid MLO image. In +order to update SPL: + +(1) Determine a slot X containing a valid image (by having a proper + TOC structure in the first sector). Designate the other + slot Y. Since the TOC is always the same 512 bytes (see section + 26.1.11 in the reference manual), checking for a valid image can + be done using something like + +.. code-block:: bash + + if cmp -s -n 512 MLO /path/to/SPL-1 ; then + X=1 + Y=2 + elif cmp -s -n 512 MLO /path/to/SPL-2 ; then + X=2 + Y=1 + else + # invariant broken, fatal error, refuse update... + fi + +(2) Ensure Y will be deemed invalid by the ROM code by writing all + zeroes to the first sector of Y, for example using + +.. code-block:: bash + + dd if=/dev/zero of=/path/to/SPL-Y bs=512 count=1 conv=fsync + +(3) Write everything but the first sector of the MLO image to slot Y: + +.. code-block:: bash + + dd if=MLO of=/path/to/SPL-Y bs=512 skip=1 seek=1 conv=fsync + +(4) Write the TOC structure to slot Y: + +.. code-block:: bash + + dd if=MLO of=/path/to/SPL-Y bs=512 count=1 conv=fsync + +(5) Repeat steps (2)--(4) for slot X. + +Now, this procedure only accounts for safely updating SPL. If U-Boot +proper is only stored in a single location, there is no way to update +that which is safe against powercut during the update. However, by +selecting the configuration option CONFIG_SPL_AM33XX_MMCSD_MULTIPLE, +you can tell SPL to load U-Boot proper from a location which depends on +where SPL itself was loaded from. Hence, one can for example put +copies of u-boot.img at offsets 512KiB and 1536KiB, and set + +:: + + CONFIG_SPL_AM33XX_MMCSD_MULTIPLE=y + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_128K=0x400 + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR_256K=0xc00 + +and amend the step (3) above by + + Write U-Boot proper to the location corresponding to slot Y: + +.. code-block:: bash + + dd if=u-boot.img of=/path/to/U-BOOT-Y bs=512 conv=fsync
Signature · 1 line
2.52.0
Quoted 10 lines · Rasmus Villemoes
Make it possible to update the (whole) bootloader on am335x robustly, i.e. so that a power failure or random OOM killing of the update process or other interruptions do not result in a bricked board.
The order the trial bits gets set is somewhat odd, but is clearly what happens, and somebody else trying to reverse engineer the ROM code has observed the same thing: See the TracingVectors.ods in
https://github.com/sjgallagher2/am335xbootrom .[...]
Applied to u-boot/next, thanks!
[1/2] am335x: add support for loading u-boot from multiple offsets
commit: 589d0a687b020fa6000afec454ec82f192613ed5
[2/2] doc: am335x: document use of CONFIG_SPL_AM33XX_MMCSD_MULTIPLE
commit: a672456d4b2bd216dcfe9d09fb5fbce2b013735aSignature · 1 line
Tom