1 /*
2  * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "otp.h"
9 
10 #include <stdint.h>
11 
12 #include "region_defs.h"
13 #include "tfm_plat_otp.h"
14 #include "tfm_plat_nv_counters.h"
15 #include "util.h"
16 
17 #ifdef TEST_BL1_1
18 extern uint8_t tfm_bl1_key_test_1_buf[];
19 extern uint8_t tfm_bl1_key_test_2_buf[];
20 #endif /* TEST_BL1_1 */
21 
22 fih_int bl1_otp_read(uint8_t *dst, uint8_t *src, size_t size);
23 fih_int bl1_otp_write(uint8_t *dst, uint8_t *src, size_t size);
24 
bl1_otp_read_key(enum tfm_bl1_key_id_t key_id,uint8_t * key_buf)25 fih_int bl1_otp_read_key(enum tfm_bl1_key_id_t key_id, uint8_t *key_buf)
26 {
27     fih_int fih_rc;
28     enum tfm_plat_err_t plat_err;
29 
30     switch (key_id) {
31     case TFM_BL1_KEY_HUK:
32         plat_err = tfm_plat_otp_read(PLAT_OTP_ID_HUK, 32, key_buf);
33         fih_rc = fih_int_encode_zero_equality(plat_err);
34         break;
35     case TFM_BL1_KEY_GUK:
36         plat_err = tfm_plat_otp_read(PLAT_OTP_ID_GUK, GUK_SIZE, key_buf);
37         fih_rc = fih_int_encode_zero_equality(plat_err);
38         break;
39     case TFM_BL1_KEY_BL2_ENCRYPTION:
40         plat_err = tfm_plat_otp_read(PLAT_OTP_ID_KEY_BL2_ENCRYPTION, 32,
41                                      key_buf);
42         fih_rc = fih_int_encode_zero_equality(plat_err);
43         break;
44     case TFM_BL1_KEY_ROTPK_0:
45         plat_err = tfm_plat_otp_read(PLAT_OTP_ID_BL1_ROTPK_0, 56, key_buf);
46         fih_rc = fih_int_encode_zero_equality(plat_err);
47         break;
48     default:
49         FIH_RET(FIH_FAILURE);
50     }
51 
52     FIH_RET(fih_rc);
53 }
54