semihosting.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
  4. * Copyright 2014 Broadcom Corporation
  5. */
  6. #include <common.h>
  7. #include <log.h>
  8. #include <semihosting.h>
  9. #define SYSOPEN 0x01
  10. #define SYSCLOSE 0x02
  11. #define SYSWRITEC 0x03
  12. #define SYSWRITE0 0x04
  13. #define SYSWRITE 0x05
  14. #define SYSREAD 0x06
  15. #define SYSREADC 0x07
  16. #define SYSISERROR 0x08
  17. #define SYSSEEK 0x0A
  18. #define SYSFLEN 0x0C
  19. #define SYSERRNO 0x13
  20. #if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)
  21. static bool _semihosting_enabled = true;
  22. static bool try_semihosting = true;
  23. bool semihosting_enabled(void)
  24. {
  25. if (try_semihosting) {
  26. smh_trap(SYSERRNO, NULL);
  27. try_semihosting = false;
  28. }
  29. return _semihosting_enabled;
  30. }
  31. void disable_semihosting(void)
  32. {
  33. _semihosting_enabled = false;
  34. }
  35. #endif
  36. /**
  37. * smh_errno() - Read the host's errno
  38. *
  39. * This gets the value of the host's errno and negates it. The host's errno may
  40. * or may not be set, so only call this function if a previous semihosting call
  41. * has failed.
  42. *
  43. * Return: a negative error value
  44. */
  45. static int smh_errno(void)
  46. {
  47. long ret = smh_trap(SYSERRNO, NULL);
  48. if (ret > 0 && ret < INT_MAX)
  49. return -ret;
  50. return -EIO;
  51. }
  52. long smh_open(const char *fname, enum smh_open_mode mode)
  53. {
  54. long fd;
  55. struct smh_open_s {
  56. const char *fname;
  57. unsigned long mode;
  58. size_t len;
  59. } open;
  60. debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode);
  61. open.fname = fname;
  62. open.len = strlen(fname);
  63. open.mode = mode;
  64. /* Open the file on the host */
  65. fd = smh_trap(SYSOPEN, &open);
  66. if (fd == -1)
  67. return smh_errno();
  68. return fd;
  69. }
  70. /**
  71. * struct smg_rdwr_s - Arguments for read and write
  72. * @fd: A file descriptor returned from smh_open()
  73. * @memp: Pointer to a buffer of memory of at least @len bytes
  74. * @len: The number of bytes to read or write
  75. */
  76. struct smh_rdwr_s {
  77. long fd;
  78. void *memp;
  79. size_t len;
  80. };
  81. long smh_read(long fd, void *memp, size_t len)
  82. {
  83. long ret;
  84. struct smh_rdwr_s read;
  85. debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
  86. read.fd = fd;
  87. read.memp = memp;
  88. read.len = len;
  89. ret = smh_trap(SYSREAD, &read);
  90. if (ret < 0)
  91. return smh_errno();
  92. return len - ret;
  93. }
  94. long smh_write(long fd, const void *memp, size_t len, ulong *written)
  95. {
  96. long ret;
  97. struct smh_rdwr_s write;
  98. debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
  99. write.fd = fd;
  100. write.memp = (void *)memp;
  101. write.len = len;
  102. ret = smh_trap(SYSWRITE, &write);
  103. *written = len - ret;
  104. if (ret)
  105. return smh_errno();
  106. return 0;
  107. }
  108. long smh_close(long fd)
  109. {
  110. long ret;
  111. debug("%s: fd %ld\n", __func__, fd);
  112. ret = smh_trap(SYSCLOSE, &fd);
  113. if (ret == -1)
  114. return smh_errno();
  115. return 0;
  116. }
  117. long smh_flen(long fd)
  118. {
  119. long ret;
  120. debug("%s: fd %ld\n", __func__, fd);
  121. ret = smh_trap(SYSFLEN, &fd);
  122. if (ret == -1)
  123. return smh_errno();
  124. return ret;
  125. }
  126. long smh_seek(long fd, long pos)
  127. {
  128. long ret;
  129. struct smh_seek_s {
  130. long fd;
  131. long pos;
  132. } seek;
  133. debug("%s: fd %ld pos %ld\n", __func__, fd, pos);
  134. seek.fd = fd;
  135. seek.pos = pos;
  136. ret = smh_trap(SYSSEEK, &seek);
  137. if (ret)
  138. return smh_errno();
  139. return 0;
  140. }
  141. int smh_getc(void)
  142. {
  143. return smh_trap(SYSREADC, NULL);
  144. }
  145. void smh_putc(char ch)
  146. {
  147. smh_trap(SYSWRITEC, &ch);
  148. }
  149. void smh_puts(const char *s)
  150. {
  151. smh_trap(SYSWRITE0, (char *)s);
  152. }