I have written a bootloader to reside in my first sector (128Kbyte) of flash which, firstly copies all code and data from flash to RAM and then checks data on an MMC card to see whether an update of the flash is necessary and, if so, copies new program data into the flash. At the moment I have two projects - the bootloader as a stand-alone loader and the full application, which has the bootloader code as part of it.
What I'd like to do is seperate the bootloader code from the application code and have the boot loader pass control to the application once it has done its checks and, if necessary, reprogramming. However, I don't think I could have the application as stand-alone code as well because it would then be re-initialising the hardware and memory of what was initialised previously in the bootloader.
My thoughts were to define the first flash sector as a seperate linker section and locate all the bootloader functions and data in there but as part of the application software. Then, when a reprogram takes place I just don't write to the first sector. The problem with that is that the old bootloader then does not know where to jump to in the application and also the copy from flash to RAM on startup won't know how much there is to copy. I only want to write to the bootloader area when the loader itself is to be updated because, if it fails, JTAG is the only recovery and that won't be available in the field.
There must be a better way of doing this but I'm not sure which direction to proceed.