<?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>nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question</link><description>Hi All. 
 Now I use soc:nrf5340, and ncs v2.9.0 
 I use QSPI to encrypt my external flash memory. Adding some print functions in xxx_encrypt can ensure that it is encrypted only once and there are no changes in between 
 
 
 I found that during the Bluetooth</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 23 Jul 2025 09:54:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question" /><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543318?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 09:54:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0411dee-e2ec-4598-b9bf-3db73d2d4f80</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;The problem is the bootutil functions expect the flash to read 0xFFs if the area is erased. A possible solution is to modify&amp;nbsp;the bootutil_public.c source to run the bootutil_buffer_is_erased() check both with and without the QSPI encryption enabled.&amp;nbsp;&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/pastedimage1753264486490v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543305?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 08:15:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad3e8f53-4ec9-4131-ab8d-2384739f381a</guid><dc:creator>ashun</dc:creator><description>&lt;p&gt;Ok I got it.&amp;nbsp;&amp;nbsp;If QSPI encryption is enabled, the data written will be the encrypted value(ciphertext) rather than the plaintext(0xFF).&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543297"]At this point, if I write in other data such as magic val, the value read out will be different from the one I have written.[/quote]
&lt;p&gt;So maybe the stored value has 0&amp;#39;s&amp;nbsp; that it can&amp;#39;t flip and then I got the wrong value.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So currently, when performing flash operations during the Bluetooth DFU process (such as checking if the flash has been erased and writing new values), do I need to manually erase that partition again and again?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543297?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 07:55:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9c681818-ff1b-4a97-99ea-de625945a1fe</guid><dc:creator>Vidar Berg</dc:creator><description>[quote userid="126131" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543291"]I mean is that I enabled QSPI encryption and wrote 0xFF. Therefore, the actual content stored in the memory is also 0xFF.[/quote]
&lt;p&gt;No, the 0xFF plaintext will be encrypted, and the ciphertext will be stored in flash.&amp;nbsp;&lt;/p&gt;
[quote userid="126131" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543291"]At this point, if I write in other data such as magic val, the value read out will be different from the one I have written.[/quote]
&lt;p&gt;This will happen if you write to a non-erased location.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543291?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 07:35:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:06f43c24-a1a2-49b9-8bdf-1ac047431370</guid><dc:creator>ashun</dc:creator><description>[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543278"]Yes, you can write to a location that is all 0xFFs. However, if your application first writes 0xFFs, the actual flash content will not remain&amp;nbsp;0xFFs because the data is passed through the stream cipher&amp;nbsp;before it&amp;#39;s written.[/quote]
&lt;p&gt;I mean is that I enabled QSPI encryption and wrote 0xFF. Therefore, the actual content stored in the memory is also 0xFF.&lt;/p&gt;
&lt;p&gt;At this point, if I write in other data such as magic val, the value read out will be different from the one I have written.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;read operation:
read magc : 0xFF, 0xFF ... , 0xFF

write operatoin:
writing magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc2 0x95 0xf3 0x60 0xd2 0xef 0x7f 0x35 0x52 0x50 0x0f 0x2c 0xb6 0x79 0x80 

read operation:
read magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc3 0xb5 0xf3 0xfa 0xf2 0xef 0x7f 0xf5 0xf6 0xf9 0x8f 0x3c 0xfe 0xff 0xc0 &lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;What&amp;#39;s going on here?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543278?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 06:16:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef9107a7-6a3c-4aae-8126-ec5a348c7fd8</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Yes, you can write to a location that is all 0xFFs. However, if your application first writes 0xFFs, the actual flash content will not remain&amp;nbsp;0xFFs because the data is passed through the stream cipher&amp;nbsp;before it&amp;#39;s written.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543264?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 01:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ca6456d-574e-4625-8e77-91fa0d817fcb</guid><dc:creator>ashun</dc:creator><description>[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543131"] and the image trailer (including the magic word) cannot be written at that location unless another erase is performed.[/quote]
&lt;p&gt;So why can do this.&lt;/p&gt;
&lt;p&gt;Currently the flash value is all 0xFF, why it can&amp;#39;t filp 1&amp;#39;s to 0&amp;#39;s?&lt;/p&gt;
&lt;p&gt;I think the actual value in Flash is 0xFF. So theoretically, the data can be written directly because it can be converted from 1&amp;#39;s to 0&amp;#39;s.&lt;/p&gt;
&lt;div id="gtx-trans" style="left:231px;position:absolute;top:149.8px;"&gt;
&lt;div class="gtx-trans-icon"&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543263?ContentTypeID=1</link><pubDate>Wed, 23 Jul 2025 01:25:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f63f548c-e00d-4ac0-8e78-b62594bdb190</guid><dc:creator>ashun</dc:creator><description>[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543131"]When the encrypted value of the 0xFFs is written to flash, the section is no longer erased, and the image trailer (including the magic word) cannot be written at that location unless another erase is performed.[/quote]
&lt;p&gt;Yes, currently if I not write the 0xFF in&amp;nbsp; &lt;strong&gt;img_mgmt_erase_image_data ,&amp;nbsp;&lt;/strong&gt;that the&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;span&gt;&lt;span&gt;&lt;strong&gt;boot_set_next&lt;/strong&gt; will return&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong&gt;BOOT_EBADIMAGE&amp;nbsp;&lt;/strong&gt;because the value is radom and decode magic value is not&amp;nbsp;boot_img_magic.val&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;But if I write the 0xFF in&amp;nbsp;&lt;strong&gt;img_mgmt_erase_image_data&lt;span&gt;&amp;nbsp;,&lt;/span&gt;&lt;/strong&gt;&lt;span&gt; that the write_boot_magic will failed such as before, writing and reading value is inconsistent. It needs erase again that the value will be consistent&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543131?ContentTypeID=1</link><pubDate>Tue, 22 Jul 2025 08:57:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6dbceed-1718-4df2-b9fc-f233078b6b6e</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;A write operation can only flip 1&amp;#39;s to 0&amp;#39;s. An erase is needed to flip all 0&amp;#39;s bits back to ones. For this reason, you must first perform an erase before updating an existing value in flash.&lt;/p&gt;
&lt;p&gt;When the encrypted value of the 0xFFs is written to flash, the section is no longer erased, and the image trailer (including the magic word) cannot be written at that location unless another erase is performed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543127?ContentTypeID=1</link><pubDate>Tue, 22 Jul 2025 08:46:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c781bee1-4e20-4e9e-a82a-a19a4e57d545</guid><dc:creator>ashun</dc:creator><description>&lt;p&gt;yes, so I rewrote the 0xFF to the flash area . but here has a new problem below&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543118?ContentTypeID=1</link><pubDate>Tue, 22 Jul 2025 08:18:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c15592e4-7c5a-40c4-8ed6-df546c34bb46</guid><dc:creator>ashun</dc:creator><description>&lt;p&gt;At present, the problem has not been located yet. The specific phenomenon is as follows.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int img_mgmt_erase_image_data(unsigned int off, unsigned int num_bytes)
{
	const struct flash_area *fa;
	char *rewrite_buf = NULL;
	int rc;

	if (off != 0) {
		rc = IMG_MGMT_ERR_INVALID_OFFSET;
		goto end;
	}

	rc = flash_area_open(g_img_mgmt_state.area_id, &amp;amp;fa);
	if (rc != 0) {
		LOG_ERR(&amp;quot;Can&amp;#39;t bind to the flash area (err %d)&amp;quot;, rc);
		rc = IMG_MGMT_ERR_FLASH_OPEN_FAILED;
		goto end;
	}

	/* align requested erase size to the erase-block-size */
	const struct device *dev = flash_area_get_device(fa);

	if (dev == NULL) {
		rc = IMG_MGMT_ERR_FLASH_AREA_DEVICE_NULL;
		goto end_fa;
	}
	struct flash_pages_info page;
	off_t page_offset = fa-&amp;gt;fa_off + num_bytes - 1;

	rc = flash_get_page_info_by_offs(dev, page_offset, &amp;amp;page);
	if (rc != 0) {
		LOG_ERR(&amp;quot;bad offset (0x%lx)&amp;quot;, (long)page_offset);
		rc = IMG_MGMT_ERR_INVALID_PAGE_OFFSET;
		goto end_fa;
	}

	size_t erase_size = page.start_offset + page.size - fa-&amp;gt;fa_off;

	rc = flash_area_flatten(fa, 0, erase_size);

	if (rc != 0) {
		LOG_ERR(&amp;quot;image slot erase of 0x%zx bytes failed (err %d)&amp;quot;, erase_size,
				rc);
		rc = IMG_MGMT_ERR_FLASH_ERASE_FAILED;
		goto end_fa;
	}

	LOG_INF(&amp;quot;Erased 0x%zx bytes of image slot&amp;quot;, erase_size);

#ifdef CONFIG_MCUBOOT_IMG_MANAGER
	/* Right now MCUmgr supports only mcuboot images.
	 * Above compilation swich might help to recognize mcuboot related
	 * code when supports for anothe bootloader will be introduced.
	 */

	/* erase the image trailer area if it was not erased */
	off = boot_get_trailer_status_offset(fa-&amp;gt;fa_size);
	if (off &amp;gt;= erase_size) {
		rc = flash_get_page_info_by_offs(dev, fa-&amp;gt;fa_off + off, &amp;amp;page);

		off = page.start_offset - fa-&amp;gt;fa_off;
		erase_size = fa-&amp;gt;fa_size - off;

		rc = flash_area_flatten(fa, off, erase_size);
		if (rc != 0) {
			LOG_ERR(&amp;quot;image slot trailer erase of 0x%zx bytes failed (err %d)&amp;quot;,
					erase_size, rc);
			rc = IMG_MGMT_ERR_FLASH_ERASE_FAILED;
			goto end_fa;
		}

		rewrite_buf = k_malloc(erase_size);
		if (rewrite_buf == NULL) {
			LOG_ERR(&amp;quot;Failed to allocate memory for trailer rewrite buffer&amp;quot;);
			rc = IMG_MGMT_ERR_NO_FREE_MEMORY;
			goto end_fa;
		}

		uint8_t erased_val = flash_area_erased_val(fa);
		memset(rewrite_buf, erased_val, erase_size);

		rc = flash_area_write(fa, off, rewrite_buf, erase_size);
		if (rc != 0)
		{
			LOG_ERR(&amp;quot;image slot trailer write of %u bytes failed (err %d)&amp;quot;,
					erase_size, rc);
			rc = IMG_MGMT_ERR_FLASH_WRITE_FAILED;
			goto end_free;

		}

		printf(&amp;quot;Erased and Rewrote %u bytes of image ID:%u, addr:0x%x\n&amp;quot;, erase_size, fa-&amp;gt;fa_id, off);

		rc = flash_area_read(fa, off, rewrite_buf, erase_size);
		for(uint32_t i=0; i&amp;lt;4096; i++) {
    	printk(&amp;quot;0x%02x &amp;quot;, rewrite_buf[i]);
			if ((i + 1) % 16 == 0) {
				printk(&amp;quot;\n&amp;quot;);
			}
    	}
	}
#endif
	rc = IMG_MGMT_ERR_OK;

end_free:
	if (rewrite_buf != NULL)
		k_free(rewrite_buf);
end_fa:
	flash_area_close(fa);
end:
	return rc;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;First of all, since I enabled QSPI encryption, after erasing the secondary partition before downloading the firmware, I need to write 0xFF to the last block of the flash.&lt;/p&gt;
&lt;p&gt;After writing, the 4k data was read out and printed - all of them were 0xFF (normal)&lt;/p&gt;
&lt;p&gt;Then, the next step is the firmware download process.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Then it comes to the &lt;strong&gt;boot_set_next&lt;/strong&gt; function. The magic and swap_info values read out through the boot_read_swap_state function are all 0xFF.&lt;/p&gt;
&lt;p&gt;It will set&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;state-&amp;gt;magic = BOOT_MAGIC_UNSET;

state-&amp;gt;swap_type = BOOT_SWAP_TYPE_NONE;
state-&amp;gt;image_num = 0;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;And the next step is&amp;nbsp;&lt;strong&gt;boot_write_magic&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As soon as I entered, I &lt;strong&gt;read&lt;/strong&gt; 4kb of data, then came the &amp;quot;&lt;strong&gt;flash_area_write magic&lt;/strong&gt;&amp;quot; process, and finally I &lt;strong&gt;read it back out&lt;/strong&gt;.The data written at this time is inconsistent with the data read out.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;code

boot_write_magic(const struct flash_area *fap)
{
    uint32_t off;
    uint32_t pad_off;
    int rc;
    uint8_t magic[BOOT_MAGIC_ALIGN_SIZE];
    uint8_t read_magic[BOOT_MAGIC_ALIGN_SIZE];
    uint8_t erased_val;

    printk(&amp;quot;boot_write_magic before------------------\n&amp;quot;);
    rc = flash_area_read(fap, 0xE1000, rewrite_buf, 4096);
    for(uint32_t i=0; i&amp;lt;4096; i++) {
    	printk(&amp;quot;0x%02x &amp;quot;, rewrite_buf[i]);
    	if ((i + 1) % 16 == 0) {
    		printk(&amp;quot;\n&amp;quot;);
    	}
    }

    off = boot_magic_off(fap);

    /* image_trailer structure was modified with additional padding such that
     * the pad+magic ends up in a flash minimum write region. The address
     * returned by boot_magic_off() is the start of magic which is not the
     * start of the flash write boundary and thus writes to the magic will fail.
     * To account for this change, write to magic is first padded with 0xFF
     * before writing to the trailer.
     */
    pad_off = ALIGN_DOWN(off, BOOT_MAX_ALIGN);
    
    erased_val = flash_area_erased_val(fap);
    printf(&amp;quot;BOOT_MAGIC_ALIGN_SIZE:%u, pad_off:%u, erase val:0x%02X\n&amp;quot;, BOOT_MAGIC_ALIGN_SIZE, pad_off, erased_val);

    memset(&amp;amp;magic[0], erased_val, sizeof(magic));
    memcpy(&amp;amp;magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);


    printf(&amp;quot;writing magic; fa_id=%d off=0x%lx (0x%lx)\n&amp;quot;,
                 flash_area_get_id(fap), (unsigned long)off,
                 (unsigned long)(flash_area_get_off(fap) + off));
    rc = flash_area_write(fap, pad_off, &amp;amp;magic[0], BOOT_MAGIC_ALIGN_SIZE);
    for(uint8_t i = 0; i &amp;lt; BOOT_MAGIC_ALIGN_SIZE; i++) {
       printf(&amp;quot;0x%02x &amp;quot;, magic[i]);
    }
    printf(&amp;quot;\n&amp;quot;);

    k_busy_wait(1000);
    rc = flash_area_read(fap, pad_off, &amp;amp;read_magic[0], BOOT_MAGIC_ALIGN_SIZE);
    if (rc != 0) {
        printf(&amp;quot;error flashhhhhhhhhh 333333\n&amp;quot;);
        return BOOT_EFLASH;
    }

    printf(&amp;quot;read magic; fa_id=%d off=0x%lx (0x%lx)\n&amp;quot;,
                 flash_area_get_id(fap), (unsigned long)off,
                 (unsigned long)(flash_area_get_off(fap) + off));
    for(uint8_t i = 0; i &amp;lt; BOOT_MAGIC_ALIGN_SIZE; i++) {
       printf(&amp;quot;0x%02x &amp;quot;, read_magic[i]);
    }
    printf(&amp;quot;\n&amp;quot;);

    printk(&amp;quot;afterrrrrr------------------\n&amp;quot;);
    rc = flash_area_read(fap, 0xE1000, rewrite_buf, 4096);
    for(uint32_t i=0; i&amp;lt;4096; i++) {
    	printk(&amp;quot;0x%02x &amp;quot;, rewrite_buf[i]);
    	if ((i + 1) % 16 == 0) {
    		printk(&amp;quot;\n&amp;quot;);
    	}
    }
    return 0;
}



logs
boot_write_magic before------------------
0xff.....
.....0xff

BOOT_MAGIC_ALIGN_SIZE:16, pad_off:925680, erase val:0xFF
writing magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc2 0x95 0xf3 0x60 0xd2 0xef 0x7f 0x35 0x52 0x50 0x0f 0x2c 0xb6 0x79 0x80 
read magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc3 0xb5 0xf3 0xfa 0xf2 0xef 0x7f 0xf5 0xf6 0xf9 0x8f 0x3c 0xfe 0xff 0xc0 

afterrrrrr------------------
0xff....
........
0x77 0xc3 0xb5 0xf3 0xfa 0xf2 0xef 0x7f 0xf5 0xf6 0xf9 0x8f 0x3c 0xfe 0xff 0xc0&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;Then I attempted to write the data here after erased the flash partition again.&lt;/div&gt;
&lt;div&gt;After erasing, the values read out all appear to be encrypted? Because the erasure operation simply sets it to 0xFF, and then I enabled QSPI encryption, the read-out value is the random value obtained by decrypting the plaintext 0xFF.&lt;/div&gt;
&lt;div&gt;At this point, write &amp;quot;magic&amp;quot; again, and then read it again. The data will be consistent.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;pre class="ui-code" data-mode="text"&gt;code

int
boot_write_magic(const struct flash_area *fap)
{
    ...
    rc = flash_area_erase(fap, 0xE1000, 4096);
    if (rc != 0) {
        printf(&amp;quot;error flashhhhhhhhhh 11111111\n&amp;quot;);
        return BOOT_EFLASH;
    }

    printk(&amp;quot;boot_write_magic before------------------\n&amp;quot;);
    rc = flash_area_read(fap, 0xE1000, rewrite_buf, 4096);
    for(uint32_t i=0; i&amp;lt;4096; i++) {
    	printk(&amp;quot;0x%02x &amp;quot;, rewrite_buf[i]);
    	if ((i + 1) % 16 == 0) {
    		printk(&amp;quot;\n&amp;quot;);
    	}
    }
    ...
}



log
boot_write_magic before------------------
0x58 0xf1 0xd6 0xf7 0x67 0x88 0xac 0x32 0x78 0x5c 0x3d 0x0b 0x01 0xc2 0xd0 0xe8 
0x17 0x10 0xc0 0xc6 0xc8 0xa7 0x2e 0x3e 0x75 0xcb 0xcf 0xf7 0x4f 0x87 0x21 0x7e\
...
0xb8 0x1b 0xbc 0x3c 0x40 0xf5 0x90 0x03 0xee 0x44 0x0d 0x65 0xc0 0x14 0xbc 0xbd 

BOOT_MAGIC_ALIGN_SIZE:16, pad_off:925680, erase val:0xFF
writing magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc2 0x95 0xf3 0x60 0xd2 0xef 0x7f 0x35 0x52 0x50 0x0f 0x2c 0xb6 0x79 0x80 
read magic; fa_id=23 off=0xe1ff0 (0x2e1ff0)
0x77 0xc2 0x95 0xf3 0x60 0xd2 0xef 0x7f 0x35 0x52 0x50 0x0f 0x2c 0xb6 0x79 0x80 

afterrrrrr------------------
0x58 0xf1 0xd6 0xf7 0x67 0x88 0xac 0x32 0x78 0x5c 0x3d 0x0b 0x01 0xc2 0xd0 0xe8 
0x17 0x10 0xc0 0xc6 0xc8 0xa7 0x2e 0x3e 0x75 0xcb 0xcf 0xf7 0x4f 0x87 0x21 0x7e
....
0x77 0xc2 0x95 0xf3 0x60 0xd2 0xef 0x7f 0x35 0x52 0x50 0x0f 0x2c 0xb6 0x79 0x80&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Therefore, I am quite puzzled. Is there an issue with the operation of rewriting to 0xFF in the previous step img_mgmt_erase_image_data? However, without this step, there will be problems with the judgment in boot_read_swap_state because the values read are random rather than 0xFF.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Why, after erasing again, did the reading and writing magic become problem-free once more?&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Perhaps I have some misunderstandings regarding the QSPI encryption and the firmware verification during the Bluetooth upgrade process.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543117?ContentTypeID=1</link><pubDate>Tue, 22 Jul 2025 08:18:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5cc9f6e3-a19a-41b9-b9ae-d41ac6d22867</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Sorry, you’re right. After looking more closely at the figure I posted in my previous comment, it’s clear that the nonce itself is fixed. The only potential issue I can think of is when the program tries to read the magic number or image trailer before anything has been written. In that case, the plaintext data (0xFFs) in the external flash will be encrypted when read as&amp;nbsp;mentioned by the answer in&amp;nbsp;this post:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/122927/nrf5340-ncsv2-9-0-ble-dfu/542453"&gt;RE: nrf5340 ncsv2.9.0 ble dfu&lt;/a&gt;&amp;nbsp;. It&amp;#39;s also important to remember to erase the QSPI flash between test runs to avoid partial writes.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543084?ContentTypeID=1</link><pubDate>Tue, 22 Jul 2025 00:35:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3bb60c3a-c937-4c06-a5e0-98d014802940</guid><dc:creator>ashun</dc:creator><description>[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543049"]ut it seems to me that the issue is that the wrong nounce is used when encrypting and decrypting the image trailer data.&amp;nbsp;[/quote]
&lt;p&gt;This nounce is loaded only once during the boot process. It is not called from any other location.&lt;/p&gt;
&lt;p&gt;I added the printing function inside, but it only appeared once. There shouldn&amp;#39;t be any other modifications elsewhere.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="4240" url="~/f/nordic-q-a/123119/nrf5340-qspi-has-encountered-question/543049"]&lt;p&gt;Could it be an option to disable the&amp;nbsp;&lt;span&gt;stream cipher when reading or writing header data?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;[/quote]
&lt;p&gt;Do you mean that when operating this, I need to temporarily disable both the encryption registers?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p class="p"&gt;&lt;strong&gt;The same nonce and key must be used for both encryption and decryption of the same memory address.&lt;/strong&gt;&lt;/p&gt;
&lt;p class="p"&gt;Regarding this, I encrypted an entire external flash. So, should stream ciphers related to the external flash automatically perform encryption and decryption?&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="p"&gt;Because all other external flash operations are normal encryption and decryption processes, and there are no issues with data writing and reading.&lt;/p&gt;
&lt;p class="p"&gt;I don&amp;#39;t know why after writing data to the trailer of the image during the upgrade process, the data read back is problematic.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/543049?ContentTypeID=1</link><pubDate>Mon, 21 Jul 2025 14:02:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a19c88ff-92f9-4bc4-b4a4-064fcf7370ab</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Since this is currently not supported in the driver I&amp;#39;m not sure how this HW feature is supposed to be used, but it seems to me that the issue is that the wrong nounce is used when encrypting and decrypting the image trailer data.&amp;nbsp;&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/8463.pastedimage1753106509719v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Could it be an option to disable the&amp;nbsp;&lt;span&gt;stream cipher when reading or writing&amp;nbsp;image trailer data?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Vidar&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf5340 qspi has encountered question</title><link>https://devzone.nordicsemi.com/thread/542986?ContentTypeID=1</link><pubDate>Mon, 21 Jul 2025 10:20:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:72ffd2ce-8e6a-436f-9314-63a46598fa62</guid><dc:creator>ashun</dc:creator><description>&lt;p&gt;In the &lt;strong&gt;img_mgmt_erase_image_data&lt;/strong&gt; function located in&lt;strong&gt; zephyr\subsys\mgmt\mcumgr\grp\img_mgmt\src\zephyr_img_mgmt.c&lt;/strong&gt;, the flash_area_write/read operations are normal and the data is consistent.&lt;br /&gt;&lt;br /&gt;but in the&amp;nbsp;&lt;strong&gt;boot_write_magic&amp;nbsp;&lt;/strong&gt;function located in&amp;nbsp;&lt;strong&gt;bootloader\mcuboot\boot\bootutil\src\bootutil_public.c,&amp;nbsp;&lt;/strong&gt;it was not work.&lt;span&gt;the flash_area_write/read operations are normal and the data is inconsistent.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;bootloader\mcuboot\boot\zephyr\main.c,&amp;nbsp;Is everything work normal here?&lt;/strong&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;SYS_INIT&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;encrypt_external_flash&lt;/span&gt;&lt;span&gt;, POST_KERNEL, &lt;/span&gt;&lt;span&gt;42&lt;/span&gt;&lt;span&gt;);&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>