<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/106093/use-mergehex-during-build-so-that</link><description>Dear Nordic support, 
 I &amp;#39;m currently working on adding metadata the UICR through the firmware build process . 
 This metadata is available at compile time and my goal is that, when a production build is generated , the merged _hex file will contain the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 05 Dec 2023 12:24:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/106093/use-mergehex-during-build-so-that" /><item><title>RE: Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/thread/458947?ContentTypeID=1</link><pubDate>Tue, 05 Dec 2023 12:24:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b4c60dc0-69c5-4de1-b1d9-07d51cba555c</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Removing the NOLOAD directive was actually the first thing I attempted after reproducing the issue. However, I must have done something wrong in my testing, since the UICR data still didn&amp;#39;t end up in my zephyr.hex. That said, I don&amp;#39;t see any problem with your approach&amp;nbsp;where you extract the UICR section from the generated hex using objcopy. I am also not an expert in CMake.&lt;/p&gt;
&lt;p&gt;Thanks for updating the ticket with your findings!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/thread/458946?ContentTypeID=1</link><pubDate>Tue, 05 Dec 2023 12:15:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e389e7ad-0562-4867-8c8c-690c2cc9c1c7</guid><dc:creator>p.martins</dc:creator><description>&lt;p&gt;Hello &lt;a href="https://devzone.nordicsemi.com/members/vibe"&gt;Vidar Berg&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;In my&amp;nbsp;Linker excerpt&amp;nbsp;on the first, the issue was that I was marking the UICR section as `NOLOAD`, so the linker was not considering this section as `PROGBITS` (as shown in my second comment), removing it from the final binary.&lt;/p&gt;
&lt;p&gt;Fixing this indeed&amp;nbsp;solves the issue, but creates (as you pointed out), binaries for the update and for flashing with several hundread megabytes.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What I ended up doing was creating a dummy app that&amp;nbsp;was built only to&amp;nbsp;extract the `my_uicr_section` from the elf file into a&amp;nbsp;hex that can be merged with the main hex file. That dummy app either shares all the configurations with the main project (sometimes through symbolic linking) or receives them through west/CMake during the build process.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m sure that that can be done in plain CMake, but I am not an expert so I used a minimal&amp;nbsp;Zephyr&amp;nbsp;CMake. I&amp;#39;m leaving the&amp;nbsp;final CMakeLists here in case it is useful helps others&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

zephyr_linker_sources(SECTIONS src/my_uicr_section.ld)

project(metadata)

# Project sources
target_sources(app PRIVATE src/metadata.c)

# Custom target to extract the UICR section from the dummy application hex
add_custom_target(uicr_hex ALL
    COMMAND ${CMAKE_OBJCOPY} -O ihex --only-section=my_uicr_section
            ${CMAKE_BINARY_DIR}/zephyr/zephyr.elf
            ${CMAKE_BINARY_DIR}/zephyr/uicr.hex
    DEPENDS ${CMAKE_BINARY_DIR}/zephyr/zephyr.elf
    COMMENT &amp;quot;Generating uicr.hex&amp;quot;
)

