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