-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcy_dfu.c
2466 lines (2203 loc) · 95.2 KB
/
cy_dfu.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
/***************************************************************************//**
* \file cy_dfu.c
* \version 6.0
*
* This file provides the implementation of DFU Middleware.
*
********************************************************************************
* \copyright
* (c) (2016-2024), Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
********************************************************************************
* This software, including source code, documentation and related materials
* ("Software") is owned by Cypress Semiconductor Corporation or one of its
* affiliates ("Cypress") and is protected by and subject to worldwide patent
* protection (United States and foreign), United States copyright laws and
* international treaty provisions. Therefore, you may use this Software only
* as provided in the license agreement accompanying the software package from
* which you obtained this Software ("EULA").
*
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software source
* code solely for use in connection with Cypress's integrated circuit products.
* Any reproduction, modification, translation, compilation, or representation
* of this Software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer of such
* system or application assumes all risk of such use and in doing so agrees to
* indemnify Cypress against all liability.
*******************************************************************************/
#include <string.h>
#include "cy_dfu.h"
#include "cy_dfu_logging.h"
#define CySoftwareReset() NVIC_SystemReset()
/** \cond INTERNAL */
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
CY_SECTION(".cy_boot_noinit.appId") __USED static uint8_t cy_dfu_appId;
#endif /* CY_DFU_FLOW == CY_DFU_BASIC_FLOW */
/* The timeout for Cy_DFU_Continue(), in milliseconds */
#define UPDATE_TIMEOUT (20U)
/* The timeout for the default Cy_DFU__TransportWrite() call in milliseconds */
#define TRANSPORT_WRITE_TIMEOUT (150U)
/* The number of bytes per app in the metadata section */
#define METADATA_BYTES_PER_APP (8U)
#define UINT16_SIZE (2U)
#define UINT32_SIZE (4U)
#define NIBBLE_POS (4U)
#define NIBBLE_MSK (0xFU)
#define SHA1_CHECKSUM_LENGTH (20U) /* The SHA1 length is 20 bytes */
/* A number of uint32_t elements in the SHA1 buffer */
#define SHA1_BUF_SIZE_UINT32 (SHA1_CHECKSUM_LENGTH/UINT32_SIZE)
#define RSA_CHECKSUM_LENGTH (256U) /* The RSA public key modulo length is 2048 bits = 256 bytes */
#define CRC_CHECKSUM_LENGTH (4U) /* The size of metadata flash row CRC in bytes */
#define CRC_POLYNOMIAL (0x1EDC6F41U) /* The CRC 32 polynomial */
#define CRC_LFSR_SEED (0xFFFFFFFFU)
#define CRC_DATA_REVERSE (1U)
#define CRC_DATA_XOR (0U)
#define CRC_REM_REVERSE (1U)
#define CRC_REM_XOR (0xFFFFFFFFU)
#define CRC_TABLE_SIZE (16U) /* A number of uint32_t elements in the CRC32 table */
#define CRC_INIT (0xFFFFFFFFU)
#define CRC_CCITT_INIT (0xFFFFU)
#define CRC_CCITT_POLYNOMIAL (0x8408U)
#define STATUS_BYTE_MSK (0xFFU)
/* The size in bytes of the DFU command parameters */
#define PARAMS_SIZE (8U)
/* The length in bytes of data in the "Set App Metadata" DFU command */
#define DATA_LENGTH (9U)
/* Possible sizes of the data field in the DFU packets */
#define DATA_PACKET_SIZE_4BYTES (4U)
#define DATA_PACKET_SIZE_6BYTES (6U)
#define DATA_PACKET_SIZE_8BYTES (8U)
#define DATA_PACKET_SIZE_16BYTES (16U)
#define PACKET_DATA_NO_OFFSET (0U)
#define PROGRAM_DATA_CRC_OFFSET (4U) /* The offset in bytes to the CRC field in the Program Data command */
#define VERIFY_DATA_CRC_OFFSET (4U) /* The offset in bytes to the CRC field in the Verify Data command */
/* The size in bytes of the data field in the Verify Application command */
#define VERIFY_APP_DATA_SIZE (1U)
/* The offset in bytes to the Application Length in the application metadata */
#define METADATA_APP_LENGTH_OFFSET (4U)
/* The offset in bytes to the application length in the data field of the Set Application Metadata command packet*/
#define SET_APP_METADATA_OFFSET (1U)
/* The offset to the application start address in the data field of the Set Application Metadata command packet */
#define SET_APP_METADATA_LENGTH_OFFSET (5U)
/* The offset to the "to" part of the data field in the Get Metadata packet */
#define GET_METADATA_TO_OFFSET (2U)
/* The size in bytes of the App Size field in Cypress Simplified User Application Object */
#define SIMPLIFIED_APP_APPSIZE_SIZE (4U)
/* The offset in bytes to the VT offset in the Cypress Standard User Application Object */
#define CYPRESS_APP_VTOFFSET_OFFSET_BYTES (0x10U)
/* The offset in uint32_t to the VT offset in the Cypress Standard User Application Object */
#define CYPRESS_APP_VTOFFSET_OFFSET_UINT32 (CYPRESS_APP_VTOFFSET_OFFSET_BYTES/UINT32_SIZE)
#define TOC_EMPTY (0UL) /* Both TOC2 and RTOC2 are empty */
#define TOC_INVALID (1UL) /* Either TOC2 or RTOC2 is invalid */
#define PUBLIC_KEY_IDX (9UL) /* The TOC item at index 9 is a public Key object */
#define PUBLIC_KEY_OFFSET (8UL) /* The Public Key offset in the Public key Object */
/* The address of the verify application function entry in the Flash Boot shared functions table */
#define VERIFY_APP_TABLE_ADDR (0x16002040UL)
/* The address of the verify key function entry in the Flash Boot shared functions table */
#define IS_VALID_KEY_TABLE_ADDR (0x16002044UL)
/* The address of the verify TOC function entry in the Flash Boot shared functions table */
#define VALIDATE_TOC_TABLE_ADDR (0x1600204CUL)
/* For the DFU packet */
#define PACKET_SOP_VALUE (0x01U)
#define PACKET_EOP_VALUE (0x17U)
#define PACKET_SOP_IDX (0x00U)
#define PACKET_CMD_IDX (0x01U)
#define PACKET_SIZE_IDX (0x02U)
#define PACKET_DATA_IDX (0x04U)
#define PACKET_CHECKSUM_LENGTH (2U) /* The length in bytes of a packet checksum field */
/* The Flash Boot verification functions*/
#if(CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP)
typedef bool (*Cy_FB_VerifyApp_t)(uint32_t address, uint32_t length, uint32_t signature, uint32_t publicKeyAddr);
typedef bool (*Cy_FB_IsValidKey_t)(uint32_t tocAddr, uint32_t publicKeyAddr);
typedef uint32_t (*Cy_FB_ValidateToc_t)(uint32_t tocAddress);
#endif /* (CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP) */
/* Pointer to function that is used to jump into address */
typedef void (*cy_fn_dfu_jump_ptr_t)(void);
/** \endcond */
/* The static functions declaration */
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
static uint32_t ElfSymbolToAddr(void volatile const *symbol);
static __NO_RETURN void SwitchToApp(uint32_t stackPointer, uint32_t address);
#if ((CY_DFU_OPT_CRYPTO_HW != 0) && (CY_DFU_APP_FORMAT == CY_DFU_BASIC_APP))
static bool ComputeSha1(uint32_t address, uint32_t length, uint8_t *result);
#endif /*((CY_DFU_OPT_CRYPTO_HW != 0) && (CY_DFU_APP_FORMAT == CY_DFU_BASIC_APP))*/
#endif /* CY_DFU_FLOW == CY_DFU_BASIC_FLOW */
static uint16_t GetU16(uint8_t const array[]);
static uint32_t GetU32(uint8_t const array[]);
static void PutU16(uint8_t array[], uint32_t offset, uint32_t value);
/* Because PutU32() is used only when updating the metadata */
#if (defined(CY_DFU_METADATA_WRITABLE) && (CY_DFU_METADATA_WRITABLE != 0)) && (CY_DFU_FLOW == CY_DFU_BASIC_FLOW)
static void PutU32(uint8_t array[], uint32_t offset, uint32_t value);
#endif /* (CY_DFU_METADATA_WRITABLE != 0) && (CY_DFU_FLOW == CY_DFU_BASIC_FLOW) */
static uint32_t PacketChecksumIndex(uint32_t size);
static uint32_t PacketEopIndex(uint32_t size);
static uint32_t GetPacketCommand(const uint8_t packet[]);
static uint32_t GetPacketDSize(const uint8_t packet[]);
static uint8_t* GetPacketData(uint8_t packet[], uint32_t offset);
static uint32_t GetPacketChecksum(const uint8_t packet[], uint32_t packetSize);
static uint32_t ValidatePacketFooter(const uint8_t packet[], uint32_t packetSize);
static void SetPacketHeader(uint8_t packet[]);
static void SetPacketCmd(uint8_t packet[], uint32_t cmd);
static void SetPacketDSize(uint8_t packet[], uint32_t size);
static void SetPacketChecksum(uint8_t packet[], uint32_t size, uint32_t checksum);
static void SetPacketFooter(uint8_t packet[], uint32_t size);
static uint32_t PacketChecksum(const uint8_t buffer[], uint32_t size);
static cy_en_dfu_status_t VerifyPacket(uint32_t numberRead, const uint8_t packet[]);
static cy_en_dfu_status_t ReadVerifyPacket(uint8_t packet[], bool *noResponse, uint32_t timeout);
static cy_en_dfu_status_t WritePacket(cy_en_dfu_status_t status, uint8_t *packet, uint32_t rspSize);
static void EnterResponse(uint8_t *packet, uint32_t *rspSize, uint32_t *state);
static cy_en_dfu_status_t CopyToDataBuffer(uint8_t dataBuffer[], uint32_t *dataOffset, uint8_t const packet[],
uint32_t packetSize);
static cy_en_dfu_status_t CommandEnter(uint8_t *packet, uint32_t *rspSize, uint32_t *state,
cy_stc_dfu_params_t *params);
static cy_en_dfu_status_t CommandProgramData(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#if CY_DFU_OPT_ERASE_DATA != 0
static cy_en_dfu_status_t CommandEraseData(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#endif
#if CY_DFU_OPT_VERIFY_DATA != 0
static cy_en_dfu_status_t CommandVerifyData(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#endif /* CY_DFU_OPT_VERIFY_DATA != 0*/
#if CY_DFU_OPT_SEND_DATA != 0
static cy_en_dfu_status_t CommandSendData(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#endif /* CY_DFU_OPT_SEND_DATA != 0 */
#if CY_DFU_OPT_VERIFY_APP != 0
static cy_en_dfu_status_t CommandVerifyApp(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#endif /* CY_DFU_OPT_VERIFY_APP != 0 */
static cy_en_dfu_status_t CommandSetAppMetadata(uint8_t *packet, uint32_t *rspSize,
cy_stc_dfu_params_t *params);
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
#if CY_DFU_OPT_GET_METADATA != 0
static cy_en_dfu_status_t CommandGetMetadata(uint8_t *packet, uint32_t *rspSize, cy_stc_dfu_params_t *params);
#endif /* CY_DFU_OPT_GET_METADATA != 0 */
#if CY_DFU_OPT_SET_EIVECTOR != 0
static cy_en_dfu_status_t CommandSetEIVector(uint8_t *packet, uint32_t *rspSize,
cy_stc_dfu_params_t *params);
#endif /* CY_DFU_OPT_SET_EIVECTOR != 0 */
#endif /* CY_DFU_FLOW == CY_DFU_BASIC_FLOW */
static cy_en_dfu_status_t CommandUnsupported(uint8_t packet[], uint32_t *rspSize,
cy_stc_dfu_params_t *params );
static cy_en_dfu_status_t ContinueHelper(uint32_t command, uint8_t *packet, uint32_t *rspSize,
cy_stc_dfu_params_t *params, bool *noResponse);
#if(CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP)
#if(CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST)
static bool VerifySecureAppShort(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr);
#else
static bool VerifySecureAppFull(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr);
#endif /* CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST */
static bool VerifySecureApp(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr);
#endif/*(CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP)*/
/*******************************************************************************
* Function Name: Cy_DFU_Init
****************************************************************************//**
*
* This function starts the application download and install operations.
* Make subsequent calls to Cy_DFU_Continue() to continue the
* process. \n
* Returns immediately, reporting success or failure. \n
* Only one updating operation can be done at a time - the user's code must
* ensure this.
*
* \param state The pointer to a state variable, that is updated by
* the function. See \ref group_dfu_macro_state
* \param params The pointer to a DFU parameters structure
* See \ref cy_stc_dfu_params_t
*
* \return See \ref cy_en_dfu_status_t.
* - \ref CY_DFU_SUCCESS if successful.
* - \ref CY_DFU_ERROR_UNKNOWN either parameter is a NULL pointer.
*
* \snippet snippet/source/COMPONENT_TEST_CS_COMMON/snippet_common.c DFU_INIT_VAR
* \snippet snippet/source/COMPONENT_TEST_CS_COMMON/snippet_common.c DFU_INIT_FUNC
*
*******************************************************************************/
cy_en_dfu_status_t Cy_DFU_Init(uint32_t *state, cy_stc_dfu_params_t *params)
{
cy_en_dfu_status_t status = CY_DFU_SUCCESS;
if ( (state == NULL) || (params == NULL) )
{
status = CY_DFU_ERROR_UNKNOWN;
}
if (status == CY_DFU_SUCCESS)
{
*state = CY_DFU_STATE_NONE;
params->dataOffset = 0U;
}
return (status);
}
/** \cond INTERNAL */
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
/*******************************************************************************
* Function Name: Cy_DFU_ExecuteApp
****************************************************************************//**
*
* This function transfers control from the current application to another
* application. The function performs switching via software reset. In case if
* application need to switch without reset, for example if it needs to enable
* some peripheral during and after the application switching,
* use \ref Cy_DFU_SwitchToApp().
* The function does not return.
* \note It is assumed appId is a valid application number.
*
* \param appId An application number of the application to switch to.
*
*******************************************************************************/
void Cy_DFU_ExecuteApp(uint32_t appId)
{
CY_ASSERT(appId < CY_DFU_MAX_APPS);
cy_dfu_appId = (uint8_t)appId;
CySoftwareReset();
}
/*******************************************************************************
* Function Name: SwitchToApp
****************************************************************************//**
*
* Set main stack pointer and then jumps into the address.
*
* \param stackPointer Stack pointer
* \param address Address to jump into
*
* \note This function does not return.
*
*******************************************************************************/
static void SwitchToApp(uint32_t stackPointer, uint32_t address)
{
__set_MSP(stackPointer);
CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.1','Casting int to a function pointer is safe as it is guaranteed to have a valid address.');
((cy_fn_dfu_jump_ptr_t) address) ();
/* This function does not return */
for(;;)
{
}
}
/*******************************************************************************
* Function Name: Cy_DFU_SwitchToApp
****************************************************************************//**
*
* This function switches to the application through the jump instruction.
* The function should be used when an application switching must be done without
* a software reset. Possible reason is a need to enable some peripheral during
* and after the application switching. In other case use \ref Cy_DFU_ExecuteApp().
*
* Before calling this function, ensure all the peripherals and bus masters are
* in a known state. User is responsible to disable peripherals and to set MCU
* internal state before or after an application switching.
*
* \note It is assumed appId is a valid application number.
*
* \param appId An application number of the application to switch to.
*
* \return It doesn't return if succeeds. If failed, returns the status code.
* See \ref cy_en_dfu_status_t.
*
*******************************************************************************/
cy_en_dfu_status_t Cy_DFU_SwitchToApp(uint32_t appId)
{
uint32_t startAddress;
cy_en_dfu_status_t status;
CY_ASSERT(appId < CY_DFU_MAX_APPS);
status = Cy_DFU_GetAppMetadata(appId, &startAddress, NULL);
if (status == CY_DFU_SUCCESS)
{
#if(CY_DFU_APP_FORMAT == CY_DFU_SIMPLIFIED_APP)
uint32_t offsetVt = ((uint32_t *)(startAddress + SIMPLIFIED_APP_APPSIZE_SIZE))[0];
startAddress += SIMPLIFIED_APP_APPSIZE_SIZE + offsetVt;
#elif(CY_DFU_APP_FORMAT == CY_DFU_CYPRESS_APP)
uint32_t offsetVt = ((uint32_t *)startAddress)[CYPRESS_APP_VTOFFSET_OFFSET_UINT32];
startAddress += CYPRESS_APP_VTOFFSET_OFFSET_BYTES + offsetVt;
#else
/* Cypress Basic Application Format (CyBAF) */
#endif
uint32_t stackPointer = ((uint32_t *)startAddress)[0]; /* The Stack Pointer of the app to switch to */
uint32_t resetHandler = ((uint32_t *)startAddress)[1]; /* Reset_Handler() address */
SwitchToApp(stackPointer, resetHandler);
}
return (status);
}
/*******************************************************************************
* Function Name: ElfSymbolToAddr
****************************************************************************//**
*
* This function is used to convert an ELF file symbol address to uint32_t. \n
* This is safer than casting a symbol address to an integer because the
* function does not produce a MISRA warning at the call side.
* Also, a function call is more readable and easier to search with the text
* editor.
*
* \param symbol The address of the ELF file symbol to get the uint32_t value for.
*
* \return The address of the ELF file symbol as uint32_t.
*
*******************************************************************************/
static uint32_t ElfSymbolToAddr(void volatile const *symbol)
{
CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.6','Casting pointer to a int is safe as symbol must be valid 4-byte value.');
return (uint32_t) symbol;
}
/*******************************************************************************
* Function Name: Cy_DFU_GetAppMetadata
****************************************************************************//**
*
* Reads application metadata to \c verifyAddress and \c verifySize.
* The metadata is supposed to be located in internal flash.
*
* This is a weak function and the user may override it in the user's code by
* providing a function with the same name.
* This allows the user to place metadata in any NVM.
*
* \note It is assumed appId is a valid application number.
*
* \param appId The application number.
* \param verifyAddress The pointer to a variable where an application
* verified area start address is stored.
* \param verifySize The pointer to a variable where a size of verified
* application area is stored.
*
* \return See \ref cy_en_dfu_status_t.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_GetAppMetadata(uint32_t appId, uint32_t *verifyAddress, uint32_t *verifySize)
{
cy_en_dfu_status_t status = CY_DFU_SUCCESS;
CY_ASSERT(appId < CY_DFU_MAX_APPS);
uint32_t *ptr = (uint32_t*) ( ElfSymbolToAddr(&__cy_boot_metadata_addr) + (appId * METADATA_BYTES_PER_APP) );
if (verifyAddress != NULL)
{
*verifyAddress = ptr[0];
}
if (verifySize != NULL)
{
*verifySize = ptr[1];
}
return (status);
}
/* Use PDL Hardware Crypto API */
#if ((CY_DFU_OPT_CRYPTO_HW != 0) && (CY_DFU_APP_FORMAT == CY_DFU_BASIC_APP))
/*******************************************************************************
* Function Name: ComputeSha1
****************************************************************************//**
*
* This function computes SHA1 for the message.
*
* \note Ensure the Crypto block is properly initialized
* and \ref CY_DFU_OPT_CRYPTO_HW is set.
*
* \param address The pointer to a buffer containing data to compute
* the checksum for. \n
* It must be 4-byte aligned.
* \param length The number of bytes in the buffer to compute SHA1 for.
* \param result The pointer to a buffer to store the SHA1 output.
* It must be 4-byte aligned.
*
* \return
* - true - If calculation is successful.
* - false - If calculation is unsuccessful.
*
*******************************************************************************/
static bool ComputeSha1(uint32_t address, uint32_t length, uint8_t *result)
{
cy_stc_crypto_context_sha_t cryptoShaContext;
cy_en_crypto_status_t cryptoStatus;
bool statusOk = true;
cryptoStatus = Cy_Crypto_Enable();
if (cryptoStatus == CY_CRYPTO_SUCCESS)
{
CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.3','Casting result operand to uint32_t is safe as calling function use uint32_t pointer.');
cryptoStatus = Cy_Crypto_Sha_Run((uint32_t *)address, length, (uint32_t *)result, CY_CRYPTO_MODE_SHA1,
&cryptoShaContext);
if (cryptoStatus == CY_CRYPTO_SUCCESS)
{
/* Waiting for SHA1 calculation is finished. */
cryptoStatus = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
}
(void) Cy_Crypto_Disable();
}
if (cryptoStatus != CY_CRYPTO_SUCCESS)
{
statusOk = false;
}
return (statusOk);
}
#endif /* ((CY_DFU_OPT_CRYPTO_HW != 0) && (CY_DFU_APP_FORMAT == CY_DFU_BASIC_APP)) */
#if(CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP)
#if(CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST)
/*******************************************************************************
* Function Name: VerifySecureAppShort
****************************************************************************//**
*
* This function reports whether or not the specified application is valid.
*
* \param verifyStartAddr The start address of the application to verify.
* \param verifyLength The length of the application to verify.
* \param signatureAddr The address of the application signature.
*
* \return
* - true - If the application is valid.
* - false - If the application is invalid
*
*******************************************************************************/
static bool VerifySecureAppShort(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr)
{
uint32_t publicKeyAddr = ( (uint32_t)& SFLASH->PUBLIC_KEY[0] ) + PUBLIC_KEY_OFFSET;
Cy_FB_VerifyApp_t Cy_FB_VerifyApp = (Cy_FB_VerifyApp_t) (*(uint32_t *) VERIFY_APP_TABLE_ADDR);
return Cy_FB_VerifyApp(verifyStartAddr, verifyLength, signatureAddr, publicKeyAddr );
}
#else
/*******************************************************************************
* Function Name: VerifySecureAppFull
****************************************************************************//**
*
* This function reports whether or not the specified application, TOC, and key
* are valid.
*
* \param verifyStartAddr The start address of the application to verify.
* \param verifyLength The length of the application to verify.
* \param signatureAddr The address of the application signature.
*
* \return
* - true - If the application is valid.
* - false - If the application is invalid.
*
*******************************************************************************/
static bool VerifySecureAppFull(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr)
{
Cy_FB_ValidateToc_t Cy_FB_ValidateToc = (Cy_FB_ValidateToc_t) (*(uint32_t *) VALIDATE_TOC_TABLE_ADDR);
Cy_FB_VerifyApp_t Cy_FB_VerifyApp = (Cy_FB_VerifyApp_t) (*(uint32_t *) VERIFY_APP_TABLE_ADDR );
Cy_FB_IsValidKey_t Cy_FB_IsValidKey = (Cy_FB_IsValidKey_t) (*(uint32_t *) IS_VALID_KEY_TABLE_ADDR);
bool status = true;
uint32_t tocAddr = Cy_FB_ValidateToc((uint32_t)& SFLASH->TOC2_OBJECT_SIZE);
if ((tocAddr == TOC_EMPTY) || (tocAddr == TOC_INVALID))
{
status = false;
}
else
{
uint32_t publicKeyAddr = *(uint32_t *)(tocAddr + (sizeof(uint32_t) * PUBLIC_KEY_IDX))
+ PUBLIC_KEY_OFFSET;
status = Cy_FB_IsValidKey(tocAddr, publicKeyAddr);
if (status)
{
status = Cy_FB_VerifyApp(verifyStartAddr, verifyLength, signatureAddr, publicKeyAddr );
}
}
return (status);
}
#endif /* CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST */
/*******************************************************************************
* Function Name: VerifySecureApp
****************************************************************************//**
*
* This function reports whether or not the specified application is valid.
*
* \param verifyStartAddr The start address of the application to verify.
* \param verifyLength The length of the application to verify.
* \param signatureAddr The address of the application signature.
*
* \return
* - true - If the application is valid.
* - false - If the application is invalid.
*
*******************************************************************************/
static bool VerifySecureApp(uint32_t verifyStartAddr, uint32_t verifyLength, uint32_t signatureAddr)
{
#if(CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST)
return VerifySecureAppShort(verifyStartAddr, verifyLength, signatureAddr);
#else
return VerifySecureAppFull (verifyStartAddr, verifyLength, signatureAddr);
#endif /* CY_DFU_SEC_APP_VERIFY_TYPE == CY_DFU_VERIFY_FAST */
}
#endif/*(CY_DFU_APP_FORMAT != CY_DFU_BASIC_APP)*/
#endif /* CY_DFU_FLOW == CY_DFU_BASIC_FLOW */
/** \endcond*/
/*******************************************************************************
* Function Name: Cy_DFU_ValidateApp
****************************************************************************//**
*
* This function reports whether or not metadata and the specified application is
* valid. It checks:
* - checksum for applications without format;
* - application signature for Cypress Standard User Application format.
*
* This is a weak function and the user may override it in the user's code by
* providing a function with the same name.
*
* \warning This function do nothing for MCUBoot flow
*
* \note It is assumed appId is a valid application number.
*
* \param appId The application number of the application to be validated.
* \param params The pointer to a DFU parameters structure.
* See \ref cy_stc_dfu_params_t .
*
* \return See \ref cy_en_dfu_status_t.
* - \ref CY_DFU_SUCCESS If the application is valid.
* - \ref CY_DFU_ERROR_VERIFY If the application is invalid.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_ValidateApp(uint32_t appId, cy_stc_dfu_params_t *params)
{
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
uint32_t appVerifyStartAddress;
uint32_t appVerifySize;
(void)params;
CY_ASSERT(appId < CY_DFU_MAX_APPS);
cy_en_dfu_status_t status = Cy_DFU_GetAppMetadata(appId, &appVerifyStartAddress, &appVerifySize);
if (status == CY_DFU_SUCCESS)
{
#if(CY_DFU_APP_FORMAT == CY_DFU_CYPRESS_APP)
status = (VerifySecureApp(appVerifyStartAddress, appVerifySize, appVerifyStartAddress + appVerifySize))?
CY_DFU_SUCCESS : CY_DFU_ERROR_VERIFY;
#elif (CY_DFU_APP_FORMAT == CY_DFU_SIMPLIFIED_APP)
status = (VerifySecureApp(appVerifyStartAddress, appVerifySize, appVerifyStartAddress - RSA_CHECKSUM_LENGTH))?
CY_DFU_SUCCESS : CY_DFU_ERROR_VERIFY;
#else
#if(CY_DFU_OPT_CRYPTO_HW != 0)
uint32_t sha1buf[SHA1_BUF_SIZE_UINT32];
uint32_t appFooterAddress = appVerifyStartAddress + appVerifySize;
if (ComputeSha1(appVerifyStartAddress, appVerifySize, (uint8_t*)&sha1buf))
{
CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 11.6','Casting int to pointer is safe as it has valid address defined in linker script.');
status = (memcmp((const void *)sha1buf, (const void *)appFooterAddress, SHA1_CHECKSUM_LENGTH) == 0)?
CY_DFU_SUCCESS : CY_DFU_ERROR_VERIFY;
}
else
{
status = CY_DFU_ERROR_VERIFY;
}
#else
uint32_t appCrc = Cy_DFU_DataChecksum((uint8_t *)appVerifyStartAddress, appVerifySize, params);
uint32_t appFooterAddress = (appVerifyStartAddress + appVerifySize);
status = (*(uint32_t*)appFooterAddress == appCrc) ? CY_DFU_SUCCESS : CY_DFU_ERROR_VERIFY;
#endif/* (CY_DFU_OPT_CRYPTO_HW != 0) */
#endif /* (CY_DFU_APP_FORMAT == CY_DFU_CYPRESS_APP) */
}
return (status);
#else
CY_UNUSED_PARAMETER(appId);
CY_UNUSED_PARAMETER(params);
return CY_DFU_SUCCESS;
#endif /*CY_DFU_FLOW == CY_DFU_BASIC_FLOW*/
}
/** \cond INTERNAL */
#if CY_DFU_FLOW == CY_DFU_BASIC_FLOW
/*******************************************************************************
* Function Name: Cy_DFU_GetRunningApp
****************************************************************************//**
*
* This function reports the application number of the currently running
* application.
*
* \return application number
*
*******************************************************************************/
uint32_t Cy_DFU_GetRunningApp(void)
{
return ElfSymbolToAddr(&__cy_app_id);
}
/*******************************************************************************
* Function Name: Cy_DFU_CopyApp
****************************************************************************//**
*
* This function copies an application from a temporary location in flash to its
* destination location in flash. This function is typically called when updating
* an application used as part of an update process, for example updating
* a BLE stack.
* \note This API is only for demonstration purpose, use it only when copying
* from internal flash to internal flash. For other user cases, implement a
* custom, more general function.
*
* \param destAddress The start address of the application to copy to.
* \param srcAddress The start address of the copy of the application to be
* copied.
* \param length The number of bytes to copy.
* \param rowSize The size of a flash row in bytes.
* \param params The pointer to a DFU parameters structure.
* See \ref cy_stc_dfu_params_t .
*
* \return See \ref cy_en_dfu_status_t.
*
*******************************************************************************/
cy_en_dfu_status_t Cy_DFU_CopyApp(uint32_t destAddress, uint32_t srcAddress, uint32_t length,
uint32_t rowSize, cy_stc_dfu_params_t *params)
{
cy_en_dfu_status_t status = CY_DFU_ERROR_UNKNOWN;
uint32_t writeAddr = destAddress;
uint32_t readAddr = srcAddress;
uint32_t endAddress = destAddress + length;
while (writeAddr < endAddress)
{
status = Cy_DFU_ReadData(readAddr, rowSize, CY_DFU_IOCTL_READ, params);
if (status == CY_DFU_SUCCESS)
{
status = Cy_DFU_WriteData(writeAddr, rowSize, CY_DFU_IOCTL_WRITE, params);
}
if (status != CY_DFU_SUCCESS)
{
break;
}
writeAddr += rowSize;
readAddr += rowSize;
}
return (status);
}
/*******************************************************************************
* Function Name: Cy_DFU_OnResetApp0
****************************************************************************//**
*
* This function is used in an App0 firmware image in Reset_Handler() only.
* Checks if switching to the other application is scheduled with
* \ref Cy_DFU_ExecuteApp(). \n
* If the switch is scheduled, then it validates the application and transfers
* control to it.
*
*******************************************************************************/
void Cy_DFU_OnResetApp0(void)
{
/* Set cy_dfu_appId to ZERO under a non-software reset. This means
* that the DFU application is scheduled - the initial clean state.
* The value of cy_dfu_appId is valid only under a software reset.
*/
if (Cy_SysLib_GetResetReason() != CY_SYSLIB_RESET_SOFT)
{
cy_dfu_appId = 0U;
}
else
{
if ((cy_dfu_appId != 0U) && (cy_dfu_appId < CY_DFU_MAX_APPS))
{
(void) Cy_DFU_SwitchToApp((uint32_t) cy_dfu_appId);
}
}
}
#endif /* CY_DFU_FLOW == CY_DFU_BASIC_FLOW */
/** \endcond*/
/*******************************************************************************
* Function Name: Cy_DFU_ReadData
****************************************************************************//**
*
* This function must be implemented in the user's code.
*
* Reads \c buffer from flash, QSPI flash, or any other external memory type with
* custom pre and post read commands.
*
* \param address The address from where to read data, must be aligned to
* a flash row, QSPI flash page, etc.
* \param length The length in bytes of data to read, must be multiple of
* a flash row, QSPI flash page, etc.
* \param ctl Additional features of the read function:
* - CY_DFU_IOCTL_READ - Only read.
* - CY_DFU_IOCTL_COMPARE - Compare the data in the buffer with the data in
* memory.
* - CY_DFU_IOCTL_BHP - Decrypt data before comparing the buffer with
* memory,
* if the DFU Host provided encrypted data.
* \param params The pointer to a DFU parameters structure.
* See \ref cy_stc_dfu_params_t .
* \return See \ref cy_en_dfu_status_t
* - CY_DFU_SUCCESS - If successful.
* - CY_DFU_ERROR_LENGTH if \c The length value is invalid.
* - CY_DFU_ERROR_ADDRESS if \c The address is invalid.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_ReadData (uint32_t address, uint32_t length, uint32_t ctl,
cy_stc_dfu_params_t *params)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
(void) address;
(void) length;
(void) ctl;
(void) params;
return (CY_DFU_SUCCESS);
}
/*******************************************************************************
* Function Name: Cy_DFU_WriteData
****************************************************************************//**
*
* This function must be implemented in the user's code.
*
* Writes the \c buffer to flash, QSPI flash, or any other external memory type
* with custom pre and post write commands.
*
* \param address The address to write data to, must be aligned to a flash
* row, QSPI flash page, etc.
* \param length The length in bytes of data to be written, must be multiple
* of a flash row, QSPI flash page, etc.
* \param ctl Additional features of the write function:
* - CY_DFU_IOCTL_WRITE - Only write.
* - CY_DFU_IOCTL_ERASE - Erase the sector, the sector size can be bigger
* than the size of the page to write.
* - CY_DFU_IOCTL_BHP - Decrypt data before writing to memory, if
* the DFU Host provided encrypted data.
* \param params The pointer to a DFU parameters structure.
* See \ref cy_stc_dfu_params_t .
* \return See \ref cy_en_dfu_status_t.
* - CY_DFU_SUCCESS - If successful.
* - CY_DFU_ERROR_LENGTH if \c The length value is invalid.
* - CY_DFU_ERROR_ADDRESS if \c The address is invalid.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_WriteData(uint32_t address, uint32_t length, uint32_t ctl,
cy_stc_dfu_params_t *params)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
(void) address;
(void) length;
(void) ctl;
(void) params;
return (CY_DFU_SUCCESS);
}
/*******************************************************************************
* Function Name: Cy_DFU_TransportRead
****************************************************************************//**
* This function must be implemented in the user's code.
*
* This function receives a command packet from the DFU Host via the
* communication channel. The function waits for a timeout until all bytes are
* received.
*
* \param buffer The pointer to a buffer to store a received command.
* \param size The number of bytes to read.
* \param count The pointer to the variable that contains the number of received
* bytes.
* \param timeout The time to wait before the function returns because of a
* timeout, in milliseconds.
*
* \return The status of the transmit operation:
* - CY_DFU_SUCCESS - If successful.
* - CY_DFU_ERROR_TIMEOUT - If no data is received.
* - See \ref cy_en_dfu_status_t.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_TransportRead(uint8_t buffer[], uint32_t size, uint32_t *count,
uint32_t timeout)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
(void) buffer;
(void) size;
(void) count;
(void) timeout;
return (CY_DFU_SUCCESS);
}
/*******************************************************************************
* Function Name: Cy_DFU_TransportWrite
****************************************************************************//**
* This function must be implemented in the user's code.
*
* This function transmits a response packet to the DFU host via the
* communication channel. The function waits for a timeout until all bytes are
* sent.
*
* \param buffer The pointer response packet buffer.
* \param size The number of bytes to transmit.
* \param count The pointer to the actual number of transmitted bytes.
* \param timeout The time to wait before the function returns because of a
* timeout, in milliseconds
*
* \return See \ref cy_en_dfu_status_t.
* The status of the transmit operation:
* - CY_DFU_SUCCESS - If successful.
* - CY_DFU_ERROR_TIMEOUT - If no data is transmitted.
*
*******************************************************************************/
__WEAK cy_en_dfu_status_t Cy_DFU_TransportWrite(uint8_t buffer[], uint32_t size, uint32_t *count,
uint32_t timeout)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
(void) buffer;
(void) size;
(void) count;
(void) timeout;
return (CY_DFU_SUCCESS);
}
/*******************************************************************************
* Function Name: Cy_DFU_TransportReset
****************************************************************************//**
*
* This function must be implemented in the user's code. \n
* Resets the communication interface with clearing buffers, offsets, length,
* etc.
*
*******************************************************************************/
__WEAK void Cy_DFU_TransportReset(void)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
}
/*******************************************************************************
* Function Name: Cy_DFU_TransportStart
****************************************************************************//**
*
* This function must be implemented in the user's code. \n
* Starts the communication interface through which updating will be working.
*
* \param transport defines transport interface to use. See
* \ref cy_en_dfu_transport_t for available options
*
*******************************************************************************/
__WEAK void Cy_DFU_TransportStart(cy_en_dfu_transport_t transport)
{
/*
* This function does nothing, weak implementation.
* The purpose of this code is to disable compiler warnings for Non-optimized
* builds which do not remove unused functions and require them for the
* completeness of the linking step.
*/
CY_UNUSED_PARAMETER(transport);
}
/*******************************************************************************
* Function Name: Cy_DFU_TransportStop
****************************************************************************//**
*
* This function must be implemented in the user's code. \n
* Stops the communication interface through which updating will be working.
*
*******************************************************************************/
__WEAK void Cy_DFU_TransportStop(void)