NCS MCUboot and NVS

NCS: 2.3.0

OS: Windows 10I have a question regarding how NVS is managed with MCUboot enabled.

So the way I understand it, when MCUboot is enabled, it automatically enabled partition manager.  This means all the flash partitions in the device tree are ignored.

Flash section from the custom project dts file...

&flash0 {

    partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;

        boot_partition: partition@0 {
            label = "mcuboot";
            reg = <0x000000000 0xC000>;
        };
        slot0_partition: partition@c000 {
            label = "image-0";
            reg = <0x0000C000 0x32000>;
        };
        slot1_partition: partition@3e000 {
            label = "image-1";
            reg = <0x0003E000 0x32000>;
        };
        scratch_partition: partition@70000 {
            label = "image-scratch";
            reg = <0x00070000 0xA000>;
        };
        storage_partition: partition@7e000 {
            label = "storage";
            reg = <0x0007e000 0x00002000>;
        };
    };
};
And the corresponding partition report with MCUboot enabled.  Clearly shows the dts &flash0 is ignored.

PS C:\Virtuix\nordic\myapps\o1_agg_nrf52833\build> west build -t partition_manager_report
-- west build: running target partition_manager_report
[1/1] cmd.exe /C "cd /D C:\Virtuix\nordic\myapps\o1_agg_n...rtuix/nordic/myapps/o1_agg_nrf52833/build/partitions.yml" flash_primary (0x80000 - 512kB):
+-------------------------------------------------+
| 0x0: mcuboot (0x6000 - 24kB) |
+---0x6000: mcuboot_primary (0x3c000 - 240kB)-----+
| 0x6000: mcuboot_pad (0x200 - 512B) |
+---0x6200: mcuboot_primary_app (0x3be00 - 239kB)-+
| 0x6200: app (0x3be00 - 239kB) |
+-------------------------------------------------+
| 0x42000: mcuboot_secondary (0x3c000 - 240kB) |
| 0x7e000: EMPTY_0 (0x1000 - 4kB) |
| 0x7f000: nvs_storage (0x1000 - 4kB) |
+-------------------------------------------------+

This does not seem to be the case for NVS storage.  I want to limit the size of NVS to as small as possible so as much flash can be used MCUboot as possible.  I was told in another devzone post that the smallest number of flash pages that can be used is two (each page is 4096 bytes).  One for data and one for swap.

devzone.nordicsemi.com/.../partition-manager-and-nvs-size-change
What I discovered is there are configuration options in three different locations. 


1. prj.conf
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_NVS=y
# Each page is 4096 (0x1000) you need to set aside a minimum
# two flash pages (one for data and use for swap/scratch
CONFIG_PM_PARTITION_SIZE_NVS_STORAGE=0x2000   <<<<<<<<  Two  pages

2. dts file

        storage_partition: partition@7e000 {
            label = "storage";
            reg = <0x0007e000 0x00002000>;   <<<<<<< Matching 0x2000 (two pages)
        };

3. NVS init routine

int nvs_flash_init(void)
{
    int err;

    printk_inf("Initializing flash device...");

    /* define the nvs file system by settings with:
     *  sector_size equal to the pagesize,
     *  3 sectors
     *  starting at FLASH_AREA_OFFSET(storage)
     */
    //nvs_fs.flash_device = FLASH_AREA_DEVICE(STORAGE_NODE_LABEL); //NVS_PARTITION_DEVICE;   // updated for NCS 2.2.0
    nvs_fs.flash_device = NVS_PARTITION_DEVICE;  // updated for NCS 2.2.0
    if (!device_is_ready(nvs_fs.flash_device)) {
        printk_err("Flash device %s is not ready", nvs_fs.flash_device->name);
        return (-1);
    }

    //nvs_fs.offset = FLASH_AREA_OFFSET(STORAGE_NODE_LABEL); //NVS_PARTITION_OFFSET;        // updated for NCS 2.2.0
    nvs_fs.offset = NVS_PARTITION_OFFSET;       // updated for NCS 2.2.0
    ERR_CHECK(flash_get_page_info_by_offs(nvs_fs.flash_device, nvs_fs.offset, &nvs_info), "Unable to get flash page info");

    nvs_fs.sector_size = nvs_info.size;
    nvs_fs.sector_count = 2U;                <<<< Matching two pages/sectors

    ERR_CHECK(nvs_mount(&nvs_fs), "Unable to mount flash");
    printk_inf("Flash device intialized");
    return(0);
}

If any of them do not match, I get these error messages.

[00:00:01:002] <info> nvs: Initializing flash device...
[00:00:01.002,380] <err> flash_nrf: flash_nrf_read: invalid address: 0x00080ff8:8
[00:00:01.005,859] <err> flash_nrf: flash_nrf_read: invalid address: 0x00080000:32
[00:00:01:005] <error> nvs: Error -22 occurred in nvs_mount(&nvs_fs) in ../src/nvs.c line 60. Unable to mount flash

Can someone give a thorough explanation of "how" NVS is managed when MCUboot is enabled and how it should be properly configured?  Even though the above works as long as all three locations have matching configuration, it simply doesn't seem like the correct way to do it.

Related