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