# Ensure that the custom target is built after the main target
add_dependencies(uicr_hex app)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Do you have any suggestionsor comments &lt;a href="https://devzone.nordicsemi.com/members/vibe"&gt;Vidar Berg&lt;/a&gt;&amp;nbsp;? If not, I believe we can close this thread.&lt;/p&gt;
&lt;p&gt;Thank you for your time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/thread/458045?ContentTypeID=1</link><pubDate>Wed, 29 Nov 2023 11:01:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4a6fce58-dc18-4abd-90d0-b6c2ef0b2996</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello Pedro,&lt;/p&gt;
&lt;p&gt;I managed to reproduce the problem where the UICR data is displayed in the *.map file but is not included in the generated .hex file, although I have not yet determined the reason. However, since the purpose is to write a serial number in production, wouldn&amp;#39;t it be simpler to directly write the serial number to the UICR using the programmer? This approach would remove the need to generate a unique .hex file for each device.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/thread/457643?ContentTypeID=1</link><pubDate>Mon, 27 Nov 2023 15:59:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:035debf7-f4a6-40bf-af22-edfce3496bc6</guid><dc:creator>p.martins</dc:creator><description>&lt;p&gt;Hello &lt;a href="https://devzone.nordicsemi.com/members/vibe"&gt;Vidar Berg&lt;/a&gt;&amp;nbsp;,&lt;/p&gt;
&lt;p&gt;Thank you for the quick response. I have the same feeling that despite the placement being correctly done, some striping is happening in the build process.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/106093/use-mergehex-during-build-so-that/457558"]potentially&amp;nbsp;end up with a very large ( ~256MB) app_update binary due to excessive byte padding[/quote]
&lt;p&gt;I was so focused in trying to get a solution to work with the hex files that I completely overlooked this. Thank you for pointing it out.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/106093/use-mergehex-during-build-so-that/457558"]Have you tried to check if &amp;#39;zephyr.hex&amp;#39; contains your UICR data?&amp;nbsp;[/quote]
&lt;p&gt;It does not. Only on the map&amp;nbsp;and elf file. `readelf -S build/zephyr/zephyr.elf`&amp;nbsp;outputs the following (notice the `my_uicr_sectio` in&amp;nbsp;the&amp;nbsp;image below):&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1701098755812v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Running west build in verbose mode, I can also see that the hex file generation&amp;nbsp;step does not seem to explicitly remove&amp;nbsp;the UICR. However, the .elf contains it but the .hex after the following objcopy does not:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;zephyr-sdk-0.16.3/arm-zephyr-eabi/bin/arm-zephyr-eabi-objcopy --gap-fill 0xff --output-target=ihex --remove-section=.comment --remove-section=COMMON --remove-section=.eh_frame build/zephyr/zephyr.elf zephyr.hex&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/106093/use-mergehex-during-build-so-that/457558"]I think a better option may be to write the UICR similar to how we configure the reset pin here:&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/mdk/system_nrf52.c#L339"&gt;https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/mdk/system_nrf52.c#L339&lt;/a&gt;, or is this something you have considered already?[/quote]
&lt;p&gt;I haven&amp;#39;t. If I understood correctly, that solution would require the firmware to write to the UICR. My goal is to have this metadata being generated at compile and merged into the hex file, so that it is added to a device during flashing at the production line and not being written by the FW during boot.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I will try to&amp;nbsp;have a separate build process for&amp;nbsp;the metadata. I will try&amp;nbsp;to&amp;nbsp;generate an hex that can be merged using the mergehex tool. That&amp;nbsp;way, the metadata would not be in the update process but would still be on the production line hex. Does that seems a feasible solution &lt;a href="https://devzone.nordicsemi.com/members/vibe"&gt;Vidar Berg&lt;/a&gt;&amp;nbsp;? Do you have any recommendations or suggestions for this?&lt;/p&gt;
&lt;p&gt;All the best,&lt;/p&gt;
&lt;p&gt;Pedro&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use mergehex during build so that</title><link>https://devzone.nordicsemi.com/thread/457558?ContentTypeID=1</link><pubDate>Mon, 27 Nov 2023 12:46:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e9eb038d-cf1f-4483-b421-dd7e34bb75e1</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Maybe the UICR section is stripped away from the image in the merge process. If not, you will potentially&amp;nbsp;end up with a very large ( ~256MB) app_update binary due to excessive byte padding. Have you tried to check if &amp;#39;zephyr.hex&amp;#39; contains your UICR data?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think a better option may be to write the UICR similar to how we configure the reset pin here:&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/mdk/system_nrf52.c#L339"&gt;https://github.com/zephyrproject-rtos/hal_nordic/blob/master/nrfx/mdk/system_nrf52.c#L339&lt;/a&gt;, or is this something you have considered already?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>