Re: Version/Signature in Flash

2022-01-19 Thread Michael Kwasnicki
I am not sure if I got it right.
The linker is only involved until the .elf file has been created.
If the question is to pad out the content of the .hex file so it is one 
continuous block then it can be done with :

avr-objcopy -j .text -j .data -j .my_signature --gap-fill 0xFF -O ihex 
prog.elf prog.hex 

The gap-fill argument fills the blanks with a specific value (0xFF in this 
case).

Cheers

Michael


> Am 2022-01-19 um 16:30 schrieb Jim Brain :
> 
> On 1/19/2022 7:13 AM, Michael Stocker wrote:
>> You are right.
>> 
>>  -Wl,--undefined=signature
>> 
>> This will keep it in flash and not let the linker strip its value.
>> Also when preparing your .hex file for flashing you may need to explicitly 
>> tell the sections you want.
>> 
>>  avr-objcopy -j .text -j .data -j .my_signature -O ihex prog.elf 
>> prog.hex 
>> 
>> Cheers
>> 
>> Michael
>> 
> This may be a naive question, but is it possible to "place" the signature in 
> a specific location in the resulting output file, padding the file to the 
> required size to include the signature?
> 
> Use case:
> 
> One of the projects I utilize defines a 4 byte signature at the end of the 
> AVR code space (top 4 bytes), and it checks that signature in the bootloader 
> to see if an update on storage is newer than the one installed.  Right now, 
> the main developer has created a Perl script that pulls the resulting object 
> file, padds it out, and puts the 4 bytes at the end.  It would be nice to do 
> all of that in the linker.
> 
> 



Re: Version/Signature in Flash

2022-01-19 Thread Jim Brain

On 1/19/2022 10:05 AM, Michael Kwasnicki wrote:

I am not sure if I got it right.
The linker is only involved until the .elf file has been created.
If the question is to pad out the content of the .hex file so it is 
one continuous block then it can be done with :


avr-objcopy -j .text -j .data -j .my_signature *--gap-fill 0xFF* -O 
ihex prog.elf prog.hex


The gap-fill argument fills the blanks with a specific value (0xFF in 
this case).


You are correct.  I thought the work was done in the linker.  In the 
above case, though, does the output have  code space> ? That appears to be the requirement in the 
resulting file.


Jim



Re: Version/Signature in Flash

2022-01-19 Thread Jim Brain

On 1/19/2022 7:13 AM, Michael Stocker wrote:

You are right.

-Wl,--undefined=signature

This will keep it in flash and not let the linker strip its value.
Also when preparing your .hex file for flashing you may need to 
explicitly tell the sections you want.


avr-objcopy -j .text -j .data -j .my_signature -O ihex prog.elf prog.hex

Cheers

Michael

This may be a naive question, but is it possible to "place" the 
signature in a specific location in the resulting output file, padding 
the file to the required size to include the signature?


Use case:

One of the projects I utilize defines a 4 byte signature at the end of 
the AVR code space (top 4 bytes), and it checks that signature in the 
bootloader to see if an update on storage is newer than the one 
installed.  Right now, the main developer has created a Perl script that 
pulls the resulting object file, padds it out, and puts the 4 bytes at 
the end.  It would be nice to do all of that in the linker.




Re: Version/Signature in Flash

2022-01-19 Thread Klaus Rudolph

Hi,




-Wl,--undefined=signature

This will keep it in flash and not let the linker strip its value.
Also when preparing your .hex file for flashing you may need to
explicitly tell the sections you want.

avr-objcopy -j .text -j .data -j .my_signature -O ihex prog.elf
prog.hex



works perfect! Thanks!

Maybe an additional question as I am using different controllers.

Is there any option to tell the linker something like
-Wl,--section-start=.my_signature=FLASH_END-0x50

This can be done easily in the linker script, but I did not want to take
a copy of the whole avr-libc/binutils installation to add a new section
in each of the .x files.
Some idea how I can "ask" for the last possible flash address of the
controller? Maybe by compiling some empty prog, picking a symbol from
elf, place it in the Makefile-variable and go for final link? Strange...
:) Sounds possible... I will also take a look...

Thanks
 Klaus



Re: Version/Signature in Flash

2022-01-19 Thread Trampas Stern
I recommend creating the section in the linker file.  It is often easier
for the next guy looking at your code to find the section in the linker
file.

