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