Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Command for encapsulating/decapsulating blob of memory. |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <asm/byteorder.h> |
| 11 | #include <linux/compiler.h> |
Breno Lima | c1957d4 | 2021-03-25 17:30:21 +0800 | [diff] [blame^] | 12 | #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) |
| 13 | #include <fsl_sec.h> |
| 14 | #include <asm/arch/clock.h> |
| 15 | #endif |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 16 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 17 | /** |
| 18 | * blob_decap() - Decapsulate the data as a blob |
| 19 | * @key_mod: - Pointer to key modifier/key |
| 20 | * @src: - Address of data to be decapsulated |
| 21 | * @dst: - Address of data to be decapsulated |
| 22 | * @len: - Size of data to be decapsulated |
| 23 | * |
| 24 | * Returns zero on success,and negative on error. |
| 25 | */ |
| 26 | __weak int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
| 27 | { |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * blob_encap() - Encapsulate the data as a blob |
| 33 | * @key_mod: - Pointer to key modifier/key |
| 34 | * @src: - Address of data to be encapsulated |
| 35 | * @dst: - Address of data to be encapsulated |
| 36 | * @len: - Size of data to be encapsulated |
| 37 | * |
| 38 | * Returns zero on success,and negative on error. |
| 39 | */ |
| 40 | __weak int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * do_blob() - Handle the "blob" command-line command |
| 47 | * @cmdtp: Command data struct pointer |
| 48 | * @flag: Command flag |
| 49 | * @argc: Command-line argument count |
| 50 | * @argv: Array of command-line arguments |
| 51 | * |
| 52 | * Returns zero on success, CMD_RET_USAGE in case of misuse and negative |
| 53 | * on error. |
| 54 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 55 | static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc, |
| 56 | char *const argv[]) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 57 | { |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 58 | ulong key_addr, src_addr, dst_addr, len; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 59 | uint8_t *km_ptr, *src_ptr, *dst_ptr; |
| 60 | int enc, ret = 0; |
| 61 | |
| 62 | if (argc != 6) |
| 63 | return CMD_RET_USAGE; |
| 64 | |
| 65 | if (!strncmp(argv[1], "enc", 3)) |
| 66 | enc = 1; |
| 67 | else if (!strncmp(argv[1], "dec", 3)) |
| 68 | enc = 0; |
| 69 | else |
| 70 | return CMD_RET_USAGE; |
| 71 | |
| 72 | src_addr = simple_strtoul(argv[2], NULL, 16); |
| 73 | dst_addr = simple_strtoul(argv[3], NULL, 16); |
| 74 | len = simple_strtoul(argv[4], NULL, 16); |
| 75 | key_addr = simple_strtoul(argv[5], NULL, 16); |
| 76 | |
Aneesh Bansal | b3e9820 | 2015-12-08 13:54:29 +0530 | [diff] [blame] | 77 | km_ptr = (uint8_t *)(uintptr_t)key_addr; |
| 78 | src_ptr = (uint8_t *)(uintptr_t)src_addr; |
| 79 | dst_ptr = (uint8_t *)(uintptr_t)dst_addr; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 80 | |
Breno Lima | c1957d4 | 2021-03-25 17:30:21 +0800 | [diff] [blame^] | 81 | #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) |
| 82 | |
| 83 | hab_caam_clock_enable(1); |
| 84 | |
| 85 | u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + |
| 86 | FSL_CAAM_ORSR_JRa_OFFSET); |
| 87 | if (out_jr_size != FSL_CAAM_MAX_JR_SIZE) |
| 88 | sec_init(); |
| 89 | #endif |
| 90 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 91 | if (enc) |
| 92 | ret = blob_encap(km_ptr, src_ptr, dst_ptr, len); |
| 93 | else |
| 94 | ret = blob_decap(km_ptr, src_ptr, dst_ptr, len); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | /***************************************************/ |
| 100 | static char blob_help_text[] = |
| 101 | "enc src dst len km - Encapsulate and create blob of data\n" |
| 102 | " $len bytes long at address $src and\n" |
| 103 | " store the result at address $dst.\n" |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 104 | " $km is the address where the key\n" |
| 105 | " modifier is stored.\n" |
| 106 | " The modifier is required for generation\n" |
| 107 | " /use as key for cryptographic operation.\n" |
| 108 | " Key modifier should be 16 byte long.\n" |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 109 | "blob dec src dst len km - Decapsulate the blob of data at address\n" |
| 110 | " $src and store result of $len byte at\n" |
| 111 | " addr $dst.\n" |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 112 | " $km is the address where the key\n" |
| 113 | " modifier is stored.\n" |
| 114 | " The modifier is required for generation\n" |
| 115 | " /use as key for cryptographic operation.\n" |
| 116 | " Key modifier should be 16 byte long.\n"; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 117 | |
| 118 | U_BOOT_CMD( |
| 119 | blob, 6, 1, do_blob, |
| 120 | "Blob encapsulation/decryption", |
| 121 | blob_help_text |
| 122 | ); |