1 /*
2  * Copyright (c) 2021, 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_fconf_getter.h>
20 #include <stm32mp_io_storage.h>
21 
22 #if STM32MP_SDMMC || STM32MP_EMMC
23 static io_block_spec_t gpt_block_spec = {
24 	.offset = 0U,
25 	.length = 34U * MMC_BLOCK_SIZE, /* Size of GPT table */
26 };
27 #endif
28 
29 /* By default, STM32 platforms load images from the FIP */
30 struct plat_io_policy policies[MAX_NUMBER_IDS] = {
31 	[FIP_IMAGE_ID] = {
32 		&storage_dev_handle,
33 		(uintptr_t)&image_block_spec,
34 		open_storage
35 	},
36 #if STM32MP_SDMMC || STM32MP_EMMC
37 	[GPT_IMAGE_ID] = {
38 		&storage_dev_handle,
39 		(uintptr_t)&gpt_block_spec,
40 		open_storage
41 	},
42 #endif
43 };
44 
45 #define FCONF_ST_IO_UUID_NUMBER	U(8)
46 
47 static io_uuid_spec_t fconf_stm32mp_uuids[FCONF_ST_IO_UUID_NUMBER];
48 static OBJECT_POOL_ARRAY(fconf_stm32mp_uuids_pool, fconf_stm32mp_uuids);
49 
50 struct policies_load_info {
51 	unsigned int image_id;
52 	const char *name;
53 };
54 
55 /* image id to property name table */
56 static const struct policies_load_info load_info[FCONF_ST_IO_UUID_NUMBER] = {
57 	{FW_CONFIG_ID, "fw_cfg_uuid"},
58 	{BL32_IMAGE_ID, "bl32_uuid"},
59 	{BL32_EXTRA1_IMAGE_ID, "bl32_extra1_uuid"},
60 	{BL32_EXTRA2_IMAGE_ID, "bl32_extra2_uuid"},
61 	{BL33_IMAGE_ID, "bl33_uuid"},
62 	{HW_CONFIG_ID, "hw_cfg_uuid"},
63 	{TOS_FW_CONFIG_ID, "tos_fw_cfg_uuid"},
64 	{NT_FW_CONFIG_ID, "nt_fw_cfg_uuid"},
65 };
66 
fconf_populate_stm32mp_io_policies(uintptr_t config)67 int fconf_populate_stm32mp_io_policies(uintptr_t config)
68 {
69 	int node;
70 	unsigned int i;
71 
72 	/* As libfdt uses void *, we can't avoid this cast */
73 	const void *dtb = (void *)config;
74 
75 	/* Assert the node offset point to "st,io-fip-handle" compatible property */
76 	const char *compatible_str = "st,io-fip-handle";
77 
78 	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
79 	if (node < 0) {
80 		ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
81 		return node;
82 	}
83 
84 	/* Locate the uuid cells and read the value for all the load info uuid */
85 	for (i = 0U; i < FCONF_ST_IO_UUID_NUMBER; i++) {
86 		union uuid_helper_t uuid_helper;
87 		io_uuid_spec_t *uuid_ptr;
88 		int err;
89 
90 		uuid_ptr = pool_alloc(&fconf_stm32mp_uuids_pool);
91 		err = fdtw_read_uuid(dtb, node, load_info[i].name, 16,
92 				     (uint8_t *)&uuid_helper);
93 		if (err < 0) {
94 			WARN("FCONF: Read cell failed for %s\n", load_info[i].name);
95 			return err;
96 		}
97 
98 		VERBOSE("FCONF: stm32mp-io_policies.%s cell found with value = "
99 			"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
100 			load_info[i].name,
101 			uuid_helper.uuid_struct.time_low[0], uuid_helper.uuid_struct.time_low[1],
102 			uuid_helper.uuid_struct.time_low[2], uuid_helper.uuid_struct.time_low[3],
103 			uuid_helper.uuid_struct.time_mid[0], uuid_helper.uuid_struct.time_mid[1],
104 			uuid_helper.uuid_struct.time_hi_and_version[0],
105 			uuid_helper.uuid_struct.time_hi_and_version[1],
106 			uuid_helper.uuid_struct.clock_seq_hi_and_reserved,
107 			uuid_helper.uuid_struct.clock_seq_low,
108 			uuid_helper.uuid_struct.node[0], uuid_helper.uuid_struct.node[1],
109 			uuid_helper.uuid_struct.node[2], uuid_helper.uuid_struct.node[3],
110 			uuid_helper.uuid_struct.node[4], uuid_helper.uuid_struct.node[5]);
111 
112 		uuid_ptr->uuid = uuid_helper.uuid_struct;
113 		policies[load_info[i].image_id].image_spec = (uintptr_t)uuid_ptr;
114 		policies[load_info[i].image_id].dev_handle = &fip_dev_handle;
115 		policies[load_info[i].image_id].check = open_fip;
116 	}
117 
118 	return 0;
119 }
120 
121 FCONF_REGISTER_POPULATOR(TB_FW, stm32mp_io, fconf_populate_stm32mp_io_policies);
122