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