blob: 7c77c410d5284e87d86e4227f4ac42a34241889d [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>
Franck LENORMAND88e7f5b2021-03-25 17:30:24 +080012#if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
Aymen Sghaier25324292021-03-25 17:30:27 +080013 defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
Breno Lima81d56052021-03-25 17:30:21 +080014#include <fsl_sec.h>
15#include <asm/arch/clock.h>
16#endif
Ruchika Guptac5de15c2014-10-07 15:46:20 +053017
Ruchika Guptac5de15c2014-10-07 15:46:20 +053018/**
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 */
Simon Glass09140112020-05-10 11:40:03 -060056static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc,
57 char *const argv[])
Ruchika Guptac5de15c2014-10-07 15:46:20 +053058{
Sumit Garg7fe1d6a2016-07-14 13:27:50 -040059 ulong key_addr, src_addr, dst_addr, len;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053060 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
Simon Glass7e5f4602021-07-24 09:03:29 -060073 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);
Ruchika Guptac5de15c2014-10-07 15:46:20 +053077
Aneesh Bansal9711f522015-12-08 13:54:29 +053078 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;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053081
Franck LENORMAND88e7f5b2021-03-25 17:30:24 +080082#if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
Aymen Sghaier25324292021-03-25 17:30:27 +080083 defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
Breno Lima81d56052021-03-25 17:30:21 +080084
85 hab_caam_clock_enable(1);
86
Tom Rini6cc04542022-10-28 20:27:13 -040087 u32 out_jr_size = sec_in32(CFG_SYS_FSL_JR0_ADDR +
Breno Lima81d56052021-03-25 17:30:21 +080088 FSL_CAAM_ORSR_JRa_OFFSET);
89 if (out_jr_size != FSL_CAAM_MAX_JR_SIZE)
90 sec_init();
91#endif
92
Ruchika Guptac5de15c2014-10-07 15:46:20 +053093 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/***************************************************/
102static 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"
gaurav rana7ee8c472015-02-25 09:37:09 +0530106 " $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"
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530111 "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"
gaurav rana7ee8c472015-02-25 09:37:09 +0530114 " $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";
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530119
120U_BOOT_CMD(
121 blob, 6, 1, do_blob,
122 "Blob encapsulation/decryption",
123 blob_help_text
124);