blob: 5838afd016bdb17eca071f9285eeee6c289d29fa [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;
191 decrypt_img_req *decrypt_cmd;
192
193 /* allocate memory for the command structure */
194 /* NEEDS TO BE CONTIGUOUS MEMORY */
195 decrypt_cmd = malloc(sizeof(decrypt_img_req));
196
197 /* setup the command for decryption */
198 setup_decrypt_cmd(decrypt_cmd, img_ptr, img_len_ptr);
199
200 ret = smc(decrypt_cmd);
201 free(decrypt_cmd);
202 return ret;
203}
204
205void set_tamper_fuse_cmd()
206{
207 uint32_t svc_id;
208 uint32_t cmd_id;
209 void *cmd_buf;
210 size_t cmd_len;
211 void *resp_buf = NULL;
212 size_t resp_len = 0;
213
214 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
215 cmd_buf = (void *)&fuse_id;
216 cmd_len = sizeof(fuse_id);
217
218 /*no response*/
219 resp_buf = NULL;
220 resp_len = 0;
221
222 svc_id = SCM_SVC_FUSE;
223 cmd_id = SCM_BLOW_SW_FUSE_ID;
224
225 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len);
226 return;
227}
228
229uint8_t get_tamper_fuse_cmd()
230{
231 uint32_t svc_id;
232 uint32_t cmd_id;
233 void *cmd_buf;
234 size_t cmd_len;
235 size_t resp_len = 0;
236 uint8_t resp_buf;
237
238 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
239 cmd_buf = (void *)&fuse_id;
240 cmd_len = sizeof(fuse_id);
241
242 /*response*/
243 resp_len = sizeof(resp_buf);
244
245 svc_id = SCM_SVC_FUSE;
246 cmd_id = SCM_IS_SW_FUSE_BLOWN_ID;
247
248 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len);
249 return resp_buf;
250}
251