David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1 | /* |
Balint Matyi | 69e2d2e | 2020-07-08 10:53:54 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Balint Matyi | 69e2d2e | 2020-07-08 10:53:54 +0100 | [diff] [blame] | 8 | #include "bootutil/security_cnt.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 9 | #include "../../platform/include/tfm_plat_nv_counters.h" |
| 10 | #include "../../platform/include/tfm_plat_defs.h" |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #define TFM_BOOT_NV_COUNTER_0 PLAT_NV_COUNTER_3 /* NV counter of Image 0 */ |
David Vincze | 9d96328 | 2020-06-05 09:18:35 +0200 | [diff] [blame] | 14 | #define TFM_BOOT_NV_COUNTER_1 PLAT_NV_COUNTER_4 /* NV counter of Image 1 */ |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 15 | #define TFM_BOOT_NV_COUNTER_MAX PLAT_NV_COUNTER_MAX |
| 16 | |
| 17 | static enum tfm_nv_counter_t get_nv_counter_from_image_id(uint32_t image_id) |
| 18 | { |
| 19 | uint32_t nv_counter; |
| 20 | |
| 21 | /* Avoid integer overflow */ |
| 22 | if ((UINT32_MAX - TFM_BOOT_NV_COUNTER_0) < image_id) { |
| 23 | return TFM_BOOT_NV_COUNTER_MAX; |
| 24 | } |
| 25 | |
| 26 | nv_counter = TFM_BOOT_NV_COUNTER_0 + image_id; |
| 27 | |
| 28 | /* Check the existence of the enumerated counter value */ |
| 29 | if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) { |
| 30 | return TFM_BOOT_NV_COUNTER_MAX; |
| 31 | } |
| 32 | |
| 33 | return (enum tfm_nv_counter_t)nv_counter; |
| 34 | } |
| 35 | |
| 36 | int32_t boot_nv_security_counter_init(void) |
| 37 | { |
| 38 | enum tfm_plat_err_t err; |
| 39 | |
| 40 | err = tfm_plat_init_nv_counter(); |
| 41 | if (err != TFM_PLAT_ERR_SUCCESS) { |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int32_t boot_nv_security_counter_get(uint32_t image_id, uint32_t *security_cnt) |
| 49 | { |
| 50 | enum tfm_nv_counter_t nv_counter; |
| 51 | enum tfm_plat_err_t err; |
| 52 | |
| 53 | /* Check if it's a null-pointer. */ |
| 54 | if (!security_cnt) { |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | nv_counter = get_nv_counter_from_image_id(image_id); |
| 59 | if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) { |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | err = tfm_plat_read_nv_counter(nv_counter, |
| 64 | sizeof(*security_cnt), |
| 65 | (uint8_t *)security_cnt); |
| 66 | if (err != TFM_PLAT_ERR_SUCCESS) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int32_t boot_nv_security_counter_update(uint32_t image_id, |
| 74 | uint32_t img_security_cnt) |
| 75 | { |
| 76 | enum tfm_nv_counter_t nv_counter; |
| 77 | enum tfm_plat_err_t err; |
| 78 | |
| 79 | nv_counter = get_nv_counter_from_image_id(image_id); |
| 80 | if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) { |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | err = tfm_plat_set_nv_counter(nv_counter, img_security_cnt); |
| 85 | if (err != TFM_PLAT_ERR_SUCCESS) { |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |