359c8940fb9aa4182244ab1aca151b7c3b6c0651
[platform/kernel/u-boot.git] / cmd / blob.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *
4  * Command for encapsulating/decapsulating blob of memory.
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <malloc.h>
10 #include <asm/byteorder.h>
11 #include <linux/compiler.h>
12 #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7)
13 #include <fsl_sec.h>
14 #include <asm/arch/clock.h>
15 #endif
16
17 /**
18  * blob_decap() - Decapsulate the data as a blob
19  * @key_mod:    - Pointer to key modifier/key
20  * @src:        - Address of data to be decapsulated
21  * @dst:        - Address of data to be decapsulated
22  * @len:        - Size of data to be decapsulated
23  *
24  * Returns zero on success,and negative on error.
25  */
26 __weak int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
27 {
28         return 0;
29 }
30
31 /**
32  * blob_encap() - Encapsulate the data as a blob
33  * @key_mod:    - Pointer to key modifier/key
34  * @src:        - Address of data to be encapsulated
35  * @dst:        - Address of data to be encapsulated
36  * @len:        - Size of data to be encapsulated
37  *
38  * Returns zero on success,and negative on error.
39  */
40 __weak int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
41 {
42         return 0;
43 }
44
45 /**
46  * do_blob() - Handle the "blob" command-line command
47  * @cmdtp:      Command data struct pointer
48  * @flag:       Command flag
49  * @argc:       Command-line argument count
50  * @argv:       Array of command-line arguments
51  *
52  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
53  * on error.
54  */
55 static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc,
56                    char *const argv[])
57 {
58         ulong key_addr, src_addr, dst_addr, len;
59         uint8_t *km_ptr, *src_ptr, *dst_ptr;
60         int enc, ret = 0;
61
62         if (argc != 6)
63                 return CMD_RET_USAGE;
64
65         if (!strncmp(argv[1], "enc", 3))
66                 enc = 1;
67         else if (!strncmp(argv[1], "dec", 3))
68                 enc = 0;
69         else
70                 return CMD_RET_USAGE;
71
72         src_addr = simple_strtoul(argv[2], NULL, 16);
73         dst_addr = simple_strtoul(argv[3], NULL, 16);
74         len = simple_strtoul(argv[4], NULL, 16);
75         key_addr = simple_strtoul(argv[5], NULL, 16);
76
77         km_ptr = (uint8_t *)(uintptr_t)key_addr;
78         src_ptr = (uint8_t *)(uintptr_t)src_addr;
79         dst_ptr = (uint8_t *)(uintptr_t)dst_addr;
80
81 #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7)
82
83         hab_caam_clock_enable(1);
84
85         u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR +
86                                    FSL_CAAM_ORSR_JRa_OFFSET);
87         if (out_jr_size != FSL_CAAM_MAX_JR_SIZE)
88                 sec_init();
89 #endif
90
91         if (enc)
92                 ret = blob_encap(km_ptr, src_ptr, dst_ptr, len);
93         else
94                 ret = blob_decap(km_ptr, src_ptr, dst_ptr, len);
95
96         return ret;
97 }
98
99 /***************************************************/
100 static char blob_help_text[] =
101         "enc src dst len km - Encapsulate and create blob of data\n"
102         "                          $len bytes long at address $src and\n"
103         "                          store the result at address $dst.\n"
104         "                          $km is the address where the key\n"
105         "                          modifier is stored.\n"
106         "                          The modifier is required for generation\n"
107         "                          /use as key for cryptographic operation.\n"
108         "                          Key modifier should be 16 byte long.\n"
109         "blob dec src dst len km - Decapsulate the  blob of data at address\n"
110         "                          $src and store result of $len byte at\n"
111         "                          addr $dst.\n"
112         "                          $km is the address where the key\n"
113         "                          modifier is stored.\n"
114         "                          The modifier is required for generation\n"
115         "                          /use as key for cryptographic operation.\n"
116         "                          Key modifier should be 16 byte long.\n";
117
118 U_BOOT_CMD(
119         blob, 6, 1, do_blob,
120         "Blob encapsulation/decryption",
121         blob_help_text
122 );