-
Notifications
You must be signed in to change notification settings - Fork 709
/
Copy pathboot_serial.c
1652 lines (1436 loc) · 49 KB
/
boot_serial.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
/*
* 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.
*/
#include <assert.h>
#include <stddef.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include "sysflash/sysflash.h"
#include "bootutil/bootutil_log.h"
#ifdef __ZEPHYR__
#include <zephyr/sys/reboot.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/crc.h>
#include <zephyr/sys/base64.h>
#include <hal/hal_flash.h>
#elif __ESPRESSIF__
#include <bootloader_utility.h>
#include <esp_rom_sys.h>
#include <esp_crc.h>
#include <endian.h>
#include <mbedtls/base64.h>
#else
#include <bsp/bsp.h>
#include <hal/hal_system.h>
#include <hal/hal_flash.h>
#include <os/endian.h>
#include <os/os_cputime.h>
#include <crc/crc16.h>
#include <base64/base64.h>
#endif /* __ZEPHYR__ */
#include <zcbor_decode.h>
#include <zcbor_encode.h>
#include "zcbor_bulk.h"
#include <flash_map_backend/flash_map_backend.h>
#include <os/os.h>
#include <os/os_malloc.h>
#include <bootutil/image.h>
#include <bootutil/bootutil.h>
#include "boot_serial/boot_serial.h"
#include "boot_serial_priv.h"
#include "mcuboot_config/mcuboot_config.h"
#include "../src/bootutil_priv.h"
#ifdef MCUBOOT_ENC_IMAGES
#include "boot_serial/boot_serial_encryption.h"
#endif
#include "bootutil/boot_hooks.h"
BOOT_LOG_MODULE_DECLARE(mcuboot);
#ifndef ARRAY_SIZE
#define ARRAY_SIZE ZCBOR_ARRAY_SIZE
#endif
#if defined(MCUBOOT_SHA512)
#define IMAGE_HASH_SIZE (64)
#define IMAGE_SHA_TLV IMAGE_TLV_SHA512
#elif defined(MCUBOOT_SIGN_EC384)
#define IMAGE_HASH_SIZE (48)
#define IMAGE_SHA_TLV IMAGE_TLV_SHA384
#else
#define IMAGE_HASH_SIZE (32)
#define IMAGE_SHA_TLV IMAGE_TLV_SHA256
#endif
#ifndef MCUBOOT_SERIAL_MAX_RECEIVE_SIZE
#define MCUBOOT_SERIAL_MAX_RECEIVE_SIZE 512
#endif
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
#define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 48
#else
#define BOOT_SERIAL_IMAGE_STATE_SIZE_MAX 0
#endif
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
#define BOOT_SERIAL_HASH_SIZE_MAX (IMAGE_HASH_SIZE + 4)
#else
#define BOOT_SERIAL_HASH_SIZE_MAX 0
#endif
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 164
#else
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 0
#endif
#if (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + BOOT_SERIAL_HASH_SIZE_MAX) > \
BOOT_SERIAL_SLOT_INFO_SIZE_MAX
#define BOOT_SERIAL_MAX_MESSAGE_SIZE (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
BOOT_SERIAL_HASH_SIZE_MAX)
#else
#define BOOT_SERIAL_MAX_MESSAGE_SIZE BOOT_SERIAL_SLOT_INFO_SIZE_MAX
#endif
#define BOOT_SERIAL_OUT_MAX (BOOT_SERIAL_MAX_MESSAGE_SIZE * BOOT_IMAGE_NUMBER)
#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */
/* Number of estimated CBOR elements for responses */
#define CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP 4
#define CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP 3
#ifdef __ZEPHYR__
/* base64 lib encodes data to null-terminated string */
#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
#define CRC_CITT_POLYMINAL 0x1021
#define ntohs(x) sys_be16_to_cpu(x)
#define htons(x) sys_cpu_to_be16(x)
#elif __ESPRESSIF__
#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
#define ntohs(x) be16toh(x)
#define htons(x) htobe16(x)
#define base64_decode mbedtls_base64_decode
#define base64_encode mbedtls_base64_encode
#endif
#if (BOOT_IMAGE_NUMBER > 1)
#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
#else
#define IMAGES_ITER(x)
#endif
#define SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN 1
#define BOOT_DIRECT_UPLOAD_SECONDARY_SLOT_ID_REMAINDER 0
static char in_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1];
static char dec_buf[MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1];
const struct boot_uart_funcs *boot_uf;
static struct nmgr_hdr *bs_hdr;
static bool bs_entry;
static char bs_obuf[BOOT_SERIAL_OUT_MAX];
static void boot_serial_output(void);
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
#ifdef MCUBOOT_SWAP_USING_OFFSET
static int boot_serial_get_hash(const struct image_header *hdr,
const struct flash_area *fap, uint8_t *hash, uint32_t start_off);
#else
static int boot_serial_get_hash(const struct image_header *hdr,
const struct flash_area *fap, uint8_t *hash);
#endif
#endif
static zcbor_state_t cbor_state[2];
void reset_cbor_state(void)
{
zcbor_new_encode_state(cbor_state, 2, (uint8_t *)bs_obuf,
sizeof(bs_obuf), 0);
}
/**
* Function that processes MGMT_GROUP_ID_PERUSER mcumgr group and may be
* used to process any groups that have not been processed by generic boot
* serial implementation.
*
* @param[in] hdr -- the decoded header of mcumgr message;
* @param[in] buffer -- buffer with first mcumgr message;
* @param[in] len -- length of of data in buffer;
* @param[out] *cs -- object with encoded response.
*
* @return 0 on success; non-0 error code otherwise.
*/
extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
const char *buffer,
int len, zcbor_state_t *cs);
#define zcbor_tstr_put_lit_cast(state, string) \
zcbor_tstr_encode_ptr(state, (char *)string, sizeof(string) - 1)
#ifndef MCUBOOT_USE_SNPRINTF
/*
* Convert version into string without use of snprintf().
*/
static int
u32toa(char *tgt, uint32_t val)
{
char *dst;
uint32_t d = 1;
uint32_t dgt;
int n = 0;
dst = tgt;
while (val / d >= 10) {
d *= 10;
}
while (d) {
dgt = val / d;
val %= d;
d /= 10;
if (n || dgt > 0 || d == 0) {
*dst++ = dgt + '0';
++n;
}
}
*dst = '\0';
return dst - tgt;
}
/*
* dst has to be able to fit "255.255.65535.4294967295" (25 characters).
*/
static void
bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
{
int off;
off = u32toa(dst, ver->iv_major);
dst[off++] = '.';
off += u32toa(dst + off, ver->iv_minor);
dst[off++] = '.';
off += u32toa(dst + off, ver->iv_revision);
if (ver->iv_build_num != 0) {
dst[off++] = '.';
off += u32toa(dst + off, ver->iv_build_num);
}
}
#else
/*
* dst has to be able to fit "255.255.65535.4294967295" (25 characters).
*/
static void
bs_list_img_ver(char *dst, int maxlen, struct image_version *ver)
{
int len;
len = snprintf(dst, maxlen, "%hu.%hu.%hu", (uint16_t)ver->iv_major,
(uint16_t)ver->iv_minor, ver->iv_revision);
if (ver->iv_build_num != 0 && len > 0 && len < maxlen) {
snprintf(&dst[len], (maxlen - len), ".%u", ver->iv_build_num);
}
}
#endif /* !MCUBOOT_USE_SNPRINTF */
/*
* List images.
*/
static void
bs_list(char *buf, int len)
{
struct image_header hdr;
uint32_t slot, area_id;
const struct flash_area *fap;
uint8_t image_index;
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
uint8_t hash[IMAGE_HASH_SIZE];
#endif
zcbor_map_start_encode(cbor_state, 1);
zcbor_tstr_put_lit_cast(cbor_state, "images");
zcbor_list_start_encode(cbor_state, 5);
image_index = 0;
IMAGES_ITER(image_index) {
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
int swap_status = boot_swap_type_multi(image_index);
#endif
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
FIH_DECLARE(fih_rc, FIH_FAILURE);
int rc;
uint8_t tmpbuf[64];
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
bool active = false;
bool confirmed = false;
bool pending = false;
bool permanent = false;
#endif
#ifdef MCUBOOT_SWAP_USING_OFFSET
uint32_t start_off = 0;
#endif
area_id = flash_area_id_from_multi_image_slot(image_index, slot);
if (flash_area_open(area_id, &fap)) {
continue;
}
#ifdef MCUBOOT_SWAP_USING_OFFSET
if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT) {
uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN;
struct flash_sector sector_data;
rc = flash_area_sectors(fap, &num_sectors, §or_data);
if ((rc != 0 && rc != -ENOMEM) ||
num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN) {
flash_area_close(fap);
continue;
}
start_off = sector_data.fs_size;
}
#endif
rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
BOOT_HOOK_REGULAR, image_index, slot, &hdr);
if (rc == BOOT_HOOK_REGULAR)
{
#ifdef MCUBOOT_SWAP_USING_OFFSET
flash_area_read(fap, start_off, &hdr, sizeof(hdr));
#else
flash_area_read(fap, 0, &hdr, sizeof(hdr));
#endif
}
if (hdr.ih_magic == IMAGE_MAGIC)
{
BOOT_HOOK_CALL_FIH(boot_image_check_hook,
FIH_BOOT_HOOK_REGULAR,
fih_rc, image_index, slot);
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
{
#if defined(MCUBOOT_ENC_IMAGES)
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
if (IS_ENCRYPTED(&hdr) && MUST_DECRYPT(fap, image_index, &hdr)) {
#ifdef MCUBOOT_SWAP_USING_OFFSET
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
#else
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
&hdr, tmpbuf, sizeof(tmpbuf));
#endif
} else {
#endif
if (IS_ENCRYPTED(&hdr)) {
/*
* There is an image present which has an encrypted flag set but is
* not encrypted, therefore remove the flag from the header and run a
* normal image validation on it.
*/
hdr.ih_flags &= ~ENCRYPTIONFLAGS;
}
#endif
#ifdef MCUBOOT_SWAP_USING_OFFSET
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
#else
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
#endif
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
}
#endif
}
}
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
flash_area_close(fap);
continue;
}
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
/* Retrieve hash of image for identification */
#ifdef MCUBOOT_SWAP_USING_OFFSET
rc = boot_serial_get_hash(&hdr, fap, hash, start_off);
#else
rc = boot_serial_get_hash(&hdr, fap, hash);
#endif
#endif
flash_area_close(fap);
zcbor_map_start_encode(cbor_state, 20);
#if (BOOT_IMAGE_NUMBER > 1)
zcbor_tstr_put_lit_cast(cbor_state, "image");
zcbor_uint32_put(cbor_state, image_index);
#endif
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
if (swap_status == BOOT_SWAP_TYPE_NONE) {
if (slot == BOOT_PRIMARY_SLOT) {
confirmed = true;
active = true;
}
} else if (swap_status == BOOT_SWAP_TYPE_TEST) {
if (slot == BOOT_PRIMARY_SLOT) {
confirmed = true;
} else {
pending = true;
}
} else if (swap_status == BOOT_SWAP_TYPE_PERM) {
if (slot == BOOT_PRIMARY_SLOT) {
confirmed = true;
} else {
pending = true;
permanent = true;
}
} else if (swap_status == BOOT_SWAP_TYPE_REVERT) {
if (slot == BOOT_PRIMARY_SLOT) {
active = true;
} else {
confirmed = true;
}
}
if (!(hdr.ih_flags & IMAGE_F_NON_BOOTABLE)) {
zcbor_tstr_put_lit_cast(cbor_state, "bootable");
zcbor_bool_put(cbor_state, true);
}
if (confirmed) {
zcbor_tstr_put_lit_cast(cbor_state, "confirmed");
zcbor_bool_put(cbor_state, true);
}
if (active) {
zcbor_tstr_put_lit_cast(cbor_state, "active");
zcbor_bool_put(cbor_state, true);
}
if (pending) {
zcbor_tstr_put_lit_cast(cbor_state, "pending");
zcbor_bool_put(cbor_state, true);
}
if (permanent) {
zcbor_tstr_put_lit_cast(cbor_state, "permanent");
zcbor_bool_put(cbor_state, true);
}
#endif
zcbor_tstr_put_lit_cast(cbor_state, "slot");
zcbor_uint32_put(cbor_state, slot);
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
if (rc == 0) {
zcbor_tstr_put_lit_cast(cbor_state, "hash");
zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash));
}
#endif
zcbor_tstr_put_lit_cast(cbor_state, "version");
bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
zcbor_tstr_encode_ptr(cbor_state, (char *)tmpbuf, strlen((char *)tmpbuf));
zcbor_map_end_encode(cbor_state, 20);
}
}
zcbor_list_end_encode(cbor_state, 5);
zcbor_map_end_encode(cbor_state, 1);
boot_serial_output();
}
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
/*
* Set image state.
*/
static void
bs_set(char *buf, int len)
{
/*
* Expected data format.
* {
* "confirm":<true for confirm, false for test>
* "hash":<hash of image (OPTIONAL for single image only)>
* }
*/
uint8_t image_index = 0;
size_t decoded = 0;
uint8_t hash[IMAGE_HASH_SIZE];
bool confirm = false;
struct zcbor_string img_hash = { 0 };
bool ok;
int rc;
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
bool found = false;
#endif
zcbor_state_t zsd[4];
zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
struct zcbor_map_decode_key_val image_set_state_decode[] = {
ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm),
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
ZCBOR_MAP_DECODE_KEY_DECODER("hash", zcbor_bstr_decode, &img_hash),
#endif
};
ok = zcbor_map_decode_bulk(zsd, image_set_state_decode, ARRAY_SIZE(image_set_state_decode),
&decoded) == 0;
if (!ok) {
rc = MGMT_ERR_EINVAL;
goto out;
}
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
if ((img_hash.len != sizeof(hash) && img_hash.len != 0) ||
(img_hash.len == 0 && BOOT_IMAGE_NUMBER > 1)) {
/* Hash is required and was not provided or is invalid size */
rc = MGMT_ERR_EINVAL;
goto out;
}
if (img_hash.len != 0) {
for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) {
struct image_header hdr;
uint32_t area_id;
const struct flash_area *fap;
uint8_t tmpbuf[64];
#ifdef MCUBOOT_SWAP_USING_OFFSET
uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN;
struct flash_sector sector_data;
uint32_t start_off = 0;
#endif
area_id = flash_area_id_from_multi_image_slot(image_index, 1);
if (flash_area_open(area_id, &fap)) {
BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
continue;
}
#ifdef MCUBOOT_SWAP_USING_OFFSET
rc = flash_area_sectors(fap, &num_sectors, §or_data);
if ((rc != 0 && rc != -ENOMEM) ||
num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN) {
flash_area_close(fap);
continue;
}
start_off = sector_data.fs_size;
#endif
rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
BOOT_HOOK_REGULAR, image_index, 1, &hdr);
if (rc == BOOT_HOOK_REGULAR)
{
#ifdef MCUBOOT_SWAP_USING_OFFSET
flash_area_read(fap, start_off, &hdr, sizeof(hdr));
#else
flash_area_read(fap, 0, &hdr, sizeof(hdr));
#endif
}
if (hdr.ih_magic == IMAGE_MAGIC)
{
FIH_DECLARE(fih_rc, FIH_FAILURE);
BOOT_HOOK_CALL_FIH(boot_image_check_hook,
FIH_BOOT_HOOK_REGULAR,
fih_rc, image_index, 1);
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
{
#ifdef MCUBOOT_ENC_IMAGES
if (IS_ENCRYPTED(&hdr)) {
#ifdef MCUBOOT_SWAP_USING_OFFSET
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
#else
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
&hdr, tmpbuf, sizeof(tmpbuf));
#endif
} else {
#endif
#ifdef MCUBOOT_SWAP_USING_OFFSET
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
#else
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
#endif
#ifdef MCUBOOT_ENC_IMAGES
}
#endif
}
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
continue;
}
}
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
/* Retrieve hash of image for identification */
#ifdef MCUBOOT_SWAP_USING_OFFSET
rc = boot_serial_get_hash(&hdr, fap, hash, start_off);
#else
rc = boot_serial_get_hash(&hdr, fap, hash);
#endif
#endif
flash_area_close(fap);
if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
/* Hash matches, set this slot for test or confirmation */
found = true;
break;
}
}
if (!found) {
/* Image was not found with specified hash */
BOOT_LOG_ERR("Did not find image with specified hash");
rc = MGMT_ERR_ENOENT;
goto out;
}
}
#endif
rc = boot_set_pending_multi(image_index, confirm);
out:
if (rc == 0) {
/* Success - return updated list of images */
bs_list(buf, len);
} else {
/* Error code, only return the error */
zcbor_map_start_encode(cbor_state, 10);
zcbor_tstr_put_lit_cast(cbor_state, "rc");
zcbor_int32_put(cbor_state, rc);
zcbor_map_end_encode(cbor_state, 10);
boot_serial_output();
}
}
#endif
/*
* Send rc code only.
*/
static void
bs_rc_rsp(int rc_code)
{
zcbor_map_start_encode(cbor_state, 10);
zcbor_tstr_put_lit_cast(cbor_state, "rc");
zcbor_int32_put(cbor_state, rc_code);
zcbor_map_end_encode(cbor_state, 10);
boot_serial_output();
}
static void
bs_list_set(uint8_t op, char *buf, int len)
{
if (op == NMGR_OP_READ) {
bs_list(buf, len);
} else {
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
bs_set(buf, len);
#else
bs_rc_rsp(MGMT_ERR_ENOTSUP);
#endif
}
}
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
static void
bs_slot_info(uint8_t op, char *buf, int len)
{
uint32_t slot, area_id;
const struct flash_area *fap;
uint8_t image_index = 0;
int rc;
bool ok = true;
const struct image_max_size *image_max_sizes;
if (op != NMGR_OP_READ) {
bs_rc_rsp(MGMT_ERR_ENOTSUP);
}
image_max_sizes = boot_get_max_app_size();
zcbor_map_start_encode(cbor_state, 1);
zcbor_tstr_put_lit_cast(cbor_state, "images");
zcbor_list_start_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);
IMAGES_ITER(image_index) {
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
if (slot == 0) {
ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP) &&
zcbor_tstr_put_lit(cbor_state, "image") &&
zcbor_uint32_put(cbor_state, (uint32_t)image_index) &&
zcbor_tstr_put_lit(cbor_state, "slots") &&
zcbor_list_start_encode(cbor_state, BOOT_NUM_SLOTS);
if (!ok) {
goto finish;
}
}
ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP) &&
zcbor_tstr_put_lit(cbor_state, "slot") &&
zcbor_uint32_put(cbor_state, slot);
if (!ok) {
goto finish;
}
area_id = flash_area_id_from_multi_image_slot(image_index, slot);
rc = flash_area_open(area_id, &fap);
if (rc) {
ok = zcbor_tstr_put_lit(cbor_state, "rc") &&
zcbor_int32_put(cbor_state, rc);
} else {
if (sizeof(fap->fa_size) == sizeof(uint64_t)) {
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
zcbor_uint64_put(cbor_state, fap->fa_size);
} else {
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
zcbor_uint32_put(cbor_state, fap->fa_size);
}
if (!ok) {
flash_area_close(fap);
goto finish;
}
/*
* Check if we support uploading to this slot and if so, return the
* image ID
*/
#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index + 1));
#elif defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index * 2 + slot + 1));
#else
if (slot == 1) {
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index * 2 + 1));
}
#endif
flash_area_close(fap);
if (!ok) {
goto finish;
}
ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP);
if (!ok) {
goto finish;
}
if (slot == (BOOT_NUM_SLOTS - 1)) {
ok = zcbor_list_end_encode(cbor_state, BOOT_NUM_SLOTS);
if (!ok) {
goto finish;
}
if (image_max_sizes[image_index].calculated == true) {
ok = zcbor_tstr_put_lit(cbor_state, "max_image_size") &&
zcbor_uint32_put(cbor_state,
image_max_sizes[image_index].max_size);
if (!ok) {
goto finish;
}
}
ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP);
}
}
if (!ok) {
goto finish;
}
}
}
ok = zcbor_list_end_encode(cbor_state, MCUBOOT_IMAGE_NUMBER) &&
zcbor_map_end_encode(cbor_state, 1);
finish:
if (!ok) {
reset_cbor_state();
bs_rc_rsp(MGMT_ERR_ENOMEM);
}
boot_serial_output();
}
#endif
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
/** Erases range of flash, aligned to sector size
*
* Function will erase all sectors withing [start, end] range; it does not check
* the @p start for alignment, and it will use @p end to find boundaries of las
* sector to erase. Function returns offset of the first byte past the last
* erased sector, so basically offset of next sector to be erased if needed.
* The function is intended to be called iteratively with previously returned
* offset as @p start.
*
* @param start starting offset, aligned to sector offset;
* @param end ending offset, maybe anywhere within sector;
*
* @retval On success: offset of the first byte past last erased sector;
* On failure: -EINVAL.
*/
static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
{
struct flash_sector sect;
size_t size;
int rc;
if (end >= flash_area_get_size(fap)) {
return -EINVAL;
}
if (end < start) {
return start;
}
if (flash_area_get_sector(fap, end, §)) {
return -EINVAL;
}
size = flash_sector_get_off(§) + flash_sector_get_size(§) - start;
BOOT_LOG_DBG("Erasing range 0x%jx:0x%jx", (intmax_t)start,
(intmax_t)(start + size - 1));
rc = flash_area_erase(fap, start, size);
if (rc != 0) {
BOOT_LOG_ERR("Error %d while erasing range", rc);
return -EINVAL;
}
return start + size;
}
#endif
/*
* Image upload request.
*/
static void
bs_upload(char *buf, int len)
{
static size_t img_size; /* Total image size, held for duration of upload */
static uint32_t curr_off; /* Expected current offset */
const uint8_t *img_chunk = NULL; /* Pointer to buffer with received image chunk */
size_t img_chunk_len = 0; /* Length of received image chunk */
size_t img_chunk_off = SIZE_MAX; /* Offset of image chunk within image */
size_t rem_bytes; /* Reminder bytes after aligning chunk write to
* to flash alignment */
uint32_t img_num_tmp = UINT_MAX; /* Temp variable for image number */
static uint32_t img_num = 0;
size_t img_size_tmp = SIZE_MAX; /* Temp variable for image size */
const struct flash_area *fap = NULL;
int rc;
struct zcbor_string img_chunk_data = { 0 };
size_t decoded = 0;
bool ok;
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
static off_t not_yet_erased = 0; /* Offset of next byte to erase; writes to flash
* are done in consecutive manner and erases are done
* to allow currently received chunk to be written;
* this state variable holds information where last
* erase has stopped to let us know whether erase
* is needed to be able to write current chunk.
*/
static struct flash_sector status_sector;
#endif
#ifdef MCUBOOT_SWAP_USING_OFFSET
static uint32_t start_off = 0;
#endif
zcbor_state_t zsd[4];
zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
struct zcbor_map_decode_key_val image_upload_decode[] = {
ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp),
ZCBOR_MAP_DECODE_KEY_DECODER("data", zcbor_bstr_decode, &img_chunk_data),
ZCBOR_MAP_DECODE_KEY_DECODER("len", zcbor_size_decode, &img_size_tmp),
ZCBOR_MAP_DECODE_KEY_DECODER("off", zcbor_size_decode, &img_chunk_off),
};
ok = zcbor_map_decode_bulk(zsd, image_upload_decode, ARRAY_SIZE(image_upload_decode),
&decoded) == 0;
if (!ok) {
goto out_invalid_data;
}
img_chunk = img_chunk_data.value;
img_chunk_len = img_chunk_data.len;
/*
* Expected data format.
* {
* "image":<image number in a multi-image set (OPTIONAL)>
* "data":<image data>
* "len":<image len>
* "off":<current offset of image data>
* }
*/
if (img_chunk_off == SIZE_MAX || img_chunk == NULL) {
/*
* Offset must be set in every block.
*/
goto out_invalid_data;
}
/* Use image number only from packet with offset == 0. */
if (img_chunk_off == 0) {
if (img_num_tmp != UINT_MAX) {
img_num = img_num_tmp;
} else {
img_num = 0;
}
}
#if !defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
rc = flash_area_open(flash_area_id_from_multi_image_slot(img_num, 0), &fap);
#else
rc = flash_area_open(flash_area_id_from_direct_image(img_num), &fap);
#endif
if (rc) {
rc = MGMT_ERR_EINVAL;
goto out;
}
if (img_chunk_off == 0) {
/* Receiving chunk with 0 offset resets the upload state; this basically
* means that upload has started from beginning.
*/
const size_t area_size = flash_area_get_size(fap);
#ifdef MCUBOOT_SWAP_USING_OFFSET
uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN;
struct flash_sector sector_data;
#endif
curr_off = 0;
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
/* Get trailer sector information; this is done early because inability to get
* that sector information means that upload will not work anyway.
* TODO: This is single occurrence issue, it should get detected during tests
* and fixed otherwise you are deploying broken mcuboot.
*/
if (flash_area_get_sector(fap, boot_status_off(fap), &status_sector)) {
rc = MGMT_ERR_EUNKNOWN;
BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
goto out;
}
#endif
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
/* We are using swap state at end of flash area to store validation
* result. Make sure the user cannot write it from an image to skip validation.
*/
if (img_size_tmp > (area_size - BOOT_MAGIC_SZ)) {
goto out_invalid_data;
}
#else
if (img_size_tmp > area_size) {
goto out_invalid_data;
}
#endif
#ifndef MCUBOOT_ERASE_PROGRESSIVELY
/* Non-progressive erase erases entire image slot when first chunk of