Understanding the difference between bootloaders mboot and hw dfu

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Thu Dec 20, 2018 12:49 pm

Hello Everyone!!

My question may be wrong. If so please point it out.

Why would we need a boot-loader stuff like mboot, while the stm32 controllers by themselves are factory programmed with hw DFU boot-loader.

Thanks in advance!!
lnsri22 :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by dhylands » Thu Dec 20, 2018 2:57 pm

The factory bootloader has limitations, like it only works on FS USB and not HS USB.

You might also want to bootload over alternate interfaces depending on your HW architecture.

The built-in bootloader only supports writing to internal flash. Mboot supports writing to an external flash chip as well.

lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Fri Dec 21, 2018 9:43 am

Dave,

You made my day!!

I were about to just go ahead and design a custom hardware based on USB HS DFU.

Thanks for pointing this out!!
lnsri22 :)

lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Wed Jan 02, 2019 4:10 pm

Hi Dave!!,

I am trying to modify the mboot configuration in order to suit my ota needs. Which approach would be better?

A) Using a bootloader like this : https://github.com/akospasztor/stm32-bootloader

or

B) trying something like this as Damien has suggested

https://github.com/micropython/micropyt ... t-31795982

Thanks in advance!!
lnsri22 :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by dhylands » Wed Jan 02, 2019 6:51 pm

It really boils down to doing a careful analysis of your particular use-case and deciding which bootloader is closer to what you ultimately want.

lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Fri Jan 18, 2019 11:27 am

Thank you so much Dave !!

By the way, I have reached half-way, implementing a custom bootloader for my custom hw.

I have my bootloader in 0x08000000 .

This looks for a bin file in sd card on startup and if the fie is present , it erases the flash memory from 0x08020000 upto 11th sector (I'm using STM32F405Vg). Then parses the bin file and loads the contents into the flash memory.

So far I have been successful upto erasing and writing to flash.

But whenever , I try to Jump to application, the hw remains silent.

For your kind information,
1. My custom bootloader is of 27K size.
2. I have put up ISR in 0x08008000
3. Shifted the file system to 0x0800C000
4. And finally the Firmware to 0x08020000

I'm doing a jump to 0x08008000. But the hw doesn't respond.

What am I missing??

Thanks in advance!!
lnsri22 :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by dhylands » Fri Jan 18, 2019 9:41 pm

Here's the layout of the beginning of the vector table stored at 0x8000000 (for the normal pyboard):
https://github.com/micropython/micropyt ... 2f4.s#L143

so 0x8000000 contains the stack address and 0x8000004 contains the address of the reset handler.(called Reset_Handler. You can verify the address of Reset_Handler in the .map file. For my build it was 0x804d580

In your case, you've moved the vector table to 0x08008000, your bootloader should be loading the stack pointer from 0x8008000 and loading the reset handler from 0x8008004 and then jumping to the address of the reset handler and not jumping to 0x8008000.

This is where mboot does this (from C):
https://github.com/micropython/micropyt ... in.c#L1207

lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Sat Jan 19, 2019 6:08 am

Thanks again Dave for the clear picture!!

I am always getting the Reset_Handler address +1 as a return value from the below. Hence I have manually compensated it by subtracting '1' from the value as below.

This way, now the hw restarts infinitely and enters the bootloader again and again.

This is what I have been doing.

print("Jumping to application\n");

uint32_t msp = *(volatile uint32_t*)(0x08008000);

uint32_t JumpAddress = *((volatile uint32_t*)(0x08008000+4));
sprintf(msg,"%lu",JumpAddress);
print("JumpAddress : ");
print(msg);
print("\n");
JumpAddress = JumpAddress - 1;
sprintf(msg,"%lu",JumpAddress);
print("JumpAddress : ");
print(msg);
print("\n");
pFunction Jump = (pFunction)JumpAddress;

HAL_RCC_DeInit();
HAL_DeInit();

SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;

#if (SET_VECTOR_TABLE)
SCB->VTOR = 0x08008000;
#endif
__set_MSP(msp);
Jump();

For your reference, the definition of pFunction is "typedef void (*pFunction)(void);"

Also Dave, thought of using MBOOT configuration by using 'USE_MBOOT=1' (basically to use the custom boot-loader instead of mboot) and to use the firmware at 0x08020000.

This at least run my firmware once successfully until a reset. After reset, it won't run and the hw remained silent as it were before!!.

I have also attached the screen-shot of the Linker Script that I have been using.
linker screen-shot.PNG
linker screen-shot.PNG (39.3 KiB) Viewed 6526 times
Do I have to change something here at linker?

Additional Information - I am loading custom boot-loader @0x08000000 and firmware0.bin @0x08008000 . Bootloader parses the file and loads the bin file @0x08020000.

Please advice
lnsri22 :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by dhylands » Sat Jan 19, 2019 7:18 pm

You shouldn't be adjusting the address.

ARM supports what's referred to as 32-bit instruction set ARM and Thumb/Thumb2 (which uses 16-bit instructions). On processors which support it, you're allowed to call 32-bit instructions from 16-bit instructions and vice-versa. Since code always lives on a 16 or 32-bit boundary, the lowest bit of the register value is used to tell the processor if the destination is 32-bit or 16-bit. If the low bit is 0, then the destination is expected to contain 32-bit code and if the lowest bit is a 1 then the destination is expected to contain 16-bit code.

The Cortex-M series of processors only support Thumb instructions (i.e. 16-bit instructions) so destination addresses all need to be odd even when the code actually lives at an even address. By subtracting 1 from the address you're indicating that the destination is 32-bit instructions which aren't supported by this processor. This probably causes an exception and causes the processor to reset.

lnsri22
Posts: 75
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding the difference between bootloaders mboot and hw dfu

Post by lnsri22 » Sun Jan 20, 2019 5:09 am

Dave, Thanks for the reply there!!

Thank you so much for the clarity on this. I understand that I should not subtract '1' from the offset.

But this doesn't seem to cause any exception as the processor remains silent after the jump statement is executed!! (behaves the same way even after removing '-1' from the offset.


Please advice!!
lnsri22 :)

Post Reply