2021-11-06 05:31:05 -04:00
|
|
|
/*
|
2021-01-05 18:26:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-06 05:31:05 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-02-19 14:50:18 -05:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2020-03-30 05:50:08 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2019-02-19 14:50:18 -05:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2021-01-05 18:26:07 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_vfs.h"
|
2022-07-21 07:24:42 -04:00
|
|
|
#include "esp_cpu.h"
|
2021-01-05 18:26:07 -05:00
|
|
|
#include "openocd_semihosting.h"
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2020-03-30 05:50:08 -04:00
|
|
|
#ifndef CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS
|
|
|
|
#define CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS 1
|
2020-03-20 08:35:00 -04:00
|
|
|
#endif
|
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
const static char *TAG = "esp_semihost";
|
|
|
|
|
2020-03-30 05:50:08 -04:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
/* Additional open flags */
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
/* There is no O_BINARY flag defined in newlib, as well as on Linux,
|
|
|
|
* but we are leaving it to have the flags table identical to OpenOCD.
|
|
|
|
*/
|
|
|
|
#define O_BINARY 0
|
|
|
|
|
|
|
|
/* The table is identical to the one in OpenOCD semihosting_common.c */
|
2020-03-30 05:50:08 -04:00
|
|
|
static const int open_modeflags[12] = {
|
2022-03-13 14:03:06 -04:00
|
|
|
O_RDONLY,
|
|
|
|
O_RDONLY | O_BINARY,
|
|
|
|
O_RDWR,
|
|
|
|
O_RDWR | O_BINARY,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
|
|
|
O_RDWR | O_CREAT | O_TRUNC,
|
|
|
|
O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
|
|
|
|
O_WRONLY | O_CREAT | O_APPEND,
|
|
|
|
O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
|
|
|
|
O_RDWR | O_CREAT | O_APPEND,
|
|
|
|
O_RDWR | O_CREAT | O_APPEND | O_BINARY
|
2020-03-30 05:50:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the number of appropriate file open mode set from open_modeflags and add some esp flags to them
|
|
|
|
*
|
|
|
|
* @param flags value, every bit of which reflects state of some open-file flag
|
2021-01-05 18:26:07 -05:00
|
|
|
* @return index of the flag from @ref open_modeflags[], or -1 if invalid flags combination is given.
|
2020-03-30 05:50:08 -04:00
|
|
|
*/
|
|
|
|
static inline int get_o_mode(int flags) {
|
2021-01-05 18:26:07 -05:00
|
|
|
if (flags & O_EXCL) { // bypassing lacking of this at table above
|
|
|
|
flags &= ~(O_EXCL);
|
|
|
|
flags |= O_CREAT;
|
2020-03-30 05:50:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(open_modeflags) / sizeof(open_modeflags[0]); i++) {
|
2021-01-05 18:26:07 -05:00
|
|
|
if (flags == open_modeflags[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
2020-03-30 05:50:08 -04:00
|
|
|
}
|
|
|
|
return -1; // there is no corresponding mode in the table
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char base_path[ESP_VFS_PATH_MAX + 1]; /* base path in VFS where host semihosting dir is mounted */
|
2019-02-19 14:50:18 -05:00
|
|
|
} vfs_semihost_ctx_t;
|
|
|
|
|
2020-03-30 05:50:08 -04:00
|
|
|
static vfs_semihost_ctx_t s_semhost_ctx[CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS];
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
static inline bool ctx_is_unused(const vfs_semihost_ctx_t* ctx)
|
2019-02-19 14:50:18 -05:00
|
|
|
{
|
|
|
|
return ctx->base_path[0] == 0;
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
#define FAIL_IF_NO_DEBUGGER() \
|
|
|
|
do { \
|
2022-07-21 07:24:42 -04:00
|
|
|
if (!esp_cpu_dbgr_is_attached()) { \
|
2021-01-05 18:26:07 -05:00
|
|
|
errno = EIO; \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2020-03-30 05:50:08 -04:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
static esp_err_t vfs_semihost_drvinfo(vfs_semihost_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2020-03-30 05:50:08 -04:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
int ret = semihosting_ver_info();
|
|
|
|
if (ret == -1) {
|
|
|
|
/* Unsupported syscall - old version of OpenOCD */
|
|
|
|
return ESP_ERR_INVALID_VERSION;
|
2020-03-30 05:50:08 -04:00
|
|
|
}
|
2021-01-05 18:26:07 -05:00
|
|
|
return ESP_OK;
|
2020-03-30 05:50:08 -04:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
static int vfs_semihost_open(void* ctx, const char* path, int flags, int mode)
|
|
|
|
{
|
|
|
|
int ret_fd = -1;
|
2022-03-13 14:03:06 -04:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2020-03-30 05:50:08 -04:00
|
|
|
|
2022-03-13 14:03:06 -04:00
|
|
|
if (path == NULL) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return ret_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGV(TAG, "%s: '%s 0x%x 0x%x'", __func__, path, flags, mode);
|
2020-03-30 05:50:08 -04:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
int o_mode = get_o_mode(flags);
|
2020-03-30 05:50:08 -04:00
|
|
|
if (o_mode == -1) { /* if wrong flags - error */
|
|
|
|
errno = EINVAL;
|
2021-01-05 18:26:07 -05:00
|
|
|
} else {
|
2022-03-13 14:03:06 -04:00
|
|
|
ret_fd = semihosting_open(path, o_mode, mode);
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
2020-03-30 05:50:08 -04:00
|
|
|
return ret_fd;
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t vfs_semihost_write(void* ctx, int fd, const void * data, size_t size)
|
|
|
|
{
|
2021-01-05 18:26:07 -05:00
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2022-03-13 14:03:06 -04:00
|
|
|
if (data == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
|
2021-01-05 18:26:07 -05:00
|
|
|
return semihosting_write(fd, data, size);
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t vfs_semihost_read(void* ctx, int fd, void* data, size_t size)
|
|
|
|
{
|
2021-01-05 18:26:07 -05:00
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2022-03-13 14:03:06 -04:00
|
|
|
if (data == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
ESP_LOGV(TAG, "%s: %d %u bytes", __func__, fd, size);
|
2021-01-05 18:26:07 -05:00
|
|
|
return semihosting_read(fd, data, size);
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vfs_semihost_close(void* ctx, int fd)
|
|
|
|
{
|
2021-01-05 18:26:07 -05:00
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2019-02-19 14:50:18 -05:00
|
|
|
|
|
|
|
ESP_LOGV(TAG, "%s: %d", __func__, fd);
|
2021-01-05 18:26:07 -05:00
|
|
|
return semihosting_close(fd);
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
2020-03-30 05:50:08 -04:00
|
|
|
static off_t vfs_semihost_lseek(void* ctx, int fd, off_t offset, int mode)
|
2019-02-19 14:50:18 -05:00
|
|
|
{
|
2021-01-05 18:26:07 -05:00
|
|
|
FAIL_IF_NO_DEBUGGER();
|
2019-02-19 14:50:18 -05:00
|
|
|
|
2021-01-05 18:26:07 -05:00
|
|
|
return semihosting_seek(fd, offset, mode);
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
2022-03-13 14:03:06 -04:00
|
|
|
esp_err_t esp_vfs_semihost_register(const char* base_path)
|
2019-02-19 14:50:18 -05:00
|
|
|
{
|
2022-03-13 14:03:06 -04:00
|
|
|
assert(base_path);
|
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
const esp_vfs_t vfs = {
|
|
|
|
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
|
|
|
|
.write_p = &vfs_semihost_write,
|
|
|
|
.open_p = &vfs_semihost_open,
|
|
|
|
.close_p = &vfs_semihost_close,
|
|
|
|
.read_p = &vfs_semihost_read,
|
|
|
|
.lseek_p = &vfs_semihost_lseek,
|
|
|
|
};
|
2022-03-13 14:03:06 -04:00
|
|
|
ESP_LOGD(TAG, "Register semihosting driver '%s'", base_path);
|
2022-07-21 07:24:42 -04:00
|
|
|
if (!esp_cpu_dbgr_is_attached()) {
|
2019-02-19 14:50:18 -05:00
|
|
|
ESP_LOGE(TAG, "OpenOCD is not connected!");
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
int i = 0;
|
2020-03-30 05:50:08 -04:00
|
|
|
for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
|
2019-02-19 14:50:18 -05:00
|
|
|
if (ctx_is_unused(&s_semhost_ctx[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 05:50:08 -04:00
|
|
|
if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
|
2019-02-19 14:50:18 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
strlcpy(s_semhost_ctx[i].base_path, base_path, sizeof(s_semhost_ctx[i].base_path) - 1);
|
2022-03-13 14:03:06 -04:00
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
ESP_LOGD(TAG, "Register semihosting driver %d %p", i, &s_semhost_ctx[i]);
|
2021-01-05 18:26:07 -05:00
|
|
|
|
|
|
|
esp_err_t err;
|
|
|
|
/* Check for older OpenOCD versions */
|
|
|
|
err = vfs_semihost_drvinfo(&s_semhost_ctx[i]); // define semihosting version
|
2020-03-30 05:50:08 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Incompatible OpenOCD version detected. Please follow the getting started guides to install the required version.");
|
|
|
|
}
|
2021-01-05 18:26:07 -05:00
|
|
|
|
2020-03-30 05:50:08 -04:00
|
|
|
err = esp_vfs_register(base_path, &vfs, &s_semhost_ctx[i]);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Can't register the semihosting! Error: %s", esp_err_to_name(err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return err;
|
2019-02-19 14:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_vfs_semihost_unregister(const char* base_path)
|
|
|
|
{
|
2022-03-13 14:03:06 -04:00
|
|
|
assert(base_path);
|
|
|
|
|
2019-02-19 14:50:18 -05:00
|
|
|
ESP_LOGD(TAG, "Unregister semihosting driver @ '%s'", base_path);
|
|
|
|
int i = 0;
|
2020-03-30 05:50:08 -04:00
|
|
|
for (i = 0; i < CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS; i++) {
|
2019-02-19 14:50:18 -05:00
|
|
|
if (s_semhost_ctx[i].base_path[0] != 0 && strcmp(base_path, s_semhost_ctx[i].base_path) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 05:50:08 -04:00
|
|
|
if (i == CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS) {
|
2019-02-19 14:50:18 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
esp_err_t ret = esp_vfs_unregister(s_semhost_ctx[i].base_path);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
s_semhost_ctx[i].base_path[0] = 0;
|
|
|
|
ESP_LOGD(TAG, "Unregistered semihosting driver @ '%s'", base_path);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|