From 59cf5fb038ee37964a8b7d6b49a480462de02199 Mon Sep 17 00:00:00 2001 From: Marek Kraus Date: Sat, 7 Jan 2023 23:25:42 +0100 Subject: [PATCH] Add blisp_easy, many improvements, adds iot single download feature --- CMakeLists.txt | 2 +- include/blisp_easy.h | 47 ++++++++++++ include/blisp_util.h | 1 + lib/blisp.c | 2 +- lib/blisp_easy.c | 137 +++++++++++++++++++++++++++++++++ tools/blisp/CMakeLists.txt | 2 +- tools/blisp/src/cmd.h | 1 + tools/blisp/src/cmd/iot.c | 140 ++++++++++++++++++++++++++++++++++ tools/blisp/src/cmd/write.c | 84 ++++++--------------- tools/blisp/src/common.c | 147 ++++++++++++++++++------------------ tools/blisp/src/common.h | 5 +- tools/blisp/src/main.c | 2 +- 12 files changed, 430 insertions(+), 140 deletions(-) create mode 100644 include/blisp_easy.h create mode 100644 lib/blisp_easy.c create mode 100644 tools/blisp/src/cmd/iot.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 2288949..d4586c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ option(BLISP_BUILD_CLI "Build CLI Tool" OFF) add_library(libblisp_obj OBJECT lib/blisp.c lib/chip/blisp_chip_bl60x.c - lib/chip/blisp_chip_bl70x.c) + lib/chip/blisp_chip_bl70x.c lib/blisp_easy.c) target_include_directories(libblisp_obj PRIVATE ${CMAKE_SOURCE_DIR}/include/) diff --git a/include/blisp_easy.h b/include/blisp_easy.h new file mode 100644 index 0000000..62c1b2b --- /dev/null +++ b/include/blisp_easy.h @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: MIT +#ifndef BLISP_BLISP_EASY_H +#define BLISP_BLISP_EASY_H + +#include +#include "blisp.h" + +struct blisp_easy_transport { + uint8_t type; // 0 - memory, 1 - FILE file_handle + union { + FILE* file_handle; + struct { + void* data_location; + uint32_t data_size; + uint32_t current_position; + } memory; + } data; +}; + +enum blisp_easy_error { + BLISP_EASY_ERR_TRANSPORT_ERROR = -100, + BLISP_EASY_ERR_CHECK_IMAGE_FAILED = -101 +}; + +typedef void (*blisp_easy_progress_callback)(uint32_t current_value, + uint32_t max_value); + +struct blisp_easy_transport blisp_easy_transport_new_from_file(FILE* file); + +int32_t blisp_easy_load_segment_data( + struct blisp_device* device, + uint32_t segment_size, + struct blisp_easy_transport* segment_transport, + blisp_easy_progress_callback progress_callback); + +int32_t blisp_easy_load_ram_image( + struct blisp_device* device, + struct blisp_easy_transport* image_transport, + blisp_easy_progress_callback progress_callback); + +int32_t blisp_easy_flash_write(struct blisp_device* device, + struct blisp_easy_transport* data_transport, + uint32_t flash_location, + uint32_t data_size, + blisp_easy_progress_callback progress_callback); + +#endif // BLISP_BLISP_EASY_H diff --git a/include/blisp_util.h b/include/blisp_util.h index a17b071..2b4313a 100644 --- a/include/blisp_util.h +++ b/include/blisp_util.h @@ -17,6 +17,7 @@ static void blisp_dlog(const char* format, ...) va_start(args, format); vfprintf(stderr, format, args); va_end(args); + fputc('\n', stderr); } diff --git a/lib/blisp.c b/lib/blisp.c index d2bcf8e..0375bef 100644 --- a/lib/blisp.c +++ b/lib/blisp.c @@ -211,7 +211,7 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) { } } } - blisp_dlog("Received no response from chip"); + blisp_dlog("Received no response from chip."); return BLISP_ERR_NO_RESPONSE; } diff --git a/lib/blisp_easy.c b/lib/blisp_easy.c new file mode 100644 index 0000000..2194ba7 --- /dev/null +++ b/lib/blisp_easy.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: MIT +#include "blisp_easy.h" +#include "blisp_struct.h" + +#include + +static int64_t blisp_easy_transport_read(struct blisp_easy_transport* transport, + void* buffer, + uint32_t size) { + if (transport->type == 0) { + } else { + return fread(buffer, size, 1, transport->data.file_handle); + } +} + +static void blisp_easy_report_progress(blisp_easy_progress_callback callback, + uint32_t current_value, + uint32_t max_value) { + if (callback != NULL) { + callback(current_value, max_value); + } +} + +struct blisp_easy_transport blisp_easy_transport_new_from_file(FILE* file) { + struct blisp_easy_transport transport = {.type = 1, .data.file_handle = file}; + return transport; +} + +int32_t blisp_easy_load_segment_data( + struct blisp_device* device, + uint32_t segment_size, + struct blisp_easy_transport* segment_transport, + blisp_easy_progress_callback progress_callback) { + int32_t ret; + + uint32_t sent_data = 0; + uint32_t buffer_size = 0; + uint8_t buffer[4092]; + + blisp_easy_report_progress(progress_callback, 0, segment_size); + + while (sent_data < segment_size) { + buffer_size = segment_size - sent_data; + if (buffer_size > 4092) { + buffer_size = 4092; + } + blisp_easy_transport_read(segment_transport, buffer, buffer_size); // TODO: Error Handling + ret = blisp_device_load_segment_data(device, buffer, + buffer_size); + if (ret < BLISP_OK) { + // TODO: Error logging fprintf(stderr, "Failed to load segment data. (ret + // %d)\n", ret); + return BLISP_EASY_ERR_TRANSPORT_ERROR; + } + sent_data += buffer_size; + blisp_easy_report_progress(progress_callback, sent_data, segment_size); + } + return 0; +} + +int32_t blisp_easy_load_ram_image( + struct blisp_device* device, + struct blisp_easy_transport* image_transport, + blisp_easy_progress_callback progress_callback) { + int32_t ret; + + struct bfl_boot_header image_boot_header; + // TODO: Error handling + blisp_easy_transport_read(image_transport, &image_boot_header, 176); + + ret = blisp_device_load_boot_header(device, (uint8_t*)&image_boot_header); + if (ret != BLISP_OK) { + // TODO: Error printing: fprintf(stderr, "Failed to load boot header.\n"); + return BLISP_EASY_ERR_TRANSPORT_ERROR; + } + + { + for (uint8_t seg_index = 0; + seg_index < image_boot_header.segment_info.segment_cnt; seg_index++) { + struct blisp_segment_header segment_header = {0}; + blisp_easy_transport_read(image_transport, &segment_header, + 16); // TODO: Error handling + + ret = blisp_device_load_segment_header(device, &segment_header); + if (ret != 0) { + // TODO: Error printing: fprintf(stderr, "Failed to load segment + // header."); + return BLISP_EASY_ERR_TRANSPORT_ERROR; + } + // TODO: Info printing: printf("Flashing %d. segment\n", seg_index + 1); + + ret = blisp_easy_load_segment_data(device, segment_header.length, + image_transport, progress_callback); + if (ret != 0) { + return BLISP_EASY_ERR_TRANSPORT_ERROR; + } + } + } + + ret = blisp_device_check_image(device); + if (ret != BLISP_OK) { + // TODO: Error printing: fprintf(stderr, "Failed to check image.\n"); + return BLISP_EASY_ERR_CHECK_IMAGE_FAILED; + } + + return BLISP_OK; +} + +int32_t blisp_easy_flash_write(struct blisp_device* device, + struct blisp_easy_transport* data_transport, + uint32_t flash_location, + uint32_t data_size, + blisp_easy_progress_callback progress_callback) { + int32_t ret; + uint32_t sent_data = 0; + uint32_t buffer_size = 0; + uint8_t buffer[8184]; + blisp_easy_report_progress(progress_callback, 0, data_size); + + while (sent_data < data_size) { + buffer_size = data_size - sent_data; + if (buffer_size > 2052) { + buffer_size = 2052; + } + blisp_easy_transport_read(data_transport, buffer, buffer_size); // TODO: Error Handling + ret = blisp_device_flash_write(device, flash_location + sent_data, buffer, + buffer_size); + if (ret < BLISP_OK) { + // TODO: Error logigng: fprintf(stderr, "Failed to write firmware! (ret: + // %d)\n", ret); + return ret; + } + sent_data += buffer_size; + blisp_easy_report_progress(progress_callback, sent_data, data_size); + } + return BLISP_OK; +} \ No newline at end of file diff --git a/tools/blisp/CMakeLists.txt b/tools/blisp/CMakeLists.txt index 4e1df64..9045873 100644 --- a/tools/blisp/CMakeLists.txt +++ b/tools/blisp/CMakeLists.txt @@ -1,6 +1,6 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/vendor/argtable3 ${CMAKE_CURRENT_BINARY_DIR}/argtable3) -add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/common.h) +add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c) target_include_directories(blisp PRIVATE "${CMAKE_SOURCE_DIR}/include" diff --git a/tools/blisp/src/cmd.h b/tools/blisp/src/cmd.h index b378b98..451b7b6 100644 --- a/tools/blisp/src/cmd.h +++ b/tools/blisp/src/cmd.h @@ -13,5 +13,6 @@ struct cmd { }; extern struct cmd cmd_write; +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 new file mode 100644 index 0000000..4f441e1 --- /dev/null +++ b/tools/blisp/src/cmd/iot.c @@ -0,0 +1,140 @@ +#include +#include "../cmd.h" +#include "../common.h" +#include + +#define REG_EXTENDED 1 +#define REG_ICASE (REG_EXTENDED << 1) + +static struct arg_rex* cmd; +static struct arg_file* single_download; +static struct arg_int* single_download_location; +static struct arg_str *port_name, *chip_type; // TODO: Make this common +static struct arg_lit* reset; +static struct arg_end* end; +static void* cmd_iot_argtable[7]; + +void blisp_single_download() +{ + struct blisp_device device; + int32_t ret; + + if (blisp_common_init_device(&device, port_name, chip_type) != 0) { + return; + } + + if (blisp_common_prepare_flash(&device) != 0) { + // TODO: Error handling + goto exit1; + } + + FILE* data_file = fopen(single_download->filename[0], "rb"); + if (data_file == NULL) { + fprintf(stderr, "Failed to open data file \"%s\".\n", + single_download->filename[0]); + goto exit1; + } + fseek(data_file, 0, SEEK_END); + int64_t data_file_size = ftell(data_file); + rewind(data_file); + + printf("Erasing the area, this might take a while...\n"); + ret = + blisp_device_flash_erase(&device, *single_download_location->ival, + *single_download_location->ival + data_file_size + 1); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to erase.\n"); + goto exit2; + } + + printf("Writing the data...\n"); + struct blisp_easy_transport data_transport = + blisp_easy_transport_new_from_file(data_file); + + ret = blisp_easy_flash_write(&device, &data_transport, *single_download_location->ival, + data_file_size, + blisp_common_progress_callback); + if (ret < BLISP_OK) { + fprintf(stderr, "Failed to write data to flash.\n"); + goto exit2; + } + + printf("Checking program...\n"); + ret = blisp_device_program_check(&device); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to check program.\n"); + goto exit2; + } + printf("Program OK!\n"); + + if (reset->count > 0) { // TODO: could be common + blisp_device_reset(&device); + printf("Resetting the chip.\n"); + } + + printf("Download complete!\n"); + +exit2: + if (data_file != NULL) + fclose(data_file); +exit1: + blisp_device_close(&device); +} + +int8_t cmd_iot_args_init() { + cmd_iot_argtable[0] = cmd = + arg_rex1(NULL, NULL, "iot", NULL, REG_ICASE, NULL); + cmd_iot_argtable[1] = chip_type = + arg_str1("c", "chip", "", "Chip Type"); + cmd_iot_argtable[2] = port_name = + arg_str0("p", "port", "", + "Name/Path to the Serial Port (empty for search)"); + cmd_iot_argtable[3] = reset = + arg_lit0(NULL, "reset", "Reset chip after write"); + cmd_iot_argtable[4] = single_download = + arg_file0("s", "single-down", "", "Single download file"); + cmd_iot_argtable[5] = single_download_location = + arg_int0("l", "single-down-loc", NULL, "Single download offset"); + cmd_iot_argtable[6] = end = arg_end(10); + + if (arg_nullcheck(cmd_iot_argtable) != 0) { + fprintf(stderr, "insufficient memory\n"); + return -1; + } + return 0; +} + +void cmd_iot_args_print_glossary() { + fputs("Usage: blisp", stdout); + arg_print_syntax(stdout, cmd_iot_argtable, "\n"); + puts("Flashes firmware as Bouffalo's DevCube"); + arg_print_glossary(stdout, cmd_iot_argtable, " %-25s %s\n"); +} + +uint8_t cmd_iot_parse_exec(int argc, char** argv) { + int errors = arg_parse(argc, argv, cmd_iot_argtable); + if (errors == 0) { + if (single_download->count == 1 && single_download_location->count == 1) { + blisp_single_download(); + return 1; + } else { + return 0; + } + } else if (cmd->count == 1) { + cmd_iot_args_print_glossary(); + return 1; + } + return 0; +} + +void cmd_iot_args_print_syntax() { + arg_print_syntax(stdout, cmd_iot_argtable, "\n"); +} + +void cmd_iot_free() { + arg_freetable(cmd_iot_argtable, + sizeof(cmd_iot_argtable) / sizeof(cmd_iot_argtable[0])); +} + +struct cmd cmd_iot = {"iot", cmd_iot_args_init, cmd_iot_parse_exec, + cmd_iot_args_print_syntax, cmd_iot_free}; diff --git a/tools/blisp/src/cmd/write.c b/tools/blisp/src/cmd/write.c index 6b21cf0..a35e921 100644 --- a/tools/blisp/src/cmd/write.c +++ b/tools/blisp/src/cmd/write.c @@ -6,8 +6,9 @@ #include "../cmd.h" #include "../common.h" #include "../util.h" -#include "argtable3.h" -#include "blisp_struct.h" +#include +#include +#include #define REG_EXTENDED 1 #define REG_ICASE (REG_EXTENDED << 1) @@ -164,37 +165,14 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) { } void blisp_flash_firmware() { - if (chip_type->count == 0) { - fprintf(stderr, "Chip type is invalid.\n"); - return; - } - - struct blisp_chip* chip = NULL; - - if (strcmp(chip_type->sval[0], "bl70x") == 0) { - chip = &blisp_chip_bl70x; - } else if (strcmp(chip_type->sval[0], "bl60x") == 0) { - chip = &blisp_chip_bl60x; - } else { - fprintf(stderr, "Chip type is invalid.\n"); - return; - } - struct blisp_device device; int32_t ret; - ret = blisp_device_init(&device, chip); - if (ret != BLISP_OK) { - fprintf(stderr, "Failed to init device.\n"); - return; - } - ret = blisp_device_open(&device, - port_name->count == 1 ? port_name->sval[0] : NULL); - if (ret != BLISP_OK) { - fprintf(stderr, "Failed to open device.\n"); + + if (blisp_common_init_device(&device, port_name, chip_type) != 0) { return; } - if (blisp_prepare_flash(&device) != 0) { + if (blisp_common_prepare_flash(&device) != 0) { // TODO: Error handling goto exit1; } @@ -213,61 +191,47 @@ void blisp_flash_firmware() { fill_up_boot_header(&boot_header); const uint32_t firmware_base_address = 0x2000; - printf("Erasing flash, this might take a while..."); + printf("Erasing flash, this might take a while...\n"); ret = blisp_device_flash_erase(&device, firmware_base_address, firmware_base_address + firmware_file_size + 1); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to erase flash.\n"); + fprintf(stderr, "Failed to erase flash.\n"); goto exit2; } ret = blisp_device_flash_erase(&device, 0x0000, sizeof(struct bfl_boot_header)); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to erase flash.\n"); + fprintf(stderr, "Failed to erase flash.\n"); goto exit2; } - printf(" OK!\nFlashing boot header..."); + printf("Flashing boot header...\n"); ret = blisp_device_flash_write(&device, 0x0000, (uint8_t*)&boot_header, sizeof(struct bfl_boot_header)); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to write boot header.\n"); + fprintf(stderr, "Failed to write boot header.\n"); goto exit2; } - printf(" OK!\nFlashing the firmware...\n"); - { - uint32_t sent_data = 0; - uint32_t buffer_size = 0; - uint8_t buffer[8184]; - printf("0b / %ldb (0.00%%)\n", firmware_file_size); + printf("Flashing the firmware...\n"); + struct blisp_easy_transport data_transport = + blisp_easy_transport_new_from_file(firmware_file); - while (sent_data < firmware_file_size) { - buffer_size = firmware_file_size - sent_data; - if (buffer_size > 2052) { - buffer_size = 2052; - } - fread(buffer, buffer_size, 1, firmware_file); - ret = blisp_device_flash_write(&device, firmware_base_address + sent_data, - buffer, - buffer_size); // TODO: Error handling - if (ret < BLISP_OK) { - fprintf(stderr, "Failed to write firmware! (ret: %d)\n", ret); - goto exit2; - } - sent_data += buffer_size; - printf("%" PRIu32 "b / %ldb (%.2f%%)\n", sent_data, firmware_file_size, - (((float)sent_data / (float)firmware_file_size) * 100.0f)); - } + ret = blisp_easy_flash_write(&device, &data_transport, firmware_base_address, + firmware_file_size, + blisp_common_progress_callback); + if (ret < BLISP_OK) { + fprintf(stderr, "Failed to write app to flash.\n"); + goto exit2; } - printf("Checking program..."); + printf("Checking program...\n"); ret = blisp_device_program_check(&device); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to check program.\n"); + fprintf(stderr, "Failed to check program.\n"); goto exit2; } - printf("OK\n"); + printf("Program OK!\n"); if (reset->count > 0) { blisp_device_reset(&device); @@ -288,7 +252,7 @@ int8_t cmd_write_args_init() { cmd_write_argtable[0] = cmd = arg_rex1(NULL, NULL, "write", NULL, REG_ICASE, NULL); cmd_write_argtable[1] = chip_type = - arg_str1("c", "chip", "", "Chip Type (bl70x)"); + arg_str1("c", "chip", "", "Chip Type"); cmd_write_argtable[2] = port_name = arg_str0("p", "port", "", "Name/Path to the Serial Port (empty for search)"); diff --git a/tools/blisp/src/common.c b/tools/blisp/src/common.c index 4cf6d4d..082cd92 100644 --- a/tools/blisp/src/common.c +++ b/tools/blisp/src/common.c @@ -1,43 +1,77 @@ // SPDX-License-Identifier: MIT #include "common.h" #include +#include +#include +#include #include #include -#include +#include "blisp_easy.h" #include "util.h" +void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value) { + printf("%" PRIu32 "b / %ldb (%.2f%%)\n", current_value, max_value, + (((float)current_value / (float)max_value) * 100.0f)); +} + +int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type) +{ + if (chip_type->count == 0) { + fprintf(stderr, "Chip type is invalid.\n"); + return -1; + } + + struct blisp_chip* chip = NULL; + + if (strcmp(chip_type->sval[0], "bl70x") == 0) { + chip = &blisp_chip_bl70x; + } else if (strcmp(chip_type->sval[0], "bl60x") == 0) { + chip = &blisp_chip_bl60x; + } else { + fprintf(stderr, "Chip type is invalid.\n"); + return -1; + } + + int32_t ret; + ret = blisp_device_init(device, chip); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to init device.\n"); + return -1; + } + ret = blisp_device_open(device, + port_name->count == 1 ? port_name->sval[0] : NULL); + if (ret != BLISP_OK) { + fprintf(stderr, ret == BLISP_ERR_DEVICE_NOT_FOUND ? "Device not found\n" : "Failed to open device.\n"); + return -1; + } + + return 0; +} + /** * Prepares chip to access flash * this means performing handshake, and loading eflash_loader if needed. */ -int32_t blisp_prepare_flash(struct blisp_device* device) { +int32_t blisp_common_prepare_flash(struct blisp_device* device) { int32_t ret = 0; FILE* eflash_loader_file = NULL; - printf("Sending a handshake..."); + printf("Sending a handshake...\n"); ret = blisp_device_handshake(device, false); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to handshake with device.\n"); + fprintf(stderr, "Failed to handshake with device.\n"); return -1; } - printf(" OK\nGetting chip info..."); + printf("Handshake successful!\nGetting chip info...\n"); struct blisp_boot_info boot_info; ret = blisp_device_get_boot_info(device, &boot_info); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to get boot info.\n"); + fprintf(stderr, "Failed to get boot info.\n"); return -1; } - if (boot_info.boot_rom_version[0] == 255 && - boot_info.boot_rom_version[1] == 255 && - boot_info.boot_rom_version[2] == 255 && - boot_info.boot_rom_version[3] == 255) { - printf(" OK\nDevice already in eflash_loader.\n"); - return 0; - } - printf( - " BootROM version %d.%d.%d.%d, ChipID: " + "BootROM version %d.%d.%d.%d, ChipID: " "%02X%02X%02X%02X%02X%02X%02X%02X\n", boot_info.boot_rom_version[0], boot_info.boot_rom_version[1], boot_info.boot_rom_version[2], boot_info.boot_rom_version[3], @@ -45,6 +79,18 @@ int32_t blisp_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->needs_eflash_loader) { + return 0; + } + + if (boot_info.boot_rom_version[0] == 255 && + boot_info.boot_rom_version[1] == 255 && + boot_info.boot_rom_version[2] == 255 && + boot_info.boot_rom_version[3] == 255) { + printf("Device already in eflash_loader.\n"); + return 0; + } + char exe_path[PATH_MAX]; char eflash_loader_path[PATH_MAX]; if (util_get_binary_folder(exe_path, PATH_MAX) <= 0) { @@ -54,9 +100,10 @@ int32_t blisp_prepare_flash(struct blisp_device* device) { return -1; } snprintf(eflash_loader_path, PATH_MAX, "%s/data/%s/eflash_loader_%s.bin", - exe_path, device->chip->type_str, device->chip->default_xtal); + exe_path, device->chip->type_str, + device->chip->default_xtal); // TODO: Let user pick printf("Loading the eflash loader file from disk\n"); - eflash_loader_file = fopen(eflash_loader_path, "rb"); // TODO: Error handling + eflash_loader_file = fopen(eflash_loader_path, "rb"); if (eflash_loader_file == NULL) { fprintf(stderr, "Could not open the eflash loader file from disk.\n" @@ -65,63 +112,13 @@ int32_t blisp_prepare_flash(struct blisp_device* device) { ret = -1; goto exit1; } - uint8_t - eflash_loader_header[176]; // TODO: Remap it to the boot header struct - fread(eflash_loader_header, 176, 1, - eflash_loader_file); // TODO: Error handling - printf("Loading eflash_loader...\n"); - ret = blisp_device_load_boot_header(device, eflash_loader_header); + struct blisp_easy_transport eflash_loader_transport = + blisp_easy_transport_new_from_file(eflash_loader_file); + ret = blisp_easy_load_ram_image(device, &eflash_loader_transport, + blisp_common_progress_callback); if (ret != BLISP_OK) { - fprintf(stderr, "Failed to load boot header.\n"); - ret = -1; - goto exit1; - } - - { - uint32_t sent_data = 0; - uint32_t buffer_size = 0; - uint8_t buffer[4092]; - - // TODO: Real checking of segments count - for (uint8_t seg_index = 0; seg_index < 1; seg_index++) { - struct blisp_segment_header segment_header = {0}; - fread(&segment_header, 16, 1, - eflash_loader_file); // TODO: Error handling - - ret = blisp_device_load_segment_header(device, &segment_header); - if (ret != 0) { - fprintf(stderr, "Failed to load segment header.\n"); - ret = -1; - goto exit1; - } - printf("Flashing %d. segment\n", seg_index + 1); - printf("0b / %" PRIu32 "b (0.00%%)\n", segment_header.length); - - while (sent_data < segment_header.length) { - buffer_size = segment_header.length - sent_data; - if (buffer_size > 4092) { - buffer_size = 4092; - } - fread(buffer, buffer_size, 1, eflash_loader_file); - ret = blisp_device_load_segment_data( - device, buffer, buffer_size); // TODO: Error handling - if (ret < BLISP_OK) { - fprintf(stderr, "Failed to load segment data. (ret %d)\n", ret); - ret = -1; - goto exit1; - } - sent_data += buffer_size; - printf("%" PRIu32 "b / %" PRIu32 "b (%.2f%%)\n", sent_data, - segment_header.length, - (((float)sent_data / (float)segment_header.length) * 100.0f)); - } - } - } - - ret = blisp_device_check_image(device); - if (ret != BLISP_OK) { - fprintf(stderr, "Failed to check image.\n"); + fprintf(stderr, "Failed to load eflash_loader, ret: %d\n", ret); ret = -1; goto exit1; } @@ -133,14 +130,14 @@ int32_t blisp_prepare_flash(struct blisp_device* device) { goto exit1; } - printf("Sending a handshake..."); + printf("Sending a handshake...\n"); ret = blisp_device_handshake(device, true); if (ret != BLISP_OK) { - fprintf(stderr, "\nFailed to handshake with device.\n"); + fprintf(stderr, "Failed to handshake with device.\n"); ret = -1; goto exit1; } - printf(" OK\n"); + printf("Handshake with eflash_loader successful.\n"); exit1: if (eflash_loader_file != NULL) fclose(eflash_loader_file); diff --git a/tools/blisp/src/common.h b/tools/blisp/src/common.h index b0a8271..e6519e7 100644 --- a/tools/blisp/src/common.h +++ b/tools/blisp/src/common.h @@ -4,7 +4,10 @@ #include #include +#include -int32_t blisp_prepare_flash(struct blisp_device* device); +int32_t blisp_common_prepare_flash(struct blisp_device* device); +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); #endif // BLISP_COMMON_H diff --git a/tools/blisp/src/main.c b/tools/blisp/src/main.c index 646cebb..1632a20 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}; +struct cmd* cmds[] = {&cmd_write, &cmd_iot}; static uint8_t cmds_count = sizeof(cmds) / sizeof(cmds[0]);