blob: ddf086dffdd36b7b497965bddb56be7352ba76ab [file] [log] [blame]
Pavel Nedev71098522014-01-06 14:26:17 +02001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Deepa Dinamani904f8f82012-12-05 16:35:01 -08002 *
Shashank Mittal162244e2011-08-08 19:01:25 -07003 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
Deepa Dinamani904f8f82012-12-05 16:35:01 -08006 * * 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 Mittal162244e2011-08-08 19:01:25 -070015 *
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 Kadabi70375042013-12-12 14:53:31 -080032#include <asm.h>
33#include <bits.h>
Neeti Desai127b9e02012-03-20 16:11:23 -070034#include <arch/ops.h>
Shashank Mittal162244e2011-08-08 19:01:25 -070035#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 Kadabi70375042013-12-12 14:53:31 -080046#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 Mittal162244e2011-08-08 19:01:25 -070054/**
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 */
64static 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 Dudanib01e5062011-12-03 23:23:42 -080068 resp_size;
Shashank Mittal162244e2011-08-08 19:01:25 -070069
Deepa Dinamani904f8f82012-12-05 16:35:01 -080070 cmd = memalign(CACHE_LINE, ROUNDUP(len, CACHE_LINE));
Ajay Dudanib01e5062011-12-03 23:23:42 -080071 if (cmd) {
Pavel Nedev71098522014-01-06 14:26:17 +020072 memset(cmd, 0, len);
Shashank Mittal162244e2011-08-08 19:01:25 -070073 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 */
86static 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 Dudanib01e5062011-12-03 23:23:42 -080097static inline struct scm_response *scm_command_to_response(const struct
98 scm_command *cmd)
Shashank Mittal162244e2011-08-08 19:01:25 -070099{
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 */
109static 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 */
120static inline void *scm_get_response_buffer(const struct scm_response *rsp)
121{
122 return (void *)rsp + rsp->buf_offset;
123}
124
125static uint32_t smc(uint32_t cmd_addr)
126{
127 uint32_t context_id;
128 register uint32_t r0 __asm__("r0") = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129 register uint32_t r1 __asm__("r1") = (uint32_t) & context_id;
Shashank Mittal162244e2011-08-08 19:01:25 -0700130 register uint32_t r2 __asm__("r2") = cmd_addr;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800131 __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 -0700132 return r0;
133}
134
135/**
Channagoud Kadabi70375042013-12-12 14:53:31 -0800136* scm_call_automic: Make scm call with one or no argument
137* @svc: service id
138* @cmd: command id
139* @ arg1: argument
140*/
141
142static 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 Parasuramd0443e22015-08-29 09:56:03 -0700146 register uint32_t r1 __asm__("r1") = (uint32_t)&context_id;
Channagoud Kadabi70375042013-12-12 14:53:31 -0800147 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 Mittal162244e2011-08-08 19:01:25 -0700162 * 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 Dudanib01e5062011-12-03 23:23:42 -0800172int
173scm_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 Mittal162244e2011-08-08 19:01:25 -0700175{
176 int ret;
177 struct scm_command *cmd;
178 struct scm_response *rsp;
Neeti Desai127b9e02012-03-20 16:11:23 -0700179 uint8_t *resp_ptr;
Shashank Mittal162244e2011-08-08 19:01:25 -0700180
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 Desai127b9e02012-03-20 16:11:23 -0700189 /* Flush command to main memory for TZ */
190 arch_clean_invalidate_cache_range((addr_t) cmd, cmd->len);
191
Ajay Dudanib01e5062011-12-03 23:23:42 -0800192 ret = smc((uint32_t) cmd);
Shashank Mittal162244e2011-08-08 19:01:25 -0700193 if (ret)
194 goto out;
195
Ajay Dudanib01e5062011-12-03 23:23:42 -0800196 if (resp_len) {
Shashank Mittal162244e2011-08-08 19:01:25 -0700197 rsp = scm_command_to_response(cmd);
198
Neeti Desai127b9e02012-03-20 16:11:23 -0700199 do
200 {
201 /* Need to invalidate before each check since TZ will update
202 * the response complete flag in main memory.
203 */
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800204 arch_invalidate_cache_range((addr_t) rsp, sizeof(*rsp));
Neeti Desai127b9e02012-03-20 16:11:23 -0700205 } while (!rsp->is_complete);
206
207
208 resp_ptr = scm_get_response_buffer(rsp);
209
210 /* Invalidate any cached response data */
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800211 arch_invalidate_cache_range((addr_t) resp_ptr, resp_len);
Shashank Mittal162244e2011-08-08 19:01:25 -0700212
213 if (resp_buf)
Neeti Desai127b9e02012-03-20 16:11:23 -0700214 memcpy(resp_buf, resp_ptr, resp_len);
Shashank Mittal162244e2011-08-08 19:01:25 -0700215 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800216 out:
Shashank Mittal162244e2011-08-08 19:01:25 -0700217 free_scm_command(cmd);
218 return ret;
219}
220
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -0800221int restore_secure_cfg(uint32_t id)
222{
223 int ret, scm_ret = 0;
224 tz_secure_cfg secure_cfg;
225
Siddhartha Agrawald4648892013-02-17 18:16:18 -0800226 secure_cfg.id = id;
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -0800227 secure_cfg.spare = 0;
228
sundarajan srinivasanc2dee742013-02-21 11:31:36 -0800229 ret = scm_call(SVC_MEMORY_PROTECTION, IOMMU_SECURE_CFG, &secure_cfg, sizeof(secure_cfg),
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -0800230 &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 Desai127b9e02012-03-20 16:11:23 -0700242/* SCM Encrypt Command */
243int encrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr)
Shashank Mittal162244e2011-08-08 19:01:25 -0700244{
Neeti Desai127b9e02012-03-20 16:11:23 -0700245 int ret;
246 img_req cmd;
Shashank Mittal162244e2011-08-08 19:01:25 -0700247
Neeti Desai127b9e02012-03-20 16:11:23 -0700248 cmd.img_ptr = (uint32*) img_ptr;
249 cmd.img_len_ptr = img_len_ptr;
Shashank Mittal162244e2011-08-08 19:01:25 -0700250
Neeti Desai127b9e02012-03-20 16:11:23 -0700251 /* 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 Mittal162244e2011-08-08 19:01:25 -0700255
Neeti Desai127b9e02012-03-20 16:11:23 -0700256 ret = scm_call(SCM_SVC_SSD, SSD_ENCRYPT_ID, &cmd, sizeof(cmd), NULL, 0);
Shashank Mittal162244e2011-08-08 19:01:25 -0700257
Neeti Desai127b9e02012-03-20 16:11:23 -0700258 /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated
259 * before we use them.
Amol Jadi55e58da2011-11-17 14:03:34 -0800260 */
261 arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr));
Neeti Desai127b9e02012-03-20 16:11:23 -0700262 arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr));
Amol Jadi55e58da2011-11-17 14:03:34 -0800263
Neeti Desai127b9e02012-03-20 16:11:23 -0700264 /* Invalidate the updated image data */
265 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
Amol Jadi55e58da2011-11-17 14:03:34 -0800266
Shashank Mittal162244e2011-08-08 19:01:25 -0700267 return ret;
268}
269
Neeti Desai127b9e02012-03-20 16:11:23 -0700270/* SCM Decrypt Command */
271int 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800299static int ssd_image_is_encrypted(uint32_t ** img_ptr, uint32_t * img_len_ptr, uint32 * ctx_id)
300{
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700301 int ret = 0;
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800302 ssd_parse_md_req parse_req;
303 ssd_parse_md_rsp parse_rsp;
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700304 int prev_len = 0;
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800305
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700306 /* 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800313 parse_req.md = (uint32*)*img_ptr;
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700314 parse_req.md_len = ((*img_len_ptr) >= SSD_HEADER_MIN_SIZE) ? SSD_HEADER_MIN_SIZE : (*img_len_ptr);
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800315
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700316 arch_clean_invalidate_cache_range((addr_t) *img_ptr, parse_req.md_len);
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800317
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700318 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 Abbarapu0a060782013-11-11 16:56:04 +0530333 arch_clean_invalidate_cache_range((addr_t) (*img_ptr + prev_len),
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700334 (parse_req.md_len - prev_len) );
335
336 continue;
337 }
338 else
339 break;
340
341 } while(true);
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800342
343 if(!ret)
344 {
345 if(parse_rsp.status == SSD_PMD_ENCRYPTED)
346 {
347 *ctx_id = parse_rsp.md_ctx_id;
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700348 *img_len_ptr = *img_len_ptr - ((uint8_t*)parse_rsp.md_end_ptr - (uint8_t*)*img_ptr);
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800349 *img_ptr = (uint32_t*)parse_rsp.md_end_ptr;
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800350 }
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700351
352 ret = parse_rsp.status;
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800353 }
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700354 else
355 {
356 dprintf(CRITICAL,"ssd_image_is_encrypted call failed");
357
358 ASSERT(ret == 0);
359 }
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800360
361 return ret;
362}
363
364int 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 Srinivasaneb6d2202013-06-04 14:24:10 -0700371 ret = ssd_image_is_encrypted(img_ptr,img_len_ptr,&ctx_id);
372 switch(ret)
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700373 {
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700374 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800378
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700379 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800380
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700381 /*decrypt the image here*/
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800382
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700383 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800387
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700388 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800394
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700395 if(!ret){
396 ret = decrypt_rsp.status;
397 }
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700398
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700399 /* 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 srinivasan4dfd4f72013-02-27 14:13:09 -0800404
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700405 /* Invalidate the updated image data */
406 arch_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
sundarajan srinivasan6173b872013-03-13 17:36:48 -0700407
Sundarajan Srinivasaneb6d2202013-06-04 14:24:10 -0700408 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 srinivasan6173b872013-03-13 17:36:48 -0700426 }
sundarajan srinivasan4dfd4f72013-02-27 14:13:09 -0800427 return ret;
428}
429
430int 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
450int 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 Mittal162244e2011-08-08 19:01:25 -0700483void 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 Dudanib01e5062011-12-03 23:23:42 -0800496 /*no response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700497 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
507uint8_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 Dudanib01e5062011-12-03 23:23:42 -0800520 /*response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700521 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 Dinamani193874e2012-02-07 14:00:04 -0800529
Amir Samuelov4620ad22013-03-13 11:30:05 +0200530#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 */
536struct qseecom_save_partition_hash_req {
537 uint32_t partition_id; /* in */
538 uint8_t digest[SHA256_DIGEST_LENGTH]; /* in */
539};
540
541
542void 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 Dinamani193874e2012-02-07 14:00:04 -0800567/*
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 */
572uint8_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 Kadabi70375042013-12-12 14:53:31 -0800601int 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 Yud7826ef2014-06-30 13:05:43 +0800609
610/* SCM Random Command */
611int 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
632void * 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 Parasuramd0443e22015-08-29 09:56:03 -0700645static bool secure_boot_enabled = true;
646static bool wdog_debug_fuse_disabled = true;
647
648void 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
667bool is_secure_boot_enable()
668{
669 scm_check_boot_fuses();
670 return secure_boot_enabled;
671}