semihosting.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <lib/semihosting.h>
  10. #ifndef SEMIHOSTING_SUPPORTED
  11. #define SEMIHOSTING_SUPPORTED 1
  12. #endif
  13. long semihosting_call(unsigned long operation, uintptr_t system_block_address);
  14. typedef struct {
  15. const char *file_name;
  16. unsigned long mode;
  17. size_t name_length;
  18. } smh_file_open_block_t;
  19. typedef struct {
  20. long handle;
  21. uintptr_t buffer;
  22. size_t length;
  23. } smh_file_read_write_block_t;
  24. typedef struct {
  25. long handle;
  26. ssize_t location;
  27. } smh_file_seek_block_t;
  28. typedef struct {
  29. char *command_line;
  30. size_t command_length;
  31. } smh_system_block_t;
  32. long semihosting_connection_supported(void)
  33. {
  34. return SEMIHOSTING_SUPPORTED;
  35. }
  36. long semihosting_file_open(const char *file_name, size_t mode)
  37. {
  38. smh_file_open_block_t open_block;
  39. open_block.file_name = file_name;
  40. open_block.mode = mode;
  41. open_block.name_length = strlen(file_name);
  42. return semihosting_call(SEMIHOSTING_SYS_OPEN, (uintptr_t)&open_block);
  43. }
  44. long semihosting_file_seek(long file_handle, ssize_t offset)
  45. {
  46. smh_file_seek_block_t seek_block;
  47. long result;
  48. seek_block.handle = file_handle;
  49. seek_block.location = offset;
  50. result = semihosting_call(SEMIHOSTING_SYS_SEEK, (uintptr_t)&seek_block);
  51. if (result < 0) {
  52. result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0);
  53. } else {
  54. result = 0;
  55. }
  56. return result;
  57. }
  58. long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
  59. {
  60. smh_file_read_write_block_t read_block;
  61. long result = -EINVAL;
  62. if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
  63. return result;
  64. }
  65. read_block.handle = file_handle;
  66. read_block.buffer = buffer;
  67. read_block.length = *length;
  68. result = semihosting_call(SEMIHOSTING_SYS_READ, (uintptr_t)&read_block);
  69. if (result == *length) {
  70. return -EINVAL;
  71. } else if (result < *length) {
  72. *length -= result;
  73. return 0;
  74. } else {
  75. return result;
  76. }
  77. }
  78. long semihosting_file_write(long file_handle, size_t *length,
  79. const uintptr_t buffer)
  80. {
  81. smh_file_read_write_block_t write_block;
  82. long result = -EINVAL;
  83. if ((length == NULL) || (buffer == (uintptr_t)NULL)) {
  84. return -EINVAL;
  85. }
  86. write_block.handle = file_handle;
  87. write_block.buffer = (uintptr_t)buffer; /* cast away const */
  88. write_block.length = *length;
  89. result = semihosting_call(SEMIHOSTING_SYS_WRITE,
  90. (uintptr_t)&write_block);
  91. *length = result;
  92. return (result == 0) ? 0 : -EINVAL;
  93. }
  94. long semihosting_file_close(long file_handle)
  95. {
  96. return semihosting_call(SEMIHOSTING_SYS_CLOSE, (uintptr_t)&file_handle);
  97. }
  98. long semihosting_file_length(long file_handle)
  99. {
  100. return semihosting_call(SEMIHOSTING_SYS_FLEN, (uintptr_t)&file_handle);
  101. }
  102. char semihosting_read_char(void)
  103. {
  104. return semihosting_call(SEMIHOSTING_SYS_READC, 0);
  105. }
  106. void semihosting_write_char(char character)
  107. {
  108. semihosting_call(SEMIHOSTING_SYS_WRITEC, (uintptr_t)&character);
  109. }
  110. void semihosting_write_string(char *string)
  111. {
  112. semihosting_call(SEMIHOSTING_SYS_WRITE0, (uintptr_t)string);
  113. }
  114. long semihosting_system(char *command_line)
  115. {
  116. smh_system_block_t system_block;
  117. system_block.command_line = command_line;
  118. system_block.command_length = strlen(command_line);
  119. return semihosting_call(SEMIHOSTING_SYS_SYSTEM,
  120. (uintptr_t)&system_block);
  121. }
  122. long semihosting_get_flen(const char *file_name)
  123. {
  124. long file_handle;
  125. long length;
  126. assert(semihosting_connection_supported() != 0);
  127. file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
  128. if (file_handle == -1) {
  129. return file_handle;
  130. }
  131. /* Find the length of the file */
  132. length = semihosting_file_length(file_handle);
  133. return (semihosting_file_close(file_handle) != 0) ? -1 : length;
  134. }
  135. long semihosting_download_file(const char *file_name,
  136. size_t buf_size,
  137. uintptr_t buf)
  138. {
  139. long ret = -EINVAL;
  140. size_t length;
  141. long file_handle;
  142. /* Null pointer check */
  143. if (buf == 0U) {
  144. return ret;
  145. }
  146. assert(semihosting_connection_supported() != 0);
  147. file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
  148. if (file_handle == -1) {
  149. return ret;
  150. }
  151. /* Find the actual length of the file */
  152. length = semihosting_file_length(file_handle);
  153. if (length == (size_t)(-1)) {
  154. goto semihosting_fail;
  155. }
  156. /* Signal error if we do not have enough space for the file */
  157. if (length > buf_size) {
  158. goto semihosting_fail;
  159. }
  160. /*
  161. * A successful read will return 0 in which case we pass back
  162. * the actual number of bytes read. Else we pass a negative
  163. * value indicating an error.
  164. */
  165. ret = semihosting_file_read(file_handle, &length, buf);
  166. if (ret != 0) {
  167. goto semihosting_fail;
  168. } else {
  169. ret = (long)length;
  170. }
  171. semihosting_fail:
  172. semihosting_file_close(file_handle);
  173. return ret;
  174. }
  175. void semihosting_exit(uint32_t reason, uint32_t subcode)
  176. {
  177. #ifdef __aarch64__
  178. uint64_t parameters[] = {reason, subcode};
  179. (void)semihosting_call(SEMIHOSTING_SYS_EXIT, (uintptr_t)&parameters);
  180. #else
  181. /* The subcode is not supported on AArch32. */
  182. (void)semihosting_call(SEMIHOSTING_SYS_EXIT, reason);
  183. #endif
  184. }