Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 2 | /* |
Andrew F. Davis | c1c564a | 2019-07-16 09:49:38 -0400 | [diff] [blame] | 3 | * K2x: Secure commands file |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 4 | * |
Andrew F. Davis | c1c564a | 2019-07-16 09:49:38 -0400 | [diff] [blame] | 5 | * Copyright (C) 2012-2019 Texas Instruments Incorporated - http://www.ti.com/ |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 8 | #include <hang.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 9 | #include <image.h> |
Andrew F. Davis | c1c564a | 2019-07-16 09:49:38 -0400 | [diff] [blame] | 10 | #include <asm/unaligned.h> |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <mach/mon.h> |
Vitaly Andrianov | e8d740f | 2017-04-07 10:00:03 -0500 | [diff] [blame] | 14 | #include <spl.h> |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 15 | asm(".arch_extension sec\n\t"); |
| 16 | |
Madan Srinivas | 1d73ce6 | 2017-07-17 12:59:15 -0500 | [diff] [blame] | 17 | int mon_install(u32 addr, u32 dpsc, u32 freq, u32 bm_addr) |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 18 | { |
| 19 | int result; |
| 20 | |
| 21 | __asm__ __volatile__ ( |
| 22 | "stmfd r13!, {lr}\n" |
| 23 | "mov r0, %1\n" |
| 24 | "mov r1, %2\n" |
| 25 | "mov r2, %3\n" |
Madan Srinivas | 1d73ce6 | 2017-07-17 12:59:15 -0500 | [diff] [blame] | 26 | "mov r3, %4\n" |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 27 | "blx r0\n" |
Srinivas, Madan | 94f536f | 2017-07-17 13:02:02 -0500 | [diff] [blame] | 28 | "mov %0, r0\n" |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 29 | "ldmfd r13!, {lr}\n" |
| 30 | : "=&r" (result) |
Madan Srinivas | 1d73ce6 | 2017-07-17 12:59:15 -0500 | [diff] [blame] | 31 | : "r" (addr), "r" (dpsc), "r" (freq), "r" (bm_addr) |
| 32 | : "cc", "r0", "r1", "r2", "r3", "memory"); |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 33 | return result; |
| 34 | } |
| 35 | |
| 36 | int mon_power_on(int core_id, void *ep) |
| 37 | { |
| 38 | int result; |
| 39 | |
| 40 | asm volatile ( |
| 41 | "stmfd r13!, {lr}\n" |
| 42 | "mov r1, %1\n" |
| 43 | "mov r2, %2\n" |
| 44 | "mov r0, #0\n" |
| 45 | "smc #0\n" |
Srinivas, Madan | 94f536f | 2017-07-17 13:02:02 -0500 | [diff] [blame] | 46 | "mov %0, r0\n" |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 47 | "ldmfd r13!, {lr}\n" |
| 48 | : "=&r" (result) |
| 49 | : "r" (core_id), "r" (ep) |
| 50 | : "cc", "r0", "r1", "r2", "memory"); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | int mon_power_off(int core_id) |
| 55 | { |
| 56 | int result; |
| 57 | |
| 58 | asm volatile ( |
| 59 | "stmfd r13!, {lr}\n" |
| 60 | "mov r1, %1\n" |
| 61 | "mov r0, #1\n" |
| 62 | "smc #1\n" |
Srinivas, Madan | 94f536f | 2017-07-17 13:02:02 -0500 | [diff] [blame] | 63 | "mov %0, r0\n" |
Tom Rini | aadd336 | 2016-03-16 09:10:08 -0400 | [diff] [blame] | 64 | "ldmfd r13!, {lr}\n" |
| 65 | : "=&r" (result) |
| 66 | : "r" (core_id) |
| 67 | : "cc", "r0", "r1", "memory"); |
| 68 | return result; |
| 69 | } |
Vitaly Andrianov | e8d740f | 2017-04-07 10:00:03 -0500 | [diff] [blame] | 70 | |
| 71 | #ifdef CONFIG_TI_SECURE_DEVICE |
| 72 | #define KS2_HS_SEC_HEADER_LEN 0x60 |
| 73 | #define KS2_HS_SEC_TAG_OFFSET 0x34 |
| 74 | #define KS2_AUTH_CMD 130 |
| 75 | |
| 76 | /** |
| 77 | * k2_hs_bm_auth() - Invokes security functions using a |
| 78 | * proprietary TI interface. This binary and source for |
| 79 | * this is available in the secure development package or |
| 80 | * SECDEV. For details on how to access this please refer |
| 81 | * doc/README.ti-secure |
| 82 | * |
| 83 | * @cmd: Secure monitor command |
| 84 | * @arg1: Argument for command |
| 85 | * |
| 86 | * returns non-zero value on success, zero on error |
| 87 | */ |
| 88 | static int k2_hs_bm_auth(int cmd, void *arg1) |
| 89 | { |
| 90 | int result; |
| 91 | |
| 92 | asm volatile ( |
| 93 | "stmfd r13!, {r4-r12, lr}\n" |
| 94 | "mov r0, %1\n" |
| 95 | "mov r1, %2\n" |
| 96 | "smc #2\n" |
Srinivas, Madan | 94f536f | 2017-07-17 13:02:02 -0500 | [diff] [blame] | 97 | "mov %0, r0\n" |
Vitaly Andrianov | e8d740f | 2017-04-07 10:00:03 -0500 | [diff] [blame] | 98 | "ldmfd r13!, {r4-r12, lr}\n" |
| 99 | : "=&r" (result) |
| 100 | : "r" (cmd), "r" (arg1) |
| 101 | : "cc", "r0", "r1", "memory"); |
| 102 | |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | void board_fit_image_post_process(void **p_image, size_t *p_size) |
| 107 | { |
| 108 | int result = 0; |
| 109 | void *image = *p_image; |
| 110 | |
| 111 | if (strncmp(image + KS2_HS_SEC_TAG_OFFSET, "KEYS", 4)) { |
| 112 | printf("No signature found in image!\n"); |
| 113 | hang(); |
| 114 | } |
| 115 | |
| 116 | result = k2_hs_bm_auth(KS2_AUTH_CMD, image); |
| 117 | if (result == 0) { |
| 118 | printf("Authentication failed!\n"); |
| 119 | hang(); |
| 120 | } |
| 121 | |
| 122 | /* |
Andrew F. Davis | 9e58d4d | 2017-06-29 08:38:25 -0500 | [diff] [blame] | 123 | * Overwrite the image headers after authentication |
| 124 | * and decryption. Update size to reflect removal |
Andrew F. Davis | c1c564a | 2019-07-16 09:49:38 -0400 | [diff] [blame] | 125 | * of header and restore original file size. |
Andrew F. Davis | 9e58d4d | 2017-06-29 08:38:25 -0500 | [diff] [blame] | 126 | */ |
Andrew F. Davis | c1c564a | 2019-07-16 09:49:38 -0400 | [diff] [blame] | 127 | *p_size = get_unaligned_le32(image + (*p_size - 4)); |
Andrew F. Davis | 9e58d4d | 2017-06-29 08:38:25 -0500 | [diff] [blame] | 128 | memcpy(image, image + KS2_HS_SEC_HEADER_LEN, *p_size); |
Vitaly Andrianov | e8d740f | 2017-04-07 10:00:03 -0500 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Output notification of successful authentication to re-assure the |
| 132 | * user that the secure code is being processed as expected. However |
| 133 | * suppress any such log output in case of building for SPL and booting |
| 134 | * via YMODEM. This is done to avoid disturbing the YMODEM serial |
| 135 | * protocol transactions. |
| 136 | */ |
| 137 | if (!(IS_ENABLED(CONFIG_SPL_BUILD) && |
| 138 | IS_ENABLED(CONFIG_SPL_YMODEM_SUPPORT) && |
| 139 | spl_boot_device() == BOOT_DEVICE_UART)) |
| 140 | printf("Authentication passed\n"); |
| 141 | } |
| 142 | #endif |