<?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>Trouble updating bootloader</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/97530/trouble-updating-bootloader</link><description>Our company has an existing product (snodar) which uses an nRF52840. When we created the snodar, it used a bootloader based on the `secure_bootloader` project. On top of that, we are using the s140 SD and have an APP which allows DFU over BLE. We have</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 16 Mar 2023 22:52:37 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/97530/trouble-updating-bootloader" /><item><title>RE: Trouble updating bootloader</title><link>https://devzone.nordicsemi.com/thread/415850?ContentTypeID=1</link><pubDate>Thu, 16 Mar 2023 22:52:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:839f81ec-fe79-41c2-918a-caca1e261b98</guid><dc:creator>jhadella</dc:creator><description>&lt;p&gt;Looks like that might have done it! Had to simplify the makefile and set that reduced dfu flag to get it to build successfully.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I was able to eventually get the bootloader to finally build. It looks I can update the original bootloader to the new one. It doesn&amp;#39;t let me update that bootloader anymore, but this is because same version updates are disallowed.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I know it works since the in-place DFU only works properly if the bootloader has been updated, which is the case.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for all your help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Trouble updating bootloader</title><link>https://devzone.nordicsemi.com/thread/415121?ContentTypeID=1</link><pubDate>Tue, 14 Mar 2023 10:30:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2123f2f-f944-4d74-90cf-5664900bfd00</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Did you base your bootloader on the &amp;#39;&lt;span&gt;pca10056_s140_ble&amp;#39; configuration?&amp;nbsp;&lt;/span&gt;It does not make sense to include the slip.c and&amp;nbsp;nrf_dfu_serial.c sources if the bootloader only uses&amp;nbsp;the BLE DFU transport. If you remove these source files from your Makefile, you will also be able to enable the NRF_DFU_PROTOCOL_REDUCED setting in sdk_config.h again, which will reduce the flash usage. Hopefully enough to make it fit into the original bootloader area again (0xF8000 - 0xFE000)&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Trouble updating bootloader</title><link>https://devzone.nordicsemi.com/thread/415026?ContentTypeID=1</link><pubDate>Mon, 13 Mar 2023 23:16:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ddf9782-edfb-4996-afe7-99d817cfc629</guid><dc:creator>jhadella</dc:creator><description>&lt;p&gt;If I restore the previous .ld and build the project, I see:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Linking target: _build/nrf52840_xxaa_s140.out&lt;br /&gt;c:/program files (x86)/gnu arm embedded toolchain/10 2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld.exe: region FLASH overflowed with .data and user data&lt;br /&gt;collect2.exe: error: ld returned 1 exit status&lt;br /&gt;make: *** [_build/nrf52840_xxaa_s140.out] Error 1&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I added my secure_bootloader project but changed the public key to just have random data for sharing. Interestingly, the snodar_2p0 build fails, but the other one doesn&amp;#39;t. Not sure if I really need the serial_dfu and slip since the in-place dfu in the other project is doing that.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I did make a mod to the nRF5_sdk/components/bootloader/dfu/nrf_dfu_validation.c. This is what caused the issue in the first place?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static bool flash_ram_validation_hash_ok(uint8_t const * p_hash, uint32_t src_addr, uint32_t data_len)
{
    ret_code_t err_code;
	
	const uint32_t fw_chunk_size = 4096;
    static uint8_t fw_chunk[4096];
	
	nrf_crypto_hash_sha256_digest_t digest;
	size_t digest_len = NRF_CRYPTO_HASH_SIZE_SHA256;
	
	nrf_crypto_hash_context_t hash_context = {0};
	
	// Initialize the hash context
	err_code = nrf_crypto_hash_init(&amp;amp;hash_context, &amp;amp;g_nrf_crypto_hash_sha256_info);
	APP_ERROR_CHECK(err_code);

	uint32_t chunk_addr = src_addr;
	uint32_t remaining = data_len;
	while (remaining) {
		uint32_t copy_len = remaining &amp;gt; fw_chunk_size ? fw_chunk_size : remaining;
		//NRF_LOG_DEBUG(&amp;quot;$$$ start_addr = %lx, size = %ld $$$&amp;quot;, chunk_addr, copy_len);
		//NRF_LOG_FLUSH();
		
		memcpy(fw_chunk, (uint8_t *)chunk_addr, copy_len);
		
		err_code = nrf_crypto_hash_update(&amp;amp;hash_context, fw_chunk, copy_len);
		APP_ERROR_CHECK(err_code);
		
		chunk_addr += copy_len;
		remaining -= copy_len;
	}
	
	// Run the finalize when all data has been fed to the update function which gives the result
	err_code = nrf_crypto_hash_finalize(&amp;amp;hash_context, digest, &amp;amp;digest_len);
	APP_ERROR_CHECK(err_code);

	// Compare memory of our calculated hash vs the expected hash
	bool hash_valid = memcmp(digest, p_hash, NRF_CRYPTO_HASH_SIZE_SHA256) == 0;
	NRF_LOG_DEBUG(&amp;quot;flash_ram_validation_hash_ok() hash OK == %d&amp;quot;, hash_valid);
	NRF_LOG_FLUSH();
	
	if (!hash_valid) {
		NRF_LOG_DEBUG(&amp;quot;Expected FW hash:&amp;quot;)
		NRF_LOG_FLUSH();
        NRF_LOG_HEXDUMP_DEBUG(p_hash, NRF_CRYPTO_HASH_SIZE_SHA256);
		NRF_LOG_FLUSH();
        NRF_LOG_DEBUG(&amp;quot;Actual FW hash:&amp;quot;)
		NRF_LOG_FLUSH();
        NRF_LOG_HEXDUMP_DEBUG(digest, NRF_CRYPTO_HASH_SIZE_SHA256);
		NRF_LOG_FLUSH();
	}
	
	return hash_valid;
}

