How to update application to a different flash location other than what it was compiled for?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to update application to a different flash location other than what it was compiled for?

2,809 Views
subbanna_manjun
Contributor I

Hi,

I am using cortex-M7 MKV56F1M0xxx24 with a custom boot-loader to update application in flash.

Refer to the attachment to know the memory layout used.

1. Is it possible to build a single binary and be able to flash it to both Primary and Secondary area?

If yes, Lets say I want to flash binary to Secondary area, would it need to be specified to start at secondary area during compile time?

2. How do i flash the binary to Secondary area even if the binary is built to start from Primary area? What are the things to be taken care of? (like IVT offset... etc)

0 Kudos
Reply
6 Replies

2,504 Views
Ray_V
Contributor V

Cortex M series do not have an MMU so it is not easy (maybe impossible) to do what you want to do.

If it was only the vectors you could try using the -fPIC option when compiling. Then move the table to RAM and add the area offset to the vectors. However, there is more than that involved, such as loading RAM initialization areas and probably function calls, which may not use relative addressing as they might be too far for it.

For these processors what I do is, build for area 1, download the new application to area 2, verify by it's integrity by CRC, and other flags that I add at specific locations to indicate the hardware that the program is meant for.

If it passes those checks, bootloader copies it to area 1 to replace the current app.

0 Kudos
Reply

2,504 Views
subbanna_manjun
Contributor I

Hi thanks for the reply,

So you mean to say it is not possible to have a common binary that can be flashed to both partitions?

But in so many posts i have read it is possible by doing Vector table relocation to RAM before starting the application. Of-course, they were all for M3/M4, ours is cortex m7. Let me know if you know anything on how to relocate the vector table in m7.

0 Kudos
Reply

2,504 Views
Ray_V
Contributor V

First let me say that for a device with MMU usually you can put your program at any address and then use the MMU to map this address to the address the program is compiled for. So it would be trivial.

Cortex M processors do not have an MMU, so it can't be done this way.

I have not tried this, but theoretically you can do the following, assuming app will be compiled for Primary Area and you want to run it from the Secondary Area

1. Do not initialize any global variables in the declaration. Rather, create a function that will initialize them and call it as soon as main starts.(avoids problem with loading initialized data to RAM)

2. Compile using the -fPIC option (Position Independent Code)

3. Vector table will still point to specific locations. So bootloader will need to calculate offset = (PrimaryArea Address - SecondaryArea Address) 

4. Copy Vector table to RAM and add offset to all vectors.

5. Point VTOR to Vectors in RAM

6. Make sure the application's ResetISR will not change VTOR

This might work if function calls only use relative addresses, which may not be the case.

0 Kudos
Reply

2,504 Views
ErichStyger
Specialist I

I think what you are asking for is commonly called 'position independent code': you can instruct the compiler to generate code that way so it is not depending on the absolute location in memory. But be aware that this means extra indirections in the code and likely less performance, more memory usage and more code size. That's why this is not common in embedded systems unless you absolutely need it.

I did that a while back but never had the time to write an article about it. But you might google for 'gcc position independent code' or for '-fpic' and you should get something to wrap your head around it.

Erich

0 Kudos
Reply

2,504 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hello subbanna.manjunath@in.bosch.com‌,

1. Is it possible to build a single binary and be able to flash it to both Primary and Secondary area?

If yes, Lets say I want to flash binary to Secondary area, would it need to be specified to start at secondary area during compile time?

Yes, you can merge both files and update the firmware in a single run. I will suggest checking this post to know more about it. You will need to change the memory map, so the applications start in the secondary area.

2. How do i flash the binary to Secondary area even if the binary is built to start from Primary area? What are the things to be taken care of? (like IVT offset... etc)

If you already have the bootloader load it will be easier use it to update the firmware, but if you want to use an external debugger be sure that you load the application to the start of the secondary area and that the bootloader before leaving the PSP, MSP, and VTOR point to this area. The KV58 has a bootloader example in its SDK, you could try looking at it and check how the jump to the application is done.

Best Regards,

Alexis Andalon

0 Kudos
Reply

2,504 Views
subbanna_manjun
Contributor I

Hi  nxf46116,

Thanks for your reply.

Answer to the 2nd question is clear thank you for that.

I want to clear the 1st question. below is the problem statement.

While compiling the application, offset address needs to be provided in the linker script.

The binary file generated for the application contains this offset address at various places including Interrupt Vector Table.

Attached is a sample binary comparison of 2 application binary files, left one built for offset address 0x32000 (primary area start) and the right one at 0x96000(secondary area start).

 pastedImage_4.png

Because of this, application compiled with offset address 0x32000 will run only if it is placed in Partition 1 and application compiled with offset address 0x96000 will run only if it is placed in Partition 2.

 

We are unable to generate one application binary file which can run from both Partition 1 and Partition 2.

The requirement is that we should be able to run same application from either of the partitions.

Thanks in advance.

0 Kudos
Reply