Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 1 | /* |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 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 <arch_helpers.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 32 | #include <assert.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 33 | #include <css_def.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 34 | #include <debug.h> |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 35 | #include <platform.h> |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 36 | #include <stdint.h> |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 37 | #include "../drivers/scpi/css_mhu.h" |
| 38 | #include "../drivers/scpi/css_scpi.h" |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 39 | #include "css_scp_bootloader.h" |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 40 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 41 | /* ID of the MHU slot used for the BOM protocol */ |
| 42 | #define BOM_MHU_SLOT_ID 0 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 43 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 44 | /* Boot commands sent from AP -> SCP */ |
| 45 | #define BOOT_CMD_INFO 0x00 |
| 46 | #define BOOT_CMD_DATA 0x01 |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 47 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 48 | /* BOM command header */ |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 49 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 50 | uint32_t id : 8; |
| 51 | uint32_t reserved : 24; |
| 52 | } bom_cmd_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 53 | |
| 54 | typedef struct { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 55 | uint32_t image_size; |
| 56 | uint32_t checksum; |
| 57 | } cmd_info_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 60 | * Unlike the SCPI protocol, the boot protocol uses the same memory region |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 61 | * for both AP -> SCP and SCP -> AP transfers; define the address of this... |
| 62 | */ |
Vikram Kanigiri | 7208419 | 2016-02-08 16:29:30 +0000 | [diff] [blame] | 63 | #define BOM_SHARED_MEM PLAT_CSS_SCP_COM_SHARED_MEM_BASE |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 64 | #define BOM_CMD_HEADER ((bom_cmd_t *) BOM_SHARED_MEM) |
| 65 | #define BOM_CMD_PAYLOAD ((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t))) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 66 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 67 | typedef struct { |
| 68 | /* Offset from the base address of the Trusted RAM */ |
| 69 | uint32_t offset; |
| 70 | uint32_t block_size; |
| 71 | } cmd_data_payload_t; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 72 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 73 | static void scp_boot_message_start(void) |
| 74 | { |
| 75 | mhu_secure_message_start(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 78 | static void scp_boot_message_send(size_t payload_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 79 | { |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 80 | /* Ensure that any write to the BOM payload area is seen by SCP before |
| 81 | * we write to the MHU register. If these 2 writes were reordered by |
| 82 | * the CPU then SCP would read stale payload data */ |
| 83 | dmbst(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 84 | |
| 85 | /* Send command to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 86 | mhu_secure_message_send(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static uint32_t scp_boot_message_wait(size_t size) |
| 90 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 91 | uint32_t mhu_status; |
| 92 | |
| 93 | mhu_status = mhu_secure_message_wait(); |
| 94 | |
| 95 | /* Expect an SCP Boot Protocol message, reject any other protocol */ |
| 96 | if (mhu_status != (1 << BOM_MHU_SLOT_ID)) { |
| 97 | ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n", |
| 98 | mhu_status); |
| 99 | panic(); |
| 100 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 101 | |
Juan Castillo | 2e86cb1 | 2016-01-13 15:01:09 +0000 | [diff] [blame] | 102 | /* Ensure that any read to the BOM payload area is done after reading |
| 103 | * the MHU register. If these 2 reads were reordered then the CPU would |
| 104 | * read invalid payload data */ |
| 105 | dmbld(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 106 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 107 | return *(uint32_t *) BOM_SHARED_MEM; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void scp_boot_message_end(void) |
| 111 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 112 | mhu_secure_message_end(BOM_MHU_SLOT_ID); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 115 | int scp_bootloader_transfer(void *image, unsigned int image_size) |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 116 | { |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 117 | uint32_t response; |
| 118 | uint32_t checksum; |
| 119 | cmd_info_payload_t *cmd_info_payload; |
| 120 | cmd_data_payload_t *cmd_data_payload; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 121 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 122 | assert((uintptr_t) image == SCP_BL2_BASE); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 123 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 124 | if ((image_size == 0) || (image_size % 4 != 0)) { |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 125 | ERROR("Invalid size for the SCP_BL2 image. Must be a multiple of " |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 126 | "4 bytes and not zero (current size = 0x%x)\n", |
| 127 | image_size); |
| 128 | return -1; |
| 129 | } |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 130 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 131 | /* Extract the checksum from the image */ |
| 132 | checksum = *(uint32_t *) image; |
| 133 | image = (char *) image + sizeof(checksum); |
| 134 | image_size -= sizeof(checksum); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 135 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 136 | mhu_secure_init(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 137 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 138 | VERBOSE("Send info about the SCP_BL2 image to be transferred to SCP\n"); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 139 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 140 | /* |
| 141 | * Send information about the SCP firmware image about to be transferred |
| 142 | * to SCP |
| 143 | */ |
| 144 | scp_boot_message_start(); |
| 145 | |
| 146 | BOM_CMD_HEADER->id = BOOT_CMD_INFO; |
| 147 | cmd_info_payload = BOM_CMD_PAYLOAD; |
| 148 | cmd_info_payload->image_size = image_size; |
| 149 | cmd_info_payload->checksum = checksum; |
| 150 | |
| 151 | scp_boot_message_send(sizeof(*cmd_info_payload)); |
Sandrine Bailleux | 7da652d | 2015-04-13 11:47:48 +0100 | [diff] [blame] | 152 | #if CSS_DETECT_PRE_1_7_0_SCP |
| 153 | { |
| 154 | const uint32_t deprecated_scp_nack_cmd = 0x404; |
| 155 | uint32_t mhu_status; |
| 156 | |
| 157 | VERBOSE("Detecting SCP version incompatibility\n"); |
| 158 | |
| 159 | mhu_status = mhu_secure_message_wait(); |
| 160 | if (mhu_status == deprecated_scp_nack_cmd) { |
| 161 | ERROR("Detected an incompatible version of the SCP firmware.\n"); |
| 162 | ERROR("Only versions from v1.7.0 onwards are supported.\n"); |
| 163 | ERROR("Please update the SCP firmware.\n"); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | VERBOSE("SCP version looks OK\n"); |
| 168 | } |
| 169 | #endif /* CSS_DETECT_PRE_1_7_0_SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 170 | response = scp_boot_message_wait(sizeof(response)); |
| 171 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 172 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 173 | if (response != 0) { |
| 174 | ERROR("SCP BOOT_CMD_INFO returned error %u\n", response); |
| 175 | return -1; |
| 176 | } |
| 177 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 178 | VERBOSE("Transferring SCP_BL2 image to SCP\n"); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 179 | |
Juan Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 180 | /* Transfer SCP_BL2 image to SCP */ |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 181 | scp_boot_message_start(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 182 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 183 | BOM_CMD_HEADER->id = BOOT_CMD_DATA; |
| 184 | cmd_data_payload = BOM_CMD_PAYLOAD; |
Sandrine Bailleux | 47ea1bc | 2015-06-09 11:53:33 +0100 | [diff] [blame] | 185 | cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE; |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 186 | cmd_data_payload->block_size = image_size; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 187 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 188 | scp_boot_message_send(sizeof(*cmd_data_payload)); |
| 189 | response = scp_boot_message_wait(sizeof(response)); |
| 190 | scp_boot_message_end(); |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 191 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 192 | if (response != 0) { |
| 193 | ERROR("SCP BOOT_CMD_DATA returned error %u\n", response); |
| 194 | return -1; |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Sandrine Bailleux | 04b66d8 | 2015-03-18 14:52:53 +0000 | [diff] [blame] | 197 | VERBOSE("Waiting for SCP to signal it is ready to go on\n"); |
| 198 | |
Dan Handley | 9df4804 | 2015-03-19 18:58:55 +0000 | [diff] [blame] | 199 | /* Wait for SCP to signal it's ready */ |
| 200 | return scpi_wait_ready(); |
| 201 | } |