<?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>custom DFU in App</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/71234/custom-dfu-in-app</link><description>Hello, 
 I am developing DFU over a custom IEEE802.15.4 Protocol in our own embedded OS. I managed to write the new Firmware starting at bank1_start address. Now I need to tell the Bootloader (the BTL from SDK16) to do its magic (verifying and copying</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 12 Feb 2021 15:47:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/71234/custom-dfu-in-app" /><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/294221?ContentTypeID=1</link><pubDate>Fri, 12 Feb 2021 15:47:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b760aa5c-0a02-46af-97f6-697a5e393db6</guid><dc:creator>MeisterBob</dc:creator><description>&lt;p&gt;It&amp;#39;s in sdk_config.h. Actualy NRF_CRYPTO_BACKEND_CC310_BL wasn&amp;#39;t enabled. So I disabled the CC310 backend and enabled the CC310_BL backend, added some src files and include pathes to the Makefile and was finaly able to update the firmware from the app.&lt;/p&gt;
&lt;p&gt;I pushed the code to &lt;a href="https://github.com/MeisterBob/nrf52_blinky_dfu_in_app"&gt;https://github.com/MeisterBob/nrf52_blinky_dfu_in_app&lt;/a&gt; for anyone who needs a minimal example for DFU_IN_APP, it&amp;#39;s based on SDK 17.0.2 and uses UART transport.&lt;/p&gt;
&lt;p&gt;I was able to find the answer to my initial question. My major missunderstanding was, that the init command needs to be stored protobuf encoded, I was trying to safe it decoded. The steps to success are:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;nrf_dfu_settings_t *p_dfu_settings = /* Pointer to bootloader settings page (0xFF000 on nRF52840) */
nrf_dfu_settings_t mku_dfu_settings;

// copy old dfu_settings
memcpy(&amp;amp;dfu_settings, p_dfu_settings, sizeof(nrf_dfu_settings_t));

// update init command (protobuf encoded !)
memcpy(dfu_settings.init_command, /* init command */, /* init command size */);

// update .progress
dfu_settings.progress.command_size               = /* size of the init command */;
dfu_settings.progress.command_offset             = dfu_settings.progress.command_size;
dfu_settings.progress.data_object_size           = 0;
dfu_settings.progress.firmware_image_crc         = /* application crc returned by nrfutil settings generate */;
dfu_settings.progress.firmware_image_crc_last    = dfu_settings.progress.firmware_image_crc;
dfu_settings.progress.firmware_image_offset      = /* application size */
dfu_settings.progress.firmware_image_offset_last = dfu_settings.progress.firmware_image_offset;
dfu_settings.progress.command_crc                = crc32_compute((uint8_t *)(dfu_settings.init_command), dfu_settings.progress.command_size, NULL);

// set bank current to 1
dfu_settings.bank_current = 1;  // NRF_DFU_CURRENT_BANK_1

// update settings crc
dfu_settings.crc = crc32_compute((uint8_t *)(&amp;amp;dfu_settings) + 4, offsetof(nrf_dfu_settings_t, init_command) - 4, NULL);

// write dfu_settings back to p_dfu_settings
// write new app to MBR_SIZE + dfu_settings-&amp;gt;bank_0.image_size (if you use a softdevice MBR_SIZE is a bad choice here)
// reboot the system&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/293902?ContentTypeID=1</link><pubDate>Thu, 11 Feb 2021 08:32:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e0c7e35b-0aa0-4711-9fb4-f36699207588</guid><dc:creator>Edvin</dc:creator><description>[quote user="MeisterBob"]NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED is set to 1 as you can see in the project files I attached to my earlier post,[/quote]
&lt;p&gt;&amp;nbsp;I can&amp;#39;t see that it is.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Try starting with an unmodified version of the bootloader and go from there. Please try to keep&amp;nbsp;&lt;span&gt;NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED&amp;nbsp; set to 1.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;BR,&lt;br /&gt;&lt;br /&gt;Edvin&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/293811?ContentTypeID=1</link><pubDate>Wed, 10 Feb 2021 14:50:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8899500c-a37a-40ff-982c-655675f17a12</guid><dc:creator>MeisterBob</dc:creator><description>&lt;p&gt;as I said, it&amp;#39;s cc310_backend_hash_sha256_update() without _bl_. Here is the call stack:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/Bildschirmfoto-von-2021_2D00_02_2D00_10-09_2D00_05_2D00_52.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED is set to 1 as you can see in the project files I attached to my earlier post, but that wasn&amp;#39;t on purpose. If my NRF_CRYPTO_* setup is wrong, could you please help me setting it up right.&lt;/p&gt;
&lt;p&gt;The new application is stored in bank1, but fw_hash_ok() fails in postvalidate(), so s_dfu_settings.bank_current isn&amp;#39;t set to NRF_DFU_CURRENT_BANK_1.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/293742?ContentTypeID=1</link><pubDate>Wed, 10 Feb 2021 11:50:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1510b808-3109-4e2c-b7e4-2bb365ff81c0</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So that is the&amp;nbsp;cc310_bl_backend_hash_sha256_update() (with &amp;quot;_bl_&amp;quot;, right?)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Remember that at this point, even if the DFU was performed from the bootloader (and not from the DFU_IN_APP), the new application would have been stored in the Flash of the nRF52840 as well.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Line 168 and 172in cc310_bl_backend_hash.c in sdk17.0.2 says:&lt;/p&gt;
&lt;p&gt;168: // Copy a block from FLASH to RAM for use in CC310&lt;br /&gt;172: // Copy from FLASH to ram&lt;/p&gt;
&lt;p&gt;Is there a particular reason why you have set the define in sdk_config.h:&lt;/p&gt;
&lt;p&gt;NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED&lt;/p&gt;
&lt;p&gt;to 0? It is 1 by default. When this is set to 1, it will copy the data from flash to RAM, and in fact, the lines 164 -&amp;gt; 182 will be replacing line 183 -&amp;gt; 199 in cc310_bl_backend_hash.c.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Try setting it (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED) back to 1. If that is not an option, I guess you need to manually pull each buffer with size &amp;quot;NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE&amp;quot; from flash before you run the VERIFY_TRUE() macro.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/293682?ContentTypeID=1</link><pubDate>Wed, 10 Feb 2021 08:13:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a847592e-1b73-40d5-8ac6-81ebe8f19a73</guid><dc:creator>MeisterBob</dc:creator><description>&lt;p&gt;Hello Edvin,&lt;/p&gt;
&lt;p&gt;I set up a DFU_IN_APP project with SDK17.0.2. The verification of the new Firmware fails with error 0x8512 (NRF_ERROR_CRYPTO_INPUT_LOCATION). I tracked it down to cc310_backend_hash_sha256_update(). There it verifies if p_data is in RAM&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;VERIFY_TRUE(nrfx_is_in_ram(p_data), NRF_ERROR_CRYPTO_INPUT_LOCATION);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But obviously the updated firmware is located in flash. What am I doing wrong here?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/dfu_5F00_in_5F00_app.zip"&gt;devzone.nordicsemi.com/.../dfu_5F00_in_5F00_app.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: custom DFU in App</title><link>https://devzone.nordicsemi.com/thread/292729?ContentTypeID=1</link><pubDate>Wed, 03 Feb 2021 14:59:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b52d8408-c629-437c-84b0-42e7d27fe301</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;So you have basically already transferred the new image, and you need the bootloader to move it into the correct location and verify the signature, right?&lt;/p&gt;
&lt;p&gt;I suggest you look into how the bootloader does this when it receive the &amp;quot;object execute&amp;quot; command. I am using SDK17.0.2 and the serial(uart) bootloader as reference, as you may not be using the softdevice.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;nrf_dfu_req_handler.c -&amp;gt; nrf_dfu_command_req() -&amp;gt; case NRF_DFU_OP_OBJECT_EXECUTE -&amp;gt; on_cmd_obj_execute_request() -&amp;gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void on_cmd_obj_execute_request(nrf_dfu_request_t const * p_req, nrf_dfu_response_t * p_res)
{
    ASSERT(p_req);
    ASSERT(p_res);

    NRF_LOG_DEBUG(&amp;quot;Handle NRF_DFU_OP_OBJECT_EXECUTE (command)&amp;quot;);

    nrf_dfu_result_t ret_val;
    ret_val = nrf_dfu_validation_init_cmd_execute(&amp;amp;m_firmware_start_addr, &amp;amp;m_firmware_size_req);
    p_res-&amp;gt;result = ext_err_code_handle(ret_val);

    if (p_res-&amp;gt;result == NRF_DFU_RES_CODE_SUCCESS)
    {
        if (nrf_dfu_settings_write_and_backup(NULL) == NRF_SUCCESS)
        {
            /* Setting DFU to initialized */
            NRF_LOG_DEBUG(&amp;quot;Writing valid init command to flash.&amp;quot;);
        }
        else
        {
            p_res-&amp;gt;result = NRF_DFU_RES_CODE_OPERATION_FAILED;
        }
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;particularly the line containing nrf_dfu_validation_init_cmd_execute().&lt;/p&gt;
&lt;p&gt;This will validate the firmware. If it is successful, it will continue to write new settings:&lt;/p&gt;
&lt;p&gt;nrf_dfu_settings_write_and_backup()&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t remember the complete application flow, but this should eventually reset the bootloader, and the next time it runs, it will run into main() -&amp;gt; nrf_bootloader_init() -&amp;gt;&amp;nbsp;nrf_bootloader_fw_activate() case ACTIVATION_SUCCESS, which will reset it again, and then your application will start.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>