1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
67
68
69
71
72
73
74
75
80
81
82
83
84
85
86
87
94
95
96
97
98
99
100
101
102
106
107
114
115
116
117
118
119
120
121
122
123
127
128
129
130
131
132
138
139
140
141
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
176
177
178
179
180
181
182
183
184
185
186
190
191
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
277
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
314
315
320
321
322
323
324
325
326
327
328
329
333
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
363
364
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
398
399
400
401
402
403
404
405
406
407
408
409
410
414
415
419
420
421
422
423
424
425
426
437
438
439
440
441
442
443
444
445
446
447
448
450
451
452
453
454
459
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
488
489
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
528
529
530
531
532
533
534
535
536
537
538
539
543
544
549
550
555
556
560
561
562
563
564
565
566
574
575
579
580
581
582
583
584
588
589
590
591
592
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/align.h>
#include <target/algorithm.h>
#include <target/target.h>
#include "esp_algorithm.h"
#define DEFAULT_ALGORITHM_TIMEOUT_MS 40000
static int esp_algorithm_read_stub_logs(struct target *target, struct esp_algorithm_stub *stub)
{
if (!stub || stub->log_buff_addr == 0 || stub->log_buff_size == 0)
return ERROR_FAIL;
uint32_t len = 0;
int retval = target_read_u32(target, stub->log_buff_addr, &len);
if (retval != ERROR_OK)
return retval;
if (len == 0 || len > stub->log_buff_size - 4)
return ERROR_FAIL;
uint8_t *log_buff = calloc(1, len);
if (!log_buff) {
LOG_ERROR("Failed to allocate memory for the stub log!");
return ERROR_FAIL;
}if (!log_buff) { ... }
retval = target_read_memory(target, stub->log_buff_addr + 4, 1, len, log_buff);
if (retval == ERROR_OK)
LOG_OUTPUT("%*.*s", len, len, log_buff);
free(log_buff);
return retval;
}{ ... }
static int esp_algorithm_run_image(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap)
{
struct working_area **mem_handles = NULL;
if (!run || !run->hw)
return ERROR_FAIL;
int retval = run->hw->algo_init(target, run, num_args, ap);
if (retval != ERROR_OK)
return retval;
if (run->mem_args.count > 0) {
mem_handles = calloc(run->mem_args.count, sizeof(*mem_handles));
if (!mem_handles) {
LOG_ERROR("Failed to alloc target mem handles!");
retval = ERROR_FAIL;
goto _cleanup;
}if (!mem_handles) { ... }
for (uint32_t i = 0; i < run->mem_args.count; i++) {
/* ... */
uint32_t usr_param_num = run->mem_args.params[i].address;
static struct working_area *area;
retval = target_alloc_working_area(target, run->mem_args.params[i].size, &area);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to alloc target buffer!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _cleanup;
}if (retval != ERROR_OK) { ... }
mem_handles[i] = area;
run->mem_args.params[i].address = area->address;
if (usr_param_num != UINT_MAX)
esp_algorithm_user_arg_set_uint(run, usr_param_num, run->mem_args.params[i].address);
}for (uint32_t i = 0; i < run->mem_args.count; i++) { ... }
}if (run->mem_args.count > 0) { ... }
if (run->usr_func_init) {
retval = run->usr_func_init(target, run, run->usr_func_arg);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to prepare algorithm host side args stub (%d)!", retval);
goto _cleanup;
}if (retval != ERROR_OK) { ... }
}if (run->usr_func_init) { ... }
LOG_DEBUG("Algorithm start @ " TARGET_ADDR_FMT ", stack %d bytes @ " TARGET_ADDR_FMT,
run->stub.tramp_mapped_addr, run->stack_size, run->stub.stack_addr);
retval = target_start_algorithm(target,
run->mem_args.count, run->mem_args.params,
run->reg_args.count, run->reg_args.params,
run->stub.tramp_mapped_addr, 0,
run->stub.ainfo);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to start algorithm (%d)!", retval);
goto _cleanup;
}if (retval != ERROR_OK) { ... }
if (run->usr_func) {
alive_sleep(100);
retval = run->usr_func(target, run->usr_func_arg);
if (retval != ERROR_OK)
LOG_ERROR("Failed to exec algorithm user func (%d)!", retval);
}if (run->usr_func) { ... }
uint32_t timeout_ms = 0;
if (retval == ERROR_OK)
timeout_ms = run->timeout_ms ? run->timeout_ms : DEFAULT_ALGORITHM_TIMEOUT_MS;
LOG_DEBUG("Wait algorithm completion");
retval = target_wait_algorithm(target,
run->mem_args.count, run->mem_args.params,
run->reg_args.count, run->reg_args.params,
0, timeout_ms,
run->stub.ainfo);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to wait algorithm (%d)!", retval);
}if (retval != ERROR_OK) { ... }
esp_algorithm_read_stub_logs(target, &run->stub);
if (run->usr_func_done)
run->usr_func_done(target, run, run->usr_func_arg);
if (retval != ERROR_OK) {
LOG_ERROR("Algorithm run failed (%d)!", retval);
}if (retval != ERROR_OK) { ... } else {
run->ret_code = esp_algorithm_user_arg_get_uint(run, 0);
LOG_DEBUG("Got algorithm RC 0x%" PRIx32, run->ret_code);
}else { ... }
_cleanup:
if (mem_handles) {
for (uint32_t i = 0; i < run->mem_args.count; i++) {
if (mem_handles[i])
target_free_working_area(target, mem_handles[i]);
}for (uint32_t i = 0; i < run->mem_args.count; i++) { ... }
free(mem_handles);
}if (mem_handles) { ... }
run->hw->algo_cleanup(target, run);
return retval;
}{ ... }
static int esp_algorithm_run_debug_stub(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap)
{
if (!run || !run->hw)
return ERROR_FAIL;
int retval = run->hw->algo_init(target, run, num_args, ap);
if (retval != ERROR_OK)
return retval;
LOG_DEBUG("Algorithm start @ " TARGET_ADDR_FMT ", stack %d bytes @ " TARGET_ADDR_FMT,
run->stub.tramp_mapped_addr, run->stack_size, run->stub.stack_addr);
retval = target_start_algorithm(target,
run->mem_args.count, run->mem_args.params,
run->reg_args.count, run->reg_args.params,
run->stub.tramp_mapped_addr, 0,
run->stub.ainfo);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to start algorithm (%d)!", retval);
goto _cleanup;
}if (retval != ERROR_OK) { ... }
uint32_t timeout_ms = 0;
if (retval == ERROR_OK)
timeout_ms = run->timeout_ms ? run->timeout_ms : DEFAULT_ALGORITHM_TIMEOUT_MS;
LOG_DEBUG("Wait algorithm completion");
retval = target_wait_algorithm(target,
run->mem_args.count, run->mem_args.params,
run->reg_args.count, run->reg_args.params,
0, timeout_ms,
run->stub.ainfo);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to wait algorithm (%d)!", retval);
}if (retval != ERROR_OK) { ... }
if (retval != ERROR_OK) {
LOG_ERROR("Algorithm run failed (%d)!", retval);
}if (retval != ERROR_OK) { ... } else {
run->ret_code = esp_algorithm_user_arg_get_uint(run, 0);
LOG_DEBUG("Got algorithm RC 0x%" PRIx32, run->ret_code);
}else { ... }
_cleanup:
run->hw->algo_cleanup(target, run);
return retval;
}{ ... }
static void reverse_binary(const uint8_t *src, uint8_t *dest, size_t length)
{
size_t remaining = length % 4;
size_t offset = 0;
size_t aligned_len = ALIGN_UP(length, 4);
if (remaining > 0) {
memset(dest + remaining, 0xFF, 4 - remaining);
for (size_t i = 0; i < remaining; i++)
dest[i] = src[length - remaining + i];
length -= remaining;
offset = 4;
}if (remaining > 0) { ... }
for (size_t i = offset; i < aligned_len; i += 4) {
dest[i + 0] = src[length - i + offset - 4];
dest[i + 1] = src[length - i + offset - 3];
dest[i + 2] = src[length - i + offset - 2];
dest[i + 3] = src[length - i + offset - 1];
}for (size_t i = offset; i < aligned_len; i += 4) { ... }
}{ ... }
static int load_section_from_image(struct target *target,
struct esp_algorithm_run_data *run,
int section_num,
bool reverse)
{
if (!run)
return ERROR_FAIL;
struct imagesection *section = &run->image.image.sections[section_num];
uint32_t sec_wr = 0;
uint8_t buf[1024];
assert(sizeof(buf) % 4 == 0);
while (sec_wr < section->size) {
uint32_t nb = section->size - sec_wr > sizeof(buf) ? sizeof(buf) : section->size - sec_wr;
size_t size_read = 0;
int retval = image_read_section(&run->image.image, section_num, sec_wr, nb, buf, &size_read);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to read stub section (%d)!", retval);
return retval;
}if (retval != ERROR_OK) { ... }
if (reverse) {
size_t aligned_len = ALIGN_UP(size_read, 4);
uint8_t reversed_buf[aligned_len];
reverse_binary(buf, reversed_buf, size_read);
/* ... */
retval = target_write_buffer(target, run->image.dram_org - sec_wr - aligned_len, aligned_len, reversed_buf);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to write stub section!");
return retval;
}if (retval != ERROR_OK) { ... }
}if (reverse) { ... } else {
retval = target_write_buffer(target, section->base_address + sec_wr, size_read, buf);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to write stub section!");
return retval;
}if (retval != ERROR_OK) { ... }
}else { ... }
sec_wr += size_read;
}while (sec_wr < section->size) { ... }
return ERROR_OK;
}{ ... }
/* ... */
int esp_algorithm_load_func_image(struct target *target, struct esp_algorithm_run_data *run)
{
int retval;
size_t tramp_sz = 0;
const uint8_t *tramp = NULL;
struct duration algo_time;
bool alloc_code_working_area = true;
if (!run || !run->hw)
return ERROR_FAIL;
if (duration_start(&algo_time) != 0) {
LOG_ERROR("Failed to start algo time measurement!");
return ERROR_FAIL;
}if (duration_start(&algo_time) != 0) { ... }
if (run->hw->stub_tramp_get) {
tramp = run->hw->stub_tramp_get(target, &tramp_sz);
if (!tramp)
return ERROR_FAIL;
}if (run->hw->stub_tramp_get) { ... }
LOG_DEBUG("stub: base 0x%x, start 0x%" PRIx32 ", %d sections",
run->image.image.base_address_set ? (unsigned int)run->image.image.base_address : 0,
run->image.image.start_address,
run->image.image.num_sections);
run->stub.entry = run->image.image.start_address;
/* ... */
if (run->image.reverse) {
if (target_alloc_working_area(target, run->image.iram_len, &run->stub.code) != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc space for stub code!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (target_alloc_working_area(target, run->image.iram_len, &run->stub.code) != ERROR_OK) { ... }
alloc_code_working_area = false;
}if (run->image.reverse) { ... }
uint32_t code_size = 0;
for (unsigned int i = 0; i < run->image.image.num_sections; i++) {
struct imagesection *section = &run->image.image.sections[i];
if (section->size == 0)
continue;
if (section->flags & ESP_IMAGE_ELF_PHF_EXEC) {
LOG_DEBUG("addr " TARGET_ADDR_FMT ", sz %d, flags %" PRIx64,
section->base_address, section->size, section->flags);
if (alloc_code_working_area) {
retval = target_alloc_working_area(target, section->size, &run->stub.code);
if (retval != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc space for stub code!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (retval != ERROR_OK) { ... }
}if (alloc_code_working_area) { ... }
if (section->base_address == 0) {
section->base_address = run->stub.code->address;
}if (section->base_address == 0) { ... } else if (run->stub.code->address != section->base_address) {
LOG_ERROR("working area " TARGET_ADDR_FMT " and stub code section " TARGET_ADDR_FMT
" address mismatch!",
section->base_address,
run->stub.code->address);
retval = ERROR_FAIL;
goto _on_error;
}else if (run->stub.code->address != section->base_address) { ... }
retval = load_section_from_image(target, run, i, run->image.reverse);
if (retval != ERROR_OK)
goto _on_error;
code_size += ALIGN_UP(section->size, 4);
break;
}if (section->flags & ESP_IMAGE_ELF_PHF_EXEC) { ... }
}for (unsigned int i = 0; i < run->image.image.num_sections; i++) { ... }
if (tramp) {
if (run->stub.tramp_addr == 0) {
if (alloc_code_working_area) {
if (target_alloc_working_area(target, tramp_sz, &run->stub.tramp) != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc space for stub jumper!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (target_alloc_working_area(target, tramp_sz, &run->stub.tramp) != ERROR_OK) { ... }
run->stub.tramp_addr = run->stub.tramp->address;
}if (alloc_code_working_area) { ... }
}if (run->stub.tramp_addr == 0) { ... }
size_t al_tramp_size = ALIGN_UP(tramp_sz, 4);
if (run->image.reverse) {
target_addr_t reversed_tramp_addr = run->image.dram_org - code_size;
uint8_t reversed_tramp[al_tramp_size];
reverse_binary(tramp, reversed_tramp, tramp_sz);
run->stub.tramp_addr = reversed_tramp_addr - al_tramp_size;
LOG_DEBUG("Write reversed tramp to addr " TARGET_ADDR_FMT ", sz %zu", run->stub.tramp_addr, al_tramp_size);
retval = target_write_buffer(target, run->stub.tramp_addr, al_tramp_size, reversed_tramp);
}if (run->image.reverse) { ... } else {
LOG_DEBUG("Write tramp to addr " TARGET_ADDR_FMT ", sz %zu", run->stub.tramp_addr, tramp_sz);
retval = target_write_buffer(target, run->stub.tramp_addr, tramp_sz, tramp);
}else { ... }
if (retval != ERROR_OK) {
LOG_ERROR("Failed to write stub jumper!");
goto _on_error;
}if (retval != ERROR_OK) { ... }
run->stub.tramp_mapped_addr = run->image.iram_org + code_size;
code_size += al_tramp_size;
LOG_DEBUG("Tramp mapped to addr " TARGET_ADDR_FMT, run->stub.tramp_mapped_addr);
}if (tramp) { ... }
if (alloc_code_working_area) {
uint32_t backup_working_area_prev = target->backup_working_area;
target->backup_working_area = 0;
if (target_alloc_working_area(target, run->image.iram_len - code_size, &run->stub.padding) != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc space for stub code!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (target_alloc_working_area(target, run->image.iram_len - code_size, &run->stub.padding) != ERROR_OK) { ... }
target->backup_working_area = backup_working_area_prev;
}if (alloc_code_working_area) { ... }
for (unsigned int i = 0; i < run->image.image.num_sections; i++) {
struct imagesection *section = &run->image.image.sections[i];
if (section->size == 0)
continue;
if (!(section->flags & ESP_IMAGE_ELF_PHF_EXEC)) {
LOG_DEBUG("addr " TARGET_ADDR_FMT ", sz %d, flags %" PRIx64, section->base_address, section->size,
section->flags);
/* ... */
uint32_t data_sec_sz = ALIGN_UP(section->size, 4);
LOG_DEBUG("DATA sec size %" PRIu32 " -> %" PRIu32, section->size, data_sec_sz);
uint32_t bss_sec_sz = ALIGN_UP(run->image.bss_size, 4);
LOG_DEBUG("BSS sec size %" PRIu32 " -> %" PRIu32, run->image.bss_size, bss_sec_sz);
if (target_alloc_working_area(target, data_sec_sz + bss_sec_sz, &run->stub.data) != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc space for stub data!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (target_alloc_working_area(target, data_sec_sz + bss_sec_sz, &run->stub.data) != ERROR_OK) { ... }
if (section->base_address == 0) {
section->base_address = run->stub.data->address;
}if (section->base_address == 0) { ... } else if (run->stub.data->address != section->base_address) {
LOG_ERROR("working area " TARGET_ADDR_FMT
" and stub data section " TARGET_ADDR_FMT
" address mismatch!",
section->base_address,
run->stub.data->address);
retval = ERROR_FAIL;
goto _on_error;
}else if (run->stub.data->address != section->base_address) { ... }
retval = load_section_from_image(target, run, i, false);
if (retval != ERROR_OK)
goto _on_error;
}if (!(section->flags & ESP_IMAGE_ELF_PHF_EXEC)) { ... }
}for (unsigned int i = 0; i < run->image.image.num_sections; i++) { ... }
if (run->stub.stack_addr == 0 && run->stack_size > 0) {
if (target_alloc_working_area(target, run->stack_size, &run->stub.stack) != ERROR_OK) {
LOG_ERROR("no working area available, can't alloc stub stack!");
retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
goto _on_error;
}if (target_alloc_working_area(target, run->stack_size, &run->stub.stack) != ERROR_OK) { ... }
run->stub.stack_addr = run->stub.stack->address + run->stack_size;
}if (run->stub.stack_addr == 0 && run->stack_size > 0) { ... }
if (duration_measure(&algo_time) != 0) {
LOG_ERROR("Failed to stop algo run measurement!");
retval = ERROR_FAIL;
goto _on_error;
}if (duration_measure(&algo_time) != 0) { ... }
LOG_DEBUG("Stub loaded in %g ms", duration_elapsed(&algo_time) * 1000);
return ERROR_OK;
_on_error:
esp_algorithm_unload_func_image(target, run);
return retval;
}{ ... }
int esp_algorithm_unload_func_image(struct target *target, struct esp_algorithm_run_data *run)
{
if (!run)
return ERROR_FAIL;
target_free_all_working_areas(target);
run->stub.tramp = NULL;
run->stub.stack = NULL;
run->stub.code = NULL;
run->stub.data = NULL;
run->stub.padding = NULL;
return ERROR_OK;
}{ ... }
int esp_algorithm_exec_func_image_va(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap)
{
if (!run || !run->image.image.start_address_set || run->image.image.start_address == 0)
return ERROR_FAIL;
return esp_algorithm_run_image(target, run, num_args, ap);
}{ ... }
int esp_algorithm_load_onboard_func(struct target *target, target_addr_t func_addr, struct esp_algorithm_run_data *run)
{
int res;
const uint8_t *tramp = NULL;
size_t tramp_sz = 0;
struct duration algo_time;
if (!run || !run->hw)
return ERROR_FAIL;
if (duration_start(&algo_time) != 0) {
LOG_ERROR("Failed to start algo time measurement!");
return ERROR_FAIL;
}if (duration_start(&algo_time) != 0) { ... }
if (run->hw->stub_tramp_get) {
tramp = run->hw->stub_tramp_get(target, &tramp_sz);
if (!tramp)
return ERROR_FAIL;
}if (run->hw->stub_tramp_get) { ... }
if (tramp_sz > run->on_board.code_buf_size) {
LOG_ERROR("Stub tramp size %zu bytes exceeds target buf size %d bytes!",
tramp_sz, run->on_board.code_buf_size);
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (tramp_sz > run->on_board.code_buf_size) { ... }
if (run->stack_size > run->on_board.min_stack_size) {
LOG_ERROR("Algorithm stack size not fit into the allocated target stack!");
return ERROR_FAIL;
}if (run->stack_size > run->on_board.min_stack_size) { ... }
run->stub.stack_addr = run->on_board.min_stack_addr + run->stack_size;
run->stub.tramp_addr = run->on_board.code_buf_addr;
run->stub.tramp_mapped_addr = run->stub.tramp_addr;
run->stub.entry = func_addr;
if (tramp) {
res = target_write_buffer(target, run->stub.tramp_addr, tramp_sz, tramp);
if (res != ERROR_OK) {
LOG_ERROR("Failed to write stub jumper!");
esp_algorithm_unload_onboard_func(target, run);
return res;
}if (res != ERROR_OK) { ... }
}if (tramp) { ... }
if (duration_measure(&algo_time) != 0) {
LOG_ERROR("Failed to stop algo run measurement!");
return ERROR_FAIL;
}if (duration_measure(&algo_time) != 0) { ... }
LOG_DEBUG("Stub loaded in %g ms", duration_elapsed(&algo_time) * 1000);
return ERROR_OK;
}{ ... }
int esp_algorithm_unload_onboard_func(struct target *target, struct esp_algorithm_run_data *run)
{
return ERROR_OK;
}{ ... }
int esp_algorithm_exec_onboard_func_va(struct target *target,
struct esp_algorithm_run_data *run,
uint32_t num_args,
va_list ap)
{
return esp_algorithm_run_debug_stub(target, run, num_args, ap);
}{ ... }