mirror of
https://github.com/pine64/blisp.git
synced 2025-06-22 11:54:44 +00:00
Compare commits
5 Commits
8a00a63600
...
59cf5fb038
Author | SHA1 | Date | |
---|---|---|---|
|
59cf5fb038 | ||
|
ed2c9b80b8 | ||
|
a072ddb3f3 | ||
|
2740d305ba | ||
|
841a52177b |
@ -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/)
|
||||
|
||||
|
10
README.md
10
README.md
@ -12,9 +12,6 @@ Open source tool and library for flashing Bouffalo RISC-V MCUs.
|
||||
- [ ] `bl61x` - BL616 / BL618
|
||||
- [ ] `bl808` - BL808
|
||||
|
||||
# Supported Devices
|
||||
- [X] [Pinecil V2](https://wiki.pine64.org/wiki/Pinecil)
|
||||
|
||||
# Supported OS
|
||||
- [x] Windows
|
||||
- [x] Linux
|
||||
@ -58,10 +55,3 @@ blisp --chip bl60x --reset -p /dev/ttyUSB0 name_of_firmware.bin
|
||||
# How to flash Pinecil V2
|
||||
|
||||
Check out the [wiki page](https://github.com/pine64/blisp/wiki/Update-Pinecil-V2).
|
||||
|
||||
# To Do
|
||||
|
||||
- [ ] Another code style
|
||||
- [ ] Finalize API
|
||||
- [ ] SDIO and JTAG support
|
||||
- [ ] Add Apple support
|
||||
|
@ -5,17 +5,26 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum blisp_chip_type { BLISP_CHIP_BL60X, BLISP_CHIP_BL70X };
|
||||
enum blisp_chip_type {
|
||||
BLISP_CHIP_BL60X,
|
||||
BLISP_CHIP_BL70X,
|
||||
BLISP_CHIP_BL606P,
|
||||
BLISP_CHIP_BL808,
|
||||
BLISP_CHIP_BL61X,
|
||||
};
|
||||
|
||||
struct blisp_chip { // TODO: Move elsewhere?
|
||||
enum blisp_chip_type type;
|
||||
const char* type_str;
|
||||
bool usb_isp_available;
|
||||
float handshake_byte_multiplier;
|
||||
const char* default_eflash_loader_xtal; // TODO: Make this selectable
|
||||
const char* default_xtal; // TODO: Make this selectable
|
||||
bool needs_eflash_loader;
|
||||
};
|
||||
|
||||
extern struct blisp_chip blisp_chip_bl60x;
|
||||
extern struct blisp_chip blisp_chip_bl70x;
|
||||
extern struct blisp_chip blisp_chip_bl808;
|
||||
extern struct blisp_chip blisp_chip_bl61x;
|
||||
|
||||
#endif
|
47
include/blisp_easy.h
Normal file
47
include/blisp_easy.h
Normal file
@ -0,0 +1,47 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#ifndef BLISP_BLISP_EASY_H
|
||||
#define BLISP_BLISP_EASY_H
|
||||
|
||||
#include <stdio.h>
|
||||
#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
|
@ -2,12 +2,25 @@
|
||||
#ifndef _BLISP_UTIL_H
|
||||
#define _BLISP_UTIL_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
static void blisp_dlog(const char* format, ...)
|
||||
{
|
||||
fflush(stdout);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
|
||||
|
||||
static void sleep_ms(int milliseconds) {
|
||||
#ifdef WIN32
|
||||
Sleep(milliseconds);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
137
lib/blisp_easy.c
Normal file
137
lib/blisp_easy.c
Normal file
@ -0,0 +1,137 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "blisp_easy.h"
|
||||
#include "blisp_struct.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
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;
|
||||
}
|
@ -5,6 +5,7 @@ struct blisp_chip blisp_chip_bl60x = {
|
||||
.type = BLISP_CHIP_BL60X,
|
||||
.type_str = "bl60x",
|
||||
.usb_isp_available = false,
|
||||
.default_eflash_loader_xtal = "40m",
|
||||
.default_xtal = "40m",
|
||||
.handshake_byte_multiplier = 0.006f,
|
||||
.needs_eflash_loader = true
|
||||
};
|
||||
|
11
lib/chip/blisp_chip_bl61x.c
Normal file
11
lib/chip/blisp_chip_bl61x.c
Normal file
@ -0,0 +1,11 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "blisp.h"
|
||||
|
||||
struct blisp_chip blisp_chip_bl61x = {
|
||||
.type = BLISP_CHIP_BL61X,
|
||||
.type_str = "bl808",
|
||||
.usb_isp_available = true,
|
||||
.default_xtal = "-", // ?
|
||||
.handshake_byte_multiplier = 0.003f,
|
||||
.needs_eflash_loader = false
|
||||
};
|
@ -5,6 +5,7 @@ struct blisp_chip blisp_chip_bl70x = {
|
||||
.type = BLISP_CHIP_BL70X,
|
||||
.type_str = "bl70x",
|
||||
.usb_isp_available = true,
|
||||
.default_eflash_loader_xtal = "32m",
|
||||
.default_xtal = "32m",
|
||||
.handshake_byte_multiplier = 0.003f,
|
||||
.needs_eflash_loader = true
|
||||
};
|
||||
|
11
lib/chip/blisp_chip_bl808.c
Normal file
11
lib/chip/blisp_chip_bl808.c
Normal file
@ -0,0 +1,11 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "blisp.h"
|
||||
|
||||
struct blisp_chip blisp_chip_bl808 = {
|
||||
.type = BLISP_CHIP_BL808,
|
||||
.type_str = "bl808",
|
||||
.usb_isp_available = true, // TODO: Only for BL808D :-(
|
||||
.default_xtal = "-", // ?
|
||||
.handshake_byte_multiplier = 0.003f,
|
||||
.needs_eflash_loader = false
|
||||
};
|
@ -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)
|
||||
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"
|
||||
|
@ -13,5 +13,6 @@ struct cmd {
|
||||
};
|
||||
|
||||
extern struct cmd cmd_write;
|
||||
extern struct cmd cmd_iot;
|
||||
|
||||
#endif // BLISP_CMD_H
|
||||
|
140
tools/blisp/src/cmd/iot.c
Normal file
140
tools/blisp/src/cmd/iot.c
Normal file
@ -0,0 +1,140 @@
|
||||
#include <blisp_easy.h>
|
||||
#include "../cmd.h"
|
||||
#include "../common.h"
|
||||
#include <argtable3.h>
|
||||
|
||||
#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>", "Chip Type");
|
||||
cmd_iot_argtable[2] = port_name =
|
||||
arg_str0("p", "port", "<port_name>",
|
||||
"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", "<file>", "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};
|
@ -1,24 +1,14 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include <assert.h>
|
||||
#include <blisp.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../cmd.h"
|
||||
#include "argtable3.h"
|
||||
#include "blisp_struct.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include <linux/limits.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
#include <windows.h>
|
||||
#define PATH_MAX MAX_PATH
|
||||
#elif defined(__APPLE__)
|
||||
#include <sys/syslimits.h>
|
||||
#endif
|
||||
#include "../common.h"
|
||||
#include "../util.h"
|
||||
#include <argtable3.h>
|
||||
#include <blisp_easy.h>
|
||||
#include <blisp_struct.h>
|
||||
|
||||
#define REG_EXTENDED 1
|
||||
#define REG_ICASE (REG_EXTENDED << 1)
|
||||
@ -30,45 +20,6 @@ static struct arg_lit* reset;
|
||||
static struct arg_end* end;
|
||||
static void* cmd_write_argtable[6];
|
||||
|
||||
#ifdef __APPLE__
|
||||
// Ugh. This stuff is just so messy without C++17 or Qt...
|
||||
// These are not thread safe, but it doesn't place the responsibility
|
||||
// to free an allocated buffer on the caller.nn
|
||||
|
||||
static void get_executable_path(char* buffer_out, uint32_t max_size) {
|
||||
assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux.
|
||||
|
||||
char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x
|
||||
char real_path_name[PATH_MAX]; // /var/tmp/x
|
||||
uint32_t raw_path_size = sizeof(raw_path_name);
|
||||
|
||||
if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) {
|
||||
realpath(raw_path_name, real_path_name);
|
||||
}
|
||||
// *real_path_name is appropriately sized and null terminated.
|
||||
strcpy(buffer_out, real_path_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t get_binary_folder(char* buffer, uint32_t buffer_size) {
|
||||
#ifdef __linux__
|
||||
if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
char* pos = strrchr(buffer, '/');
|
||||
#elif defined(__APPLE__)
|
||||
get_executable_path(buffer, buffer_size);
|
||||
char* pos = strrchr(buffer, '/');
|
||||
#else
|
||||
if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
char* pos = strrchr(buffer, '\\');
|
||||
#endif
|
||||
pos[0] = '\0';
|
||||
return pos - buffer;
|
||||
}
|
||||
|
||||
void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
||||
memcpy(boot_header->magiccode, "BFNP", 4);
|
||||
|
||||
@ -214,160 +165,18 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
||||
}
|
||||
|
||||
void blisp_flash_firmware() {
|
||||
FILE* eflash_loader_file = NULL;
|
||||
|
||||
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");
|
||||
|
||||
if (blisp_common_init_device(&device, port_name, chip_type) != 0) {
|
||||
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");
|
||||
return;
|
||||
}
|
||||
printf("Sending a handshake...");
|
||||
ret = blisp_device_handshake(&device, false);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to handshake with device.\n");
|
||||
goto exit1;
|
||||
}
|
||||
printf(" OK\nGetting chip info...");
|
||||
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");
|
||||
|
||||
if (blisp_common_prepare_flash(&device) != 0) {
|
||||
// TODO: Error handling
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
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");
|
||||
goto eflash_loader;
|
||||
}
|
||||
|
||||
printf(
|
||||
" 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],
|
||||
boot_info.chip_id[0], boot_info.chip_id[1], boot_info.chip_id[2],
|
||||
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]);
|
||||
|
||||
char exe_path[PATH_MAX];
|
||||
char eflash_loader_path[PATH_MAX];
|
||||
if (get_binary_folder(exe_path, PATH_MAX) <= 0) {
|
||||
fprintf(stderr,
|
||||
"Failed to find executable path to search for the "
|
||||
"eflash loader\n");
|
||||
goto exit1;
|
||||
}
|
||||
snprintf(eflash_loader_path, PATH_MAX, "%s/data/%s/eflash_loader_%s.bin",
|
||||
exe_path, device.chip->type_str,
|
||||
device.chip->default_eflash_loader_xtal);
|
||||
printf("Loading the eflash loader file from disk\n");
|
||||
eflash_loader_file = fopen(eflash_loader_path, "rb"); // TODO: Error handling
|
||||
if (eflash_loader_file == NULL) {
|
||||
fprintf(stderr,
|
||||
"Could not open the eflash loader file from disk.\n"
|
||||
"Does \"%s\" exist?\n",
|
||||
eflash_loader_path);
|
||||
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);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to load boot header.\n");
|
||||
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");
|
||||
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);
|
||||
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");
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
ret = blisp_device_run_image(&device);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to run image.\n");
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
printf("Sending a handshake...");
|
||||
ret = blisp_device_handshake(&device, true);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "\nFailed to handshake with device.\n");
|
||||
goto exit1;
|
||||
}
|
||||
printf(" OK\n");
|
||||
|
||||
eflash_loader:;
|
||||
FILE* firmware_file = fopen(binary_to_write->filename[0], "rb");
|
||||
if (firmware_file == NULL) {
|
||||
fprintf(stderr, "Failed to open firmware file \"%s\".\n",
|
||||
@ -382,61 +191,47 @@ eflash_loader:;
|
||||
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);
|
||||
@ -450,8 +245,6 @@ exit2:
|
||||
if (firmware_file != NULL)
|
||||
fclose(firmware_file);
|
||||
exit1:
|
||||
if (eflash_loader_file != NULL)
|
||||
fclose(eflash_loader_file);
|
||||
blisp_device_close(&device);
|
||||
}
|
||||
|
||||
@ -459,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>", "Chip Type (bl70x)");
|
||||
arg_str1("c", "chip", "<chip_type>", "Chip Type");
|
||||
cmd_write_argtable[2] = port_name =
|
||||
arg_str0("p", "port", "<port_name>",
|
||||
"Name/Path to the Serial Port (empty for search)");
|
||||
|
145
tools/blisp/src/common.c
Normal file
145
tools/blisp/src/common.c
Normal file
@ -0,0 +1,145 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "common.h"
|
||||
#include <blisp.h>
|
||||
#include <argtable3.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#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_common_prepare_flash(struct blisp_device* device) {
|
||||
int32_t ret = 0;
|
||||
FILE* eflash_loader_file = NULL;
|
||||
|
||||
printf("Sending a handshake...\n");
|
||||
ret = blisp_device_handshake(device, false);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to handshake with device.\n");
|
||||
return -1;
|
||||
}
|
||||
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, "Failed to get boot info.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf(
|
||||
"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],
|
||||
boot_info.chip_id[0], boot_info.chip_id[1], boot_info.chip_id[2],
|
||||
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) {
|
||||
fprintf(stderr,
|
||||
"Failed to find executable path to search for the "
|
||||
"eflash loader\n");
|
||||
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); // TODO: Let user pick
|
||||
printf("Loading the eflash loader file from disk\n");
|
||||
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"
|
||||
"Does \"%s\" exist?\n",
|
||||
eflash_loader_path);
|
||||
ret = -1;
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
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 eflash_loader, ret: %d\n", ret);
|
||||
ret = -1;
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
ret = blisp_device_run_image(device);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to run image.\n");
|
||||
ret = -1;
|
||||
goto exit1;
|
||||
}
|
||||
|
||||
printf("Sending a handshake...\n");
|
||||
ret = blisp_device_handshake(device, true);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to handshake with device.\n");
|
||||
ret = -1;
|
||||
goto exit1;
|
||||
}
|
||||
printf("Handshake with eflash_loader successful.\n");
|
||||
exit1:
|
||||
if (eflash_loader_file != NULL)
|
||||
fclose(eflash_loader_file);
|
||||
return ret;
|
||||
}
|
13
tools/blisp/src/common.h
Normal file
13
tools/blisp/src/common.h
Normal file
@ -0,0 +1,13 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#ifndef BLISP_COMMON_H
|
||||
#define BLISP_COMMON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <blisp.h>
|
||||
#include <argtable3.h>
|
||||
|
||||
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
|
@ -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]);
|
||||
|
||||
|
44
tools/blisp/src/util.c
Normal file
44
tools/blisp/src/util.c
Normal file
@ -0,0 +1,44 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
// Ugh. This stuff is just so messy without C++17 or Qt...
|
||||
// These are not thread safe, but it doesn't place the responsibility
|
||||
// to free an allocated buffer on the caller.nn
|
||||
|
||||
static void util_get_executable_path(char* buffer_out, uint32_t max_size) {
|
||||
assert(max_size >= PATH_MAX); // n.b. 1024 on MacOS. 4K on most Linux.
|
||||
|
||||
char raw_path_name[PATH_MAX]; // $HOME/../../var/tmp/x
|
||||
char real_path_name[PATH_MAX]; // /var/tmp/x
|
||||
uint32_t raw_path_size = sizeof(raw_path_name);
|
||||
|
||||
if (!_NSGetExecutablePath(raw_path_name, &raw_path_size)) {
|
||||
realpath(raw_path_name, real_path_name);
|
||||
}
|
||||
// *real_path_name is appropriately sized and null terminated.
|
||||
strcpy(buffer_out, real_path_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size) {
|
||||
#ifdef __linux__
|
||||
if (readlink("/proc/self/exe", buffer, buffer_size) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
char* pos = strrchr(buffer, '/');
|
||||
#elif defined(__APPLE__)
|
||||
util_get_executable_path(buffer, buffer_size);
|
||||
char* pos = strrchr(buffer, '/');
|
||||
#else
|
||||
if (GetModuleFileName(NULL, buffer, buffer_size) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
char* pos = strrchr(buffer, '\\');
|
||||
#endif
|
||||
pos[0] = '\0';
|
||||
return pos - buffer;
|
||||
}
|
27
tools/blisp/src/util.h
Normal file
27
tools/blisp/src/util.h
Normal file
@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#ifndef BLISP_UTIL_H
|
||||
#define BLISP_UTIL_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <unistd.h>
|
||||
#elif defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
#include <windows.h>
|
||||
#define PATH_MAX MAX_PATH
|
||||
#elif defined(__APPLE__)
|
||||
#include <sys/syslimits.h>
|
||||
#include <assert.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#include <linux/limits.h>
|
||||
#endif
|
||||
|
||||
ssize_t util_get_binary_folder(char* buffer, uint32_t buffer_size);
|
||||
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user