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