blob: 95a61ec52c3df861559eb1a4faca7476ffc29bfb [file] [log] [blame]
David Vincze060968d2019-05-23 01:13:14 +02001/*
Tintu Thomaseab1b472022-03-21 14:27:58 +00002 * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
David Vincze060968d2019-05-23 01:13:14 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Balint Matyi69e2d2e2020-07-08 10:53:54 +01008#include "bootutil/security_cnt.h"
David Vincze060968d2019-05-23 01:13:14 +02009#include "../../platform/include/tfm_plat_nv_counters.h"
10#include "../../platform/include/tfm_plat_defs.h"
Tamas Ban1bfc9da2020-07-09 13:55:38 +010011#include "bootutil/fault_injection_hardening.h"
David Vincze060968d2019-05-23 01:13:14 +020012#include <stdint.h>
13
Raef Coles91fadb92021-06-18 09:20:50 +010014#define TFM_BOOT_NV_COUNTER_0 PLAT_NV_COUNTER_BL2_0 /* NV counter of Image 0 */
15#define TFM_BOOT_NV_COUNTER_1 PLAT_NV_COUNTER_BL2_1 /* NV counter of Image 1 */
16#define TFM_BOOT_NV_COUNTER_2 PLAT_NV_COUNTER_BL2_2 /* NV counter of Image 2 */
Tintu Thomaseab1b472022-03-21 14:27:58 +000017#define TFM_BOOT_NV_COUNTER_3 PLAT_NV_COUNTER_BL2_3 /* NV counter of Image 3 */
18#define TFM_BOOT_NV_COUNTER_MAX PLAT_NV_COUNTER_BL2_3 + 1
David Vincze060968d2019-05-23 01:13:14 +020019
20static enum tfm_nv_counter_t get_nv_counter_from_image_id(uint32_t image_id)
21{
22 uint32_t nv_counter;
23
24 /* Avoid integer overflow */
25 if ((UINT32_MAX - TFM_BOOT_NV_COUNTER_0) < image_id) {
26 return TFM_BOOT_NV_COUNTER_MAX;
27 }
28
29 nv_counter = TFM_BOOT_NV_COUNTER_0 + image_id;
30
31 /* Check the existence of the enumerated counter value */
32 if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) {
33 return TFM_BOOT_NV_COUNTER_MAX;
34 }
35
36 return (enum tfm_nv_counter_t)nv_counter;
37}
38
Tamas Ban1bfc9da2020-07-09 13:55:38 +010039fih_int boot_nv_security_counter_init(void)
David Vincze060968d2019-05-23 01:13:14 +020040{
Tamas Ban1bfc9da2020-07-09 13:55:38 +010041 fih_int fih_rc = FIH_FAILURE;
David Vincze060968d2019-05-23 01:13:14 +020042
Tamas Ban1bfc9da2020-07-09 13:55:38 +010043 fih_rc = fih_int_encode_zero_equality(tfm_plat_init_nv_counter());
David Vincze060968d2019-05-23 01:13:14 +020044
Tamas Ban1bfc9da2020-07-09 13:55:38 +010045 FIH_RET(fih_rc);
David Vincze060968d2019-05-23 01:13:14 +020046}
47
Tamas Ban1bfc9da2020-07-09 13:55:38 +010048fih_int boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt)
David Vincze060968d2019-05-23 01:13:14 +020049{
50 enum tfm_nv_counter_t nv_counter;
Tamas Ban1bfc9da2020-07-09 13:55:38 +010051 fih_int fih_rc = FIH_FAILURE;
52 uint32_t security_cnt_soft;
David Vincze060968d2019-05-23 01:13:14 +020053
54 /* Check if it's a null-pointer. */
55 if (!security_cnt) {
Tamas Ban1bfc9da2020-07-09 13:55:38 +010056 FIH_RET(FIH_FAILURE);
David Vincze060968d2019-05-23 01:13:14 +020057 }
58
59 nv_counter = get_nv_counter_from_image_id(image_id);
Raef Coles91fadb92021-06-18 09:20:50 +010060 if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) {
Tamas Ban1bfc9da2020-07-09 13:55:38 +010061 FIH_RET(FIH_FAILURE);
David Vincze060968d2019-05-23 01:13:14 +020062 }
63
Tamas Ban1bfc9da2020-07-09 13:55:38 +010064 fih_rc = fih_int_encode_zero_equality(
65 tfm_plat_read_nv_counter(nv_counter,
66 sizeof(security_cnt_soft),
67 (uint8_t *)&security_cnt_soft));
68 *security_cnt = fih_int_encode(security_cnt_soft);
David Vincze060968d2019-05-23 01:13:14 +020069
Tamas Ban1bfc9da2020-07-09 13:55:38 +010070 FIH_RET(fih_rc);
David Vincze060968d2019-05-23 01:13:14 +020071}
72
73int32_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);
Raef Coles91fadb92021-06-18 09:20:50 +010080 if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) {
David Vincze060968d2019-05-23 01:13:14 +020081 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}