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 <errno.h> 12 #include <firmware_image_package.h> 13 #include <io_block.h> 14 #include <io_driver.h> 15 #include <io_fip.h> 16 #include <io_memmap.h> 17 #include <io_storage.h> 18 #include <mmio.h> 19 #include <platform_def.h> 20 #include <semihosting.h> /* For FOPEN_MODE_... */ 21 #include <string.h> 22 #include "hikey_private.h" 23 24 #define EMMC_BLOCK_SHIFT 9 25 26 /* Page 1024, since only a few pages before 2048 are used as partition table */ 27 #define SERIALNO_EMMC_OFFSET (1024 * 512) 28 29 struct plat_io_policy { 30 uintptr_t *dev_handle; 31 uintptr_t image_spec; 32 int (*check)(const uintptr_t spec); 33 }; 34 35 static const io_dev_connector_t *emmc_dev_con; 36 static uintptr_t emmc_dev_handle; 37 static const io_dev_connector_t *fip_dev_con; 38 static uintptr_t fip_dev_handle; 39 40 static int check_emmc(const uintptr_t spec); 41 static int check_fip(const uintptr_t spec); 42 43 static const io_block_spec_t emmc_fip_spec = { 44 .offset = HIKEY_FIP_BASE, 45 .length = HIKEY_FIP_MAX_SIZE, 46 }; 47 48 static const io_block_dev_spec_t emmc_dev_spec = { 49 /* It's used as temp buffer in block driver. */ 50 #if IMAGE_BL1 51 .buffer = { 52 .offset = HIKEY_BL1_MMC_DATA_BASE, 53 .length = HIKEY_BL1_MMC_DATA_SIZE, 54 }, 55 #else 56 .buffer = { 57 .offset = HIKEY_MMC_DATA_BASE, 58 .length = HIKEY_MMC_DATA_SIZE, 59 }, 60 #endif 61 .ops = { 62 .read = emmc_read_blocks, 63 .write = emmc_write_blocks, 64 }, 65 .block_size = EMMC_BLOCK_SIZE, 66 }; 67 68 static const io_uuid_spec_t bl2_uuid_spec = { 69 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, 70 }; 71 72 static const io_uuid_spec_t bl31_uuid_spec = { 73 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, 74 }; 75 76 static const io_uuid_spec_t bl32_uuid_spec = { 77 .uuid = UUID_SECURE_PAYLOAD_BL32, 78 }; 79 80 static const io_uuid_spec_t bl33_uuid_spec = { 81 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, 82 }; 83 84 static const io_uuid_spec_t scp_bl2_uuid_spec = { 85 .uuid = UUID_SCP_FIRMWARE_SCP_BL2, 86 }; 87 88 static const struct plat_io_policy policies[] = { 89 [FIP_IMAGE_ID] = { 90 &emmc_dev_handle, 91 (uintptr_t)&emmc_fip_spec, 92 check_emmc 93 }, 94 [BL2_IMAGE_ID] = { 95 &fip_dev_handle, 96 (uintptr_t)&bl2_uuid_spec, 97 check_fip 98 }, 99 [SCP_BL2_IMAGE_ID] = { 100 &fip_dev_handle, 101 (uintptr_t)&scp_bl2_uuid_spec, 102 check_fip 103 }, 104 [BL31_IMAGE_ID] = { 105 &fip_dev_handle, 106 (uintptr_t)&bl31_uuid_spec, 107 check_fip 108 }, 109 [BL32_IMAGE_ID] = { 110 &fip_dev_handle, 111 (uintptr_t)&bl32_uuid_spec, 112 check_fip 113 }, 114 [BL33_IMAGE_ID] = { 115 &fip_dev_handle, 116 (uintptr_t)&bl33_uuid_spec, 117 check_fip 118 } 119 }; 120 121 static int check_emmc(const uintptr_t spec) 122 { 123 int result; 124 uintptr_t local_handle; 125 126 result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL); 127 if (result == 0) { 128 result = io_open(emmc_dev_handle, spec, &local_handle); 129 if (result == 0) 130 io_close(local_handle); 131 } 132 return result; 133 } 134 135 static int check_fip(const uintptr_t spec) 136 { 137 int result; 138 uintptr_t local_image_handle; 139 140 /* See if a Firmware Image Package is available */ 141 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); 142 if (result == 0) { 143 result = io_open(fip_dev_handle, spec, &local_image_handle); 144 if (result == 0) { 145 VERBOSE("Using FIP\n"); 146 io_close(local_image_handle); 147 } 148 } 149 return result; 150 } 151 152 void hikey_io_setup(void) 153 { 154 int result; 155 156 result = register_io_dev_block(&emmc_dev_con); 157 assert(result == 0); 158 159 result = register_io_dev_fip(&fip_dev_con); 160 assert(result == 0); 161 162 result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec, 163 &emmc_dev_handle); 164 assert(result == 0); 165 166 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle); 167 assert(result == 0); 168 169 /* Ignore improbable errors in release builds */ 170 (void)result; 171 } 172 173 /* Return an IO device handle and specification which can be used to access 174 * an image. Use this to enforce platform load policy 175 */ 176 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, 177 uintptr_t *image_spec) 178 { 179 int result; 180 const struct plat_io_policy *policy; 181 182 assert(image_id < ARRAY_SIZE(policies)); 183 184 policy = &policies[image_id]; 185 result = policy->check(policy->image_spec); 186 assert(result == 0); 187 188 *image_spec = policy->image_spec; 189 *dev_handle = *(policy->dev_handle); 190 191 return result; 192 } 193