blob: ad83f5870a1e9cf07143ea0bfdad36b89c4c634b [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 +
57 resp_size;
58
59 cmd = malloc(len);
60 if (cmd)
61 {
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 */
75static 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 */
86static inline struct scm_response *scm_command_to_response(
87 const struct scm_command *cmd)
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 */
98static 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 */
109static inline void *scm_get_response_buffer(const struct scm_response *rsp)
110{
111 return (void *)rsp + rsp->buf_offset;
112}
113
114static uint32_t smc(uint32_t cmd_addr)
115{
116 uint32_t context_id;
117 register uint32_t r0 __asm__("r0") = 1;
118 register uint32_t r1 __asm__("r1") = (uint32_t)&context_id;
119 register uint32_t r2 __asm__("r2") = cmd_addr;
120 __asm__(
121 "1:smc #0 @ switch to secure world\n"
122 "cmp r0, #1 \n"
123 "beq 1b \n"
124 : "=r" (r0)
125 : "r" (r0), "r" (r1), "r" (r2)
126 : "r3", "cc");
127 return r0;
128}
129
130/**
131 * scm_call() - Send an SCM command
132 * @svc_id: service identifier
133 * @cmd_id: command identifier
134 * @cmd_buf: command buffer
135 * @cmd_len: length of the command buffer
136 * @resp_buf: response buffer
137 * @resp_len: length of the response buffer
138 *
139 * Sends a command to the SCM and waits for the command to finish processing.
140 */
141int scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf, size_t cmd_len,
142 void *resp_buf, size_t resp_len)
143{
144 int ret;
145 struct scm_command *cmd;
146 struct scm_response *rsp;
147
148 cmd = alloc_scm_command(cmd_len, resp_len);
149 if (!cmd)
150 return ERR_NO_MEMORY;
151
152 cmd->id = (svc_id << 10) | cmd_id;
153 if (cmd_buf)
154 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
155
156 ret = smc((uint32_t)cmd);
157 if (ret)
158 goto out;
159
160 if(resp_len)
161 {
162 rsp = scm_command_to_response(cmd);
163
164 while (!rsp->is_complete);
165
166 if (resp_buf)
167 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
168 }
169out:
170 free_scm_command(cmd);
171 return ret;
172}
173
174/* SCM Decrypt Command */
175void setup_decrypt_cmd ( decrypt_img_req* dec_cmd,
176 uint32_t** img_ptr,
177 uint32_t* img_len_ptr)
178{
179 dec_cmd->common_req.len = sizeof(decrypt_img_req);
180 dec_cmd->common_req.buf_offset = sizeof(scm_command);
181 dec_cmd->common_req.resp_hdr_offset = 0;
182 dec_cmd->common_req.id = SSD_DECRYPT_IMG_ID;
183
184 dec_cmd->img_ptr = img_ptr;
185 dec_cmd->img_len_ptr = img_len_ptr;
186}
187
188int decrypt_img_scm(uint32_t** img_ptr, uint32_t* img_len_ptr)
189{
190 int ret = 0;
Amol Jadi55e58da2011-11-17 14:03:34 -0800191 decrypt_img_req decrypt_cmd;
Shashank Mittal162244e2011-08-08 19:01:25 -0700192
193 /* setup the command for decryption */
Amol Jadi55e58da2011-11-17 14:03:34 -0800194 setup_decrypt_cmd(&decrypt_cmd, img_ptr, img_len_ptr);
Shashank Mittal162244e2011-08-08 19:01:25 -0700195
Amol Jadi55e58da2011-11-17 14:03:34 -0800196 /* Since TZ cannot access cached data, cmd must be flushed to main memory */
197 arch_clean_invalidate_cache_range((addr_t) &decrypt_cmd, sizeof(decrypt_cmd));
198
199 /* Invalidate img ptr and len from cache so that we read the updated data
200 * from the main memory.
201 */
202 arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr));
203 arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr));
204
205 ret = smc(&decrypt_cmd);
206
Shashank Mittal162244e2011-08-08 19:01:25 -0700207 return ret;
208}
209
210void set_tamper_fuse_cmd()
211{
212 uint32_t svc_id;
213 uint32_t cmd_id;
214 void *cmd_buf;
215 size_t cmd_len;
216 void *resp_buf = NULL;
217 size_t resp_len = 0;
218
219 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
220 cmd_buf = (void *)&fuse_id;
221 cmd_len = sizeof(fuse_id);
222
223 /*no response*/
224 resp_buf = NULL;
225 resp_len = 0;
226
227 svc_id = SCM_SVC_FUSE;
228 cmd_id = SCM_BLOW_SW_FUSE_ID;
229
230 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len);
231 return;
232}
233
234uint8_t get_tamper_fuse_cmd()
235{
236 uint32_t svc_id;
237 uint32_t cmd_id;
238 void *cmd_buf;
239 size_t cmd_len;
240 size_t resp_len = 0;
241 uint8_t resp_buf;
242
243 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
244 cmd_buf = (void *)&fuse_id;
245 cmd_len = sizeof(fuse_id);
246
247 /*response*/
248 resp_len = sizeof(resp_buf);
249
250 svc_id = SCM_SVC_FUSE;
251 cmd_id = SCM_IS_SW_FUSE_BLOWN_ID;
252
253 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len);
254 return resp_buf;
255}
256