OpenOCD
esp.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Espressif chips common target API for OpenOCD *
5  * Copyright (C) 2021 Espressif Systems Ltd. *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <helper/log.h>
13 #include <helper/binarybuffer.h>
14 #include "target/target.h"
15 #include "esp.h"
16 
17 int esp_common_init(struct esp_common *esp, const struct esp_algorithm_hw *algo_hw)
18 {
19  if (!esp)
20  return ERROR_FAIL;
21 
22  esp->algo_hw = algo_hw;
23 
24  return ERROR_OK;
25 }
26 
27 int esp_dbgstubs_table_read(struct target *target, struct esp_dbg_stubs *dbg_stubs)
28 {
29  uint32_t table_size, table_start_id, desc_entry_id, gcov_entry_id;
30  uint32_t entries[ESP_DBG_STUB_ENTRY_MAX] = {0};
31  uint8_t entry_buff[sizeof(entries)] = {0}; /* to avoid endiannes issues */
32 
33  LOG_TARGET_DEBUG(target, "Read debug stubs info %" PRIx32 " / %d", dbg_stubs->base, dbg_stubs->entries_count);
34 
35  /* First of, read 2 entries to get magic num and table size */
36  int res = target_read_buffer(target, dbg_stubs->base, sizeof(uint32_t) * 2, entry_buff);
37  if (res != ERROR_OK) {
38  LOG_ERROR("%s: Failed to read first debug stub entry!", target_name(target));
39  return res;
40  }
41  entries[0] = target_buffer_get_u32(target, entry_buff);
42  entries[1] = target_buffer_get_u32(target, entry_buff + sizeof(uint32_t));
43 
44  if (entries[0] != ESP_DBG_STUB_MAGIC_NUM_VAL) {
45  /* idf with the old table entry structure */
46  table_size = 2;
47  table_start_id = 0;
48  desc_entry_id = 0;
49  gcov_entry_id = 1;
50  } else {
51  table_size = entries[1];
52  table_start_id = ESP_DBG_STUB_TABLE_START;
53  desc_entry_id = ESP_DBG_STUB_TABLE_START;
54  gcov_entry_id = ESP_DBG_STUB_ENTRY_FIRST;
55 
56  /* discard unsupported entries */
57  if (table_size > ESP_DBG_STUB_ENTRY_MAX)
58  table_size = ESP_DBG_STUB_ENTRY_MAX;
59 
60  /* now read the remaining entries */
61  res = target_read_buffer(target, dbg_stubs->base + 2 * sizeof(uint32_t), sizeof(uint32_t) * table_size - 2,
62  entry_buff + sizeof(uint32_t) * 2);
63  if (res != ERROR_OK) {
64  LOG_TARGET_ERROR(target, "Failed to read debug stubs info!");
65  return res;
66  }
67  for (unsigned int i = 2; i < table_size; ++i)
68  entries[i] = target_buffer_get_u32(target, entry_buff + sizeof(uint32_t) * i);
69 
71  }
72 
73  dbg_stubs->entries[ESP_DBG_STUB_DESC] = entries[desc_entry_id];
74  dbg_stubs->entries[ESP_DBG_STUB_ENTRY_GCOV] = entries[gcov_entry_id];
75 
77  LOG_DEBUG("Check dbg stub %d - %x", i, dbg_stubs->entries[i]);
78  if (dbg_stubs->entries[i]) {
79  LOG_DEBUG("New dbg stub %d at %x", dbg_stubs->entries_count, dbg_stubs->entries[i]);
80  dbg_stubs->entries_count++;
81  }
82  }
83  if (dbg_stubs->entries_count < table_size - table_start_id)
84  LOG_WARNING("Not full dbg stub table %d of %d", dbg_stubs->entries_count, table_size - table_start_id);
85 
86  return ERROR_OK;
87 }
Support functions to access arbitrary bits in a byte array.
int esp_dbgstubs_table_read(struct target *target, struct esp_dbg_stubs *dbg_stubs)
Definition: esp.c:27
int esp_common_init(struct esp_common *esp, const struct esp_algorithm_hw *algo_hw)
Definition: esp.c:17
esp_dbg_stub_id
Debug stubs table entries IDs.
Definition: esp.h:27
@ ESP_DBG_STUB_TABLE_START
Definition: esp.h:30
@ ESP_DBG_STUB_ENTRY_FIRST
Definition: esp.h:32
@ ESP_DBG_STUB_CAPABILITIES
Definition: esp.h:34
@ ESP_DBG_STUB_DESC
Definition: esp.h:31
@ ESP_DBG_STUB_ENTRY_MAX
Definition: esp.h:36
@ ESP_DBG_STUB_ENTRY_GCOV
Definition: esp.h:33
#define ESP_DBG_STUB_MAGIC_NUM_VAL
Definition: esp.h:39
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:158
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:149
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
Definition: esp.h:79
const struct esp_algorithm_hw * algo_hw
Definition: esp.h:80
Debug stubs info.
Definition: esp.h:68
uint32_t entries[ESP_DBG_STUB_ENTRY_MAX]
Table contents.
Definition: esp.h:72
uint32_t entries_count
Number of table entries.
Definition: esp.h:74
uint32_t base
Address.
Definition: esp.h:70
Definition: target.h:116
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2407
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:316
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233