That is, embedded development follows a path, from writing code to make it
work, to making it work reliably, to making it where the next guy can
understand 6 months from now.

In that respect most people use command line options in embedded
development for changing options, for example code might support two boards
and you have a command line option as to which board, or if you build for
debug or release.  Whereas the linker file would be more for global project
options that do not need to change per build/board.

Think of embedded development. It is like driving a car, sure you can make
a lane change locking up brakes and skidding to the next lane.  However the
guy behind you expects you to turn on the turn signals and gracefully make
lane changes.  It is not always about getting the job done, but doing it
where the guy behind you can predict what you have done and why so that
everyone is safe and comfortable.

Like driving a car there are numerous ways to get where you are going,
often it is how to get there so that others understand and can predict what
you are doing and why so they can stay out of your way.

Trampas


On Wed, Jan 19, 2022 at 8:13 AM Michael Stocker 
wrote:

> You are right.
>
> -Wl,--undefined=signature
>
> This will keep it in flash and not let the linker strip its value.
> Also when preparing your .hex file for flashing you may need to explicitly
> tell the sections you want.
>
> avr-objcopy -j .text -j .data -j .my_signature -O ihex prog.elf prog.hex
>
> Cheers
>
> Michael
>
>
>
> Am 2022-01-19 um 14:06 schrieb Klaus Rudolph :
>
> Thanks for the linker command! I was not aware that we are able to
> define a section and put the content to flash by simply adding something
> on the linker command. Is there no need to tell the linker that this
> section should be kept as it is typically not referenced and "optimized"
> out?
>
> Thanks!
> Klaus
>
> Am 19.01.22 um 13:48 schrieb Michael Stocker:
>
> Hallo Klaus,
>
> you can always define your own sections at a fixed location in flash and
> write anything you like in there.
> But every time you start a new section the previous one is closed
> implicitly. So it cannot be in the middle of another section.
> It also cannot go at the beginning of flash because the interrupt
> vectors are expected to be there.
>
> You can put anything into a section as long as it fits. Here for example
> a 32-bit signature:
>
> const uint32_t __attribute__((section(".my_signature"))) signature =
> 0xCAFEBABE;
>
> The linker then needs an additional argument to know where this section is:
>
> -Wl,--section-start=. my_signature =0x7FFC
>
> Where 0x7FFC is the byte-offset in flash not the word-offset. 0x7FFC
> would mean the end of flash on an ATmega328P.
>
> Hope this helps.
>
> Michael
>
>
>
> Am 2022-01-18 um 11:21 schrieb Klaus Rudolph  >:
>
> Hi,
>
> is there already a defined wy to place a user defined signature in the
> flash memory?
>
> Use Case: Simply put some version info, maybe a a string with given
> format or a git hash id or whatever in a specified flash area.
>
> I see we have a section ".signature" but this is in use for a predefined
> 3 byte signature. I also see in the linker scripts that there is
> something called "user_signatures" but that seems to be unsupported for
> the rest of the linker script. But maybe I looked in the wrong file...
>
> Any suggestions?
>
> Thanks
> Klaus
>
>
>


Re: Version/Signature in Flash

2022-01-19 Thread Klaus Rudolph

Thanks for the linker command! I was not aware that we are able to
define a section and put the content to flash by simply adding something
on the linker command. Is there no need to tell the linker that this
section should be kept as it is typically not referenced and "optimized"
out?

Thanks!
 Klaus

Am 19.01.22 um 13:48 schrieb Michael Stocker:

Hallo Klaus,

you can always define your own sections at a fixed location in flash and
write anything you like in there.
But every time you start a new section the previous one is closed
implicitly. So it cannot be in the middle of another section.
It also cannot go at the beginning of flash because the interrupt
vectors are expected to be there.

You can put anything into a section as long as it fits. Here for example
a 32-bit signature:

const uint32_t __attribute__((section(".my_signature"))) signature =
0xCAFEBABE;

The linker then needs an additional argument to know where this section is:

-Wl,--section-start=. my_signature =0x7FFC

Where 0x7FFC is the byte-offset in flash not the word-offset. 0x7FFC
would mean the end of flash on an ATmega328P.

Hope this helps.

Michael




Am 2022-01-18 um 11:21 schrieb Klaus Rudolph mailto:lts-rudo...@gmx.de>>:

