OpenOCD
esp_semihosting.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Semihosting API for Espressif chips *
5  * Copyright (C) 2022 Espressif Systems Ltd. *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <helper/log.h>
13 #include <target/target.h>
15 #include "esp_semihosting.h"
16 #include "esp_xtensa.h"
17 
18 static struct esp_semihost_data __attribute__((unused)) *target_to_esp_semihost_data(struct target *target)
19 {
20  const char *arch = target_get_gdb_arch(target);
21  if (arch) {
22  if (strncmp(arch, "xtensa", 6) == 0)
24  /* TODO: add riscv */
25  }
26  LOG_ERROR("Unknown target arch!");
27  return NULL;
28 }
29 
30 static int esp_semihosting_sys_seek(struct target *target, uint64_t fd, uint32_t pos, size_t whence)
31 {
33 
34  semihosting->result = lseek(fd, pos, whence);
35  semihosting->sys_errno = errno;
36  LOG_TARGET_DEBUG(target, "lseek(%" PRIx64 ", %" PRIu32 " %" PRId64 ")=%d", fd, pos, semihosting->result, errno);
37  return ERROR_OK;
38 }
39 
41 {
43  if (!semihosting)
44  /* Silently ignore if the semihosting field was not set. */
45  return ERROR_OK;
46 
47  int retval = ERROR_NOT_IMPLEMENTED;
48 
49  /* Enough space to hold 4 long words. */
50  uint8_t fields[4 * 8];
51 
52  /*
53  * By default return an error.
54  * The actual result must be set by each function
55  */
56  semihosting->result = -1;
57  semihosting->sys_errno = EIO;
58 
59  LOG_TARGET_DEBUG(target, "op=0x%x, param=0x%" PRIx64, semihosting->op, semihosting->param);
60 
61  switch (semihosting->op) {
63  /* Return success to make esp-idf application happy */
64  retval = ERROR_OK;
65  semihosting->result = 0;
67  break;
68 
70  retval = semihosting_read_fields(target, 3, fields);
71  if (retval == ERROR_OK) {
72  uint64_t fd = semihosting_get_field(target, 0, fields);
73  uint32_t pos = semihosting_get_field(target, 1, fields);
74  size_t whence = semihosting_get_field(target, 2, fields);
75  retval = esp_semihosting_sys_seek(target, fd, pos, whence);
76  }
77  break;
78 
83  /* For the time being only riscv chips support these commands
84  * TODO: invoke riscv custom command handler */
85  break;
86  }
87 
88  return retval;
89 }
90 
92 {
94 
95  if (!target) {
96  LOG_ERROR("No target selected");
97  return ERROR_FAIL;
98  }
99 
101  if (!semihosting) {
102  command_print(CMD, "semihosting not supported for current target");
103  return ERROR_FAIL;
104  }
105 
106  if (!semihosting->is_active) {
107  if (semihosting->setup(target, true) != ERROR_OK) {
108  LOG_ERROR("Failed to Configure semihosting");
109  return ERROR_FAIL;
110  }
111  semihosting->is_active = true;
112  }
113 
114  if (CMD_ARGC > 0) {
115  free(semihosting->basedir);
116  semihosting->basedir = strdup(CMD_ARGV[0]);
117  if (!semihosting->basedir) {
118  command_print(CMD, "semihosting failed to allocate memory for basedir!");
119  return ERROR_FAIL;
120  }
121  }
122 
123  command_print(CMD, "DEPRECATED! semihosting base dir: %s",
125 
126  return ERROR_OK;
127 }
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
int esp_semihosting_basedir_command(struct command_invocation *cmd)
static struct esp_semihost_data __attribute__((unused))
int esp_semihosting_common(struct target *target)
static int esp_semihosting_sys_seek(struct target *target, uint64_t fd, uint32_t pos, size_t whence)
#define ESP_SEMIHOSTING_SYS_WATCHPOINT_SET
#define ESP_SEMIHOSTING_SYS_DEBUG_STUBS_INIT
#define ESP_SEMIHOSTING_SYS_APPTRACE_INIT
#define ESP_SEMIHOSTING_SYS_DRV_INFO
#define ESP_SEMIHOSTING_SYS_BREAKPOINT_SET
#define ESP_SEMIHOSTING_SYS_SEEK
static struct esp_xtensa_common * target_to_esp_xtensa(struct target *target)
Definition: esp_xtensa.h:21
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:165
#define ERROR_FAIL
Definition: log.h:161
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:140
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define ERROR_OK
Definition: log.h:155
uint64_t semihosting_get_field(struct target *target, size_t index, uint8_t *fields)
Extract a field from the buffer, considering register size and endianness.
int semihosting_read_fields(struct target *target, size_t number, uint8_t *fields)
Read all fields of a command from target to buffer.
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
struct esp_semihost_data semihost
Definition: esp_xtensa.h:18
int(* setup)(struct target *target, int enable)
int64_t result
The current semihosting result to be returned to the application.
char * basedir
Base directory for semihosting I/O operations.
bool is_active
A flag reporting whether semihosting is active.
int op
The current semihosting operation (R0 on ARM).
int sys_errno
The value to be returned by semihosting SYS_ERRNO request.
uint64_t param
The current semihosting parameter (R1 or ARM).
Definition: target.h:120
struct semihosting * semihosting
Definition: target.h:210
const char * target_get_gdb_arch(struct target *target)
Obtain the architecture for GDB.
Definition: target.c:1430
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:536
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1