diff --git a/.idea/misc.xml b/.idea/misc.xml index 79b3c94..0b76fe5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/include/blisp_easy.h b/include/blisp_easy.h index 15f35cc..66e040a 100644 --- a/include/blisp_easy.h +++ b/include/blisp_easy.h @@ -8,7 +8,10 @@ struct blisp_easy_transport { uint8_t type; // 0 - memory, 1 - FILE file_handle union { - FILE* file_handle; + struct { + FILE* file_handle; + int64_t file_size; + } file; struct { void* data_location; uint32_t data_size; diff --git a/lib/blisp_easy.c b/lib/blisp_easy.c index f96debe..978d910 100644 --- a/lib/blisp_easy.c +++ b/lib/blisp_easy.c @@ -19,7 +19,7 @@ static blisp_return_t blisp_easy_transport_read( transport->data.memory.current_position += size; return size; } else { - return fread(buffer, size, 1, transport->data.file_handle); + return fread(buffer, size, 1, transport->data.file.file_handle); } } @@ -28,9 +28,13 @@ static blisp_return_t blisp_easy_transport_size( if (transport->type == 0) { return transport->data.memory.data_size; } else { - // TODO: Implement - printf("%s() Warning: calling non-implemented function\n", __func__); - return BLISP_ERR_NOT_IMPLEMENTED; + if (transport->data.file.file_size == -1) { + FILE* handle = transport->data.file.file_handle; + fseek(handle, 0, SEEK_END); + transport->data.file.file_size = ftell(handle); + rewind(handle); + } + return transport->data.file.file_size; } } @@ -43,7 +47,7 @@ static void blisp_easy_report_progress(blisp_easy_progress_callback callback, } struct blisp_easy_transport blisp_easy_transport_new_from_file(FILE* file) { - struct blisp_easy_transport transport = {.type = 1, .data.file_handle = file}; + struct blisp_easy_transport transport = {.type = 1, .data.file.file_handle = file, .data.file.file_size = -1}; return transport; } diff --git a/tools/blisp/CMakeLists.txt b/tools/blisp/CMakeLists.txt index b271dc4..9ec4917 100644 --- a/tools/blisp/CMakeLists.txt +++ b/tools/blisp/CMakeLists.txt @@ -2,7 +2,7 @@ set(ARGTABLE3_ENABLE_TESTS OFF CACHE BOOL "Enable unit tests") set(ARGTABLE3_ENABLE_EXAMPLES OFF CACHE BOOL "Enable examples") #set(ARGTABLE3_REPLACE_GETOPT OFF CACHE BOOL "Replace getopt in the system C library") -add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c) +add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c src/cmd/run.c) add_subdirectory(src/file_parsers) diff --git a/tools/blisp/src/cmd.h b/tools/blisp/src/cmd.h index 6d4dbb0..077d43c 100644 --- a/tools/blisp/src/cmd.h +++ b/tools/blisp/src/cmd.h @@ -13,6 +13,7 @@ struct cmd { }; extern struct cmd cmd_write; +extern struct cmd cmd_run; extern struct cmd cmd_iot; #endif // BLISP_CMD_H diff --git a/tools/blisp/src/cmd/iot.c b/tools/blisp/src/cmd/iot.c index 9137765..b88274a 100644 --- a/tools/blisp/src/cmd/iot.c +++ b/tools/blisp/src/cmd/iot.c @@ -22,7 +22,7 @@ blisp_return_t blisp_single_download() { if (ret != BLISP_OK) { return ret; } - ret = blisp_common_prepare_flash(&device); + ret = blisp_common_prepare_flash(&device, true); if (ret != BLISP_OK) { // TODO: Error handling goto exit1; diff --git a/tools/blisp/src/cmd/run.c b/tools/blisp/src/cmd/run.c new file mode 100644 index 0000000..c317ef4 --- /dev/null +++ b/tools/blisp/src/cmd/run.c @@ -0,0 +1,119 @@ +#include +#include +#include "../cmd.h" +#include "../common.h" + +#define REG_EXTENDED 1 +#define REG_ICASE (REG_EXTENDED << 1) + +static struct arg_rex* cmd; +static struct arg_str *port_name, *chip_type; // TODO: Make this common +static struct arg_lit* reset; +static struct arg_end* end; +static struct arg_file* binary_to_run; +static void* cmd_run_argtable[6]; + + +blisp_return_t cmd_run_args_init() { + cmd_run_argtable[0] = cmd = + arg_rex1(NULL, NULL, "run", NULL, REG_ICASE, NULL); + cmd_run_argtable[1] = chip_type = + arg_str1("c", "chip", "", "Chip Type"); + cmd_run_argtable[2] = port_name = + arg_str0("p", "port", "", + "Name/Path to the Serial Port (empty for search)"); + cmd_run_argtable[3] = reset = + arg_lit0(NULL, "reset", "Reset chip after write"); + cmd_run_argtable[4] = binary_to_run = + arg_file1(NULL, NULL, "", "Binary to run"); + cmd_run_argtable[5] = end = arg_end(10); + + if (arg_nullcheck(cmd_run_argtable) != 0) { + fprintf(stderr, "insufficient memory\n"); + return BLISP_ERR_OUT_OF_MEMORY; + } + return BLISP_OK; +} + +void cmd_run_args_print_glossary() { + fputs("Usage: blisp", stdout); + arg_print_syntax(stdout, cmd_run_argtable, "\n"); + puts("Flashes firmware to RAM and then executes it."); + arg_print_glossary(stdout, cmd_run_argtable, " %-25s %s\n"); +} + +blisp_return_t blisp_run_firmware() { + struct blisp_device device; + blisp_return_t ret; + + ret = blisp_common_init_device(&device, port_name, chip_type); + if (ret != BLISP_OK) { + return ret; + } + + FILE* data_file = fopen(binary_to_run->filename[0], "rb"); + if (data_file == NULL) { + fprintf(stderr, "Failed to open data file \"%s\".\n", + binary_to_run->filename[0]); + ret = BLISP_ERR_CANT_OPEN_FILE; + goto exit1; + } + + if (blisp_common_prepare_flash(&device, false) != 0) { + // TODO: Error handling + goto exit1; + } + + struct blisp_easy_transport firmware_transport = + blisp_easy_transport_new_from_file(data_file); + + ret = blisp_easy_load_ram_app(&device, &firmware_transport, + blisp_common_progress_callback); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to load firmware, ret: %d\n", ret); + goto exit1; + } + + ret = blisp_device_check_image(&device); + if (ret != 0) { + fprintf(stderr, "Failed to check image.\n"); + goto exit1; + } + + ret = blisp_device_run_image(&device); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to run image.\n"); + goto exit1; + } + +exit2: + if (data_file != NULL) + fclose(data_file); +exit1: + blisp_device_close(&device); + return ret; +} + +blisp_return_t cmd_run_parse_exec(int argc, char** argv) { + int errors = arg_parse(argc, argv, cmd_run_argtable); + if (errors == 0) { + blisp_run_firmware(); + return BLISP_OK; + } else if (cmd->count == 1) { + cmd_run_args_print_glossary(); + return BLISP_OK; + } + return BLISP_ERR_INVALID_COMMAND; +} + +void cmd_run_args_print_syntax() { + arg_print_syntax(stdout, cmd_run_argtable, "\n"); +} + +void cmd_run_free() { + arg_freetable(cmd_run_argtable, + sizeof(cmd_run_argtable) / sizeof(cmd_run_argtable[0])); +} + +struct cmd cmd_run = {"run", cmd_run_args_init, cmd_run_parse_exec, + cmd_run_args_print_syntax, cmd_run_free}; diff --git a/tools/blisp/src/cmd/write.c b/tools/blisp/src/cmd/write.c index ef9d446..6c5801f 100644 --- a/tools/blisp/src/cmd/write.c +++ b/tools/blisp/src/cmd/write.c @@ -174,7 +174,7 @@ blisp_return_t blisp_flash_firmware() { return ret; } - if (blisp_common_prepare_flash(&device) != 0) { + if (blisp_common_prepare_flash(&device, true) != 0) { // TODO: Error handling goto exit1; } diff --git a/tools/blisp/src/common.c b/tools/blisp/src/common.c index b486daa..b7d50ec 100644 --- a/tools/blisp/src/common.c +++ b/tools/blisp/src/common.c @@ -58,7 +58,8 @@ blisp_return_t blisp_common_init_device(struct blisp_device* device, * Prepares chip to access flash * this means performing handshake, and loading eflash_loader if needed. */ -blisp_return_t blisp_common_prepare_flash(struct blisp_device* device) { +blisp_return_t blisp_common_prepare_flash(struct blisp_device* device, + bool goto_eflash_loader) { blisp_return_t ret = 0; printf("Sending a handshake...\n"); @@ -84,7 +85,7 @@ blisp_return_t blisp_common_prepare_flash(struct blisp_device* device) { boot_info.chip_id[3], boot_info.chip_id[4], boot_info.chip_id[5], boot_info.chip_id[6], boot_info.chip_id[7]); - if (device->chip->load_eflash_loader == NULL) { + if (device->chip->load_eflash_loader == NULL || !goto_eflash_loader) { return BLISP_OK; } diff --git a/tools/blisp/src/common.h b/tools/blisp/src/common.h index e6519e7..1c63375 100644 --- a/tools/blisp/src/common.h +++ b/tools/blisp/src/common.h @@ -6,7 +6,8 @@ #include #include -int32_t blisp_common_prepare_flash(struct blisp_device* device); +blisp_return_t blisp_common_prepare_flash(struct blisp_device* device, + bool goto_eflash_loader); void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value); int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type); diff --git a/tools/blisp/src/main.c b/tools/blisp/src/main.c index 5861994..7520858 100644 --- a/tools/blisp/src/main.c +++ b/tools/blisp/src/main.c @@ -5,7 +5,7 @@ #include "argtable3.h" #include "cmd.h" -struct cmd* cmds[] = {&cmd_write, &cmd_iot}; +struct cmd* cmds[] = {&cmd_write, &cmd_run, &cmd_iot}; static uint8_t cmds_count = sizeof(cmds) / sizeof(cmds[0]);