Amit Blay | fe23ee2 | 2015-01-09 19:09:51 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. |
Deepa Dinamani | 904f8f8 | 2012-12-05 16:35:01 -0800 | [diff] [blame] | 2 | * |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
Deepa Dinamani | 904f8f8 | 2012-12-05 16:35:01 -0800 | [diff] [blame] | 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <err.h> |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 32 | #include <asm.h> |
| 33 | #include <bits.h> |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 34 | #include <arch/ops.h> |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 35 | #include <rand.h> |
| 36 | #include <image_verify.h> |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 37 | #include <dload_util.h> |
| 38 | #include <platform/iomap.h> |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 39 | #include <board.h> |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 40 | #include "scm.h" |
| 41 | |
| 42 | #pragma GCC optimize ("O0") |
| 43 | |
| 44 | /* From Linux Kernel asm/system.h */ |
| 45 | #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t" |
| 46 | |
| 47 | #ifndef offsetof |
| 48 | # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 49 | #endif |
| 50 | |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 51 | #define SCM_CLASS_REGISTER (0x2 << 8) |
| 52 | #define SCM_MASK_IRQS BIT(5) |
| 53 | #define SCM_ATOMIC(svc, cmd, n) ((((((svc) & 0x3f) << 10)|((cmd) & 0x3ff)) << 12) | \ |
| 54 | SCM_CLASS_REGISTER | \ |
| 55 | SCM_MASK_IRQS | \ |
| 56 | ((n) & 0xf)) |
| 57 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 58 | /* SCM interface as per ARM spec present? */ |
| 59 | bool scm_arm_support; |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 60 | static bool scm_initialized; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 61 | |
Dinesh K Garg | 6bbbb70 | 2015-01-30 11:13:31 -0800 | [diff] [blame] | 62 | bool is_scm_armv8_support() |
| 63 | { |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 64 | if (!scm_initialized) |
| 65 | { |
| 66 | scm_init(); |
| 67 | scm_initialized = true; |
| 68 | } |
| 69 | |
Dinesh K Garg | 6bbbb70 | 2015-01-30 11:13:31 -0800 | [diff] [blame] | 70 | return scm_arm_support; |
| 71 | } |
| 72 | |
Channagoud Kadabi | 77f46a3 | 2015-08-05 16:13:13 -0700 | [diff] [blame] | 73 | int is_scm_call_available(uint32_t svc_id, uint32_t cmd_id) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 74 | { |
vijay kumar | 496a2ff | 2015-07-22 21:22:48 +0530 | [diff] [blame] | 75 | int ret; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 76 | scmcall_arg scm_arg = {0}; |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 77 | scmcall_ret scm_ret = {0}; |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 78 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 79 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_INFO, IS_CALL_AVAIL_CMD); |
| 80 | scm_arg.x1 = MAKE_SCM_ARGS(0x1); |
| 81 | scm_arg.x2 = MAKE_SIP_SCM_CMD(svc_id, cmd_id); |
| 82 | |
| 83 | ret = scm_call2(&scm_arg, &scm_ret); |
| 84 | |
| 85 | if (!ret) |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 86 | return scm_ret.x1; |
| 87 | |
| 88 | return ret; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 91 | static int scm_arm_support_available(uint32_t svc_id, uint32_t cmd_id) |
| 92 | { |
vijay kumar | 496a2ff | 2015-07-22 21:22:48 +0530 | [diff] [blame] | 93 | int ret; |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 94 | |
| 95 | ret = is_scm_call_available(SCM_SVC_INFO, IS_CALL_AVAIL_CMD); |
| 96 | |
| 97 | if (ret > 0) |
| 98 | scm_arm_support = true; |
| 99 | |
| 100 | return ret; |
| 101 | } |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 102 | |
| 103 | void scm_init() |
| 104 | { |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 105 | int ret; |
| 106 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 107 | if (scm_initialized) |
| 108 | return; |
| 109 | |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 110 | ret = scm_arm_support_available(SCM_SVC_INFO, IS_CALL_AVAIL_CMD); |
| 111 | |
vijay kumar | 496a2ff | 2015-07-22 21:22:48 +0530 | [diff] [blame] | 112 | if (ret < 0) |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 113 | dprintf(CRITICAL, "Failed to initialize SCM\n"); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 114 | } |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 115 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 116 | /** |
| 117 | * alloc_scm_command() - Allocate an SCM command |
| 118 | * @cmd_size: size of the command buffer |
| 119 | * @resp_size: size of the response buffer |
| 120 | * |
| 121 | * Allocate an SCM command, including enough room for the command |
| 122 | * and response headers as well as the command and response buffers. |
| 123 | * |
| 124 | * Returns a valid &scm_command on success or %NULL if the allocation fails. |
| 125 | */ |
| 126 | static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size) |
| 127 | { |
| 128 | struct scm_command *cmd; |
| 129 | size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 130 | resp_size; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 131 | |
Deepa Dinamani | 904f8f8 | 2012-12-05 16:35:01 -0800 | [diff] [blame] | 132 | cmd = memalign(CACHE_LINE, ROUNDUP(len, CACHE_LINE)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 133 | if (cmd) { |
Pavel Nedev | 80ce36f | 2014-01-06 14:26:17 +0200 | [diff] [blame] | 134 | memset(cmd, 0, len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 135 | cmd->len = len; |
| 136 | cmd->buf_offset = offsetof(struct scm_command, buf); |
| 137 | cmd->resp_hdr_offset = cmd->buf_offset + cmd_size; |
| 138 | } |
| 139 | return cmd; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * free_scm_command() - Free an SCM command |
| 144 | * @cmd: command to free |
| 145 | * |
| 146 | * Free an SCM command. |
| 147 | */ |
| 148 | static inline void free_scm_command(struct scm_command *cmd) |
| 149 | { |
| 150 | free(cmd); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * scm_command_to_response() - Get a pointer to a scm_response |
| 155 | * @cmd: command |
| 156 | * |
| 157 | * Returns a pointer to a response for a command. |
| 158 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 159 | static inline struct scm_response *scm_command_to_response(const struct |
| 160 | scm_command *cmd) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 161 | { |
| 162 | return (void *)cmd + cmd->resp_hdr_offset; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * scm_get_command_buffer() - Get a pointer to a command buffer |
| 167 | * @cmd: command |
| 168 | * |
| 169 | * Returns a pointer to the command buffer of a command. |
| 170 | */ |
| 171 | static inline void *scm_get_command_buffer(const struct scm_command *cmd) |
| 172 | { |
| 173 | return (void *)cmd->buf; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * scm_get_response_buffer() - Get a pointer to a response buffer |
| 178 | * @rsp: response |
| 179 | * |
| 180 | * Returns a pointer to a response buffer of a response. |
| 181 | */ |
| 182 | static inline void *scm_get_response_buffer(const struct scm_response *rsp) |
| 183 | { |
| 184 | return (void *)rsp + rsp->buf_offset; |
| 185 | } |
| 186 | |
| 187 | static uint32_t smc(uint32_t cmd_addr) |
| 188 | { |
| 189 | uint32_t context_id; |
| 190 | register uint32_t r0 __asm__("r0") = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 191 | register uint32_t r1 __asm__("r1") = (uint32_t) & context_id; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 192 | register uint32_t r2 __asm__("r2") = cmd_addr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 193 | __asm__("1:smc #0 @ switch to secure world\n" "cmp r0, #1 \n" "beq 1b \n": "=r"(r0): "r"(r0), "r"(r1), "r"(r2):"r3", "cc"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 194 | return r0; |
| 195 | } |
| 196 | |
| 197 | /** |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 198 | * scm_call_automic: Make scm call with one or no argument |
| 199 | * @svc: service id |
| 200 | * @cmd: command id |
| 201 | * @ arg1: argument |
| 202 | */ |
| 203 | |
| 204 | static int scm_call_atomic(uint32_t svc, uint32_t cmd, uint32_t arg1) |
| 205 | { |
| 206 | uint32_t context_id; |
| 207 | register uint32_t r0 __asm__("r0") = SCM_ATOMIC(svc, cmd, 1); |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 208 | register uint32_t r1 __asm__("r1") = (uint32_t)&context_id; |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 209 | register uint32_t r2 __asm__("r2") = arg1; |
| 210 | |
| 211 | __asm__ volatile( |
| 212 | __asmeq("%0", "r0") |
| 213 | __asmeq("%1", "r0") |
| 214 | __asmeq("%2", "r1") |
| 215 | __asmeq("%3", "r2") |
| 216 | "smc #0 @ switch to secure world\n" |
| 217 | : "=r" (r0) |
| 218 | : "r" (r0), "r" (r1), "r" (r2) |
| 219 | : "r3"); |
| 220 | return r0; |
| 221 | } |
| 222 | |
| 223 | /** |
Aparna Mallavarapu | 68e233f | 2014-03-21 19:18:34 +0530 | [diff] [blame] | 224 | * scm_call_atomic2() - Send an atomic SCM command with two arguments |
| 225 | * @svc_id: service identifier |
| 226 | * @cmd_id: command identifier |
| 227 | * @arg1: first argument |
| 228 | * @arg2: second argument |
| 229 | * |
| 230 | * This shall only be used with commands that are guaranteed to be |
| 231 | * uninterruptable, atomic and SMP safe. |
| 232 | */ |
| 233 | int scm_call_atomic2(uint32_t svc, uint32_t cmd, uint32_t arg1, uint32_t arg2) |
| 234 | { |
| 235 | int context_id; |
| 236 | register uint32_t r0 __asm__("r0") = SCM_ATOMIC(svc, cmd, 2); |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 237 | register uint32_t r1 __asm__("r1") = (uint32_t)&context_id; |
Aparna Mallavarapu | 68e233f | 2014-03-21 19:18:34 +0530 | [diff] [blame] | 238 | register uint32_t r2 __asm__("r2") = arg1; |
| 239 | register uint32_t r3 __asm__("r3") = arg2; |
| 240 | |
| 241 | __asm__ volatile( |
| 242 | __asmeq("%0", "r0") |
| 243 | __asmeq("%1", "r0") |
| 244 | __asmeq("%2", "r1") |
| 245 | __asmeq("%3", "r2") |
| 246 | __asmeq("%4", "r3") |
| 247 | "smc #0 @ switch to secure world\n" |
| 248 | : "=r" (r0) |
| 249 | : "r" (r0), "r" (r1), "r" (r2), "r" (r3)); |
| 250 | return r0; |
| 251 | } |
| 252 | |
| 253 | /** |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 254 | * scm_call() - Send an SCM command |
| 255 | * @svc_id: service identifier |
| 256 | * @cmd_id: command identifier |
| 257 | * @cmd_buf: command buffer |
| 258 | * @cmd_len: length of the command buffer |
| 259 | * @resp_buf: response buffer |
| 260 | * @resp_len: length of the response buffer |
| 261 | * |
| 262 | * Sends a command to the SCM and waits for the command to finish processing. |
| 263 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 264 | int |
| 265 | scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf, |
| 266 | size_t cmd_len, void *resp_buf, size_t resp_len) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 267 | { |
| 268 | int ret; |
| 269 | struct scm_command *cmd; |
| 270 | struct scm_response *rsp; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 271 | uint8_t *resp_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 272 | |
| 273 | cmd = alloc_scm_command(cmd_len, resp_len); |
| 274 | if (!cmd) |
| 275 | return ERR_NO_MEMORY; |
| 276 | |
| 277 | cmd->id = (svc_id << 10) | cmd_id; |
| 278 | if (cmd_buf) |
| 279 | memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len); |
| 280 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 281 | /* Flush command to main memory for TZ */ |
| 282 | arch_clean_invalidate_cache_range((addr_t) cmd, cmd->len); |
| 283 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 284 | ret = smc((uint32_t) cmd); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 285 | if (ret) |
| 286 | goto out; |
| 287 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 288 | if (resp_len) { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 289 | rsp = scm_command_to_response(cmd); |
| 290 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 291 | do |
| 292 | { |
| 293 | /* Need to invalidate before each check since TZ will update |
| 294 | * the response complete flag in main memory. |
| 295 | */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 296 | arch_invalidate_cache_range((addr_t) rsp, sizeof(*rsp)); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 297 | } while (!rsp->is_complete); |
| 298 | |
| 299 | |
| 300 | resp_ptr = scm_get_response_buffer(rsp); |
| 301 | |
| 302 | /* Invalidate any cached response data */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 303 | arch_invalidate_cache_range((addr_t) resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 304 | |
| 305 | if (resp_buf) |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 306 | memcpy(resp_buf, resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 307 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 308 | out: |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 309 | free_scm_command(cmd); |
| 310 | return ret; |
| 311 | } |
| 312 | |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 313 | int restore_secure_cfg(uint32_t id) |
| 314 | { |
Channagoud Kadabi | acaa75e | 2014-06-09 16:29:29 -0700 | [diff] [blame] | 315 | int ret = 0; |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 316 | tz_secure_cfg secure_cfg; |
| 317 | |
Siddhartha Agrawal | d464889 | 2013-02-17 18:16:18 -0800 | [diff] [blame] | 318 | secure_cfg.id = id; |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 319 | secure_cfg.spare = 0; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 320 | scmcall_arg scm_arg = {0}; |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 321 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 322 | if(!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 323 | { |
| 324 | ret = scm_call(SVC_MEMORY_PROTECTION, IOMMU_SECURE_CFG, &secure_cfg, sizeof(secure_cfg), |
| 325 | NULL, 0); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SVC_MEMORY_PROTECTION, IOMMU_SECURE_CFG); |
| 330 | scm_arg.x1 = MAKE_SCM_ARGS(0x2); |
| 331 | scm_arg.x2 = id; |
| 332 | scm_arg.x3 = 0x0; /* Spare unused */ |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 333 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 334 | ret = scm_call2(&scm_arg, NULL); |
| 335 | } |
| 336 | |
| 337 | if (ret) |
| 338 | { |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 339 | dprintf(CRITICAL, "Secure Config failed\n"); |
| 340 | ret = 1; |
Channagoud Kadabi | acaa75e | 2014-06-09 16:29:29 -0700 | [diff] [blame] | 341 | } |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 342 | |
| 343 | return ret; |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 346 | /* SCM Encrypt Command */ |
| 347 | int encrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 348 | { |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 349 | int ret; |
| 350 | img_req cmd; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 351 | scmcall_arg scm_arg = {0}; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 352 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 353 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 354 | cmd.img_ptr = (uint32*) img_ptr; |
| 355 | cmd.img_len_ptr = img_len_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 356 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 357 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 358 | * It must be flushed/invalidated before and after TZ call. |
| 359 | */ |
| 360 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 361 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 362 | if (!is_scm_armv8_support()) |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 363 | { |
| 364 | ret = scm_call(SCM_SVC_SSD, SSD_ENCRYPT_ID, &cmd, sizeof(cmd), NULL, 0); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_SSD,SSD_ENCRYPT_ID); |
vijay kumar | 83b50d6 | 2015-01-09 19:09:59 +0530 | [diff] [blame] | 369 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_BUFFER_READWRITE,SMC_PARAM_TYPE_BUFFER_READWRITE); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 370 | scm_arg.x2 = (uint32_t) cmd.img_ptr; |
| 371 | scm_arg.x3 = (uint32_t) cmd.img_len_ptr; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 372 | |
| 373 | ret = scm_call2(&scm_arg, NULL); |
| 374 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 375 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 376 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 377 | * before we use them. |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 378 | */ |
| 379 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 380 | arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr)); |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 381 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 382 | /* Invalidate the updated image data */ |
| 383 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 384 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 388 | /* SCM Decrypt Command */ |
| 389 | int decrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
| 390 | { |
| 391 | int ret; |
| 392 | img_req cmd; |
| 393 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 394 | if (is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 395 | { |
| 396 | dprintf(INFO, "%s:SCM call is not supported\n",__func__); |
| 397 | return -1; |
| 398 | } |
| 399 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 400 | cmd.img_ptr = (uint32*) img_ptr; |
| 401 | cmd.img_len_ptr = img_len_ptr; |
| 402 | |
| 403 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 404 | * It must be flushed/invalidated before and after TZ call. |
| 405 | */ |
| 406 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 407 | |
| 408 | ret = scm_call(SCM_SVC_SSD, SSD_DECRYPT_ID, &cmd, sizeof(cmd), NULL, 0); |
| 409 | |
| 410 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 411 | * before we use them. |
| 412 | */ |
| 413 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
| 414 | arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr)); |
| 415 | |
| 416 | /* Invalidate the updated image data */ |
| 417 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 418 | |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 423 | static int ssd_image_is_encrypted(uint32_t ** img_ptr, uint32_t * img_len_ptr, uint32 * ctx_id) |
| 424 | { |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 425 | int ret = 0; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 426 | ssd_parse_md_req parse_req; |
| 427 | ssd_parse_md_rsp parse_rsp; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 428 | int prev_len = 0; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 429 | scmcall_arg scm_arg = {0}; |
| 430 | scmcall_ret scm_ret = {0}; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 431 | /* Populate meta-data ptr. Here md_len is the meta-data length. |
| 432 | * The Code below follows a growing length approach. First send |
| 433 | * min(img_len_ptr,SSD_HEADER_MIN_SIZE) say 128 bytes for example. |
| 434 | * If parse_rsp.status = PARSING_INCOMPLETE we send md_len = 256. |
| 435 | * If subsequent status = PARSING_INCOMPLETE we send md_len = 512, |
| 436 | * 1024bytes and so on until we get an valid response(rsp.status) from TZ*/ |
| 437 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 438 | parse_req.md = (uint32*)*img_ptr; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 439 | parse_req.md_len = ((*img_len_ptr) >= SSD_HEADER_MIN_SIZE) ? SSD_HEADER_MIN_SIZE : (*img_len_ptr); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 440 | |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 441 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, parse_req.md_len); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 442 | |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 443 | do |
| 444 | { |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 445 | if (!is_scm_armv8_support()) |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 446 | { |
| 447 | ret = scm_call(SCM_SVC_SSD, |
| 448 | SSD_PARSE_MD_ID, |
| 449 | &parse_req, |
| 450 | sizeof(parse_req), |
| 451 | &parse_rsp, |
| 452 | sizeof(parse_rsp)); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_SSD, SSD_PARSE_MD_ID); |
| 457 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_VALUE,SMC_PARAM_TYPE_BUFFER_READWRITE); |
| 458 | scm_arg.x2 = parse_req.md_len; |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 459 | scm_arg.x3 = (uint32_t) parse_req.md; |
Aparna Mallavarapu | d83990a | 2014-12-24 12:54:35 +0530 | [diff] [blame] | 460 | scm_arg.atomic = true; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 461 | |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 462 | ret = scm_call2(&scm_arg, &scm_ret); |
| 463 | parse_rsp.status = scm_ret.x1; |
| 464 | } |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 465 | if(!ret && (parse_rsp.status == SSD_PMD_PARSING_INCOMPLETE)) |
| 466 | { |
| 467 | prev_len = parse_req.md_len; |
| 468 | |
| 469 | parse_req.md_len *= MULTIPLICATION_FACTOR; |
| 470 | |
Venkatesh Yadav Abbarapu | af7bfe0 | 2013-11-11 16:56:04 +0530 | [diff] [blame] | 471 | arch_clean_invalidate_cache_range((addr_t) (*img_ptr + prev_len), |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 472 | (parse_req.md_len - prev_len) ); |
| 473 | |
| 474 | continue; |
| 475 | } |
| 476 | else |
| 477 | break; |
| 478 | |
| 479 | } while(true); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 480 | |
| 481 | if(!ret) |
| 482 | { |
| 483 | if(parse_rsp.status == SSD_PMD_ENCRYPTED) |
| 484 | { |
| 485 | *ctx_id = parse_rsp.md_ctx_id; |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 486 | *img_len_ptr = *img_len_ptr - ((uint8_t*)parse_rsp.md_end_ptr - (uint8_t*)*img_ptr); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 487 | *img_ptr = (uint32_t*)parse_rsp.md_end_ptr; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 488 | } |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 489 | |
| 490 | ret = parse_rsp.status; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 491 | } |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 492 | else |
| 493 | { |
| 494 | dprintf(CRITICAL,"ssd_image_is_encrypted call failed"); |
| 495 | |
| 496 | ASSERT(ret == 0); |
| 497 | } |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | int decrypt_scm_v2(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
| 503 | { |
| 504 | int ret = 0; |
| 505 | uint32 ctx_id = 0; |
| 506 | ssd_decrypt_img_frag_req decrypt_req; |
| 507 | ssd_decrypt_img_frag_rsp decrypt_rsp; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 508 | scmcall_arg scm_arg = {0}; |
| 509 | scmcall_ret scm_ret = {0}; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 510 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 511 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 512 | ret = ssd_image_is_encrypted(img_ptr,img_len_ptr,&ctx_id); |
| 513 | switch(ret) |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 514 | { |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 515 | case SSD_PMD_ENCRYPTED: |
| 516 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 517 | * It must be flushed/invalidated before and after TZ call. |
| 518 | */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 519 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 520 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 521 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 522 | /*decrypt the image here*/ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 523 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 524 | decrypt_req.md_ctx_id = ctx_id; |
| 525 | decrypt_req.last_frag = 1; |
| 526 | decrypt_req.frag_len = *img_len_ptr; |
| 527 | decrypt_req.frag = *img_ptr; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 528 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 529 | if (!is_scm_armv8_support()) |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 530 | { |
| 531 | ret = scm_call(SCM_SVC_SSD, |
| 532 | SSD_DECRYPT_IMG_FRAG_ID, |
| 533 | &decrypt_req, |
| 534 | sizeof(decrypt_req), |
| 535 | &decrypt_rsp, |
| 536 | sizeof(decrypt_rsp)); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_SSD, SSD_DECRYPT_IMG_FRAG_ID); |
| 541 | scm_arg.x1 = MAKE_SCM_ARGS(0x4,SMC_PARAM_TYPE_VALUE,SMC_PARAM_TYPE_VALUE,SMC_PARAM_TYPE_VALUE,SMC_PARAM_TYPE_BUFFER_READWRITE); |
| 542 | scm_arg.x2 = decrypt_req.md_ctx_id; |
| 543 | scm_arg.x3 = decrypt_req.last_frag; |
| 544 | scm_arg.x4 = decrypt_req.frag_len; |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 545 | scm_arg.x5[0] = (uint32_t) decrypt_req.frag; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 546 | |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 547 | ret = scm_call2(&scm_arg, &scm_ret); |
| 548 | decrypt_rsp.status = scm_ret.x1; |
| 549 | } |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 550 | if(!ret){ |
| 551 | ret = decrypt_rsp.status; |
| 552 | } |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 553 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 554 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 555 | * before we use them. |
| 556 | */ |
| 557 | arch_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
| 558 | arch_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr)); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 559 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 560 | /* Invalidate the updated image data */ |
| 561 | arch_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 562 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | case SSD_PMD_NOT_ENCRYPTED: |
| 566 | case SSD_PMD_NO_MD_FOUND: |
| 567 | ret = 0; |
| 568 | break; |
| 569 | |
| 570 | case SSD_PMD_BUSY: |
| 571 | case SSD_PMD_BAD_MD_PTR_OR_LEN: |
| 572 | case SSD_PMD_PARSING_INCOMPLETE: |
| 573 | case SSD_PMD_PARSING_FAILED: |
| 574 | case SSD_PMD_SETUP_CIPHER_FAILED: |
| 575 | dprintf(CRITICAL,"decrypt_scm_v2: failed status %d\n",ret); |
| 576 | break; |
| 577 | |
| 578 | default: |
| 579 | dprintf(CRITICAL,"decrypt_scm_v2: case default: failed status %d\n",ret); |
| 580 | break; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 581 | } |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | int scm_svc_version(uint32 * major, uint32 * minor) |
| 586 | { |
| 587 | feature_version_req feature_req; |
| 588 | feature_version_rsp feature_rsp; |
| 589 | int ret = 0; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 590 | scmcall_arg scm_arg = {0}; |
| 591 | scmcall_ret scm_ret = {0}; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 592 | |
| 593 | feature_req.feature_id = TZBSP_FVER_SSD; |
| 594 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 595 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 596 | { |
| 597 | ret = scm_call(TZBSP_SVC_INFO, |
| 598 | TZ_INFO_GET_FEATURE_ID, |
| 599 | &feature_req, |
| 600 | sizeof(feature_req), |
| 601 | &feature_rsp, |
| 602 | sizeof(feature_rsp)); |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | scm_arg.x0 = MAKE_SIP_SCM_CMD(TZBSP_SVC_INFO, TZ_INFO_GET_FEATURE_ID); |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 607 | scm_arg.x1 = MAKE_SCM_ARGS(0x1,SMC_PARAM_TYPE_VALUE); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 608 | scm_arg.x2 = feature_req.feature_id; |
| 609 | |
| 610 | ret = scm_call2(&scm_arg, &scm_ret); |
| 611 | feature_rsp.version = scm_ret.x1; |
| 612 | } |
| 613 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 614 | if(!ret) |
| 615 | *major = TZBSP_GET_FEATURE_VERSION(feature_rsp.version); |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
Amit Blay | bdfabc6 | 2015-01-29 22:04:13 +0200 | [diff] [blame] | 620 | int scm_svc_get_secure_state(uint32_t *state_low, uint32_t *state_high) |
| 621 | { |
| 622 | get_secure_state_req req; |
| 623 | get_secure_state_rsp rsp; |
| 624 | |
| 625 | int ret = 0; |
| 626 | |
| 627 | scmcall_arg scm_arg = {0}; |
| 628 | scmcall_ret scm_ret = {0}; |
| 629 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 630 | if (!is_scm_armv8_support()) |
Amit Blay | bdfabc6 | 2015-01-29 22:04:13 +0200 | [diff] [blame] | 631 | { |
| 632 | req.status_ptr = (uint32_t*)&rsp; |
| 633 | req.status_len = sizeof(rsp); |
| 634 | |
| 635 | ret = scm_call(TZBSP_SVC_INFO, |
| 636 | TZ_INFO_GET_SECURE_STATE, |
| 637 | &req, |
| 638 | sizeof(req), |
| 639 | NULL, |
| 640 | 0); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | scm_arg.x0 = MAKE_SIP_SCM_CMD(TZBSP_SVC_INFO, TZ_INFO_GET_SECURE_STATE); |
| 645 | scm_arg.x1 = MAKE_SCM_ARGS(0x0); |
| 646 | |
| 647 | ret = scm_call2(&scm_arg, &scm_ret); |
| 648 | |
| 649 | rsp.status_low = scm_ret.x1; |
| 650 | rsp.status_high = scm_ret.x2; |
| 651 | } |
| 652 | |
| 653 | if(!ret) |
| 654 | { |
| 655 | *state_low = rsp.status_low; |
| 656 | *state_high = rsp.status_high; |
| 657 | } |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 662 | int scm_protect_keystore(uint32_t * img_ptr, uint32_t img_len) |
| 663 | { |
| 664 | int ret=0; |
| 665 | ssd_protect_keystore_req protect_req; |
| 666 | ssd_protect_keystore_rsp protect_rsp; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 667 | scmcall_arg scm_arg = {0}; |
| 668 | scmcall_ret scm_ret = {0}; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 669 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 670 | protect_req.keystore_ptr = img_ptr; |
| 671 | protect_req.keystore_len = img_len; |
| 672 | |
| 673 | arch_clean_invalidate_cache_range((addr_t) img_ptr, img_len); |
| 674 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 675 | if (!is_scm_armv8_support()) |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 676 | { |
| 677 | ret = scm_call(SCM_SVC_SSD, |
| 678 | SSD_PROTECT_KEYSTORE_ID, |
| 679 | &protect_req, |
| 680 | sizeof(protect_req), |
| 681 | &protect_rsp, |
| 682 | sizeof(protect_rsp)); |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_SSD, SSD_PROTECT_KEYSTORE_ID); |
| 687 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_BUFFER_READWRITE,SMC_PARAM_TYPE_VALUE); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 688 | scm_arg.x2 = (uint32_t) protect_req.keystore_ptr; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 689 | scm_arg.x3 = protect_req.keystore_len; |
| 690 | |
| 691 | ret = scm_call2(&scm_arg, &scm_ret); |
| 692 | protect_rsp.status = scm_ret.x1; |
| 693 | } |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 694 | if(!ret) |
| 695 | { |
| 696 | if(protect_rsp.status == TZBSP_SSD_PKS_SUCCESS) |
| 697 | dprintf(INFO,"Successfully loaded the keystore "); |
| 698 | else |
| 699 | { |
| 700 | dprintf(INFO,"Loading keystore failed status %d ",protect_rsp.status); |
| 701 | ret = protect_rsp.status; |
| 702 | } |
| 703 | } |
| 704 | else |
| 705 | dprintf(INFO,"scm_call failed "); |
| 706 | |
| 707 | return ret; |
| 708 | } |
| 709 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 710 | void set_tamper_fuse_cmd() |
| 711 | { |
| 712 | uint32_t svc_id; |
| 713 | uint32_t cmd_id; |
| 714 | void *cmd_buf; |
| 715 | size_t cmd_len; |
| 716 | void *resp_buf = NULL; |
| 717 | size_t resp_len = 0; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 718 | scmcall_arg scm_arg = {0}; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 719 | |
| 720 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
| 721 | cmd_buf = (void *)&fuse_id; |
| 722 | cmd_len = sizeof(fuse_id); |
| 723 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 724 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 725 | { |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 726 | /*no response */ |
| 727 | resp_buf = NULL; |
| 728 | resp_len = 0; |
| 729 | |
| 730 | svc_id = SCM_SVC_FUSE; |
| 731 | cmd_id = SCM_BLOW_SW_FUSE_ID; |
| 732 | |
| 733 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len); |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_FUSE, SCM_BLOW_SW_FUSE_ID); |
| 738 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_BUFFER_READWRITE,SMC_PARAM_TYPE_VALUE); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 739 | scm_arg.x2 = (uint32_t) cmd_buf; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 740 | scm_arg.x3 = cmd_len; |
| 741 | |
| 742 | scm_call2(&scm_arg, NULL); |
| 743 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | uint8_t get_tamper_fuse_cmd() |
| 749 | { |
| 750 | uint32_t svc_id; |
| 751 | uint32_t cmd_id; |
| 752 | void *cmd_buf; |
| 753 | size_t cmd_len; |
| 754 | size_t resp_len = 0; |
| 755 | uint8_t resp_buf; |
| 756 | |
| 757 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 758 | scmcall_arg scm_arg = {0}; |
| 759 | scmcall_ret scm_ret = {0}; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 760 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 761 | cmd_buf = (void *)&fuse_id; |
| 762 | cmd_len = sizeof(fuse_id); |
| 763 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 764 | if (!is_scm_armv8_support()) |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 765 | { |
| 766 | /*response */ |
| 767 | resp_len = sizeof(resp_buf); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 768 | |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 769 | svc_id = SCM_SVC_FUSE; |
| 770 | cmd_id = SCM_IS_SW_FUSE_BLOWN_ID; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 771 | |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 772 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 773 | return resp_buf; |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_FUSE, SCM_IS_SW_FUSE_BLOWN_ID); |
| 778 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_BUFFER_READWRITE,SMC_PARAM_TYPE_VALUE); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 779 | scm_arg.x2 = (uint32_t) cmd_buf; |
vijay kumar | e95092d | 2014-10-20 19:24:49 +0530 | [diff] [blame] | 780 | scm_arg.x3 = cmd_len; |
| 781 | |
| 782 | scm_call2(&scm_arg, &scm_ret); |
| 783 | return (uint8_t)scm_ret.x1; |
| 784 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 785 | } |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 786 | |
Amir Samuelov | 4620ad2 | 2013-03-13 11:30:05 +0200 | [diff] [blame] | 787 | /* |
| 788 | * struct qseecom_save_partition_hash_req |
| 789 | * @partition_id - partition id. |
| 790 | * @digest[SHA256_DIGEST_LENGTH] - sha256 digest. |
| 791 | */ |
| 792 | struct qseecom_save_partition_hash_req { |
| 793 | uint32_t partition_id; /* in */ |
| 794 | uint8_t digest[SHA256_DIGEST_LENGTH]; /* in */ |
| 795 | }; |
| 796 | |
| 797 | |
| 798 | void save_kernel_hash_cmd(void *digest) |
| 799 | { |
| 800 | uint32_t svc_id; |
| 801 | uint32_t cmd_id; |
| 802 | void *cmd_buf; |
| 803 | size_t cmd_len; |
| 804 | void *resp_buf = NULL; |
| 805 | size_t resp_len = 0; |
| 806 | struct qseecom_save_partition_hash_req req; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 807 | scmcall_arg scm_arg = {0}; |
Amir Samuelov | 4620ad2 | 2013-03-13 11:30:05 +0200 | [diff] [blame] | 808 | |
| 809 | /*no response */ |
| 810 | resp_buf = NULL; |
| 811 | resp_len = 0; |
| 812 | |
| 813 | req.partition_id = 0; /* kernel */ |
| 814 | memcpy(req.digest, digest, sizeof(req.digest)); |
| 815 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 816 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 817 | { |
| 818 | svc_id = SCM_SVC_ES; |
| 819 | cmd_id = SCM_SAVE_PARTITION_HASH_ID; |
| 820 | cmd_buf = (void *)&req; |
| 821 | cmd_len = sizeof(req); |
Amir Samuelov | 4620ad2 | 2013-03-13 11:30:05 +0200 | [diff] [blame] | 822 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 823 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len); |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_ES, SCM_SAVE_PARTITION_HASH_ID); |
| 828 | scm_arg.x1 = MAKE_SCM_ARGS(0x3, 0, SMC_PARAM_TYPE_BUFFER_READWRITE); |
| 829 | scm_arg.x2 = req.partition_id; |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 830 | scm_arg.x3 = (uint32_t) &req.digest; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 831 | scm_arg.x4 = sizeof(req.digest); |
| 832 | |
| 833 | if (scm_call2(&scm_arg, NULL)) |
| 834 | dprintf(CRITICAL, "Failed to Save kernel hash\n"); |
| 835 | } |
Amir Samuelov | 4620ad2 | 2013-03-13 11:30:05 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Amit Blay | fe23ee2 | 2015-01-09 19:09:51 +0200 | [diff] [blame] | 838 | int mdtp_cipher_dip_cmd(uint8_t *in_buf, uint32_t in_buf_size, uint8_t *out_buf, |
| 839 | uint32_t out_buf_size, uint32_t direction) |
| 840 | { |
| 841 | uint32_t svc_id; |
| 842 | uint32_t cmd_id; |
| 843 | void *cmd_buf; |
| 844 | void *rsp_buf; |
| 845 | size_t cmd_len; |
| 846 | size_t rsp_len; |
| 847 | mdtp_cipher_dip_req req; |
| 848 | scmcall_arg scm_arg = {0}; |
| 849 | scmcall_ret scm_ret = {0}; |
| 850 | |
| 851 | ASSERT(in_buf != NULL); |
| 852 | ASSERT(out_buf != NULL); |
| 853 | |
| 854 | req.in_buf = in_buf; |
| 855 | req.in_buf_size = in_buf_size; |
| 856 | req.out_buf = out_buf; |
| 857 | req.out_buf_size = out_buf_size; |
| 858 | req.direction = direction; |
| 859 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 860 | if (!is_scm_armv8_support()) |
Amit Blay | fe23ee2 | 2015-01-09 19:09:51 +0200 | [diff] [blame] | 861 | { |
| 862 | svc_id = SCM_SVC_MDTP; |
| 863 | cmd_id = SCM_MDTP_CIPHER_DIP; |
| 864 | cmd_buf = (void *)&req; |
| 865 | cmd_len = sizeof(req); |
| 866 | rsp_buf = NULL; |
| 867 | rsp_len = 0; |
| 868 | |
| 869 | if (scm_call(svc_id, cmd_id, cmd_buf, cmd_len, rsp_buf, rsp_len)) |
| 870 | { |
| 871 | dprintf(CRITICAL, "Failed to call Cipher DIP SCM\n"); |
| 872 | return -1; |
| 873 | } |
| 874 | } |
| 875 | else |
| 876 | { |
| 877 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_MDTP, SCM_MDTP_CIPHER_DIP); |
| 878 | scm_arg.x1 = MAKE_SCM_ARGS(0x5, SMC_PARAM_TYPE_BUFFER_READ, SMC_PARAM_TYPE_VALUE, |
| 879 | SMC_PARAM_TYPE_BUFFER_READWRITE, SMC_PARAM_TYPE_VALUE, SMC_PARAM_TYPE_VALUE); |
| 880 | scm_arg.x2 = (uint32_t)req.in_buf; |
| 881 | scm_arg.x3 = req.in_buf_size; |
| 882 | scm_arg.x4 = (uint32_t)req.out_buf; |
| 883 | scm_arg.x5[0] = req.out_buf_size; |
| 884 | scm_arg.x5[1] = req.direction; |
| 885 | |
| 886 | if (scm_call2(&scm_arg, &scm_ret)) |
| 887 | { |
| 888 | dprintf(CRITICAL, "Failed in Cipher DIP SCM call\n"); |
| 889 | return -1; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
Amit Blay | bdfabc6 | 2015-01-29 22:04:13 +0200 | [diff] [blame] | 896 | int qfprom_read_row_cmd(uint32_t row_address, |
| 897 | uint32_t addr_type, |
| 898 | uint32_t *row_data, |
| 899 | uint32_t *qfprom_api_status) |
| 900 | { |
| 901 | uint32_t svc_id; |
| 902 | uint32_t cmd_id; |
| 903 | void *cmd_buf; |
| 904 | void *rsp_buf; |
| 905 | size_t cmd_len; |
| 906 | size_t rsp_len; |
| 907 | qfprom_read_row_req req; |
| 908 | scmcall_arg scm_arg = {0}; |
| 909 | scmcall_ret scm_ret = {0}; |
| 910 | |
| 911 | req.row_address = row_address; |
| 912 | req.addr_type = addr_type; |
| 913 | req.row_data = row_data; |
| 914 | req.qfprom_api_status = qfprom_api_status; |
| 915 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 916 | if (!is_scm_armv8_support()) |
Amit Blay | bdfabc6 | 2015-01-29 22:04:13 +0200 | [diff] [blame] | 917 | { |
| 918 | svc_id = SCM_SVC_FUSE; |
| 919 | cmd_id = SCM_QFPROM_READ_ROW_ID; |
| 920 | cmd_buf = (void *)&req; |
| 921 | cmd_len = sizeof(req); |
| 922 | rsp_buf = NULL; |
| 923 | rsp_len = 0; |
| 924 | |
| 925 | if (scm_call(svc_id, cmd_id, cmd_buf, cmd_len, rsp_buf, rsp_len)) |
| 926 | { |
| 927 | dprintf(CRITICAL, "Failed to call SCM_SVC_FUSE.SCM_QFPROM_READ_ROW_ID SCM\n"); |
| 928 | return -1; |
| 929 | } |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_FUSE, SCM_QFPROM_READ_ROW_ID); |
| 934 | scm_arg.x1 = MAKE_SCM_ARGS(0x4, SMC_PARAM_TYPE_VALUE, SMC_PARAM_TYPE_VALUE, |
| 935 | SMC_PARAM_TYPE_BUFFER_READWRITE, SMC_PARAM_TYPE_BUFFER_READWRITE); |
| 936 | scm_arg.x2 = req.row_address; |
| 937 | scm_arg.x3 = req.addr_type; |
| 938 | scm_arg.x4 = (uint32_t)req.row_data; |
| 939 | scm_arg.x5[0] = (uint32_t)req.qfprom_api_status; |
| 940 | |
| 941 | if (scm_call2(&scm_arg, &scm_ret)) |
| 942 | { |
| 943 | dprintf(CRITICAL, "Failed to call SCM_SVC_FUSE.SCM_QFPROM_READ_ROW_ID SCM\n"); |
| 944 | return -1; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 951 | /* |
| 952 | * Switches the CE1 channel between ADM and register usage. |
| 953 | * channel : AP_CE_REGISTER_USE, CE1 uses register interface |
| 954 | * : AP_CE_ADM_USE, CE1 uses ADM interface |
| 955 | */ |
| 956 | uint8_t switch_ce_chn_cmd(enum ap_ce_channel_type channel) |
| 957 | { |
| 958 | uint32_t svc_id; |
| 959 | uint32_t cmd_id; |
| 960 | void *cmd_buf; |
| 961 | size_t cmd_len; |
| 962 | size_t resp_len = 0; |
| 963 | uint8_t resp_buf; |
| 964 | |
| 965 | struct { |
| 966 | uint32_t resource; |
| 967 | uint32_t chn_id; |
| 968 | }__PACKED switch_ce_chn_buf; |
| 969 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 970 | if (is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 971 | { |
| 972 | dprintf(INFO, "%s:SCM call is not supported\n",__func__); |
| 973 | return 0; |
| 974 | } |
| 975 | |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 976 | switch_ce_chn_buf.resource = TZ_RESOURCE_CE_AP; |
| 977 | switch_ce_chn_buf.chn_id = channel; |
| 978 | cmd_buf = (void *)&switch_ce_chn_buf; |
| 979 | cmd_len = sizeof(switch_ce_chn_buf); |
| 980 | |
| 981 | /*response */ |
| 982 | resp_len = sizeof(resp_buf); |
| 983 | |
| 984 | svc_id = SCM_SVC_CE_CHN_SWITCH_ID; |
| 985 | cmd_id = SCM_CE_CHN_SWITCH_ID; |
| 986 | |
| 987 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 988 | return resp_buf; |
| 989 | } |
| 990 | |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 991 | int scm_halt_pmic_arbiter() |
| 992 | { |
| 993 | int ret = 0; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 994 | scmcall_arg scm_arg = {0}; |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 995 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 996 | if (is_scm_armv8_support()) { |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 997 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER); |
| 998 | scm_arg.x1 = MAKE_SCM_ARGS(0x1); |
| 999 | scm_arg.x2 = 0; |
| 1000 | scm_arg.atomic = true; |
| 1001 | ret = scm_call2(&scm_arg, NULL); |
| 1002 | } else { |
| 1003 | ret = scm_call_atomic(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER, 0); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1006 | /* Retry with the SCM_IO_DISABLE_PMIC_ARBITER1 func ID if the above Func ID fails*/ |
| 1007 | if(ret) { |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1008 | if (is_scm_armv8_support()) { |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1009 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER1); |
| 1010 | scm_arg.x1 = MAKE_SCM_ARGS(0x1); |
| 1011 | scm_arg.x2 = 0; |
| 1012 | scm_arg.atomic = true; |
| 1013 | ret = scm_call2(&scm_arg, NULL); |
| 1014 | } else |
| 1015 | ret = scm_call_atomic(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER1, 0); |
| 1016 | } |
Channagoud Kadabi | 179df0b | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 1017 | |
| 1018 | return ret; |
| 1019 | } |
Abhimanyu Kapur | b3207fb | 2014-01-27 21:33:23 -0800 | [diff] [blame] | 1020 | |
| 1021 | /* Execption Level exec secure-os call |
| 1022 | * Jumps to kernel via secure-os and does not return |
| 1023 | * on successful jump. System parameters are setup & |
| 1024 | * passed on to secure-os and are utilized to boot the |
| 1025 | * kernel. |
| 1026 | * |
| 1027 | @ kernel_entry : kernel entry point passed in as link register. |
| 1028 | @ dtb_offset : dt blob address passed in as w0. |
| 1029 | @ svc_id : indicates direction of switch 32->64 or 64->32 |
| 1030 | * |
| 1031 | * Assumes all sanity checks have been performed on arguments. |
| 1032 | */ |
| 1033 | |
| 1034 | void scm_elexec_call(paddr_t kernel_entry, paddr_t dtb_offset) |
| 1035 | { |
| 1036 | uint32_t svc_id = SCM_SVC_MILESTONE_32_64_ID; |
| 1037 | uint32_t cmd_id = SCM_SVC_MILESTONE_CMD_ID; |
| 1038 | void *cmd_buf; |
| 1039 | size_t cmd_len; |
Sridhar Parasuram | fc6ea71 | 2015-06-30 11:22:49 -0700 | [diff] [blame] | 1040 | static el1_system_param param __attribute__((aligned(0x1000))); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1041 | scmcall_arg scm_arg = {0}; |
Abhimanyu Kapur | b3207fb | 2014-01-27 21:33:23 -0800 | [diff] [blame] | 1042 | |
| 1043 | param.el1_x0 = dtb_offset; |
| 1044 | param.el1_elr = kernel_entry; |
| 1045 | |
Abhimanyu Kapur | b3207fb | 2014-01-27 21:33:23 -0800 | [diff] [blame] | 1046 | /* Response Buffer = Null as no response expected */ |
| 1047 | dprintf(INFO, "Jumping to kernel via monitor\n"); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1048 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1049 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1050 | { |
| 1051 | /* Command Buffer */ |
| 1052 | cmd_buf = (void *)¶m; |
| 1053 | cmd_len = sizeof(el1_system_param); |
| 1054 | |
| 1055 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, NULL, 0); |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_MILESTONE_32_64_ID, SCM_SVC_MILESTONE_CMD_ID); |
| 1060 | scm_arg.x1 = MAKE_SCM_ARGS(0x2, SMC_PARAM_TYPE_BUFFER_READ); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 1061 | scm_arg.x2 = (uint32_t ) ¶m; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1062 | scm_arg.x3 = sizeof(el1_system_param); |
| 1063 | |
| 1064 | scm_call2(&scm_arg, NULL); |
| 1065 | } |
Abhimanyu Kapur | b3207fb | 2014-01-27 21:33:23 -0800 | [diff] [blame] | 1066 | |
| 1067 | /* Assert if execution ever reaches here */ |
| 1068 | dprintf(CRITICAL, "Failed to jump to kernel\n"); |
| 1069 | ASSERT(0); |
| 1070 | } |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1071 | |
| 1072 | /* SCM Random Command */ |
| 1073 | int scm_random(uint32_t * rbuf, uint32_t r_len) |
| 1074 | { |
| 1075 | int ret; |
| 1076 | struct tz_prng_data data; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1077 | scmcall_arg scm_arg = {0}; |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1078 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1079 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1080 | { |
| 1081 | data.out_buf = (uint8_t*) rbuf; |
| 1082 | data.out_buf_size = r_len; |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1083 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1084 | /* |
| 1085 | * random buffer must be flushed/invalidated before and after TZ call. |
| 1086 | */ |
| 1087 | arch_clean_invalidate_cache_range((addr_t) rbuf, r_len); |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1088 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1089 | ret = scm_call(TZ_SVC_CRYPTO, PRNG_CMD_ID, &data, sizeof(data), NULL, 0); |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1090 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1091 | /* Invalidate the updated random buffer */ |
| 1092 | arch_clean_invalidate_cache_range((addr_t) rbuf, r_len); |
| 1093 | } |
| 1094 | else |
| 1095 | { |
| 1096 | scm_arg.x0 = MAKE_SIP_SCM_CMD(TZ_SVC_CRYPTO, PRNG_CMD_ID); |
| 1097 | scm_arg.x1 = MAKE_SCM_ARGS(0x2,SMC_PARAM_TYPE_BUFFER_READWRITE); |
Veera Sundaram Sankaran | 0018151 | 2014-12-09 11:23:39 -0800 | [diff] [blame] | 1098 | scm_arg.x2 = (uint32_t) rbuf; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1099 | scm_arg.x3 = r_len; |
| 1100 | |
| 1101 | ret = scm_call2(&scm_arg, NULL); |
| 1102 | if (!ret) |
| 1103 | arch_clean_invalidate_cache_range((addr_t) rbuf, r_len); |
| 1104 | else |
| 1105 | dprintf(CRITICAL, "Secure canary SCM failed: %x\n", ret); |
| 1106 | } |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1107 | |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | void * get_canary() |
| 1112 | { |
| 1113 | void * canary; |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 1114 | if(scm_random((uint32_t *)&canary, sizeof(canary))) { |
Maria Yu | beeeeaf | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 1115 | dprintf(CRITICAL,"scm_call for random failed !!!"); |
| 1116 | /* |
| 1117 | * fall back to use lib rand API if scm call failed. |
| 1118 | */ |
| 1119 | canary = (void *)rand(); |
| 1120 | } |
| 1121 | |
| 1122 | return canary; |
| 1123 | } |
Aparna Mallavarapu | 6875ade | 2014-06-16 22:15:28 +0530 | [diff] [blame] | 1124 | |
| 1125 | int scm_xpu_err_fatal_init() |
| 1126 | { |
| 1127 | uint32_t ret = 0; |
| 1128 | uint32_t response = 0; |
| 1129 | tz_xpu_prot_cmd cmd; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1130 | scmcall_arg scm_arg = {0}; |
| 1131 | scmcall_ret scm_ret = {0}; |
Aparna Mallavarapu | 6875ade | 2014-06-16 22:15:28 +0530 | [diff] [blame] | 1132 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1133 | if (!is_scm_armv8_support()) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1134 | { |
| 1135 | cmd.config = ERR_FATAL_ENABLE; |
| 1136 | cmd.spare = 0; |
Aparna Mallavarapu | 6875ade | 2014-06-16 22:15:28 +0530 | [diff] [blame] | 1137 | |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1138 | ret = scm_call(SVC_MEMORY_PROTECTION, XPU_ERR_FATAL, &cmd, sizeof(cmd), &response, |
| 1139 | sizeof(response)); |
| 1140 | } |
| 1141 | else |
| 1142 | { |
| 1143 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SVC_MEMORY_PROTECTION, XPU_ERR_FATAL); |
| 1144 | scm_arg.x1 = MAKE_SCM_ARGS(0x2); |
| 1145 | scm_arg.x2 = ERR_FATAL_ENABLE; |
| 1146 | scm_arg.x3 = 0x0; |
| 1147 | ret = scm_call2(&scm_arg, &scm_ret); |
| 1148 | response = scm_ret.x1; |
| 1149 | } |
Aparna Mallavarapu | 6875ade | 2014-06-16 22:15:28 +0530 | [diff] [blame] | 1150 | |
| 1151 | if (ret) |
| 1152 | dprintf(CRITICAL, "Failed to set XPU violations as fatal errors: %u\n", response); |
| 1153 | else |
| 1154 | dprintf(INFO, "Configured XPU violations to be fatal errors\n"); |
| 1155 | |
| 1156 | return ret; |
| 1157 | } |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1158 | |
| 1159 | static uint32_t scm_call_a32(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, scmcall_ret *ret) |
| 1160 | { |
| 1161 | register uint32_t r0 __asm__("r0") = x0; |
| 1162 | register uint32_t r1 __asm__("r1") = x1; |
| 1163 | register uint32_t r2 __asm__("r2") = x2; |
| 1164 | register uint32_t r3 __asm__("r3") = x3; |
| 1165 | register uint32_t r4 __asm__("r4") = x4; |
| 1166 | register uint32_t r5 __asm__("r5") = x5; |
Channagoud Kadabi | e75efb9 | 2015-05-19 14:20:05 -0700 | [diff] [blame] | 1167 | register uint32_t r6 __asm__("r6") = 0; |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1168 | |
Dinesh K Garg | 6bbbb70 | 2015-01-30 11:13:31 -0800 | [diff] [blame] | 1169 | do { |
| 1170 | __asm__ volatile( |
| 1171 | __asmeq("%0", "r0") |
| 1172 | __asmeq("%1", "r1") |
| 1173 | __asmeq("%2", "r2") |
| 1174 | __asmeq("%3", "r3") |
| 1175 | __asmeq("%4", "r0") |
| 1176 | __asmeq("%5", "r1") |
| 1177 | __asmeq("%6", "r2") |
| 1178 | __asmeq("%7", "r3") |
| 1179 | __asmeq("%8", "r4") |
| 1180 | __asmeq("%9", "r5") |
Channagoud Kadabi | e75efb9 | 2015-05-19 14:20:05 -0700 | [diff] [blame] | 1181 | __asmeq("%10", "r6") |
Dinesh K Garg | 6bbbb70 | 2015-01-30 11:13:31 -0800 | [diff] [blame] | 1182 | "smc #0 @ switch to secure world\n" |
| 1183 | : "=r" (r0), "=r" (r1), "=r" (r2), "=r" (r3) |
Channagoud Kadabi | e75efb9 | 2015-05-19 14:20:05 -0700 | [diff] [blame] | 1184 | : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5), "r" (r6)); |
Dinesh K Garg | 6bbbb70 | 2015-01-30 11:13:31 -0800 | [diff] [blame] | 1185 | } while(r0 == 1); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1186 | |
| 1187 | if (ret) |
| 1188 | { |
| 1189 | ret->x1 = r1; |
| 1190 | ret->x2 = r2; |
| 1191 | ret->x3 = r3; |
| 1192 | } |
| 1193 | |
| 1194 | return r0; |
| 1195 | } |
| 1196 | |
| 1197 | uint32_t scm_call2(scmcall_arg *arg, scmcall_ret *ret) |
| 1198 | { |
| 1199 | uint32_t *indir_arg = NULL; |
| 1200 | uint32_t x5; |
| 1201 | int i; |
| 1202 | uint32_t rc; |
| 1203 | |
| 1204 | arg->x0 = arg->atomic ? (arg->x0 | SCM_ATOMIC_BIT) : arg->x0; |
| 1205 | x5 = arg->x5[0]; |
| 1206 | |
Amit Blay | fe23ee2 | 2015-01-09 19:09:51 +0200 | [diff] [blame] | 1207 | if ((arg->x1 & 0xF) > SCM_MAX_ARG_LEN - 1) |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1208 | { |
| 1209 | indir_arg = memalign(CACHE_LINE, (SCM_INDIR_MAX_LEN * sizeof(uint32_t))); |
| 1210 | ASSERT(indir_arg); |
| 1211 | |
| 1212 | for (i = 0 ; i < SCM_INDIR_MAX_LEN; i++) |
| 1213 | { |
| 1214 | indir_arg[i] = arg->x5[i]; |
| 1215 | } |
Channagoud Kadabi | cf3afe4 | 2015-08-07 16:08:08 -0700 | [diff] [blame] | 1216 | arch_clean_invalidate_cache_range((addr_t) indir_arg, (SCM_INDIR_MAX_LEN * sizeof(uint32_t))); |
Channagoud Kadabi | dd85e7f | 2014-08-05 19:58:37 -0700 | [diff] [blame] | 1217 | x5 = (addr_t) indir_arg; |
| 1218 | } |
| 1219 | |
| 1220 | rc = scm_call_a32(arg->x0, arg->x1, arg->x2, arg->x3, arg->x4, x5, ret); |
| 1221 | |
| 1222 | if (rc) |
| 1223 | { |
| 1224 | dprintf(CRITICAL, "SCM call: 0x%x failed with :%x\n", arg->x0, rc); |
| 1225 | return rc; |
| 1226 | } |
| 1227 | |
| 1228 | if (indir_arg) |
| 1229 | free(indir_arg); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1233 | |
Aparna Mallavarapu | 8adbee2 | 2015-03-10 19:58:06 +0530 | [diff] [blame] | 1234 | static bool secure_boot_enabled = true; |
| 1235 | static bool wdog_debug_fuse_disabled = true; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1236 | |
| 1237 | void scm_check_boot_fuses() |
| 1238 | { |
| 1239 | uint32_t ret = 0; |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1240 | uint32_t resp; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1241 | scmcall_arg scm_arg = {0}; |
| 1242 | scmcall_ret scm_ret = {0}; |
| 1243 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1244 | if (!is_scm_armv8_support()) { |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1245 | ret = scm_call(TZBSP_SVC_INFO, IS_SECURE_BOOT_ENABLED, NULL, 0, &resp, sizeof(resp)); |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1246 | } else { |
| 1247 | scm_arg.x0 = MAKE_SIP_SCM_CMD(TZBSP_SVC_INFO, IS_SECURE_BOOT_ENABLED); |
| 1248 | ret = scm_call2(&scm_arg, &scm_ret); |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1249 | resp = scm_ret.x1; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | /* Parse Bit 0 and Bit 2 of the response */ |
| 1253 | if(!ret) { |
| 1254 | /* Bit 0 - SECBOOT_ENABLE_CHECK */ |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1255 | if(resp & 0x1) |
Aparna Mallavarapu | 8adbee2 | 2015-03-10 19:58:06 +0530 | [diff] [blame] | 1256 | secure_boot_enabled = false; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1257 | /* Bit 2 - DEBUG_DISABLE_CHECK */ |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1258 | if(resp & 0x4) |
Aparna Mallavarapu | 8adbee2 | 2015-03-10 19:58:06 +0530 | [diff] [blame] | 1259 | wdog_debug_fuse_disabled = false; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1260 | } else |
| 1261 | dprintf(CRITICAL, "scm call to check secure boot fuses failed\n"); |
| 1262 | } |
| 1263 | |
| 1264 | bool is_secure_boot_enable() |
| 1265 | { |
| 1266 | scm_check_boot_fuses(); |
Aparna Mallavarapu | 8adbee2 | 2015-03-10 19:58:06 +0530 | [diff] [blame] | 1267 | return secure_boot_enabled; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static uint32_t scm_io_read(addr_t address) |
| 1271 | { |
| 1272 | uint32_t ret; |
| 1273 | scmcall_arg scm_arg = {0}; |
| 1274 | scmcall_ret scm_ret = {0}; |
| 1275 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1276 | if (!is_scm_armv8_support()) { |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1277 | ret = scm_call_atomic(SCM_SVC_IO, SCM_IO_READ, address); |
| 1278 | } else { |
| 1279 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_IO, SCM_IO_READ); |
| 1280 | scm_arg.x1 = MAKE_SCM_ARGS(0x1); |
| 1281 | scm_arg.x2 = address; |
| 1282 | scm_arg.atomic = true; |
| 1283 | ret = scm_call2(&scm_arg, &scm_ret); |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1284 | /* Return the value read if the call is successful */ |
| 1285 | if (!ret) |
| 1286 | ret = scm_ret.x1; |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1287 | } |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Aparna Mallavarapu | da91ea9 | 2015-07-10 12:03:46 +0530 | [diff] [blame] | 1291 | uint32_t scm_io_write(uint32_t address, uint32_t val) |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1292 | { |
| 1293 | uint32_t ret; |
| 1294 | scmcall_arg scm_arg = {0}; |
| 1295 | scmcall_ret scm_ret = {0}; |
| 1296 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1297 | if (!is_scm_armv8_support()) { |
Aparna Mallavarapu | 246c30b | 2014-12-11 12:07:51 +0530 | [diff] [blame] | 1298 | ret = scm_call_atomic2(SCM_SVC_IO, SCM_IO_WRITE, address, val); |
| 1299 | } else { |
| 1300 | scm_arg.x0 = MAKE_SIP_SCM_CMD(SCM_SVC_IO, SCM_IO_WRITE); |
| 1301 | scm_arg.x1 = MAKE_SCM_ARGS(0x2); |
| 1302 | scm_arg.x2 = address; |
| 1303 | scm_arg.x3 = val; |
| 1304 | scm_arg.atomic = true; |
| 1305 | ret = scm_call2(&scm_arg, &scm_ret); |
| 1306 | } |
| 1307 | return ret; |
| 1308 | } |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1309 | |
Aparna Mallavarapu | da91ea9 | 2015-07-10 12:03:46 +0530 | [diff] [blame] | 1310 | int scm_call2_atomic(uint32_t svc, uint32_t cmd, uint32_t arg1, uint32_t arg2) |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1311 | { |
| 1312 | uint32_t ret = 0; |
| 1313 | scmcall_arg scm_arg = {0}; |
| 1314 | scmcall_ret scm_ret = {0}; |
| 1315 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1316 | if (!is_scm_armv8_support()) |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1317 | { |
| 1318 | ret = scm_call_atomic2(svc, cmd, arg1, arg2); |
| 1319 | } else { |
| 1320 | scm_arg.x0 = MAKE_SIP_SCM_CMD(svc, cmd); |
| 1321 | scm_arg.x1 = MAKE_SCM_ARGS(0x2); |
| 1322 | scm_arg.x2 = arg1; |
| 1323 | scm_arg.x3 = arg2; |
| 1324 | ret = scm_call2(&scm_arg, &scm_ret); |
| 1325 | } |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | #if PLATFORM_USE_SCM_DLOAD |
| 1330 | int scm_dload_mode(int mode) |
| 1331 | { |
| 1332 | int ret = 0; |
| 1333 | uint32_t dload_type; |
| 1334 | |
| 1335 | dprintf(SPEW, "DLOAD mode: %d\n", mode); |
| 1336 | if (mode == NORMAL_DLOAD) |
| 1337 | dload_type = SCM_DLOAD_MODE; |
| 1338 | else if(mode == EMERGENCY_DLOAD) |
| 1339 | dload_type = SCM_EDLOAD_MODE; |
| 1340 | else |
| 1341 | dload_type = 0; |
| 1342 | |
| 1343 | /* Write to the Boot MISC register */ |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 1344 | ret = is_scm_call_available(SCM_SVC_BOOT, SCM_DLOAD_CMD); |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1345 | |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 1346 | if (ret > 0) |
| 1347 | ret = scm_call2_atomic(SCM_SVC_BOOT, SCM_DLOAD_CMD, dload_type, 0); |
| 1348 | else |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1349 | ret = scm_io_write(TCSR_BOOT_MISC_DETECT,dload_type); |
Channagoud Kadabi | a2184b8 | 2015-07-07 10:09:32 -0700 | [diff] [blame] | 1350 | |
| 1351 | if(ret) { |
| 1352 | dprintf(CRITICAL, "Failed to write to boot misc: %d\n", ret); |
| 1353 | return ret; |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | scm_check_boot_fuses(); |
| 1357 | |
| 1358 | /* Make WDOG_DEBUG DISABLE scm call only in non-secure boot */ |
Aparna Mallavarapu | 8adbee2 | 2015-03-10 19:58:06 +0530 | [diff] [blame] | 1359 | if(!(secure_boot_enabled || wdog_debug_fuse_disabled)) { |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1360 | ret = scm_call2_atomic(SCM_SVC_BOOT, WDOG_DEBUG_DISABLE, 1, 0); |
| 1361 | if(ret) |
| 1362 | dprintf(CRITICAL, "Failed to disable the wdog debug \n"); |
| 1363 | } |
| 1364 | |
| 1365 | return ret; |
| 1366 | } |
Channagoud Kadabi | 6479ce3 | 2015-06-17 17:30:40 -0700 | [diff] [blame] | 1367 | |
| 1368 | bool scm_device_enter_dload() |
| 1369 | { |
| 1370 | uint32_t ret = 0; |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1371 | uint32_t dload_mode = 0; |
Channagoud Kadabi | 6479ce3 | 2015-06-17 17:30:40 -0700 | [diff] [blame] | 1372 | |
| 1373 | scmcall_arg scm_arg = {0}; |
| 1374 | scmcall_ret scm_ret = {0}; |
| 1375 | |
| 1376 | scm_arg.x0 = MAKE_SIP_SCM_CMD(TZ_SVC_DLOAD_MODE, SCM_DLOAD_CMD); |
| 1377 | ret = scm_call2(&scm_arg, &scm_ret); |
| 1378 | if (ret) |
| 1379 | dprintf(CRITICAL, "SCM call to check dload mode failed: %x\n", ret); |
| 1380 | |
Channagoud Kadabi | 36cdfb6 | 2015-10-23 16:53:37 -0700 | [diff] [blame^] | 1381 | if (!ret) |
| 1382 | { |
| 1383 | dload_mode = scm_io_read(TCSR_BOOT_MISC_DETECT); |
| 1384 | if (board_soc_version() < 0x30000) |
| 1385 | dload_mode = (dload_mode >> 16) & 0xFFFF; |
| 1386 | } |
| 1387 | |
| 1388 | if (dload_mode == SCM_DLOAD_MODE) |
Channagoud Kadabi | 6479ce3 | 2015-06-17 17:30:40 -0700 | [diff] [blame] | 1389 | return true; |
| 1390 | |
| 1391 | return false; |
| 1392 | } |
Aparna Mallavarapu | 664ea77 | 2015-02-24 18:44:33 +0530 | [diff] [blame] | 1393 | #endif |