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 static const io_dev_connector_t *emmc_dev_con; 26 static const io_dev_connector_t *fip_dev_con; 27 28 static uintptr_t emmc_dev_handle; 29 static uintptr_t fip_dev_handle; 30 31 static int open_emmc(const uintptr_t spec); 32 static int open_fip(const uintptr_t spec); 33 34 static const io_block_spec_t emmc_fip_spec = { 35 .offset = FIP_BASE_EMMC, 36 .length = FIP_SIZE 37 }; 38 39 static const io_block_dev_spec_t emmc_dev_spec = { 40 .buffer = { 41 .offset = POPLAR_EMMC_DATA_BASE, 42 .length = POPLAR_EMMC_DATA_SIZE, 43 }, 44 .ops = { 45 .read = emmc_read_blocks, 46 .write = emmc_write_blocks, 47 }, 48 .block_size = EMMC_BLOCK_SIZE, 49 }; 50 51 static const io_uuid_spec_t bl2_uuid_spec = { 52 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, 53 }; 54 55 static const io_uuid_spec_t bl31_uuid_spec = { 56 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, 57 }; 58 59 static const io_uuid_spec_t bl32_uuid_spec = { 60 .uuid = UUID_SECURE_PAYLOAD_BL32, 61 }; 62 63 static const io_uuid_spec_t bl33_uuid_spec = { 64 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, 65 }; 66 67 struct plat_io_policy { 68 uintptr_t *dev_handle; 69 uintptr_t image_spec; 70 int (*check)(const uintptr_t spec); 71 }; 72 73 static const struct plat_io_policy policies[] = { 74 [FIP_IMAGE_ID] = { 75 &emmc_dev_handle, 76 (uintptr_t)&emmc_fip_spec, 77 open_emmc 78 }, 79 [BL2_IMAGE_ID] = { 80 &fip_dev_handle, 81 (uintptr_t)&bl2_uuid_spec, 82 open_fip 83 }, 84 [BL31_IMAGE_ID] = { 85 &fip_dev_handle, 86 (uintptr_t)&bl31_uuid_spec, 87 open_fip 88 }, 89 [BL32_IMAGE_ID] = { 90 &fip_dev_handle, 91 (uintptr_t)&bl32_uuid_spec, 92 open_fip 93 }, 94 [BL33_IMAGE_ID] = { 95 &fip_dev_handle, 96 (uintptr_t)&bl33_uuid_spec, 97 open_fip 98 }, 99 }; 100 101 static int open_emmc(const uintptr_t spec) 102 { 103 int result; 104 uintptr_t local_image_handle; 105 106 result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL); 107 if (result == 0) { 108 result = io_open(emmc_dev_handle, spec, &local_image_handle); 109 if (result == 0) { 110 INFO("Using eMMC\n"); 111 io_close(local_image_handle); 112 } else { 113 ERROR("error opening emmc\n"); 114 } 115 } else { 116 ERROR("error initializing emmc\n"); 117 } 118 119 return result; 120 } 121 122 static int open_fip(const uintptr_t spec) 123 { 124 uintptr_t local_image_handle; 125 int result; 126 127 result = io_dev_init(fip_dev_handle, (uintptr_t) FIP_IMAGE_ID); 128 if (result == 0) { 129 result = io_open(fip_dev_handle, spec, &local_image_handle); 130 if (result == 0) { 131 INFO("Using FIP\n"); 132 io_close(local_image_handle); 133 } else { 134 ERROR("error opening fip\n"); 135 } 136 } else { 137 ERROR("error initializing fip\n"); 138 } 139 140 return result; 141 } 142 143 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, 144 uintptr_t *image_spec) 145 { 146 const struct plat_io_policy *policy; 147 int result; 148 149 assert(image_id < ARRAY_SIZE(policies)); 150 151 policy = &policies[image_id]; 152 result = policy->check(policy->image_spec); 153 assert(result == 0); 154 155 *image_spec = policy->image_spec; 156 *dev_handle = *(policy->dev_handle); 157 158 return result; 159 } 160 161 void plat_io_setup(void) 162 { 163 int result; 164 165 result = register_io_dev_block(&emmc_dev_con); 166 assert(result == 0); 167 168 result = register_io_dev_fip(&fip_dev_con); 169 assert(result == 0); 170 171 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, 172 &fip_dev_handle); 173 assert(result == 0); 174 175 result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec, 176 &emmc_dev_handle); 177 assert(result == 0); 178 179 (void) result; 180 } 181