Compare commits

..

No commits in common. "5580eec07b83cd13e364b9b694f955e61f1da486" and "26cee48869a03d2b1149848fcbe43a92e08a0369" have entirely different histories.

10 changed files with 70 additions and 119 deletions

View File

@ -11,21 +11,10 @@ option(COMPILE_TESTS "Compile the tests" OFF)
add_library(libblisp_obj OBJECT
lib/blisp.c
lib/blisp_easy.c
lib/blisp_util.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/)
if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
target_compile_options(libblisp_obj PRIVATE -Wall -Wextra -Wpedantic)
else()
# MSVC does not support 'extra' and 'pedantic' levels to warnings.
# `/Wall` seems to generate way too many non-actionable output marked as warnings.
# We settle for `/W4`.
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
target_compile_options(libblisp_obj PRIVATE -W4)
endif()
set_property(TARGET libblisp_obj PROPERTY POSITION_INDEPENDENT_CODE 1)

View File

@ -31,34 +31,34 @@ struct blisp_boot_info {
// TODO: Refactor variable names, so all will follow same semantic, like
// image_run, image_check etc.
blisp_return_t blisp_device_init(struct blisp_device* device, struct blisp_chip* chip);
blisp_return_t blisp_device_open(struct blisp_device* device, const char* port_name);
blisp_return_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader);
blisp_return_t blisp_device_get_boot_info(struct blisp_device* device,
int32_t blisp_device_init(struct blisp_device* device, struct blisp_chip* chip);
int32_t blisp_device_open(struct blisp_device* device, const char* port_name);
int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader);
int32_t blisp_device_get_boot_info(struct blisp_device* device,
struct blisp_boot_info* boot_info);
blisp_return_t blisp_device_load_boot_header(struct blisp_device* device,
int32_t blisp_device_load_boot_header(struct blisp_device* device,
uint8_t* boot_header);
blisp_return_t blisp_device_load_segment_header(
int32_t blisp_device_load_segment_header(
struct blisp_device* device,
struct blisp_segment_header* segment_header);
blisp_return_t blisp_device_load_segment_data(struct blisp_device* device,
int32_t blisp_device_load_segment_data(struct blisp_device* device,
uint8_t* segment_data,
uint32_t segment_data_length);
blisp_return_t blisp_device_write_memory(struct blisp_device* device,
int32_t blisp_device_write_memory(struct blisp_device* device,
uint32_t address,
uint32_t value,
bool wait_for_res);
blisp_return_t blisp_device_check_image(struct blisp_device* device);
blisp_return_t blisp_device_run_image(struct blisp_device* device);
blisp_return_t blisp_device_flash_erase(struct blisp_device* device,
int32_t blisp_device_check_image(struct blisp_device* device);
int32_t blisp_device_run_image(struct blisp_device* device);
int32_t blisp_device_flash_erase(struct blisp_device* device,
uint32_t start_address,
uint32_t end_address);
blisp_return_t blisp_device_flash_write(struct blisp_device* device,
int32_t blisp_device_flash_write(struct blisp_device* device,
uint32_t start_address,
uint8_t* payload,
uint32_t payload_size);
blisp_return_t blisp_device_program_check(struct blisp_device* device);
blisp_return_t blisp_device_reset(struct blisp_device* device);
int32_t blisp_device_program_check(struct blisp_device* device);
int32_t blisp_device_reset(struct blisp_device* device);
void blisp_device_close(struct blisp_device* device);
#endif

View File

@ -2,18 +2,35 @@
#ifndef _BLISP_UTIL_H
#define _BLISP_UTIL_H
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef WIN32
# include <windows.h>
#include <windows.h>
#else
# include <time.h>
#include <time.h>
#endif
void blisp_dlog(const char* format, ...);
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);
}
void sleep_ms(int milliseconds);
uint32_t crc32_calculate(const void *data, size_t data_len);
static void sleep_ms(int milliseconds) {
#ifdef WIN32
Sleep(milliseconds);
#else
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#endif
}
/**
* * Generated on Mon Jan 9 19:56:36 2023
@ -63,4 +80,19 @@ static const uint32_t crc_table[256] = {
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
#endif
static uint32_t crc32_calculate(const void *data, size_t data_len)
{
uint32_t crc = 0xffffffff;
const unsigned char *d = (const unsigned char *)data;
unsigned int tbl_idx;
while (data_len--) {
tbl_idx = (crc ^ *d) & 0xff;
crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff;
d++;
}
return (crc & 0xffffffff) ^ 0xffffffff;
}
#endif

View File

@ -16,8 +16,6 @@
static void drain(struct sp_port* port) {
#if defined(__APPLE__) || defined(__FreeBSD__)
sp_drain(port);
#else
(void)port; // unused
#endif
}
@ -30,7 +28,7 @@ blisp_return_t blisp_device_init(struct blisp_device* device,
blisp_return_t blisp_device_open(struct blisp_device* device,
const char* port_name) {
enum sp_return ret;
blisp_return_t ret;
struct sp_port* serial_port = NULL;
if (port_name != NULL) {
@ -192,8 +190,8 @@ blisp_return_t blisp_device_handshake(struct blisp_device* device,
sleep_ms(50); // Wait a bit so BootROM can init
}
uint32_t bytes_count = (uint32_t)(device->chip->handshake_byte_multiplier *
(float)device->current_baud_rate / 10.0f);
uint32_t bytes_count = device->chip->handshake_byte_multiplier *
(float)device->current_baud_rate / 10.0f;
if (bytes_count > 600)
bytes_count = 600;
memset(handshake_buffer, 'U', bytes_count);
@ -416,4 +414,4 @@ blisp_return_t blisp_device_reset(struct blisp_device* device) {
void blisp_device_close(struct blisp_device* device) {
struct sp_port* serial_port = device->serial_port;
sp_close(serial_port);
}
}

View File

@ -1,48 +0,0 @@
// SPDX-License-Identifier: MIT
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#ifdef WIN32
# include <windows.h>
#else
# include <time.h>
#endif
#include "blisp_util.h"
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);
}
void sleep_ms(int milliseconds) {
#ifdef WIN32
Sleep(milliseconds);
#else
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#endif
}
uint32_t crc32_calculate(const void *data, size_t data_len)
{
uint32_t crc = 0xffffffff;
const unsigned char *d = (const unsigned char *)data;
unsigned int tbl_idx;
while (data_len--) {
tbl_idx = (crc ^ *d) & 0xff;
crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff;
d++;
}
return (crc & 0xffffffff) ^ 0xffffffff;
}

View File

@ -6,8 +6,6 @@
int64_t blisp_chip_bl60x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
{
(void)clk_type; // unused
uint8_t* firmware_buf = malloc(sizeof(bl60x_eflash_loader_bin));
memcpy(firmware_buf, bl60x_eflash_loader_bin, sizeof(bl60x_eflash_loader_bin));
*(firmware_buf + 0xE0) = 4; // TODO: 40 MHz clock

View File

@ -6,8 +6,6 @@
int64_t blisp_chip_bl70x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
{
(void) clk_type; // ununsed
uint8_t* firmware_buf = malloc(sizeof(bl70x_eflash_loader_bin));
memcpy(firmware_buf, bl70x_eflash_loader_bin, sizeof(bl70x_eflash_loader_bin));
*(firmware_buf + 0xE0) = 1; // TODO: 32 MHz clock

View File

@ -23,16 +23,6 @@ target_link_libraries(blisp PRIVATE
argtable3::argtable3
libblisp_static file_parsers)
if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
target_compile_options(libblisp_obj PRIVATE -Wall -Wextra -Wpedantic)
else()
# MSVC does not support 'extra' and 'pedantic' levels to warnings.
# `/Wall` seems to generate way too many non-actionable output marked as warnings.
# We settle for `/W4`.
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170
target_compile_options(libblisp_obj PRIVATE -W4)
endif()
if (WIN32)
target_link_libraries(blisp PRIVATE Setupapi.lib)
elseif (APPLE)

View File

@ -168,35 +168,31 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) {
blisp_return_t blisp_flash_firmware() {
struct blisp_device device;
blisp_return_t ret = BLISP_OK;
blisp_return_t ret;
if (access(binary_to_write->filename[0], R_OK) != 0) {
// File not accessible, error out.
fprintf(stderr, "Input firmware not found: %s\n", binary_to_write->filename[0]);
cmd_write_args_print_glossary(); /* Print help to assist user */
/* No need to free memory, will now exit with ret code 1 */
return BLISP_ERR_CANT_OPEN_FILE;
return 1;
}
ret = blisp_common_init_device(&device, port_name, chip_type);
if (ret != BLISP_OK) {
if (ret != 0) {
return ret;
}
ret = blisp_common_prepare_flash(&device);
if (ret != BLISP_OK) {
if (blisp_common_prepare_flash(&device) != 0) {
// TODO: Error handling
goto exit1;
}
parsed_firmware_file_t parsed_file;
memset(&parsed_file, 0, sizeof(parsed_file));
if (parse_firmware_file(binary_to_write->filename[0], &parsed_file) < 0) {
// `parse_firmware_file` doesn't return `blisp_return_t`
// so we default to the generic error.
ret = BLISP_ERR_UNKNOWN;
goto exit1;
}
int parsed_result =
parse_firmware_file(binary_to_write->filename[0], &parsed_file);
// If we are injecting a bootloader section, make it, erase flash, and flash
// it. Then when we do firmware later on; it will be located afterwards
@ -237,13 +233,13 @@ blisp_return_t blisp_flash_firmware() {
if (ret != BLISP_OK) {
fprintf(stderr,
"Failed to erase flash. Tried to erase from 0x%08zx to 0x%08zx\n",
"Failed to erase flash. Tried to erase from 0x%08lu to 0x%08lu\n",
parsed_file.payload_address,
parsed_file.payload_address + parsed_file.payload_length + 1);
goto exit2;
}
printf("Flashing the firmware %zu bytes @ 0x%08zx...\n",
printf("Flashing the firmware %lu bytes @ 0x%08lu...\n",
parsed_file.payload_length, parsed_file.payload_address);
struct blisp_easy_transport data_transport =
blisp_easy_transport_new_from_memory(parsed_file.payload,
@ -253,7 +249,7 @@ blisp_return_t blisp_flash_firmware() {
&device, &data_transport, parsed_file.payload_address,
parsed_file.payload_length, blisp_common_progress_callback);
if (ret != BLISP_OK) {
if (ret < BLISP_OK) {
fprintf(stderr, "Failed to write app to flash.\n");
goto exit2;
}
@ -279,8 +275,6 @@ exit2:
free(parsed_file.payload);
exit1:
blisp_device_close(&device);
return ret;
}
blisp_return_t cmd_write_args_init() {

View File

@ -6,8 +6,8 @@
#include <blisp.h>
#include <argtable3.h>
blisp_return_t blisp_common_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);
blisp_return_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type);
int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* port_name, struct arg_str* chip_type);
#endif // BLISP_COMMON_H