imx8m: Add DEK blob encapsulation for imx8m
[platform/kernel/u-boot.git] / arch / arm / mach-imx / cmd_dek.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008-2015 Freescale Semiconductor, Inc.
4  *
5  * Command for encapsulating DEK blob
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <asm/byteorder.h>
13 #include <linux/compiler.h>
14 #include <fsl_sec.h>
15 #include <asm/arch/clock.h>
16 #include <mapmem.h>
17 #include <tee.h>
18
19 /**
20 * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
21 * @src: - Address of data to be encapsulated
22 * @dst: - Desination address of encapsulated data
23 * @len: - Size of data to be encapsulated
24 *
25 * Returns zero on success,and negative on error.
26 */
27 #ifdef CONFIG_IMX_CAAM_DEK_ENCAP
28 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
29 {
30         u8 *src_ptr, *dst_ptr;
31
32         src_ptr = map_sysmem(src_addr, len / 8);
33         dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len / 8));
34
35         hab_caam_clock_enable(1);
36
37         u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR +
38                                    FSL_CAAM_ORSR_JRa_OFFSET);
39         if (out_jr_size != FSL_CAAM_MAX_JR_SIZE)
40                 sec_init();
41
42         if (!((len == 128) | (len == 192) | (len == 256))) {
43                 debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
44                 return -1;
45         }
46
47         len /= 8;
48         return blob_dek(src_ptr, dst_ptr, len);
49 }
50 #endif /* CONFIG_IMX_CAAM_DEK_ENCAP */
51
52 #ifdef CONFIG_IMX_OPTEE_DEK_ENCAP
53
54 #define PTA_DEK_BLOB_PTA_UUID {0xef477737, 0x0db1, 0x4a9d, \
55         {0x84, 0x37, 0xf2, 0xf5, 0x35, 0xc0, 0xbd, 0x92} }
56
57 #define OPTEE_BLOB_HDR_SIZE             8
58
59 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
60 {
61         struct udevice *dev = NULL;
62         struct tee_shm *shm_input, *shm_output;
63         struct tee_open_session_arg arg = {0};
64         struct tee_invoke_arg arg_func = {0};
65         const struct tee_optee_ta_uuid uuid = PTA_DEK_BLOB_PTA_UUID;
66         struct tee_param param[4] = {0};
67         int ret;
68
69         /* Get tee device */
70         dev = tee_find_device(NULL, NULL, NULL, NULL);
71         if (!dev) {
72                 printf("Cannot get OP-TEE device\n");
73                 return -1;
74         }
75
76         /* Set TA UUID */
77         tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
78
79         /* Open TA session */
80         ret = tee_open_session(dev, &arg, 0, NULL);
81         if (ret < 0) {
82                 printf("Cannot open session with PTA Blob 0x%X\n", ret);
83                 return -1;
84         }
85
86         /* Allocate shared input and output buffers for TA */
87         ret = tee_shm_register(dev, (void *)(ulong)src_addr, len / 8, 0x0, &shm_input);
88         if (ret < 0) {
89                 printf("Cannot register input shared memory 0x%X\n", ret);
90                 goto error;
91         }
92
93         ret = tee_shm_register(dev, (void *)(ulong)dst_addr,
94                                BLOB_SIZE(len / 8) + OPTEE_BLOB_HDR_SIZE,
95                                0x0, &shm_output);
96         if (ret < 0) {
97                 printf("Cannot register output shared memory 0x%X\n", ret);
98                 goto error;
99         }
100
101         param[0].u.memref.shm   = shm_input;
102         param[0].u.memref.size  = shm_input->size;
103         param[0].attr           = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
104         param[1].u.memref.shm   = shm_output;
105         param[1].u.memref.size  = shm_output->size;
106         param[1].attr           = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
107         param[2].attr           = TEE_PARAM_ATTR_TYPE_NONE;
108         param[3].attr           = TEE_PARAM_ATTR_TYPE_NONE;
109
110         arg_func.func = 0;
111         arg_func.session = arg.session;
112
113         /* Generate DEK blob */
114         arg_func.session = arg.session;
115         ret = tee_invoke_func(dev, &arg_func, 4, param);
116         if (ret < 0)
117                 printf("Cannot generate Blob with PTA DEK Blob 0x%X\n", ret);
118
119 error:
120         /* Free shared memory */
121         tee_shm_free(shm_input);
122         tee_shm_free(shm_output);
123
124         /* Close session */
125         ret = tee_close_session(dev, arg.session);
126         if (ret < 0)
127                 printf("Cannot close session with PTA DEK Blob 0x%X\n", ret);
128
129         return ret;
130 }
131 #endif /* CONFIG_IMX_OPTEE_DEK_ENCAP */
132
133 /**
134  * do_dek_blob() - Handle the "dek_blob" command-line command
135  * @cmdtp:  Command data struct pointer
136  * @flag:   Command flag
137  * @argc:   Command-line argument count
138  * @argv:   Array of command-line arguments
139  *
140  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
141  * on error.
142  */
143 static int do_dek_blob(struct cmd_tbl *cmdtp, int flag, int argc,
144                        char *const argv[])
145 {
146         uint32_t src_addr, dst_addr, len;
147
148         if (argc != 4)
149                 return CMD_RET_USAGE;
150
151         src_addr = simple_strtoul(argv[1], NULL, 16);
152         dst_addr = simple_strtoul(argv[2], NULL, 16);
153         len = simple_strtoul(argv[3], NULL, 10);
154
155         return blob_encap_dek(src_addr, dst_addr, len);
156 }
157
158 /***************************************************/
159 static char dek_blob_help_text[] =
160         "src dst len            - Encapsulate and create blob of data\n"
161         "                         $len bits long at address $src and\n"
162         "                         store the result at address $dst.\n";
163
164 U_BOOT_CMD(
165         dek_blob, 4, 1, do_dek_blob,
166         "Data Encryption Key blob encapsulation",
167         dek_blob_help_text
168 );