1 /*
2  * Copyright 2018-2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <assert.h>
9 #include <stdint.h>
10 #include <string.h>
11 
12 #include <common/debug.h>
13 #include <lib/cassert.h>
14 #include <sfp.h>
15 #include <tools_share/tbbr_oid.h>
16 
17 #include <plat/common/platform.h>
18 #include "plat_common.h"
19 
20 extern char nxp_rotpk_hash[], nxp_rotpk_hash_end[];
21 
plat_get_rotpk_info(void * cookie,void ** key_ptr,unsigned int * key_len,unsigned int * flags)22 int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len,
23 			unsigned int *flags)
24 {
25 	*key_ptr = nxp_rotpk_hash;
26 	*key_len = nxp_rotpk_hash_end - nxp_rotpk_hash;
27 	*flags = ROTPK_IS_HASH;
28 
29 	return 0;
30 }
31 
plat_get_nv_ctr(void * cookie,unsigned int * nv_ctr)32 int plat_get_nv_ctr(void *cookie, unsigned int *nv_ctr)
33 {
34 	const char *oid;
35 	uint32_t uid_num;
36 	uint32_t val = 0U;
37 
38 	assert(cookie != NULL);
39 	assert(nv_ctr != NULL);
40 
41 	oid = (const char *)cookie;
42 	if (strcmp(oid, TRUSTED_FW_NVCOUNTER_OID) == 0) {
43 		uid_num = 3U;
44 	} else if (strcmp(oid, NON_TRUSTED_FW_NVCOUNTER_OID) == 0) {
45 		uid_num = 4U;
46 	} else {
47 		return 1;
48 	}
49 
50 	val = sfp_read_oem_uid(uid_num);
51 
52 	INFO("SFP Value read is %x from UID %d\n", val, uid_num);
53 	if (val == 0U) {
54 		*nv_ctr = 0U;
55 	} else {
56 		*nv_ctr = (32U - __builtin_clz(val));
57 	}
58 
59 	INFO("NV Counter value for UID %d is %d\n", uid_num, *nv_ctr);
60 	return 0;
61 
62 }
63 
plat_set_nv_ctr(void * cookie,unsigned int nv_ctr)64 int plat_set_nv_ctr(void *cookie, unsigned int nv_ctr)
65 {
66 	const char *oid;
67 	uint32_t uid_num, sfp_val;
68 
69 	assert(cookie != NULL);
70 
71 	/* Counter values upto 32 are supported */
72 	if (nv_ctr > 32U) {
73 		return 1;
74 	}
75 
76 	oid = (const char *)cookie;
77 	if (strcmp(oid, TRUSTED_FW_NVCOUNTER_OID) == 0) {
78 		uid_num = 3U;
79 	} else if (strcmp(oid, NON_TRUSTED_FW_NVCOUNTER_OID) == 0) {
80 		uid_num = 4U;
81 	} else {
82 		return 1;
83 	}
84 	sfp_val = (1U << (nv_ctr - 1));
85 
86 	if (sfp_write_oem_uid(uid_num, sfp_val) == 1) {
87 		/* Enable POVDD on board */
88 		if (board_enable_povdd()) {
89 			sfp_program_fuses();
90 		}
91 
92 		/* Disable POVDD on board */
93 		board_disable_povdd();
94 	} else {
95 		ERROR("Invalid OEM UID sent.\n");
96 		return 1;
97 	}
98 
99 	return 0;
100 }
101 
plat_get_mbedtls_heap(void ** heap_addr,size_t * heap_size)102 int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
103 {
104 	return get_mbedtls_heap_helper(heap_addr, heap_size);
105 }
106