1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2552a848eSStefano Babic /*
3552a848eSStefano Babic * Copyright 2008-2015 Freescale Semiconductor, Inc.
4552a848eSStefano Babic *
5552a848eSStefano Babic * Command for encapsulating DEK blob
6552a848eSStefano Babic */
7552a848eSStefano Babic
8552a848eSStefano Babic #include <common.h>
9552a848eSStefano Babic #include <command.h>
10552a848eSStefano Babic #include <environment.h>
11552a848eSStefano Babic #include <malloc.h>
12552a848eSStefano Babic #include <asm/byteorder.h>
13552a848eSStefano Babic #include <linux/compiler.h>
14552a848eSStefano Babic #include <fsl_sec.h>
15552a848eSStefano Babic #include <asm/arch/clock.h>
16552a848eSStefano Babic #include <mapmem.h>
17552a848eSStefano Babic
18552a848eSStefano Babic /**
19552a848eSStefano Babic * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
20552a848eSStefano Babic * @src: - Address of data to be encapsulated
21552a848eSStefano Babic * @dst: - Desination address of encapsulated data
22552a848eSStefano Babic * @len: - Size of data to be encapsulated
23552a848eSStefano Babic *
24552a848eSStefano Babic * Returns zero on success,and negative on error.
25552a848eSStefano Babic */
blob_encap_dek(const u8 * src,u8 * dst,u32 len)26552a848eSStefano Babic static int blob_encap_dek(const u8 *src, u8 *dst, u32 len)
27552a848eSStefano Babic {
28552a848eSStefano Babic int ret = 0;
29552a848eSStefano Babic u32 jr_size = 4;
30552a848eSStefano Babic
31552a848eSStefano Babic u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
32552a848eSStefano Babic if (out_jr_size != jr_size) {
33552a848eSStefano Babic hab_caam_clock_enable(1);
34552a848eSStefano Babic sec_init();
35552a848eSStefano Babic }
36552a848eSStefano Babic
37552a848eSStefano Babic if (!((len == 128) | (len == 192) | (len == 256))) {
38552a848eSStefano Babic debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
39552a848eSStefano Babic return -1;
40552a848eSStefano Babic }
41552a848eSStefano Babic
42552a848eSStefano Babic len /= 8;
43552a848eSStefano Babic ret = blob_dek(src, dst, len);
44552a848eSStefano Babic
45552a848eSStefano Babic return ret;
46552a848eSStefano Babic }
47552a848eSStefano Babic
48552a848eSStefano Babic /**
49552a848eSStefano Babic * do_dek_blob() - Handle the "dek_blob" command-line command
50552a848eSStefano Babic * @cmdtp: Command data struct pointer
51552a848eSStefano Babic * @flag: Command flag
52552a848eSStefano Babic * @argc: Command-line argument count
53552a848eSStefano Babic * @argv: Array of command-line arguments
54552a848eSStefano Babic *
55552a848eSStefano Babic * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
56552a848eSStefano Babic * on error.
57552a848eSStefano Babic */
do_dek_blob(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])58552a848eSStefano Babic static int do_dek_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
59552a848eSStefano Babic {
60552a848eSStefano Babic uint32_t src_addr, dst_addr, len;
61552a848eSStefano Babic uint8_t *src_ptr, *dst_ptr;
62552a848eSStefano Babic int ret = 0;
63552a848eSStefano Babic
64552a848eSStefano Babic if (argc != 4)
65552a848eSStefano Babic return CMD_RET_USAGE;
66552a848eSStefano Babic
67552a848eSStefano Babic src_addr = simple_strtoul(argv[1], NULL, 16);
68552a848eSStefano Babic dst_addr = simple_strtoul(argv[2], NULL, 16);
69552a848eSStefano Babic len = simple_strtoul(argv[3], NULL, 10);
70552a848eSStefano Babic
71552a848eSStefano Babic src_ptr = map_sysmem(src_addr, len/8);
72552a848eSStefano Babic dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len/8));
73552a848eSStefano Babic
74552a848eSStefano Babic ret = blob_encap_dek(src_ptr, dst_ptr, len);
75552a848eSStefano Babic
76552a848eSStefano Babic return ret;
77552a848eSStefano Babic }
78552a848eSStefano Babic
79552a848eSStefano Babic /***************************************************/
80552a848eSStefano Babic static char dek_blob_help_text[] =
81552a848eSStefano Babic "src dst len - Encapsulate and create blob of data\n"
82552a848eSStefano Babic " $len bits long at address $src and\n"
83552a848eSStefano Babic " store the result at address $dst.\n";
84552a848eSStefano Babic
85552a848eSStefano Babic U_BOOT_CMD(
86552a848eSStefano Babic dek_blob, 4, 1, do_dek_blob,
87552a848eSStefano Babic "Data Encryption Key blob encapsulation",
88552a848eSStefano Babic dek_blob_help_text
89552a848eSStefano Babic );
90