forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renesas-rz.c
216 lines (175 loc) · 5.34 KB
/
renesas-rz.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
/* renesas-rz.c
*
* Stubs for custom HAL implementation. Defines the
* functions used by wolfboot for a specific target.
*
* Copyright (C) 2024 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
*/
#include "user_settings.h"
#include "target.h"
#include "wolfboot/wolfboot.h"
#include "printf.h"
#include "hal_data.h"
#if defined(WOLFBOOT_RENESAS_RSIP) && !defined(WOLFBOOT_RENESAS_APP)
#include "rsa_pub.h"
#include "wolfssl/wolfcrypt/wc_port.h"
#include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h"
#include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-types.h"
FSPSM_ST pkInfo;
uint8_t wrapped_public_key[RSIP_BYTE_SIZE_WRAPPED_KEY_VALUE_RSA_2048_PUBLIC];
rsip_wrapped_key_t *p_wrapped_public_key = (rsip_wrapped_key_t *) wrapped_public_key;
int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx);
#endif
#define BSC_SDRAM_SPACE (0x30000000)
#ifdef DEBUG_FLASH_WRITE_VERIFY
static uint8_t readbuf[MINIMUM_BLOCK*sizeof(uint32_t)];
#endif
/* #define DEBUG_FLASH_WRITE_VERIFY */
static inline void hal_panic(void)
{
while(1)
;
}
void hal_init(void);
void hal_prepare_boot(void);
int hal_flash_write(uint32_t addr, const uint8_t *data, int len);
int hal_flash_erase(uint32_t address, int int_len);
void hal_flash_unlock(void);
void hal_flash_lock(void);
int ext_flash_read(unsigned long address, uint8_t *data, int len);
int ext_flash_erase(unsigned long address, int len);
int ext_flash_write(unsigned long address, const uint8_t *data, int len);
void ext_flash_lock(void);
void ext_flash_unlock(void);
uint32_t rz_memcopy(uint32_t *src, uint32_t *dst, uint32_t bytesize);
void* hal_get_primary_address(void);
void* hal_get_update_address(void);
uint32_t rz_memcopy(uint32_t *src, uint32_t *dst, uint32_t bytesize)
{
uint32_t i;
uint32_t cnt;
/* copy count in 4 byte unit */
cnt = (bytesize + 3) >> 2;
for (i = 0; i < cnt; i++)
{
*dst++ = *src++;
}
/* ensuring data-changing */
__DSB();
return bytesize;
}
#ifdef EXT_FLASH
int ext_flash_read(unsigned long address, uint8_t *data, int len)
{
return (int)rz_memcopy((void*)address, (uint32_t*)data, (uint32_t)len);
}
int ext_flash_erase(unsigned long address, int len)
{
(void) address;
(void) len;
return 0;
}
int ext_flash_write(unsigned long address, const uint8_t *data, int len)
{
(void) address;
(void) data;
(void) len;
return 0;
}
void ext_flash_lock(void)
{
}
void ext_flash_unlock(void)
{
}
#endif
void hal_init(void)
{
#if defined(WOLFBOOT_RENESAS_RSIP) && !defined(WOLFBOOT_RENESAS_APP)
fsp_err_t err;
int ret;
rsa_public_t rsip_pub_key;
const size_t key_size = sizeof(rsip_pub_key);
err = wolfCrypt_Init();
if (err != 0) {
printf("ERROR: wolfCrypt_Init %d\n", err);
hal_panic();
}
/* copy the key from ext flash to RAM */
ret = ext_flash_read(RENESAS_RSIP_INSTALLEDKEY_FLASH_ADDR,
(uint8_t*)RENESAS_RSIP_INSTALLEDKEY_RAM_ADDR, key_size);
if (ret != key_size){
wolfBoot_printf("Error reading public key at %lx\n",
RENESAS_RSIP_INSTALLEDKEY_FLASH_ADDR);
hal_panic();
}
/* import enrypted key */
XMEMCPY(&rsip_pub_key, (const void*)RENESAS_RSIP_INSTALLEDKEY_RAM_ADDR, key_size);
err = R_RSIP_KeyImportWithUFPK(&rsip_ctrl, rsip_pub_key.wufpk,
rsip_pub_key.initial_vector,
RSIP_KEY_TYPE_RSA_2048_PUBLIC_ENHANCED,
rsip_pub_key.encrypted_user_key,
p_wrapped_public_key);
XMEMSET(&pkInfo, 0, sizeof(pkInfo));
pkInfo.wrapped_key_rsapub2048 =
(rsip_wrapped_key_t*)p_wrapped_public_key;
pkInfo.keyflgs_crypt.bits.rsapub2048_installedkey_set = 1;
pkInfo.keyflgs_crypt.bits.message_type = 1;
pkInfo.hash_type = RSIP_HASH_TYPE_SHA256;
err = wc_CryptoCb_CryptInitRenesasCmn(NULL, &pkInfo);
if (err < 0) {
wolfBoot_printf("ERROR: wc_CryptoCb_CryptInitRenesasCmn %d\n", err);
hal_panic();
}
#endif
}
void hal_prepare_boot(void)
{
}
/* write data to sdram */
int hal_flash_write(uint32_t addr, const uint8_t *data, int len)
{
(void)addr;
(void)data;
(void)len;
return 0;
}
/* write data to sdram */
int hal_flash_erase(uint32_t address, int int_len)
{
(void)address;
(void)int_len;
return 0;
}
void hal_flash_unlock(void)
{
return;
}
void hal_flash_lock(void)
{
return;
}
void* hal_get_primary_address(void)
{
return (void*)WOLFBOOT_PARTITION_BOOT_ADDRESS;
}
void* hal_get_update_address(void)
{
return (void*)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
}