mon.c 3.0 KB

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