blob: 7fc6e4c292748066325510edfb4f5c8b532cbe83 [file] [log] [blame]
David Vincze060968d2019-05-23 01:13:14 +02001/*
Balint Matyi69e2d2e2020-07-08 10:53:54 +01002 * Copyright (c) 2019-2020, 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
14#define TFM_BOOT_NV_COUNTER_0 PLAT_NV_COUNTER_3 /* NV counter of Image 0 */
David Vincze9d963282020-06-05 09:18:35 +020015#define TFM_BOOT_NV_COUNTER_1 PLAT_NV_COUNTER_4 /* NV counter of Image 1 */
David Vincze060968d2019-05-23 01:13:14 +020016#define TFM_BOOT_NV_COUNTER_MAX PLAT_NV_COUNTER_MAX
17
18static enum tfm_nv_counter_t get_nv_counter_from_image_id(uint32_t image_id)
19{
20 uint32_t nv_counter;
21
22 /* Avoid integer overflow */
23 if ((UINT32_MAX - TFM_BOOT_NV_COUNTER_0) < image_id) {
24 return TFM_BOOT_NV_COUNTER_MAX;
25 }
26
27 nv_counter = TFM_BOOT_NV_COUNTER_0 + image_id;
28
29 /* Check the existence of the enumerated counter value */
30 if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) {
31 return TFM_BOOT_NV_COUNTER_MAX;
32 }
33
34 return (enum tfm_nv_counter_t)nv_counter;
35}
36
Tamas Ban1bfc9da2020-07-09 13:55:38 +010037fih_int boot_nv_security_counter_init(void)
David Vincze060968d2019-05-23 01:13:14 +020038{
Tamas Ban1bfc9da2020-07-09 13:55:38 +010039 fih_int fih_rc = FIH_FAILURE;
David Vincze060968d2019-05-23 01:13:14 +020040
Tamas Ban1bfc9da2020-07-09 13:55:38 +010041 fih_rc = fih_int_encode_zero_equality(tfm_plat_init_nv_counter());
David Vincze060968d2019-05-23 01:13:14 +020042
Tamas Ban1bfc9da2020-07-09 13:55:38 +010043 FIH_RET(fih_rc);
David Vincze060968d2019-05-23 01:13:14 +020044}
45
Tamas Ban1bfc9da2020-07-09 13:55:38 +010046fih_int boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt)
David Vincze060968d2019-05-23 01:13:14 +020047{
48 enum tfm_nv_counter_t nv_counter;
Tamas Ban1bfc9da2020-07-09 13:55:38 +010049 fih_int fih_rc = FIH_FAILURE;
50 uint32_t security_cnt_soft;
David Vincze060968d2019-05-23 01:13:14 +020051
52 /* Check if it's a null-pointer. */
53 if (!security_cnt) {
Tamas Ban1bfc9da2020-07-09 13:55:38 +010054 FIH_RET(FIH_FAILURE);
David Vincze060968d2019-05-23 01:13:14 +020055 }
56
57 nv_counter = get_nv_counter_from_image_id(image_id);
58 if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) {
Tamas Ban1bfc9da2020-07-09 13:55:38 +010059 FIH_RET(FIH_FAILURE);
David Vincze060968d2019-05-23 01:13:14 +020060 }
61
Tamas Ban1bfc9da2020-07-09 13:55:38 +010062 fih_rc = fih_int_encode_zero_equality(
63 tfm_plat_read_nv_counter(nv_counter,
64 sizeof(security_cnt_soft),
65 (uint8_t *)&security_cnt_soft));
66 *security_cnt = fih_int_encode(security_cnt_soft);
David Vincze060968d2019-05-23 01:13:14 +020067
Tamas Ban1bfc9da2020-07-09 13:55:38 +010068 FIH_RET(fih_rc);
David Vincze060968d2019-05-23 01:13:14 +020069}
70
71int32_t boot_nv_security_counter_update(uint32_t image_id,
72 uint32_t img_security_cnt)
73{
74 enum tfm_nv_counter_t nv_counter;
75 enum tfm_plat_err_t err;
76
77 nv_counter = get_nv_counter_from_image_id(image_id);
78 if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) {
79 return -1;
80 }
81
82 err = tfm_plat_set_nv_counter(nv_counter, img_security_cnt);
83 if (err != TFM_PLAT_ERR_SUCCESS) {
84 return -1;
85 }
86
87 return 0;
88}