Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008-2015 Freescale Semiconductor, Inc. |
| 4 | * |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 5 | * Command for encapsulating DEK blob |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <asm/byteorder.h> |
| 13 | #include <linux/compiler.h> |
| 14 | #include <fsl_sec.h> |
| 15 | #include <asm/arch/clock.h> |
Ulises Cardenas | 817ffc0 | 2015-05-04 09:53:28 -0500 | [diff] [blame] | 16 | #include <mapmem.h> |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 17 | |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 18 | /** |
| 19 | * blob_dek() - Encapsulate the DEK as a blob using CAM's Key |
| 20 | * @src: - Address of data to be encapsulated |
| 21 | * @dst: - Desination address of encapsulated data |
| 22 | * @len: - Size of data to be encapsulated |
| 23 | * |
| 24 | * Returns zero on success,and negative on error. |
| 25 | */ |
| 26 | static int blob_encap_dek(const u8 *src, u8 *dst, u32 len) |
| 27 | { |
| 28 | int ret = 0; |
| 29 | u32 jr_size = 4; |
| 30 | |
| 31 | u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c); |
| 32 | if (out_jr_size != jr_size) { |
| 33 | hab_caam_clock_enable(1); |
| 34 | sec_init(); |
| 35 | } |
| 36 | |
| 37 | if (!((len == 128) | (len == 192) | (len == 256))) { |
| 38 | debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n"); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | len /= 8; |
| 43 | ret = blob_dek(src, dst, len); |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * do_dek_blob() - Handle the "dek_blob" command-line command |
| 50 | * @cmdtp: Command data struct pointer |
| 51 | * @flag: Command flag |
| 52 | * @argc: Command-line argument count |
| 53 | * @argv: Array of command-line arguments |
| 54 | * |
| 55 | * Returns zero on success, CMD_RET_USAGE in case of misuse and negative |
| 56 | * on error. |
| 57 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 58 | static int do_dek_blob(struct cmd_tbl *cmdtp, int flag, int argc, |
| 59 | char *const argv[]) |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 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 | ); |