1 /*
2 * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /* INTERNAL API
8 * implementation of generic interface to MMU memory protection features
9 */
10
11 #include <stdio.h>
12 #include "sdkconfig.h"
13 #include "soc/sensitive_reg.h"
14 #include "soc/dport_access.h"
15 #include "soc/periph_defs.h"
16 #include "esp_intr_alloc.h"
17 #include "hal/memprot_ll.h"
18 #include "hal/memprot_peri_ll.h"
19 #include "esp32s2/memprot.h"
20 #include "esp_fault.h"
21 #include "soc/cpu.h"
22 #include "esp32s2/rom/ets_sys.h"
23
24 extern int _iram_text_end;
25 extern int _data_start;
26 extern int _rtc_text_end;
27 extern int _rtc_dummy_end;
28
esp_memprot_ll_err_to_esp_err(memprot_ll_err_t err)29 static inline esp_err_t esp_memprot_ll_err_to_esp_err(memprot_ll_err_t err)
30 {
31 switch (err) {
32 case MEMP_LL_OK: return ESP_OK;
33 case MEMP_LL_FAIL: return ESP_FAIL;
34 case MEMP_LL_ERR_SPLIT_ADDR_INVALID: return ESP_ERR_INVALID_STATE;
35 case MEMP_LL_ERR_SPLIT_ADDR_UNALIGNED: return ESP_ERR_INVALID_SIZE;
36 case MEMP_LL_ERR_UNI_BLOCK_INVALID: return ESP_ERR_NOT_FOUND;
37 default:
38 return ESP_FAIL;
39 }
40 }
41
esp_memprot_iram0_sram_get_min_split_addr(void)42 uint32_t *esp_memprot_iram0_sram_get_min_split_addr(void)
43 {
44 return (uint32_t *)&_iram_text_end;
45 }
46
esp_memprot_iram0_rtcfast_get_min_split_addr(void)47 uint32_t *esp_memprot_iram0_rtcfast_get_min_split_addr(void)
48 {
49 return (uint32_t *)&_rtc_text_end;
50 }
51
esp_memprot_dram0_sram_get_min_split_addr(void)52 uint32_t *esp_memprot_dram0_sram_get_min_split_addr(void)
53 {
54 return (uint32_t *)&_data_start;
55 }
56
esp_memprot_dram0_rtcfast_get_min_split_addr(void)57 uint32_t *esp_memprot_dram0_rtcfast_get_min_split_addr(void)
58 {
59 return (uint32_t *)&_rtc_dummy_end;
60 }
61
esp_memprot_peri1_rtcslow_get_min_split_addr(void)62 uint32_t *esp_memprot_peri1_rtcslow_get_min_split_addr(void)
63 {
64 return (uint32_t *)PERI1_RTCSLOW_ADDRESS_BASE;
65 }
66
esp_memprot_peri2_rtcslow_0_get_min_split_addr(void)67 uint32_t *esp_memprot_peri2_rtcslow_0_get_min_split_addr(void)
68 {
69 return (uint32_t *)PERI2_RTCSLOW_0_ADDRESS_BASE;
70 }
71
esp_memprot_peri2_rtcslow_1_get_min_split_addr(void)72 uint32_t *esp_memprot_peri2_rtcslow_1_get_min_split_addr(void)
73 {
74 return (uint32_t *)PERI2_RTCSLOW_1_ADDRESS_BASE;
75 }
76
esp_memprot_get_split_addr(mem_type_prot_t mem_type)77 uint32_t *esp_memprot_get_split_addr(mem_type_prot_t mem_type)
78 {
79 switch (mem_type) {
80 case MEMPROT_IRAM0_SRAM:
81 return esp_memprot_iram0_sram_get_min_split_addr();
82 case MEMPROT_DRAM0_SRAM:
83 return esp_memprot_dram0_sram_get_min_split_addr();
84 case MEMPROT_IRAM0_RTCFAST:
85 return esp_memprot_iram0_rtcfast_get_min_split_addr();
86 case MEMPROT_DRAM0_RTCFAST:
87 return esp_memprot_dram0_rtcfast_get_min_split_addr();
88 case MEMPROT_PERI1_RTCSLOW:
89 return esp_memprot_peri1_rtcslow_get_min_split_addr();
90 case MEMPROT_PERI2_RTCSLOW_0:
91 return esp_memprot_peri2_rtcslow_0_get_min_split_addr();
92 case MEMPROT_PERI2_RTCSLOW_1:
93 return esp_memprot_peri2_rtcslow_1_get_min_split_addr();
94 default:
95 return (uint32_t *)MEMPROT_INVALID_ADDRESS;
96 }
97 }
98
esp_memprot_type_to_str(mem_type_prot_t mem_type)99 const char *esp_memprot_type_to_str(mem_type_prot_t mem_type)
100 {
101 switch (mem_type) {
102 case MEMPROT_IRAM0_SRAM:
103 return "IRAM0_SRAM";
104 case MEMPROT_DRAM0_SRAM:
105 return "DRAM0_SRAM";
106 case MEMPROT_IRAM0_RTCFAST:
107 return "IRAM0_RTCFAST";
108 case MEMPROT_DRAM0_RTCFAST:
109 return "DRAM0_RTCFAST";
110 case MEMPROT_PERI1_RTCSLOW:
111 return "PERI1_RTCSLOW";
112 case MEMPROT_PERI2_RTCSLOW_0:
113 return "PERI2_RTCSLOW_0";
114 case MEMPROT_PERI2_RTCSLOW_1:
115 return "PERI2_RTCSLOW_1";
116 default:
117 return "INVALID_MEM_TYPE";
118 }
119 }
120
esp_memprot_intr_init(mem_type_prot_t mem_type)121 esp_err_t esp_memprot_intr_init(mem_type_prot_t mem_type)
122 {
123 ESP_INTR_DISABLE(ETS_MEMACCESS_ERR_INUM);
124
125 switch (mem_type) {
126 case MEMPROT_IRAM0_SRAM:
127 case MEMPROT_IRAM0_RTCFAST:
128 intr_matrix_set(PRO_CPU_NUM, memprot_ll_iram0_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
129 break;
130 case MEMPROT_DRAM0_SRAM:
131 case MEMPROT_DRAM0_RTCFAST:
132 intr_matrix_set(PRO_CPU_NUM, memprot_ll_dram0_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
133 break;
134 case MEMPROT_PERI1_RTCSLOW:
135 intr_matrix_set(PRO_CPU_NUM, memprot_ll_peri1_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
136 break;
137 case MEMPROT_PERI2_RTCSLOW_0:
138 case MEMPROT_PERI2_RTCSLOW_1:
139 intr_matrix_set(PRO_CPU_NUM, memprot_ll_peri2_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
140 break;
141 default:
142 return ESP_ERR_NOT_SUPPORTED;
143 }
144
145 ESP_INTR_ENABLE(ETS_MEMACCESS_ERR_INUM);
146
147 return ESP_OK;
148 }
149
esp_memprot_intr_ena(mem_type_prot_t mem_type,bool enable)150 esp_err_t esp_memprot_intr_ena(mem_type_prot_t mem_type, bool enable)
151 {
152 switch (mem_type) {
153 case MEMPROT_IRAM0_SRAM:
154 case MEMPROT_IRAM0_RTCFAST:
155 memprot_ll_iram0_intr_ena(enable);
156 break;
157 case MEMPROT_DRAM0_SRAM:
158 case MEMPROT_DRAM0_RTCFAST:
159 memprot_ll_dram0_intr_ena(enable);
160 break;
161 case MEMPROT_PERI1_RTCSLOW:
162 memprot_ll_peri1_intr_ena(enable);
163 break;
164 case MEMPROT_PERI2_RTCSLOW_0:
165 case MEMPROT_PERI2_RTCSLOW_1:
166 memprot_ll_peri2_intr_ena(enable);
167 break;
168 default:
169 return ESP_ERR_NOT_SUPPORTED;
170 }
171
172 return ESP_OK;
173 }
174
esp_memprot_get_active_intr_memtype()175 mem_type_prot_t esp_memprot_get_active_intr_memtype()
176 {
177 if (memprot_ll_iram0_sram_is_intr_mine()) {
178 return MEMPROT_IRAM0_SRAM;
179 } else if (memprot_ll_iram0_rtcfast_is_intr_mine()) {
180 return MEMPROT_IRAM0_RTCFAST;
181 } else if (memprot_ll_dram0_sram_is_intr_mine()) {
182 return MEMPROT_DRAM0_SRAM;
183 } else if (memprot_ll_dram0_rtcfast_is_intr_mine()) {
184 return MEMPROT_DRAM0_RTCFAST;
185 } else if (memprot_ll_peri1_rtcslow_is_intr_mine()) {
186 return MEMPROT_PERI1_RTCSLOW;
187 } else if (memprot_ll_peri2_rtcslow_0_is_intr_mine()) {
188 return MEMPROT_PERI2_RTCSLOW_0;
189 } else if (memprot_ll_peri2_rtcslow_1_is_intr_mine()) {
190 return MEMPROT_PERI2_RTCSLOW_1;
191 }
192
193 return MEMPROT_NONE;
194 }
195
esp_memprot_clear_intr(mem_type_prot_t mem_type)196 esp_err_t esp_memprot_clear_intr(mem_type_prot_t mem_type)
197 {
198 switch (mem_type) {
199 case MEMPROT_IRAM0_SRAM:
200 case MEMPROT_IRAM0_RTCFAST:
201 memprot_ll_iram0_clear_intr();
202 break;
203 case MEMPROT_DRAM0_SRAM:
204 case MEMPROT_DRAM0_RTCFAST:
205 memprot_ll_dram0_clear_intr();
206 break;
207 case MEMPROT_PERI1_RTCSLOW:
208 memprot_ll_peri1_clear_intr();
209 break;
210 case MEMPROT_PERI2_RTCSLOW_0:
211 case MEMPROT_PERI2_RTCSLOW_1:
212 memprot_ll_peri2_clear_intr();
213 break;
214 default:
215 return ESP_ERR_NOT_SUPPORTED;
216 }
217
218 return ESP_OK;
219 }
220
esp_memprot_set_lock(mem_type_prot_t mem_type)221 esp_err_t esp_memprot_set_lock(mem_type_prot_t mem_type)
222 {
223 switch (mem_type) {
224 case MEMPROT_IRAM0_SRAM:
225 case MEMPROT_IRAM0_RTCFAST:
226 memprot_ll_iram0_set_lock();
227 break;
228 case MEMPROT_DRAM0_SRAM:
229 case MEMPROT_DRAM0_RTCFAST:
230 memprot_ll_dram0_set_lock();
231 break;
232 case MEMPROT_PERI1_RTCSLOW:
233 memprot_ll_peri1_set_lock();
234 break;
235 case MEMPROT_PERI2_RTCSLOW_0:
236 case MEMPROT_PERI2_RTCSLOW_1:
237 memprot_ll_peri2_set_lock();
238 break;
239 default:
240 return ESP_ERR_NOT_SUPPORTED;
241 }
242
243 return ESP_OK;
244 }
245
esp_memprot_get_lock(mem_type_prot_t mem_type,bool * locked)246 esp_err_t esp_memprot_get_lock(mem_type_prot_t mem_type, bool *locked)
247 {
248 if (locked == NULL) {
249 return ESP_ERR_INVALID_ARG;
250 }
251
252 switch (mem_type) {
253 case MEMPROT_IRAM0_SRAM:
254 case MEMPROT_IRAM0_RTCFAST:
255 *locked = memprot_ll_iram0_get_lock_bit() > 0;
256 break;
257 case MEMPROT_DRAM0_SRAM:
258 case MEMPROT_DRAM0_RTCFAST:
259 *locked = memprot_ll_dram0_get_lock_bit() > 0;
260 break;
261 case MEMPROT_PERI1_RTCSLOW:
262 *locked = memprot_ll_peri1_get_lock_bit() > 0;
263 break;
264 case MEMPROT_PERI2_RTCSLOW_0:
265 case MEMPROT_PERI2_RTCSLOW_1:
266 *locked = memprot_ll_peri2_get_lock_bit() > 0;
267 break;
268 default:
269 return ESP_ERR_NOT_SUPPORTED;
270 }
271
272 return ESP_OK;
273 }
274
esp_memprot_is_locked_any()275 bool esp_memprot_is_locked_any()
276 {
277 return
278 memprot_ll_iram0_get_lock_bit() > 0 ||
279 memprot_ll_dram0_get_lock_bit() > 0 ||
280 memprot_ll_peri1_get_lock_bit() > 0 ||
281 memprot_ll_peri2_get_lock_bit() > 0;
282 }
283
esp_memprot_get_conf_reg(mem_type_prot_t mem_type,uint32_t * conf_reg_val)284 esp_err_t esp_memprot_get_conf_reg(mem_type_prot_t mem_type, uint32_t *conf_reg_val)
285 {
286 if (conf_reg_val == NULL) {
287 return ESP_ERR_INVALID_ARG;
288 }
289
290 switch (mem_type) {
291 case MEMPROT_IRAM0_SRAM:
292 case MEMPROT_IRAM0_RTCFAST:
293 *conf_reg_val = memprot_ll_iram0_get_conf_reg();
294 break;
295 case MEMPROT_DRAM0_SRAM:
296 case MEMPROT_DRAM0_RTCFAST:
297 *conf_reg_val = memprot_ll_dram0_get_conf_reg();
298 break;
299 case MEMPROT_PERI1_RTCSLOW:
300 *conf_reg_val = memprot_ll_peri1_rtcslow_get_conf_reg();
301 break;
302 case MEMPROT_PERI2_RTCSLOW_0:
303 *conf_reg_val = memprot_ll_peri2_rtcslow_0_get_conf_reg();
304 break;
305 case MEMPROT_PERI2_RTCSLOW_1:
306 *conf_reg_val = memprot_ll_peri2_rtcslow_1_get_conf_reg();
307 break;
308 default:
309 return ESP_ERR_NOT_SUPPORTED;
310 }
311
312 return ESP_OK;
313 }
314
esp_memprot_get_fault_reg(mem_type_prot_t mem_type,uint32_t * fault_reg_val)315 esp_err_t esp_memprot_get_fault_reg(mem_type_prot_t mem_type, uint32_t *fault_reg_val)
316 {
317 if (fault_reg_val == NULL) {
318 return ESP_ERR_INVALID_ARG;
319 }
320
321 switch (mem_type) {
322 case MEMPROT_IRAM0_SRAM:
323 case MEMPROT_IRAM0_RTCFAST:
324 *fault_reg_val = memprot_ll_iram0_get_fault_reg();
325 break;
326 case MEMPROT_DRAM0_SRAM:
327 case MEMPROT_DRAM0_RTCFAST:
328 *fault_reg_val = memprot_ll_dram0_get_fault_reg();
329 break;
330 case MEMPROT_PERI1_RTCSLOW:
331 *fault_reg_val = memprot_ll_peri1_get_fault_reg();
332 break;
333 case MEMPROT_PERI2_RTCSLOW_0:
334 case MEMPROT_PERI2_RTCSLOW_1:
335 *fault_reg_val = memprot_ll_peri2_get_fault_reg();
336 break;
337 default:
338 return ESP_ERR_NOT_SUPPORTED;
339 }
340
341 return ESP_OK;
342 }
343
esp_memprot_get_fault_status(mem_type_prot_t mem_type,uint32_t ** faulting_address,uint32_t * op_type,uint32_t * op_subtype)344 esp_err_t esp_memprot_get_fault_status(mem_type_prot_t mem_type, uint32_t **faulting_address, uint32_t *op_type, uint32_t *op_subtype)
345 {
346 if (*faulting_address == NULL || op_type == NULL || op_subtype == NULL) {
347 return ESP_ERR_INVALID_ARG;
348 }
349
350 switch (mem_type) {
351 case MEMPROT_IRAM0_SRAM:
352 *faulting_address = (uint32_t *)memprot_ll_iram0_sram_get_fault_address();
353 memprot_ll_iram0_get_fault_op_type(op_type, op_subtype);
354 break;
355 case MEMPROT_IRAM0_RTCFAST:
356 *faulting_address = (uint32_t *)memprot_ll_iram0_rtcfast_get_fault_address();
357 memprot_ll_iram0_get_fault_op_type(op_type, op_subtype);
358 break;
359 case MEMPROT_DRAM0_SRAM:
360 *faulting_address = (uint32_t *)memprot_ll_dram0_sram_get_fault_address();
361 memprot_ll_dram0_get_fault_op_type(op_type, op_subtype);
362 break;
363 case MEMPROT_DRAM0_RTCFAST:
364 *faulting_address = (uint32_t *)memprot_ll_dram0_rtcfast_get_fault_address();
365 memprot_ll_dram0_get_fault_op_type(op_type, op_subtype);
366 break;
367 case MEMPROT_PERI1_RTCSLOW:
368 *faulting_address = (uint32_t *)memprot_ll_peri1_rtcslow_get_fault_address();
369 memprot_ll_peri1_get_fault_op_type(op_type, op_subtype);
370 break;
371 case MEMPROT_PERI2_RTCSLOW_0:
372 case MEMPROT_PERI2_RTCSLOW_1:
373 *faulting_address = (uint32_t *)memprot_ll_peri2_rtcslow_get_fault_address();
374 memprot_ll_peri2_get_fault_op_type(op_type, op_subtype);
375 break;
376 default:
377 return ESP_ERR_NOT_SUPPORTED;
378 }
379
380 return ESP_OK;
381 }
382
esp_memprot_is_intr_ena_any()383 bool esp_memprot_is_intr_ena_any()
384 {
385 return
386 memprot_ll_iram0_get_intr_ena_bit() > 0 ||
387 memprot_ll_dram0_get_intr_ena_bit() > 0 ||
388 memprot_ll_peri1_get_intr_ena_bit() > 0 ||
389 memprot_ll_peri2_get_intr_ena_bit() > 0;
390 }
391
esp_memprot_get_intr_ena_bit(mem_type_prot_t mem_type,uint32_t * enable_bit)392 esp_err_t esp_memprot_get_intr_ena_bit(mem_type_prot_t mem_type, uint32_t *enable_bit)
393 {
394 if (enable_bit == NULL) {
395 return ESP_ERR_INVALID_ARG;
396 }
397
398 switch (mem_type) {
399 case MEMPROT_IRAM0_SRAM:
400 case MEMPROT_IRAM0_RTCFAST:
401 *enable_bit = memprot_ll_iram0_get_intr_ena_bit();
402 break;
403 case MEMPROT_DRAM0_SRAM:
404 case MEMPROT_DRAM0_RTCFAST:
405 *enable_bit = memprot_ll_dram0_get_intr_ena_bit();
406 break;
407 case MEMPROT_PERI1_RTCSLOW:
408 *enable_bit = memprot_ll_peri1_get_intr_ena_bit();
409 break;
410 case MEMPROT_PERI2_RTCSLOW_0:
411 case MEMPROT_PERI2_RTCSLOW_1:
412 *enable_bit = memprot_ll_peri2_get_intr_ena_bit();
413 break;
414 default:
415 return ESP_ERR_NOT_SUPPORTED;
416 }
417
418 return ESP_OK;
419 }
420
esp_memprot_get_intr_on_bit(mem_type_prot_t mem_type,uint32_t * intr_on_bit)421 esp_err_t esp_memprot_get_intr_on_bit(mem_type_prot_t mem_type, uint32_t *intr_on_bit)
422 {
423 if (intr_on_bit == NULL) {
424 return ESP_ERR_INVALID_ARG;
425 }
426
427 switch (mem_type) {
428 case MEMPROT_IRAM0_SRAM:
429 case MEMPROT_IRAM0_RTCFAST:
430 *intr_on_bit = memprot_ll_iram0_get_intr_on_bit();
431 break;
432 case MEMPROT_DRAM0_SRAM:
433 case MEMPROT_DRAM0_RTCFAST:
434 *intr_on_bit = memprot_ll_dram0_get_intr_on_bit();
435 break;
436 case MEMPROT_PERI1_RTCSLOW:
437 *intr_on_bit = memprot_ll_peri1_get_intr_on_bit();
438 break;
439 case MEMPROT_PERI2_RTCSLOW_0:
440 case MEMPROT_PERI2_RTCSLOW_1:
441 *intr_on_bit = memprot_ll_peri2_get_intr_on_bit();
442 break;
443 default:
444 return ESP_ERR_NOT_SUPPORTED;
445 }
446
447 return ESP_OK;
448 }
449
esp_memprot_get_intr_clr_bit(mem_type_prot_t mem_type,uint32_t * clear_bit)450 esp_err_t esp_memprot_get_intr_clr_bit(mem_type_prot_t mem_type, uint32_t *clear_bit)
451 {
452 if (clear_bit == NULL) {
453 return ESP_ERR_INVALID_ARG;
454 }
455
456 switch (mem_type) {
457 case MEMPROT_IRAM0_SRAM:
458 case MEMPROT_IRAM0_RTCFAST:
459 *clear_bit = memprot_ll_iram0_get_intr_clr_bit();
460 break;
461 case MEMPROT_DRAM0_SRAM:
462 case MEMPROT_DRAM0_RTCFAST:
463 *clear_bit = memprot_ll_dram0_get_intr_clr_bit();
464 break;
465 case MEMPROT_PERI1_RTCSLOW:
466 *clear_bit = memprot_ll_peri1_get_intr_clr_bit();
467 break;
468 case MEMPROT_PERI2_RTCSLOW_0:
469 case MEMPROT_PERI2_RTCSLOW_1:
470 *clear_bit = memprot_ll_peri2_get_intr_clr_bit();
471 break;
472 default:
473 return ESP_ERR_NOT_SUPPORTED;
474 }
475
476 return ESP_OK;
477 }
478
esp_memprot_get_uni_block_read_bit(mem_type_prot_t mem_type,uint32_t block,uint32_t * read_bit)479 esp_err_t esp_memprot_get_uni_block_read_bit(mem_type_prot_t mem_type, uint32_t block, uint32_t *read_bit)
480 {
481 if (read_bit == NULL) {
482 return ESP_ERR_INVALID_ARG;
483 }
484
485 esp_err_t ret = ESP_OK;
486
487 switch (mem_type) {
488 case MEMPROT_IRAM0_SRAM:
489 if (!memprot_ll_iram0_sram_get_uni_block_read_bit(block, read_bit)) {
490 ret = ESP_ERR_NOT_FOUND;
491 }
492 break;
493 case MEMPROT_DRAM0_SRAM:
494 if (!memprot_ll_dram0_sram_get_uni_block_read_bit(block, read_bit)) {
495 ret = ESP_ERR_NOT_FOUND;
496 }
497 break;
498 default:
499 ret = ESP_ERR_NOT_SUPPORTED;
500 break;
501 }
502
503 return ret;
504 }
505
esp_memprot_get_uni_block_write_bit(mem_type_prot_t mem_type,uint32_t block,uint32_t * write_bit)506 esp_err_t esp_memprot_get_uni_block_write_bit(mem_type_prot_t mem_type, uint32_t block, uint32_t *write_bit)
507 {
508 if (write_bit == NULL) {
509 return ESP_ERR_INVALID_ARG;
510 }
511
512 esp_err_t ret = ESP_OK;
513
514 switch (mem_type) {
515 case MEMPROT_IRAM0_SRAM:
516 if (!memprot_ll_iram0_sram_get_uni_block_write_bit(block, write_bit)) {
517 ret = ESP_ERR_NOT_FOUND;
518 }
519 break;
520 case MEMPROT_DRAM0_SRAM:
521 if (!memprot_ll_dram0_sram_get_uni_block_write_bit(block, write_bit)) {
522 ret = ESP_ERR_NOT_FOUND;
523 }
524 break;
525 default:
526 ret = ESP_ERR_NOT_SUPPORTED;
527 break;
528 }
529
530 return ret;
531 }
532
esp_memprot_get_uni_block_exec_bit(mem_type_prot_t mem_type,uint32_t block,uint32_t * exec_bit)533 esp_err_t esp_memprot_get_uni_block_exec_bit(mem_type_prot_t mem_type, uint32_t block, uint32_t *exec_bit)
534 {
535 if (exec_bit == NULL) {
536 return ESP_ERR_INVALID_ARG;
537 }
538
539 switch (mem_type) {
540 case MEMPROT_IRAM0_SRAM:
541 if (!memprot_ll_iram0_sram_get_uni_block_exec_bit(block, exec_bit)) {
542 return ESP_ERR_NOT_FOUND;
543 }
544 break;
545 default:
546 return ESP_ERR_NOT_SUPPORTED;
547 }
548
549 return ESP_OK;
550 }
551
esp_memprot_set_uni_block_perm_dram(mem_type_prot_t mem_type,uint32_t block,bool write_perm,bool read_perm)552 esp_err_t esp_memprot_set_uni_block_perm_dram(mem_type_prot_t mem_type, uint32_t block, bool write_perm, bool read_perm)
553 {
554 switch (mem_type) {
555 case MEMPROT_DRAM0_SRAM:
556 return esp_memprot_ll_err_to_esp_err(memprot_ll_dram0_sram_set_uni_block_perm(block, write_perm, read_perm));
557 default:
558 return ESP_ERR_NOT_SUPPORTED;
559 }
560 }
561
esp_memprot_get_perm_uni_reg(mem_type_prot_t mem_type,uint32_t * perm_reg)562 esp_err_t esp_memprot_get_perm_uni_reg(mem_type_prot_t mem_type, uint32_t *perm_reg)
563 {
564 if (perm_reg == NULL) {
565 return ESP_ERR_INVALID_ARG;
566 }
567
568 switch (mem_type) {
569 case MEMPROT_IRAM0_SRAM:
570 *perm_reg = memprot_ll_iram0_sram_get_perm_uni_reg();
571 break;
572 case MEMPROT_DRAM0_SRAM:
573 *perm_reg = memprot_ll_dram0_sram_get_perm_reg();
574 break;
575 default:
576 return ESP_ERR_NOT_SUPPORTED;
577 }
578
579 return ESP_OK;
580 }
581
esp_memprot_get_perm_split_reg(mem_type_prot_t mem_type,uint32_t * split_reg)582 esp_err_t esp_memprot_get_perm_split_reg(mem_type_prot_t mem_type, uint32_t *split_reg)
583 {
584 if (split_reg == NULL) {
585 return ESP_ERR_INVALID_ARG;
586 }
587
588 switch (mem_type) {
589 case MEMPROT_IRAM0_SRAM:
590 *split_reg = memprot_ll_iram0_sram_get_perm_split_reg();
591 break;
592 case MEMPROT_IRAM0_RTCFAST:
593 *split_reg = memprot_ll_iram0_rtcfast_get_perm_split_reg();
594 break;
595 case MEMPROT_DRAM0_SRAM:
596 *split_reg = memprot_ll_dram0_sram_get_perm_reg();
597 break;
598 case MEMPROT_DRAM0_RTCFAST:
599 *split_reg = memprot_ll_dram0_rtcfast_get_perm_split_reg();
600 break;
601 case MEMPROT_PERI1_RTCSLOW:
602 *split_reg = memprot_ll_peri1_rtcslow_get_conf_reg();
603 break;
604 case MEMPROT_PERI2_RTCSLOW_0:
605 *split_reg = memprot_ll_peri2_rtcslow_0_get_conf_reg();
606 break;
607 case MEMPROT_PERI2_RTCSLOW_1:
608 *split_reg = memprot_ll_peri2_rtcslow_1_get_conf_reg();
609 break;
610 default:
611 return ESP_ERR_NOT_SUPPORTED;
612 }
613
614 return ESP_OK;
615 }
616
esp_memprot_set_prot_dram(mem_type_prot_t mem_type,uint32_t * split_addr,bool lw,bool lr,bool hw,bool hr)617 esp_err_t esp_memprot_set_prot_dram(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool hw, bool hr)
618 {
619 switch (mem_type) {
620 case MEMPROT_DRAM0_SRAM:
621 return esp_memprot_ll_err_to_esp_err(memprot_ll_dram0_sram_set_prot(split_addr != NULL ? split_addr : esp_memprot_dram0_sram_get_min_split_addr(), lw, lr, hw, hr));
622 case MEMPROT_DRAM0_RTCFAST:
623 return esp_memprot_ll_err_to_esp_err(memprot_ll_dram0_rtcfast_set_prot(split_addr != NULL ? split_addr : esp_memprot_dram0_rtcfast_get_min_split_addr(), lw, lr, hw, hr));
624 default:
625 return ESP_ERR_NOT_SUPPORTED;
626 }
627 }
628
esp_memprot_set_uni_block_perm_iram(mem_type_prot_t mem_type,uint32_t block,bool write_perm,bool read_perm,bool exec_perm)629 esp_err_t esp_memprot_set_uni_block_perm_iram(mem_type_prot_t mem_type, uint32_t block, bool write_perm, bool read_perm, bool exec_perm)
630 {
631 switch (mem_type) {
632 case MEMPROT_IRAM0_SRAM:
633 if (!memprot_ll_iram0_sram_set_uni_block_perm(block, write_perm, read_perm, exec_perm)) {
634 return ESP_ERR_INVALID_ARG;
635 }
636 break;
637 default:
638 return ESP_ERR_NOT_SUPPORTED;
639 }
640
641 return ESP_OK;
642 }
643
esp_memprot_set_prot_iram(mem_type_prot_t mem_type,uint32_t * split_addr,bool lw,bool lr,bool lx,bool hw,bool hr,bool hx)644 esp_err_t esp_memprot_set_prot_iram(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool lx, bool hw, bool hr, bool hx)
645 {
646 switch (mem_type) {
647 case MEMPROT_IRAM0_SRAM:
648 return esp_memprot_ll_err_to_esp_err(memprot_ll_iram0_sram_set_prot(split_addr != NULL ? split_addr : esp_memprot_iram0_sram_get_min_split_addr(), lw, lr, lx, hw, hr, hx));
649 case MEMPROT_IRAM0_RTCFAST:
650 return esp_memprot_ll_err_to_esp_err(memprot_ll_iram0_rtcfast_set_prot(split_addr != NULL ? split_addr : esp_memprot_iram0_rtcfast_get_min_split_addr(), lw, lr, lx, hw, hr, hx));
651 default:
652 return ESP_ERR_NOT_SUPPORTED;
653 }
654 }
655
esp_memprot_get_perm_split_bits_iram(mem_type_prot_t mem_type,bool * lw,bool * lr,bool * lx,bool * hw,bool * hr,bool * hx)656 esp_err_t esp_memprot_get_perm_split_bits_iram(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *lx, bool *hw, bool *hr, bool *hx)
657 {
658 if (lw == NULL || lr == NULL || lx == NULL || hw == NULL || hr == NULL || hx == NULL) {
659 return ESP_ERR_INVALID_ARG;
660 }
661
662 switch (mem_type) {
663 case MEMPROT_IRAM0_SRAM:
664 memprot_ll_iram0_sram_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
665 break;
666 case MEMPROT_IRAM0_RTCFAST:
667 memprot_ll_iram0_rtcfast_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
668 break;
669 default:
670 return ESP_ERR_NOT_SUPPORTED;
671 }
672
673 return ESP_OK;
674 }
675
esp_memprot_get_perm_split_bits_dram(mem_type_prot_t mem_type,bool * lw,bool * lr,bool * hw,bool * hr)676 esp_err_t esp_memprot_get_perm_split_bits_dram(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *hw, bool *hr)
677 {
678 if (lw == NULL || lr == NULL || hw == NULL || hr == NULL) {
679 return ESP_ERR_INVALID_ARG;
680 }
681
682 switch (mem_type) {
683 case MEMPROT_DRAM0_SRAM:
684 memprot_ll_dram0_sram_get_split_sgnf_bits(lw, lr, hw, hr);
685 break;
686 case MEMPROT_DRAM0_RTCFAST:
687 memprot_ll_dram0_rtcfast_get_split_sgnf_bits(lw, lr, hw, hr);
688 break;
689 default:
690 return ESP_ERR_NOT_SUPPORTED;
691 }
692
693 return ESP_OK;
694 }
695
esp_memprot_get_perm_split_bits_peri1(mem_type_prot_t mem_type,bool * lw,bool * lr,bool * hw,bool * hr)696 esp_err_t esp_memprot_get_perm_split_bits_peri1(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *hw, bool *hr)
697 {
698 if (lw == NULL || lr == NULL || hw == NULL || hr == NULL) {
699 return ESP_ERR_INVALID_ARG;
700 }
701
702 switch (mem_type) {
703 case MEMPROT_PERI1_RTCSLOW:
704 memprot_ll_peri1_rtcslow_get_split_sgnf_bits(lw, lr, hw, hr);
705 break;
706 default:
707 return ESP_ERR_NOT_SUPPORTED;
708 }
709
710 return ESP_OK;
711 }
712
esp_memprot_set_prot_peri1(mem_type_prot_t mem_type,uint32_t * split_addr,bool lw,bool lr,bool hw,bool hr)713 esp_err_t esp_memprot_set_prot_peri1(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool hw, bool hr)
714 {
715 switch (mem_type) {
716 case MEMPROT_PERI1_RTCSLOW:
717 return esp_memprot_ll_err_to_esp_err(
718 memprot_ll_peri1_rtcslow_set_prot(
719 split_addr != NULL ? split_addr : esp_memprot_peri1_rtcslow_get_min_split_addr(),
720 lw,
721 lr,
722 hw,
723 hr)
724 );
725 default:
726 return ESP_ERR_NOT_SUPPORTED;
727 }
728 }
729
esp_memprot_get_perm_split_bits_peri2(mem_type_prot_t mem_type,bool * lw,bool * lr,bool * lx,bool * hw,bool * hr,bool * hx)730 esp_err_t esp_memprot_get_perm_split_bits_peri2(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *lx, bool *hw, bool *hr, bool *hx)
731 {
732 if (lw == NULL || lr == NULL || lx == NULL || hw == NULL || hr == NULL || hx == NULL) {
733 return ESP_ERR_INVALID_ARG;
734 }
735
736 switch (mem_type) {
737 case MEMPROT_PERI2_RTCSLOW_0:
738 memprot_ll_peri2_rtcslow_0_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
739 break;
740 case MEMPROT_PERI2_RTCSLOW_1:
741 memprot_ll_peri2_rtcslow_1_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
742 break;
743 default:
744 return ESP_ERR_NOT_SUPPORTED;
745 }
746
747 return ESP_OK;
748 }
749
esp_memprot_set_prot_peri2(mem_type_prot_t mem_type,uint32_t * split_addr,bool lw,bool lr,bool lx,bool hw,bool hr,bool hx)750 esp_err_t esp_memprot_set_prot_peri2(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool lx, bool hw, bool hr, bool hx)
751 {
752 switch (mem_type) {
753 case MEMPROT_PERI2_RTCSLOW_0:
754 return esp_memprot_ll_err_to_esp_err(
755 memprot_ll_peri2_rtcslow_0_set_prot(
756 split_addr != NULL ? split_addr : esp_memprot_peri2_rtcslow_0_get_min_split_addr(),
757 lw,
758 lr,
759 lx,
760 hw,
761 hr,
762 hx)
763 );
764 case MEMPROT_PERI2_RTCSLOW_1:
765 return esp_memprot_ll_err_to_esp_err(
766 memprot_ll_peri2_rtcslow_1_set_prot(
767 split_addr != NULL ? split_addr : esp_memprot_peri2_rtcslow_1_get_min_split_addr(),
768 lw,
769 lr,
770 lx,
771 hw,
772 hr,
773 hx)
774 );
775 default:
776 return ESP_ERR_NOT_SUPPORTED;
777 }
778 }
779
esp_memprot_set_prot(bool invoke_panic_handler,bool lock_feature,uint32_t * mem_type_mask)780 esp_err_t esp_memprot_set_prot(bool invoke_panic_handler, bool lock_feature, uint32_t *mem_type_mask)
781 {
782 esp_err_t ret;
783
784 //any IRAM0/DRAM0 enable/disable call applies to all memory modules connected
785 uint32_t required_mem_prot = mem_type_mask == NULL ? (uint32_t)MEMPROT_ALL : *mem_type_mask;
786 bool use_iram0 = required_mem_prot & MEMPROT_IRAM0_SRAM || required_mem_prot & MEMPROT_IRAM0_RTCFAST;
787 bool use_dram0 = required_mem_prot & MEMPROT_DRAM0_SRAM || required_mem_prot & MEMPROT_DRAM0_RTCFAST;
788 bool use_peri1 = required_mem_prot & MEMPROT_PERI1_RTCSLOW;
789 bool use_peri2 = required_mem_prot & MEMPROT_PERI2_RTCSLOW_0 || required_mem_prot & MEMPROT_PERI2_RTCSLOW_1;
790
791 //disable protection
792 if (use_iram0 && (ret = esp_memprot_intr_ena(MEMPROT_IRAM0_SRAM, false)) != ESP_OK) {
793 return ret;
794 }
795 if (use_dram0 && (ret = esp_memprot_intr_ena(MEMPROT_DRAM0_SRAM, false)) != ESP_OK) {
796 return ret;
797 }
798 if (use_peri1 && (ret = esp_memprot_intr_ena(MEMPROT_PERI1_RTCSLOW, false)) != ESP_OK) {
799 return ret;
800 }
801 if (use_peri2 && (ret = esp_memprot_intr_ena(MEMPROT_PERI2_RTCSLOW_0, false)) != ESP_OK) {
802 return ret;
803 }
804
805 //if being debugged check we are not glitched and dont enable Memprot
806 if (esp_cpu_in_ocd_debug_mode()) {
807 ESP_FAULT_ASSERT(esp_cpu_in_ocd_debug_mode());
808 } else {
809 //initialize for specific buses (any memory type does the job)
810 if (invoke_panic_handler) {
811 if (use_iram0 && (ret = esp_memprot_intr_init(MEMPROT_IRAM0_SRAM)) != ESP_OK) {
812 return ret;
813 }
814 if (use_dram0 && (ret = esp_memprot_intr_init(MEMPROT_DRAM0_SRAM)) != ESP_OK) {
815 return ret;
816 }
817 if (use_peri1 && (ret = esp_memprot_intr_init(MEMPROT_PERI1_RTCSLOW)) != ESP_OK) {
818 return ret;
819 }
820 if (use_peri2 && (ret = esp_memprot_intr_init(MEMPROT_PERI2_RTCSLOW_0)) != ESP_OK) {
821 return ret;
822 }
823 }
824
825 //set permissions
826 if (required_mem_prot & MEMPROT_IRAM0_SRAM) {
827 ret = esp_memprot_set_prot_iram(MEMPROT_IRAM0_SRAM, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_ENA, EX_LOW_ENA, WR_HIGH_DIS, RD_HIGH_DIS, EX_HIGH_DIS);
828 if (ret != ESP_OK) {
829 return ret;
830 }
831 }
832 if (required_mem_prot & MEMPROT_IRAM0_RTCFAST) {
833 ret = esp_memprot_set_prot_iram(MEMPROT_IRAM0_RTCFAST, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_ENA, EX_LOW_ENA, WR_HIGH_DIS, RD_HIGH_DIS, EX_HIGH_DIS);
834 if (ret != ESP_OK) {
835 return ret;
836 }
837 }
838 if (required_mem_prot & MEMPROT_DRAM0_SRAM) {
839 ret = esp_memprot_set_prot_dram(MEMPROT_DRAM0_SRAM, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_ENA, WR_HIGH_ENA, RD_HIGH_ENA);
840 if (ret != ESP_OK) {
841 return ret;
842 }
843 }
844 if (required_mem_prot & MEMPROT_DRAM0_RTCFAST) {
845 ret = esp_memprot_set_prot_dram(MEMPROT_DRAM0_RTCFAST, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_ENA, WR_HIGH_ENA, RD_HIGH_ENA);
846 if (ret != ESP_OK) {
847 return ret;
848 }
849 }
850 if (required_mem_prot & MEMPROT_PERI1_RTCSLOW) {
851 ret = esp_memprot_set_prot_peri1(MEMPROT_PERI1_RTCSLOW, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_DIS, WR_HIGH_DIS, RD_HIGH_DIS);
852 if (ret != ESP_OK) {
853 return ret;
854 }
855 }
856 if (required_mem_prot & MEMPROT_PERI2_RTCSLOW_0) {
857 ret = esp_memprot_set_prot_peri2(MEMPROT_PERI2_RTCSLOW_0, DEF_SPLIT_LINE, WR_LOW_ENA, RD_LOW_ENA, EX_LOW_DIS, WR_HIGH_ENA, RD_HIGH_ENA, EX_HIGH_DIS);
858 if (ret != ESP_OK) {
859 return ret;
860 }
861 }
862 if (required_mem_prot & MEMPROT_PERI2_RTCSLOW_1) {
863 ret = esp_memprot_set_prot_peri2(MEMPROT_PERI2_RTCSLOW_1, DEF_SPLIT_LINE, WR_LOW_DIS, RD_LOW_DIS, EX_LOW_DIS, WR_HIGH_DIS, RD_HIGH_DIS, EX_HIGH_DIS);
864 if (ret != ESP_OK) {
865 return ret;
866 }
867 }
868
869 //reenable protection (bus based)
870 if (use_iram0 && (ret = esp_memprot_intr_ena(MEMPROT_IRAM0_SRAM, true)) != ESP_OK) {
871 return ret;
872 }
873 if (use_dram0 && (ret = esp_memprot_intr_ena(MEMPROT_DRAM0_SRAM, true)) != ESP_OK) {
874 return ret;
875 }
876 if (use_peri1 && (ret = esp_memprot_intr_ena(MEMPROT_PERI1_RTCSLOW, true)) != ESP_OK) {
877 return ret;
878 }
879 if (use_peri2 && (ret = esp_memprot_intr_ena(MEMPROT_PERI2_RTCSLOW_0, true)) != ESP_OK) {
880 return ret;
881 }
882
883 //lock if required (bus based)
884 if (lock_feature) {
885 if (use_iram0 && (ret = esp_memprot_set_lock(MEMPROT_IRAM0_SRAM)) != ESP_OK) {
886 return ret;
887 }
888 if (use_dram0 && (ret = esp_memprot_set_lock(MEMPROT_DRAM0_SRAM)) != ESP_OK) {
889 return ret;
890 }
891 if (use_peri1 && (ret = esp_memprot_set_lock(MEMPROT_PERI1_RTCSLOW)) != ESP_OK) {
892 return ret;
893 }
894 if (use_peri2 && (ret = esp_memprot_set_lock(MEMPROT_PERI2_RTCSLOW_0)) != ESP_OK) {
895 return ret;
896 }
897 }
898 }
899
900 return ESP_OK;
901 }
902
esp_memprot_get_permissions(mem_type_prot_t mem_type,bool * lw,bool * lr,bool * lx,bool * hw,bool * hr,bool * hx)903 esp_err_t esp_memprot_get_permissions(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *lx, bool *hw, bool *hr, bool *hx)
904 {
905 if (lw == NULL || lr == NULL || lx == NULL || hw == NULL || hr == NULL || hx == NULL) {
906 return ESP_ERR_INVALID_ARG;
907 }
908
909 switch (mem_type) {
910 case MEMPROT_IRAM0_SRAM:
911 memprot_ll_iram0_sram_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
912 break;
913 case MEMPROT_DRAM0_SRAM:
914 memprot_ll_dram0_sram_get_split_sgnf_bits(lw, lr, hw, hr);
915 break;
916 case MEMPROT_IRAM0_RTCFAST:
917 memprot_ll_iram0_rtcfast_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
918 break;
919 case MEMPROT_DRAM0_RTCFAST:
920 memprot_ll_dram0_rtcfast_get_split_sgnf_bits(lw, lr, hw, hr);
921 break;
922 case MEMPROT_PERI1_RTCSLOW:
923 memprot_ll_peri1_rtcslow_get_split_sgnf_bits(lw, lr, hw, hr);
924 break;
925 case MEMPROT_PERI2_RTCSLOW_0:
926 memprot_ll_peri2_rtcslow_0_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
927 break;
928 case MEMPROT_PERI2_RTCSLOW_1:
929 memprot_ll_peri2_rtcslow_1_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
930 break;
931 default:
932 return ESP_ERR_NOT_SUPPORTED;
933 }
934
935 return ESP_OK;
936 }
937
esp_memprot_get_perm_read(mem_type_prot_t mem_type,bool * lr,bool * hr)938 esp_err_t esp_memprot_get_perm_read(mem_type_prot_t mem_type, bool *lr, bool *hr)
939 {
940 bool _lw, _lr, _lx, _hw, _hr, _hx;
941 esp_err_t ret = esp_memprot_get_permissions(mem_type, &_lw, &_lr, &_lx, &_hw, &_hr, &_hx);
942 if (ret == ESP_OK) {
943 *lr = _lr;
944 *hr = _hr;
945 }
946 return ret;
947 }
948
esp_memprot_get_perm_write(mem_type_prot_t mem_type,bool * lw,bool * hw)949 esp_err_t esp_memprot_get_perm_write(mem_type_prot_t mem_type, bool *lw, bool *hw)
950 {
951 bool _lw, _lr, _lx, _hw, _hr, _hx;
952 esp_err_t ret = esp_memprot_get_permissions(mem_type, &_lw, &_lr, &_lx, &_hw, &_hr, &_hx);
953 if (ret == ESP_OK) {
954 *lw = _lw;
955 *hw = _hw;
956 }
957
958 return ret;
959 }
960
esp_memprot_get_perm_exec(mem_type_prot_t mem_type,bool * lx,bool * hx)961 esp_err_t esp_memprot_get_perm_exec(mem_type_prot_t mem_type, bool *lx, bool *hx)
962 {
963 if (mem_type == MEMPROT_DRAM0_SRAM ||
964 mem_type == MEMPROT_DRAM0_RTCFAST ||
965 mem_type == MEMPROT_PERI1_RTCSLOW) {
966 return ESP_ERR_NOT_SUPPORTED;
967 }
968
969 bool _lw, _lr, _lx, _hw, _hr, _hx;
970 esp_err_t ret = esp_memprot_get_permissions(mem_type, &_lw, &_lr, &_lx, &_hw, &_hr, &_hx);
971 if (ret == ESP_OK) {
972 *lx = _lx;
973 *hx = _hx;
974 }
975
976 return ret;
977 }
978
esp_memprot_get_low_limit(mem_type_prot_t mem_type)979 uint32_t esp_memprot_get_low_limit(mem_type_prot_t mem_type)
980 {
981 switch (mem_type) {
982 case MEMPROT_IRAM0_SRAM:
983 return IRAM0_SRAM_ADDRESS_LOW;
984 case MEMPROT_DRAM0_SRAM:
985 return DRAM0_SRAM_ADDRESS_LOW;
986 case MEMPROT_IRAM0_RTCFAST:
987 return IRAM0_RTCFAST_ADDRESS_LOW;
988 case MEMPROT_DRAM0_RTCFAST:
989 return DRAM0_RTCFAST_ADDRESS_LOW;
990 case MEMPROT_PERI1_RTCSLOW:
991 return PERI1_RTCSLOW_ADDRESS_LOW;
992 case MEMPROT_PERI2_RTCSLOW_0:
993 return PERI2_RTCSLOW_0_ADDRESS_LOW;
994 case MEMPROT_PERI2_RTCSLOW_1:
995 return PERI2_RTCSLOW_1_ADDRESS_LOW;
996 default:
997 return MEMPROT_INVALID_ADDRESS;
998 }
999 }
1000
esp_memprot_get_high_limit(mem_type_prot_t mem_type)1001 uint32_t esp_memprot_get_high_limit(mem_type_prot_t mem_type)
1002 {
1003 switch (mem_type) {
1004 case MEMPROT_IRAM0_SRAM:
1005 return IRAM0_SRAM_ADDRESS_HIGH;
1006 case MEMPROT_DRAM0_SRAM:
1007 return DRAM0_SRAM_ADDRESS_HIGH;
1008 case MEMPROT_IRAM0_RTCFAST:
1009 return IRAM0_RTCFAST_ADDRESS_HIGH;
1010 case MEMPROT_DRAM0_RTCFAST:
1011 return DRAM0_RTCFAST_ADDRESS_HIGH;
1012 case MEMPROT_PERI1_RTCSLOW:
1013 return PERI1_RTCSLOW_ADDRESS_HIGH;
1014 case MEMPROT_PERI2_RTCSLOW_0:
1015 return PERI2_RTCSLOW_0_ADDRESS_HIGH;
1016 case MEMPROT_PERI2_RTCSLOW_1:
1017 return PERI2_RTCSLOW_1_ADDRESS_HIGH;
1018 default:
1019 return MEMPROT_INVALID_ADDRESS;
1020 }
1021 }
1022
esp_memprot_set_read_perm(mem_type_prot_t mem_type,bool lr,bool hr)1023 esp_err_t esp_memprot_set_read_perm(mem_type_prot_t mem_type, bool lr, bool hr)
1024 {
1025 switch (mem_type) {
1026 case MEMPROT_IRAM0_SRAM:
1027 memprot_ll_iram0_sram_set_read_perm(lr, hr);
1028 break;
1029 case MEMPROT_DRAM0_SRAM:
1030 memprot_ll_dram0_sram_set_read_perm(lr, hr);
1031 break;
1032 case MEMPROT_IRAM0_RTCFAST:
1033 memprot_ll_iram0_rtcfast_set_read_perm(lr, hr);
1034 break;
1035 case MEMPROT_DRAM0_RTCFAST:
1036 memprot_ll_dram0_rtcfast_set_read_perm(lr, hr);
1037 break;
1038 case MEMPROT_PERI1_RTCSLOW:
1039 memprot_ll_peri1_rtcslow_set_read_perm(lr, hr);
1040 break;
1041 case MEMPROT_PERI2_RTCSLOW_0:
1042 memprot_ll_peri2_rtcslow_0_set_read_perm(lr, hr);
1043 break;
1044 case MEMPROT_PERI2_RTCSLOW_1:
1045 memprot_ll_peri2_rtcslow_1_set_read_perm(lr, hr);
1046 break;
1047 default:
1048 return ESP_ERR_NOT_SUPPORTED;
1049 }
1050
1051 return ESP_OK;
1052 }
1053
esp_memprot_set_write_perm(mem_type_prot_t mem_type,bool lw,bool hw)1054 esp_err_t esp_memprot_set_write_perm(mem_type_prot_t mem_type, bool lw, bool hw)
1055 {
1056 switch (mem_type) {
1057 case MEMPROT_IRAM0_SRAM:
1058 memprot_ll_iram0_sram_set_write_perm(lw, hw);
1059 break;
1060 case MEMPROT_DRAM0_SRAM:
1061 memprot_ll_dram0_sram_set_write_perm(lw, hw);
1062 break;
1063 case MEMPROT_IRAM0_RTCFAST:
1064 memprot_ll_iram0_rtcfast_set_write_perm(lw, hw);
1065 break;
1066 case MEMPROT_DRAM0_RTCFAST:
1067 memprot_ll_dram0_rtcfast_set_write_perm(lw, hw);
1068 break;
1069 case MEMPROT_PERI1_RTCSLOW:
1070 memprot_ll_peri1_rtcslow_set_write_perm(lw, hw);
1071 break;
1072 case MEMPROT_PERI2_RTCSLOW_0:
1073 memprot_ll_peri2_rtcslow_0_set_write_perm(lw, hw);
1074 break;
1075 case MEMPROT_PERI2_RTCSLOW_1:
1076 memprot_ll_peri2_rtcslow_1_set_write_perm(lw, hw);
1077 break;
1078 default:
1079 return ESP_ERR_NOT_SUPPORTED;
1080 }
1081
1082 return ESP_OK;
1083 }
1084
esp_memprot_set_exec_perm(mem_type_prot_t mem_type,bool lx,bool hx)1085 esp_err_t esp_memprot_set_exec_perm(mem_type_prot_t mem_type, bool lx, bool hx)
1086 {
1087 switch (mem_type) {
1088 case MEMPROT_IRAM0_SRAM:
1089 memprot_ll_iram0_sram_set_exec_perm(lx, hx);
1090 break;
1091 case MEMPROT_IRAM0_RTCFAST:
1092 memprot_ll_iram0_rtcfast_set_exec_perm(lx, hx);
1093 break;
1094 case MEMPROT_PERI2_RTCSLOW_0:
1095 memprot_ll_peri2_rtcslow_0_set_exec_perm(lx, hx);
1096 break;
1097 case MEMPROT_PERI2_RTCSLOW_1:
1098 memprot_ll_peri2_rtcslow_1_set_exec_perm(lx, hx);
1099 break;
1100 default:
1101 return ESP_ERR_NOT_SUPPORTED;
1102 }
1103
1104 return ESP_OK;
1105 }
1106