Hi,

is there already a defined wy to place a user defined signature in the
flash memory?

Use Case: Simply put some version info, maybe a a string with given
format or a git hash id or whatever in a specified flash area.

I see we have a section ".signature" but this is in use for a predefined
3 byte signature. I also see in the linker scripts that there is
something called "user_signatures" but that seems to be unsupported for
the rest of the linker script. But maybe I looked in the wrong file...

Any suggestions?

Thanks
Klaus







Re: Version/Signature in Flash

2022-01-19 Thread Michael Stocker
You are right.

-Wl,--undefined=signature

This will keep it in flash and not let the linker strip its value.
Also when preparing your .hex file for flashing you may need to explicitly tell 
the sections you want.

avr-objcopy -j .text -j .data -j .my_signature -O ihex prog.elf 
prog.hex 

Cheers

Michael



> Am 2022-01-19 um 14:06 schrieb Klaus Rudolph :
> 
> Thanks for the linker command! I was not aware that we are able to
> define a section and put the content to flash by simply adding something
> on the linker command. Is there no need to tell the linker that this
> section should be kept as it is typically not referenced and "optimized"
> out?
> 
> Thanks!
> Klaus
> 
> Am 19.01.22 um 13:48 schrieb Michael Stocker:
>> Hallo Klaus,
>> 
>> you can always define your own sections at a fixed location in flash and
>> write anything you like in there.
>> But every time you start a new section the previous one is closed
>> implicitly. So it cannot be in the middle of another section.
>> It also cannot go at the beginning of flash because the interrupt
>> vectors are expected to be there.
>> 
>> You can put anything into a section as long as it fits. Here for example
>> a 32-bit signature:
>> 
>> const uint32_t __attribute__((section(".my_signature"))) signature =
>> 0xCAFEBABE;
>> 
>> The linker then needs an additional argument to know where this section is:
>> 
>> -Wl,--section-start=. my_signature =0x7FFC
>> 
>> Where 0x7FFC is the byte-offset in flash not the word-offset. 0x7FFC
>> would mean the end of flash on an ATmega328P.
>> 
>> Hope this helps.
>> 
>> Michael
>> 
>> 
>> 
>>> Am 2022-01-18 um 11:21 schrieb Klaus Rudolph >> >>:
>>> 
>>> Hi,
>>> 
>>> is there already a defined wy to place a user defined signature in the
>>> flash memory?
>>> 
>>> Use Case: Simply put some version info, maybe a a string with given
>>> format or a git hash id or whatever in a specified flash area.
>>> 
>>> I see we have a section ".signature" but this is in use for a predefined
>>> 3 byte signature. I also see in the linker scripts that there is
>>> something called "user_signatures" but that seems to be unsupported for
>>> the rest of the linker script. But maybe I looked in the wrong file...
>>> 
>>> Any suggestions?
>>> 
>>> Thanks
>>> Klaus



Re: Version/Signature in Flash

2022-01-19 Thread Michael Stocker
Hallo Klaus,

you can always define your own sections at a fixed location in flash and write 
anything you like in there.
But every time you start a new section the previous one is closed implicitly. 
So it cannot be in the middle of another section.
It also cannot go at the beginning of flash because the interrupt vectors are 
expected to be there.

You can put anything into a section as long as it fits. Here for example a 
32-bit signature:

const uint32_t __attribute__((section(".my_signature"))) signature = 
0xCAFEBABE;

The linker then needs an additional argument to know where this section is:

-Wl,--section-start=. my_signature =0x7FFC

Where 0x7FFC is the byte-offset in flash not the word-offset. 0x7FFC would mean 
the end of flash on an ATmega328P.

Hope this helps.

Michael



> Am 2022-01-18 um 11:21 schrieb Klaus Rudolph :
> 
> Hi,
> 
> is there already a defined wy to place a user defined signature in the
> flash memory?
> 
> Use Case: Simply put some version info, maybe a a string with given
> format or a git hash id or whatever in a specified flash area.
> 
> I see we have a section ".signature" but this is in use for a predefined
> 3 byte signature. I also see in the linker scripts that there is
> something called "user_signatures" but that seems to be unsupported for
> the rest of the linker script. But maybe I looked in the wrong file...
> 
> Any suggestions?
> 
> Thanks
> Klaus
>