cancel
Showing results for 
Search instead for 
Did you mean: 

basic bootloader for M0

stenasc
Senior
Posted on June 20, 2016 at 23:21

Hello Forum,

I've been reading on the forums about custom bootloaders stored in flash and I was wondering if there was another possibility (possibly because I want to avoid messing with stack pointers, reset vectors etc.)

In my main app, I have a bootloader function, that copies the new app stored in external flash to processor flash. This bootloader function contains a number of inline functions to read from external flash and copy to processor flash. Checking the .map file, this bootloader function looks to be all located in one place.

My thought process is to copy this bootloader function into RAM at say 0x20000100 and then jump to that address as shown below

void CopyLoaderToRam(void)

{

 //   uint32_t *Ram = (uint32_t *)RAM_ADDR;

    char *Ram = (char *)RAM_ADDR;///////////// Byte Alignment test

    typedef  void (*pFunction)(void);

    pFunction RunCode;

 ptr_bootloader_fn = &bootloader;    // Find the address of the bootloader function.  

   

//    memcpy(Ram, (void *)ptr_bootloader_fn, LOADER_FLASH_SIZE); // Copy bootloader into ram

   

    

      char *boot_byte;

      boot_byte = (char *)&bootloader;

      int i;

      for (i=0; i<= 4*LOADER_FLASH_SIZE; i++)

          {

                Ram[i] = *(boot_byte+i);    

                }

             

    RunCode = (pFunction)Ram[1];       // ResetHandler (PC in second vector)

     

    RunCode();

}

Well the first issue is it doesnt seem to work as I get a hard fault.My fears are that I am overwriting some necessary data in Ram or some alignment issue. I'm not sure. 

However, is it even technically feasible in the first place and if not, why? If it is a non-starter, I would rather know at this stage. Device is a STM32F051.

Kind Regards

Bob
43 REPLIES 43
stenasc
Senior
Posted on August 15, 2016 at 10:12

Hi Clive,

As the bootloader runs from memory, I would have thought that there should be no requirement to maintain a separate area and that all flash could be overwritten. Am I wrong in this assumption?

Bob 

stenasc
Senior
Posted on August 15, 2016 at 10:14

Hi Clive,

As the bootloader runs from memory, I would have thought that there should be no requirement to maintain a separate area and that all flash could be overwritten. Am I wrong in this assumption?

Bob 

Posted on August 15, 2016 at 13:12

Sure you can do that, the problem lies where the end-user removes the power mid-process, or it fails for some other reason, and you are left with a bricked device.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stenasc
Senior
Posted on August 15, 2016 at 16:03

OK Clive,

Got you loud and clear.

Bob