semihosting.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2014 Broadcom Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /*
  7. * Minimal semihosting implementation for reading files into memory. If more
  8. * features like writing files or console output are required they can be
  9. * added later. This code has been tested on arm64/aarch64 fastmodel only.
  10. * An untested placeholder exists for armv7 architectures, but since they
  11. * are commonly available in silicon now, fastmodel usage makes less sense
  12. * for them.
  13. */
  14. #include <common.h>
  15. #include <asm/semihosting.h>
  16. #define SYSOPEN 0x01
  17. #define SYSCLOSE 0x02
  18. #define SYSREAD 0x06
  19. #define SYSFLEN 0x0C
  20. #define MODE_READ 0x0
  21. #define MODE_READBIN 0x1
  22. static long smh_read(long fd, void *memp, size_t len);
  23. static long smh_open(const char *fname, char *modestr);
  24. static long smh_close(long fd);
  25. static long smh_len_fd(long fd);
  26. /*
  27. * Call the handler
  28. */
  29. static long smh_trap(unsigned int sysnum, void *addr)
  30. {
  31. register long result asm("r0");
  32. #if defined(CONFIG_ARM64)
  33. asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
  34. #else
  35. /* Note - untested placeholder */
  36. asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
  37. #endif
  38. return result;
  39. }
  40. /*
  41. * Open, load a file into memory, and close it. Check that the available space
  42. * is sufficient to store the entire file. Return the bytes actually read from
  43. * the file as seen by the read function. The verbose flag enables some extra
  44. * printing of successful read status.
  45. */
  46. int smh_load(const char *fname, void *memp, int avail, int verbose)
  47. {
  48. long ret;
  49. long fd;
  50. size_t len;
  51. ret = -1;
  52. debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
  53. avail, memp);
  54. /* Open the file */
  55. fd = smh_open(fname, "rb");
  56. if (fd == -1)
  57. return -1;
  58. /* Get the file length */
  59. ret = smh_len_fd(fd);
  60. if (ret == -1) {
  61. smh_close(fd);
  62. return -1;
  63. }
  64. /* Check that the file will fit in the supplied buffer */
  65. if (ret > avail) {
  66. printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
  67. avail);
  68. smh_close(fd);
  69. return -1;
  70. }
  71. len = ret;
  72. /* Read the file into the buffer */
  73. ret = smh_read(fd, memp, len);
  74. if (ret == 0) {
  75. /* Print successful load information if requested */
  76. if (verbose) {
  77. printf("\n%s\n", fname);
  78. printf(" 0x%8p dest\n", memp);
  79. printf(" 0x%08lx size\n", len);
  80. printf(" 0x%08x avail\n", avail);
  81. }
  82. }
  83. /* Close the file */
  84. smh_close(fd);
  85. return ret;
  86. }
  87. /*
  88. * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
  89. */
  90. static long smh_read(long fd, void *memp, size_t len)
  91. {
  92. long ret;
  93. struct smh_read_s {
  94. long fd;
  95. void *memp;
  96. size_t len;
  97. } read;
  98. debug("%s: fd %ld, memp %p, len %lu\n", __func__, fd, memp, len);
  99. read.fd = fd;
  100. read.memp = memp;
  101. read.len = len;
  102. ret = smh_trap(SYSREAD, &read);
  103. if (ret < 0) {
  104. /*
  105. * The ARM handler allows for returning partial lengths,
  106. * but in practice this never happens so rather than create
  107. * hard to maintain partial read loops and such, just fail
  108. * with an error message.
  109. */
  110. printf("%s: ERROR ret %ld, fd %ld, len %lu memp %p\n",
  111. __func__, ret, fd, len, memp);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. /*
  117. * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
  118. * descriptor or -1 on error.
  119. */
  120. static long smh_open(const char *fname, char *modestr)
  121. {
  122. long fd;
  123. unsigned long mode;
  124. struct smh_open_s {
  125. const char *fname;
  126. unsigned long mode;
  127. size_t len;
  128. } open;
  129. debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
  130. /* Check the file mode */
  131. if (!(strcmp(modestr, "r"))) {
  132. mode = MODE_READ;
  133. } else if (!(strcmp(modestr, "rb"))) {
  134. mode = MODE_READBIN;
  135. } else {
  136. printf("%s: ERROR mode \'%s\' not supported\n", __func__,
  137. modestr);
  138. return -1;
  139. }
  140. open.fname = fname;
  141. open.len = strlen(fname);
  142. open.mode = mode;
  143. /* Open the file on the host */
  144. fd = smh_trap(SYSOPEN, &open);
  145. if (fd == -1)
  146. printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
  147. fname);
  148. return fd;
  149. }
  150. /*
  151. * Close the file using the file descriptor
  152. */
  153. static long smh_close(long fd)
  154. {
  155. long ret;
  156. debug("%s: fd %ld\n", __func__, fd);
  157. ret = smh_trap(SYSCLOSE, &fd);
  158. if (ret == -1)
  159. printf("%s: ERROR fd %ld\n", __func__, fd);
  160. return ret;
  161. }
  162. /*
  163. * Get the file length from the file descriptor
  164. */
  165. static long smh_len_fd(long fd)
  166. {
  167. long ret;
  168. debug("%s: fd %ld\n", __func__, fd);
  169. ret = smh_trap(SYSFLEN, &fd);
  170. if (ret == -1)
  171. printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
  172. return ret;
  173. }
  174. /*
  175. * Get the file length from the filename
  176. */
  177. long smh_len(const char *fname)
  178. {
  179. long ret;
  180. long fd;
  181. long len;
  182. debug("%s: file \'%s\'\n", __func__, fname);
  183. /* Open the file */
  184. fd = smh_open(fname, "rb");
  185. if (fd < 0)
  186. return fd;
  187. /* Get the file length */
  188. len = smh_len_fd(fd);
  189. if (len < 0) {
  190. smh_close(fd);
  191. return len;
  192. }
  193. /* Close the file */
  194. ret = smh_close(fd);
  195. if (ret < 0)
  196. return ret;
  197. debug("%s: returning len %ld\n", __func__, len);
  198. /* Return the file length (or -1 error indication) */
  199. return len;
  200. }