blob: acc7351ce728d45b0d530794b0da920563619ba0 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Sandrine Bailleux04b66d82015-03-18 14:52:53 +00002 * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
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 Bailleux04b66d82015-03-18 14:52:53 +000032#include <assert.h>
Dan Handley9df48042015-03-19 18:58:55 +000033#include <css_def.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000034#include <debug.h>
Dan Handley9df48042015-03-19 18:58:55 +000035#include <platform.h>
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000036#include <stdint.h>
Dan Handley9df48042015-03-19 18:58:55 +000037#include "css_mhu.h"
38#include "css_scp_bootloader.h"
39#include "css_scpi.h"
40
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000041/* ID of the MHU slot used for the BOM protocol */
42#define BOM_MHU_SLOT_ID 0
Dan Handley9df48042015-03-19 18:58:55 +000043
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000044/* Boot commands sent from AP -> SCP */
45#define BOOT_CMD_INFO 0x00
46#define BOOT_CMD_DATA 0x01
Dan Handley9df48042015-03-19 18:58:55 +000047
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000048/* BOM command header */
Dan Handley9df48042015-03-19 18:58:55 +000049typedef struct {
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000050 uint32_t id : 8;
51 uint32_t reserved : 24;
52} bom_cmd_t;
Dan Handley9df48042015-03-19 18:58:55 +000053
54typedef struct {
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000055 uint32_t image_size;
56 uint32_t checksum;
57} cmd_info_payload_t;
Dan Handley9df48042015-03-19 18:58:55 +000058
59/*
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000060 * Unlike the SCPI protocol, the boot protocol uses the same memory region
Dan Handley9df48042015-03-19 18:58:55 +000061 * for both AP -> SCP and SCP -> AP transfers; define the address of this...
62 */
Sandrine Bailleux47ea1bc2015-06-09 11:53:33 +010063#define BOM_SHARED_MEM SCP_COM_SHARED_MEM_BASE
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000064#define BOM_CMD_HEADER ((bom_cmd_t *) BOM_SHARED_MEM)
65#define BOM_CMD_PAYLOAD ((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t)))
Dan Handley9df48042015-03-19 18:58:55 +000066
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000067typedef 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 Handley9df48042015-03-19 18:58:55 +000072
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000073static void scp_boot_message_start(void)
74{
75 mhu_secure_message_start(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000076}
77
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000078static void scp_boot_message_send(size_t payload_size)
Dan Handley9df48042015-03-19 18:58:55 +000079{
80 /* Make sure payload can be seen by SCP */
81 if (MHU_PAYLOAD_CACHED)
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000082 flush_dcache_range(BOM_SHARED_MEM,
83 sizeof(bom_cmd_t) + payload_size);
Dan Handley9df48042015-03-19 18:58:55 +000084
85 /* Send command to SCP */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000086 mhu_secure_message_send(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +000087}
88
89static uint32_t scp_boot_message_wait(size_t size)
90{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +000091 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 Handley9df48042015-03-19 18:58:55 +0000101
102 /* Make sure we see the reply from the SCP and not any stale data */
103 if (MHU_PAYLOAD_CACHED)
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000104 inv_dcache_range(BOM_SHARED_MEM, size);
Dan Handley9df48042015-03-19 18:58:55 +0000105
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000106 return *(uint32_t *) BOM_SHARED_MEM;
Dan Handley9df48042015-03-19 18:58:55 +0000107}
108
109static void scp_boot_message_end(void)
110{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000111 mhu_secure_message_end(BOM_MHU_SLOT_ID);
Dan Handley9df48042015-03-19 18:58:55 +0000112}
113
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000114int scp_bootloader_transfer(void *image, unsigned int image_size)
Dan Handley9df48042015-03-19 18:58:55 +0000115{
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000116 uint32_t response;
117 uint32_t checksum;
118 cmd_info_payload_t *cmd_info_payload;
119 cmd_data_payload_t *cmd_data_payload;
Dan Handley9df48042015-03-19 18:58:55 +0000120
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000121 assert((uintptr_t) image == BL30_BASE);
Dan Handley9df48042015-03-19 18:58:55 +0000122
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000123 if ((image_size == 0) || (image_size % 4 != 0)) {
124 ERROR("Invalid size for the BL3-0 image. Must be a multiple of "
125 "4 bytes and not zero (current size = 0x%x)\n",
126 image_size);
127 return -1;
128 }
Dan Handley9df48042015-03-19 18:58:55 +0000129
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000130 /* Extract the checksum from the image */
131 checksum = *(uint32_t *) image;
132 image = (char *) image + sizeof(checksum);
133 image_size -= sizeof(checksum);
Dan Handley9df48042015-03-19 18:58:55 +0000134
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000135 mhu_secure_init();
Dan Handley9df48042015-03-19 18:58:55 +0000136
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000137 VERBOSE("Send info about the BL3-0 image to be transferred to SCP\n");
Dan Handley9df48042015-03-19 18:58:55 +0000138
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000139 /*
140 * Send information about the SCP firmware image about to be transferred
141 * to SCP
142 */
143 scp_boot_message_start();
144
145 BOM_CMD_HEADER->id = BOOT_CMD_INFO;
146 cmd_info_payload = BOM_CMD_PAYLOAD;
147 cmd_info_payload->image_size = image_size;
148 cmd_info_payload->checksum = checksum;
149
150 scp_boot_message_send(sizeof(*cmd_info_payload));
Sandrine Bailleux7da652d2015-04-13 11:47:48 +0100151#if CSS_DETECT_PRE_1_7_0_SCP
152 {
153 const uint32_t deprecated_scp_nack_cmd = 0x404;
154 uint32_t mhu_status;
155
156 VERBOSE("Detecting SCP version incompatibility\n");
157
158 mhu_status = mhu_secure_message_wait();
159 if (mhu_status == deprecated_scp_nack_cmd) {
160 ERROR("Detected an incompatible version of the SCP firmware.\n");
161 ERROR("Only versions from v1.7.0 onwards are supported.\n");
162 ERROR("Please update the SCP firmware.\n");
163 return -1;
164 }
165
166 VERBOSE("SCP version looks OK\n");
167 }
168#endif /* CSS_DETECT_PRE_1_7_0_SCP */
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000169 response = scp_boot_message_wait(sizeof(response));
170 scp_boot_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000171
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000172 if (response != 0) {
173 ERROR("SCP BOOT_CMD_INFO returned error %u\n", response);
174 return -1;
175 }
176
177 VERBOSE("Transferring BL3-0 image to SCP\n");
Dan Handley9df48042015-03-19 18:58:55 +0000178
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000179 /* Transfer BL3-0 image to SCP */
180 scp_boot_message_start();
Dan Handley9df48042015-03-19 18:58:55 +0000181
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000182 BOM_CMD_HEADER->id = BOOT_CMD_DATA;
183 cmd_data_payload = BOM_CMD_PAYLOAD;
Sandrine Bailleux47ea1bc2015-06-09 11:53:33 +0100184 cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE;
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000185 cmd_data_payload->block_size = image_size;
Dan Handley9df48042015-03-19 18:58:55 +0000186
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000187 scp_boot_message_send(sizeof(*cmd_data_payload));
188 response = scp_boot_message_wait(sizeof(response));
189 scp_boot_message_end();
Dan Handley9df48042015-03-19 18:58:55 +0000190
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000191 if (response != 0) {
192 ERROR("SCP BOOT_CMD_DATA returned error %u\n", response);
193 return -1;
Dan Handley9df48042015-03-19 18:58:55 +0000194 }
195
Sandrine Bailleux04b66d82015-03-18 14:52:53 +0000196 VERBOSE("Waiting for SCP to signal it is ready to go on\n");
197
Dan Handley9df48042015-03-19 18:58:55 +0000198 /* Wait for SCP to signal it's ready */
199 return scpi_wait_ready();
200}