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