1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #include "tool_msg.h"
5 #include "reboot_param.h"
6 #include "norflash_cfg.h"
7 #include "hal_cmu.h"
8 
9 extern const char sys_build_info[];
10 
11 #ifdef USER_SECURE_BOOT
12 extern const unsigned int system_info;
13 #endif
14 
15 // -----------------------------------------------------------
16 // Boot struct and code sig struct
17 // -----------------------------------------------------------
18 #if defined(PROGRAMMER_INFLASH)
19 #undef PROGRAMMER
20 #endif
21 
22 #define BOOT_STRUCT_LOC                 __attribute((section(".boot_struct")))
23 
24 #ifdef USER_SECURE_BOOT
25 #define DEFAULT_BUILD_INFO              ((uint32_t)&system_info)
26 #else
27 #define DEFAULT_BUILD_INFO              ((uint32_t)sys_build_info)
28 #endif
29 
30 #define DEFAULT_CODE_SIG                \
31     {                                   \
32         .code_size = 0,                 \
33         .sig_len = SIG_LEN,             \
34     }
35 
36 #ifdef SIMU
37 #define DEFAULT_NORFLASH_SAMDLY         0x1
38 #define DEFAULT_NORFLASH_MOD_CLK        HAL_CMU_FREQ_26M
39 #define DEFAULT_NORFLASH_RDCMD          0x03
40 #else
41 #define DEFAULT_NORFLASH_SAMDLY         0x2
42 // Select 26M for 40M crystal case
43 #define DEFAULT_NORFLASH_MOD_CLK        HAL_CMU_FREQ_104M
44 // Below 50M: 0x03, Above 50M: 0x0B
45 #define DEFAULT_NORFLASH_RDCMD          0x0B
46 #endif
47 
48 #define DEFAULT_NORFLASH_CFG            \
49     {                                   \
50         .neg_phase = 0,                 \
51         .pos_neg   = 0,                 \
52         .cmdquad   = 0,                 \
53         .div       = 0x2,               \
54         .samdly    = DEFAULT_NORFLASH_SAMDLY, \
55         .dualmode  = 1,                 \
56         .holdpin   = 0,                 \
57         .wprpin    = 0,                 \
58         .quadmode  = 0,                 \
59         .mod_clk   = DEFAULT_NORFLASH_MOD_CLK, \
60         .spiruen   = 0,                 \
61         .spirden   = 0,                 \
62         .dualiocmd = 0xBB,              \
63         .rdcmd     = DEFAULT_NORFLASH_RDCMD, \
64         .frdcmd    = 0x0B,              \
65         .qrdcmd    = 0xEB,              \
66     }
67 
68 #ifdef SECURE_BOOT_V1
69 
70 #ifdef PROGRAMMER
71 
72 struct programmer_boot_struct_t {
73     struct boot_struct_v1_t boot_struct;
74     struct code_sig_struct_t code_sig_struct;
75 };
76 
77 const struct programmer_boot_struct_t BOOT_STRUCT_LOC programmer_boot_struct = {
78     .boot_struct = {
79         .hdr = {
80             .magic = BOOT_MAGIC_NUMBER,
81             .security = 1,
82             .hash_type = BOOT_HASH_TYPE_SHA256,
83             .key_type = BOOT_KEY_TYPE_RSA2048,
84             .key_len = KEY_LEN,
85             .sig_len = SIG_LEN,
86             .build_info_start = ((uint32_t)sys_build_info),
87         },
88     },
89     .code_sig_struct = DEFAULT_CODE_SIG,
90 };
91 
92 #elif defined(SECURE_BOOT)
93 
94 struct secure_boot_struct_t {
95     struct boot_struct_v1_t boot_struct;
96     struct code_sig_struct_t code_sig_struct;
97     struct norflash_cfg_struct_t norflash_cfg;
98 };
99 
100 const struct secure_boot_struct_t BOOT_STRUCT_LOC secure_boot_struct = {
101     .boot_struct = {
102         .hdr = {
103             .magic = ~0UL,
104             .security = 1,
105             .hash_type = BOOT_HASH_TYPE_SHA256,
106             .key_type = BOOT_KEY_TYPE_RSA2048,
107             .key_len = KEY_LEN,
108             .sig_len = SIG_LEN,
109             .build_info_start = DEFAULT_BUILD_INFO,
110         },
111     },
112     .code_sig_struct = DEFAULT_CODE_SIG,
113     .norflash_cfg = DEFAULT_NORFLASH_CFG,
114 };
115 
116 #else
117 
118 const struct boot_hdr_v1_t BOOT_STRUCT_LOC boot_struct = {
119     .magic = ~0UL,
120     .security = 0,
121     .build_info_start = DEFAULT_BUILD_INFO,
122 };
123 
124 #endif
125 
126 #else // !SECURE_BOOT_V1
127 
128 #ifdef PROGRAMMER
129 
130 union programmer_boot_struct_t {
131     // To keep compatible with the old download tools when downloadig non-secure images
132     struct {
133         struct boot_struct_v1_t dummy;
134         struct code_sig_struct_t code_sig_struct;
135     } s_v1;
136     struct {
137         struct boot_struct_t boot_struct;
138         struct code_sig_struct_t code_sig_struct;
139     } s;
140 };
141 
142 const union programmer_boot_struct_t BOOT_STRUCT_LOC programmer_boot_struct = {
143     .s = {
144         .boot_struct = {
145             .hdr = {
146                 .magic = BOOT_MAGIC_NUMBER,
147                 .security = 1,
148                 .version = BOOT_STRUCT_VERSION,
149                 .build_info_start = ((uint32_t)sys_build_info),
150             },
151         },
152         .code_sig_struct = DEFAULT_CODE_SIG,
153     },
154 };
155 
156 #elif defined(SECURE_BOOT)
157 
158 struct secure_boot_struct_t {
159     struct boot_struct_t boot_struct;
160     struct code_sig_struct_t code_sig_struct;
161     struct norflash_cfg_struct_t norflash_cfg;
162 };
163 
164 const struct secure_boot_struct_t BOOT_STRUCT_LOC secure_boot_struct = {
165     .boot_struct = {
166         .hdr = {
167             .magic = ~0UL,
168             .security = 1,
169             .version = BOOT_STRUCT_VERSION,
170             .build_info_start = DEFAULT_BUILD_INFO,
171         },
172 #if HAAS_OTA_ENABLED
173         .ver = HAAS_OTA_BIN_VER,
174 #endif
175     },
176     .code_sig_struct = DEFAULT_CODE_SIG,
177     .norflash_cfg = DEFAULT_NORFLASH_CFG,
178 };
179 
180 #else
181 
182 const struct boot_hdr_t BOOT_STRUCT_LOC boot_struct = {
183     .magic = ~0UL,
184     .security = 0,
185     .version = BOOT_STRUCT_VERSION,
186     .build_info_start = DEFAULT_BUILD_INFO,
187 };
188 
189 #endif
190 
191 #endif // !SECURE_BOOT_V1
192 
193 // -----------------------------------------------------------
194 // Reboot param
195 // -----------------------------------------------------------
196 
197 #ifdef __ARMCC_VERSION
198 #define REBOOT_PARAM_LOC __attribute((section(".bss.reboot_param")))
199 #else
200 #define REBOOT_PARAM_LOC __attribute((section(".reboot_param")))
201 #endif
202 
203 struct REBOOT_PARAM_T REBOOT_PARAM_LOC reboot_param;
204 
205