Jassi Brar | 2244bf1 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022, Socionext Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <drivers/io/io_block.h> |
| 12 | #include <drivers/io/io_driver.h> |
| 13 | #include <drivers/io/io_fip.h> |
| 14 | #include <drivers/io/io_memmap.h> |
| 15 | #include <lib/mmio.h> |
| 16 | #include <lib/utils_def.h> |
| 17 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 18 | #include <tools_share/firmware_image_package.h> |
| 19 | |
| 20 | #include <platform_def.h> |
| 21 | #include <sq_common.h> |
| 22 | |
| 23 | static const io_dev_connector_t *sq_fip_dev_con; |
| 24 | static uintptr_t sq_fip_dev_handle; |
| 25 | |
| 26 | static const io_dev_connector_t *sq_backend_dev_con; |
| 27 | static uintptr_t sq_backend_dev_handle; |
| 28 | |
| 29 | static io_block_spec_t sq_fip_spec = { |
| 30 | .offset = PLAT_SQ_FIP_IOBASE, /* FIP Image is at 5MB offset on memory-mapped NOR flash */ |
| 31 | .length = PLAT_SQ_FIP_MAXSIZE, /* Expected maximum FIP image size */ |
| 32 | }; |
| 33 | |
| 34 | static const io_uuid_spec_t sq_bl2_spec = { |
| 35 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 36 | }; |
| 37 | |
| 38 | static const io_uuid_spec_t sq_bl31_spec = { |
| 39 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 40 | }; |
| 41 | |
| 42 | static const io_uuid_spec_t sq_bl32_spec = { |
| 43 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 44 | }; |
| 45 | |
| 46 | static const io_uuid_spec_t sq_bl33_spec = { |
| 47 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 48 | }; |
| 49 | |
Jassi Brar | 554f172 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 50 | #if TRUSTED_BOARD_BOOT |
| 51 | static const io_uuid_spec_t sq_tb_fw_cert_spec = { |
| 52 | .uuid = UUID_TRUSTED_BOOT_FW_CERT, |
| 53 | }; |
| 54 | |
| 55 | static const io_uuid_spec_t sq_trusted_key_cert_spec = { |
| 56 | .uuid = UUID_TRUSTED_KEY_CERT, |
| 57 | }; |
| 58 | |
| 59 | static const io_uuid_spec_t sq_soc_fw_key_cert_spec = { |
| 60 | .uuid = UUID_SOC_FW_KEY_CERT, |
| 61 | }; |
| 62 | |
| 63 | static const io_uuid_spec_t sq_tos_fw_key_cert_spec = { |
| 64 | .uuid = UUID_TRUSTED_OS_FW_KEY_CERT, |
| 65 | }; |
| 66 | |
| 67 | static const io_uuid_spec_t sq_nt_fw_key_cert_spec = { |
| 68 | .uuid = UUID_NON_TRUSTED_FW_KEY_CERT, |
| 69 | }; |
| 70 | |
| 71 | static const io_uuid_spec_t sq_soc_fw_cert_spec = { |
| 72 | .uuid = UUID_SOC_FW_CONTENT_CERT, |
| 73 | }; |
| 74 | |
| 75 | static const io_uuid_spec_t sq_tos_fw_cert_spec = { |
| 76 | .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT, |
| 77 | }; |
| 78 | |
| 79 | static const io_uuid_spec_t sq_nt_fw_cert_spec = { |
| 80 | .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT, |
| 81 | }; |
| 82 | #endif /* TRUSTED_BOARD_BOOT */ |
| 83 | |
Jassi Brar | 2244bf1 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 84 | struct sq_io_policy { |
| 85 | uintptr_t *dev_handle; |
| 86 | uintptr_t image_spec; |
| 87 | uintptr_t init_params; |
| 88 | }; |
| 89 | |
| 90 | static const struct sq_io_policy sq_io_policies[] = { |
| 91 | [FIP_IMAGE_ID] = { |
| 92 | .dev_handle = &sq_backend_dev_handle, |
| 93 | .image_spec = (uintptr_t)&sq_fip_spec, |
| 94 | }, |
| 95 | [BL2_IMAGE_ID] = { |
| 96 | .dev_handle = &sq_fip_dev_handle, |
| 97 | .image_spec = (uintptr_t)&sq_bl2_spec, |
| 98 | .init_params = FIP_IMAGE_ID, |
| 99 | }, |
| 100 | [BL31_IMAGE_ID] = { |
| 101 | .dev_handle = &sq_fip_dev_handle, |
| 102 | .image_spec = (uintptr_t)&sq_bl31_spec, |
| 103 | .init_params = FIP_IMAGE_ID, |
| 104 | }, |
| 105 | [BL32_IMAGE_ID] = { |
| 106 | .dev_handle = &sq_fip_dev_handle, |
| 107 | .image_spec = (uintptr_t)&sq_bl32_spec, |
| 108 | .init_params = FIP_IMAGE_ID, |
| 109 | }, |
| 110 | [BL33_IMAGE_ID] = { |
| 111 | .dev_handle = &sq_fip_dev_handle, |
| 112 | .image_spec = (uintptr_t)&sq_bl33_spec, |
| 113 | .init_params = FIP_IMAGE_ID, |
| 114 | }, |
Jassi Brar | 554f172 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 115 | #if TRUSTED_BOARD_BOOT |
| 116 | [TRUSTED_BOOT_FW_CERT_ID] = { |
| 117 | .dev_handle = &sq_fip_dev_handle, |
| 118 | .image_spec = (uintptr_t)&sq_tb_fw_cert_spec, |
| 119 | .init_params = FIP_IMAGE_ID, |
| 120 | }, |
| 121 | [TRUSTED_KEY_CERT_ID] = { |
| 122 | .dev_handle = &sq_fip_dev_handle, |
| 123 | .image_spec = (uintptr_t)&sq_trusted_key_cert_spec, |
| 124 | .init_params = FIP_IMAGE_ID, |
| 125 | }, |
| 126 | [SOC_FW_KEY_CERT_ID] = { |
| 127 | .dev_handle = &sq_fip_dev_handle, |
| 128 | .image_spec = (uintptr_t)&sq_soc_fw_key_cert_spec, |
| 129 | .init_params = FIP_IMAGE_ID, |
| 130 | }, |
| 131 | [TRUSTED_OS_FW_KEY_CERT_ID] = { |
| 132 | .dev_handle = &sq_fip_dev_handle, |
| 133 | .image_spec = (uintptr_t)&sq_tos_fw_key_cert_spec, |
| 134 | .init_params = FIP_IMAGE_ID, |
| 135 | }, |
| 136 | [NON_TRUSTED_FW_KEY_CERT_ID] = { |
| 137 | .dev_handle = &sq_fip_dev_handle, |
| 138 | .image_spec = (uintptr_t)&sq_nt_fw_key_cert_spec, |
| 139 | .init_params = FIP_IMAGE_ID, |
| 140 | }, |
| 141 | [SOC_FW_CONTENT_CERT_ID] = { |
| 142 | .dev_handle = &sq_fip_dev_handle, |
| 143 | .image_spec = (uintptr_t)&sq_soc_fw_cert_spec, |
| 144 | .init_params = FIP_IMAGE_ID, |
| 145 | }, |
| 146 | [TRUSTED_OS_FW_CONTENT_CERT_ID] = { |
| 147 | .dev_handle = &sq_fip_dev_handle, |
| 148 | .image_spec = (uintptr_t)&sq_tos_fw_cert_spec, |
| 149 | .init_params = FIP_IMAGE_ID, |
| 150 | }, |
| 151 | [NON_TRUSTED_FW_CONTENT_CERT_ID] = { |
| 152 | .dev_handle = &sq_fip_dev_handle, |
| 153 | .image_spec = (uintptr_t)&sq_nt_fw_cert_spec, |
| 154 | .init_params = FIP_IMAGE_ID, |
| 155 | }, |
| 156 | #endif |
Jassi Brar | 2244bf1 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 157 | }; |
| 158 | |
Jassi Brar | 2832f59 | 2022-05-23 13:16:01 -0500 | [diff] [blame] | 159 | static int sq_update_fip_spec(void) |
| 160 | { |
| 161 | uint32_t boot_index; |
| 162 | int ret; |
| 163 | |
| 164 | ret = mmap_add_dynamic_region(PLAT_SQ_BOOTIDX_BASE, PLAT_SQ_BOOTIDX_BASE, |
| 165 | PAGE_SIZE, MT_RO_DATA | MT_SECURE); |
| 166 | if (ret) { |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | boot_index = mmio_read_32(PLAT_SQ_BOOTIDX_BASE); |
| 171 | if (boot_index < PLAT_SQ_MAX_BOOT_INDEX) { |
| 172 | sq_fip_spec.offset += PLAT_SQ_FIP_MAXSIZE * boot_index; |
| 173 | INFO("FWU Enabled: boot_index %d\n", boot_index); |
| 174 | } else { |
| 175 | WARN("FWU Disabled: wrong boot_index value. Fallback to index 0.\n"); |
| 176 | } |
| 177 | |
| 178 | mmap_remove_dynamic_region(PLAT_SQ_BOOTIDX_BASE, PAGE_SIZE); |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jassi Brar | 2244bf1 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 182 | static int sq_io_memmap_setup(void) |
| 183 | { |
| 184 | int ret; |
| 185 | |
Jassi Brar | 2832f59 | 2022-05-23 13:16:01 -0500 | [diff] [blame] | 186 | ret = sq_update_fip_spec(); |
| 187 | if (ret) { |
| 188 | return ret; |
| 189 | } |
| 190 | |
Jassi Brar | 2244bf1 | 2022-03-03 15:24:31 -0600 | [diff] [blame] | 191 | ret = mmap_add_dynamic_region(sq_fip_spec.offset, sq_fip_spec.offset, |
| 192 | sq_fip_spec.length, MT_RO_DATA | MT_SECURE); |
| 193 | if (ret) { |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | ret = register_io_dev_memmap(&sq_backend_dev_con); |
| 198 | if (ret) { |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | return io_dev_open(sq_backend_dev_con, 0, &sq_backend_dev_handle); |
| 203 | } |
| 204 | |
| 205 | static int sq_io_fip_setup(void) |
| 206 | { |
| 207 | int ret; |
| 208 | |
| 209 | ret = register_io_dev_fip(&sq_fip_dev_con); |
| 210 | if (ret) { |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | return io_dev_open(sq_fip_dev_con, 0, &sq_fip_dev_handle); |
| 215 | } |
| 216 | |
| 217 | int sq_io_setup(void) |
| 218 | { |
| 219 | int ret; |
| 220 | |
| 221 | ret = sq_io_memmap_setup(); |
| 222 | if (ret) { |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | ret = sq_io_fip_setup(); |
| 227 | if (ret) { |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 235 | uintptr_t *image_spec) |
| 236 | { |
| 237 | uintptr_t init_params; |
| 238 | |
| 239 | assert(image_id < ARRAY_SIZE(sq_io_policies)); |
| 240 | |
| 241 | *dev_handle = *sq_io_policies[image_id].dev_handle; |
| 242 | *image_spec = sq_io_policies[image_id].image_spec; |
| 243 | init_params = sq_io_policies[image_id].init_params; |
| 244 | |
| 245 | return io_dev_init(*dev_handle, init_params); |
| 246 | } |