xref: /rk3399_ARM-atf/plat/common/plat_bl_common.c (revision 689dcfe0e31d5c2a0aab7e0d19a50001072de6b3)
1566034fcSSoby Mathew /*
2*3537dad5SXialin Liu  * Copyright (c) 2018-2025, Arm Limited and Contributors. All rights reserved.
3566034fcSSoby Mathew  *
4566034fcSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5566034fcSSoby Mathew  */
6566034fcSSoby Mathew 
7566034fcSSoby Mathew #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1009d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1109d40e0eSAntonio Nino Diaz #include <common/debug.h>
12b5d0740eSHarrison Mutai #if TRANSFER_LIST
13b5d0740eSHarrison Mutai #include <transfer_list.h>
14b5d0740eSHarrison Mutai #endif
1509d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
1609d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
176f0a2f04SManish V Badarkhe #include <services/arm_arch_svc.h>
180e753437SManish V Badarkhe #include <smccc_helpers.h>
192be57b86SSumit Garg #include <tools_share/firmware_encrypted.h>
20566034fcSSoby Mathew 
21566034fcSSoby Mathew /*
22566034fcSSoby Mathew  * The following platform functions are weakly defined. The Platforms
23566034fcSSoby Mathew  * may redefine with strong definition.
24566034fcSSoby Mathew  */
25566034fcSSoby Mathew #pragma weak bl2_el3_plat_prepare_exit
26566034fcSSoby Mathew #pragma weak plat_error_handler
27566034fcSSoby Mathew #pragma weak bl2_plat_preload_setup
28566034fcSSoby Mathew #pragma weak bl2_plat_handle_pre_image_load
29566034fcSSoby Mathew #pragma weak bl2_plat_handle_post_image_load
302be57b86SSumit Garg #pragma weak plat_get_enc_key_info
316f0a2f04SManish V Badarkhe #pragma weak plat_is_smccc_feature_available
320e753437SManish V Badarkhe #pragma weak plat_get_soc_version
330e753437SManish V Badarkhe #pragma weak plat_get_soc_revision
34cb4ee3e4SArvind Ram Prakash #pragma weak plat_get_soc_name
350e753437SManish V Badarkhe 
36*3537dad5SXialin Liu /* Pointer and function to register platform function to log GPT corruption */
37*3537dad5SXialin Liu const struct plat_log_gpt_corrupted *plat_log_gpt_ptr;
38*3537dad5SXialin Liu 
plat_setup_log_gpt_corrupted(const struct plat_log_gpt_corrupted * log_gpt)39*3537dad5SXialin Liu void plat_setup_log_gpt_corrupted(const struct plat_log_gpt_corrupted *log_gpt)
40*3537dad5SXialin Liu {
41*3537dad5SXialin Liu 	plat_log_gpt_ptr = log_gpt;
42*3537dad5SXialin Liu }
43*3537dad5SXialin Liu 
plat_get_soc_version(void)440e753437SManish V Badarkhe int32_t plat_get_soc_version(void)
450e753437SManish V Badarkhe {
460e753437SManish V Badarkhe 	return SMC_ARCH_CALL_NOT_SUPPORTED;
470e753437SManish V Badarkhe }
480e753437SManish V Badarkhe 
plat_get_soc_revision(void)490e753437SManish V Badarkhe int32_t plat_get_soc_revision(void)
500e753437SManish V Badarkhe {
510e753437SManish V Badarkhe 	return SMC_ARCH_CALL_NOT_SUPPORTED;
520e753437SManish V Badarkhe }
53566034fcSSoby Mathew 
plat_get_soc_name(char * soc_name __unused)54cb4ee3e4SArvind Ram Prakash int32_t plat_get_soc_name(char *soc_name __unused)
55cb4ee3e4SArvind Ram Prakash {
56cb4ee3e4SArvind Ram Prakash 	return SMC_ARCH_CALL_NOT_SUPPORTED;
57cb4ee3e4SArvind Ram Prakash }
58cb4ee3e4SArvind Ram Prakash 
plat_is_smccc_feature_available(u_register_t fid __unused)596f0a2f04SManish V Badarkhe int32_t plat_is_smccc_feature_available(u_register_t fid __unused)
606f0a2f04SManish V Badarkhe {
61877279deSSigned-off-by: Maheedhar Bollapalli 	(void)fid;
626f0a2f04SManish V Badarkhe 	return SMC_ARCH_CALL_NOT_SUPPORTED;
636f0a2f04SManish V Badarkhe }
646f0a2f04SManish V Badarkhe 
bl2_el3_plat_prepare_exit(void)65566034fcSSoby Mathew void bl2_el3_plat_prepare_exit(void)
66566034fcSSoby Mathew {
67566034fcSSoby Mathew }
68566034fcSSoby Mathew 
plat_error_handler(int err)69566034fcSSoby Mathew void __dead2 plat_error_handler(int err)
70566034fcSSoby Mathew {
71ee14e1aeSSaivardhan Thatikonda 	(void)err;
72ee14e1aeSSaivardhan Thatikonda 
73d83e1f05SSaivardhan Thatikonda 	while (true) {
74566034fcSSoby Mathew 		wfi();
75566034fcSSoby Mathew 	}
767e288d11SMaheedhar Bollapalli }
77566034fcSSoby Mathew 
bl2_plat_preload_setup(void)78566034fcSSoby Mathew void bl2_plat_preload_setup(void)
79566034fcSSoby Mathew {
80566034fcSSoby Mathew }
81566034fcSSoby Mathew 
bl2_plat_handle_pre_image_load(unsigned int image_id)82566034fcSSoby Mathew int bl2_plat_handle_pre_image_load(unsigned int image_id)
83566034fcSSoby Mathew {
84ee14e1aeSSaivardhan Thatikonda 	(void)image_id;
85ee14e1aeSSaivardhan Thatikonda 
86566034fcSSoby Mathew 	return 0;
87566034fcSSoby Mathew }
88566034fcSSoby Mathew 
bl2_plat_handle_post_image_load(unsigned int image_id)89566034fcSSoby Mathew int bl2_plat_handle_post_image_load(unsigned int image_id)
90566034fcSSoby Mathew {
91ee14e1aeSSaivardhan Thatikonda 	(void)image_id;
92ee14e1aeSSaivardhan Thatikonda 
93566034fcSSoby Mathew 	return 0;
94566034fcSSoby Mathew }
95566034fcSSoby Mathew 
960916c38dSRoberto Vargas /*
972be57b86SSumit Garg  * Weak implementation to provide dummy decryption key only for test purposes,
982be57b86SSumit Garg  * platforms must override this API for any real world firmware encryption
992be57b86SSumit Garg  * use-case.
1002be57b86SSumit Garg  */
plat_get_enc_key_info(enum fw_enc_status_t fw_enc_status,uint8_t * key,size_t * key_len,unsigned int * flags,const uint8_t * img_id,size_t img_id_len)1012be57b86SSumit Garg int plat_get_enc_key_info(enum fw_enc_status_t fw_enc_status, uint8_t *key,
1022be57b86SSumit Garg 			  size_t *key_len, unsigned int *flags,
1032be57b86SSumit Garg 			  const uint8_t *img_id, size_t img_id_len)
1042be57b86SSumit Garg {
105ee14e1aeSSaivardhan Thatikonda 	(void)fw_enc_status;
106ee14e1aeSSaivardhan Thatikonda 	(void)img_id;
107ee14e1aeSSaivardhan Thatikonda 	(void)img_id_len;
108ee14e1aeSSaivardhan Thatikonda 
1092be57b86SSumit Garg #define DUMMY_FIP_ENC_KEY { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, \
1102be57b86SSumit Garg 			    0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, \
1112be57b86SSumit Garg 			    0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, \
1122be57b86SSumit Garg 			    0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }
1132be57b86SSumit Garg 
1142be57b86SSumit Garg 	const uint8_t dummy_key[] = DUMMY_FIP_ENC_KEY;
1152be57b86SSumit Garg 
1162be57b86SSumit Garg 	assert(*key_len >= sizeof(dummy_key));
1172be57b86SSumit Garg 
1182be57b86SSumit Garg 	*key_len = sizeof(dummy_key);
1192be57b86SSumit Garg 	memcpy(key, dummy_key, *key_len);
1202be57b86SSumit Garg 	*flags = 0;
1212be57b86SSumit Garg 
1222be57b86SSumit Garg 	return 0;
1232be57b86SSumit Garg }
1242be57b86SSumit Garg 
1252be57b86SSumit Garg /*
1260916c38dSRoberto Vargas  * Set up the page tables for the generic and platform-specific memory regions.
1270916c38dSRoberto Vargas  * The size of the Trusted SRAM seen by the BL image must be specified as well
1280916c38dSRoberto Vargas  * as an array specifying the generic memory regions which can be;
1290916c38dSRoberto Vargas  * - Code section;
1300916c38dSRoberto Vargas  * - Read-only data section;
1310916c38dSRoberto Vargas  * - Init code section, if applicable
1320916c38dSRoberto Vargas  * - Coherent memory region, if applicable.
1330916c38dSRoberto Vargas  */
1340916c38dSRoberto Vargas 
setup_page_tables(const mmap_region_t * bl_regions,const mmap_region_t * plat_regions)1350916c38dSRoberto Vargas void __init setup_page_tables(const mmap_region_t *bl_regions,
1360916c38dSRoberto Vargas 			      const mmap_region_t *plat_regions)
1370916c38dSRoberto Vargas {
1380916c38dSRoberto Vargas #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
1390916c38dSRoberto Vargas 	const mmap_region_t *regions = bl_regions;
1400916c38dSRoberto Vargas 
1410916c38dSRoberto Vargas 	while (regions->size != 0U) {
1420916c38dSRoberto Vargas 		VERBOSE("Region: 0x%lx - 0x%lx has attributes 0x%x\n",
1430916c38dSRoberto Vargas 				regions->base_va,
1440916c38dSRoberto Vargas 				regions->base_va + regions->size,
1450916c38dSRoberto Vargas 				regions->attr);
1460916c38dSRoberto Vargas 		regions++;
1470916c38dSRoberto Vargas 	}
1480916c38dSRoberto Vargas #endif
1490916c38dSRoberto Vargas 	/*
1500916c38dSRoberto Vargas 	 * Map the Trusted SRAM with appropriate memory attributes.
1510916c38dSRoberto Vargas 	 * Subsequent mappings will adjust the attributes for specific regions.
1520916c38dSRoberto Vargas 	 */
1530916c38dSRoberto Vargas 	mmap_add(bl_regions);
1540916c38dSRoberto Vargas 
1550916c38dSRoberto Vargas 	/* Now (re-)map the platform-specific memory regions */
1560916c38dSRoberto Vargas 	mmap_add(plat_regions);
1570916c38dSRoberto Vargas 
1580916c38dSRoberto Vargas 	/* Create the page tables to reflect the above mappings */
1590916c38dSRoberto Vargas 	init_xlat_tables();
1600916c38dSRoberto Vargas }
1614d8b4ca0SRaymond Mao 
1624d8b4ca0SRaymond Mao #if ((MEASURED_BOOT || DICE_PROTECTION_ENVIRONMENT) && TRANSFER_LIST)
plat_handoff_mboot(const void * data,uint32_t data_size,void * tl_base)1634d8b4ca0SRaymond Mao int plat_handoff_mboot(const void *data, uint32_t data_size, void *tl_base)
1644d8b4ca0SRaymond Mao {
1654d8b4ca0SRaymond Mao 	if (!transfer_list_add(tl_base, TL_TAG_TPM_EVLOG, data_size, data))
1664d8b4ca0SRaymond Mao 		return -1;
1674d8b4ca0SRaymond Mao 
1684d8b4ca0SRaymond Mao 	return 0;
1694d8b4ca0SRaymond Mao }
1704d8b4ca0SRaymond Mao #endif
171