Skip to content

Commit a58afdd

Browse files
committed
fix: clean up error codes
1 parent 6af3d44 commit a58afdd

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

example/stub_main.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,29 @@ __asm__(
7979
"j stub_main;");
8080
#endif
8181

82+
const char *stub_err_str(int ret_code)
83+
{
84+
switch (ret_code) {
85+
case ESP_STUB_FAIL:
86+
return "Generic error";
87+
case ESP_STUB_OK:
88+
return "Success";
89+
90+
case STUB_LIB_FAIL:
91+
return "Failure in the lib";
92+
case STUB_LIB_ERR_UNKNOWN_FLASH_ID:
93+
return "Unknown flash ID";
94+
95+
default:
96+
return "Unknown stub error";
97+
}
98+
}
99+
82100
int stub_main(int cmd, ...)
83101
{
84102
va_list ap;
85103
void *flash_state = NULL;
86104
int ret = ESP_STUB_FAIL;
87-
stub_lib_err_t rc = STUB_LIB_FAIL;
88105

89106
/* zero bss */
90107
for (uint32_t *p = &_bss_start; p < &_bss_end; p++) {
@@ -95,20 +112,21 @@ int stub_main(int cmd, ...)
95112

96113
STUB_LOG_INIT();
97114

98-
rc = stub_lib_flash_init(&flash_state);
99-
if (rc != STUB_LIB_OK) {
100-
return ESP_STUB_FAIL;
101-
}
102-
103115
STUB_LOGI("Command: 0x%x\n", cmd);
104116

117+
int lib_ret = stub_lib_flash_init(&flash_state);
118+
if (lib_ret != STUB_LIB_OK) {
119+
STUB_LOGE("Flash init failure: (0x%X) %s\n", lib_ret, stub_err_str(lib_ret));
120+
return lib_ret;
121+
}
122+
105123
const struct stub_cmd_handler *handler = cmd_handlers;
106124
while (handler->handler) {
107125
if (handler->cmd == cmd) {
108126
STUB_LOGI("Executing command: %s\n", handler->name);
109127
ret = handler->handler(ap);
110128
if (ret != ESP_STUB_OK) {
111-
STUB_LOGE("Command %s (0x%x) failed\n", handler->name, handler->cmd);
129+
STUB_LOGE("Command %s (0x%x) failed: (0x%X) %s\n", handler->name, handler->cmd, ret, stub_err_str(ret));
112130
goto flash_va_end;
113131
}
114132
break;
@@ -118,6 +136,7 @@ int stub_main(int cmd, ...)
118136

119137
if (!handler->handler) {
120138
STUB_LOGE("Unknown command (0x%x)!\n", cmd);
139+
ret = ESP_STUB_ERR_CMD_NOT_SUPPORTED;
121140
}
122141

123142
flash_va_end:

example/stub_main.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ enum esp_stub_cmd {
1313
ESP_STUB_CMD_FLASH_MAX_ID = 0x01,
1414
};
1515

16-
// Stub return codes
17-
#define ESP_STUB_OK 0
18-
#define ESP_STUB_FAIL -1
16+
// Stub return codes, all compatible with esp-stub-lib/err.h without overlap
17+
18+
#define ESP_STUB_OK 0x0 // Equal to STUB_LIB_OK
19+
#define ESP_STUB_FAIL 0x1 // Different from STUB_LIB_FAIL
20+
21+
#define ESP_STUB_ERR_CMD_NOT_SUPPORTED 0x2

include/esp-stub-lib/err.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@
66

77
#pragma once
88

9-
typedef int stub_lib_err_t;
9+
/**
10+
* @file
11+
* @brief Error codes used in esp-stub-lib
12+
*
13+
* Please use int for return codes
14+
*
15+
* The error code range of esp-stub-lib starts at STUB_LIB_ERROR_BASE, intentionally chosen to distinguish library codes
16+
*/
1017

11-
#define STUB_LIB_OK 0 // stub_lib_err_t value indicating success (no error)
12-
#define STUB_LIB_FAIL -1 // Generic stub_lib_err_t code indicating failure
18+
#define STUB_LIB_ERROR_BASE 0x10000000
1319

14-
#define STUB_LIB_ERR_UNKNOWN_FLASH_ID 0x1
20+
#define STUB_LIB_OK 0 /**< Success (no error) */
21+
#define STUB_LIB_FAIL STUB_LIB_ERROR_BASE /**< A generic error code. Prefer specific codes instead. */
22+
23+
/** @name Specific error codes
24+
* @{
25+
*/
26+
#define STUB_LIB_ERR_UNKNOWN_FLASH_ID (STUB_LIB_ERROR_BASE + 0x1)
27+
/** @} */

include/esp-stub-lib/flash.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C" {
3434
* - STUB_LIB_OK - success
3535
* - STUB_LIB_ERR_UNKNOWN_FLASH_ID - can't get size from flash id
3636
*/
37-
stub_lib_err_t stub_lib_flash_init(void **state);
37+
int stub_lib_flash_init(void **state);
3838

3939
/**
4040
* @brief Restore flash state at the end of the stub.
@@ -51,12 +51,12 @@ void stub_lib_flash_deinit(const void *state);
5151
void stub_lib_flash_get_info(stub_lib_flash_info_t *info);
5252

5353
void stub_lib_flash_info_print(const stub_lib_flash_info_t *info);
54-
stub_lib_err_t stub_lib_flash_read_buff(uint32_t addr, void *buffer, uint32_t size);
55-
stub_lib_err_t stub_lib_flash_write_buff(uint32_t addr, const void *buffer, uint32_t size, int encrypt);
56-
stub_lib_err_t stub_lib_flash_erase_area(uint32_t addr, uint32_t size);
57-
stub_lib_err_t stub_lib_flash_erase_sector(uint32_t addr);
58-
stub_lib_err_t stub_lib_flash_erase_block(uint32_t addr);
59-
stub_lib_err_t stub_lib_flash_erase_chip(void);
54+
int stub_lib_flash_read_buff(uint32_t addr, void *buffer, uint32_t size);
55+
int stub_lib_flash_write_buff(uint32_t addr, const void *buffer, uint32_t size, int encrypt);
56+
int stub_lib_flash_erase_area(uint32_t addr, uint32_t size);
57+
int stub_lib_flash_erase_sector(uint32_t addr);
58+
int stub_lib_flash_erase_block(uint32_t addr);
59+
int stub_lib_flash_erase_chip(void);
6060

6161
#ifdef __cplusplus
6262
}

src/flash.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
#include <target/flash.h>
1212
#include <private/rom_flash_config.h>
1313

14-
stub_lib_err_t stub_lib_flash_init(void **state)
14+
int stub_lib_flash_init(void **state)
1515
{
1616
stub_target_flash_init(state);
1717
uint32_t flash_id = stub_target_flash_get_flash_id();
1818
uint32_t flash_size = stub_target_flash_id_to_flash_size(flash_id);
1919
if (flash_size == 0) {
20-
STUB_LOGE("Invalid flash size: 0\n");
2120
return STUB_LIB_ERR_UNKNOWN_FLASH_ID;
2221
}
2322
STUB_LOG_TRACEF("Flash size: %d MB\n", MB(flash_size));

src/target/common/src/flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ uint32_t stub_target_flash_id_to_flash_size(uint32_t flash_id)
4545
return 32 * 1024 * 1024;
4646
}
4747

48-
STUB_LOGE("Unknown flash_id: 0x%x", flash_id);
48+
STUB_LOGE("Unknown flash_id: 0x%x\n", flash_id);
4949
return 0;
5050
}

0 commit comments

Comments
 (0)