|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | +#include <hal/nrf_rramc.h> |
| 9 | + |
| 10 | +#define RRAMC_REGION_FOR_BOOTCONF 3 |
| 11 | +static uint32_t expected_fatal; |
| 12 | +static uint32_t actual_fatal; |
| 13 | +static nrf_rramc_region_config_t config; |
| 14 | + |
| 15 | +void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf) |
| 16 | +{ |
| 17 | + printk("Caught system error -- reason %d\n", reason); |
| 18 | + actual_fatal++; |
| 19 | +} |
| 20 | + |
| 21 | +void check_fatal(void *unused) |
| 22 | +{ |
| 23 | + zassert_equal(expected_fatal, actual_fatal, |
| 24 | + "Wrong number of fatal errors has occurred (e:%d != a:%d).", |
| 25 | + expected_fatal, actual_fatal); |
| 26 | +} |
| 27 | + |
| 28 | +void *get_config(void) |
| 29 | +{ |
| 30 | + nrf_rramc_region_config_get(NRF_RRAMC, |
| 31 | + RRAMC_REGION_FOR_BOOTCONF, |
| 32 | + &config); |
| 33 | + zassert_equal(0, config.permissions & |
| 34 | + (NRF_RRAMC_REGION_PERM_READ_MASK | |
| 35 | + NRF_RRAMC_REGION_PERM_WRITE_MASK | |
| 36 | + NRF_RRAMC_REGION_PERM_EXECUTE_MASK), |
| 37 | + "Read Write and eXecute permissions aren't cleared"); |
| 38 | + zassert_true(config.size_kb > 0, "Protected region has zero size."); |
| 39 | + return NULL; |
| 40 | +} |
| 41 | + |
| 42 | +ZTEST(b0_self_lock_test, test_reading_b0_image) |
| 43 | +{ |
| 44 | + uint32_t protected_end_address = 1024 * config.size_kb; |
| 45 | + int val; |
| 46 | + |
| 47 | + printk("Legal read\n"); |
| 48 | + val = *((volatile int*)protected_end_address); |
| 49 | + config.permissions = NRF_RRAMC_REGION_PERM_READ_MASK | |
| 50 | + NRF_RRAMC_REGION_PERM_WRITE_MASK | |
| 51 | + NRF_RRAMC_REGION_PERM_EXECUTE_MASK; |
| 52 | + /* Try unlocking. This should take no effect at this point */ |
| 53 | + nrf_rramc_region_config_set(NRF_RRAMC, RRAMC_REGION_FOR_BOOTCONF, &config); |
| 54 | + printk("Illegal read\n"); |
| 55 | + expected_fatal++; |
| 56 | + val = *((volatile int*)protected_end_address-1); |
| 57 | +} |
| 58 | + |
| 59 | +ZTEST_SUITE(b0_self_lock_test, NULL, get_config, NULL, check_fatal, NULL); |
0 commit comments