@@ -72,6 +72,15 @@ LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_LOG_LEVEL);
7272 #define BOOTLOADER_NAME "MCUBOOT"
7373 #endif
7474
75+ /* Validate the MCUboot bootloader mode compatibility with this DFU module. */
76+ #if CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP
77+ BUILD_ASSERT (CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_DIRECT_XIP );
78+ #elif CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD
79+ BUILD_ASSERT (CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_RAM_LOAD );
80+ #else
81+ BUILD_ASSERT (CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_UNSPECIFIED );
82+ #endif
83+
7584 #if CONFIG_PARTITION_MANAGER_ENABLED
7685 #include <pm_config.h>
7786
@@ -123,18 +132,28 @@ LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_LOG_LEVEL);
123132 #define MCUBOOT_PRIMARY_SLOT_ID DT_FIXED_PARTITION_ID(MCUBOOT_PRIMARY_NODE)
124133 #define MCUBOOT_SECONDARY_SLOT_ID DT_FIXED_PARTITION_ID(MCUBOOT_SECONDARY_NODE)
125134
126- /* Use range check to allow for placing MCUboot header in a separate partition,
127- * so the application code partition is not an alias for the MCUboot partition,
128- * but a subpartition of the MCUboot partition.
129- */
130- #if (CODE_PARTITION_START_ADDR >= MCUBOOT_PRIMARY_START_ADDR ) && \
131- (CODE_PARTITION_START_ADDR < MCUBOOT_PRIMARY_END_ADDR )
132- #define DFU_SLOT_ID MCUBOOT_SECONDARY_SLOT_ID
133- #elif (CODE_PARTITION_START_ADDR >= MCUBOOT_SECONDARY_START_ADDR ) && \
134- (CODE_PARTITION_START_ADDR < MCUBOOT_SECONDARY_END_ADDR )
135- #define DFU_SLOT_ID MCUBOOT_PRIMARY_SLOT_ID
135+
136+ #if CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_RAM_LOAD
137+ /* Use the macro alias for the DFU slot ID that needs to be set in runtime
138+ * when the MCUboot is configured for the RAM load mode.
139+ */
140+ #define DFU_SLOT_ID_INVALID (0xFF)
141+ static uint8_t dfu_slot_id = DFU_SLOT_ID_INVALID ;
142+ #define DFU_SLOT_ID (dfu_slot_id)
136143 #else
137- #error Missing partition definitions in DTS.
144+ /* Use range check to allow for placing MCUboot header in a separate
145+ * partition,so the application code partition is not an alias for the
146+ * MCUboot partition, but a subpartition of the MCUboot partition.
147+ */
148+ #if (CODE_PARTITION_START_ADDR >= MCUBOOT_PRIMARY_START_ADDR ) && \
149+ (CODE_PARTITION_START_ADDR < MCUBOOT_PRIMARY_END_ADDR )
150+ #define DFU_SLOT_ID MCUBOOT_SECONDARY_SLOT_ID
151+ #elif (CODE_PARTITION_START_ADDR >= MCUBOOT_SECONDARY_START_ADDR ) && \
152+ (CODE_PARTITION_START_ADDR < MCUBOOT_SECONDARY_END_ADDR )
153+ #define DFU_SLOT_ID MCUBOOT_PRIMARY_SLOT_ID
154+ #else
155+ #error Missing partition definitions in DTS.
156+ #endif
138157 #endif
139158 #else
140159 #error Unsupported partitioning scheme.
@@ -143,6 +162,15 @@ LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_LOG_LEVEL);
143162 #error Bootloader not supported.
144163#endif
145164
165+ /* This value denotes whether the revert feature is supported by the bootloader.
166+ * Currently, the revert feature is only supported by the MCUboot bootloader that
167+ * is not configured for the standard direct-xip or RAM load mode.
168+ */
169+ #define DFU_REVERT_FEATURE_IS_SUPPORTED \
170+ (CONFIG_BOOTLOADER_MCUBOOT) && \
171+ !(CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_DIRECT_XIP) && \
172+ !(CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_RAM_LOAD)
173+
146174static struct k_work_delayable dfu_timeout ;
147175static struct k_work_delayable reboot_request ;
148176static struct k_work_delayable background_erase ;
@@ -357,7 +385,7 @@ static void complete_dfu_data_store(void)
357385
358386 if (cur_offset == img_length ) {
359387 LOG_INF ("DFU image written" );
360- #if CONFIG_BOOTLOADER_MCUBOOT && ! CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_DIRECT_XIP
388+ #if DFU_REVERT_FEATURE_IS_SUPPORTED
361389 int err = boot_request_upgrade (false);
362390 if (err ) {
363391 LOG_ERR ("Cannot request the image upgrade (err:%d)" , err );
@@ -879,6 +907,26 @@ static void fetch_config(const uint8_t opt_id, uint8_t *data, size_t *size)
879907 }
880908}
881909
910+ #if CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_RAM_LOAD
911+ static void dfu_slot_id_runtime_init (void )
912+ {
913+ uint8_t active_slot_id ;
914+
915+ __ASSERT_NO_MSG (dfu_slot_id == DFU_SLOT_ID_INVALID );
916+
917+ active_slot_id = boot_fetch_active_slot ();
918+ dfu_slot_id = (active_slot_id == MCUBOOT_SECONDARY_SLOT_ID ) ?
919+ MCUBOOT_PRIMARY_SLOT_ID : MCUBOOT_SECONDARY_SLOT_ID ;
920+
921+ __ASSERT_NO_MSG (dfu_slot_id != DFU_SLOT_ID_INVALID );
922+ __ASSERT_NO_MSG ((active_slot_id == MCUBOOT_PRIMARY_SLOT_ID ) ||
923+ (active_slot_id == MCUBOOT_SECONDARY_SLOT_ID ));
924+
925+ LOG_INF ("DFU slot configured in runtime for the %s slot" ,
926+ dfu_slot_id == MCUBOOT_PRIMARY_SLOT_ID ? "primary" : "secondary" );
927+ }
928+ #endif
929+
882930static bool app_event_handler (const struct app_event_header * aeh )
883931{
884932 if (is_hid_report_event (aeh )) {
@@ -902,7 +950,12 @@ static bool app_event_handler(const struct app_event_header *aeh)
902950
903951 if (check_state (event , MODULE_ID (main ), MODULE_STATE_READY )) {
904952 int err ;
905- #if CONFIG_BOOTLOADER_MCUBOOT && !CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_DIRECT_XIP
953+
954+ #if CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_MCUBOOT_RAM_LOAD
955+ dfu_slot_id_runtime_init ();
956+ #endif
957+
958+ #if DFU_REVERT_FEATURE_IS_SUPPORTED
906959 err = boot_write_img_confirmed ();
907960
908961 if (err ) {
0 commit comments