Siva Durga Prasad Paladugu | b1acb65 | 2018-02-28 13:26:53 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2018 Xilinx, Inc. |
| 3 | * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0 |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <asm/arch/sys_proto.h> |
| 11 | #include <asm/io.h> |
| 12 | |
| 13 | static int zynqmp_verify_secure(u8 *key_ptr, u8 *src_ptr, u32 len) |
| 14 | { |
| 15 | int ret; |
| 16 | u32 src_lo, src_hi; |
| 17 | u32 key_lo = 0; |
| 18 | u32 key_hi = 0; |
| 19 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 20 | u64 addr; |
| 21 | |
| 22 | if ((ulong)src_ptr != ALIGN((ulong)src_ptr, |
| 23 | CONFIG_SYS_CACHELINE_SIZE)) { |
| 24 | printf("Failed: source address not aligned:%p\n", src_ptr); |
| 25 | return -EINVAL; |
| 26 | } |
| 27 | |
| 28 | src_lo = lower_32_bits((ulong)src_ptr); |
| 29 | src_hi = upper_32_bits((ulong)src_ptr); |
| 30 | flush_dcache_range((ulong)src_ptr, (ulong)(src_ptr + len)); |
| 31 | |
| 32 | if (key_ptr) { |
| 33 | key_lo = lower_32_bits((ulong)key_ptr); |
| 34 | key_hi = upper_32_bits((ulong)key_ptr); |
| 35 | flush_dcache_range((ulong)key_ptr, |
| 36 | (ulong)(key_ptr + KEY_PTR_LEN)); |
| 37 | } |
| 38 | |
| 39 | ret = invoke_smc(ZYNQMP_SIP_SVC_PM_SECURE_IMG_LOAD, src_lo, src_hi, |
| 40 | key_lo, key_hi, ret_payload); |
| 41 | if (ret) { |
| 42 | printf("Failed: secure op status:0x%x\n", ret); |
| 43 | } else { |
| 44 | addr = (u64)ret_payload[1] << 32 | ret_payload[2]; |
| 45 | printf("Verified image at 0x%llx\n", addr); |
| 46 | env_set_hex("zynqmp_verified_img_addr", addr); |
| 47 | } |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * do_zynqmp - Handle the "zynqmp" command-line command |
| 54 | * @cmdtp: Command data struct pointer |
| 55 | * @flag: Command flag |
| 56 | * @argc: Command-line argument count |
| 57 | * @argv: Array of command-line arguments |
| 58 | * |
| 59 | * Processes the zynqmp specific commands |
| 60 | * |
| 61 | * Return: return 0 on success and CMD_RET_USAGE incase of misuse and error |
| 62 | */ |
| 63 | static int do_zynqmp(cmd_tbl_t *cmdtp, int flag, int argc, |
| 64 | char *const argv[]) |
| 65 | { |
| 66 | u64 src_addr; |
| 67 | u32 len; |
| 68 | u8 *key_ptr = NULL; |
| 69 | u8 *src_ptr; |
| 70 | int ret; |
| 71 | |
| 72 | if (argc > 5 || argc < 4 || strncmp(argv[1], "secure", 6)) |
| 73 | return CMD_RET_USAGE; |
| 74 | |
| 75 | src_addr = simple_strtoull(argv[2], NULL, 16); |
| 76 | |
| 77 | len = simple_strtoul(argv[3], NULL, 16); |
| 78 | |
| 79 | if (argc > 4) |
| 80 | key_ptr = (uint8_t *)(uintptr_t)simple_strtoull(argv[4], |
| 81 | NULL, 16); |
| 82 | |
| 83 | src_ptr = (uint8_t *)(uintptr_t)src_addr; |
| 84 | |
| 85 | ret = zynqmp_verify_secure(key_ptr, src_ptr, len); |
| 86 | if (ret) |
| 87 | return CMD_RET_FAILURE; |
| 88 | |
| 89 | return CMD_RET_SUCCESS; |
| 90 | } |
| 91 | |
| 92 | /***************************************************/ |
| 93 | #ifdef CONFIG_SYS_LONGHELP |
| 94 | static char zynqmp_help_text[] = |
| 95 | "secure src len [key_addr] - verifies secure images of $len bytes\n" |
| 96 | " long at address $src. Optional key_addr\n" |
| 97 | " can be specified if user key needs to\n" |
| 98 | " be used for decryption\n"; |
| 99 | #endif |
| 100 | |
| 101 | U_BOOT_CMD( |
| 102 | zynqmp, 5, 1, do_zynqmp, |
| 103 | "Verify and load secure images", |
| 104 | zynqmp_help_text |
| 105 | ) |