forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libwolfboot.c
1912 lines (1758 loc) · 56.9 KB
/
libwolfboot.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
/* libwolfboot.c
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/**
* @file libwolfboot.c
*
* @brief wolfBoot library implementation.
*
* This file contains the implementation of the wolfBoot library.
*/
#include <stdint.h>
#include "hal.h"
#include "wolfboot/wolfboot.h"
#include "image.h"
#include "printf.h"
#ifdef UNIT_TEST
/**
* @def unit_dbg
* @brief Conditional debug macro for unit tests.
*
* Conditional debug macro for unit tests, redirects to wolfBoot_printf.
*/
# define unit_dbg wolfBoot_printf
#else
/**
* @def unit_dbg
* @brief Empty macro for unit_dbg in non-test builds.
*
* Empty macro for unit_dbg in non-test builds.
*/
# define unit_dbg(...) do{}while(0)
#endif
#ifndef TRAILER_SKIP
/**
* @def TRAILER_SKIP
* @brief Trailer skip value for partition encryption.
*
* Trailer skip value for partition encryption, defaults to 0 if not defined.
*/
# define TRAILER_SKIP 0
#endif
#include <stddef.h> /* for size_t */
#if defined(EXT_ENCRYPTED)
static int encrypt_initialized = 0;
static uint8_t encrypt_iv_nonce[ENCRYPT_NONCE_SIZE];
#if defined(__WOLFBOOT)
#include "encrypt.h"
#elif !defined(XMEMSET)
#include <string.h>
#define XMEMSET memset
#define XMEMCPY memcpy
#define XMEMCMP memcmp
#endif
#endif
#if defined(EXT_FLASH) && defined(EXT_ENCRYPTED)
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - \
(TRAILER_SKIP + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE))
#define TRAILER_OVERHEAD (4 + 1 + (WOLFBOOT_PARTITION_SIZE / \
(2 * WOLFBOOT_SECTOR_SIZE)))
/* MAGIC (4B) + PART_FLAG (1B) + (N_SECTORS / 2) */
#define START_FLAGS_OFFSET (ENCRYPT_TMP_SECRET_OFFSET - TRAILER_OVERHEAD)
#define SECTOR_FLAGS_SIZE WOLFBOOT_SECTOR_SIZE - (4 + 1 + \
ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE)
/* MAGIC (4B) + PART_FLAG (1B) + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE */
#else
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP))
#define SECTOR_FLAGS_SIZE WOLFBOOT_SECTOR_SIZE - (4 + 1)
/* MAGIC (4B) + PART_FLAG (1B) */
#endif /* EXT_FLASH && EXT_ENCRYPTED */
#if !defined(__WOLFBOOT) && !defined(UNIT_TEST)
#define XMEMSET memset
#define XMEMCPY memcpy
#define XMEMCMP memcmp
#endif
#ifndef NULL
# define NULL (void *)0
#endif
#ifndef NVM_CACHE_SIZE
#define NVM_CACHE_SIZE WOLFBOOT_SECTOR_SIZE
#endif
#ifdef BUILD_TOOL
/* Building for a local utility tool */
#undef EXT_FLASH
#undef EXT_ENCRYPTED
#undef WOLFBOOT_FIXED_PARTITIONS
#endif
#ifdef EXT_FLASH
static uint32_t ext_cache;
#endif
#if defined(__WOLFBOOT) || defined(UNIT_TEST)
#define WOLFSSL_MISC_INCLUDED /* allow misc.c code to be inlined */
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfcrypt/src/misc.c> /* for ByteReverseWord32 */
#if defined(EXT_ENCRYPTED) || defined(UNIT_TEST)
static uint32_t wb_reverse_word32(uint32_t x)
{
return ByteReverseWord32(x);
}
#endif
#endif
static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
/* Top addresses for FLAGS field
* - PART_BOOT_ENDFLAGS = top of flags for BOOT partition
* - PART_UPDATE_ENDFLAGS = top of flags for UPDATE_PARTITION
*/
#ifndef PART_BOOT_ENDFLAGS
#define PART_BOOT_ENDFLAGS (WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET)
#endif
#define FLAGS_BOOT_EXT() PARTN_IS_EXT(PART_BOOT)
#ifdef FLAGS_HOME
/*
* In FLAGS_HOME mode, all FLAGS live at the end of the boot partition:
* / -12 /-8 /-4 / END
* |Sn| ... |S2|S1|S0|PU| MAGIC |X|X|X|PB| MAGIC |
* ^--sectors --^ ^--update ^---boot partition
* flags partition flag
* flag
*
* */
#define PART_UPDATE_ENDFLAGS (PART_BOOT_ENDFLAGS - 8)
#define FLAGS_UPDATE_EXT() PARTN_IS_EXT(PART_BOOT)
#else
/* FLAGS are at the end of each partition */
#define PART_UPDATE_ENDFLAGS (WOLFBOOT_PARTITION_UPDATE_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET)
#define FLAGS_UPDATE_EXT() PARTN_IS_EXT(PART_UPDATE)
#endif
#ifdef NVM_FLASH_WRITEONCE
/* Some internal FLASH memory models don't allow
* multiple writes after erase in the same page/area.
*
* NVM_FLASH_WRITEONCE uses a redundant two-sector model
* to mitigate the effect of power failures.
*
*/
#ifndef WOLFBOOT_FLAGS_INVERT
#define FLAG_CMP(a,b) ((a < b)? 0 : 1)
#else
#define FLAG_CMP(a,b) ((a > b)? 0 : 1)
#endif
#include <stddef.h>
#include <string.h>
static uint8_t NVM_CACHE[NVM_CACHE_SIZE] __attribute__((aligned(16)));
static int nvm_cached_sector = 0;
static uint8_t get_base_offset(uint8_t *base, uintptr_t off)
{
return *(base - off); /* ignore array bounds error */
}
void WEAKFUNCTION hal_cache_invalidate(void)
{
/* if cache flushing is required implement in hal */
}
static int RAMFUNCTION nvm_select_fresh_sector(int part)
{
int sel;
uintptr_t off;
uint8_t *base;
uint8_t* addrErase = 0;
uint32_t word_0;
uint32_t word_1;
#if defined(EXT_FLASH) && !defined(FLAGS_HOME)
if ((part == PART_UPDATE) && FLAGS_UPDATE_EXT()) {
return 0;
}
#endif
hal_cache_invalidate();
if (part == PART_BOOT) {
base = (uint8_t *)PART_BOOT_ENDFLAGS;
addrErase = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS +
WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE;
}
else {
base = (uint8_t *)PART_UPDATE_ENDFLAGS;
#ifdef FLAGS_HOME
addrErase = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS +
WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE;
#else
addrErase = (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS +
WOLFBOOT_PARTITION_SIZE - WOLFBOOT_SECTOR_SIZE;
#endif
}
/* check magic in case the sector is corrupt */
word_0 = *((uint32_t*)((uintptr_t)base - sizeof(uint32_t)));
word_1 = *((uint32_t*)((uintptr_t)base - (WOLFBOOT_SECTOR_SIZE + sizeof(uint32_t))));
if (word_0 == WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL) {
sel = 0;
goto finish;
}
else if (word_0 != WOLFBOOT_MAGIC_TRAIL && word_1 == WOLFBOOT_MAGIC_TRAIL) {
sel = 1;
goto finish;
} else if (word_0 != WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL) {
/* none of the partition has a valid trailer, default to '0' */
sel = 0;
goto finish;
}
/* Default to last sector if no match is found */
sel = 0;
/* Select the sector with more flags set. Partition flag is at offset '4'.
* Sector flags begin from offset '5'.
*/
for (off = 4; off < WOLFBOOT_SECTOR_SIZE; off++) {
volatile uint8_t byte_0 = get_base_offset(base, off);
volatile uint8_t byte_1 = get_base_offset(base, (WOLFBOOT_SECTOR_SIZE + off));
if (byte_0 == FLASH_BYTE_ERASED && byte_1 != FLASH_BYTE_ERASED) {
sel = 1;
break;
}
else if (byte_0 != FLASH_BYTE_ERASED && byte_1 == FLASH_BYTE_ERASED) {
sel = 0;
break;
}
else if ((byte_0 == FLASH_BYTE_ERASED) &&
(byte_1 == FLASH_BYTE_ERASED)) {
/* Examine previous position one byte ahead */
byte_0 = get_base_offset(base, (off - 1));
byte_1 = get_base_offset(base, ((WOLFBOOT_SECTOR_SIZE + off) - 1));
sel = FLAG_CMP(byte_0, byte_1);
break;
}
}
finish:
/* Erase the non-selected partition */
addrErase -= WOLFBOOT_SECTOR_SIZE * (!sel);
if (*((uint32_t*)(addrErase + WOLFBOOT_SECTOR_SIZE - sizeof(uint32_t)))
!= FLASH_WORD_ERASED) {
hal_flash_erase((uintptr_t)addrErase, WOLFBOOT_SECTOR_SIZE);
}
return sel;
}
/**
* @brief Write the trailer in a non-volatile memory.
*
* This function writes the trailer in a non-volatile memory.
*
* @param[in] part Partition number.
* @param[in] addr Address of the trailer.
* @param[in] val New value to write in the trailer.
* @return 0 on success, -1 on failure.
*/
static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val)
{
uintptr_t addr_align = (size_t)(addr & (~(NVM_CACHE_SIZE - 1)));
uintptr_t addr_read, addr_write;
uintptr_t addr_off = addr & (NVM_CACHE_SIZE - 1);
int ret = 0;
nvm_cached_sector = nvm_select_fresh_sector(part);
addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE);
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
NVM_CACHE[addr_off] = val;
/* Calculate write address */
addr_write = addr_align - ((!nvm_cached_sector) * NVM_CACHE_SIZE);
/* Ensure that the destination was erased */
hal_flash_erase(addr_write, NVM_CACHE_SIZE);
#if FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE
addr_off = 0;
while ((addr_off < WOLFBOOT_SECTOR_SIZE) && (ret == 0)) {
ret = hal_flash_write(addr_write + addr_off, NVM_CACHE + addr_off,
FLASHBUFFER_SIZE);
addr_off += FLASHBUFFER_SIZE;
}
#else
ret = hal_flash_write(addr_write, NVM_CACHE, NVM_CACHE_SIZE);
#endif
/* Once a copy has been written, erase the older sector */
ret = hal_flash_erase(addr_read, NVM_CACHE_SIZE);
nvm_cached_sector = !nvm_cached_sector;
return ret;
}
/**
* @brief Write the partition magic in a non-volatile memory.
*
* This function writes the partition magic in a non-volatile memory.
*
* @param[in] part Partition number.
* @param[in] addr Address of the magic trailer.
* @return 0 on success, -1 on failure.
*/
static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
{
uintptr_t off = addr % NVM_CACHE_SIZE;
uintptr_t base = (uintptr_t)addr - off;
uintptr_t addr_read, addr_write;
int ret;
nvm_cached_sector = nvm_select_fresh_sector(part);
addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE);
addr_write = base - (!nvm_cached_sector * NVM_CACHE_SIZE);
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
XMEMCPY(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t));
ret = hal_flash_write(addr_write, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
nvm_cached_sector = !nvm_cached_sector;
ret = hal_flash_erase(addr_read, WOLFBOOT_SECTOR_SIZE);
return ret;
}
#else
# define trailer_write(part,addr, val) hal_flash_write(addr, (void *)&val, 1)
# define partition_magic_write(part,addr) hal_flash_write(addr, \
(void*)&wolfboot_magic_trail, sizeof(uint32_t));
#endif /* NVM_FLASH_WRITEONCE */
#ifndef MOCK_PARTITION_TRAILER /* used for unit-mock-state.c */
#ifdef CUSTOM_PARTITION_TRAILER
/* Custom partition trailer
* Function implementation externally defined
*/
uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at);
void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val);
void RAMFUNCTION set_partition_magic(uint8_t part);
#elif !defined(WOLFBOOT_FIXED_PARTITIONS)
static uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at)
{
(void)part;
(void)at;
return 0;
}
static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
{
(void)part;
(void)at;
(void)val;
return;
}
static void RAMFUNCTION set_partition_magic(uint8_t part)
{
(void)part;
return;
}
#else
/**
* @brief Get the trailer at a specific address
*
* This function retrieves the trailer at a specific address in external or
* internal flash
*
* @param[in] part Partition number.
* @param[in] at Address offset.
* @return Pointer to the trailer at the specified address.
*/
static uint8_t* RAMFUNCTION get_trailer_at(uint8_t part, uint32_t at)
{
uint8_t *ret = NULL;
uint32_t sel_sec = 0;
if (part == PART_BOOT) {
#ifdef EXT_FLASH
if (FLAGS_BOOT_EXT()){
ext_flash_check_read(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at),
(void *)&ext_cache, sizeof(uint32_t));
ret = (uint8_t *)&ext_cache;
}
else
#endif
{
/* only internal flash should be writeonce */
#ifdef NVM_FLASH_WRITEONCE
sel_sec = nvm_select_fresh_sector(part);
#endif
ret = (void *)(PART_BOOT_ENDFLAGS -
(WOLFBOOT_SECTOR_SIZE * sel_sec + (sizeof(uint32_t) + at)));
}
}
else if (part == PART_UPDATE) {
#ifdef EXT_FLASH
if (FLAGS_UPDATE_EXT()) {
ext_flash_check_read(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at),
(void *)&ext_cache, sizeof(uint32_t));
ret = (uint8_t *)&ext_cache;
}
else
#endif
{
/* only internal flash should be writeonce */
#ifdef NVM_FLASH_WRITEONCE
sel_sec = nvm_select_fresh_sector(part);
#endif
ret = (void *)(PART_UPDATE_ENDFLAGS -
(WOLFBOOT_SECTOR_SIZE * sel_sec + (sizeof(uint32_t) + at)));
}
}
return ret;
}
/**
* @brief Set the trailer at a specific address
*
* This function sets the trailer at a specific address in external or
* internal flash.
*
* @param[in] part Partition number.
* @param[in] at Address offset.
* @param[in] val New value to set in the trailer.
*/
static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
{
if (part == PART_BOOT) {
#ifdef EXT_FLASH
if (FLAGS_BOOT_EXT()) {
/* use ext_cache and 32-bit writes to avoid any underlying hardware
* issues with 1-byte write */
ext_cache &= ~0xFF;
ext_cache |= val;
ext_flash_check_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at),
(void *)&ext_cache, sizeof(uint32_t));
}
else
#endif
{
trailer_write(part, PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), val);
}
}
else if (part == PART_UPDATE) {
#ifdef EXT_FLASH
if (FLAGS_UPDATE_EXT()) {
/* use ext_cache and 32-bit writes to avoid any underlying hardware
* issues with 1-byte write */
ext_cache &= ~0xFF;
ext_cache |= val;
ext_flash_check_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at),
(void *)&ext_cache, sizeof(uint32_t));
}
else
#endif
{
trailer_write(part, PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), val);
}
}
}
/**
* @brief Set the partition magic trailer
*
* This function sets the partition magic trailer in external or internal flash.
*
* @param[in] part Partition number.
*/
static void RAMFUNCTION set_partition_magic(uint8_t part)
{
if (part == PART_BOOT) {
#ifdef EXT_FLASH
if (FLAGS_BOOT_EXT()) {
ext_flash_check_write(PART_BOOT_ENDFLAGS - sizeof(uint32_t),
(void *)&wolfboot_magic_trail, sizeof(uint32_t));
}
else
#endif
{
partition_magic_write(part, PART_BOOT_ENDFLAGS - sizeof(uint32_t));
}
}
else if (part == PART_UPDATE) {
#ifdef EXT_FLASH
if (FLAGS_UPDATE_EXT()) {
ext_flash_check_write(PART_UPDATE_ENDFLAGS - sizeof(uint32_t),
(void *)&wolfboot_magic_trail, sizeof(uint32_t));
}
else
#endif
{
partition_magic_write(part, PART_UPDATE_ENDFLAGS - sizeof(uint32_t));
}
}
}
#endif
#endif /* !MOCK_PARTITION_TRAILER */
#ifdef WOLFBOOT_FIXED_PARTITIONS
/**
* @brief Get the magic trailer of a partition.
*
* This function retrieves the magic trailer of a fixed partition.
*
* @param[in] part Partition number.
* @return Pointer to the magic trailer of the partition.
*/
static uint32_t* RAMFUNCTION get_partition_magic(uint8_t part)
{
return (uint32_t *)get_trailer_at(part, 0);
}
static uint8_t* RAMFUNCTION get_partition_state(uint8_t part)
{
return (uint8_t *)get_trailer_at(part, 1);
}
static void RAMFUNCTION set_partition_state(uint8_t part, uint8_t val)
{
set_trailer_at(part, 1, val);
}
/**
* @brief Set the flags of an update sector.
*
* This function sets the flags of an update sector in a fixed partition.
*
* @param[in] pos Update sector position.
* @param[in] val New flags value to set.
* @return 0 on success, -1 on failure.
*/
static void RAMFUNCTION set_update_sector_flags(uint32_t pos, uint8_t val)
{
set_trailer_at(PART_UPDATE, 2 + pos, val);
}
/**
* @brief Get the flags of an update sector.
*
* This function retrieves the flags of an update sector in a fixed partition.
*
* @param[in] pos Update sector position.
* @return Pointer to the flags of the update sector.
*/
static uint8_t* RAMFUNCTION get_update_sector_flags(uint32_t pos)
{
return (uint8_t *)get_trailer_at(PART_UPDATE, 2 + pos);
}
/**
* @brief Set the state of a partition.
*
* This function sets the state of a fixed partition.
*
* @param[in] part Partition number.
* @param[in] newst New state value to set.
* @return 0 on success, -1 on failure.
*/
int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
{
uint32_t *magic;
uint8_t *state;
if (part == PART_NONE)
return -1;
magic = get_partition_magic(part);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
set_partition_magic(part);
state = get_partition_state(part);
if (*state != newst)
set_partition_state(part, newst);
return 0;
}
/**
* @brief Set the flag for sector
*
* This function sets the sector flag for update partition.
*
* @param[in] sector Sector number.
* @param[in] newflag Nibble (4-bits) for sector flag
* @return 0 on success, -1 on failure.
*/
int RAMFUNCTION wolfBoot_set_update_sector_flag(uint16_t sector,
uint8_t newflag)
{
uint32_t *magic;
uint8_t *flags;
uint8_t fl_value;
uint8_t pos = sector >> 1;
magic = get_partition_magic(PART_UPDATE);
if (*magic != wolfboot_magic_trail)
set_partition_magic(PART_UPDATE);
flags = get_update_sector_flags(pos);
if (sector == (pos << 1))
fl_value = (*flags & 0xF0) | (newflag & 0x0F);
else
fl_value = ((newflag & 0x0F) << 4) | (*flags & 0x0F);
if (fl_value != *flags)
set_update_sector_flags(pos, fl_value);
return 0;
}
/**
* @brief Get the state of a partition.
*
* This function retrieves the state of a fixed partition.
*
* @param[in] part Partition number.
* @param[out] st Pointer to store the partition state.
* @return 0 on success, -1 on failure.
*/
int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
{
uint32_t *magic;
uint8_t *state;
if (part == PART_NONE)
return -1;
magic = get_partition_magic(part);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
return -1;
state = get_partition_state(part);
*st = *state;
return 0;
}
/**
* @brief Get the flag for sector
*
* This function retrieves the sector flag for update partition.
*
* User may override this is function for cases where the update partition
* flags are not at the end of partition
*
* @param[in] sector Sector number.
* @param[out] flag Nibble (4-bits) for sector flags
* @return 0 on success, -1 on failure.
*/
int wolfBoot_get_update_sector_flag(uint16_t sector, uint8_t *flag)
{
uint32_t *magic;
uint8_t *flags;
uint8_t pos = sector >> 1;
magic = get_partition_magic(PART_UPDATE);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
return -1;
flags = get_update_sector_flags(pos);
if (sector == (pos << 1))
*flag = *flags & 0x0F;
else
*flag = (*flags & 0xF0) >> 4;
return 0;
}
/**
* @brief Erase a partition.
*
* This function erases a partition.
*
* @param[in] part Partition number.
*/
void RAMFUNCTION wolfBoot_erase_partition(uint8_t part)
{
uint32_t address = 0;
int size = 0;
switch (part) {
case PART_BOOT:
address = (uint32_t)WOLFBOOT_PARTITION_BOOT_ADDRESS;
size = WOLFBOOT_PARTITION_SIZE;
break;
case PART_UPDATE:
address = (uint32_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
size = WOLFBOOT_PARTITION_SIZE;
break;
case PART_SWAP:
address = (uint32_t)WOLFBOOT_PARTITION_SWAP_ADDRESS;
size = WOLFBOOT_SECTOR_SIZE;
break;
default:
break;
}
if (size > 0) {
if (PARTN_IS_EXT(part)) {
ext_flash_unlock();
ext_flash_erase(address, size);
ext_flash_lock();
} else {
hal_flash_erase(address, size);
}
}
}
/**
* @brief Update trigger function.
*
* This function updates the boot partition state to "IMG_STATE_UPDATING".
* If the FLAGS_HOME macro is defined, it erases the last sector of the boot
* partition before updating the partition state. It also checks FLAGS_UPDATE_EXT
* and calls the appropriate flash unlock and lock functions before
* updating the partition state.
*/
void RAMFUNCTION wolfBoot_update_trigger(void)
{
uint8_t st = IMG_STATE_UPDATING;
uintptr_t lastSector = ((PART_UPDATE_ENDFLAGS - 1) / WOLFBOOT_SECTOR_SIZE) * WOLFBOOT_SECTOR_SIZE;
#ifdef NVM_FLASH_WRITEONCE
uint8_t selSec = 0;
#endif
/* if PART_UPDATE_ENDFLAGS straddles a sector, (all non FLAGS_HOME builds)
* align it to the correct sector */
if (PART_UPDATE_ENDFLAGS % WOLFBOOT_SECTOR_SIZE == 0)
lastSector -= WOLFBOOT_SECTOR_SIZE;
/* erase the sector flags */
if (FLAGS_UPDATE_EXT()) {
ext_flash_unlock();
} else {
hal_flash_unlock();
}
/* NVM_FLASH_WRITEONCE needs erased flags since it selects the fresh
* partition based on how many flags are non-erased
* FLAGS_INVERT needs erased flags because the bin-assemble's fill byte may
* not match what's in wolfBoot */
if (FLAGS_UPDATE_EXT()) {
ext_flash_erase(lastSector, SECTOR_FLAGS_SIZE);
} else {
#ifndef NVM_FLASH_WRITEONCE
hal_flash_erase(lastSector, SECTOR_FLAGS_SIZE);
#else
selSec = nvm_select_fresh_sector(PART_UPDATE);
lastSector -= selSec * WOLFBOOT_SECTOR_SIZE;
XMEMCPY(NVM_CACHE, (uint8_t*)lastSector, WOLFBOOT_SECTOR_SIZE);
/* write to the non selected sector */
hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec, WOLFBOOT_SECTOR_SIZE);
hal_flash_write(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec, NVM_CACHE,
WOLFBOOT_SECTOR_SIZE);
/* erase the previously selected sector */
hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * selSec,
WOLFBOOT_SECTOR_SIZE);
#endif
}
wolfBoot_set_partition_state(PART_UPDATE, st);
if (FLAGS_UPDATE_EXT()) {
ext_flash_lock();
} else {
hal_flash_lock();
}
}
/**
* @brief Success function.
*
* This function updates the boot partition state to "IMG_STATE_SUCCESS".
* If the FLAGS_BOOT_EXT macro is defined, it calls the appropriate flash unlock
* and lock functions before updating the partition state. If the EXT_ENCRYPTED
* macro is defined, it calls wolfBoot_erase_encrypt_key function.
*/
void RAMFUNCTION wolfBoot_success(void)
{
uint8_t st = IMG_STATE_SUCCESS;
if (FLAGS_BOOT_EXT()) {
ext_flash_unlock();
wolfBoot_set_partition_state(PART_BOOT, st);
ext_flash_lock();
} else {
hal_flash_unlock();
wolfBoot_set_partition_state(PART_BOOT, st);
hal_flash_lock();
}
#ifdef EXT_ENCRYPTED
wolfBoot_erase_encrypt_key();
#endif
}
#endif /* WOLFBOOT_FIXED_PARTITIONS */
/**
* @brief Find header function.
*
* This function searches for a specific header type in the given buffer.
* It returns the length of the header and sets the 'ptr' parameter to the
* position of the header if found.
* @param haystack Pointer to the buffer to search for the header.
* @param type The type of header to search for.
* @param ptr Pointer to store the position of the header.
*
* @return uint16_t The length of the header found, or 0 if not found.
*
*/
uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr)
{
uint8_t *p = haystack;
uint16_t len, htype;
const volatile uint8_t *max_p = (haystack - IMAGE_HEADER_OFFSET) +
IMAGE_HEADER_SIZE;
*ptr = NULL;
if (p > max_p) {
unit_dbg("Illegal address (too high)\n");
return 0;
}
while ((p + 4) < max_p) {
htype = p[0] | (p[1] << 8);
if (htype == 0) {
unit_dbg("Explicit end of options reached\n");
break;
}
/* skip unaligned half-words and padding bytes */
if ((p[0] == HDR_PADDING) || ((((size_t)p) & 0x01) != 0)) {
p++;
continue;
}
len = p[2] | (p[3] << 8);
/* check len */
if ((4 + len) > (uint16_t)(IMAGE_HEADER_SIZE - IMAGE_HEADER_OFFSET)) {
unit_dbg("This field is too large (bigger than the space available "
"in the current header)\n");
unit_dbg("%d %d %d\n", len, IMAGE_HEADER_SIZE, IMAGE_HEADER_OFFSET);
break;
}
/* check max pointer */
if (p + 4 + len > max_p) {
unit_dbg("This field is too large and would overflow the image "
"header\n");
break;
}
/* skip header [type|len] */
p += 4;
if (htype == type) {
/* found, return pointer to data portion */
*ptr = p;
return len;
}
p += len;
}
return 0;
}
#ifdef EXT_FLASH
static uint8_t hdr_cpy[IMAGE_HEADER_SIZE];
static uint32_t hdr_cpy_done = 0;
#endif
/**
* @brief Convert little-endian to native-endian (uint32_t).
*
* This function converts a little-endian 32-bit value to the native-endian format.
* It is used to handle endianness differences when reading data from memory.
*
* @param val The value to convert.
*
* @return The converted value.
*/
static inline uint32_t im2n(uint32_t val)
{
#ifdef BIG_ENDIAN_ORDER
val = (((val & 0x000000FF) << 24) |
((val & 0x0000FF00) << 8) |
((val & 0x00FF0000) >> 8) |
((val & 0xFF000000) >> 24));
#endif
return val;
}
/**
* @brief Convert little-endian to native-endian (uint16_t).
*
* This function converts a little-endian 16-bit value to the native-endian format.
* It is used to handle endianness differences when reading data from memory.
*
* @param val The value to convert.
* @return uint16_t The converted value.
*/
static inline uint16_t im2ns(uint16_t val)
{
#ifdef BIG_ENDIAN_ORDER
val = (((val & 0x000000FF) << 8) |
((val & 0x0000FF00) >> 8));
#endif
return val;
}
#ifdef DELTA_UPDATES
/**
* @brief Get delta update information.
*
* This function retrieves the delta update information for a given partition.
* It checks if the partition is extended, reads the image header, and returns
* the delta image offset and size. The 'inverse' flag indicates whether to get
* the inverse delta information or regular delta information.
*
* @param part The partition to check for delta update information.
* @param inverse Flag to indicate if the delta update is inverse.
* @param img_offset Pointer to store the delta image offset.
* @param img_size Pointer to store the delta image size.
*
* @return int 0 if successful, -1 if not found or an error occurred.
*
*/
int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset,
uint32_t **img_size, uint8_t **base_hash, uint16_t *base_hash_size)
{
uint32_t *magic = NULL;
uint8_t *image = (uint8_t *)0x00000000;
if (part == PART_UPDATE) {
if (PARTN_IS_EXT(PART_UPDATE)) {
#ifdef EXT_FLASH
ext_flash_check_read((uintptr_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS,
hdr_cpy, IMAGE_HEADER_SIZE);
hdr_cpy_done = 1;
image = hdr_cpy;
#endif
} else {
image = (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
}
} else if (part == PART_BOOT) {
if (PARTN_IS_EXT(PART_BOOT)) {
#ifdef EXT_FLASH
ext_flash_check_read((uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS,
hdr_cpy, IMAGE_HEADER_SIZE);
hdr_cpy_done = 1;
image = hdr_cpy;
#endif
} else {
image = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
}
}
/* Don't check image against NULL to allow using address 0x00000000 */
magic = (uint32_t *)image;
if (*magic != WOLFBOOT_MAGIC)
return -1;
if (inverse) {
if (wolfBoot_find_header((uint8_t *)(image + IMAGE_HEADER_OFFSET),
HDR_IMG_DELTA_INVERSE, (uint8_t **)img_offset)
!= sizeof(uint32_t)) {
return -1;
}
if (wolfBoot_find_header((uint8_t *)(image + IMAGE_HEADER_OFFSET),
HDR_IMG_DELTA_INVERSE_SIZE, (uint8_t **)img_size)
!= sizeof(uint32_t)) {
return -1;
}
} else {
*img_offset = 0x0000000;
if (wolfBoot_find_header((uint8_t *)(image + IMAGE_HEADER_OFFSET),
HDR_IMG_DELTA_SIZE, (uint8_t **)img_size)
!= sizeof(uint32_t)) {
return -1;
}
}
*base_hash_size = wolfBoot_find_header((uint8_t *)(image + IMAGE_HEADER_OFFSET),
HDR_IMG_DELTA_BASE_HASH, base_hash);
return 0;
}
#endif
#if defined(EXT_ENCRYPTED) && defined(MMU)
static uint8_t dec_hdr[IMAGE_HEADER_SIZE];
static int decrypt_header(uint8_t *src)