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 } enum_rom_version_t;
27 
28 #ifdef __SLA_FWK__
29 /***** Includes *****/
30 #include <stdio.h>
31 #include <stdint.h>
32 
33 extern unsigned int _start_SWAP;
34 extern unsigned int _SLA_Size_SWAP;
35 
36 typedef enum {
37     MagicH = 0x44495357,
38     /* NOTE: The 0xF nibble means something called stack_method=1.
39  * If set to 0xF, the ROM fetches SP and PC immediately after the header at offset 0x20.
40  * If set to 0x4, the ROM uses the PC in the header at offset 0x14.
41  */
42     /*  MagicH = 0xF6495357,*/
43     MagicL = 0x45444744,
44 } enum_magic_t;
45 
46 typedef struct {
47     enum_magic_t MagicHigh; //> SLA Header magic
48     enum_magic_t MagicLow; //> SLA Header magic
49 } magic_t;
50 
51 typedef struct {
52     magic_t Magic;
53     enum_rom_version_t RomVersion; //> ROM version
54     unsigned int LoadAddr; //> Relocation address.
55     unsigned int SLA_CodeSize; //> SLA code size in bytes
56     unsigned int *JumpAddr; //> Rom code will jump at this address
57     unsigned int ArgSize; //> Size of the Argument
58     unsigned int AppVersionNumber; //> Version of this application
59 } flash_app_header_t;
60 
61 __attribute__((section(".sb_sla_header"))) __attribute__((__used__))
62 const flash_app_header_t sb_header = {
63     .Magic =
64         {
65             .MagicHigh = SWAP(MagicH),
66             .MagicLow  = SWAP(MagicL),
67         },
68 
69     .RomVersion       = SWAP(ROM_A1_VERSION),
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 MAX32570_A1
148     .RomVersion = SWAP(ROM_A1_VERSION),
149 #else
150 #error "Please Select a chip ROM revision"
151 #endif
152     .mem_base_addr = SCPA_MEM_BASE_ADDR,
153     .mem_size      = SCPA_MEM_SIZE,
154     .ops =
155         {
156             .write   = (__scpa_write_t)start_scpa_write,
157             .compare = (__scpa_write_t)start_scpa_compare,
158             .erase   = (__scpa_erase_t)start_scpa_erase,
159         },
160 };
161 
162 int __attribute__((section(".scpa_ops")))
start_scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)163 start_scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
164 {
165     volatile unsigned int bss_size =
166         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
167     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
168     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
169 
170     // Automatic Code for bss init
171     if (*p_magic == 0xABADCAFE) {
172         memset((void *)p_bss, 0x00, bss_size);
173         *p_magic = 0x0;
174     }
175     return scpa_write(dest, length, p_src);
176 }
177 
178 int __attribute__((section(".scpa_ops")))
start_scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)179 start_scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
180 {
181     volatile unsigned int bss_size =
182         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
183     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
184     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
185 
186     // Automatic Code for bss init
187     if (*p_magic == 0xABADCAFE) {
188         memset((void *)p_bss, 0x00, bss_size);
189         *p_magic = 0x0;
190     }
191     return scpa_compare(dest, length, p_src);
192 }
193 
start_scpa_erase(unsigned int dest,unsigned int length)194 int __attribute__((section(".scpa_ops"))) start_scpa_erase(unsigned int dest, unsigned int length)
195 {
196     volatile unsigned int bss_size =
197         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
198     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
199     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
200 
201     // Automatic Code for bss init
202     if ((*p_magic == 0xABADCAFE)) {
203         memset((void *)p_bss, 0x00, bss_size);
204         *p_magic = 0x0;
205     }
206     return scpa_erase(dest, length);
207 }
208 
209 int __attribute__((section(".scpa_ops")))
scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)210 scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
211 {
212     (void)dest;
213     (void)length;
214     (void)p_src;
215     return 0;
216 }
217 
218 int __attribute__((section(".scpa_ops")))
scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)219 scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
220 {
221     (void)dest;
222     (void)length;
223     (void)p_src;
224     return 0;
225 }
226 
scpa_erase(unsigned int dest,unsigned int length)227 int __attribute__((section(".scpa_ops"))) scpa_erase(unsigned int dest, unsigned int length)
228 {
229     (void)dest;
230     (void)length;
231     return 0;
232 }
233 
Reset_Handler(void)234 void Reset_Handler(void) {}
235 
236 #endif //__SCPA_FWK__
237