xref: /optee_os/core/arch/arm/plat-telechips/main.c (revision a0f3154cfa75eda772785dfcb586b916514d7007)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2024, Telechips Inc.
4  */
5 
6 #include <console.h>
7 #include <crypto/crypto.h>
8 #include <drivers/gic.h>
9 #include <drivers/pl011.h>
10 #include <drivers/tcc_otp.h>
11 #include <kernel/boot.h>
12 #include <kernel/tee_common_otp.h>
13 #include <otprom.h>
14 #include <platform_config.h>
15 
16 register_phys_mem(MEM_AREA_IO_SEC, TCC_IO_BASE, TCC_IO_SIZE);
17 #if defined(TZC_BASE)
18 register_phys_mem(MEM_AREA_IO_SEC, TZC_BASE, TZC_SIZE);
19 #endif
20 
21 register_ddr(DRAM0_BASE, DRAM0_SIZE);
22 #if defined(DRAM1_BASE)
23 register_ddr(DRAM1_BASE, DRAM1_SIZE);
24 #endif
25 
26 static bool huk_is_ready;
27 static uint32_t plat_huk[OTP_DATA_TEE_HUK_SIZE / sizeof(uint32_t)];
28 
29 void boot_primary_init_intc(void)
30 {
31 	gic_init(GICC_BASE, GICD_BASE);
32 }
33 
34 void boot_secondary_init_intc(void)
35 {
36 	gic_init_per_cpu();
37 }
38 
39 void plat_console_init(void)
40 {
41 #if defined(CFG_PL011)
42 	static struct pl011_data console_data;
43 
44 	pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ,
45 		   CONSOLE_BAUDRATE);
46 	register_serial_console(&console_data.chip);
47 #endif
48 }
49 
50 TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey)
51 {
52 	static_assert(sizeof(plat_huk) == sizeof(hwkey->data));
53 
54 	if (!huk_is_ready)
55 		return TEE_ERROR_GENERIC;
56 
57 	memcpy(hwkey->data, plat_huk, OTP_DATA_TEE_HUK_SIZE);
58 	return TEE_SUCCESS;
59 }
60 
61 static TEE_Result init_huk(void)
62 {
63 	TEE_Result res = TEE_ERROR_GENERIC;
64 
65 	res = tcc_otp_read_128(OTP_DATA_TEE_HUK_OFFSET, plat_huk);
66 	if (res == TEE_ERROR_NO_DATA) {
67 		IMSG("There is no HUK in OTP. Starting HUK Provisioning");
68 		if (!crypto_rng_read(plat_huk, OTP_DATA_TEE_HUK_SIZE)) {
69 			tcc_otp_write_128(OTP_DATA_TEE_HUK_OFFSET, plat_huk);
70 			res = tcc_otp_read_128(OTP_DATA_TEE_HUK_OFFSET,
71 					       plat_huk);
72 			if (res != TEE_SUCCESS)
73 				EMSG("Failed to store HUK to OTP");
74 		} else {
75 			EMSG("Failed to generate random number for HUK");
76 		}
77 	}
78 
79 	if (res == TEE_SUCCESS)
80 		huk_is_ready = true;
81 	else
82 		EMSG("Failed to get HUK from OTP");
83 
84 	return res;
85 }
86 service_init(init_huk);
87