mon.c 2.9 KB

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