vfs_semihost.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2020 Espressif Systems (Shanghai) Co. Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_vfs.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "soc/cpu.h"
  18. #include <stdarg.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <sys/errno.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #ifndef CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS
  25. #define CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS 1
  26. #endif
  27. #ifndef CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN
  28. #define CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN 128
  29. #endif
  30. #ifdef VFS_SUPPRESS_SEMIHOSTING_LOG
  31. #define LOG_LOCAL_LEVEL ESP_LOG_NONE
  32. #endif //VFS_SUPPRESS_SEMIHOSTING_LOG
  33. #include "esp_log.h"
  34. const static char *TAG = "esp_semihost";
  35. /* current semihosting implementation version */
  36. #define DRIVER_SEMIHOSTING_VERSION 0x1
  37. /* syscalls */
  38. #define SYSCALL_INSTR "break 1,1\n"
  39. #define SYS_OPEN 0x01
  40. #define SYS_CLOSE 0x02
  41. #define SYS_WRITE 0x05
  42. #define SYS_READ 0x06
  43. #define SYS_SEEK 0x0A
  44. #define SYS_DRVINFO 0xE0
  45. /* additional open flags */
  46. #define O_BINARY 0 // there is no binary flag in our toolchain, as well as in Linux OS
  47. // but we are leaving it to have an identical to OOCD flags table
  48. /** ESP-specific file open flag. Indicates that path passed to open() is absolute host path. */
  49. #define ESP_O_SEMIHOST_ABSPATH 0x80000000
  50. /* The table is identical to semihosting_common's one from OpenOCD */
  51. static const int open_modeflags[12] = {
  52. O_RDONLY,
  53. O_RDONLY | O_BINARY,
  54. O_RDWR,
  55. O_RDWR | O_BINARY,
  56. O_WRONLY | O_CREAT | O_TRUNC,
  57. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
  58. O_RDWR | O_CREAT | O_TRUNC,
  59. O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  60. O_WRONLY | O_CREAT | O_APPEND,
  61. O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
  62. O_RDWR | O_CREAT | O_APPEND,
  63. O_RDWR | O_CREAT | O_APPEND | O_BINARY
  64. };
  65. /**
  66. * @brief semihosting driver information
  67. *
  68. */
  69. typedef struct {
  70. int ver;
  71. } drv_info_t;
  72. /**
  73. * @brief Get the number of appropriate file open mode set from open_modeflags and add some esp flags to them
  74. *
  75. * @param flags value, every bit of which reflects state of some open-file flag
  76. * @return int
  77. * -1 - there is no appropriate entry of open_modeflags[]
  78. * esp_flags | (0...11) - esp-specific flags and number of flag set for oocd from @ref open_modeflags[]
  79. */
  80. static inline int get_o_mode(int flags) {
  81. uint32_t esp_flags = flags & 0xfff00000; // that bits are not used, so let's use it for our espressif's purposes
  82. uint32_t semi_comm_flags = flags & 0x000fffff;
  83. if (semi_comm_flags & O_EXCL) { // bypassing lacking of this at table above
  84. semi_comm_flags &= ~(O_EXCL);
  85. semi_comm_flags |= O_CREAT;
  86. }
  87. for (int i = 0; i < sizeof(open_modeflags) / sizeof(open_modeflags[0]); i++) {
  88. if (semi_comm_flags == open_modeflags[i])
  89. return (esp_flags | i);
  90. }
  91. return -1; // there is no corresponding mode in the table
  92. }
  93. typedef struct {
  94. char base_path[ESP_VFS_PATH_MAX + 1]; /* base path in VFS where host semihosting dir is mounted */
  95. char host_path[CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN + 1]; /* host path to use as base dir for open files */
  96. } vfs_semihost_ctx_t;
  97. static vfs_semihost_ctx_t s_semhost_ctx[CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS];
  98. static inline int generic_syscall(int sys_nr, int arg1, int arg2, int arg3, int arg4, int* ret_errno)
  99. {
  100. int host_ret, host_errno;
  101. if (!esp_cpu_in_ocd_debug_mode()) {
  102. *ret_errno = EIO;
  103. return -1;
  104. }
  105. __asm__ volatile (
  106. "mov a2, %[sys_nr]\n" \
  107. "mov a3, %[arg1]\n" \
  108. "mov a4, %[arg2]\n" \
  109. "mov a5, %[arg3]\n" \
  110. "mov a6, %[arg4]\n" \
  111. SYSCALL_INSTR \
  112. "mov %[host_ret], a2\n" \
  113. "mov %[host_errno], a3\n" \
  114. :[host_ret]"=r"(host_ret),[host_errno]"=r"(host_errno)
  115. :[sys_nr]"r"(sys_nr),[arg1]"r"(arg1),[arg2]"r"(arg2),[arg3]"r"(arg3),[arg4]"r"(arg4)
  116. :"a2","a3","a4","a5","a6");
  117. *ret_errno = host_errno;
  118. return host_ret;
  119. }
  120. inline bool ctx_is_unused(const vfs_semihost_ctx_t* ctx)
  121. {
  122. return ctx->base_path[0] == 0;
  123. }
  124. inline bool ctx_uses_abspath(const vfs_semihost_ctx_t* ctx)
  125. {
  126. return ctx->host_path[0];
  127. }
  128. /**
  129. * @brief Send a custom syscall SYS_DRVINFO to the host for determining
  130. *
  131. * @param ctx context
  132. * @return error
  133. */
  134. static esp_err_t vfs_semihost_drvinfo(vfs_semihost_ctx_t *ctx) {
  135. drv_info_t drv_info = {
  136. .ver = DRIVER_SEMIHOSTING_VERSION
  137. };
  138. int host_err = 0;
  139. size_t ret = -1;
  140. ESP_LOGV(TAG, "%s: s_ver: %x, flags: %x, par3: %x, par4: %x", __func__, (int)&drv_info, sizeof(drv_info), 0, 0);
  141. ret = generic_syscall(SYS_DRVINFO, (int)&drv_info, sizeof(drv_info), 0, 0, &host_err);
  142. /* Recognizing the version */
  143. ESP_LOGV(TAG, "Trying to determine semihosting's version...");
  144. if (ret == -1) { /* there is no such syscall - old semihosting */
  145. ret = ESP_ERR_INVALID_VERSION;
  146. } else {
  147. ESP_LOGI(TAG, "OpenOCD Semihosting v.%d [Read from an OpenOCD response]", drv_info.ver);
  148. ESP_LOGV(TAG, "[Version was read from an OpenOCD response]");
  149. }
  150. return ret;
  151. }
  152. static int vfs_semihost_open(void* ctx, const char* path, int flags, int mode) {
  153. int ret_fd = -1, o_mode = 0, host_err = 0;
  154. char *host_path;
  155. vfs_semihost_ctx_t *semi_ctx = ctx;
  156. ESP_LOGV(TAG, "%s: %p '%s 0x%x 0x%x'", __func__, semi_ctx, path, flags, mode);
  157. /* flags processing */
  158. if (ctx_uses_abspath(semi_ctx)) {
  159. flags |= ESP_O_SEMIHOST_ABSPATH;
  160. }
  161. o_mode = get_o_mode(flags);
  162. if (o_mode == -1) { /* if wrong flags - error */
  163. errno = EINVAL;
  164. } else { /* if ok - host_path processing */
  165. if (ctx_uses_abspath(semi_ctx)) {
  166. host_path = malloc(strlen(semi_ctx->host_path) + strlen(path) + 1);
  167. if (host_path == NULL) { /* if no valid pointer - error and return */
  168. errno = ENOMEM;
  169. return -1;
  170. }
  171. strcpy(host_path, semi_ctx->host_path);
  172. strcat(host_path, path);
  173. } else {
  174. host_path = (char *)path;
  175. }
  176. /* everything is ready: syscall and cleanup */
  177. ret_fd = generic_syscall(SYS_OPEN, (int)host_path, o_mode, strlen(host_path), mode, &host_err);
  178. if (ret_fd == -1) {
  179. errno = host_err;
  180. }
  181. if (ctx_uses_abspath(semi_ctx)) {
  182. free(host_path);
  183. }
  184. }
  185. return ret_fd;
  186. }
  187. static ssize_t vfs_semihost_write(void* ctx, int fd, const void * data, size_t size)
  188. {
  189. int host_err = 0;
  190. size_t ret = -1;
  191. ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
  192. ret = generic_syscall(SYS_WRITE, fd, (int)data, size, 0, &host_err);
  193. if (ret == -1) {
  194. errno = host_err;
  195. }
  196. return size - (ssize_t)ret; /* Write syscall returns the number of bytes NOT written */
  197. }
  198. static ssize_t vfs_semihost_read(void* ctx, int fd, void* data, size_t size)
  199. {
  200. int host_err = 0;
  201. size_t ret = -1;
  202. ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
  203. ret = generic_syscall(SYS_READ, fd, (int)data, size, 0, &host_err);
  204. if (ret == -1) {
  205. errno = host_err;
  206. return ret;
  207. }
  208. return size - (ssize_t)ret; /* Read syscall returns the number of bytes NOT read */
  209. }
  210. static int vfs_semihost_close(void* ctx, int fd)
  211. {
  212. int ret = -1, host_err = 0;
  213. ESP_LOGV(TAG, "%s: %d", __func__, fd);
  214. ret = generic_syscall(SYS_CLOSE, fd, 0, 0, 0, &host_err);
  215. if (ret == -1) {
  216. errno = host_err;
  217. }
  218. return ret;
  219. }
  220. static off_t vfs_semihost_lseek(void* ctx, int fd, off_t offset, int mode)
  221. {
  222. int ret = -1, host_err = 0;
  223. ESP_LOGV(TAG, "%s: %d %ld %d", __func__, fd, offset, mode);
  224. ret = generic_syscall(SYS_SEEK, fd, offset, mode, 0, &host_err);
  225. if (ret == -1) {
  226. errno = host_err;
  227. }
  228. return (off_t)ret;
  229. }
  230. esp_err_t esp_vfs_semihost_register(const char* base_path, const char* host_path)
  231. {
  232. const esp_vfs_t vfs = {
  233. .flags = ESP_VFS_FLAG_CONTEXT_PTR,
  234. .write_p = &vfs_semihost_write,
  235. .open_p = &vfs_semihost_open,
  236. .close_p = &vfs_semihost_close,
  237. .read_p = &vfs_semihost_read,
  238. .lseek_p = &vfs_semihost_lseek,
  239. };
  240. ESP_LOGD(TAG, "Register semihosting driver '%s' -> '%s'", base_path, host_path ? host_path : "null");
  241. if (!esp_cpu_in_ocd_debug_mode()) {
  242. ESP_LOGE(TAG, "OpenOCD is not connected!");
  243. return ESP_ERR_NOT_SUPPORTED;
  244. }
  245. int i = 0;
  246. for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
  247. if (ctx_is_unused(&s_semhost_ctx[i])) {
  248. break;
  249. }
  250. if (strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
  251. return ESP_ERR_INVALID_STATE;
  252. }
  253. }
  254. if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
  255. return ESP_ERR_NO_MEM;
  256. }
  257. strlcpy(s_semhost_ctx[i].base_path, base_path, sizeof(s_semhost_ctx[i].base_path) - 1);
  258. if (host_path) {
  259. strlcpy(s_semhost_ctx[i].host_path, host_path, sizeof(s_semhost_ctx[i].host_path) - 1);
  260. }
  261. ESP_LOGD(TAG, "Register semihosting driver %d %p", i, &s_semhost_ctx[i]);
  262. esp_err_t err = vfs_semihost_drvinfo(&s_semhost_ctx[i]); // define semihosting version
  263. if (err != ESP_OK) {
  264. ESP_LOGE(TAG, "Incompatible OpenOCD version detected. Please follow the getting started guides to install the required version.");
  265. }
  266. err = esp_vfs_register(base_path, &vfs, &s_semhost_ctx[i]);
  267. if (err != ESP_OK) {
  268. ESP_LOGE(TAG, "Can't register the semihosting! Error: %s", esp_err_to_name(err));
  269. return err;
  270. }
  271. return err;
  272. }
  273. esp_err_t esp_vfs_semihost_unregister(const char* base_path)
  274. {
  275. ESP_LOGD(TAG, "Unregister semihosting driver @ '%s'", base_path);
  276. int i = 0;
  277. for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
  278. if (s_semhost_ctx[i].base_path[0] != 0 && strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
  279. break;
  280. }
  281. }
  282. if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
  283. return ESP_ERR_INVALID_ARG;
  284. }
  285. esp_err_t ret = esp_vfs_unregister(s_semhost_ctx[i].base_path);
  286. if (ret != ESP_OK) {
  287. return ret;
  288. }
  289. s_semhost_ctx[i].base_path[0] = 0;
  290. s_semhost_ctx[i].host_path[0] = 0;
  291. ESP_LOGD(TAG, "Unregistered semihosting driver @ '%s'", base_path);
  292. return ESP_OK;
  293. }