Search code examples
embedded-linuxbootloaderatmelflash-memory

Replace bootloader on sama5d3 from the running linux system


I'd like to replace the first stage bootloader in the nand flash on a sama5d36 based system running 4.1.0-linux4sam_5.1 and buildroot-2016.02.

I can replace the kernel image with flashcp just fine, but when I try it with the bootloader, flashcp runs without errors, but the system doesn't boot afterwards, stays at the ROMBOOT prompt.

buildroot:~# flashcp -v at91bootstrap.bin /dev/mtd0
Erasing block: 1/1 (100%) 
Writing kb: 14/14 (100%) 
Verifying kb: 14/14 (100%) 
buildroot:~# reboot
[...]
Sent SIGKILL to all processes
Requesting system reboot
�RomBOOTRestarting system

Then I can write the same bootloader image with sam-ba, and it will boot, so the image is good. How can it be flashed in Linux, without user intervention?


Solution

  • There should be a 208 byte header preceding the actual boot code at the beginning of the flash.

    From the SAMA5D3 Datasheet (that I should have read before posting the question)

    After Initialization and Reset command, the Boot Program reads the first page without an ECC check, to determine if the NAND parameter header is present. The header is made of 52 times the same 32-bit word (for redundancy reasons) which must contain NAND and PMECC parameters used to correctly perform the read of the rest of the data in the NAND.

    The header is of course there when I dump the contents of the boot sector

    buildroot:~# hd < /dev/mtd0 | head -4
    00000000  05 24 90 c0 05 24 90 c0  05 24 90 c0 05 24 90 c0  |.$...$...$...$..|
    *
    000000d0  0e 00 00 ea 05 00 00 ea  05 00 00 ea 05 00 00 ea  |................|
    000000e0  05 00 00 ea cc 3b 00 00  06 00 00 ea 06 00 00 ea  |.....;..........|
    

    the first four bytes are repeated over and over, and the ARM jump table begins at offset 0xD0 (=208=52 * 4)

    sam-ba takes care of this header when it writes the boot sector, but the Linux mtd driver and flashcp treats it as ordinary data, so I should supply it.