blob: a3c1dc49224d00f5616c7bc0f639ba19bb0b2743 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Gupta4345a572014-10-07 15:46:20 +05302/*
3 *
4 * Command for encapsulating/decapsulating blob of memory.
Ruchika Gupta4345a572014-10-07 15:46:20 +05305 */
6
Ruchika Gupta4345a572014-10-07 15:46:20 +05307#include <command.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +05308#include <malloc.h>
Tom Rini5c1444f2024-04-27 08:10:59 -06009#include <vsprintf.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +053010#include <asm/byteorder.h>
11#include <linux/compiler.h>
Franck LENORMANDff925e22021-03-25 17:30:24 +080012#if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
Aymen Sghaierf18c71f2021-03-25 17:30:27 +080013 defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
Breno Limac1957d42021-03-25 17:30:21 +080014#include <fsl_sec.h>
15#include <asm/arch/clock.h>
16#endif
Ruchika Gupta4345a572014-10-07 15:46:20 +053017
Ruchika Gupta4345a572014-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 Glassed38aef2020-05-10 11:40:03 -060056static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc,
57 char *const argv[])
Ruchika Gupta4345a572014-10-07 15:46:20 +053058{
Sumit Gargff9eb052016-07-14 13:27:50 -040059 ulong key_addr, src_addr, dst_addr, len;
Ruchika Gupta4345a572014-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 Glass3ff49ec2021-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 Gupta4345a572014-10-07 15:46:20 +053077
Aneesh Bansalb3e98202015-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 Gupta4345a572014-10-07 15:46:20 +053081
Franck LENORMANDff925e22021-03-25 17:30:24 +080082#if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
Aymen Sghaierf18c71f2021-03-25 17:30:27 +080083 defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
Breno Limac1957d42021-03-25 17:30:21 +080084
85 hab_caam_clock_enable(1);
86
Tom Rini376b88a2022-10-28 20:27:13 -040087 u32 out_jr_size = sec_in32(CFG_SYS_FSL_JR0_ADDR +
Breno Limac1957d42021-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 Gupta4345a572014-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 ranab64308e2015-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 Gupta4345a572014-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 ranab64308e2015-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 Gupta4345a572014-10-07 15:46:20 +0530119
120U_BOOT_CMD(
121 blob, 6, 1, do_blob,
122 "Blob encapsulation/decryption",
123 blob_help_text
124);