James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 32 | #include <debug.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 33 | #include <io_driver.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 34 | #include <io_fip.h> |
| 35 | #include <io_memmap.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 36 | #include <io_storage.h> |
| 37 | #include <io_semihosting.h> |
| 38 | #include <platform.h> |
| 39 | #include <semihosting.h> /* For FOPEN_MODE_... */ |
| 40 | #include <string.h> |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 41 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 42 | /* IO devices */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 43 | static io_plat_data_t io_data; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 44 | static const io_dev_connector_t *sh_dev_con; |
| 45 | static uintptr_t sh_dev_spec; |
| 46 | static uintptr_t sh_init_params; |
| 47 | static uintptr_t sh_dev_handle; |
| 48 | static const io_dev_connector_t *fip_dev_con; |
| 49 | static uintptr_t fip_dev_spec; |
| 50 | static uintptr_t fip_dev_handle; |
| 51 | static const io_dev_connector_t *memmap_dev_con; |
| 52 | static uintptr_t memmap_dev_spec; |
| 53 | static uintptr_t memmap_init_params; |
| 54 | static uintptr_t memmap_dev_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 55 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 56 | static const io_block_spec_t fip_block_spec = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 57 | .offset = FLASH0_BASE, |
| 58 | .length = FLASH0_SIZE |
| 59 | }; |
| 60 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 61 | static const io_file_spec_t bl2_file_spec = { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 62 | .path = BL2_IMAGE_NAME, |
Sandrine Bailleux | a9de1bb | 2014-03-20 15:51:02 +0000 | [diff] [blame] | 63 | .mode = FOPEN_MODE_RB |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 66 | static const io_file_spec_t bl31_file_spec = { |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 67 | .path = BL31_IMAGE_NAME, |
Sandrine Bailleux | a9de1bb | 2014-03-20 15:51:02 +0000 | [diff] [blame] | 68 | .mode = FOPEN_MODE_RB |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 71 | static const io_file_spec_t bl32_file_spec = { |
Achin Gupta | 375f538 | 2014-02-18 18:12:48 +0000 | [diff] [blame] | 72 | .path = BL32_IMAGE_NAME, |
Sandrine Bailleux | a9de1bb | 2014-03-20 15:51:02 +0000 | [diff] [blame] | 73 | .mode = FOPEN_MODE_RB |
Achin Gupta | 375f538 | 2014-02-18 18:12:48 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 76 | static const io_file_spec_t bl33_file_spec = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 77 | .path = BL33_IMAGE_NAME, |
Sandrine Bailleux | a9de1bb | 2014-03-20 15:51:02 +0000 | [diff] [blame] | 78 | .mode = FOPEN_MODE_RB |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 81 | static int open_fip(const uintptr_t spec); |
| 82 | static int open_memmap(const uintptr_t spec); |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 83 | |
| 84 | struct plat_io_policy { |
| 85 | char *image_name; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 86 | uintptr_t *dev_handle; |
| 87 | uintptr_t image_spec; |
| 88 | int (*check)(const uintptr_t spec); |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 91 | static const struct plat_io_policy policies[] = { |
| 92 | { |
| 93 | FIP_IMAGE_NAME, |
| 94 | &memmap_dev_handle, |
| 95 | (uintptr_t)&fip_block_spec, |
| 96 | open_memmap |
| 97 | }, { |
| 98 | BL2_IMAGE_NAME, |
| 99 | &fip_dev_handle, |
| 100 | (uintptr_t)&bl2_file_spec, |
| 101 | open_fip |
| 102 | }, { |
| 103 | BL31_IMAGE_NAME, |
| 104 | &fip_dev_handle, |
| 105 | (uintptr_t)&bl31_file_spec, |
| 106 | open_fip |
| 107 | }, { |
| 108 | BL32_IMAGE_NAME, |
| 109 | &fip_dev_handle, |
| 110 | (uintptr_t)&bl32_file_spec, |
| 111 | open_fip |
| 112 | }, { |
| 113 | BL33_IMAGE_NAME, |
| 114 | &fip_dev_handle, |
| 115 | (uintptr_t)&bl33_file_spec, |
| 116 | open_fip |
| 117 | }, { |
| 118 | 0, 0, 0 |
| 119 | } |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 123 | static int open_fip(const uintptr_t spec) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 124 | { |
| 125 | int result = IO_FAIL; |
| 126 | |
| 127 | /* See if a Firmware Image Package is available */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 128 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_NAME); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 129 | if (result == IO_SUCCESS) { |
| 130 | INFO("Using FIP\n"); |
| 131 | /*TODO: Check image defined in spec is present in FIP. */ |
| 132 | } |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 137 | static int open_memmap(const uintptr_t spec) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 138 | { |
| 139 | int result = IO_FAIL; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 140 | uintptr_t local_image_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 141 | |
| 142 | result = io_dev_init(memmap_dev_handle, memmap_init_params); |
| 143 | if (result == IO_SUCCESS) { |
| 144 | result = io_open(memmap_dev_handle, spec, &local_image_handle); |
| 145 | if (result == IO_SUCCESS) { |
| 146 | /* INFO("Using Memmap IO\n"); */ |
| 147 | io_close(local_image_handle); |
| 148 | } |
| 149 | } |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 154 | static int open_semihosting(const uintptr_t spec) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 155 | { |
| 156 | int result = IO_FAIL; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 157 | uintptr_t local_image_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 158 | |
| 159 | /* See if the file exists on semi-hosting.*/ |
| 160 | result = io_dev_init(sh_dev_handle, sh_init_params); |
| 161 | if (result == IO_SUCCESS) { |
| 162 | result = io_open(sh_dev_handle, spec, &local_image_handle); |
| 163 | if (result == IO_SUCCESS) { |
| 164 | INFO("Using Semi-hosting IO\n"); |
| 165 | io_close(local_image_handle); |
| 166 | } |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 171 | void io_setup (void) |
| 172 | { |
| 173 | int io_result = IO_FAIL; |
| 174 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 175 | /* Initialise the IO layer */ |
| 176 | io_init(&io_data); |
| 177 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 178 | /* Register the IO devices on this platform */ |
| 179 | io_result = register_io_dev_sh(&sh_dev_con); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 180 | assert(io_result == IO_SUCCESS); |
| 181 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 182 | io_result = register_io_dev_fip(&fip_dev_con); |
| 183 | assert(io_result == IO_SUCCESS); |
| 184 | |
| 185 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 186 | assert(io_result == IO_SUCCESS); |
| 187 | |
| 188 | /* Open connections to devices and cache the handles */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 189 | io_result = io_dev_open(sh_dev_con, sh_dev_spec, &sh_dev_handle); |
| 190 | assert(io_result == IO_SUCCESS); |
| 191 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 192 | io_result = io_dev_open(fip_dev_con, fip_dev_spec, &fip_dev_handle); |
| 193 | assert(io_result == IO_SUCCESS); |
| 194 | |
| 195 | io_result = io_dev_open(memmap_dev_con, memmap_dev_spec, |
| 196 | &memmap_dev_handle); |
| 197 | assert(io_result == IO_SUCCESS); |
| 198 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 199 | /* Ignore improbable errors in release builds */ |
| 200 | (void)io_result; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* Return an IO device handle and specification which can be used to access |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 205 | * an image. Use this to enforce platform load policy */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 206 | int plat_get_image_source(const char *image_name, uintptr_t *dev_handle, |
| 207 | uintptr_t *image_spec) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 208 | { |
| 209 | int result = IO_FAIL; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 210 | const struct plat_io_policy *policy; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 211 | |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 212 | if ((image_name != NULL) && (dev_handle != NULL) && |
| 213 | (image_spec != NULL)) { |
| 214 | policy = policies; |
| 215 | while (policy->image_name != NULL) { |
| 216 | if (strcmp(policy->image_name, image_name) == 0) { |
| 217 | result = policy->check(policy->image_spec); |
| 218 | if (result == IO_SUCCESS) { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 219 | *image_spec = policy->image_spec; |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 220 | *dev_handle = *(policy->dev_handle); |
| 221 | break; |
| 222 | } else { |
| 223 | result = open_semihosting( |
| 224 | policy->image_spec); |
| 225 | if (result == IO_SUCCESS) { |
| 226 | *dev_handle = sh_dev_handle; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 227 | *image_spec = |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 228 | policy->image_spec; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | policy++; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 233 | } |
Ryan Harkin | 1bf80fc | 2014-02-18 17:40:24 +0000 | [diff] [blame] | 234 | } else { |
| 235 | result = IO_FAIL; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 236 | } |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 237 | return result; |
| 238 | } |