Pavel Nedev | 7109852 | 2014-01-06 14:26:17 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014, 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 | 7037504 | 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> |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 35 | #include "scm.h" |
| 36 | |
| 37 | #pragma GCC optimize ("O0") |
| 38 | |
| 39 | /* From Linux Kernel asm/system.h */ |
| 40 | #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t" |
| 41 | |
| 42 | #ifndef offsetof |
| 43 | # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 44 | #endif |
| 45 | |
Channagoud Kadabi | 7037504 | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 46 | #define SCM_CLASS_REGISTER (0x2 << 8) |
| 47 | #define SCM_MASK_IRQS BIT(5) |
| 48 | #define SCM_ATOMIC(svc, cmd, n) ((((((svc) & 0x3f) << 10)|((cmd) & 0x3ff)) << 12) | \ |
| 49 | SCM_CLASS_REGISTER | \ |
| 50 | SCM_MASK_IRQS | \ |
| 51 | ((n) & 0xf)) |
| 52 | |
| 53 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 54 | /** |
| 55 | * alloc_scm_command() - Allocate an SCM command |
| 56 | * @cmd_size: size of the command buffer |
| 57 | * @resp_size: size of the response buffer |
| 58 | * |
| 59 | * Allocate an SCM command, including enough room for the command |
| 60 | * and response headers as well as the command and response buffers. |
| 61 | * |
| 62 | * Returns a valid &scm_command on success or %NULL if the allocation fails. |
| 63 | */ |
| 64 | static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size) |
| 65 | { |
| 66 | struct scm_command *cmd; |
| 67 | size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 68 | resp_size; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 69 | |
Deepa Dinamani | 904f8f8 | 2012-12-05 16:35:01 -0800 | [diff] [blame] | 70 | cmd = memalign(CACHE_LINE, ROUNDUP(len, CACHE_LINE)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 71 | if (cmd) { |
Pavel Nedev | 7109852 | 2014-01-06 14:26:17 +0200 | [diff] [blame] | 72 | memset(cmd, 0, len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 73 | cmd->len = len; |
| 74 | cmd->buf_offset = offsetof(struct scm_command, buf); |
| 75 | cmd->resp_hdr_offset = cmd->buf_offset + cmd_size; |
| 76 | } |
| 77 | return cmd; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * free_scm_command() - Free an SCM command |
| 82 | * @cmd: command to free |
| 83 | * |
| 84 | * Free an SCM command. |
| 85 | */ |
| 86 | static inline void free_scm_command(struct scm_command *cmd) |
| 87 | { |
| 88 | free(cmd); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * scm_command_to_response() - Get a pointer to a scm_response |
| 93 | * @cmd: command |
| 94 | * |
| 95 | * Returns a pointer to a response for a command. |
| 96 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 97 | static inline struct scm_response *scm_command_to_response(const struct |
| 98 | scm_command *cmd) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 99 | { |
| 100 | return (void *)cmd + cmd->resp_hdr_offset; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * scm_get_command_buffer() - Get a pointer to a command buffer |
| 105 | * @cmd: command |
| 106 | * |
| 107 | * Returns a pointer to the command buffer of a command. |
| 108 | */ |
| 109 | static inline void *scm_get_command_buffer(const struct scm_command *cmd) |
| 110 | { |
| 111 | return (void *)cmd->buf; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * scm_get_response_buffer() - Get a pointer to a response buffer |
| 116 | * @rsp: response |
| 117 | * |
| 118 | * Returns a pointer to a response buffer of a response. |
| 119 | */ |
| 120 | static inline void *scm_get_response_buffer(const struct scm_response *rsp) |
| 121 | { |
| 122 | return (void *)rsp + rsp->buf_offset; |
| 123 | } |
| 124 | |
| 125 | static uint32_t smc(uint32_t cmd_addr) |
| 126 | { |
| 127 | uint32_t context_id; |
| 128 | register uint32_t r0 __asm__("r0") = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 129 | register uint32_t r1 __asm__("r1") = (uint32_t) & context_id; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 130 | register uint32_t r2 __asm__("r2") = cmd_addr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 131 | __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] | 132 | return r0; |
| 133 | } |
| 134 | |
| 135 | /** |
Channagoud Kadabi | 7037504 | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 136 | * scm_call_automic: Make scm call with one or no argument |
| 137 | * @svc: service id |
| 138 | * @cmd: command id |
| 139 | * @ arg1: argument |
| 140 | */ |
| 141 | |
| 142 | static int scm_call_atomic(uint32_t svc, uint32_t cmd, uint32_t arg1) |
| 143 | { |
| 144 | uint32_t context_id; |
| 145 | register uint32_t r0 __asm__("r0") = SCM_ATOMIC(svc, cmd, 1); |
Sridhar Parasuram | d0443e2 | 2015-08-29 09:56:03 -0700 | [diff] [blame] | 146 | register uint32_t r1 __asm__("r1") = (uint32_t)&context_id; |
Channagoud Kadabi | 7037504 | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 147 | register uint32_t r2 __asm__("r2") = arg1; |
| 148 | |
| 149 | __asm__ volatile( |
| 150 | __asmeq("%0", "r0") |
| 151 | __asmeq("%1", "r0") |
| 152 | __asmeq("%2", "r1") |
| 153 | __asmeq("%3", "r2") |
| 154 | "smc #0 @ switch to secure world\n" |
| 155 | : "=r" (r0) |
| 156 | : "r" (r0), "r" (r1), "r" (r2) |
| 157 | : "r3"); |
| 158 | return r0; |
| 159 | } |
| 160 | |
| 161 | /** |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 162 | * scm_call() - Send an SCM command |
| 163 | * @svc_id: service identifier |
| 164 | * @cmd_id: command identifier |
| 165 | * @cmd_buf: command buffer |
| 166 | * @cmd_len: length of the command buffer |
| 167 | * @resp_buf: response buffer |
| 168 | * @resp_len: length of the response buffer |
| 169 | * |
| 170 | * Sends a command to the SCM and waits for the command to finish processing. |
| 171 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 172 | int |
| 173 | scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf, |
| 174 | size_t cmd_len, void *resp_buf, size_t resp_len) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 175 | { |
| 176 | int ret; |
| 177 | struct scm_command *cmd; |
| 178 | struct scm_response *rsp; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 179 | uint8_t *resp_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 180 | |
| 181 | cmd = alloc_scm_command(cmd_len, resp_len); |
| 182 | if (!cmd) |
| 183 | return ERR_NO_MEMORY; |
| 184 | |
| 185 | cmd->id = (svc_id << 10) | cmd_id; |
| 186 | if (cmd_buf) |
| 187 | memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len); |
| 188 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 189 | /* Flush command to main memory for TZ */ |
| 190 | arch_clean_invalidate_cache_range((addr_t) cmd, cmd->len); |
| 191 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 192 | ret = smc((uint32_t) cmd); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 193 | if (ret) |
| 194 | goto out; |
| 195 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 196 | if (resp_len) { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 197 | rsp = scm_command_to_response(cmd); |
| 198 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 199 | do |
| 200 | { |
| 201 | /* Need to invalidate before each check since TZ will update |
| 202 | * the response complete flag in main memory. |
| 203 | */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 204 | arch_invalidate_cache_range((addr_t) rsp, sizeof(*rsp)); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 205 | } while (!rsp->is_complete); |
| 206 | |
| 207 | |
| 208 | resp_ptr = scm_get_response_buffer(rsp); |
| 209 | |
| 210 | /* Invalidate any cached response data */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 211 | arch_invalidate_cache_range((addr_t) resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 212 | |
| 213 | if (resp_buf) |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 214 | memcpy(resp_buf, resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 215 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 216 | out: |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 217 | free_scm_command(cmd); |
| 218 | return ret; |
| 219 | } |
| 220 | |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 221 | int restore_secure_cfg(uint32_t id) |
| 222 | { |
| 223 | int ret, scm_ret = 0; |
| 224 | tz_secure_cfg secure_cfg; |
| 225 | |
Siddhartha Agrawal | d464889 | 2013-02-17 18:16:18 -0800 | [diff] [blame] | 226 | secure_cfg.id = id; |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 227 | secure_cfg.spare = 0; |
| 228 | |
sundarajan srinivasan | c2dee74 | 2013-02-21 11:31:36 -0800 | [diff] [blame] | 229 | ret = scm_call(SVC_MEMORY_PROTECTION, IOMMU_SECURE_CFG, &secure_cfg, sizeof(secure_cfg), |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 230 | &scm_ret, sizeof(scm_ret)); |
| 231 | |
| 232 | if (ret || scm_ret) { |
| 233 | dprintf(CRITICAL, "Secure Config failed\n"); |
| 234 | ret = 1; |
| 235 | } else |
| 236 | ret = 0; |
| 237 | |
| 238 | return ret; |
| 239 | |
| 240 | } |
| 241 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 242 | /* SCM Encrypt Command */ |
| 243 | int encrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 244 | { |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 245 | int ret; |
| 246 | img_req cmd; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 247 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 248 | cmd.img_ptr = (uint32*) img_ptr; |
| 249 | cmd.img_len_ptr = img_len_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 250 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 251 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 252 | * It must be flushed/invalidated before and after TZ call. |
| 253 | */ |
| 254 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 255 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 256 | ret = scm_call(SCM_SVC_SSD, SSD_ENCRYPT_ID, &cmd, sizeof(cmd), NULL, 0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 257 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 258 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 259 | * before we use them. |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 260 | */ |
| 261 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 262 | 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] | 263 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 264 | /* Invalidate the updated image data */ |
| 265 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 266 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 270 | /* SCM Decrypt Command */ |
| 271 | int decrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
| 272 | { |
| 273 | int ret; |
| 274 | img_req cmd; |
| 275 | |
| 276 | cmd.img_ptr = (uint32*) img_ptr; |
| 277 | cmd.img_len_ptr = img_len_ptr; |
| 278 | |
| 279 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 280 | * It must be flushed/invalidated before and after TZ call. |
| 281 | */ |
| 282 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 283 | |
| 284 | ret = scm_call(SCM_SVC_SSD, SSD_DECRYPT_ID, &cmd, sizeof(cmd), NULL, 0); |
| 285 | |
| 286 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 287 | * before we use them. |
| 288 | */ |
| 289 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
| 290 | arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr)); |
| 291 | |
| 292 | /* Invalidate the updated image data */ |
| 293 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 299 | static int ssd_image_is_encrypted(uint32_t ** img_ptr, uint32_t * img_len_ptr, uint32 * ctx_id) |
| 300 | { |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 301 | int ret = 0; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 302 | ssd_parse_md_req parse_req; |
| 303 | ssd_parse_md_rsp parse_rsp; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 304 | int prev_len = 0; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 305 | |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 306 | /* Populate meta-data ptr. Here md_len is the meta-data length. |
| 307 | * The Code below follows a growing length approach. First send |
| 308 | * min(img_len_ptr,SSD_HEADER_MIN_SIZE) say 128 bytes for example. |
| 309 | * If parse_rsp.status = PARSING_INCOMPLETE we send md_len = 256. |
| 310 | * If subsequent status = PARSING_INCOMPLETE we send md_len = 512, |
| 311 | * 1024bytes and so on until we get an valid response(rsp.status) from TZ*/ |
| 312 | |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 313 | parse_req.md = (uint32*)*img_ptr; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 314 | 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] | 315 | |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 316 | 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] | 317 | |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 318 | do |
| 319 | { |
| 320 | ret = scm_call(SCM_SVC_SSD, |
| 321 | SSD_PARSE_MD_ID, |
| 322 | &parse_req, |
| 323 | sizeof(parse_req), |
| 324 | &parse_rsp, |
| 325 | sizeof(parse_rsp)); |
| 326 | |
| 327 | if(!ret && (parse_rsp.status == SSD_PMD_PARSING_INCOMPLETE)) |
| 328 | { |
| 329 | prev_len = parse_req.md_len; |
| 330 | |
| 331 | parse_req.md_len *= MULTIPLICATION_FACTOR; |
| 332 | |
Venkatesh Yadav Abbarapu | 0a06078 | 2013-11-11 16:56:04 +0530 | [diff] [blame] | 333 | arch_clean_invalidate_cache_range((addr_t) (*img_ptr + prev_len), |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 334 | (parse_req.md_len - prev_len) ); |
| 335 | |
| 336 | continue; |
| 337 | } |
| 338 | else |
| 339 | break; |
| 340 | |
| 341 | } while(true); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 342 | |
| 343 | if(!ret) |
| 344 | { |
| 345 | if(parse_rsp.status == SSD_PMD_ENCRYPTED) |
| 346 | { |
| 347 | *ctx_id = parse_rsp.md_ctx_id; |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 348 | *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] | 349 | *img_ptr = (uint32_t*)parse_rsp.md_end_ptr; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 350 | } |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 351 | |
| 352 | ret = parse_rsp.status; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 353 | } |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 354 | else |
| 355 | { |
| 356 | dprintf(CRITICAL,"ssd_image_is_encrypted call failed"); |
| 357 | |
| 358 | ASSERT(ret == 0); |
| 359 | } |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | int decrypt_scm_v2(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | uint32 ctx_id = 0; |
| 368 | ssd_decrypt_img_frag_req decrypt_req; |
| 369 | ssd_decrypt_img_frag_rsp decrypt_rsp; |
| 370 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 371 | ret = ssd_image_is_encrypted(img_ptr,img_len_ptr,&ctx_id); |
| 372 | switch(ret) |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 373 | { |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 374 | case SSD_PMD_ENCRYPTED: |
| 375 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 376 | * It must be flushed/invalidated before and after TZ call. |
| 377 | */ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 378 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 379 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 380 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 381 | /*decrypt the image here*/ |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 382 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 383 | decrypt_req.md_ctx_id = ctx_id; |
| 384 | decrypt_req.last_frag = 1; |
| 385 | decrypt_req.frag_len = *img_len_ptr; |
| 386 | decrypt_req.frag = *img_ptr; |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 387 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 388 | ret = scm_call(SCM_SVC_SSD, |
| 389 | SSD_DECRYPT_IMG_FRAG_ID, |
| 390 | &decrypt_req, |
| 391 | sizeof(decrypt_req), |
| 392 | &decrypt_rsp, |
| 393 | sizeof(decrypt_rsp)); |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 394 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 395 | if(!ret){ |
| 396 | ret = decrypt_rsp.status; |
| 397 | } |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 398 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 399 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 400 | * before we use them. |
| 401 | */ |
| 402 | arch_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
| 403 | 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] | 404 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 405 | /* Invalidate the updated image data */ |
| 406 | arch_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 407 | |
Sundarajan Srinivasan | eb6d220 | 2013-06-04 14:24:10 -0700 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case SSD_PMD_NOT_ENCRYPTED: |
| 411 | case SSD_PMD_NO_MD_FOUND: |
| 412 | ret = 0; |
| 413 | break; |
| 414 | |
| 415 | case SSD_PMD_BUSY: |
| 416 | case SSD_PMD_BAD_MD_PTR_OR_LEN: |
| 417 | case SSD_PMD_PARSING_INCOMPLETE: |
| 418 | case SSD_PMD_PARSING_FAILED: |
| 419 | case SSD_PMD_SETUP_CIPHER_FAILED: |
| 420 | dprintf(CRITICAL,"decrypt_scm_v2: failed status %d\n",ret); |
| 421 | break; |
| 422 | |
| 423 | default: |
| 424 | dprintf(CRITICAL,"decrypt_scm_v2: case default: failed status %d\n",ret); |
| 425 | break; |
sundarajan srinivasan | 6173b87 | 2013-03-13 17:36:48 -0700 | [diff] [blame] | 426 | } |
sundarajan srinivasan | 4dfd4f7 | 2013-02-27 14:13:09 -0800 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | int scm_svc_version(uint32 * major, uint32 * minor) |
| 431 | { |
| 432 | feature_version_req feature_req; |
| 433 | feature_version_rsp feature_rsp; |
| 434 | int ret = 0; |
| 435 | |
| 436 | feature_req.feature_id = TZBSP_FVER_SSD; |
| 437 | |
| 438 | ret = scm_call(TZBSP_SVC_INFO, |
| 439 | TZ_INFO_GET_FEATURE_ID, |
| 440 | &feature_req, |
| 441 | sizeof(feature_req), |
| 442 | &feature_rsp, |
| 443 | sizeof(feature_rsp)); |
| 444 | if(!ret) |
| 445 | *major = TZBSP_GET_FEATURE_VERSION(feature_rsp.version); |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | int scm_protect_keystore(uint32_t * img_ptr, uint32_t img_len) |
| 451 | { |
| 452 | int ret=0; |
| 453 | ssd_protect_keystore_req protect_req; |
| 454 | ssd_protect_keystore_rsp protect_rsp; |
| 455 | |
| 456 | protect_req.keystore_ptr = img_ptr; |
| 457 | protect_req.keystore_len = img_len; |
| 458 | |
| 459 | arch_clean_invalidate_cache_range((addr_t) img_ptr, img_len); |
| 460 | |
| 461 | ret = scm_call(SCM_SVC_SSD, |
| 462 | SSD_PROTECT_KEYSTORE_ID, |
| 463 | &protect_req, |
| 464 | sizeof(protect_req), |
| 465 | &protect_rsp, |
| 466 | sizeof(protect_rsp)); |
| 467 | if(!ret) |
| 468 | { |
| 469 | if(protect_rsp.status == TZBSP_SSD_PKS_SUCCESS) |
| 470 | dprintf(INFO,"Successfully loaded the keystore "); |
| 471 | else |
| 472 | { |
| 473 | dprintf(INFO,"Loading keystore failed status %d ",protect_rsp.status); |
| 474 | ret = protect_rsp.status; |
| 475 | } |
| 476 | } |
| 477 | else |
| 478 | dprintf(INFO,"scm_call failed "); |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 483 | void set_tamper_fuse_cmd() |
| 484 | { |
| 485 | uint32_t svc_id; |
| 486 | uint32_t cmd_id; |
| 487 | void *cmd_buf; |
| 488 | size_t cmd_len; |
| 489 | void *resp_buf = NULL; |
| 490 | size_t resp_len = 0; |
| 491 | |
| 492 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
| 493 | cmd_buf = (void *)&fuse_id; |
| 494 | cmd_len = sizeof(fuse_id); |
| 495 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 496 | /*no response */ |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 497 | resp_buf = NULL; |
| 498 | resp_len = 0; |
| 499 | |
| 500 | svc_id = SCM_SVC_FUSE; |
| 501 | cmd_id = SCM_BLOW_SW_FUSE_ID; |
| 502 | |
| 503 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | uint8_t get_tamper_fuse_cmd() |
| 508 | { |
| 509 | uint32_t svc_id; |
| 510 | uint32_t cmd_id; |
| 511 | void *cmd_buf; |
| 512 | size_t cmd_len; |
| 513 | size_t resp_len = 0; |
| 514 | uint8_t resp_buf; |
| 515 | |
| 516 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
| 517 | cmd_buf = (void *)&fuse_id; |
| 518 | cmd_len = sizeof(fuse_id); |
| 519 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 520 | /*response */ |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 521 | resp_len = sizeof(resp_buf); |
| 522 | |
| 523 | svc_id = SCM_SVC_FUSE; |
| 524 | cmd_id = SCM_IS_SW_FUSE_BLOWN_ID; |
| 525 | |
| 526 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 527 | return resp_buf; |
| 528 | } |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 529 | |
Amir Samuelov | 4620ad2 | 2013-03-13 11:30:05 +0200 | [diff] [blame] | 530 | #define SHA256_DIGEST_LENGTH (256/8) |
| 531 | /* |
| 532 | * struct qseecom_save_partition_hash_req |
| 533 | * @partition_id - partition id. |
| 534 | * @digest[SHA256_DIGEST_LENGTH] - sha256 digest. |
| 535 | */ |
| 536 | struct qseecom_save_partition_hash_req { |
| 537 | uint32_t partition_id; /* in */ |
| 538 | uint8_t digest[SHA256_DIGEST_LENGTH]; /* in */ |
| 539 | }; |
| 540 | |
| 541 | |
| 542 | void save_kernel_hash_cmd(void *digest) |
| 543 | { |
| 544 | uint32_t svc_id; |
| 545 | uint32_t cmd_id; |
| 546 | void *cmd_buf; |
| 547 | size_t cmd_len; |
| 548 | void *resp_buf = NULL; |
| 549 | size_t resp_len = 0; |
| 550 | struct qseecom_save_partition_hash_req req; |
| 551 | |
| 552 | /*no response */ |
| 553 | resp_buf = NULL; |
| 554 | resp_len = 0; |
| 555 | |
| 556 | req.partition_id = 0; /* kernel */ |
| 557 | memcpy(req.digest, digest, sizeof(req.digest)); |
| 558 | |
| 559 | svc_id = SCM_SVC_ES; |
| 560 | cmd_id = SCM_SAVE_PARTITION_HASH_ID; |
| 561 | cmd_buf = (void *)&req; |
| 562 | cmd_len = sizeof(req); |
| 563 | |
| 564 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len); |
| 565 | } |
| 566 | |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 567 | /* |
| 568 | * Switches the CE1 channel between ADM and register usage. |
| 569 | * channel : AP_CE_REGISTER_USE, CE1 uses register interface |
| 570 | * : AP_CE_ADM_USE, CE1 uses ADM interface |
| 571 | */ |
| 572 | uint8_t switch_ce_chn_cmd(enum ap_ce_channel_type channel) |
| 573 | { |
| 574 | uint32_t svc_id; |
| 575 | uint32_t cmd_id; |
| 576 | void *cmd_buf; |
| 577 | size_t cmd_len; |
| 578 | size_t resp_len = 0; |
| 579 | uint8_t resp_buf; |
| 580 | |
| 581 | struct { |
| 582 | uint32_t resource; |
| 583 | uint32_t chn_id; |
| 584 | }__PACKED switch_ce_chn_buf; |
| 585 | |
| 586 | switch_ce_chn_buf.resource = TZ_RESOURCE_CE_AP; |
| 587 | switch_ce_chn_buf.chn_id = channel; |
| 588 | cmd_buf = (void *)&switch_ce_chn_buf; |
| 589 | cmd_len = sizeof(switch_ce_chn_buf); |
| 590 | |
| 591 | /*response */ |
| 592 | resp_len = sizeof(resp_buf); |
| 593 | |
| 594 | svc_id = SCM_SVC_CE_CHN_SWITCH_ID; |
| 595 | cmd_id = SCM_CE_CHN_SWITCH_ID; |
| 596 | |
| 597 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 598 | return resp_buf; |
| 599 | } |
| 600 | |
Channagoud Kadabi | 7037504 | 2013-12-12 14:53:31 -0800 | [diff] [blame] | 601 | int scm_halt_pmic_arbiter() |
| 602 | { |
| 603 | int ret = 0; |
| 604 | |
| 605 | ret = scm_call_atomic(SCM_SVC_PWR, SCM_IO_DISABLE_PMIC_ARBITER, 0); |
| 606 | |
| 607 | return ret; |
| 608 | } |
Maria Yu | d7826ef | 2014-06-30 13:05:43 +0800 | [diff] [blame] | 609 | |
| 610 | /* SCM Random Command */ |
| 611 | int scm_random(uint32_t * rbuf, uint32_t r_len) |
| 612 | { |
| 613 | int ret; |
| 614 | struct tz_prng_data data; |
| 615 | |
| 616 | data.out_buf = (uint8_t*) rbuf; |
| 617 | data.out_buf_size = r_len; |
| 618 | |
| 619 | /* |
| 620 | * random buffer must be flushed/invalidated before and after TZ call. |
| 621 | */ |
| 622 | arch_clean_invalidate_cache_range((addr_t) rbuf, r_len); |
| 623 | |
| 624 | ret = scm_call(TZ_SVC_CRYPTO, PRNG_CMD_ID, &data, sizeof(data), NULL, 0); |
| 625 | |
| 626 | /* Invalidate the updated random buffer */ |
| 627 | arch_clean_invalidate_cache_range((addr_t) rbuf, r_len); |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | void * get_canary() |
| 633 | { |
| 634 | void * canary; |
| 635 | if(scm_random(&canary, sizeof(canary))) { |
| 636 | dprintf(CRITICAL,"scm_call for random failed !!!"); |
| 637 | /* |
| 638 | * fall back to use lib rand API if scm call failed. |
| 639 | */ |
| 640 | canary = (void *)rand(); |
| 641 | } |
| 642 | |
| 643 | return canary; |
| 644 | } |
Sridhar Parasuram | d0443e2 | 2015-08-29 09:56:03 -0700 | [diff] [blame] | 645 | static bool secure_boot_enabled = true; |
| 646 | static bool wdog_debug_fuse_disabled = true; |
| 647 | |
| 648 | void scm_check_boot_fuses() |
| 649 | { |
| 650 | uint32_t ret = 0; |
| 651 | uint32_t resp; |
| 652 | |
| 653 | ret = scm_call(TZBSP_SVC_INFO, IS_SECURE_BOOT_ENABLED, NULL, 0, &resp, sizeof(resp)); |
| 654 | |
| 655 | /* Parse Bit 0 and Bit 2 of the response */ |
| 656 | if(!ret) { |
| 657 | /* Bit 0 - SECBOOT_ENABLE_CHECK */ |
| 658 | if(resp & 0x1) |
| 659 | secure_boot_enabled = false; |
| 660 | /* Bit 2 - DEBUG_DISABLE_CHECK */ |
| 661 | if(resp & 0x4) |
| 662 | wdog_debug_fuse_disabled = false; |
| 663 | } else |
| 664 | dprintf(CRITICAL, "scm call to check secure boot fuses failed\n"); |
| 665 | } |
| 666 | |
| 667 | bool is_secure_boot_enable() |
| 668 | { |
| 669 | scm_check_boot_fuses(); |
| 670 | return secure_boot_enabled; |
| 671 | } |