blob: 4905a9e544b593954b58744fbed3286fb513084e [file] [log] [blame]
Shashank Mittal162244e2011-08-08 19:01:25 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
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 Code Aurora Forum, Inc. 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.
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>
32#include "scm.h"
33
34#pragma GCC optimize ("O0")
35
36/* From Linux Kernel asm/system.h */
37#define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
38
39#ifndef offsetof
40# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
41#endif
42
43/**
44 * alloc_scm_command() - Allocate an SCM command
45 * @cmd_size: size of the command buffer
46 * @resp_size: size of the response buffer
47 *
48 * Allocate an SCM command, including enough room for the command
49 * and response headers as well as the command and response buffers.
50 *
51 * Returns a valid &scm_command on success or %NULL if the allocation fails.
52 */
53static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
54{
55 struct scm_command *cmd;
56 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -080057 resp_size;
Shashank Mittal162244e2011-08-08 19:01:25 -070058
59 cmd = malloc(len);
Ajay Dudanib01e5062011-12-03 23:23:42 -080060 if (cmd) {
Shashank Mittal162244e2011-08-08 19:01:25 -070061 cmd->len = len;
62 cmd->buf_offset = offsetof(struct scm_command, buf);
63 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
64 }
65 return cmd;
66}
67
68/**
69 * free_scm_command() - Free an SCM command
70 * @cmd: command to free
71 *
72 * Free an SCM command.
73 */
74static inline void free_scm_command(struct scm_command *cmd)
75{
76 free(cmd);
77}
78
79/**
80 * scm_command_to_response() - Get a pointer to a scm_response
81 * @cmd: command
82 *
83 * Returns a pointer to a response for a command.
84 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080085static inline struct scm_response *scm_command_to_response(const struct
86 scm_command *cmd)
Shashank Mittal162244e2011-08-08 19:01:25 -070087{
88 return (void *)cmd + cmd->resp_hdr_offset;
89}
90
91/**
92 * scm_get_command_buffer() - Get a pointer to a command buffer
93 * @cmd: command
94 *
95 * Returns a pointer to the command buffer of a command.
96 */
97static inline void *scm_get_command_buffer(const struct scm_command *cmd)
98{
99 return (void *)cmd->buf;
100}
101
102/**
103 * scm_get_response_buffer() - Get a pointer to a response buffer
104 * @rsp: response
105 *
106 * Returns a pointer to a response buffer of a response.
107 */
108static inline void *scm_get_response_buffer(const struct scm_response *rsp)
109{
110 return (void *)rsp + rsp->buf_offset;
111}
112
113static uint32_t smc(uint32_t cmd_addr)
114{
115 uint32_t context_id;
116 register uint32_t r0 __asm__("r0") = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800117 register uint32_t r1 __asm__("r1") = (uint32_t) & context_id;
Shashank Mittal162244e2011-08-08 19:01:25 -0700118 register uint32_t r2 __asm__("r2") = cmd_addr;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800119 __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 Mittal162244e2011-08-08 19:01:25 -0700120 return r0;
121}
122
123/**
124 * scm_call() - Send an SCM command
125 * @svc_id: service identifier
126 * @cmd_id: command identifier
127 * @cmd_buf: command buffer
128 * @cmd_len: length of the command buffer
129 * @resp_buf: response buffer
130 * @resp_len: length of the response buffer
131 *
132 * Sends a command to the SCM and waits for the command to finish processing.
133 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800134int
135scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf,
136 size_t cmd_len, void *resp_buf, size_t resp_len)
Shashank Mittal162244e2011-08-08 19:01:25 -0700137{
138 int ret;
139 struct scm_command *cmd;
140 struct scm_response *rsp;
141
142 cmd = alloc_scm_command(cmd_len, resp_len);
143 if (!cmd)
144 return ERR_NO_MEMORY;
145
146 cmd->id = (svc_id << 10) | cmd_id;
147 if (cmd_buf)
148 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
149
Ajay Dudanib01e5062011-12-03 23:23:42 -0800150 ret = smc((uint32_t) cmd);
Shashank Mittal162244e2011-08-08 19:01:25 -0700151 if (ret)
152 goto out;
153
Ajay Dudanib01e5062011-12-03 23:23:42 -0800154 if (resp_len) {
Shashank Mittal162244e2011-08-08 19:01:25 -0700155 rsp = scm_command_to_response(cmd);
156
Ajay Dudanib01e5062011-12-03 23:23:42 -0800157 while (!rsp->is_complete) ;
Shashank Mittal162244e2011-08-08 19:01:25 -0700158
159 if (resp_buf)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 memcpy(resp_buf, scm_get_response_buffer(rsp),
161 resp_len);
Shashank Mittal162244e2011-08-08 19:01:25 -0700162 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800163 out:
Shashank Mittal162244e2011-08-08 19:01:25 -0700164 free_scm_command(cmd);
165 return ret;
166}
167
168/* SCM Decrypt Command */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800169void
170setup_decrypt_cmd(decrypt_img_req * dec_cmd,
171 uint32_t ** img_ptr, uint32_t * img_len_ptr)
Shashank Mittal162244e2011-08-08 19:01:25 -0700172{
173 dec_cmd->common_req.len = sizeof(decrypt_img_req);
174 dec_cmd->common_req.buf_offset = sizeof(scm_command);
175 dec_cmd->common_req.resp_hdr_offset = 0;
176 dec_cmd->common_req.id = SSD_DECRYPT_IMG_ID;
177
178 dec_cmd->img_ptr = img_ptr;
179 dec_cmd->img_len_ptr = img_len_ptr;
180}
181
Ajay Dudanib01e5062011-12-03 23:23:42 -0800182int decrypt_img_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr)
Shashank Mittal162244e2011-08-08 19:01:25 -0700183{
184 int ret = 0;
Amol Jadi55e58da2011-11-17 14:03:34 -0800185 decrypt_img_req decrypt_cmd;
Shashank Mittal162244e2011-08-08 19:01:25 -0700186
187 /* setup the command for decryption */
Amol Jadi55e58da2011-11-17 14:03:34 -0800188 setup_decrypt_cmd(&decrypt_cmd, img_ptr, img_len_ptr);
Shashank Mittal162244e2011-08-08 19:01:25 -0700189
Amol Jadi55e58da2011-11-17 14:03:34 -0800190 /* Since TZ cannot access cached data, cmd must be flushed to main memory */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800191 arch_clean_invalidate_cache_range((addr_t) & decrypt_cmd,
192 sizeof(decrypt_cmd));
Amol Jadi55e58da2011-11-17 14:03:34 -0800193
194 /* Invalidate img ptr and len from cache so that we read the updated data
195 * from the main memory.
196 */
197 arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr));
Ajay Dudanib01e5062011-12-03 23:23:42 -0800198 arch_clean_invalidate_cache_range((addr_t) img_len_ptr,
199 sizeof(img_len_ptr));
Amol Jadi55e58da2011-11-17 14:03:34 -0800200
201 ret = smc(&decrypt_cmd);
202
Shashank Mittal162244e2011-08-08 19:01:25 -0700203 return ret;
204}
205
206void set_tamper_fuse_cmd()
207{
208 uint32_t svc_id;
209 uint32_t cmd_id;
210 void *cmd_buf;
211 size_t cmd_len;
212 void *resp_buf = NULL;
213 size_t resp_len = 0;
214
215 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
216 cmd_buf = (void *)&fuse_id;
217 cmd_len = sizeof(fuse_id);
218
Ajay Dudanib01e5062011-12-03 23:23:42 -0800219 /*no response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700220 resp_buf = NULL;
221 resp_len = 0;
222
223 svc_id = SCM_SVC_FUSE;
224 cmd_id = SCM_BLOW_SW_FUSE_ID;
225
226 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len);
227 return;
228}
229
230uint8_t get_tamper_fuse_cmd()
231{
232 uint32_t svc_id;
233 uint32_t cmd_id;
234 void *cmd_buf;
235 size_t cmd_len;
236 size_t resp_len = 0;
237 uint8_t resp_buf;
238
239 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
240 cmd_buf = (void *)&fuse_id;
241 cmd_len = sizeof(fuse_id);
242
Ajay Dudanib01e5062011-12-03 23:23:42 -0800243 /*response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700244 resp_len = sizeof(resp_buf);
245
246 svc_id = SCM_SVC_FUSE;
247 cmd_id = SCM_IS_SW_FUSE_BLOWN_ID;
248
249 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len);
250 return resp_buf;
251}