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