I am porting an open-source bootloader to a commercial project I am working on. Unfortunately, the memory footprint size of this bootloader is larger than the 8kb of the maximum flash bootloader section of AT90CAN128 chip (not supported in the simulator) and I have to split the bootloader within the memory into two sections so that the flash manipulation functions to be positioned in the bootloader section.
I have tried positioning my flash write function to the address 0x1E000, which is the starting address of the bootloader section, by forcing its address as:
blt_bool __attribute__((at(0x1E000))) FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data);
The application crashes, or runs for a few seconds and crashes. It looks like the AVR is not very happy about the position-independent code.
Maybe I need to transfer all the functions that are called from this function etc, hence the whole section .booloader; but I am having difficulties doing that. When I set flags for the Linker via the IDE such as -Tbootloader, or LDSECTION = --section-start=.bootloader=0x1E000 etc give me build errors (-Ttext=0x1E000 works but it is not what I need in this case):
blt_bool __attribute__ ((section (".bootloader"))) FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data);
How do I solve this problem of a bootloader not fitting in the bootloader section? (Note: I have not tried the size optimization because I do not have a license for the XC8 Pro Compiler).
In the worst case that having a large bootloader is a dead end, I am thinking of having three applications in memory: the ported bootloader to manage the file transfer, etc, the main application of course, and another bootloader at the end of the flash dedicated to copy data to flash from another source (maybe an external flash).
I have tried this simple nicely written example that I found on this website and I am convinced that it worked for me too by observing the flash contents after doing a read of flash, also commenting the flash write function and do another read of flash, and comparing the differences: