forked from nrfconnect/sdk-mcuboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.c
3620 lines (3187 loc) · 114 KB
/
loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2016-2020 Linaro LTD
* Copyright (c) 2016-2019 JUUL Labs
* Copyright (c) 2019-2023 Arm Limited
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* Original license:
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* This file provides an interface to the boot loader. Functions defined in
* this file should only be called while the boot loader is running.
*/
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "bootutil/bootutil.h"
#include "bootutil/bootutil_public.h"
#include "bootutil/image.h"
#include "bootutil_priv.h"
#include "swap_priv.h"
#include "bootutil/bootutil_log.h"
#include "bootutil/security_cnt.h"
#include "bootutil/boot_record.h"
#include "bootutil/fault_injection_hardening.h"
#include "bootutil/ramload.h"
#include "bootutil/boot_hooks.h"
#include "bootutil/mcuboot_status.h"
#ifdef __ZEPHYR__
#include <zephyr/sys/reboot.h>
#endif
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
#include <dfu/pcd.h>
#ifdef CONFIG_PCD_READ_NETCORE_APP_VERSION
#include <fw_info_bare.h>
int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr);
#endif
#endif
#ifdef MCUBOOT_ENC_IMAGES
#include "bootutil/enc_key.h"
#endif
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
#include <os/os_malloc.h>
#endif
#include "mcuboot_config/mcuboot_config.h"
BOOT_LOG_MODULE_DECLARE(mcuboot);
static struct boot_loader_state boot_data;
#ifdef PM_S1_ADDRESS
static bool owner_nsib[BOOT_IMAGE_NUMBER] = {false};
#endif
#if (BOOT_IMAGE_NUMBER > 1)
#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
#else
#define IMAGES_ITER(x)
#endif
/*
* This macro allows some control on the allocation of local variables.
* When running natively on a target, we don't want to allocated huge
* variables on the stack, so make them global instead. For the simulator
* we want to run as many threads as there are tests, and it's safer
* to just make those variables stack allocated.
*/
#if !defined(__BOOTSIM__)
#define TARGET_STATIC static
#else
#define TARGET_STATIC
#endif
#if BOOT_MAX_ALIGN > 1024
#define BUF_SZ BOOT_MAX_ALIGN
#else
#define BUF_SZ 1024
#endif
#define NO_ACTIVE_SLOT UINT32_MAX
static int
boot_read_image_headers(struct boot_loader_state *state, bool require_all,
struct boot_status *bs)
{
int rc;
int i;
for (i = 0; i < BOOT_NUM_SLOTS; i++) {
rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
if (rc == BOOT_HOOK_REGULAR)
{
rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
}
if (rc != 0) {
/* If `require_all` is set, fail on any single fail, otherwise
* if at least the first slot's header was read successfully,
* then the boot loader can attempt a boot.
*
* Failure to read any headers is a fatal error.
*/
#ifdef PM_S1_ADDRESS
/* Patch needed for NCS. The primary slot of the second image
* (image 1) will not contain a valid image header until an upgrade
* of mcuboot has happened (filling S1 with the new version).
*/
if (BOOT_CURR_IMG(state) == 1 && i == 0) {
continue;
}
#endif /* PM_S1_ADDRESS */
if (i > 0 && !require_all) {
return 0;
} else {
return rc;
}
}
}
return 0;
}
/**
* Saves boot status and shared data for current image.
*
* @param state Boot loader status information.
* @param active_slot Index of the slot will be loaded for current image.
*
* @return 0 on success; nonzero on failure.
*/
static int
boot_add_shared_data(struct boot_loader_state *state,
uint8_t active_slot)
{
#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
int rc;
#ifdef MCUBOOT_DATA_SHARING
int max_app_size;
#endif
#ifdef MCUBOOT_MEASURED_BOOT
rc = boot_save_boot_status(BOOT_CURR_IMG(state),
boot_img_hdr(state, active_slot),
BOOT_IMG_AREA(state, active_slot));
if (rc != 0) {
BOOT_LOG_ERR("Failed to add image data to shared area");
return rc;
}
#endif /* MCUBOOT_MEASURED_BOOT */
#ifdef MCUBOOT_DATA_SHARING
max_app_size = app_max_size(state);
rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
BOOT_IMG_AREA(state, active_slot),
active_slot, max_app_size);
if (rc != 0) {
BOOT_LOG_ERR("Failed to add data to shared memory area.");
return rc;
}
#endif /* MCUBOOT_DATA_SHARING */
return 0;
#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
(void) (state);
(void) (active_slot);
return 0;
#endif
}
/**
* Fills rsp to indicate how booting should occur.
*
* @param state Boot loader status information.
* @param rsp boot_rsp struct to fill.
*/
static void
fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
{
uint32_t active_slot;
#if (BOOT_IMAGE_NUMBER > 1)
/* Always boot from the first enabled image. */
BOOT_CURR_IMG(state) = 0;
IMAGES_ITER(BOOT_CURR_IMG(state)) {
if (!state->img_mask[BOOT_CURR_IMG(state)]) {
break;
}
}
/* At least one image must be active, otherwise skip the execution */
if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
return;
}
#endif
#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
#else
active_slot = BOOT_PRIMARY_SLOT;
#endif
rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
rsp->br_image_off = boot_img_slot_off(state, active_slot);
rsp->br_hdr = boot_img_hdr(state, active_slot);
}
/**
* Closes all flash areas.
*
* @param state Boot loader status information.
*/
static void
close_all_flash_areas(struct boot_loader_state *state)
{
uint32_t slot;
IMAGES_ITER(BOOT_CURR_IMG(state)) {
#if BOOT_IMAGE_NUMBER > 1
if (state->img_mask[BOOT_CURR_IMG(state)]) {
continue;
}
#endif
#if MCUBOOT_SWAP_USING_SCRATCH
flash_area_close(BOOT_SCRATCH_AREA(state));
#endif
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
}
}
}
#if (BOOT_IMAGE_NUMBER > 1) || \
defined(MCUBOOT_DIRECT_XIP) || \
defined(MCUBOOT_RAM_LOAD) || \
defined(MCUBOOT_DOWNGRADE_PREVENTION)
/**
* Compare image version numbers
*
* By default, the comparison does not take build number into account.
* Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
*
* @param ver1 Pointer to the first image version to compare.
* @param ver2 Pointer to the second image version to compare.
*
* @retval -1 If ver1 is less than ver2.
* @retval 0 If the image version numbers are equal.
* @retval 1 If ver1 is greater than ver2.
*/
static int
boot_version_cmp(const struct image_version *ver1,
const struct image_version *ver2)
{
if (ver1->iv_major > ver2->iv_major) {
return 1;
}
if (ver1->iv_major < ver2->iv_major) {
return -1;
}
/* The major version numbers are equal, continue comparison. */
if (ver1->iv_minor > ver2->iv_minor) {
return 1;
}
if (ver1->iv_minor < ver2->iv_minor) {
return -1;
}
/* The minor version numbers are equal, continue comparison. */
if (ver1->iv_revision > ver2->iv_revision) {
return 1;
}
if (ver1->iv_revision < ver2->iv_revision) {
return -1;
}
#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
/* The revisions are equal, continue comparison. */
if (ver1->iv_build_num > ver2->iv_build_num) {
return 1;
}
if (ver1->iv_build_num < ver2->iv_build_num) {
return -1;
}
#endif
return 0;
}
#endif
#if (BOOT_IMAGE_NUMBER > 1)
static int
boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot);
/**
* Check the image dependency whether it is satisfied and modify
* the swap type if necessary.
*
* @param dep Image dependency which has to be verified.
*
* @return 0 on success; nonzero on failure.
*/
static int
boot_verify_slot_dependency(struct boot_loader_state *state,
struct image_dependency *dep)
{
struct image_version *dep_version;
size_t dep_slot;
int rc;
/* Determine the source of the image which is the subject of
* the dependency and get it's version. */
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
uint8_t swap_type = state->swap_type[dep->image_id];
dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
: BOOT_PRIMARY_SLOT;
#else
dep_slot = state->slot_usage[dep->image_id].active_slot;
#endif
dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
rc = boot_version_cmp(dep_version, &dep->image_min_version);
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
if (rc < 0) {
/* Dependency not satisfied.
* Modify the swap type to decrease the version number of the image
* (which will be located in the primary slot after the boot process),
* consequently the number of unsatisfied dependencies will be
* decreased or remain the same.
*/
switch (BOOT_SWAP_TYPE(state)) {
case BOOT_SWAP_TYPE_TEST:
case BOOT_SWAP_TYPE_PERM:
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
break;
case BOOT_SWAP_TYPE_NONE:
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
break;
default:
break;
}
} else {
/* Dependency satisfied. */
rc = 0;
}
#else
if (rc >= 0) {
/* Dependency satisfied. */
rc = 0;
}
#endif
return rc;
}
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
/**
* Iterate over all the images and verify whether the image dependencies in the
* TLV area are all satisfied and update the related swap type if necessary.
*/
static int
boot_verify_dependencies(struct boot_loader_state *state)
{
int rc = -1;
uint8_t slot;
BOOT_CURR_IMG(state) = 0;
while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
if (state->img_mask[BOOT_CURR_IMG(state)]) {
BOOT_CURR_IMG(state)++;
continue;
}
if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
slot = BOOT_SECONDARY_SLOT;
} else {
slot = BOOT_PRIMARY_SLOT;
}
rc = boot_verify_slot_dependencies(state, slot);
if (rc == 0) {
/* All dependencies've been satisfied, continue with next image. */
BOOT_CURR_IMG(state)++;
} else if (rc == BOOT_EBADIMAGE) {
/* Cannot upgrade due to non-met dependencies, so disable all
* image upgrades.
*/
for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
BOOT_CURR_IMG(state) = idx;
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
}
break;
} else {
/* Other error happened, images are inconsistent */
return rc;
}
}
return rc;
}
#else
#if defined MCUBOOT_RAM_LOAD
static inline int
boot_remove_image_from_sram(struct boot_loader_state *state);
#endif
/**
* Checks the dependency of all the active slots. If an image found with
* invalid or not satisfied dependencies the image is removed from SRAM (in
* case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
*
* @param state Boot loader status information.
*
* @return 0 if dependencies are met; nonzero otherwise.
*/
static int
boot_verify_dependencies(struct boot_loader_state *state)
{
int rc = -1;
uint32_t active_slot;
IMAGES_ITER(BOOT_CURR_IMG(state)) {
if (state->img_mask[BOOT_CURR_IMG(state)]) {
continue;
}
active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
rc = boot_verify_slot_dependencies(state, active_slot);
if (rc != 0) {
/* Dependencies not met or invalid dependencies. */
#ifdef MCUBOOT_RAM_LOAD
boot_remove_image_from_sram(state);
#endif /* MCUBOOT_RAM_LOAD */
state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
return rc;
}
}
return rc;
}
#endif
/**
* Read all dependency TLVs of an image from the flash and verify
* one after another to see if they are all satisfied.
*
* @param slot Image slot number.
*
* @return 0 on success; nonzero on failure.
*/
static int
boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
{
const struct flash_area *fap;
struct image_tlv_iter it;
struct image_dependency dep;
uint32_t off;
uint16_t len;
int area_id;
int rc;
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
}
rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
IMAGE_TLV_DEPENDENCY, true);
if (rc != 0) {
goto done;
}
while (true) {
rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
if (rc < 0) {
return -1;
} else if (rc > 0) {
rc = 0;
break;
}
if (len != sizeof(dep)) {
rc = BOOT_EBADIMAGE;
goto done;
}
rc = LOAD_IMAGE_DATA(boot_img_hdr(state, slot),
fap, off, &dep, len);
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
}
if (dep.image_id >= BOOT_IMAGE_NUMBER) {
rc = BOOT_EBADARGS;
goto done;
}
/* Verify dependency and modify the swap type if not satisfied. */
rc = boot_verify_slot_dependency(state, &dep);
if (rc != 0) {
/* Dependency not satisfied */
goto done;
}
}
done:
flash_area_close(fap);
return rc;
}
#endif /* (BOOT_IMAGE_NUMBER > 1) */
#if !defined(MCUBOOT_DIRECT_XIP)
/*
* Compute the total size of the given image. Includes the size of
* the TLVs.
*/
#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
static int
boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
{
const struct flash_area *fap;
struct image_tlv_info info;
uint32_t off;
uint32_t protect_tlv_size;
int area_id;
int rc;
#if (BOOT_IMAGE_NUMBER == 1)
(void)state;
#endif
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
}
off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
if (flash_area_read(fap, off, &info, sizeof(info))) {
rc = BOOT_EFLASH;
goto done;
}
protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
if (protect_tlv_size != info.it_tlv_tot) {
rc = BOOT_EBADIMAGE;
goto done;
}
if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
rc = BOOT_EFLASH;
goto done;
}
} else if (protect_tlv_size != 0) {
rc = BOOT_EBADIMAGE;
goto done;
}
if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
rc = BOOT_EBADIMAGE;
goto done;
}
*size = off + protect_tlv_size + info.it_tlv_tot;
rc = 0;
done:
flash_area_close(fap);
return rc;
}
#endif /* !MCUBOOT_OVERWRITE_ONLY */
#if !defined(MCUBOOT_RAM_LOAD)
static uint32_t
boot_write_sz(struct boot_loader_state *state)
{
uint32_t elem_sz;
#if MCUBOOT_SWAP_USING_SCRATCH
uint32_t align;
#endif
/* Figure out what size to write update status update as. The size depends
* on what the minimum write size is for scratch area, active image slot.
* We need to use the bigger of those 2 values.
*/
elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
#if MCUBOOT_SWAP_USING_SCRATCH
align = flash_area_align(BOOT_SCRATCH_AREA(state));
if (align > elem_sz) {
elem_sz = align;
}
#endif
return elem_sz;
}
static int
boot_initialize_area(struct boot_loader_state *state, int flash_area)
{
uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
boot_sector_t *out_sectors;
uint32_t *out_num_sectors;
int rc;
num_sectors = BOOT_MAX_IMG_SECTORS;
if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
} else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
#if MCUBOOT_SWAP_USING_SCRATCH
} else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
out_sectors = state->scratch.sectors;
out_num_sectors = &state->scratch.num_sectors;
#endif
} else {
return BOOT_EFLASH;
}
#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
#else
_Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
if (rc != 0) {
return rc;
}
*out_num_sectors = num_sectors;
return 0;
}
/**
* Determines the sector layout of both image slots and the scratch area.
* This information is necessary for calculating the number of bytes to erase
* and copy during an image swap. The information collected during this
* function is used to populate the state.
*/
static int
boot_read_sectors(struct boot_loader_state *state)
{
uint8_t image_index;
int rc;
image_index = BOOT_CURR_IMG(state);
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
if (rc != 0) {
return BOOT_EFLASH;
}
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
if (rc != 0) {
/* We need to differentiate from the primary image issue */
return BOOT_EFLASH_SEC;
}
#if MCUBOOT_SWAP_USING_SCRATCH
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
if (rc != 0) {
return BOOT_EFLASH;
}
#endif
BOOT_WRITE_SZ(state) = boot_write_sz(state);
return 0;
}
void
boot_status_reset(struct boot_status *bs)
{
#ifdef MCUBOOT_ENC_IMAGES
memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
#if MCUBOOT_SWAP_SAVE_ENCTLV
memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
#endif
#endif /* MCUBOOT_ENC_IMAGES */
bs->use_scratch = 0;
bs->swap_size = 0;
bs->source = 0;
bs->op = BOOT_STATUS_OP_MOVE;
bs->idx = BOOT_STATUS_IDX_0;
bs->state = BOOT_STATUS_STATE_0;
bs->swap_type = BOOT_SWAP_TYPE_NONE;
}
bool
boot_status_is_reset(const struct boot_status *bs)
{
return (bs->op == BOOT_STATUS_OP_MOVE &&
bs->idx == BOOT_STATUS_IDX_0 &&
bs->state == BOOT_STATUS_STATE_0);
}
/**
* Writes the supplied boot status to the flash file system. The boot status
* contains the current state of an in-progress image copy operation.
*
* @param bs The boot status to write.
*
* @return 0 on success; nonzero on failure.
*/
int
boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
{
const struct flash_area *fap;
uint32_t off;
int area_id;
int rc = 0;
uint8_t buf[BOOT_MAX_ALIGN];
uint32_t align;
uint8_t erased_val;
/* NOTE: The first sector copied (that is the last sector on slot) contains
* the trailer. Since in the last step the primary slot is erased, the
* first two status writes go to the scratch which will be copied to
* the primary slot!
*/
#if MCUBOOT_SWAP_USING_SCRATCH
if (bs->use_scratch) {
/* Write to scratch. */
area_id = FLASH_AREA_IMAGE_SCRATCH;
} else {
#endif
/* Write to the primary slot. */
area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
#if MCUBOOT_SWAP_USING_SCRATCH
}
#endif
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
return BOOT_EFLASH;
}
off = boot_status_off(fap) +
boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
align = flash_area_align(fap);
erased_val = flash_area_erased_val(fap);
memset(buf, erased_val, BOOT_MAX_ALIGN);
buf[0] = bs->state;
BOOT_LOG_DBG("writing swap status; fa_id=%d off=0x%lx (0x%lx)",
flash_area_get_id(fap), (unsigned long)off,
(unsigned long)flash_area_get_off(fap) + off);
rc = flash_area_write(fap, off, buf, align);
if (rc != 0) {
rc = BOOT_EFLASH;
}
flash_area_close(fap);
return rc;
}
#endif /* !MCUBOOT_RAM_LOAD */
#endif /* !MCUBOOT_DIRECT_XIP */
/*
* Validate image hash/signature and optionally the security counter in a slot.
*/
static fih_ret
boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
const struct flash_area *fap, struct boot_status *bs)
{
TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
uint8_t image_index;
int rc;
FIH_DECLARE(fih_rc, FIH_FAILURE);
#if (BOOT_IMAGE_NUMBER == 1)
(void)state;
#endif
(void)bs;
(void)rc;
image_index = BOOT_CURR_IMG(state);
/* In the case of ram loading the image has already been decrypted as it is
* decrypted when copied in ram */
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
if (MUST_DECRYPT(fap, image_index, hdr)) {
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
if (rc < 0) {
FIH_RET(fih_rc);
}
if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
FIH_RET(fih_rc);
}
}
#endif
FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
FIH_RET(fih_rc);
}
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
static fih_ret
split_image_check(struct image_header *app_hdr,
const struct flash_area *app_fap,
struct image_header *loader_hdr,
const struct flash_area *loader_fap)
{
static void *tmpbuf;
uint8_t loader_hash[32];
FIH_DECLARE(fih_rc, FIH_FAILURE);
if (!tmpbuf) {
tmpbuf = malloc(BOOT_TMPBUF_SZ);
if (!tmpbuf) {
goto out;
}
}
FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
FIH_RET(fih_rc);
}
FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
out:
FIH_RET(fih_rc);
}
#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
/*
* Check that this is a valid header. Valid means that the magic is
* correct, and that the sizes/offsets are "sane". Sane means that
* there is no overflow on the arithmetic, and that the result fits
* within the flash area we are in.
*/
static bool
boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
{
uint32_t size;
if (hdr->ih_magic != IMAGE_MAGIC) {
return false;
}
if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
return false;
}
if (size >= flash_area_get_size(fap)) {
return false;
}
return true;
}
/*
* Check that a memory area consists of a given value.
*/
static inline bool
boot_data_is_set_to(uint8_t val, void *data, size_t len)
{
uint8_t i;
uint8_t *p = (uint8_t *)data;
for (i = 0; i < len; i++) {
if (val != p[i]) {
return false;
}
}
return true;
}
static int
boot_check_header_erased(struct boot_loader_state *state, int slot)
{
const struct flash_area *fap;
struct image_header *hdr;
uint8_t erased_val;
int area_id;
int rc;
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
return -1;
}
erased_val = flash_area_erased_val(fap);
flash_area_close(fap);
hdr = boot_img_hdr(state, slot);
if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
return -1;
}
return 0;
}
#if defined(MCUBOOT_DIRECT_XIP)
/**
* Check if image in slot has been set with specific ROM address to run from
* and whether the slot starts at that address.
*
* @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
* 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
* header matches the slot address;
* 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
* does not match the slot address.
*/
static bool
boot_rom_address_check(struct boot_loader_state *state)
{
uint32_t active_slot;
const struct image_header *hdr;
uint32_t f_off;
active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
hdr = boot_img_hdr(state, active_slot);
f_off = boot_img_slot_off(state, active_slot);
if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
", skipping",
active_slot == 0 ? "primary" : "secondary", f_off,
hdr->ih_load_addr);
/* If there is address mismatch, the image is not bootable from this
* slot.
*/
return 1;
}
return 0;
}
#endif
/*
* Check that there is a valid image in a slot
*
* @returns
* FIH_SUCCESS if image was successfully validated
* FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
* FIH_FAILURE on any errors
*/
static fih_ret
boot_validate_slot(struct boot_loader_state *state, int slot,
struct boot_status *bs)
{
const struct flash_area *fap;
struct image_header *hdr;
int area_id;
FIH_DECLARE(fih_rc, FIH_FAILURE);
int rc;
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap);