user46299: I'm trying to add a serial number to the bootloader's flash area of Adafruit Feather m0 board. Seems like feather m0 0-0x2000 area on the flash is non writable by the arduino sketch. i wanted to add a serial number to address 0x2000 - 8 on the flash. 
The idea is for the data to be included in the bootloader elf\bin so i can easily flash it on devices and later on access it using flash address  (0x2000 - 8) with NVMCTRL.
I've started out with this solution:
downloaded the code from samd21 arduino bootloader github directory and in ./bootloaders/zero edited the boololoader ld script, first the "Memory" section
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x2000 /* First 8KB used by bootloader */
FLASHID (rx) : ORIGIN = 0x00002000 - 0x0008, LENGTH = 8

Then the 'sections' section:
.id_sec :
  {
    KEEP(*(.myid))
  } > FLASHID

This should have saved some room in the end of the flash for the id. now in the main.c file:
__attribute__ ((section(".myid"))) const unsigned long ID = 0xdead1234;

Which should have saved the 0xdead1234 hex value in the (0x2000-8) address.
The issue is this:
I compiled this code and linked it just fine, got .hex,.bin,.elf files.
If i'm correct (unless they are somehow compressed) i should be able to see the code 0xdead in the binary. 
BUT i don't. 
After objdump on the binary(any of them) i can't find the 0xde 0xad in any order anywhere.
The arm-none-eabi-size" --format=sysv -t -x build/.elf does not show the new section i added no where.
Also i do wonder, as i said the 0x2000 address on the flash is non writable once i flashed with atmel-ice \j-link even the bootloader code cannot write to it, i did define in the ld script that i want that memory region part of the flash so i do expect that the compiler + linker + jlink flasher will know to write that data to the right place but not sure.
Anyone got some experience with it ? did it before ?

user4143: This is quite an easy fix, actually.
The problem here is the compiler doesn't think the const is used for anything - so it (quite rightly) throws it away.
All you need to do is tell the compiler that it's used and all should be good:
__attribute__((section(".myid"),used)) const unsigned long ID = 0xdead1234;

And when you compile:
build/.elf  :
section             size         addr
.vectors            0x40          0x0
.text             0x18d4         0x40
.data               0x5c   0x20000000
.bss               0x34c   0x2000005c
.id_sec              0x4       0x1ff8
.ARM.attributes     0x28          0x0
.comment            0x80          0x0
.debug_frame        0x68          0x0
Total             0x1dd0

