1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Espressif chips common algorithm API for OpenOCD *
5 * Copyright (C) 2022 Espressif Systems Ltd. *
6 ***************************************************************************/
8 #ifndef OPENOCD_TARGET_ESP_ALGORITHM_H
9 #define OPENOCD_TARGET_ESP_ALGORITHM_H
11 #include "helper/log.h"
12 #include "helper/binarybuffer.h"
13 #include <helper/time_support.h>
14 #include <target/algorithm.h>
15 #include <target/image.h>
18 * API defined below allows executing pieces of code on target without breaking the execution of the running program.
19 * This functionality can be useful for various debugging and maintenance procedures.
20 * @note ESP flashing code to load flasher stub on target and write/read/erase flash.
21 * Also ESP GCOV command uses some of these functions to run onboard routines to dump coverage info.
22 * Stub entry function can take up to 5 arguments and should be of the following form:
24 * int stub_entry([uint32_t a1 [, uint32_t a2 [, uint32_t a3 [, uint32_t a4 [, uint32_t a5]]]]]);
26 * The general scheme of stub code execution is shown below.
28 * ------- ----------- (initial frame) ----
29 * | | -------(registers, stub entry, stub args)------> |trampoline | ---(stub args)---> | |
31 * |OpenOCD| <----------(stub-specific communications)---------------------------------------> |stub|
33 * | | <---------(target halted event, ret code)------- |tramp break| <---(ret code)---- | |
34 * ------- ----------- ----
36 * Procedure of executing stub on target includes:
37 * 1) User prepares struct esp_algorithm_run_data and calls one of algorithm_run_xxx() functions.
38 * 2) Routine allocates all necessary stub code and data sections.
39 * 3) If a user specifies an initializer func esp_algorithm_usr_func_init_t it is called just before the stub starts.
40 * 4) If user specifies stub communication func esp_algorithm_usr_func_t (@see esp_flash_write/read in ESP flash driver)
41 * it is called just after the stub starts. When communication with stub is finished this function must return.
42 * 5) OpenOCD waits for the stub to finish (hit exit breakpoint).
43 * 6) If the user specified arguments cleanup func esp_algorithm_usr_func_done_t,
44 * it is called just after the stub finishes.
46 * There are two options to run code on target under OpenOCD control:
47 * - Run externally compiled stub code.
48 * - Run onboard pre-compiled code. @note For ESP chips debug stubs must be enabled in target code @see ESP IDF docs.
49 * The main difference between the execution of external stub code and target built-in functions is that
50 * in the latter case working areas can not be used to allocate target memory for code and data because they can overlap
51 * with code and data involved in onboard function execution. For example, if memory allocated in the working area
52 * for the stub stack will overlap with some on-board data used by the stub the stack will get overwritten.
53 * The same stands for allocations in target code space.
55 * External Code Execution
56 * -----------------------
57 * To run external code on the target user should use esp_algorithm_run_func_image().
58 * In this case all necessary memory (code/data) is allocated in working areas that have fixed configuration
59 * defined in target TCL file. Stub code is actually a standalone program, so all its segments must have known
60 * addresses due to position-dependent code nature. So stub must be linked in such a way that its code segment
61 * starts at the beginning of the working area for code space defined in TCL. The same restriction must be applied
62 * to stub's data segment and base addresses of working area for data space. @see ESP stub flasher LD scripts.
63 * Also in order to simplify memory allocation BSS section must follow the DATA section in the stub image.
64 * The size of the BSS section must be specified in the bss_size field of struct algorithm_image.
65 * Sample stub memory map is shown below.
66 * ___________________________________________
67 * | data space working area start |
69 * | <stub .data segment> |
70 * |___________________________________________|
73 * | <stub .bss segment of size 'bss_size'> |
74 * |___________________________________________|
78 * |___________________________________________|
81 * |___________________________________________|
84 * |___________________________________________|
85 * ___________________________________________
86 * | code space working area start |
88 * | <stub .text segment> |
89 * |___________________________________________|
91 * | <stub trampoline with exit breakpoint> |
92 * |___________________________________________|
94 * For example on how to execute external code with memory arguments @see esp_algo_flash_blank_check in
97 * On-Board Code Execution
98 * -----------------------
99 * To run on-board code on the target user should use esp_algorithm_run_onboard_func().
100 * On-board code execution process does not need to allocate target memory for stub code and data,
101 * Because the stub is pre-compiled to the code running on the target.
102 * But it still needs memory for stub trampoline, stack, and memory arguments.
103 * Working areas can not be used due to possible memory layout conflicts with on-board stub code and data.
104 * Debug stubs functionality provided by ESP IDF allows OpenOCD to overcome the above problem.
105 * It provides a special descriptor which provides info necessary to safely allocate memory on target.
106 * @see struct esp_dbg_stubs_desc.
107 * That info is also used to locate memory for stub trampoline code.
108 * User can execute target function at any address, but @see ESP IDF debug stubs also provide a way to pass to the host
109 * an entry address of pre-defined registered stub functions.
110 * For example of an on-board code execution @see esp32_cmd_gcov() in ESP32 apptrace module.
114 * Algorithm image data.
115 * Helper struct to work with algorithms consisting of code and data segments.
117 struct esp_algorithm_image
{
120 /** BSS section size. */
122 /** IRAM start address in the linker script */
124 /** Total reserved IRAM size */
126 /** DRAM start address in the linker script */
128 /** Total reserved DRAM size */
130 /** IRAM DRAM address range reversed or not */
134 #define ESP_IMAGE_ELF_PHF_EXEC 0x1
137 * Algorithm stub data.
139 struct esp_algorithm_stub
{
142 /** Working area for code segment. */
143 struct working_area
*code
;
144 /** Working area for data segment. */
145 struct working_area
*data
;
146 /** Working area for trampoline. */
147 struct working_area
*tramp
;
148 /** Working area for padding between code and data area. */
149 struct working_area
*padding
;
150 /** Address of the target buffer for stub trampoline. If zero tramp->address will be used. */
151 target_addr_t tramp_addr
;
152 /** Tramp code area will be filled from dbus.
153 * We need to map it to the ibus to be able to initialize PC register to start algorithm execution from.
155 target_addr_t tramp_mapped_addr
;
156 /** Working area for stack. */
157 struct working_area
*stack
;
158 /** Address of the target buffer for stack. If zero tramp->address will be used. */
159 target_addr_t stack_addr
;
160 /** Address of the log buffer */
161 target_addr_t log_buff_addr
;
162 /** Size of the log buffer */
163 uint32_t log_buff_size
;
164 /** Algorithm's arch-specific info. */
169 * Algorithm stub in-memory arguments.
171 struct esp_algorithm_mem_args
{
172 /** Memory params. */
173 struct mem_param
*params
;
174 /** Number of memory params. */
179 * Algorithm stub register arguments.
181 struct esp_algorithm_reg_args
{
182 /** Algorithm register params. User args start from user_first_reg_param */
183 struct reg_param
*params
;
184 /** Number of register params. */
186 /** The first several reg_params can be used by stub itself (e.g. for trampoline).
187 * This is the index of the first reg_param available for user to pass args to algorithm stub. */
188 uint32_t first_user_param
;
191 struct esp_algorithm_run_data
;
194 * @brief Algorithm run function.
196 * @param target Pointer to target.
197 * @param run Pointer to algo run data.
198 * @param arg Function specific argument.
200 * @return ERROR_OK on success, otherwise ERROR_XXX.
202 typedef int (*esp_algorithm_func_t
)(struct target
*target
, struct esp_algorithm_run_data
*run
, void *arg
);
205 * @brief Host part of algorithm.
206 * This function will be called while stub is running on target.
207 * It can be used for communication with stub.
209 * @param target Pointer to target.
210 * @param usr_arg Function specific argument.
212 * @return ERROR_OK on success, otherwise ERROR_XXX.
214 typedef int (*esp_algorithm_usr_func_t
)(struct target
*target
, void *usr_arg
);
217 * @brief Algorithm's arguments setup function.
218 * This function will be called just before stub start.
219 * It must return when all operations with running stub are completed.
220 * It can be used to prepare stub memory parameters.
222 * @param target Pointer to target.
223 * @param run Pointer to algo run data.
224 * @param usr_arg Function specific argument. The same as for esp_algorithm_usr_func_t.
226 * @return ERROR_OK on success, otherwise ERROR_XXX.
228 typedef int (*esp_algorithm_usr_func_init_t
)(struct target
*target
,
229 struct esp_algorithm_run_data
*run
,
233 * @brief Algorithm's arguments cleanup function.
234 * This function will be called just after stub exit.
235 * It can be used to cleanup stub memory parameters.
237 * @param target Pointer to target.
238 * @param run Pointer to algo run data.
239 * @param usr_arg Function specific argument. The same as for esp_algorithm_usr_func_t.
241 * @return ERROR_OK on success, otherwise ERROR_XXX.
243 typedef void (*esp_algorithm_usr_func_done_t
)(struct target
*target
,
244 struct esp_algorithm_run_data
*run
,
247 struct esp_algorithm_hw
{
248 int (*algo_init
)(struct target
*target
, struct esp_algorithm_run_data
*run
, uint32_t num_args
, va_list ap
);
249 int (*algo_cleanup
)(struct target
*target
, struct esp_algorithm_run_data
*run
);
250 const uint8_t *(*stub_tramp_get
)(struct target
*target
, size_t *size
);
254 * Algorithm run data.
256 struct esp_algorithm_run_data
{
257 /** Algorithm completion timeout in ms. If 0, default value will be used */
259 /** Algorithm stack size. */
261 /** Algorithm register arguments. */
262 struct esp_algorithm_reg_args reg_args
;
263 /** Algorithm memory arguments. */
264 struct esp_algorithm_mem_args mem_args
;
265 /** Algorithm arch-specific info. For Xtensa this should point to struct xtensa_algorithm. */
267 /** Algorithm return code. */
270 struct esp_algorithm_stub stub
;
273 /** Size of the pre-alocated on-board buffer for stub's code. */
274 uint32_t code_buf_size
;
275 /** Address of pre-compiled target buffer for stub trampoline. */
276 target_addr_t code_buf_addr
;
277 /** Size of the pre-alocated on-board buffer for stub's stack. */
278 uint32_t min_stack_size
;
279 /** Pre-compiled target buffer's addr for stack. */
280 target_addr_t min_stack_addr
;
282 struct esp_algorithm_image image
;
284 /** Host side algorithm function argument. */
286 /** Host side algorithm function. */
287 esp_algorithm_usr_func_t usr_func
;
288 /** Host side algorithm function setup routine. */
289 esp_algorithm_usr_func_init_t usr_func_init
;
290 /** Host side algorithm function cleanup routine. */
291 esp_algorithm_usr_func_done_t usr_func_done
;
292 /** Algorithm run function: see algorithm_run_xxx for example. */
293 esp_algorithm_func_t algo_func
;
294 /** HW specific API */
295 const struct esp_algorithm_hw
*hw
;
298 int esp_algorithm_load_func_image(struct target
*target
, struct esp_algorithm_run_data
*run
);
299 int esp_algorithm_unload_func_image(struct target
*target
, struct esp_algorithm_run_data
*run
);
301 int esp_algorithm_exec_func_image_va(struct target
*target
,
302 struct esp_algorithm_run_data
*run
,
307 * @brief Loads and runs stub from specified image.
308 * This function should be used to run external stub code on target.
310 * @param target Pointer to target.
311 * @param run Pointer to algo run data.
312 * @param num_args Number of stub arguments that follow.
314 * @return ERROR_OK on success, otherwise ERROR_XXX. Stub return code is in run->ret_code.
316 static inline int esp_algorithm_run_func_image_va(struct target
*target
,
317 struct esp_algorithm_run_data
*run
,
321 int ret
= esp_algorithm_load_func_image(target
, run
);
324 ret
= esp_algorithm_exec_func_image_va(target
, run
, num_args
, ap
);
325 int rc
= esp_algorithm_unload_func_image(target
, run
);
326 return ret
!= ERROR_OK
? ret
: rc
;
329 static inline int esp_algorithm_run_func_image(struct target
*target
,
330 struct esp_algorithm_run_data
*run
,
335 va_start(ap
, num_args
);
336 int retval
= esp_algorithm_run_func_image_va(target
, run
, num_args
, ap
);
341 int esp_algorithm_load_onboard_func(struct target
*target
,
342 target_addr_t func_addr
,
343 struct esp_algorithm_run_data
*run
);
344 int esp_algorithm_unload_onboard_func(struct target
*target
, struct esp_algorithm_run_data
*run
);
345 int esp_algorithm_exec_onboard_func_va(struct target
*target
,
346 struct esp_algorithm_run_data
*run
,
351 * @brief Runs pre-compiled on-board function.
352 * This function should be used to run on-board stub code.
354 * @param target Pointer to target.
355 * @param run Pointer to algo run data.
356 * @param func_entry Address of the function to run.
357 * @param num_args Number of function arguments that follow.
359 * @return ERROR_OK on success, otherwise ERROR_XXX. Stub return code is in run->ret_code.
361 static inline int esp_algorithm_run_onboard_func_va(struct target
*target
,
362 struct esp_algorithm_run_data
*run
,
363 target_addr_t func_addr
,
367 int ret
= esp_algorithm_load_onboard_func(target
, func_addr
, run
);
370 ret
= esp_algorithm_exec_onboard_func_va(target
, run
, num_args
, ap
);
373 return esp_algorithm_unload_onboard_func(target
, run
);
376 static inline int esp_algorithm_run_onboard_func(struct target
*target
,
377 struct esp_algorithm_run_data
*run
,
378 target_addr_t func_addr
,
383 va_start(ap
, num_args
);
384 int retval
= esp_algorithm_run_onboard_func_va(target
, run
, func_addr
, num_args
, ap
);
390 * @brief Set the value of an argument passed via registers to the stub main function.
392 static inline void esp_algorithm_user_arg_set_uint(struct esp_algorithm_run_data
*run
,
396 struct reg_param
*param
= &run
->reg_args
.params
[run
->reg_args
.first_user_param
+ arg_num
];
398 assert(param
->size
<= 64);
400 if (param
->size
<= 32)
401 buf_set_u32(param
->value
, 0, param
->size
, val
);
403 buf_set_u64(param
->value
, 0, param
->size
, val
);
407 * @brief Get the value of an argument passed via registers from the stub main function.
409 static inline uint64_t esp_algorithm_user_arg_get_uint(struct esp_algorithm_run_data
*run
, int arg_num
)
411 struct reg_param
*param
= &run
->reg_args
.params
[run
->reg_args
.first_user_param
+ arg_num
];
413 assert(param
->size
<= 64);
415 if (param
->size
<= 32)
416 return buf_get_u32(param
->value
, 0, param
->size
);
417 return buf_get_u64(param
->value
, 0, param
->size
);
420 #endif /* OPENOCD_TARGET_ESP_ALGORITHM_H */
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)