xref: /arm-trusted-firmware/plat/st/common/stm32mp_fconf_io.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2021-2023, STMicroelectronics - All Rights Reserved
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <common/debug.h>
10*91f16700Schasinglulu #include <common/fdt_wrappers.h>
11*91f16700Schasinglulu #include <drivers/io/io_storage.h>
12*91f16700Schasinglulu #include <drivers/mmc.h>
13*91f16700Schasinglulu #include <lib/fconf/fconf.h>
14*91f16700Schasinglulu #include <lib/object_pool.h>
15*91f16700Schasinglulu #include <libfdt.h>
16*91f16700Schasinglulu #include <tools_share/firmware_image_package.h>
17*91f16700Schasinglulu 
18*91f16700Schasinglulu #include <platform_def.h>
19*91f16700Schasinglulu #include <stm32mp_efi.h>
20*91f16700Schasinglulu #include <stm32mp_fconf_getter.h>
21*91f16700Schasinglulu #include <stm32mp_io_storage.h>
22*91f16700Schasinglulu 
23*91f16700Schasinglulu #if STM32MP_SDMMC || STM32MP_EMMC
24*91f16700Schasinglulu static io_block_spec_t gpt_block_spec = {
25*91f16700Schasinglulu 	.offset = 0U,
26*91f16700Schasinglulu 	.length = 34U * MMC_BLOCK_SIZE, /* Size of GPT table */
27*91f16700Schasinglulu };
28*91f16700Schasinglulu #endif
29*91f16700Schasinglulu 
30*91f16700Schasinglulu #if (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT
31*91f16700Schasinglulu static io_block_spec_t metadata_block_spec = {
32*91f16700Schasinglulu 	.offset = 0,    /* To be filled at runtime */
33*91f16700Schasinglulu 	.length = 0,    /* To be filled at runtime */
34*91f16700Schasinglulu };
35*91f16700Schasinglulu #endif /* (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT */
36*91f16700Schasinglulu 
37*91f16700Schasinglulu /* By default, STM32 platforms load images from the FIP */
38*91f16700Schasinglulu struct plat_io_policy policies[MAX_NUMBER_IDS] = {
39*91f16700Schasinglulu 	[FIP_IMAGE_ID] = {
40*91f16700Schasinglulu 		.dev_handle = &storage_dev_handle,
41*91f16700Schasinglulu 		.image_spec = (uintptr_t)&image_block_spec,
42*91f16700Schasinglulu 		.img_type_guid = STM32MP_FIP_GUID,
43*91f16700Schasinglulu 		.check = open_storage
44*91f16700Schasinglulu 	},
45*91f16700Schasinglulu #ifndef DECRYPTION_SUPPORT_none
46*91f16700Schasinglulu 	[ENC_IMAGE_ID] = {
47*91f16700Schasinglulu 		.dev_handle = &fip_dev_handle,
48*91f16700Schasinglulu 		.image_spec = (uintptr_t)NULL,
49*91f16700Schasinglulu 		.img_type_guid = NULL_GUID,
50*91f16700Schasinglulu 		.check = open_fip
51*91f16700Schasinglulu 	},
52*91f16700Schasinglulu #endif
53*91f16700Schasinglulu #if STM32MP_SDMMC || STM32MP_EMMC
54*91f16700Schasinglulu 	[GPT_IMAGE_ID] = {
55*91f16700Schasinglulu 		.dev_handle = &storage_dev_handle,
56*91f16700Schasinglulu 		.image_spec = (uintptr_t)&gpt_block_spec,
57*91f16700Schasinglulu 		.img_type_guid = NULL_GUID,
58*91f16700Schasinglulu 		.check = open_storage
59*91f16700Schasinglulu 	},
60*91f16700Schasinglulu #endif
61*91f16700Schasinglulu #if (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT
62*91f16700Schasinglulu 	[FWU_METADATA_IMAGE_ID] = {
63*91f16700Schasinglulu 		.dev_handle = &storage_dev_handle,
64*91f16700Schasinglulu 		.image_spec = (uintptr_t)&metadata_block_spec,
65*91f16700Schasinglulu 		.img_type_guid = NULL_GUID,
66*91f16700Schasinglulu 		.check = open_storage
67*91f16700Schasinglulu 	},
68*91f16700Schasinglulu 	[BKUP_FWU_METADATA_IMAGE_ID] = {
69*91f16700Schasinglulu 		.dev_handle = &storage_dev_handle,
70*91f16700Schasinglulu 		.image_spec = (uintptr_t)&metadata_block_spec,
71*91f16700Schasinglulu 		.img_type_guid = NULL_GUID,
72*91f16700Schasinglulu 		.check = open_storage
73*91f16700Schasinglulu 	},
74*91f16700Schasinglulu #endif /* (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT */
75*91f16700Schasinglulu };
76*91f16700Schasinglulu 
77*91f16700Schasinglulu #define DEFAULT_UUID_NUMBER	U(7)
78*91f16700Schasinglulu 
79*91f16700Schasinglulu #if TRUSTED_BOARD_BOOT
80*91f16700Schasinglulu #define TBBR_UUID_NUMBER	U(6)
81*91f16700Schasinglulu #else
82*91f16700Schasinglulu #define TBBR_UUID_NUMBER	U(0)
83*91f16700Schasinglulu #endif
84*91f16700Schasinglulu 
85*91f16700Schasinglulu #define FCONF_ST_IO_UUID_NUMBER	(DEFAULT_UUID_NUMBER + \
86*91f16700Schasinglulu 				 TBBR_UUID_NUMBER)
87*91f16700Schasinglulu 
88*91f16700Schasinglulu static io_uuid_spec_t fconf_stm32mp_uuids[FCONF_ST_IO_UUID_NUMBER];
89*91f16700Schasinglulu static OBJECT_POOL_ARRAY(fconf_stm32mp_uuids_pool, fconf_stm32mp_uuids);
90*91f16700Schasinglulu 
91*91f16700Schasinglulu struct policies_load_info {
92*91f16700Schasinglulu 	unsigned int image_id;
93*91f16700Schasinglulu 	const char *name;
94*91f16700Schasinglulu };
95*91f16700Schasinglulu 
96*91f16700Schasinglulu /* image id to property name table */
97*91f16700Schasinglulu static const struct policies_load_info load_info[FCONF_ST_IO_UUID_NUMBER] = {
98*91f16700Schasinglulu 	{FW_CONFIG_ID, "fw_cfg_uuid"},
99*91f16700Schasinglulu 	{BL32_IMAGE_ID, "bl32_uuid"},
100*91f16700Schasinglulu 	{BL32_EXTRA1_IMAGE_ID, "bl32_extra1_uuid"},
101*91f16700Schasinglulu 	{BL32_EXTRA2_IMAGE_ID, "bl32_extra2_uuid"},
102*91f16700Schasinglulu 	{BL33_IMAGE_ID, "bl33_uuid"},
103*91f16700Schasinglulu 	{HW_CONFIG_ID, "hw_cfg_uuid"},
104*91f16700Schasinglulu 	{TOS_FW_CONFIG_ID, "tos_fw_cfg_uuid"},
105*91f16700Schasinglulu #if TRUSTED_BOARD_BOOT
106*91f16700Schasinglulu 	{STM32MP_CONFIG_CERT_ID, "stm32mp_cfg_cert_uuid"},
107*91f16700Schasinglulu 	{TRUSTED_KEY_CERT_ID, "t_key_cert_uuid"},
108*91f16700Schasinglulu 	{TRUSTED_OS_FW_KEY_CERT_ID, "tos_fw_key_cert_uuid"},
109*91f16700Schasinglulu 	{NON_TRUSTED_FW_KEY_CERT_ID, "nt_fw_key_cert_uuid"},
110*91f16700Schasinglulu 	{TRUSTED_OS_FW_CONTENT_CERT_ID, "tos_fw_content_cert_uuid"},
111*91f16700Schasinglulu 	{NON_TRUSTED_FW_CONTENT_CERT_ID, "nt_fw_content_cert_uuid"},
112*91f16700Schasinglulu #endif /* TRUSTED_BOARD_BOOT */
113*91f16700Schasinglulu };
114*91f16700Schasinglulu 
115*91f16700Schasinglulu int fconf_populate_stm32mp_io_policies(uintptr_t config)
116*91f16700Schasinglulu {
117*91f16700Schasinglulu 	int node;
118*91f16700Schasinglulu 	unsigned int i;
119*91f16700Schasinglulu 
120*91f16700Schasinglulu 	/* As libfdt uses void *, we can't avoid this cast */
121*91f16700Schasinglulu 	const void *dtb = (void *)config;
122*91f16700Schasinglulu 
123*91f16700Schasinglulu 	/* Assert the node offset point to "st,io-fip-handle" compatible property */
124*91f16700Schasinglulu 	const char *compatible_str = "st,io-fip-handle";
125*91f16700Schasinglulu 
126*91f16700Schasinglulu 	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
127*91f16700Schasinglulu 	if (node < 0) {
128*91f16700Schasinglulu 		ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
129*91f16700Schasinglulu 		return node;
130*91f16700Schasinglulu 	}
131*91f16700Schasinglulu 
132*91f16700Schasinglulu 	/* Locate the uuid cells and read the value for all the load info uuid */
133*91f16700Schasinglulu 	for (i = 0U; i < FCONF_ST_IO_UUID_NUMBER; i++) {
134*91f16700Schasinglulu 		union uuid_helper_t uuid_helper;
135*91f16700Schasinglulu 		io_uuid_spec_t *uuid_ptr;
136*91f16700Schasinglulu 		int err;
137*91f16700Schasinglulu 
138*91f16700Schasinglulu 		uuid_ptr = pool_alloc(&fconf_stm32mp_uuids_pool);
139*91f16700Schasinglulu 		err = fdtw_read_uuid(dtb, node, load_info[i].name, 16,
140*91f16700Schasinglulu 				     (uint8_t *)&uuid_helper);
141*91f16700Schasinglulu 		if (err < 0) {
142*91f16700Schasinglulu 			WARN("FCONF: Read cell failed for %s\n", load_info[i].name);
143*91f16700Schasinglulu 			return err;
144*91f16700Schasinglulu 		}
145*91f16700Schasinglulu 
146*91f16700Schasinglulu 		VERBOSE("FCONF: stm32mp-io_policies.%s cell found with value = "
147*91f16700Schasinglulu 			"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
148*91f16700Schasinglulu 			load_info[i].name,
149*91f16700Schasinglulu 			uuid_helper.uuid_struct.time_low[0], uuid_helper.uuid_struct.time_low[1],
150*91f16700Schasinglulu 			uuid_helper.uuid_struct.time_low[2], uuid_helper.uuid_struct.time_low[3],
151*91f16700Schasinglulu 			uuid_helper.uuid_struct.time_mid[0], uuid_helper.uuid_struct.time_mid[1],
152*91f16700Schasinglulu 			uuid_helper.uuid_struct.time_hi_and_version[0],
153*91f16700Schasinglulu 			uuid_helper.uuid_struct.time_hi_and_version[1],
154*91f16700Schasinglulu 			uuid_helper.uuid_struct.clock_seq_hi_and_reserved,
155*91f16700Schasinglulu 			uuid_helper.uuid_struct.clock_seq_low,
156*91f16700Schasinglulu 			uuid_helper.uuid_struct.node[0], uuid_helper.uuid_struct.node[1],
157*91f16700Schasinglulu 			uuid_helper.uuid_struct.node[2], uuid_helper.uuid_struct.node[3],
158*91f16700Schasinglulu 			uuid_helper.uuid_struct.node[4], uuid_helper.uuid_struct.node[5]);
159*91f16700Schasinglulu 
160*91f16700Schasinglulu 		uuid_ptr->uuid = uuid_helper.uuid_struct;
161*91f16700Schasinglulu 		policies[load_info[i].image_id].image_spec = (uintptr_t)uuid_ptr;
162*91f16700Schasinglulu 		switch (load_info[i].image_id) {
163*91f16700Schasinglulu #if ENCRYPT_BL32 && !defined(DECRYPTION_SUPPORT_none)
164*91f16700Schasinglulu 		case BL32_IMAGE_ID:
165*91f16700Schasinglulu 		case BL32_EXTRA1_IMAGE_ID:
166*91f16700Schasinglulu 		case BL32_EXTRA2_IMAGE_ID:
167*91f16700Schasinglulu 			policies[load_info[i].image_id].dev_handle = &enc_dev_handle;
168*91f16700Schasinglulu 			policies[load_info[i].image_id].check = open_enc_fip;
169*91f16700Schasinglulu 			break;
170*91f16700Schasinglulu #endif
171*91f16700Schasinglulu 		default:
172*91f16700Schasinglulu 			policies[load_info[i].image_id].dev_handle = &fip_dev_handle;
173*91f16700Schasinglulu 			policies[load_info[i].image_id].check = open_fip;
174*91f16700Schasinglulu 			break;
175*91f16700Schasinglulu 		}
176*91f16700Schasinglulu 	}
177*91f16700Schasinglulu 
178*91f16700Schasinglulu 	return 0;
179*91f16700Schasinglulu }
180*91f16700Schasinglulu 
181*91f16700Schasinglulu FCONF_REGISTER_POPULATOR(TB_FW, stm32mp_io, fconf_populate_stm32mp_io_policies);
182