Properly, though, you should make some more changes to the linker script to properly isolate those 8 bytes of flash (though you don't have to).

You should reduce FLASH by 8 bytes.
You should include the FLASHID length in the __sketch_vectors_ptr calculation.

That's:
MEMORY
{
  FLASH   (rx) : ORIGIN = 0x00000000, LENGTH = 0x1FF8 /* First 8KB used by bootloader */
  FLASHID (rx) : ORIGIN = 0x00001FF8, LENGTH = 8
  RAM    (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000-0x0004 /* 4 bytes used by bootloader to keep data between resets */
}

And:
PROVIDE(__sketch_vectors_ptr = ORIGIN(FLASH) + LENGTH(FLASH) + LENGTH(FLASHID));

That way the compiled bootloader code can never be placed in the FLASHID area (not that it would at the moment, but if a new version of the bootloader were released that used 100% of the lower block of flash it would conflict).
Of course, if all you need is something unique to identify the board with, all AVR and SAMD devices that have built-in USB support have a unique ID number. This is intended to be used as the Serial Number for the USB, but could also be used for a MAC address for Ethernet (or for portions of it anyway) or other unique "node" addresses.

46299: <p>I'm trying to add a serial number to the bootloader's flash area of Adafruit Feather m0 board. Seems like feather m0 0-0x2000 area on the flash is non writable by the arduino sketch. i wanted to add a serial number to address 0x2000 - 8 on the flash. 
The idea is for the data to be included in the bootloader elf\bin so i can easily flash it on devices and later on access it using flash address  (0x2000 - 8) with NVMCTRL.</p>

<p>I've started out with this solution:</p>

<p>downloaded the code from <a href="https://github.com/arduino/ArduinoCore-samd" rel="nofollow noreferrer">samd21 arduino bootloader github directory</a> and in ./bootloaders/zero edited the boololoader ld script, first the "Memory" section</p>

<pre><code>FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x2000 /* First 8KB used by bootloader */
FLASHID (rx) : ORIGIN = 0x00002000 - 0x0008, LENGTH = 8
</code></pre>

<p>Then the 'sections' section:</p>

<pre><code>.id_sec :
  {
    KEEP(*(.myid))
  } &gt; FLASHID
</code></pre>

<p>This should have saved some room in the end of the flash for the id. now in the main.c file:</p>

<pre><code>__attribute__ ((section(".myid"))) const unsigned long ID = 0xdead1234;
</code></pre>

<p>Which should have saved the 0xdead1234 hex value in the (0x2000-8) address.</p>

<p>The issue is this:
I compiled this code and linked it just fine, got .hex,.bin,.elf files.
If i'm correct (unless they are somehow compressed) i should be able to see the code 0xdead in the binary. 
<strong>BUT</strong> i don't. 
After objdump on the binary(any of them) i can't find the 0xde 0xad in any order anywhere.
The <code>arm-none-eabi-size" --format=sysv -t -x build/.elf</code> does not show the new section i added no where.
Also i do wonder, as i said the 0x2000 address on the flash is non writable once i flashed with atmel-ice \j-link even the bootloader code cannot write to it, i did define in the ld script that i want that memory region part of the flash so i do expect that the compiler + linker + jlink flasher will know to write that data to the right place but not sure.</p>

<p>Anyone got some experience with it ? did it before ?</p>
 4143: <p>This is quite an easy fix, actually.</p>

<p>The problem here is the compiler doesn't think the const is used for anything - so it (quite rightly) throws it away.</p>

<p>All you need to do is tell the compiler that it's <code>used</code> and all should be good:</p>

<pre><code>__attribute__((section(".myid"),used)) const unsigned long ID = 0xdead1234;
</code></pre>

<p>And when you compile:</p>

<pre><code>build/.elf  :
section             size         addr
.vectors            0x40          0x0
.text             0x18d4         0x40
.data               0x5c   0x20000000
.bss               0x34c   0x2000005c
.id_sec              0x4       0x1ff8
.ARM.attributes     0x28          0x0
.comment            0x80          0x0
.debug_frame        0x68          0x0
Total             0x1dd0
</code></pre>

<p>Properly, though, you should make some more changes to the linker script to properly isolate those 8 bytes of flash (though you don't <em>have</em> to).</p>

<ol>
<li>You should reduce <code>FLASH</code> by 8 bytes.</li>
<li>You should include the FLASHID length in the <code>__sketch_vectors_ptr</code> calculation.</li>
</ol>

<p>That's:</p>

<pre><code>MEMORY
{
  FLASH   (rx) : ORIGIN = 0x00000000, LENGTH = 0x1FF8 /* First 8KB used by bootloader */
  FLASHID (rx) : ORIGIN = 0x00001FF8, LENGTH = 8
  RAM    (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000-0x0004 /* 4 bytes used by bootloader to keep data between resets */
}
</code></pre>

<p>And:</p>

<pre><code>PROVIDE(__sketch_vectors_ptr = ORIGIN(FLASH) + LENGTH(FLASH) + LENGTH(FLASHID));
</code></pre>

<p>That way the compiled bootloader code can never be placed in the FLASHID area (not that it would at the moment, but if a new version of the bootloader were released that used 100% of the lower block of flash it would conflict).</p>

<p>Of course, if all you need is something unique to identify the board with, all AVR and SAMD devices that have built-in USB support have a unique ID number. This is intended to be used as the Serial Number for the USB, but could also be used for a MAC address for Ethernet (or for portions of it anyway) or other unique "node" addresses.</p>
