Hi Ken,

> Could help to provide the details so that we can see what the problem is? Looks like you mentioned two problems here, one for the un-aligned memory accessing and another is the FLASH read/write.

 

Sure. This is being based on the nRF5340 (Cortex-M33) chipset, it uses image swapping rather than overwriting, the primary image is located in the on-chip flash and the secondary slot is located in QSPI flash, the scratch slot is also located in QSPI flash, this is the trailing part of the log when the issue with trying to write to QSPI from a non-RAM buffer occurs (GCC build):

[ERR] Qerase 0xfe000

[ERR] Qerase 0xff000

[ERR] read area=3, off=0x9fff0, len=0x10

[ERR] Qread 0x9fff0 10 to 0x20005ad8

[ERR] read area=3, off=0x9ffd8, len=0x1

[ERR] Qread 0x9ffd8 1 to 0x20005ad7

[ERR] read area=3, off=0x9ffe0, len=0x1

[ERR] Qread 0x9ffe0 1 to 0x20005b22

[ERR] read area=3, off=0x9ffe8, len=0x1

[ERR] Qread 0x9ffe8 1 to 0x20005b23

[ERR] write area=5, off=0x1ffd8, len=0x4

[ERR] Qwrite 0xfffd8 4 from 0x20005ae0

[ERR] write area=5, off=0x1ffd0, len=0x4

[ERR] Qwrite 0xfffd0 4 from 0x20005ae0

[ERR] write area=5, off=0x1fff0, len=0x10

[ERR] Qwrite 0xffff0 10 from 0xcfdc

[ERR] QSPI write failed -400

assertion "rc == 0" failed: file "lib/ext/mcuboot-src/boot/bootutil/src/swap_misc.c", line 125, function: swap_status_init

The final write fails because it is attempting to write to QSPI from a non-RAM address, 0xcfdc is an address in the internal flash. Unless this is possibly an issue with the buffer address being overwritten somehow?

> For the memory alignment, to ensure the isolation setting all sections should be 32-bytes aligned; and the stack-alignment is 8 bytes. Other contents are set as default, the toolchain should have handled them correctly.

 

This is the error in the trailing part of the log I am getting with it reading 1 byte at a time to an invalid buffer:

[ERR] read area=1, off=0, len=0x20

[ERR] Fread 0x10000 0x20 to 0x20002530

[ERR] read area=3, off=0, len=0x20

[ERR] Qread 0x0 0x20 to 0x2000255c

[ERR] read area=1, off=0x9fff0, len=0x10

[ERR] Fread 0xafff0 10 to 0x20005b60

[ERR] read area=1, off=0x9ffd8, len=0x1

[ERR] Fread 0xaffd8 1 to 0x20005b5f

[ERR] read area=1, off=0x9ffe0, len=0x1

[ERR] Fread 0xaffe0 1 to 0x20005baa

[ERR] read area=1, off=0x9ffe8, len=0x1

[ERR] Fread 0xaffe8 1 to 0x20005bab

[ERR] read area=5, off=0x1fff0, len=0x10

[ERR] Qread 0xffff0 10 to 0x20005b60

[ERR] read area=5, off=0x1ffd8, len=0x1

[ERR] Qread 0xfffd8 1 to 0x20005b5f

[ERR] Qread QSPI failed

assertion "rc == 0" failed: file "lib/ext/mcuboot-src/boot/bootutil/src/swap_scratch.c", line 384, function: swap_status_source

The 2 issues are QSPI reads must be multiples of 4 bytes, it cannot read just 1 byte, and secondly the 0x20005b5f buffer is not 4-byte aligned so it cannot be used to store the data anyway. The same GCC linker .ld files are being used as are currently in the TF-M repository for the nRF5340-DK which is 4-byte aligned for data and bss and 8-byte aligned for heap.

Qread/Qwrite = QSPI, Fread/Write = internal flash, area 5 is the scratch partition. This is TF-M built with separate secure and non-secure image slots.

 

> Please provide the details, thank you!

>

> /Ken

>

> From: TF-M <tf-m-bounces at lists.trustedfirmware.org> On Behalf Of Jamie Mccrae via TF-M

> Sent: Monday, March 29, 2021 10:19 PM

> To: tf-m at lists.trustedfirmware.org

> Subject: [TF-M] Issues with alignment and buffer locations

>

> Hi,

> There seems to be a large oversight in the TF-M codeline relating to buffer alignment and locations of buffers, in terms of buffer alignment, some ARM-based silicon has restrictions on buffer alignment, generally 4-byte boundaries, this means that if a non-4 byte boundary address, buffer or data size is provided, the operation cannot be completed. Another issue is that some ARM-based silicon has restrictions on where data can be located, e.g. in RAM only for writing and not from other address spaces.

> I am currently trying to get TF-M working on a Nordic nRF53-based designed and have encountered these issues many times, and can see no way to set an alignment, data size or address space limitation in the whole code line which I find to be quite displeasing given that both the silicon and software are ARM-based, I would expect the software you give for silicon based on your reference designs to actually work with it out of the box rather than need lots of work to have these limitations supported. On Nordic silicon, data can only be written and read from RAM e.g. you cannot write to flash from a flash offset, and writes must be 4-byte aligned and 4-byte sized (the QSPI  functionality is being used here).

>

> So I am emailing here to ask: what is the suggestion for dealing with this? Why does the baseline project not (from what I can see) have any sort of support for this?

> Thanks,

> Jamie

THIS MESSAGE, ANY ATTACHMENT(S), AND THE INFORMATION CONTAINED HEREIN MAY BE PROPRIETARY TO LAIRD CONNECTIVITY, INC. AND/OR ANOTHER PARTY, AND MAY FURTHER BE INTENDED TO BE KEPT CONFIDENTIAL. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE DELETE THE EMAIL AND ANY ATTACHMENTS, AND IMMEDIATELY NOTIFY THE SENDER BY RETURN EMAIL. THIS MESSAGE AND ITS CONTENTS ARE THE PROPERTY OF LAIRD CONNECTIVITY, INC. AND MAY NOT BE REPRODUCED OR USED WITHOUT THE EXPRESS WRITTEN CONSENT OF LAIRD CONNECTIVITY, INC.