|
| 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_timer.h> |
| 9 | +#include <zephyr/drivers/retained_mem.h> |
| 10 | + |
| 11 | +/* As peripheral registers are not cleared during RAM cleanup, we can |
| 12 | + * use a register of a peripheral unused by NSIB to save data which can then |
| 13 | + * be read by the application. |
| 14 | + * We use this trick to save the start and end addresses of the RAMFUNC region. |
| 15 | + * This region is not cleared during the RAM cleanup, as it contains the code responsible |
| 16 | + * for performing the RAM cleanup, so it must be skipped when checking if the RAM cleanup |
| 17 | + * has been successfully performed. |
| 18 | + * The TIMER00 is not used by NSIB, and is therefore its CC[0] and CC[1] registers |
| 19 | + * are used for this purpose. |
| 20 | + */ |
| 21 | +#define RAMFUNC_START_SAVE_REGISTER NRF_TIMER00->CC[0] |
| 22 | +#define RAMFUNC_END_SAVE_REGISTER NRF_TIMER00->CC[1] |
| 23 | + |
| 24 | +/* Using 2-byte magic values allows to load the whole value with movw */ |
| 25 | +#define RAM_CLEANUP_SUCCESS_MAGIC 0x5678 |
| 26 | +#define RAM_CLEANUP_FAILURE_MAGIC 0x4321 |
| 27 | + |
| 28 | +static uint32_t __noinit ram_cleanup_result; |
| 29 | +static uint32_t __noinit uncleared_address; |
| 30 | +static uint32_t __noinit uncleared_value; |
| 31 | + |
| 32 | +#define EXPECTED_RETAINED_DATA "RETAINED" |
| 33 | + |
| 34 | +/** |
| 35 | + * This hook needs to have the attribute __attribute__((naked)), |
| 36 | + * as otherwise the stack would be modified when calling it, leading to |
| 37 | + * test failure, as part of the RAM in which the stack resides wouldn't |
| 38 | + * be zeroed. |
| 39 | + */ |
| 40 | +__attribute__((naked)) void soc_early_reset_hook(void) |
| 41 | +{ |
| 42 | + __asm__ volatile ( |
| 43 | + /* Load zero (value used to clear memory) into r0 */ |
| 44 | + " mov r0, #0\n" |
| 45 | + /* Explicitly clear the ram_cleanup_result variable */ |
| 46 | + " mov r2, %6\n" |
| 47 | + " str r0, [r2]\n" |
| 48 | + /* Load the location of the saved ram func start address to r1 */ |
| 49 | + " mov r1, %0\n" |
| 50 | + /* Load the ram func start address to r2 */ |
| 51 | + " ldr r2, [r1]\n" |
| 52 | + /* Load the location of the saved ram func end address to r1 */ |
| 53 | + " mov r1, %1\n" |
| 54 | + /* Load the ram func end address to r3*/ |
| 55 | + " ldr r3, [r1]\n" |
| 56 | + /* Clear memory from ram func start to ram func end. |
| 57 | + * The area is skipped during the RAM cleanup, |
| 58 | + * so it is not cleared. |
| 59 | + * Simply cleaning it up here is quicker |
| 60 | + * than verifying if the current address is within the gap |
| 61 | + * in each iteration of the loop. |
| 62 | + */ |
| 63 | + "ram_func_zero_loop:\n" |
| 64 | + " cmp r2, r3\n" |
| 65 | + " bge ram_func_zero_loop_done\n" |
| 66 | + " str r0, [r2]\n" |
| 67 | + /* Increment the address by 4 (word size) */ |
| 68 | + " add r2, r2, #4\n" |
| 69 | + " b ram_func_zero_loop\n" |
| 70 | + "ram_func_zero_loop_done:\n" |
| 71 | + /* Verify that all of the RAM memory has been cleared */ |
| 72 | + /* Load SRAM base address to r1 */ |
| 73 | + " mov r1, %2\n" |
| 74 | + /* Load SRAM size to r2 */ |
| 75 | + " mov r2, %3\n" |
| 76 | + /* Calculate SRAM end address (base + size) */ |
| 77 | + " add r2, r1, r2\n" |
| 78 | + "verify_ram_loop:\n" |
| 79 | + " cmp r1, r2\n" |
| 80 | + " bge verify_ram_done\n" |
| 81 | + /* Load value at current address and verify if it equals 0 */ |
| 82 | + " ldr r3, [r1]\n" |
| 83 | + " cmp r3, #0\n" |
| 84 | + " bne ram_cleanup_failed\n" |
| 85 | + /* Increment the address by 4 (word size) */ |
| 86 | + " add r1, r1, #4\n" |
| 87 | + " b verify_ram_loop\n" |
| 88 | + "ram_cleanup_failed:\n" |
| 89 | + /* Load RAM_CLEANUP_FAILURE_MAGIC into r0 */ |
| 90 | + " movw r0, %4\n" |
| 91 | + /* Save the uncleared address in uncleared_address variable */ |
| 92 | + " mov r2, %7\n" |
| 93 | + " str r1, [r2]\n" |
| 94 | + /* Save the uncleared value in uncleared_value variable */ |
| 95 | + " mov r2, %8\n" |
| 96 | + " str r3, [r2]\n" |
| 97 | + " b verification_done\n" |
| 98 | + "verify_ram_done:\n" |
| 99 | + /* Load RAM_CLEANUP_SUCCESS_MAGIC */ |
| 100 | + " movw r0, %5\n" |
| 101 | + "verification_done:\n" |
| 102 | + /* Store result in ram_cleanup_result variable */ |
| 103 | + " mov r2, %6\n" |
| 104 | + " str r0, [r2]\n" |
| 105 | + /* __attribute__((naked)) requires manual branching */ |
| 106 | + " bx lr\n" |
| 107 | + : |
| 108 | + : "r" (&RAMFUNC_START_SAVE_REGISTER), |
| 109 | + "r" (&RAMFUNC_END_SAVE_REGISTER), |
| 110 | + "r" (CONFIG_SRAM_BASE_ADDRESS), |
| 111 | + "r" (CONFIG_SRAM_SIZE * 1024), |
| 112 | + "i" (RAM_CLEANUP_FAILURE_MAGIC), |
| 113 | + "i" (RAM_CLEANUP_SUCCESS_MAGIC), |
| 114 | + "r" (&ram_cleanup_result), |
| 115 | + "r" (&uncleared_address), |
| 116 | + "r" (&uncleared_value) |
| 117 | + : "r0", "r1", "r2", "r3", "lr", "memory" |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +ZTEST(b0_ram_cleanup, test_ram_cleanup) |
| 122 | +{ |
| 123 | + zassert_true((ram_cleanup_result == RAM_CLEANUP_SUCCESS_MAGIC) || |
| 124 | + (ram_cleanup_result == RAM_CLEANUP_FAILURE_MAGIC), |
| 125 | + "RAM cleanup result should be either success or failure"); |
| 126 | + zassert_equal(ram_cleanup_result, RAM_CLEANUP_SUCCESS_MAGIC, |
| 127 | + "Uncleared word detected at address %p, value 0x%x", uncleared_address, |
| 128 | + uncleared_value); |
| 129 | + |
| 130 | +#if DT_HAS_COMPAT_STATUS_OKAY(zephyr_retained_ram) |
| 131 | + const struct device *retained_mem_dev = |
| 132 | + DEVICE_DT_GET(DT_INST(0, zephyr_retained_ram)); |
| 133 | + size_t retained_size; |
| 134 | + uint8_t buffer[strlen(EXPECTED_RETAINED_DATA)+1]; |
| 135 | + |
| 136 | + zassert_true(device_is_ready(retained_mem_dev), "Retained memory device is not ready"); |
| 137 | + |
| 138 | + retained_size = retained_mem_size(retained_mem_dev); |
| 139 | + |
| 140 | + zassert_true(retained_size >= strlen(EXPECTED_RETAINED_DATA), |
| 141 | + "Retained memory size is too small"); |
| 142 | + |
| 143 | + retained_mem_read(retained_mem_dev, 0, buffer, strlen(EXPECTED_RETAINED_DATA)); |
| 144 | + zassert_mem_equal(buffer, EXPECTED_RETAINED_DATA, strlen(EXPECTED_RETAINED_DATA), |
| 145 | + "Retained data is not correct"); |
| 146 | +#endif |
| 147 | +} |
| 148 | + |
| 149 | +ZTEST_SUITE(b0_ram_cleanup, NULL, NULL, NULL, NULL, NULL); |
0 commit comments