1414
1515LOG_MODULE_DECLARE (ab_sample );
1616
17- #define ACTIVE_IMAGE 0
18-
1917#define CODE_PARTITION DT_CHOSEN(zephyr_code_partition)
2018#define CODE_PARTITION_OFFSET FIXED_PARTITION_NODE_OFFSET(CODE_PARTITION)
2119
2220#define SLOT_A_PARTITION cpuapp_slot0_partition
2321#define SLOT_B_PARTITION cpuapp_slot1_partition
24- #define CPURAD_SLOT_A_PARTITION cpurad_slot0_partition
25- #define CPURAD_SLOT_B_PARTITION cpurad_slot1_partition
2622
2723#define SLOT_A_OFFSET FIXED_PARTITION_OFFSET(SLOT_A_PARTITION)
2824#define SLOT_B_OFFSET FIXED_PARTITION_OFFSET(SLOT_B_PARTITION)
@@ -31,8 +27,6 @@ LOG_MODULE_DECLARE(ab_sample);
3127
3228#define SLOT_A_FLASH_AREA_ID FIXED_PARTITION_ID(SLOT_A_PARTITION)
3329#define SLOT_B_FLASH_AREA_ID FIXED_PARTITION_ID(SLOT_B_PARTITION)
34- #define CPURAD_SLOT_A_FLASH_AREA_ID FIXED_PARTITION_ID(CPURAD_SLOT_A_PARTITION)
35- #define CPURAD_SLOT_B_FLASH_AREA_ID FIXED_PARTITION_ID(CPURAD_SLOT_B_PARTITION)
3630
3731#define IS_SLOT_A \
3832 (CODE_PARTITION_OFFSET >= SLOT_A_OFFSET && \
@@ -45,12 +39,6 @@ LOG_MODULE_DECLARE(ab_sample);
4539#define STATUS_LEDS_THREAD_PRIORITY (CONFIG_NUM_PREEMPT_PRIORITIES - 1)
4640K_THREAD_STACK_DEFINE (status_leds_thread_stack_area , STATUS_LEDS_THREAD_STACK_SIZE );
4741
48- enum ab_boot_slot {
49- SLOT_A = 0 ,
50- SLOT_B = 1 ,
51- SLOT_INVALID ,
52- };
53-
5442/** @brief Radio firmware self test
5543 *
5644 * @details
@@ -78,44 +66,79 @@ static bool app_domain_healthy(void)
7866 return true;
7967}
8068
81- static enum ab_boot_slot active_boot_slot_get (void )
69+ static enum boot_slot active_boot_slot_get (void )
8270{
83- enum ab_boot_slot active_slot = SLOT_INVALID ;
71+ enum boot_slot active_slot = BOOT_SLOT_NONE ;
8472
8573 if (IS_SLOT_A ) {
86- active_slot = SLOT_A ;
74+ active_slot = BOOT_SLOT_PRIMARY ;
8775 } else if (IS_SLOT_B ) {
88- active_slot = SLOT_B ;
76+ active_slot = BOOT_SLOT_SECONDARY ;
8977 } else {
9078 LOG_ERR ("Cannot determine current slot" );
9179 }
9280
9381 return active_slot ;
9482}
9583
96- static void device_healthcheck ( void )
84+ static bool slot_confirmed ( enum boot_slot slot )
9785{
98- int err ;
99- char * img_set = NULL ;
86+ struct boot_swap_state state ;
10087 const struct flash_area * fa ;
10188 int area_id = -1 ;
102- int cpurad_area_id = -1 ;
103- enum ab_boot_slot active_slot = active_boot_slot_get ();
104-
105- if (active_slot == SLOT_INVALID ) {
106- return ;
107- }
89+ char * img_set = NULL ;
90+ bool confirmed = false;
91+ int ret ;
10892
109- /* Confirming only in non-degraded boot states
110- */
111- if (active_slot == SLOT_A ) {
93+ if (slot == BOOT_SLOT_PRIMARY ) {
11294 img_set = "A" ;
11395 area_id = SLOT_A_FLASH_AREA_ID ;
114- cpurad_area_id = CPURAD_SLOT_A_FLASH_AREA_ID ;
115- } else if (active_slot == SLOT_B ) {
96+ } else if (slot == BOOT_SLOT_SECONDARY ) {
11697 img_set = "B" ;
11798 area_id = SLOT_B_FLASH_AREA_ID ;
118- cpurad_area_id = CPURAD_SLOT_B_FLASH_AREA_ID ;
99+ } else {
100+ LOG_ERR ("Cannot determine slot to check for confirmation" );
101+ return false;
102+ }
103+
104+ if (flash_area_open (area_id , & fa ) != 0 ) {
105+ LOG_ERR ("Cannot open flash area for slot %s" , img_set );
106+ return false;
107+ }
108+
109+ ret = boot_read_swap_state (fa , & state );
110+ if (ret != 0 ) {
111+ LOG_ERR ("Cannot read swap state for slot %s" , img_set );
112+ } else if (state .image_ok == BOOT_FLAG_SET ) {
113+ confirmed = true;
114+ } else {
115+ confirmed = false;
116+ }
117+
118+ flash_area_close (fa );
119+
120+ return confirmed ;
121+ }
122+
123+ static void device_healthcheck (void )
124+ {
125+ int err ;
126+ char * img_set = NULL ;
127+ enum boot_slot active_slot = active_boot_slot_get ();
128+
129+ /* Confirming only in non-degraded boot states */
130+ if (active_slot == BOOT_SLOT_PRIMARY ) {
131+ img_set = "A" ;
132+ } else if (active_slot == BOOT_SLOT_SECONDARY ) {
133+ img_set = "B" ;
134+ } else {
135+ LOG_ERR ("Cannot determine active slot for health check" );
136+ return ;
137+ }
138+
139+ if (slot_confirmed (active_slot )) {
140+ LOG_INF ("Slot %s already confirmed, no action needed" , img_set );
141+ return ;
119142 }
120143
121144 LOG_INF ("Testing image set %s..." , img_set );
@@ -139,54 +162,40 @@ static void device_healthcheck(void)
139162
140163 LOG_INF ("Confirming..." );
141164
142- if (flash_area_open (area_id , & fa ) != 0 ) {
143- LOG_ERR ("Cannot open flash area for application slot %s" , img_set );
144- return ;
145- }
146-
147- err = boot_set_next (fa , true, true);
148-
149- flash_area_close (fa );
150- if (err == 0 ) {
151- LOG_INF ("Application confirmed\n" );
152- } else {
153- LOG_ERR ("Failed to confirm application, err: %d" , err );
154- }
155-
156- if (flash_area_open (cpurad_area_id , & fa ) != 0 ) {
157- LOG_ERR ("Cannot open flash area for radio slot %s" , img_set );
158- return ;
159- }
160-
161- err = boot_set_next (fa , true, true);
162-
163- flash_area_close (fa );
165+ err = boot_request_confirm_slot (CONFIG_NCS_MCUBOOT_MANIFEST_IMAGE_INDEX , active_slot );
164166 if (err == 0 ) {
165- LOG_INF ("Radio confirmed \n" );
167+ LOG_INF ("Confirmed \n" );
166168 } else {
167- LOG_ERR ("Failed to confirm radio , err: %d" , err );
169+ LOG_ERR ("Failed to confirm, err: %d" , err );
168170 }
169171}
170172
171- static void select_slot_for_single_boot (enum ab_boot_slot slot )
173+ static void select_slot_for_single_boot (enum boot_slot slot )
172174{
173175 int err = 0 ;
174- char active_slot = (active_boot_slot_get () == SLOT_A ) ? 'A' : 'B' ;
175- enum boot_slot new_slot = BOOT_SLOT_NONE ;
176+ char active_slot = (active_boot_slot_get () == BOOT_SLOT_PRIMARY ) ? 'A' : 'B' ;
176177
177- if (slot == SLOT_A ) {
178+ #ifdef CONFIG_NRF_MCUBOOT_BOOT_REQUEST_PREFERENCE_KEEP
179+ if (slot == BOOT_SLOT_PRIMARY ) {
180+ LOG_INF ("Switching slots (%c -> A)" , active_slot );
181+ } else if (slot == BOOT_SLOT_SECONDARY ) {
182+ LOG_INF ("Switching slots (%c -> B)" , active_slot );
183+ } else {
184+ LOG_ERR ("Cannot determine active slot, cannot toggle" );
185+ return ;
186+ }
187+ #else
188+ if (slot == BOOT_SLOT_PRIMARY ) {
178189 LOG_INF ("Temporarily switching slots (%c -> A)" , active_slot );
179- new_slot = BOOT_SLOT_PRIMARY ;
180- } else if (slot == SLOT_B ) {
190+ } else if (slot == BOOT_SLOT_SECONDARY ) {
181191 LOG_INF ("Temporarily switching slots (%c -> B)" , active_slot );
182- new_slot = BOOT_SLOT_SECONDARY ;
183192 } else {
184193 LOG_ERR ("Cannot determine active slot, cannot toggle" );
185194 return ;
186195 }
196+ #endif
187197
188- err = boot_request_set_preferred_slot (ACTIVE_IMAGE , new_slot );
189-
198+ err = boot_request_set_preferred_slot (CONFIG_NCS_MCUBOOT_MANIFEST_IMAGE_INDEX , slot );
190199 if (err == 0 ) {
191200 LOG_INF ("Slot toggled, restart the device to enforce" );
192201 } else {
@@ -196,11 +205,11 @@ static void select_slot_for_single_boot(enum ab_boot_slot slot)
196205
197206static void boot_state_report (void )
198207{
199- enum ab_boot_slot active_slot = active_boot_slot_get ();
208+ enum boot_slot active_slot = active_boot_slot_get ();
200209
201- if (active_slot == SLOT_A ) {
210+ if (active_slot == BOOT_SLOT_PRIMARY ) {
202211 LOG_INF ("Booted from slot A" );
203- } else if (active_slot == SLOT_B ) {
212+ } else if (active_slot == BOOT_SLOT_SECONDARY ) {
204213 LOG_INF ("Booted from slot B" );
205214 } else {
206215 LOG_INF ("Cannot determine active slot" );
@@ -210,9 +219,9 @@ static void boot_state_report(void)
210219static void button_handler (uint32_t button_state , uint32_t has_changed )
211220{
212221 if ((has_changed & DK_BTN1_MSK ) && (button_state & DK_BTN1_MSK )) {
213- select_slot_for_single_boot (SLOT_A );
222+ select_slot_for_single_boot (BOOT_SLOT_PRIMARY );
214223 } else if ((has_changed & DK_BTN2_MSK ) && (button_state & DK_BTN2_MSK )) {
215- select_slot_for_single_boot (SLOT_B );
224+ select_slot_for_single_boot (BOOT_SLOT_SECONDARY );
216225 }
217226}
218227
@@ -221,11 +230,11 @@ struct k_thread status_leds_thread_data;
221230static void status_leds_thread_entry_point (void * p1 , void * p2 , void * p3 )
222231{
223232 int blinking_led = DK_LED1 ;
224- enum ab_boot_slot active_slot = active_boot_slot_get ();
233+ enum boot_slot active_slot = active_boot_slot_get ();
225234
226- if (active_slot == SLOT_A ) {
235+ if (active_slot == BOOT_SLOT_PRIMARY ) {
227236 blinking_led = DK_LED1 ;
228- } else if (active_slot == SLOT_B ) {
237+ } else if (active_slot == BOOT_SLOT_SECONDARY ) {
229238 blinking_led = DK_LED2 ;
230239 } else {
231240 return ;
0 commit comments