xref: /openbmc/u-boot/cmd/blob.c (revision d9b23e26)
1 /*
2  *
3  * Command for encapsulating/decapsulating blob of memory.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <command.h>
10 #include <environment.h>
11 #include <malloc.h>
12 #include <asm/byteorder.h>
13 #include <linux/compiler.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
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(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
56 {
57 	ulong key_addr, src_addr, dst_addr, len;
58 	uint8_t *km_ptr, *src_ptr, *dst_ptr;
59 	int enc, ret = 0;
60 
61 	if (argc != 6)
62 		return CMD_RET_USAGE;
63 
64 	if (!strncmp(argv[1], "enc", 3))
65 		enc = 1;
66 	else if (!strncmp(argv[1], "dec", 3))
67 		enc = 0;
68 	else
69 		return CMD_RET_USAGE;
70 
71 	src_addr = simple_strtoul(argv[2], NULL, 16);
72 	dst_addr = simple_strtoul(argv[3], NULL, 16);
73 	len = simple_strtoul(argv[4], NULL, 16);
74 	key_addr = simple_strtoul(argv[5], NULL, 16);
75 
76 	km_ptr = (uint8_t *)(uintptr_t)key_addr;
77 	src_ptr = (uint8_t *)(uintptr_t)src_addr;
78 	dst_ptr = (uint8_t *)(uintptr_t)dst_addr;
79 
80 	if (enc)
81 		ret = blob_encap(km_ptr, src_ptr, dst_ptr, len);
82 	else
83 		ret = blob_decap(km_ptr, src_ptr, dst_ptr, len);
84 
85 	return ret;
86 }
87 
88 /***************************************************/
89 static char blob_help_text[] =
90 	"enc src dst len km - Encapsulate and create blob of data\n"
91 	"                          $len bytes long at address $src and\n"
92 	"                          store the result at address $dst.\n"
93 	"                          $km is the address where the key\n"
94 	"                          modifier is stored.\n"
95 	"                          The modifier is required for generation\n"
96 	"                          /use as key for cryptographic operation.\n"
97 	"                          Key modifier should be 16 byte long.\n"
98 	"blob dec src dst len km - Decapsulate the  blob of data at address\n"
99 	"                          $src and store result of $len byte at\n"
100 	"                          addr $dst.\n"
101 	"                          $km is the address where the key\n"
102 	"                          modifier is stored.\n"
103 	"                          The modifier is required for generation\n"
104 	"                          /use as key for cryptographic operation.\n"
105 	"                          Key modifier should be 16 byte long.\n";
106 
107 U_BOOT_CMD(
108 	blob, 6, 1, do_blob,
109 	"Blob encapsulation/decryption",
110 	blob_help_text
111 );
112