Jiafei Pan | 46367ad | 2018-03-02 07:23:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | #include <assert.h> |
| 7 | #include <debug.h> |
| 8 | #include <firmware_image_package.h> |
| 9 | #include <io_driver.h> |
| 10 | #include <io_fip.h> |
| 11 | #include <io_memmap.h> |
| 12 | #include <io_storage.h> |
| 13 | #include "platform_def.h" |
| 14 | |
| 15 | /* IO devices */ |
| 16 | static const io_dev_connector_t *fip_dev_con; |
| 17 | static uintptr_t fip_dev_handle; |
| 18 | static const io_dev_connector_t *memmap_dev_con; |
| 19 | static uintptr_t memmap_dev_handle; |
| 20 | |
| 21 | static const io_block_spec_t fip_block_spec = { |
| 22 | .offset = PLAT_LS_FIP_BASE, |
| 23 | .length = PLAT_LS_FIP_MAX_SIZE |
| 24 | }; |
| 25 | |
| 26 | static const io_uuid_spec_t bl2_uuid_spec = { |
| 27 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 28 | }; |
| 29 | |
| 30 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 31 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 32 | }; |
| 33 | |
| 34 | static const io_uuid_spec_t bl32_uuid_spec = { |
| 35 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 36 | }; |
| 37 | |
| 38 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 39 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 40 | }; |
| 41 | |
| 42 | static int open_fip(const uintptr_t spec); |
| 43 | static int open_memmap(const uintptr_t spec); |
| 44 | |
| 45 | struct plat_io_policy { |
| 46 | uintptr_t *dev_handle; |
| 47 | uintptr_t image_spec; |
| 48 | int (*check)(const uintptr_t spec); |
| 49 | }; |
| 50 | |
| 51 | static const struct plat_io_policy policies[] = { |
| 52 | [FIP_IMAGE_ID] = { |
| 53 | &memmap_dev_handle, |
| 54 | (uintptr_t)&fip_block_spec, |
| 55 | open_memmap |
| 56 | }, |
| 57 | [BL2_IMAGE_ID] = { |
| 58 | &fip_dev_handle, |
| 59 | (uintptr_t)&bl2_uuid_spec, |
| 60 | open_fip |
| 61 | }, |
| 62 | [BL31_IMAGE_ID] = { |
| 63 | &fip_dev_handle, |
| 64 | (uintptr_t)&bl31_uuid_spec, |
| 65 | open_fip |
| 66 | }, |
| 67 | [BL32_IMAGE_ID] = { |
| 68 | &fip_dev_handle, |
| 69 | (uintptr_t)&bl32_uuid_spec, |
| 70 | open_fip |
| 71 | }, |
| 72 | [BL33_IMAGE_ID] = { |
| 73 | &fip_dev_handle, |
| 74 | (uintptr_t)&bl33_uuid_spec, |
| 75 | open_fip |
| 76 | }, |
| 77 | }; |
| 78 | |
| 79 | static int open_fip(const uintptr_t spec) |
| 80 | { |
| 81 | int result; |
| 82 | uintptr_t local_image_handle; |
| 83 | |
| 84 | /* See if a Firmware Image Package is available */ |
| 85 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
| 86 | if (result == 0) { |
| 87 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 88 | if (result == 0) { |
| 89 | VERBOSE("Using FIP\n"); |
| 90 | io_close(local_image_handle); |
| 91 | } |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static int open_memmap(const uintptr_t spec) |
| 98 | { |
| 99 | int result; |
| 100 | uintptr_t local_image_handle; |
| 101 | |
| 102 | result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL); |
| 103 | if (result == 0) { |
| 104 | result = io_open(memmap_dev_handle, spec, &local_image_handle); |
| 105 | if (result == 0) { |
| 106 | VERBOSE("Using Memmap\n"); |
| 107 | io_close(local_image_handle); |
| 108 | } |
| 109 | } |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void ls_io_setup(void) |
| 115 | { |
| 116 | int io_result; |
| 117 | |
| 118 | io_result = register_io_dev_fip(&fip_dev_con); |
| 119 | assert(io_result == 0); |
| 120 | |
| 121 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 122 | assert(io_result == 0); |
| 123 | |
| 124 | /* Open connections to devices and cache the handles */ |
| 125 | io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
| 126 | &fip_dev_handle); |
| 127 | assert(io_result == 0); |
| 128 | |
| 129 | io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, |
| 130 | &memmap_dev_handle); |
| 131 | assert(io_result == 0); |
| 132 | |
| 133 | /* Ignore improbable errors in release builds */ |
| 134 | (void)io_result; |
| 135 | } |
| 136 | |
| 137 | void plat_ls_io_setup(void) |
| 138 | { |
| 139 | ls_io_setup(); |
| 140 | } |
| 141 | |
| 142 | int plat_ls_get_alt_image_source( |
| 143 | unsigned int image_id __unused, |
| 144 | uintptr_t *dev_handle __unused, |
| 145 | uintptr_t *image_spec __unused) |
| 146 | { |
| 147 | /* By default do not try an alternative */ |
| 148 | return -ENOENT; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Return an IO device handle and specification which can be used to access |
| 153 | * an image. Use this to enforce platform load policy. |
| 154 | */ |
| 155 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 156 | uintptr_t *image_spec) |
| 157 | { |
| 158 | int result; |
| 159 | const struct plat_io_policy *policy; |
| 160 | |
| 161 | assert(image_id < ARRAY_SIZE(policies)); |
| 162 | |
| 163 | policy = &policies[image_id]; |
| 164 | result = policy->check(policy->image_spec); |
| 165 | if (result == 0) { |
| 166 | *image_spec = policy->image_spec; |
| 167 | *dev_handle = *(policy->dev_handle); |
| 168 | } else { |
| 169 | VERBOSE("Trying alternative IO\n"); |
| 170 | result = plat_ls_get_alt_image_source(image_id, dev_handle, |
| 171 | image_spec); |
| 172 | } |
| 173 | |
| 174 | return result; |
| 175 | } |