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