Cleanup blisp tool to use error codes

This commit is contained in:
Ben V. Brown 2023-05-09 07:58:18 +10:00
parent 6ec0e9e862
commit bac4f4b49f
2 changed files with 32 additions and 28 deletions

View File

@ -58,21 +58,21 @@ blisp_return_t blisp_common_init_device(struct blisp_device* device,
* Prepares chip to access flash * Prepares chip to access flash
* this means performing handshake, and loading eflash_loader if needed. * this means performing handshake, and loading eflash_loader if needed.
*/ */
int32_t blisp_common_prepare_flash(struct blisp_device* device) { blisp_return_t blisp_common_prepare_flash(struct blisp_device* device) {
int32_t ret = 0; blisp_return_t ret = 0;
printf("Sending a handshake...\n"); printf("Sending a handshake...\n");
ret = blisp_device_handshake(device, false); ret = blisp_device_handshake(device, false);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to handshake with device.\n"); fprintf(stderr, "Failed to handshake with device.\n");
return -1; return ret;
} }
printf("Handshake successful!\nGetting chip info...\n"); printf("Handshake successful!\nGetting chip info...\n");
struct blisp_boot_info boot_info; struct blisp_boot_info boot_info;
ret = blisp_device_get_boot_info(device, &boot_info); ret = blisp_device_get_boot_info(device, &boot_info);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to get boot info.\n"); fprintf(stderr, "Failed to get boot info.\n");
return -1; return ret;
} }
printf( printf(
@ -85,7 +85,7 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
boot_info.chip_id[6], boot_info.chip_id[7]); boot_info.chip_id[6], boot_info.chip_id[7]);
if (device->chip->load_eflash_loader == NULL) { if (device->chip->load_eflash_loader == NULL) {
return 0; return BLISP_OK;
} }
if (boot_info.boot_rom_version[0] == 255 && if (boot_info.boot_rom_version[0] == 255 &&
@ -93,7 +93,7 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
boot_info.boot_rom_version[2] == 255 && boot_info.boot_rom_version[2] == 255 &&
boot_info.boot_rom_version[3] == 255) { boot_info.boot_rom_version[3] == 255) {
printf("Device already in eflash_loader.\n"); printf("Device already in eflash_loader.\n");
return 0; return BLISP_OK;
} }
uint8_t* eflash_loader_buffer = NULL; uint8_t* eflash_loader_buffer = NULL;
@ -110,7 +110,7 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to load eflash_loader, ret: %d\n", ret); fprintf(stderr, "Failed to load eflash_loader, ret: %d\n", ret);
ret = -1;
goto exit1; goto exit1;
} }
@ -120,14 +120,12 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
ret = blisp_device_check_image(device); ret = blisp_device_check_image(device);
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "Failed to check image.\n"); fprintf(stderr, "Failed to check image.\n");
ret = -1;
goto exit1; goto exit1;
} }
ret = blisp_device_run_image(device); ret = blisp_device_run_image(device);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to run image.\n"); fprintf(stderr, "Failed to run image.\n");
ret = -1;
goto exit1; goto exit1;
} }
@ -135,12 +133,12 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
ret = blisp_device_handshake(device, true); ret = blisp_device_handshake(device, true);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to handshake with device.\n"); fprintf(stderr, "Failed to handshake with device.\n");
ret = -1;
goto exit1; goto exit1;
} }
printf("Handshake with eflash_loader successful.\n"); printf("Handshake with eflash_loader successful.\n");
exit1: exit1:
if (eflash_loader_buffer != NULL) if (eflash_loader_buffer != NULL)
free(eflash_loader_buffer); free(eflash_loader_buffer);
return ret; return ret;
} }

View File

@ -14,7 +14,7 @@ static struct arg_lit* version;
static struct arg_end* end; static struct arg_end* end;
static void* argtable[3]; static void* argtable[3];
int8_t args_init() { blisp_return_t args_init() {
argtable[0] = help = arg_lit0(NULL, "help", "print this help and exit"); argtable[0] = help = arg_lit0(NULL, "help", "print this help and exit");
argtable[1] = version = argtable[1] = version =
arg_lit0(NULL, "version", "print version information and exit"); arg_lit0(NULL, "version", "print version information and exit");
@ -22,10 +22,10 @@ int8_t args_init() {
if (arg_nullcheck(argtable) != 0) { if (arg_nullcheck(argtable) != 0) {
fprintf(stderr, "insufficient memory\n"); fprintf(stderr, "insufficient memory\n");
return -1; return BLISP_ERR_OUT_OF_MEMORY;
} }
return 0; return BLISP_OK;
} }
void print_help() { void print_help() {
@ -43,14 +43,14 @@ int8_t args_parse_exec(int argc, char** argv) {
if (error == 0) { if (error == 0) {
if (help->count) { if (help->count) {
print_help(); print_help();
return 1; return BLISP_OK;
} else if (version->count) { } else if (version->count) {
printf("blisp v0.0.3\n"); printf("blisp v0.0.3\n");
printf("Copyright (C) 2023 Marek Kraus and PINE64 Community\n"); printf("Copyright (C) 2023 Marek Kraus and PINE64 Community\n");
return 1; return BLISP_OK;
} }
} }
return 0; return BLISP_ERR_INVALID_COMMAND;
} }
void args_free() { void args_free() {
@ -58,27 +58,29 @@ void args_free() {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
int exit_code = 0; blisp_return_t ret = args_init();
if (ret != 0) {
if (args_init() != 0) {
exit_code = -1;
goto exit; goto exit;
} }
for (uint8_t i = 0; i < cmds_count; i++) { for (uint8_t i = 0; i < cmds_count; i++) {
if (cmds[i]->args_init() != 0) { ret = cmds[i]->args_init();
exit_code = -1; if (ret != BLISP_OK) {
goto exit;
}
}
// Try and parse as a help request
{
ret = args_parse_exec(argc, argv);
if (ret == BLISP_OK) {
goto exit; goto exit;
} }
} }
if (args_parse_exec(argc, argv)) {
goto exit;
}
uint8_t command_found = false; uint8_t command_found = false;
for (uint8_t i = 0; i < cmds_count; i++) { for (uint8_t i = 0; i < cmds_count; i++) {
if (cmds[i]->args_parse_exec(argc, argv)) { ret = cmds[i]->args_parse_exec(argc, argv);
if (ret != BLISP_ERR_INVALID_COMMAND) {
command_found = true; command_found = true;
break; break;
} }
@ -93,5 +95,9 @@ exit:
cmds[i]->args_free(); cmds[i]->args_free();
} }
args_free(); args_free();
return exit_code; // Make error codes more intuitive, but converting to +ve mirror
if (ret < 0) {
ret = -ret;
}
return ret;
} }