blob: 359c8940fb9aa4182244ab1aca151b7c3b6c0651 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptac5de15c2014-10-07 15:46:20 +05302/*
3 *
4 * Command for encapsulating/decapsulating blob of memory.
Ruchika Guptac5de15c2014-10-07 15:46:20 +05305 */
6
7#include <common.h>
8#include <command.h>
Ruchika Guptac5de15c2014-10-07 15:46:20 +05309#include <malloc.h>
10#include <asm/byteorder.h>
11#include <linux/compiler.h>
Breno Lima81d56052021-03-25 17:30:21 +080012#if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7)
13#include <fsl_sec.h>
14#include <asm/arch/clock.h>
15#endif
Ruchika Guptac5de15c2014-10-07 15:46:20 +053016
Ruchika Guptac5de15c2014-10-07 15:46:20 +053017/**
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 */
Simon Glass09140112020-05-10 11:40:03 -060055static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc,
56 char *const argv[])
Ruchika Guptac5de15c2014-10-07 15:46:20 +053057{
Sumit Garg7fe1d6a2016-07-14 13:27:50 -040058 ulong key_addr, src_addr, dst_addr, len;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053059 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
Aneesh Bansal9711f522015-12-08 13:54:29 +053077 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;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053080
Breno Lima81d56052021-03-25 17:30:21 +080081#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
Ruchika Guptac5de15c2014-10-07 15:46:20 +053091 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/***************************************************/
100static 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"
gaurav rana7ee8c472015-02-25 09:37:09 +0530104 " $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"
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530109 "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"
gaurav rana7ee8c472015-02-25 09:37:09 +0530112 " $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";
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530117
118U_BOOT_CMD(
119 blob, 6, 1, do_blob,
120 "Blob encapsulation/decryption",
121 blob_help_text
122);