1
2
3
7
8
9
10
11
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
139
140
141
142
143
144
145
146
147
148
149
150
151
152
155
156
157
158
159
160
161
162
163
164
165
166
167
168
171
177
178
181
182
183
184
185
186
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
252
253
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
409
410
411
412
413
414
415
416
417
418
419
420
/* ... */
#ifndef OPENOCD_TARGET_ESP_ALGORITHM_H
#define OPENOCD_TARGET_ESP_ALGORITHM_H
#include "helper/log.h"
#include "helper/binarybuffer.h"
#include <helper/time_support.h>
#include <target/algorithm.h>
#include <target/image.h>
5 includes
/* ... */
/* ... */
struct esp_algorithm_image {
struct image image;
uint32_t bss_size;
uint32_t iram_org;
uint32_t iram_len;
uint32_t dram_org;
uint32_t dram_len;
bool reverse;
...};
#define ESP_IMAGE_ELF_PHF_EXEC 0x1
/* ... */
struct esp_algorithm_stub {
target_addr_t entry;
struct working_area *code;
struct working_area *data;
struct working_area *tramp;
struct working_area *padding;
target_addr_t tramp_addr;
/* ... */
target_addr_t tramp_mapped_addr;
struct working_area *stack;
target_addr_t stack_addr;
target_addr_t log_buff_addr;
uint32_t log_buff_size;
void *ainfo;
...};
/* ... */
struct esp_algorithm_mem_args {
struct mem_param *params;
uint32_t count;
...};
/* ... */
struct esp_algorithm_reg_args {
struct reg_param *params;
uint32_t count;
/* ... */
uint32_t first_user_param;
...};
struct esp_algorithm_run_data;
/* ... */
typedef int (*esp_algorithm_func_t)(struct target *target, struct esp_algorithm_run_data *run, void *arg);
/* ... */
typedef int (*esp_algorithm_usr_func_t)(struct target *target, void *usr_arg);
/* ... */
typedef int (*esp_algorithm_usr_func_init_t)(struct target *target,
struct esp_algorithm_run_data *run,
void *usr_arg);
/* ... */
typedef void (*esp_algorithm_usr_func_done_t)(struct target *target,
struct esp_algorithm_run_data *run,
void *usr_arg);
struct esp_algorithm_hw {
int (*algo_init)(struct target *target, struct esp_algorithm_run_data *run, uint32_t num_args, va_list ap);
int (*algo_cleanup)(struct target *target, struct esp_algorithm_run_data *run);
const uint8_t *(*stub_tramp_get)(struct target *target, size_t *size);
...};
/* ... */
struct esp_algorithm_run_data {
uint32_t timeout_ms;
uint32_t stack_size;
struct esp_algorithm_reg_args reg_args;
struct esp_algorithm_mem_args mem_args;
void *arch_info;
int32_t ret_code;
struct esp_algorithm_stub stub;
union {
struct {
uint32_t code_buf_size;
target_addr_t code_buf_addr;
uint32_t min_stack_size;
target_addr_t min_stack_addr;
...} on_board;
struct esp_algorithm_image image;
...};
void *usr_func_arg;
esp_algorithm_usr_func_t usr_func;
esp_algorithm_usr_func_init_t usr_func_init;
esp_algorithm_usr_func_done_t usr_func_done;
esp_algorithm_func_t algo_func;
const struct esp_algorithm_hw *hw;
...};
int esp_algorithm_load_func_image(struct target *target, struct esp_algorithm_run_data *run);
int esp_algorithm_unload_func_image(struct target *target, struct esp_algorithm_run_data *run);
int esp_algorithm_exec_func_image_va(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap);
/* ... */
static inline int esp_algorithm_run_func_image_va(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap)
{
int ret = esp_algorithm_load_func_image(target, run);
if (ret != ERROR_OK)
return ret;
ret = esp_algorithm_exec_func_image_va(target, run, num_args, ap);
int rc = esp_algorithm_unload_func_image(target, run);
return ret != ERROR_OK ? ret : rc;
}{ ... }
static inline int esp_algorithm_run_func_image(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
...)
{
va_list ap;
va_start(ap, num_args);
int retval = esp_algorithm_run_func_image_va(target, run, num_args, ap);
va_end(ap);
return retval;
}{ ... }
int esp_algorithm_load_onboard_func(struct target *target,
target_addr_t func_addr,
struct esp_algorithm_run_data *run);
int esp_algorithm_unload_onboard_func(struct target *target, struct esp_algorithm_run_data *run);
int esp_algorithm_exec_onboard_func_va(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap);
/* ... */
static inline int esp_algorithm_run_onboard_func_va(struct target *target,
struct esp_algorithm_run_data *run,
target_addr_t func_addr,
uint32_t num_args,
va_list ap)
{
int ret = esp_algorithm_load_onboard_func(target, func_addr, run);
if (ret != ERROR_OK)
return ret;
ret = esp_algorithm_exec_onboard_func_va(target, run, num_args, ap);
if (ret != ERROR_OK)
return ret;
return esp_algorithm_unload_onboard_func(target, run);
}{ ... }
static inline int esp_algorithm_run_onboard_func(struct target *target,
struct esp_algorithm_run_data *run,
target_addr_t func_addr,
uint32_t num_args,
...)
{
va_list ap;
va_start(ap, num_args);
int retval = esp_algorithm_run_onboard_func_va(target, run, func_addr, num_args, ap);
va_end(ap);
return retval;
}{ ... }
/* ... */
static inline void esp_algorithm_user_arg_set_uint(struct esp_algorithm_run_data *run,
int arg_num,
uint64_t val)
{
struct reg_param *param = &run->reg_args.params[run->reg_args.first_user_param + arg_num];
assert(param->size <= 64);
if (param->size <= 32)
buf_set_u32(param->value, 0, param->size, val);
else
buf_set_u64(param->value, 0, param->size, val);
}{ ... }
/* ... */
static inline uint64_t esp_algorithm_user_arg_get_uint(struct esp_algorithm_run_data *run, int arg_num)
{
struct reg_param *param = &run->reg_args.params[run->reg_args.first_user_param + arg_num];
assert(param->size <= 64);
if (param->size <= 32)
return buf_get_u32(param->value, 0, param->size);
return buf_get_u64(param->value, 0, param->size);
}{ ... }
/* ... */#endif