1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #define SWAP(x) \
22     (((x) >> 24) | (((x)&0x00FF0000) >> 8) | (((x)&0x0000FF00) << 8) | (((x)&0x000000FF) << 24))
23 
24 typedef enum {
25     ROM_A1_VERSION = 0x01000000,
26     ROM_A2_VERSION = 0x01000000,
27 } enum_rom_version_t;
28 
29 #ifdef __SLA_FWK__
30 /***** Includes *****/
31 #include <stdio.h>
32 #include <stdint.h>
33 
34 extern unsigned int _start_SWAP;
35 extern unsigned int _SLA_Size_SWAP;
36 
37 typedef enum {
38     MagicH = 0x48495357,
39     MagicL = 0x45444744,
40 } enum_magic_t;
41 
42 typedef struct {
43     enum_magic_t MagicHigh; //> SLA Header magic
44     enum_magic_t MagicLow; //> SLA Header magic
45 } magic_t;
46 
47 typedef struct {
48     magic_t Magic;
49     enum_rom_version_t RomVersion; //> ROM version
50     unsigned int LoadAddr; //> Relocation address.
51     unsigned int SLA_CodeSize; //> SLA code size in bytes
52     unsigned int *JumpAddr; //> Rom code will jump at this address
53     unsigned int ArgSize; //> Size of the Argument
54     unsigned int AppVersionNumber; //> Version of this application
55 } flash_app_header_t;
56 
57 __attribute__((section(".sb_sla_header"))) __attribute__((__used__))
58 const flash_app_header_t sb_header = {
59     .Magic =
60         {
61             .MagicHigh = SWAP(MagicH),
62             .MagicLow  = SWAP(MagicL),
63         },
64 
65 #if defined(MAX32672_A2)
66     .RomVersion = SWAP(ROM_A2_VERSION),
67 #else
68     .RomVersion = SWAP(ROM_A1_VERSION),
69 #endif
70     .LoadAddr         = SWAP(0x10000000),
71     .SLA_CodeSize     = (unsigned int)&_SLA_Size_SWAP, // Trick to get constant defined at link time
72     .JumpAddr         = &_start_SWAP,
73     .ArgSize          = 0,
74     .AppVersionNumber = SWAP(0x01000000), // 0xAABBCCCC for version AA.BB.CCCC
75 };
76 
77 //__attribute__ ((section(".sb_sla_trailer"))) __attribute__ ((__used__))
78 //const unsigned int dummy_signature=0xCAFEFADE;
79 
80 #endif //__SLA_FWK__
81 
82 #ifdef __SCPA_FWK__
83 /** Global declarations */
84 
85 #include <string.h>
86 
87 typedef enum {
88     MagicH = 0xDEADBEEF,
89     MagicL = 0xCAFEFADE,
90 } enum_magic_t;
91 
92 typedef struct {
93     enum_magic_t MagicHigh; //> SLA Header magic
94     enum_magic_t MagicLow; //> SLA Header magic
95 } magic_t;
96 
97 typedef int (*__scpa_write_t)(unsigned int dest, unsigned int length, unsigned char *p_src);
98 typedef int (*__scpa_erase_t)(unsigned int dest, unsigned int length);
99 
100 /** Generic Plugin Operations */
101 typedef struct {
102     __scpa_write_t write; //> Write to memory
103     __scpa_write_t compare; //> Compare memory data
104     __scpa_erase_t erase; //> Erase memory
105 } scpa_ops_t;
106 
107 typedef struct {
108     magic_t Magic;
109     enum_rom_version_t RomVersion; //> ROM version
110     unsigned int mem_base_addr; //> Base address of memory targetted by applet
111     unsigned int mem_size; //> Size of this memory
112     scpa_ops_t ops; //> Operations of the SCP Applet
113 } scpa_header_t;
114 
115 int start_scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src);
116 int start_scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src);
117 int start_scpa_erase(unsigned int dest, unsigned int length);
118 
119 int __attribute__((weak)) scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src);
120 int __attribute__((weak))
121 scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src);
122 int __attribute__((weak)) scpa_erase(unsigned int dest, unsigned int length);
123 
124 extern unsigned int __bss_start__;
125 extern unsigned int __bss_end__;
126 extern unsigned int __bss_magic__;
127 
128 #ifndef SCPA_MEM_BASE_ADDR
129 #define SCPA_MEM_BASE_ADDR 0xC0000000
130 #warning 'SCPA_MEM_BASE_ADDR not defined using default value 0xC0000000'
131 #endif
132 
133 #ifndef SCPA_MEM_SIZE
134 #define SCPA_MEM_SIZE 1024
135 #warning 'SCPA_MEM_SIZE not defined using default value 1024'
136 #endif
137 
138 unsigned int __attribute__((section(".scpa_init"))) Magic_bss = 0xABADCAFE;
139 
140 __attribute__((section(".scpa_header"))) __attribute__((__used__))
141 const scpa_header_t scpa_header = {
142     .Magic =
143         {
144             .MagicHigh = MagicH,
145             .MagicLow  = MagicL,
146         },
147 #ifdef MAX32672_A1
148     .RomVersion = SWAP(ROM_A1_VERSION),
149 #elif defined(MAX32672_A2)
150     .RomVersion = SWAP(ROM_A2_VERSION),
151 #else
152 #error "Please Select a chip ROM revision"
153 #endif
154     .mem_base_addr = SCPA_MEM_BASE_ADDR,
155     .mem_size      = SCPA_MEM_SIZE,
156     .ops =
157         {
158             .write   = (__scpa_write_t)start_scpa_write,
159             .compare = (__scpa_write_t)start_scpa_compare,
160             .erase   = (__scpa_erase_t)start_scpa_erase,
161         },
162 };
163 
164 int __attribute__((section(".scpa_ops")))
start_scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)165 start_scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
166 {
167     volatile unsigned int bss_size =
168         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
169     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
170     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
171 
172     // Automatic Code for bss init
173     if (*p_magic == 0xABADCAFE) {
174         memset((void *)p_bss, 0x00, bss_size);
175         *p_magic = 0x0;
176     }
177     return scpa_write(dest, length, p_src);
178 }
179 
180 int __attribute__((section(".scpa_ops")))
start_scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)181 start_scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
182 {
183     volatile unsigned int bss_size =
184         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
185     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
186     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
187 
188     // Automatic Code for bss init
189     if (*p_magic == 0xABADCAFE) {
190         memset((void *)p_bss, 0x00, bss_size);
191         *p_magic = 0x0;
192     }
193     return scpa_compare(dest, length, p_src);
194 }
195 
start_scpa_erase(unsigned int dest,unsigned int length)196 int __attribute__((section(".scpa_ops"))) start_scpa_erase(unsigned int dest, unsigned int length)
197 {
198     volatile unsigned int bss_size =
199         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
200     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
201     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
202 
203     // Automatic Code for bss init
204     if ((*p_magic == 0xABADCAFE)) {
205         memset((void *)p_bss, 0x00, bss_size);
206         *p_magic = 0x0;
207     }
208     return scpa_erase(dest, length);
209 }
210 
211 int __attribute__((section(".scpa_ops")))
scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)212 scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
213 {
214     (void)dest;
215     (void)length;
216     (void)p_src;
217     return 0;
218 }
219 
220 int __attribute__((section(".scpa_ops")))
scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)221 scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
222 {
223     (void)dest;
224     (void)length;
225     (void)p_src;
226     return 0;
227 }
228 
scpa_erase(unsigned int dest,unsigned int length)229 int __attribute__((section(".scpa_ops"))) scpa_erase(unsigned int dest, unsigned int length)
230 {
231     (void)dest;
232     (void)length;
233     return 0;
234 }
235 
Reset_Handler(void)236 void Reset_Handler(void) {}
237 
238 #endif //__SCPA_FWK__
239