// Function to check the hash received in the init command against the received firmware.
// little_endian specifies the endianness of @p p_hash.
static bool nrf_dfu_validation_hash_ok(uint8_t const * p_hash, uint32_t src_addr, uint32_t data_len, bool little_endian)
{
    ret_code_t err_code;
    bool       result   = true;
    uint8_t    hash_be[NRF_CRYPTO_HASH_SIZE_SHA256];
    size_t     hash_len = NRF_CRYPTO_HASH_SIZE_SHA256;

    nrf_crypto_hash_context_t hash_context = {0};

    crypto_init();

    if (little_endian)
    {
        // Convert to hash to big-endian format for use in nrf_crypto.
        nrf_crypto_internal_swap_endian(hash_be,
                                        p_hash,
                                        NRF_CRYPTO_HASH_SIZE_SHA256);
        p_hash = hash_be;
    }

    NRF_LOG_DEBUG(&amp;quot;Hash verification. start address: 0x%x, size: 0x%x&amp;quot;,
                  src_addr,
                  data_len);

    err_code = nrf_crypto_hash_calculate(&amp;amp;hash_context,
                                         &amp;amp;g_nrf_crypto_hash_sha256_info,
                                         (uint8_t*)src_addr,
                                         data_len,
                                         m_fw_hash,
                                         &amp;amp;hash_len);

    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR(&amp;quot;Could not run hash verification (err_code 0x%x).&amp;quot;, err_code);
		NRF_LOG_FLUSH();
	
		// This will trigger if the crypto can&amp;#39;t do a hash calculation as it has no access
		// to the memory being checked. This happens for the in-place DFU since the data is
		// in flash memory (not RAM). We will retry using custom method...
		result = flash_ram_validation_hash_ok(p_hash, src_addr, data_len);
    }
    else if (memcmp(m_fw_hash, p_hash, NRF_CRYPTO_HASH_SIZE_SHA256) != 0)
    {
        NRF_LOG_WARNING(&amp;quot;Hash verification failed.&amp;quot;);
        NRF_LOG_DEBUG(&amp;quot;Expected FW hash:&amp;quot;)
        NRF_LOG_HEXDUMP_DEBUG(p_hash, NRF_CRYPTO_HASH_SIZE_SHA256);
        NRF_LOG_DEBUG(&amp;quot;Actual FW hash:&amp;quot;)
        NRF_LOG_HEXDUMP_DEBUG(m_fw_hash, NRF_CRYPTO_HASH_SIZE_SHA256);
        NRF_LOG_FLUSH();

        result = false;
    }

    return result;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/1425.secure_5F00_bootloader.zip"&gt;devzone.nordicsemi.com/.../1425.secure_5F00_bootloader.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Trouble updating bootloader</title><link>https://devzone.nordicsemi.com/thread/414949?ContentTypeID=1</link><pubDate>Mon, 13 Mar 2023 14:26:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f43a023-c23d-415f-8772-63e6df21d998</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]Do I just add the --bootloader and --bootloader_version to the call which is making the zip now?[/quote]
