OpenOCD
target/rtt.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Copyright (C) 2016-2020 by Marc Schink <dev@zapb.de>
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <helper/log.h>
14 #include <helper/binarybuffer.h>
15 #include <helper/command.h>
16 #include <rtt/rtt.h>
17 
18 #include "target.h"
19 
20 static int read_rtt_channel(struct target *target,
21  const struct rtt_control *ctrl, unsigned int channel_index,
22  enum rtt_channel_type type, struct rtt_channel *channel)
23 {
24  int ret;
25  uint8_t buf[RTT_CHANNEL_SIZE];
26  target_addr_t address;
27 
28  address = ctrl->address + RTT_CB_SIZE + (channel_index * RTT_CHANNEL_SIZE);
29 
32 
33  ret = target_read_buffer(target, address, RTT_CHANNEL_SIZE, buf);
34 
35  if (ret != ERROR_OK)
36  return ret;
37 
38  channel->address = address;
39  channel->name_addr = buf_get_u32(buf + 0, 0, 32);
40  channel->buffer_addr = buf_get_u32(buf + 4, 0, 32);
41  channel->size = buf_get_u32(buf + 8, 0, 32);
42  channel->write_pos = buf_get_u32(buf + 12, 0, 32);
43  channel->read_pos = buf_get_u32(buf + 16, 0, 32);
44  channel->flags = buf_get_u32(buf + 20, 0, 32);
45 
46  return ERROR_OK;
47 }
48 
49 int target_rtt_start(struct target *target, const struct rtt_control *ctrl,
50  void *user_data)
51 {
52  return ERROR_OK;
53 }
54 
55 int target_rtt_stop(struct target *target, void *user_data)
56 {
57  return ERROR_OK;
58 }
59 
60 static int read_channel_name(struct target *target, target_addr_t address,
61  char *name, size_t length)
62 {
63  size_t offset;
64 
65  offset = 0;
66 
67  while (offset < length) {
68  int ret;
69  size_t read_length;
70 
71  read_length = MIN(32, length - offset);
72  ret = target_read_buffer(target, address + offset, read_length,
73  (uint8_t *)name + offset);
74 
75  if (ret != ERROR_OK)
76  return ret;
77 
78  if (memchr(name + offset, '\0', read_length))
79  return ERROR_OK;
80 
81  offset += read_length;
82  }
83 
84  name[length - 1] = '\0';
85 
86  return ERROR_OK;
87 }
88 
89 static int write_to_channel(struct target *target,
90  const struct rtt_channel *channel, const uint8_t *buffer,
91  size_t *length)
92 {
93  int ret;
94  uint32_t len;
95 
96  if (!*length)
97  return ERROR_OK;
98 
99  if (channel->write_pos == channel->read_pos) {
100  uint32_t first_length;
101 
102  len = MIN(*length, channel->size - 1);
103  first_length = MIN(len, channel->size - channel->write_pos);
104 
106  channel->buffer_addr + channel->write_pos, first_length,
107  buffer);
108 
109  if (ret != ERROR_OK)
110  return ret;
111 
112  ret = target_write_buffer(target, channel->buffer_addr,
113  len - first_length, buffer + first_length);
114 
115  if (ret != ERROR_OK)
116  return ret;
117  } else if (channel->write_pos < channel->read_pos) {
118  len = MIN(*length, channel->read_pos - channel->write_pos - 1);
119 
120  if (!len) {
121  *length = 0;
122  return ERROR_OK;
123  }
124 
126  channel->buffer_addr + channel->write_pos, len, buffer);
127 
128  if (ret != ERROR_OK)
129  return ret;
130  } else {
131  uint32_t first_length;
132 
133  len = MIN(*length,
134  channel->size - channel->write_pos + channel->read_pos - 1);
135 
136  if (!len) {
137  *length = 0;
138  return ERROR_OK;
139  }
140 
141  first_length = MIN(len, channel->size - channel->write_pos);
142 
144  channel->buffer_addr + channel->write_pos, first_length,
145  buffer);
146 
147  if (ret != ERROR_OK)
148  return ret;
149 
150  buffer = buffer + first_length;
151 
152  ret = target_write_buffer(target, channel->buffer_addr,
153  len - first_length, buffer);
154 
155  if (ret != ERROR_OK)
156  return ret;
157  }
158 
159  ret = target_write_u32(target, channel->address + 12,
160  (channel->write_pos + len) % channel->size);
161 
162  if (ret != ERROR_OK)
163  return ret;
164 
165  *length = len;
166 
167  return ERROR_OK;
168 }
169 
170 static bool channel_is_active(const struct rtt_channel *channel)
171 {
172  if (!channel)
173  return false;
174 
175  if (!channel->size)
176  return false;
177 
178  return true;
179 }
180 
182  unsigned int channel_index, const uint8_t *buffer, size_t *length,
183  void *user_data)
184 {
185  int ret;
186  struct rtt_channel channel;
187 
188  ret = read_rtt_channel(target, ctrl, channel_index,
189  RTT_CHANNEL_TYPE_DOWN, &channel);
190 
191  if (ret != ERROR_OK) {
192  LOG_ERROR("rtt: Failed to read down-channel %u description",
193  channel_index);
194  return ret;
195  }
196 
197  if (!channel_is_active(&channel)) {
198  LOG_WARNING("rtt: Down-channel %u is not active", channel_index);
199  return ERROR_OK;
200  }
201 
202  if (channel.size < RTT_CHANNEL_BUFFER_MIN_SIZE) {
203  LOG_WARNING("rtt: Down-channel %u is not large enough",
204  channel_index);
205  return ERROR_OK;
206  }
207 
208  ret = write_to_channel(target, &channel, buffer, length);
209 
210  if (ret != ERROR_OK)
211  return ret;
212 
213  LOG_DEBUG("rtt: Wrote %zu bytes into down-channel %u", *length,
214  channel_index);
215 
216  return ERROR_OK;
217 }
218 
220  target_addr_t address, struct rtt_control *ctrl, void *user_data)
221 {
222  int ret;
223  uint8_t buf[RTT_CB_SIZE];
224 
226 
227  if (ret != ERROR_OK)
228  return ret;
229 
230  memcpy(ctrl->id, buf, RTT_CB_MAX_ID_LENGTH);
231  ctrl->id[RTT_CB_MAX_ID_LENGTH - 1] = '\0';
233  0, 32);
235  0, 32);
236 
237  return ERROR_OK;
238 }
239 
241  target_addr_t *address, size_t size, const char *id, bool *found,
242  void *user_data)
243 {
244  target_addr_t address_end = *address + size;
245  uint8_t buf[1024];
246 
247  *found = false;
248 
249  size_t id_matched_length = 0;
250  const size_t id_length = strlen(id);
251 
252  LOG_INFO("rtt: Searching for control block '%s'", id);
253 
254  for (target_addr_t addr = *address; addr < address_end; addr += sizeof(buf)) {
255  int ret;
256 
257  const size_t buf_size = MIN(sizeof(buf), address_end - addr);
258  ret = target_read_buffer(target, addr, buf_size, buf);
259 
260  if (ret != ERROR_OK)
261  return ret;
262 
263  for (size_t buf_off = 0; buf_off < buf_size; buf_off++) {
264  if (id_matched_length > 0 &&
265  buf[buf_off] != id[id_matched_length]) {
266  /* Start from beginning */
267  id_matched_length = 0;
268  }
269 
270  if (buf[buf_off] == id[id_matched_length])
271  id_matched_length++;
272 
273  if (id_matched_length == id_length) {
274  *address = addr + buf_off + 1 - id_length;
275  *found = true;
276  return ERROR_OK;
277  }
278  }
279  }
280 
281  return ERROR_OK;
282 }
283 
285  const struct rtt_control *ctrl, unsigned int channel_index,
287  void *user_data)
288 {
289  int ret;
290  struct rtt_channel channel;
291 
292  ret = read_rtt_channel(target, ctrl, channel_index, type, &channel);
293 
294  if (ret != ERROR_OK) {
295  LOG_ERROR("rtt: Failed to read channel %u description",
296  channel_index);
297  return ret;
298  }
299 
300  ret = read_channel_name(target, channel.name_addr, info->name,
301  info->name_length);
302 
303  if (ret != ERROR_OK)
304  return ret;
305 
306  info->size = channel.size;
307  info->flags = channel.flags;
308 
309  return ERROR_OK;
310 }
311 
312 static int read_from_channel(struct target *target,
313  const struct rtt_channel *channel, uint8_t *buffer,
314  size_t *length)
315 {
316  int ret;
317  uint32_t len;
318 
319  if (!*length)
320  return ERROR_OK;
321 
322  if (channel->read_pos == channel->write_pos) {
323  len = 0;
324  } else if (channel->read_pos < channel->write_pos) {
325  len = MIN(*length, channel->write_pos - channel->read_pos);
326 
328  channel->buffer_addr + channel->read_pos, len, buffer);
329 
330  if (ret != ERROR_OK)
331  return ret;
332  } else {
333  uint32_t first_length;
334 
335  len = MIN(*length,
336  channel->size - channel->read_pos + channel->write_pos);
337  first_length = MIN(len, channel->size - channel->read_pos);
338 
340  channel->buffer_addr + channel->read_pos, first_length, buffer);
341 
342  if (ret != ERROR_OK)
343  return ret;
344 
345  ret = target_read_buffer(target, channel->buffer_addr,
346  len - first_length, buffer + first_length);
347 
348  if (ret != ERROR_OK)
349  return ret;
350  }
351 
352  if (len > 0) {
353  ret = target_write_u32(target, channel->address + 16,
354  (channel->read_pos + len) % channel->size);
355 
356  if (ret != ERROR_OK)
357  return ret;
358  }
359 
360  *length = len;
361 
362  return ERROR_OK;
363 }
364 
366  const struct rtt_control *ctrl, struct rtt_sink_list **sinks,
367  size_t num_channels, void *user_data)
368 {
369  num_channels = MIN(num_channels, ctrl->num_up_channels);
370 
371  for (size_t i = 0; i < num_channels; i++) {
372  int ret;
373  struct rtt_channel channel;
374  uint8_t buffer[1024];
375  size_t length;
376 
377  if (!sinks[i])
378  continue;
379 
381  &channel);
382 
383  if (ret != ERROR_OK) {
384  LOG_ERROR("rtt: Failed to read up-channel %zu description", i);
385  return ret;
386  }
387 
388  if (!channel_is_active(&channel)) {
389  LOG_WARNING("rtt: Up-channel %zu is not active", i);
390  continue;
391  }
392 
393  if (channel.size < RTT_CHANNEL_BUFFER_MIN_SIZE) {
394  LOG_WARNING("rtt: Up-channel %zu is not large enough", i);
395  continue;
396  }
397 
398  length = sizeof(buffer);
399  ret = read_from_channel(target, &channel, buffer, &length);
400 
401  if (ret != ERROR_OK) {
402  LOG_ERROR("rtt: Failed to read from up-channel %zu", i);
403  return ret;
404  }
405 
406  for (struct rtt_sink_list *sink = sinks[i]; sink; sink = sink->next)
407  sink->read(i, buffer, length, sink->user_data);
408  }
409 
410  return ERROR_OK;
411 }
const char * name
Definition: armv4_5.c:76
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:98
uint8_t type
Definition: esp_usb_jtag.c:0
uint8_t length
Definition: esp_usb_jtag.c:1
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
uint32_t addr
Definition: nuttx.c:65
#define MIN(a, b)
Definition: replacements.h:22
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
#define RTT_CB_SIZE
Definition: rtt/rtt.h:22
#define RTT_CB_MAX_ID_LENGTH
Control block ID length in bytes, including the trailing null-terminator.
Definition: rtt/rtt.h:19
#define RTT_CHANNEL_BUFFER_MIN_SIZE
Definition: rtt/rtt.h:28
rtt_channel_type
Channel type.
Definition: rtt/rtt.h:91
@ RTT_CHANNEL_TYPE_UP
Up channel (target to host).
Definition: rtt/rtt.h:93
@ RTT_CHANNEL_TYPE_DOWN
Down channel (host to target).
Definition: rtt/rtt.h:95
#define RTT_CHANNEL_SIZE
Definition: rtt/rtt.h:25
RTT channel information.
Definition: rtt/rtt.h:65
RTT channel.
Definition: rtt/rtt.h:43
uint32_t read_pos
Read position within the buffer in bytes.
Definition: rtt/rtt.h:55
uint32_t name_addr
Channel name address on the target.
Definition: rtt/rtt.h:47
target_addr_t address
Channel structure address on the target.
Definition: rtt/rtt.h:45
uint32_t write_pos
Write position within the buffer in bytes.
Definition: rtt/rtt.h:53
uint32_t size
Channel buffer size in bytes.
Definition: rtt/rtt.h:51
uint32_t flags
Buffer flags.
Definition: rtt/rtt.h:61
uint32_t buffer_addr
Buffer address on the target.
Definition: rtt/rtt.h:49
RTT control block.
Definition: rtt/rtt.h:31
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier, including trailing null-terminator.
Definition: rtt/rtt.h:35
target_addr_t address
Control block address on the target.
Definition: rtt/rtt.h:33
uint32_t num_up_channels
Maximum number of up-channels.
Definition: rtt/rtt.h:37
uint32_t num_down_channels
Maximum number of down-channels.
Definition: rtt/rtt.h:39
struct rtt_sink_list * next
Definition: rtt/rtt.h:87
Definition: target.h:120
int target_rtt_read_control_block(struct target *target, target_addr_t address, struct rtt_control *ctrl, void *user_data)
Definition: target/rtt.c:219
static int read_from_channel(struct target *target, const struct rtt_channel *channel, uint8_t *buffer, size_t *length)
Definition: target/rtt.c:312
static int read_channel_name(struct target *target, target_addr_t address, char *name, size_t length)
Definition: target/rtt.c:60
static int read_rtt_channel(struct target *target, const struct rtt_control *ctrl, unsigned int channel_index, enum rtt_channel_type type, struct rtt_channel *channel)
Definition: target/rtt.c:20
static bool channel_is_active(const struct rtt_channel *channel)
Definition: target/rtt.c:170
int target_rtt_read_callback(struct target *target, const struct rtt_control *ctrl, struct rtt_sink_list **sinks, size_t num_channels, void *user_data)
Definition: target/rtt.c:365
static int write_to_channel(struct target *target, const struct rtt_channel *channel, const uint8_t *buffer, size_t *length)
Definition: target/rtt.c:89
int target_rtt_find_control_block(struct target *target, target_addr_t *address, size_t size, const char *id, bool *found, void *user_data)
Definition: target/rtt.c:240
int target_rtt_stop(struct target *target, void *user_data)
Definition: target/rtt.c:55
int target_rtt_start(struct target *target, const struct rtt_control *ctrl, void *user_data)
Definition: target/rtt.c:49
int target_rtt_write_callback(struct target *target, struct rtt_control *ctrl, unsigned int channel_index, const uint8_t *buffer, size_t *length, void *user_data)
Definition: target/rtt.c:181
int target_rtt_read_channel_info(struct target *target, const struct rtt_control *ctrl, unsigned int channel_index, enum rtt_channel_type type, struct rtt_channel_info *info, void *user_data)
Definition: target/rtt.c:284
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2408
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2473
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2707
uint64_t target_addr_t
Definition: types.h:335
static struct ublast_lowlevel_priv info
uint8_t offset[4]
Definition: vdebug.c:9