Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 8 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <arch_helpers.h> |
| 13 | #include <common/debug.h> |
| 14 | #include <common/tbbr/tbbr_img_def.h> |
| 15 | #include <drivers/io/io_block.h> |
| 16 | #include <drivers/io/io_driver.h> |
| 17 | #include <drivers/io/io_fip.h> |
| 18 | #include <drivers/io/io_memmap.h> |
| 19 | #include <drivers/io/io_storage.h> |
| 20 | #include <drivers/mmc.h> |
| 21 | #include <drivers/partition/partition.h> |
| 22 | #include <lib/mmio.h> |
| 23 | #include <lib/semihosting.h> |
| 24 | #include <lib/utils.h> |
| 25 | #include <tools_share/firmware_image_package.h> |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 26 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 27 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 28 | static const io_dev_connector_t *emmc_dev_con; |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 29 | static uintptr_t emmc_dev_handle; |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 30 | static int open_emmc(const uintptr_t spec); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 31 | |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 32 | static const io_block_spec_t emmc_fip_spec = { |
| 33 | .offset = FIP_BASE_EMMC, |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 34 | .length = FIP_SIZE |
| 35 | }; |
| 36 | |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 37 | static const io_block_dev_spec_t emmc_dev_spec = { |
| 38 | .buffer = { |
| 39 | .offset = POPLAR_EMMC_DATA_BASE, |
| 40 | .length = POPLAR_EMMC_DATA_SIZE, |
| 41 | }, |
| 42 | .ops = { |
Haojian Zhuang | 3eff409 | 2018-08-04 18:07:26 +0800 | [diff] [blame] | 43 | .read = mmc_read_blocks, |
| 44 | .write = mmc_write_blocks, |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 45 | }, |
Haojian Zhuang | 3eff409 | 2018-08-04 18:07:26 +0800 | [diff] [blame] | 46 | .block_size = MMC_BLOCK_SIZE, |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 47 | }; |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 48 | #else |
| 49 | static const io_dev_connector_t *mmap_dev_con; |
| 50 | static uintptr_t mmap_dev_handle; |
| 51 | static int open_mmap(const uintptr_t spec); |
| 52 | |
| 53 | static const io_block_spec_t loader_fip_spec = { |
| 54 | .offset = FIP_BASE, |
| 55 | .length = FIP_SIZE |
| 56 | }; |
| 57 | #endif |
| 58 | |
| 59 | static const io_dev_connector_t *fip_dev_con; |
| 60 | static uintptr_t fip_dev_handle; |
| 61 | static int open_fip(const uintptr_t spec); |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 62 | |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 63 | static const io_uuid_spec_t bl2_uuid_spec = { |
| 64 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 65 | }; |
| 66 | |
| 67 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 68 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 69 | }; |
| 70 | |
Victor Chong | 662556a | 2017-10-28 01:59:41 +0900 | [diff] [blame] | 71 | static const io_uuid_spec_t bl32_uuid_spec = { |
| 72 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 73 | }; |
| 74 | |
Victor Chong | aa03347 | 2018-02-01 00:35:39 +0900 | [diff] [blame] | 75 | static const io_uuid_spec_t bl32_extra1_uuid_spec = { |
| 76 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1, |
| 77 | }; |
| 78 | |
| 79 | static const io_uuid_spec_t bl32_extra2_uuid_spec = { |
| 80 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2, |
| 81 | }; |
| 82 | |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 83 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 84 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 85 | }; |
| 86 | |
| 87 | struct plat_io_policy { |
| 88 | uintptr_t *dev_handle; |
| 89 | uintptr_t image_spec; |
| 90 | int (*check)(const uintptr_t spec); |
| 91 | }; |
| 92 | |
| 93 | static const struct plat_io_policy policies[] = { |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 94 | #if !POPLAR_RECOVERY |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 95 | [FIP_IMAGE_ID] = { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 96 | &emmc_dev_handle, |
| 97 | (uintptr_t)&emmc_fip_spec, |
| 98 | open_emmc |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 99 | }, |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 100 | #else |
| 101 | [FIP_IMAGE_ID] = { |
| 102 | &mmap_dev_handle, |
| 103 | (uintptr_t)&loader_fip_spec, |
| 104 | open_mmap |
| 105 | }, |
| 106 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 107 | [BL2_IMAGE_ID] = { |
| 108 | &fip_dev_handle, |
| 109 | (uintptr_t)&bl2_uuid_spec, |
| 110 | open_fip |
| 111 | }, |
| 112 | [BL31_IMAGE_ID] = { |
| 113 | &fip_dev_handle, |
| 114 | (uintptr_t)&bl31_uuid_spec, |
| 115 | open_fip |
| 116 | }, |
Victor Chong | 662556a | 2017-10-28 01:59:41 +0900 | [diff] [blame] | 117 | [BL32_IMAGE_ID] = { |
| 118 | &fip_dev_handle, |
| 119 | (uintptr_t)&bl32_uuid_spec, |
| 120 | open_fip |
| 121 | }, |
Victor Chong | aa03347 | 2018-02-01 00:35:39 +0900 | [diff] [blame] | 122 | [BL32_EXTRA1_IMAGE_ID] = { |
| 123 | &fip_dev_handle, |
| 124 | (uintptr_t)&bl32_extra1_uuid_spec, |
| 125 | open_fip |
| 126 | }, |
| 127 | [BL32_EXTRA2_IMAGE_ID] = { |
| 128 | &fip_dev_handle, |
| 129 | (uintptr_t)&bl32_extra2_uuid_spec, |
| 130 | open_fip |
| 131 | }, |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 132 | [BL33_IMAGE_ID] = { |
| 133 | &fip_dev_handle, |
| 134 | (uintptr_t)&bl33_uuid_spec, |
| 135 | open_fip |
| 136 | }, |
| 137 | }; |
| 138 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 139 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 140 | static int open_emmc(const uintptr_t spec) |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 141 | { |
| 142 | int result; |
| 143 | uintptr_t local_image_handle; |
| 144 | |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 145 | result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 146 | if (result == 0) { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 147 | result = io_open(emmc_dev_handle, spec, &local_image_handle); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 148 | if (result == 0) { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 149 | INFO("Using eMMC\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 150 | io_close(local_image_handle); |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 151 | } else { |
| 152 | ERROR("error opening emmc\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 153 | } |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 154 | } else { |
| 155 | ERROR("error initializing emmc\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 156 | } |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 157 | |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 158 | return result; |
| 159 | } |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 160 | #else |
| 161 | static int open_mmap(const uintptr_t spec) |
| 162 | { |
| 163 | int result; |
| 164 | uintptr_t local_image_handle; |
| 165 | |
| 166 | result = io_dev_init(mmap_dev_handle, (uintptr_t)NULL); |
| 167 | if (result == 0) { |
| 168 | result = io_open(mmap_dev_handle, spec, &local_image_handle); |
| 169 | if (result == 0) { |
| 170 | INFO("Using mmap\n"); |
| 171 | io_close(local_image_handle); |
| 172 | } else { |
| 173 | ERROR("error opening mmap\n"); |
| 174 | } |
| 175 | } else { |
| 176 | ERROR("error initializing mmap\n"); |
| 177 | } |
| 178 | |
| 179 | return result; |
| 180 | } |
| 181 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 182 | |
| 183 | static int open_fip(const uintptr_t spec) |
| 184 | { |
| 185 | uintptr_t local_image_handle; |
| 186 | int result; |
| 187 | |
| 188 | result = io_dev_init(fip_dev_handle, (uintptr_t) FIP_IMAGE_ID); |
| 189 | if (result == 0) { |
| 190 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 191 | if (result == 0) { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 192 | INFO("Using FIP\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 193 | io_close(local_image_handle); |
| 194 | } else { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 195 | ERROR("error opening fip\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 196 | } |
| 197 | } else { |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 198 | ERROR("error initializing fip\n"); |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 205 | uintptr_t *image_spec) |
| 206 | { |
| 207 | const struct plat_io_policy *policy; |
| 208 | int result; |
| 209 | |
| 210 | assert(image_id < ARRAY_SIZE(policies)); |
| 211 | |
| 212 | policy = &policies[image_id]; |
| 213 | result = policy->check(policy->image_spec); |
| 214 | assert(result == 0); |
| 215 | |
| 216 | *image_spec = policy->image_spec; |
| 217 | *dev_handle = *(policy->dev_handle); |
| 218 | |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | void plat_io_setup(void) |
| 223 | { |
| 224 | int result; |
| 225 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 226 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 227 | result = register_io_dev_block(&emmc_dev_con); |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 228 | #else |
| 229 | result = register_io_dev_memmap(&mmap_dev_con); |
| 230 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 231 | assert(result == 0); |
| 232 | |
| 233 | result = register_io_dev_fip(&fip_dev_con); |
| 234 | assert(result == 0); |
| 235 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 236 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 237 | result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 238 | &fip_dev_handle); |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 239 | #else |
| 240 | result = io_dev_open(fip_dev_con, (uintptr_t)&loader_fip_spec, |
| 241 | &fip_dev_handle); |
| 242 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 243 | assert(result == 0); |
| 244 | |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 245 | #if !POPLAR_RECOVERY |
Victor Chong | 539408d | 2018-01-03 01:53:08 +0900 | [diff] [blame] | 246 | result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec, |
| 247 | &emmc_dev_handle); |
Victor Chong | f0c7c61 | 2018-01-16 00:29:47 +0900 | [diff] [blame] | 248 | #else |
| 249 | result = io_dev_open(mmap_dev_con, (uintptr_t)NULL, &mmap_dev_handle); |
| 250 | #endif |
Jorge Ramirez-Ortiz | a29d9a6 | 2017-06-28 10:11:31 +0200 | [diff] [blame] | 251 | assert(result == 0); |
| 252 | |
| 253 | (void) result; |
| 254 | } |