mon.c 3.0 KB

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