Having a bootloader, and that upgrading the firmware is one solution. But then the bootloader can never change.
Instead I have the following idea:
What if I split the flash in half, and while running from the first half, it writes the second half. And when all is well, i switch to second half.
Something like this:
00000 interrupt vectors 00200 application code 1 10000 unused area 10200 application code 2 20000 bootloader area, functions having SPM instructions
So when updating from area 1 to area 2, first i fill from 10200 to 20000
Then change interrupt vectors as atomic as possible, to point to area 2 (this is where it can go wrong)
After reboot area 2 will be active.
(about changing interrupt vectors, i think the best way is to store the interrupt vector table in SRAM.
And then write those pages to flash in one loop, aligning application start to 200 helps, because that way i can update only the vectors, and not the code.)
What do you think, can it work?
As far as I understand only interrupt vectors and SPM instructions are not movable. Is this correct?