mon.c 3.1 KB

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