Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, 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> |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 32 | #include <arch/ops.h> |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 33 | #include "scm.h" |
| 34 | |
| 35 | #pragma GCC optimize ("O0") |
| 36 | |
| 37 | /* From Linux Kernel asm/system.h */ |
| 38 | #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t" |
| 39 | |
| 40 | #ifndef offsetof |
| 41 | # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 42 | #endif |
| 43 | |
| 44 | /** |
| 45 | * alloc_scm_command() - Allocate an SCM command |
| 46 | * @cmd_size: size of the command buffer |
| 47 | * @resp_size: size of the response buffer |
| 48 | * |
| 49 | * Allocate an SCM command, including enough room for the command |
| 50 | * and response headers as well as the command and response buffers. |
| 51 | * |
| 52 | * Returns a valid &scm_command on success or %NULL if the allocation fails. |
| 53 | */ |
| 54 | static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size) |
| 55 | { |
| 56 | struct scm_command *cmd; |
| 57 | size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 58 | resp_size; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 59 | |
Deepa Dinamani | 904f8f8 | 2012-12-05 16:35:01 -0800 | [diff] [blame] | 60 | cmd = memalign(CACHE_LINE, ROUNDUP(len, CACHE_LINE)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 61 | if (cmd) { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 62 | cmd->len = len; |
| 63 | cmd->buf_offset = offsetof(struct scm_command, buf); |
| 64 | cmd->resp_hdr_offset = cmd->buf_offset + cmd_size; |
| 65 | } |
| 66 | return cmd; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * free_scm_command() - Free an SCM command |
| 71 | * @cmd: command to free |
| 72 | * |
| 73 | * Free an SCM command. |
| 74 | */ |
| 75 | static inline void free_scm_command(struct scm_command *cmd) |
| 76 | { |
| 77 | free(cmd); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * scm_command_to_response() - Get a pointer to a scm_response |
| 82 | * @cmd: command |
| 83 | * |
| 84 | * Returns a pointer to a response for a command. |
| 85 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 86 | static inline struct scm_response *scm_command_to_response(const struct |
| 87 | scm_command *cmd) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 88 | { |
| 89 | return (void *)cmd + cmd->resp_hdr_offset; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * scm_get_command_buffer() - Get a pointer to a command buffer |
| 94 | * @cmd: command |
| 95 | * |
| 96 | * Returns a pointer to the command buffer of a command. |
| 97 | */ |
| 98 | static inline void *scm_get_command_buffer(const struct scm_command *cmd) |
| 99 | { |
| 100 | return (void *)cmd->buf; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * scm_get_response_buffer() - Get a pointer to a response buffer |
| 105 | * @rsp: response |
| 106 | * |
| 107 | * Returns a pointer to a response buffer of a response. |
| 108 | */ |
| 109 | static inline void *scm_get_response_buffer(const struct scm_response *rsp) |
| 110 | { |
| 111 | return (void *)rsp + rsp->buf_offset; |
| 112 | } |
| 113 | |
| 114 | static uint32_t smc(uint32_t cmd_addr) |
| 115 | { |
| 116 | uint32_t context_id; |
| 117 | register uint32_t r0 __asm__("r0") = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 118 | register uint32_t r1 __asm__("r1") = (uint32_t) & context_id; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 119 | register uint32_t r2 __asm__("r2") = cmd_addr; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 120 | __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] | 121 | return r0; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * scm_call() - Send an SCM command |
| 126 | * @svc_id: service identifier |
| 127 | * @cmd_id: command identifier |
| 128 | * @cmd_buf: command buffer |
| 129 | * @cmd_len: length of the command buffer |
| 130 | * @resp_buf: response buffer |
| 131 | * @resp_len: length of the response buffer |
| 132 | * |
| 133 | * Sends a command to the SCM and waits for the command to finish processing. |
| 134 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 135 | int |
| 136 | scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf, |
| 137 | size_t cmd_len, void *resp_buf, size_t resp_len) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 138 | { |
| 139 | int ret; |
| 140 | struct scm_command *cmd; |
| 141 | struct scm_response *rsp; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 142 | uint8_t *resp_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 143 | |
| 144 | cmd = alloc_scm_command(cmd_len, resp_len); |
| 145 | if (!cmd) |
| 146 | return ERR_NO_MEMORY; |
| 147 | |
| 148 | cmd->id = (svc_id << 10) | cmd_id; |
| 149 | if (cmd_buf) |
| 150 | memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len); |
| 151 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 152 | /* Flush command to main memory for TZ */ |
| 153 | arch_clean_invalidate_cache_range((addr_t) cmd, cmd->len); |
| 154 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 155 | ret = smc((uint32_t) cmd); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 156 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 157 | if (ret) |
| 158 | goto out; |
| 159 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | if (resp_len) { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 161 | rsp = scm_command_to_response(cmd); |
| 162 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 163 | do |
| 164 | { |
| 165 | /* Need to invalidate before each check since TZ will update |
| 166 | * the response complete flag in main memory. |
| 167 | */ |
| 168 | arch_clean_invalidate_cache_range((addr_t) rsp, sizeof(*rsp)); |
| 169 | } while (!rsp->is_complete); |
| 170 | |
| 171 | |
| 172 | resp_ptr = scm_get_response_buffer(rsp); |
| 173 | |
| 174 | /* Invalidate any cached response data */ |
| 175 | arch_clean_invalidate_cache_range((addr_t) resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 176 | |
| 177 | if (resp_buf) |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 178 | memcpy(resp_buf, resp_ptr, resp_len); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 179 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 180 | out: |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 181 | free_scm_command(cmd); |
| 182 | return ret; |
| 183 | } |
| 184 | |
Siddhartha Agrawal | eb094c5 | 2013-01-28 12:11:43 -0800 | [diff] [blame] | 185 | int restore_secure_cfg(uint32_t id) |
| 186 | { |
| 187 | int ret, scm_ret = 0; |
| 188 | tz_secure_cfg secure_cfg; |
| 189 | |
| 190 | secure_cfg.id = 1; |
| 191 | secure_cfg.spare = 0; |
| 192 | |
sundarajan srinivasan | c2dee74 | 2013-02-21 11:31:36 -0800 | [diff] [blame^] | 193 | 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] | 194 | &scm_ret, sizeof(scm_ret)); |
| 195 | |
| 196 | if (ret || scm_ret) { |
| 197 | dprintf(CRITICAL, "Secure Config failed\n"); |
| 198 | ret = 1; |
| 199 | } else |
| 200 | ret = 0; |
| 201 | |
| 202 | return ret; |
| 203 | |
| 204 | } |
| 205 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 206 | /* SCM Encrypt Command */ |
| 207 | int encrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 208 | { |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 209 | int ret; |
| 210 | img_req cmd; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 211 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 212 | cmd.img_ptr = (uint32*) img_ptr; |
| 213 | cmd.img_len_ptr = img_len_ptr; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 214 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 215 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 216 | * It must be flushed/invalidated before and after TZ call. |
| 217 | */ |
| 218 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 219 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 220 | 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] | 221 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 222 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 223 | * before we use them. |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 224 | */ |
| 225 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 226 | 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] | 227 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 228 | /* Invalidate the updated image data */ |
| 229 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
Amol Jadi | 55e58da | 2011-11-17 14:03:34 -0800 | [diff] [blame] | 230 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 231 | return ret; |
| 232 | } |
| 233 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 234 | /* SCM Decrypt Command */ |
| 235 | int decrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr) |
| 236 | { |
| 237 | int ret; |
| 238 | img_req cmd; |
| 239 | |
| 240 | cmd.img_ptr = (uint32*) img_ptr; |
| 241 | cmd.img_len_ptr = img_len_ptr; |
| 242 | |
| 243 | /* Image data is operated upon by TZ, which accesses only the main memory. |
| 244 | * It must be flushed/invalidated before and after TZ call. |
| 245 | */ |
| 246 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 247 | |
| 248 | ret = scm_call(SCM_SVC_SSD, SSD_DECRYPT_ID, &cmd, sizeof(cmd), NULL, 0); |
| 249 | |
| 250 | /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated |
| 251 | * before we use them. |
| 252 | */ |
| 253 | arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr)); |
| 254 | arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr)); |
| 255 | |
| 256 | /* Invalidate the updated image data */ |
| 257 | arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 263 | void set_tamper_fuse_cmd() |
| 264 | { |
| 265 | uint32_t svc_id; |
| 266 | uint32_t cmd_id; |
| 267 | void *cmd_buf; |
| 268 | size_t cmd_len; |
| 269 | void *resp_buf = NULL; |
| 270 | size_t resp_len = 0; |
| 271 | |
| 272 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
| 273 | cmd_buf = (void *)&fuse_id; |
| 274 | cmd_len = sizeof(fuse_id); |
| 275 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 276 | /*no response */ |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 277 | resp_buf = NULL; |
| 278 | resp_len = 0; |
| 279 | |
| 280 | svc_id = SCM_SVC_FUSE; |
| 281 | cmd_id = SCM_BLOW_SW_FUSE_ID; |
| 282 | |
| 283 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | uint8_t get_tamper_fuse_cmd() |
| 288 | { |
| 289 | uint32_t svc_id; |
| 290 | uint32_t cmd_id; |
| 291 | void *cmd_buf; |
| 292 | size_t cmd_len; |
| 293 | size_t resp_len = 0; |
| 294 | uint8_t resp_buf; |
| 295 | |
| 296 | uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE; |
| 297 | cmd_buf = (void *)&fuse_id; |
| 298 | cmd_len = sizeof(fuse_id); |
| 299 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 300 | /*response */ |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 301 | resp_len = sizeof(resp_buf); |
| 302 | |
| 303 | svc_id = SCM_SVC_FUSE; |
| 304 | cmd_id = SCM_IS_SW_FUSE_BLOWN_ID; |
| 305 | |
| 306 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 307 | return resp_buf; |
| 308 | } |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * Switches the CE1 channel between ADM and register usage. |
| 312 | * channel : AP_CE_REGISTER_USE, CE1 uses register interface |
| 313 | * : AP_CE_ADM_USE, CE1 uses ADM interface |
| 314 | */ |
| 315 | uint8_t switch_ce_chn_cmd(enum ap_ce_channel_type channel) |
| 316 | { |
| 317 | uint32_t svc_id; |
| 318 | uint32_t cmd_id; |
| 319 | void *cmd_buf; |
| 320 | size_t cmd_len; |
| 321 | size_t resp_len = 0; |
| 322 | uint8_t resp_buf; |
| 323 | |
| 324 | struct { |
| 325 | uint32_t resource; |
| 326 | uint32_t chn_id; |
| 327 | }__PACKED switch_ce_chn_buf; |
| 328 | |
| 329 | switch_ce_chn_buf.resource = TZ_RESOURCE_CE_AP; |
| 330 | switch_ce_chn_buf.chn_id = channel; |
| 331 | cmd_buf = (void *)&switch_ce_chn_buf; |
| 332 | cmd_len = sizeof(switch_ce_chn_buf); |
| 333 | |
| 334 | /*response */ |
| 335 | resp_len = sizeof(resp_buf); |
| 336 | |
| 337 | svc_id = SCM_SVC_CE_CHN_SWITCH_ID; |
| 338 | cmd_id = SCM_CE_CHN_SWITCH_ID; |
| 339 | |
| 340 | scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len); |
| 341 | return resp_buf; |
| 342 | } |
| 343 | |