Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2023 Arm Limited |
| 5 | */ |
| 6 | |
| 7 | #include "bootutil/security_cnt.h" |
| 8 | #include "mcuboot_config/mcuboot_logging.h" |
| 9 | #include "bootutil/fault_injection_hardening.h" |
| 10 | |
| 11 | /* |
| 12 | * Since the simulator is executing unit tests in parallel, |
| 13 | * the storage area where the security counter values reside |
| 14 | * has to be managed per thread from Rust's side. |
| 15 | */ |
| 16 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 17 | |
| 18 | int sim_set_nv_counter_for_image(uint32_t image_index, uint32_t security_counter_value); |
| 19 | |
| 20 | int sim_get_nv_counter_for_image(uint32_t image_index, uint32_t* data); |
| 21 | |
| 22 | fih_ret boot_nv_security_counter_init(void) { |
| 23 | return FIH_SUCCESS; |
| 24 | } |
| 25 | |
| 26 | fih_ret boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt) { |
| 27 | uint32_t counter = 0; |
| 28 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 29 | fih_rc = fih_ret_encode_zero_equality(sim_get_nv_counter_for_image(image_id, &counter)); |
| 30 | |
| 31 | MCUBOOT_LOG_INF("Read security counter value (%d) for image: %d\n", counter, image_id); |
| 32 | *security_cnt = fih_int_encode(counter); |
| 33 | |
| 34 | FIH_RET(fih_rc); |
| 35 | } |
| 36 | |
| 37 | int32_t boot_nv_security_counter_update(uint32_t image_id, uint32_t img_security_cnt) { |
| 38 | MCUBOOT_LOG_INF("Writing security counter value (%d) for image: %d\n", img_security_cnt, image_id); |
| 39 | |
| 40 | return sim_set_nv_counter_for_image(image_id, img_security_cnt); |
| 41 | } |
| 42 | |
| 43 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |