blob: df1b39950ca1e75fee1b8e21760e5ddabde7dcf7 [file] [log] [blame]
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -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 "scm_decrypt.h"
30
31#pragma GCC optimize ("O0")
32
33/* From Linux Kernel asm/system.h */
34#define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
35
36void setup_decrypt_cmd ( decrypt_img_req* dec_cmd,
37 uint32_t** img_ptr,
38 uint32_t* img_len_ptr)
39{
40 dec_cmd->common_req.len = sizeof(decrypt_img_req);
41 dec_cmd->common_req.buf_offset = sizeof(scm_command);
42 dec_cmd->common_req.resp_hdr_offset = 0;
43 dec_cmd->common_req.id = SSD_DECRYPT_IMG_ID;
44
45 dec_cmd->img_ptr = img_ptr;
46 dec_cmd->img_len_ptr = img_len_ptr;
47}
48
49static uint32_t smc(uint32_t cmd_addr)
50{
51 uint32_t context_id;
52 register uint32_t r0 __asm__("r0") = 1;
53 register uint32_t r1 __asm__("r1") = (uint32_t)&context_id;
54 register uint32_t r2 __asm__("r2") = cmd_addr;
55 __asm__(
56 "1:smc #0 @ switch to secure world\n"
57 "cmp r0, #1 \n"
58 "beq 1b \n"
59 : "=r" (r0)
60 : "r" (r0), "r" (r1), "r" (r2)
61 : "r3", "cc");
62 return r0;
63}
64
65
66int decrypt_img_scm(uint32_t** img_ptr, uint32_t* img_len_ptr)
67{
68 int ret = 0;
69 decrypt_img_req *decrypt_cmd;
70
71 /* allocate memory for the command structure */
72 /* NEEDS TO BE CONTIGUOUS MEMORY */
73 decrypt_cmd = malloc(sizeof(decrypt_img_req));
74
75 /* setup the command for decryption */
76 setup_decrypt_cmd(decrypt_cmd, img_ptr, img_len_ptr);
77
78 ret = smc(decrypt_cmd);
79 free(decrypt_cmd);
80 return ret;
81}
82