&lt;p&gt;Yes.&lt;/p&gt;
[quote user=""]For this in-place dfu, we stay in the snodar fw and it is able to update itself -- it doesn&amp;#39;t switch to the bootloader.[/quote]
&lt;p&gt;This is&amp;nbsp;commonly referred to as &amp;quot;background DFU&amp;quot; because the app continues to run while&amp;nbsp;receiving&amp;nbsp;the FW update unlike normal DFU where the entire process takes place in the bootloader. So, the application is&amp;nbsp;responsible for receiving the FW update image and storing it to flash (bank_1 area). But you still need to reboot the device afterwards to let the bootloader activate the update by copying the image from Bank 1 to bank 0.&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/pastedimage1678713868964v2.png" alt=" " /&gt;&lt;/p&gt;
[quote user=""]The FLASH was from 0xf8000 with len 0x6000 before the mod.[/quote]
&lt;p&gt;It is not possible to change the Bootloader&amp;#39;s start address through DFU as the start address is stored in a protected area of flash. What is the actual size of your bootloader after your modifications? I am basically wondering if it&amp;nbsp;would&amp;nbsp;be possible to keep the original start address with some small&amp;nbsp;code optimizations.&lt;/p&gt;
&lt;p&gt;I get&amp;nbsp;just below the 24K limit&amp;nbsp; here when I build the bootloader (nRF5_SDK_17.1.0_ddde560/examples/dfu/secure_bootloader/pca10056_s140_ble/armgcc) with&amp;nbsp;NRF_BL_DFU_ALLOW_UPDATE_FROM_APP==1:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Linking target: _build/nrf52840_xxaa_s140.out
   text	   data	    bss	    dec	    hex	filename
  23968	    184	  21976	  46128	   b430	_build/nrf52840_xxaa_s140.out
&lt;/pre&gt;&lt;/p&gt;
[quote user=""]If I attempt to ble dfu of this, what I witness is that the snodar switches to some old version of the bootloader. The advertising&amp;nbsp;name I see is &amp;quot;SNOdar BootloaderD&amp;quot; -- this is different from the new one. At this stage, I am able to then update the app firmware fine, but If I try to update this bootloader zip, it just gives me a bunch of &amp;quot;x&amp;quot; in the dfu app and really no feedback.[/quote]
&lt;p&gt;This is strange. The DFU app should give a clear error message if an update is rejected. Could you try DFU with the nRF connect app on Android/iOS and see if it gives the same result?&amp;nbsp; &amp;nbsp;&lt;/p&gt;
[quote user=""]Is there any way to just wipe the flash and make a zip that just contains a BL + SD? over a BLE DFU? Do you have any other suggestions to update the bootloader?[/quote]
&lt;p&gt;It should be possible to update just the bootloader as long as you bump the version number and make sure the old and new bootloader have the same start address in FLASH.&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></channel></rss>