semihosting.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
  4. */
  5. #ifndef _SEMIHOSTING_H
  6. #define _SEMIHOSTING_H
  7. /*
  8. * These are the encoded instructions used to indicate a semihosting trap. They
  9. * are named like SMH_ISA_INSN, where ISA is the instruction set (e.g.
  10. * AArch64), and INSN is the mneumonic for the instruction.
  11. */
  12. #define SMH_A64_HLT 0xD45E0000
  13. #define SMH_A32_SVC 0xEF123456
  14. #define SMH_A32_HLT 0xE10F0070
  15. #define SMH_T32_SVC 0xDFAB
  16. #define SMH_T32_HLT 0xBABC
  17. /**
  18. * smh_trap() - ARCH-specific semihosting call.
  19. *
  20. * Semihosting library/driver can use this function to do the
  21. * actual semihosting calls.
  22. *
  23. * Return: Error code defined by semihosting spec.
  24. */
  25. long smh_trap(unsigned int sysnum, void *addr);
  26. #if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)
  27. /**
  28. * semihosting_enabled() - Determine whether semihosting is supported
  29. *
  30. * Semihosting-based drivers should call this function before making other
  31. * semihosting calls.
  32. *
  33. * Return: %true if a debugger is attached which supports semihosting, %false
  34. * otherwise
  35. */
  36. bool semihosting_enabled(void);
  37. /**
  38. * disable_semihosting() - Cause semihosting_enabled() to return false
  39. *
  40. * If U-Boot ever receives an unhandled exception caused by a semihosting trap,
  41. * the trap handler should call this function.
  42. */
  43. void disable_semihosting(void);
  44. #else
  45. static inline bool semihosting_enabled(void)
  46. {
  47. return CONFIG_IS_ENABLED(SEMIHOSTING);
  48. }
  49. static inline void disable_semihosting(void)
  50. {
  51. }
  52. #endif
  53. /**
  54. * enum smh_open_mode - Numeric file modes for use with smh_open()
  55. * MODE_READ: 'r'
  56. * MODE_BINARY: 'b'
  57. * MODE_PLUS: '+'
  58. * MODE_WRITE: 'w'
  59. * MODE_APPEND: 'a'
  60. *
  61. * These modes represent the mode string used by fopen(3) in a form which can
  62. * be passed to smh_open(). These do NOT correspond directly to %O_RDONLY,
  63. * %O_CREAT, etc; see fopen(3) for details. In particular, @MODE_PLUS
  64. * effectively results in adding %O_RDWR, and @MODE_WRITE will add %O_TRUNC.
  65. * For compatibility, @MODE_BINARY should be added when opening non-text files
  66. * (such as images).
  67. */
  68. enum smh_open_mode {
  69. MODE_READ = 0x0,
  70. MODE_BINARY = 0x1,
  71. MODE_PLUS = 0x2,
  72. MODE_WRITE = 0x4,
  73. MODE_APPEND = 0x8,
  74. };
  75. /**
  76. * smh_open() - Open a file on the host
  77. * @fname: The name of the file to open
  78. * @mode: The mode to use when opening the file
  79. *
  80. * Return: Either a file descriptor or a negative error on failure
  81. */
  82. long smh_open(const char *fname, enum smh_open_mode mode);
  83. /**
  84. * smh_read() - Read data from a file
  85. * @fd: A file descriptor returned from smh_open()
  86. * @memp: Pointer to a buffer of memory of at least @len bytes
  87. * @len: The number of bytes to read
  88. *
  89. * Return:
  90. * * The number of bytes read on success, with 0 indicating %EOF
  91. * * A negative error on failure
  92. */
  93. long smh_read(long fd, void *memp, size_t len);
  94. /**
  95. * smh_write() - Write data to a file
  96. * @fd: A file descriptor returned from smh_open()
  97. * @memp: Pointer to a buffer of memory of at least @len bytes
  98. * @len: The number of bytes to read
  99. * @written: Pointer which will be updated with the actual bytes written
  100. *
  101. * Return: 0 on success or negative error on failure
  102. */
  103. long smh_write(long fd, const void *memp, size_t len, ulong *written);
  104. /**
  105. * smh_close() - Close an open file
  106. * @fd: A file descriptor returned from smh_open()
  107. *
  108. * Return: 0 on success or negative error on failure
  109. */
  110. long smh_close(long fd);
  111. /**
  112. * smh_flen() - Get the length of a file
  113. * @fd: A file descriptor returned from smh_open()
  114. *
  115. * Return: The length of the file, in bytes, or a negative error on failure
  116. */
  117. long smh_flen(long fd);
  118. /**
  119. * smh_seek() - Seek to a position in a file
  120. * @fd: A file descriptor returned from smh_open()
  121. * @pos: The offset (in bytes) to seek to
  122. *
  123. * Return: 0 on success or negative error on failure
  124. */
  125. long smh_seek(long fd, long pos);
  126. /**
  127. * smh_getc() - Read a character from stdin
  128. *
  129. * Return: The character read, or a negative error on failure
  130. */
  131. int smh_getc(void);
  132. /**
  133. * smh_putc() - Print a character on stdout
  134. * @ch: The character to print
  135. */
  136. void smh_putc(char ch);
  137. /**
  138. * smh_write0() - Print a nul-terminated string on stdout
  139. * @s: The string to print
  140. */
  141. void smh_puts(const char *s);
  142. #endif /* _SEMIHOSTING_H */