mirror of
https://github.com/pine64/blisp.git
synced 2024-12-22 06:20:12 +00:00
Start migration to error codes enum
This commit is contained in:
parent
179a4ea267
commit
fced48570a
110
lib/blisp.c
110
lib/blisp.c
@ -15,19 +15,20 @@
|
||||
|
||||
static void drain(struct sp_port* port) {
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__)
|
||||
sp_drain(port);
|
||||
sp_drain(port);
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t blisp_device_init(struct blisp_device* device,
|
||||
struct blisp_chip* chip) {
|
||||
blisp_return_t blisp_device_init(struct blisp_device* device,
|
||||
struct blisp_chip* chip) {
|
||||
device->chip = chip;
|
||||
device->is_usb = false;
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_open(struct blisp_device* device,
|
||||
const char* port_name) {
|
||||
blisp_return_t ret;
|
||||
struct sp_port* serial_port = NULL;
|
||||
|
||||
if (port_name != NULL) {
|
||||
@ -69,8 +70,7 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
ret = sp_open(serial_port, SP_MODE_READ_WRITE);
|
||||
if (ret != SP_OK) {
|
||||
blisp_dlog("SP open failed: %d", ret);
|
||||
return BLISP_ERR_UNKNOWN; // TODO: Maybe this should be that it can't open
|
||||
// device?
|
||||
return BLISP_ERR_CANT_OPEN_DEVICE;
|
||||
}
|
||||
// TODO: Handle errors in following functions, although, none of them *should*
|
||||
// fail
|
||||
@ -107,11 +107,11 @@ int32_t blisp_device_open(struct blisp_device* device, const char* port_name) {
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_send_command(struct blisp_device* device,
|
||||
uint8_t command,
|
||||
void* payload,
|
||||
uint16_t payload_size,
|
||||
bool add_checksum) {
|
||||
blisp_return_t blisp_send_command(struct blisp_device* device,
|
||||
uint8_t command,
|
||||
void* payload,
|
||||
uint16_t payload_size,
|
||||
bool add_checksum) {
|
||||
int ret;
|
||||
struct sp_port* serial_port = device->serial_port;
|
||||
|
||||
@ -141,8 +141,8 @@ int32_t blisp_send_command(struct blisp_device* device,
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_receive_response(struct blisp_device* device,
|
||||
bool expect_payload) {
|
||||
blisp_return_t blisp_receive_response(struct blisp_device* device,
|
||||
bool expect_payload) {
|
||||
// TODO: Check checksum
|
||||
int ret;
|
||||
struct sp_port* serial_port = device->serial_port;
|
||||
@ -174,7 +174,8 @@ int32_t blisp_receive_response(struct blisp_device* device,
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
blisp_return_t blisp_device_handshake(struct blisp_device* device,
|
||||
bool in_ef_loader) {
|
||||
int ret;
|
||||
uint8_t handshake_buffer[600];
|
||||
struct sp_port* serial_port = device->serial_port;
|
||||
@ -203,8 +204,8 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
}
|
||||
}
|
||||
ret = sp_blocking_write(serial_port, handshake_buffer, bytes_count, 500);
|
||||
// not sure about Apple part, but FreeBSD needs it
|
||||
drain(serial_port);
|
||||
// not sure about Apple part, but FreeBSD needs it
|
||||
drain(serial_port);
|
||||
if (ret < 0) {
|
||||
blisp_dlog("Handshake write failed, ret %d", ret);
|
||||
return BLISP_ERR_UNKNOWN;
|
||||
@ -221,15 +222,14 @@ int32_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) {
|
||||
return BLISP_OK;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
blisp_dlog("Received no response from chip.");
|
||||
return BLISP_ERR_NO_RESPONSE;
|
||||
}
|
||||
|
||||
int32_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
struct blisp_boot_info* boot_info) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
struct blisp_boot_info* boot_info) {
|
||||
blisp_return_t ret;
|
||||
|
||||
ret = blisp_send_command(device, 0x10, NULL, 0, false);
|
||||
if (ret < 0)
|
||||
@ -240,7 +240,8 @@ int32_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
return ret;
|
||||
|
||||
memcpy(boot_info->boot_rom_version, &device->rx_buffer[0],
|
||||
4); // TODO: Endianess
|
||||
4); // TODO: Endianess; this may break on big endian machines
|
||||
|
||||
if (device->chip->type == BLISP_CHIP_BL70X) {
|
||||
memcpy(boot_info->chip_id, &device->rx_buffer[16], 8);
|
||||
}
|
||||
@ -249,9 +250,9 @@ int32_t blisp_device_get_boot_info(struct blisp_device* device,
|
||||
}
|
||||
|
||||
// TODO: Use struct instead of uint8_t*
|
||||
int32_t blisp_device_load_boot_header(struct blisp_device* device,
|
||||
uint8_t* boot_header) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_load_boot_header(struct blisp_device* device,
|
||||
uint8_t* boot_header) {
|
||||
blisp_return_t ret;
|
||||
ret = blisp_send_command(device, 0x11, boot_header, 176, false);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -262,10 +263,10 @@ int32_t blisp_device_load_boot_header(struct blisp_device* device,
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_load_segment_header(
|
||||
blisp_return_t blisp_device_load_segment_header(
|
||||
struct blisp_device* device,
|
||||
struct blisp_segment_header* segment_header) {
|
||||
int ret;
|
||||
blisp_return_t ret;
|
||||
ret = blisp_send_command(device, 0x17, segment_header, 16, false);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -276,10 +277,10 @@ int32_t blisp_device_load_segment_header(
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_load_segment_data(struct blisp_device* device,
|
||||
uint8_t* segment_data,
|
||||
uint32_t segment_data_length) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_load_segment_data(struct blisp_device* device,
|
||||
uint8_t* segment_data,
|
||||
uint32_t segment_data_length) {
|
||||
blisp_return_t ret;
|
||||
ret = blisp_send_command(device, 0x18, segment_data, segment_data_length,
|
||||
false);
|
||||
if (ret < 0)
|
||||
@ -291,8 +292,8 @@ int32_t blisp_device_load_segment_data(struct blisp_device* device,
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_check_image(struct blisp_device* device) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_check_image(struct blisp_device* device) {
|
||||
blisp_return_t ret;
|
||||
ret = blisp_send_command(device, 0x19, NULL, 0, false);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -303,11 +304,11 @@ int32_t blisp_device_check_image(struct blisp_device* device) {
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_write_memory(struct blisp_device* device,
|
||||
uint32_t address,
|
||||
uint32_t value,
|
||||
bool wait_for_res) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_write_memory(struct blisp_device* device,
|
||||
uint32_t address,
|
||||
uint32_t value,
|
||||
bool wait_for_res) {
|
||||
blisp_return_t ret;
|
||||
uint8_t payload[8];
|
||||
*(uint32_t*)(payload) = address;
|
||||
*(uint32_t*)(payload + 4) = value; // TODO: Endianness
|
||||
@ -323,8 +324,8 @@ int32_t blisp_device_write_memory(struct blisp_device* device,
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_run_image(struct blisp_device* device) {
|
||||
int ret;
|
||||
blisp_return_t blisp_device_run_image(struct blisp_device* device) {
|
||||
blisp_return_t ret;
|
||||
|
||||
if (device->chip->type == BLISP_CHIP_BL70X) { // ERRATA
|
||||
ret = blisp_device_write_memory(device, 0x4000F100, 0x4E424845, true);
|
||||
@ -351,14 +352,14 @@ int32_t blisp_device_run_image(struct blisp_device* device) {
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_flash_erase(struct blisp_device* device,
|
||||
uint32_t start_address,
|
||||
uint32_t end_address) {
|
||||
blisp_return_t blisp_device_flash_erase(struct blisp_device* device,
|
||||
uint32_t start_address,
|
||||
uint32_t end_address) {
|
||||
uint8_t payload[8];
|
||||
*(uint32_t*)(payload + 0) = start_address;
|
||||
*(uint32_t*)(payload + 4) = end_address;
|
||||
|
||||
int ret = blisp_send_command(device, 0x30, payload, 8, true);
|
||||
blisp_return_t ret = blisp_send_command(device, 0x30, payload, 8, true);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
do {
|
||||
@ -368,17 +369,18 @@ int32_t blisp_device_flash_erase(struct blisp_device* device,
|
||||
return 0;
|
||||
}
|
||||
|
||||
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_flash_write(struct blisp_device* device,
|
||||
uint32_t start_address,
|
||||
uint8_t* payload,
|
||||
uint32_t payload_size) {
|
||||
// TODO: Add max payload size (8184?)
|
||||
// TODO: Don't use malloc + add check
|
||||
|
||||
uint8_t* buffer = malloc(4 + payload_size);
|
||||
*((uint32_t*)(buffer)) = start_address;
|
||||
memcpy(buffer + 4, payload, payload_size);
|
||||
int ret = blisp_send_command(device, 0x31, buffer, payload_size + 4, true);
|
||||
blisp_return_t ret =
|
||||
blisp_send_command(device, 0x31, buffer, payload_size + 4, true);
|
||||
if (ret < 0)
|
||||
goto exit1;
|
||||
ret = blisp_receive_response(device, false);
|
||||
@ -387,7 +389,7 @@ exit1:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t blisp_device_program_check(struct blisp_device* device) {
|
||||
blisp_return_t blisp_device_program_check(struct blisp_device* device) {
|
||||
int ret = blisp_send_command(device, 0x3A, NULL, 0, true);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -395,11 +397,11 @@ int32_t blisp_device_program_check(struct blisp_device* device) {
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
int32_t blisp_device_reset(struct blisp_device* device) {
|
||||
int ret = blisp_send_command(device, 0x21, NULL, 0, true);
|
||||
blisp_return_t blisp_device_reset(struct blisp_device* device) {
|
||||
blisp_return_t ret = blisp_send_command(device, 0x21, NULL, 0, true);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
ret = blisp_receive_response(device, false);
|
||||
|
@ -1,14 +1,14 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include <argtable3.h>
|
||||
#include <blisp.h>
|
||||
#include <blisp_easy.h>
|
||||
#include <blisp_struct.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../cmd.h"
|
||||
#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)
|
||||
@ -164,12 +164,13 @@ void fill_up_boot_header(struct bfl_boot_header* boot_header) {
|
||||
boot_header->crc32 = 0xDEADBEEF;
|
||||
}
|
||||
|
||||
void blisp_flash_firmware() {
|
||||
blisp_return_t blisp_flash_firmware() {
|
||||
struct blisp_device device;
|
||||
int32_t ret;
|
||||
blisp_return_t ret;
|
||||
ret = blisp_common_init_device(&device, port_name, chip_type);
|
||||
|
||||
if (blisp_common_init_device(&device, port_name, chip_type) != 0) {
|
||||
return;
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (blisp_common_prepare_flash(&device) != 0) {
|
||||
|
@ -3,23 +3,26 @@
|
||||
#include <argtable3.h>
|
||||
#include <blisp.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "blisp_easy.h"
|
||||
#include "error_codes.h"
|
||||
#include "util.h"
|
||||
|
||||
void blisp_common_progress_callback(uint32_t current_value, uint32_t max_value) {
|
||||
void blisp_common_progress_callback(uint32_t current_value,
|
||||
uint32_t max_value) {
|
||||
printf("%" PRIu32 "b / %u (%.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)
|
||||
{
|
||||
blisp_return_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;
|
||||
return BLISP_ERR_INVALID_CHIP_TYPE;
|
||||
}
|
||||
|
||||
struct blisp_chip* chip = NULL;
|
||||
@ -30,23 +33,25 @@ int32_t blisp_common_init_device(struct blisp_device* device, struct arg_str* po
|
||||
chip = &blisp_chip_bl60x;
|
||||
} else {
|
||||
fprintf(stderr, "Chip type is invalid.\n");
|
||||
return -1;
|
||||
return BLISP_ERR_INVALID_CHIP_TYPE;
|
||||
}
|
||||
|
||||
int32_t ret;
|
||||
blisp_return_t ret;
|
||||
ret = blisp_device_init(device, chip);
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to init device.\n");
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
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;
|
||||
fprintf(stderr, ret == BLISP_ERR_DEVICE_NOT_FOUND
|
||||
? "Device not found\n"
|
||||
: "Failed to open device.\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return BLISP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,12 +98,15 @@ int32_t blisp_common_prepare_flash(struct blisp_device* device) {
|
||||
|
||||
uint8_t* eflash_loader_buffer = NULL;
|
||||
// TODO: Error check
|
||||
int64_t eflash_loader_buffer_length = device->chip->load_eflash_loader(0, &eflash_loader_buffer);
|
||||
int64_t eflash_loader_buffer_length =
|
||||
device->chip->load_eflash_loader(0, &eflash_loader_buffer);
|
||||
|
||||
struct blisp_easy_transport eflash_loader_transport =
|
||||
blisp_easy_transport_new_from_memory(eflash_loader_buffer, eflash_loader_buffer_length);
|
||||
blisp_easy_transport_new_from_memory(eflash_loader_buffer,
|
||||
eflash_loader_buffer_length);
|
||||
|
||||
ret = blisp_easy_load_ram_app(device, &eflash_loader_transport, blisp_common_progress_callback);
|
||||
ret = blisp_easy_load_ram_app(device, &eflash_loader_transport,
|
||||
blisp_common_progress_callback);
|
||||
|
||||
if (ret != BLISP_OK) {
|
||||
fprintf(stderr, "Failed to load eflash_loader, ret: %d\n", ret);
|
||||
|
Loading…
Reference in New Issue
Block a user