Merge git://git.denx.de/u-boot-imx
[oweals/u-boot.git] / arch / arm / mach-imx / cmd_dek.c
1 /*
2  * Copyright 2008-2015 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier: GPL-2.0+
5  *
6  * Command for encapsulating DEK blob
7  */
8
9 #include <common.h>
10 #include <command.h>
11 #include <environment.h>
12 #include <malloc.h>
13 #include <asm/byteorder.h>
14 #include <linux/compiler.h>
15 #include <fsl_sec.h>
16 #include <asm/arch/clock.h>
17 #include <mapmem.h>
18
19 /**
20 * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
21 * @src: - Address of data to be encapsulated
22 * @dst: - Desination address of encapsulated data
23 * @len: - Size of data to be encapsulated
24 *
25 * Returns zero on success,and negative on error.
26 */
27 static int blob_encap_dek(const u8 *src, u8 *dst, u32 len)
28 {
29         int ret = 0;
30         u32 jr_size = 4;
31
32         u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
33         if (out_jr_size != jr_size) {
34                 hab_caam_clock_enable(1);
35                 sec_init();
36         }
37
38         if (!((len == 128) | (len == 192) | (len == 256))) {
39                 debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
40                 return -1;
41         }
42
43         len /= 8;
44         ret = blob_dek(src, dst, len);
45
46         return ret;
47 }
48
49 /**
50  * do_dek_blob() - Handle the "dek_blob" command-line command
51  * @cmdtp:  Command data struct pointer
52  * @flag:   Command flag
53  * @argc:   Command-line argument count
54  * @argv:   Array of command-line arguments
55  *
56  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
57  * on error.
58  */
59 static int do_dek_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
60 {
61         uint32_t src_addr, dst_addr, len;
62         uint8_t *src_ptr, *dst_ptr;
63         int ret = 0;
64
65         if (argc != 4)
66                 return CMD_RET_USAGE;
67
68         src_addr = simple_strtoul(argv[1], NULL, 16);
69         dst_addr = simple_strtoul(argv[2], NULL, 16);
70         len = simple_strtoul(argv[3], NULL, 10);
71
72         src_ptr = map_sysmem(src_addr, len/8);
73         dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len/8));
74
75         ret = blob_encap_dek(src_ptr, dst_ptr, len);
76
77         return ret;
78 }
79
80 /***************************************************/
81 static char dek_blob_help_text[] =
82         "src dst len            - Encapsulate and create blob of data\n"
83         "                         $len bits long at address $src and\n"
84         "                         store the result at address $dst.\n";
85
86 U_BOOT_CMD(
87         dek_blob, 4, 1, do_dek_blob,
88         "Data Encryption Key blob encapsulation",
89         dek_blob_help_text
90 );