|
| 1 | +.. _ab_split_sample: |
| 2 | + |
| 3 | +A/B with MCUboot and separated slots |
| 4 | +#################################### |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +The A/B with MCUboot and separated slots sample demonstrates how to configure the application for updates using the A/B method using MCUboot. |
| 11 | +This sample is a variant of the :ref:`A/B sample <ab_sample>`, where the application and radio images are not merged, but reside in separate MCUboot slots. |
| 12 | +This split increases the number of memory areas that must be individually protected from accidental writes. |
| 13 | +It also requires additional care when preparing updates to ensure that only a compatible set of slots is booted. |
| 14 | +The additional dependency check during the boot process increases the time to boot the system. |
| 15 | + |
| 16 | +It also includes an example to perform a device health check before confirming the image after the update. |
| 17 | +You can update the sample using the Simple Management Protocol (SMP) with UART or Bluetooth® Low Energy. |
| 18 | + |
| 19 | +To prevent the build system from merging slots, the sysbuild :kconfig:option:`SB_CONFIG_MCUBOOT_SIGN_MERGED_BINARY` option is disabled. |
| 20 | +To enable manifest-based dependency management, the :kconfig:option:`SB_CONFIG_MCUBOOT_MANIFEST_UPDATES=y` option is enabled in the :file:`sysbuild.conf` file. |
| 21 | + |
| 22 | +Requirements |
| 23 | +************ |
| 24 | + |
| 25 | +The sample supports the following development kits: |
| 26 | + |
| 27 | +.. table-from-sample-yaml:: |
| 28 | + |
| 29 | +You need the nRF Device Manager app for update over Bluetooth® Low Energy: |
| 30 | + |
| 31 | +* `nRF Device Manager mobile app for Android`_ |
| 32 | +* `nRF Device Manager mobile app for iOS`_ |
| 33 | + |
| 34 | + |
| 35 | +Overview |
| 36 | +******** |
| 37 | + |
| 38 | +This sample demonstrates a firmware update using the A/B method. |
| 39 | +This method allows you to store two copies of the application in non-volatile memory (NVM). |
| 40 | +You can switch between these copies without performing a swap. |
| 41 | +This solution significantly reduces the time during the update in which the device is unavailable. |
| 42 | +The switch between images can be triggered by the application or, for example, by a hardware button. |
| 43 | + |
| 44 | +This sample implements an SMP server. |
| 45 | +SMP is a basic transfer encoding used with the MCUmgr management protocol. |
| 46 | +For more information about MCUmgr and SMP, see :ref:`device_mgmt`. |
| 47 | + |
| 48 | +The sample supports the following MCUmgr transports by default: |
| 49 | + |
| 50 | +* Bluetooth |
| 51 | +* Serial (UART) |
| 52 | + |
| 53 | +A/B functionality |
| 54 | +================= |
| 55 | + |
| 56 | +When you use the A/B configuration with separated slots, the device provides two slots for each set of application and radio firmware: slot A and slot B. |
| 57 | +The slots are equivalent, and the device can boot from either of them. |
| 58 | +With MCUboot, this is achieved by using the Direct XIP feature. |
| 59 | +By design, slot A of the application image boots slot A of the radio image. |
| 60 | +This design implies that verifying the image pairs correctly requires a manifest-based dependency. |
| 61 | +There can be only one image that includes the manifest TLV. |
| 62 | +Its index is configured using :kconfig:option:`SB_CONFIG_MCUBOOT_MANIFEST_IMAGE_INDEX`. |
| 63 | +By default, the application image index (``0``) is selected. |
| 64 | + |
| 65 | +In this document, the following conventions are followed: |
| 66 | + |
| 67 | +* The application image index (``0``) is referred to as the *manifest image*. |
| 68 | +* The following names refer to the same images and are used interchangeably throughout the documentation: |
| 69 | + |
| 70 | + * *Slot 0*, *primary slot*, and *slot A* |
| 71 | + * *Slot 1*, *secondary slot*, and *slot B* |
| 72 | + |
| 73 | +This configuration allows a background update of the non-active slot while the application runs from the active slot. |
| 74 | +After the update is complete, the device can quickly switch to the updated slot on the next reboot. |
| 75 | + |
| 76 | +The following conditions decide which slot is considered *active* and is booted on the next reboot: |
| 77 | + |
| 78 | +1. If one of the slots of the manifest image contains a valid image, it is marked as valid only if all other images, described by the manifest are present and placed in the same slot as the manifest. |
| 79 | +#. If one of the slots of the manifest image is not valid, the other slot is selected as active. |
| 80 | +#. If both slots of the manifest image are valid, the slot marked as "preferred" is selected as active. |
| 81 | +#. If both slots of the manifest image are valid and none is marked as *preferred*, the slot with the higher version number is selected as active. |
| 82 | +#. If none of the above conditions is met, slot A is selected as active. |
| 83 | +#. For all other images, the same slot is selected. |
| 84 | + |
| 85 | +You can set the preferred slot using the ``boot_request_set_preferred_slot`` function. |
| 86 | +Currently, this only sets the boot preference for a single reboot. |
| 87 | + |
| 88 | +Identifying the active slot |
| 89 | +--------------------------- |
| 90 | + |
| 91 | +If the project uses the Partition Manager, the currently running slot can be identified by checking if ``CONFIG_NCS_IS_VARIANT_IMAGE`` is defined. |
| 92 | +If it is defined, the application is running from slot B. |
| 93 | +Otherwise, it is running from slot A. |
| 94 | + |
| 95 | +If the project does not use the Partition Manager (a configuration currently supported only on the nRF54H20 SoC), you can identify the currently running slot by comparing the address referenced by ``zephyr,code-partition`` with the specific node addresses defined in the devicetree. |
| 96 | +The following node partitions are used by default: |
| 97 | + |
| 98 | +* ``cpuapp_slot0_partition`` - Application core, slot A |
| 99 | +* ``cpuapp_slot1_partition`` - Application core, slot B |
| 100 | +* ``cpurad_slot0_partition`` - Radio core, slot A |
| 101 | +* ``cpurad_slot1_partition`` - Radio core, slot B |
| 102 | + |
| 103 | +For example, verifying that the application is running from slot A can be done by using the following macro: |
| 104 | + |
| 105 | +.. code-block:: c |
| 106 | +
|
| 107 | + #define IS_RUNNING_FROM_SLOT_A \ |
| 108 | + (FIXED_PARTITION_NODE_OFFSET(DT_CHOSEN(zephyr_code_partition)) == \ |
| 109 | + FIXED_PARTITION_OFFSET(cpuapp_slot0_partition)) |
| 110 | +
|
| 111 | +.. _ab_split_build_files: |
| 112 | + |
| 113 | +Build files |
| 114 | +----------- |
| 115 | + |
| 116 | +This sample overrides the default build strategy, so application and radio images are built separately. |
| 117 | +In this case, you must send the following files to the device when performing an update: |
| 118 | + |
| 119 | + |
| 120 | +* :file:`build/mcuboot_secondary_app/zephyr/zephyr.signed.bin` - Contains the slot B of the application image. |
| 121 | + Upload this file to the secondary slot when the device is running from slot A. |
| 122 | +* :file:`build/ipc_radio_secondary_app/zephyr/zephyr.signed.bin` - Contains the slot B of the radio image. |
| 123 | + Upload this file to the secondary slot when the device is running from slot A. |
| 124 | +* :file:`build/ab/zephyr/zephyr.signed.bin` - Contains the slot A of the application image. |
| 125 | + Upload this file to the primary slot when the device is running from slot B. |
| 126 | +* :file:`build/ipc_radio/zephyr/zephyr.signed.bin` - Contains the slot A of the radio image. |
| 127 | + Upload this file to the primary slot when the device is running from slot B. |
| 128 | + |
| 129 | +User interface |
| 130 | +************** |
| 131 | + |
| 132 | +LED 0: |
| 133 | + This LED indicates that the application is running from slot A. |
| 134 | + It is controlled as active low. |
| 135 | + This means that it turns on once the application is booted and turns off in short intervals to blinks. |
| 136 | + The number of short blinks is configurable using the :kconfig:option:`CONFIG_N_BLINKS` Kconfig option. |
| 137 | + It remains off when the application is running from slot B. |
| 138 | + |
| 139 | +LED 1: |
| 140 | + This LED indicates that the application is running from slot B. |
| 141 | + It is controlled as active low. |
| 142 | + This means that it turns on once the application is booted and turns off at short intervals to blinks. |
| 143 | + The number of short blinks is configurable using the :kconfig:option:`CONFIG_N_BLINKS` Kconfig option. |
| 144 | + It remains off when the application is running from slot A. |
| 145 | + |
| 146 | +Button 0: |
| 147 | + By pressing this button, you select the non-active slot as the preferred slot for the next reboot. |
| 148 | + This preference applies only to the next boot and is cleared after the subsequent reset. |
| 149 | + |
| 150 | +Configuration |
| 151 | +************* |
| 152 | + |
| 153 | +|config| |
| 154 | + |
| 155 | +Configuration options |
| 156 | +===================== |
| 157 | + |
| 158 | +Check and configure the following configuration options for the sample: |
| 159 | + |
| 160 | +.. _CONFIG_N_BLINKS_ABSPLIT: |
| 161 | + |
| 162 | +CONFIG_N_BLINKS - The number of blinks. |
| 163 | + This configuration option sets the number of times the LED corresponding to the currently active slot blinks (LED0 for slot A, LED1 for slot B). |
| 164 | + The default value of the option is set to ``1``, causing a single blink to indicate *Version 1*. |
| 165 | + You can increment this value to represent an update, such as set it to ``2`` to indicate *Version 2*. |
| 166 | + |
| 167 | +.. _CONFIG_EMULATE_APP_HEALTH_CHECK_FAILURE_AB_SPLIT: |
| 168 | + |
| 169 | +CONFIG_EMULATE_APP_HEALTH_CHECK_FAILURE - Enables emulation of a broken application that fails the self-test. |
| 170 | + This configuration option emulates a broken application that does not pass the self-test. |
| 171 | + |
| 172 | +Additional configuration |
| 173 | +======================== |
| 174 | + |
| 175 | +Check and configure the :kconfig:option:`CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION` Kconfig option for the MCUboot library. |
| 176 | +This configuration option sets the version to pass to imgtool when signing. |
| 177 | +To ensure the updated build is preferred after a DFU, set this option to a higher version than the version currently running on the device. |
| 178 | + |
| 179 | +Building and running |
| 180 | +******************** |
| 181 | + |
| 182 | +.. |sample path| replace:: :file:`samples/dfu/ab_split` |
| 183 | + |
| 184 | +.. include:: /includes/build_and_run.txt |
| 185 | + |
| 186 | +Testing |
| 187 | +======= |
| 188 | + |
| 189 | +To perform DFU using the `nRF Connect Device Manager`_ mobile app, complete the following steps: |
| 190 | + |
| 191 | +.. include:: /app_dev/device_guides/nrf52/fota_update.rst |
| 192 | + :start-after: fota_upgrades_over_ble_nrfcdm_common_dfu_steps_start |
| 193 | + :end-before: fota_upgrades_over_ble_nrfcdm_common_dfu_steps_end |
| 194 | + |
| 195 | +Instead of using the :file:`dfu_application.zip` file, you can also send the appropriate binary file directly, as described in :ref:`ab_split_build_files`. |
| 196 | +Make sure to select the correct file based on the currently running slot. |
| 197 | + |
| 198 | +Dependencies |
| 199 | +************ |
| 200 | + |
| 201 | +This sample uses the following |NCS| library: |
| 202 | + |
| 203 | +* :ref:`MCUboot <mcuboot_index_ncs>` |
0 commit comments