Compare commits

...

6 Commits

Author SHA1 Message Date
Marek Kraus
6306a00de7 Include Windows headers for missing data types 2023-08-01 14:38:55 +02:00
Ben V. Brown
c87a4a092c Pragma -> ifndef 2023-08-01 21:36:39 +10:00
Ben V. Brown
dbd7ee2257 Adjust DFU to avoid goto 2023-08-01 21:31:07 +10:00
Ben V. Brown
05b62d2fa5 Drop extra erase 2023-08-01 21:08:58 +10:00
Ben V. Brown
d25bd65f15 Improve logging 2023-08-01 20:59:36 +10:00
Ben V. Brown
ec44c2358b Update dfu_file.c 2023-08-01 18:49:14 +10:00
5 changed files with 49 additions and 16 deletions

View File

@ -354,13 +354,16 @@ int32_t blisp_easy_flash_write(struct blisp_device* device,
if (buffer_size > buffer_max_size) { if (buffer_size > buffer_max_size) {
buffer_size = buffer_max_size; buffer_size = buffer_max_size;
} }
blisp_easy_transport_read(data_transport, buffer, ret = blisp_easy_transport_read(data_transport, buffer, buffer_size);
buffer_size); // TODO: Error Handling if (ret < BLISP_OK) {
fprintf(stderr, "Failed to read firmware chunk! (ret:%d)\n ", ret);
return ret;
}
ret = blisp_device_flash_write(device, flash_location + sent_data, buffer, ret = blisp_device_flash_write(device, flash_location + sent_data, buffer,
buffer_size); buffer_size);
if (ret < BLISP_OK) { if (ret < BLISP_OK) {
// TODO: Error logigng: fprintf(stderr, "Failed to write firmware! (ret: fprintf(stderr, "Failed to write firmware! (ret:%d)\n ", ret);
// %d)\n", ret);
return ret; return ret;
} }
sent_data += buffer_size; sent_data += buffer_size;

View File

@ -195,6 +195,7 @@ blisp_return_t blisp_flash_firmware() {
printf("Erasing flash to flash boot header\n"); printf("Erasing flash to flash boot header\n");
ret = blisp_device_flash_erase(&device, 0x0000, ret = blisp_device_flash_erase(&device, 0x0000,
sizeof(struct bfl_boot_header)); sizeof(struct bfl_boot_header));
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, "Failed to erase flash.\n"); fprintf(stderr, "Failed to erase flash.\n");
goto exit2; goto exit2;
@ -217,7 +218,8 @@ blisp_return_t blisp_flash_firmware() {
printf("Erasing flash for firmware, this might take a while...\n"); printf("Erasing flash for firmware, this might take a while...\n");
ret = blisp_device_flash_erase( ret = blisp_device_flash_erase(
&device, parsed_file.payload_address, &device, parsed_file.payload_address,
parsed_file.payload_address + parsed_file.payload_length + 1); parsed_file.payload_address + parsed_file.payload_length);
if (ret != BLISP_OK) { if (ret != BLISP_OK) {
fprintf(stderr, fprintf(stderr,
"Failed to erase flash. Tried to erase from 0x%08X to 0x%08X\n", "Failed to erase flash. Tried to erase from 0x%08X to 0x%08X\n",
@ -226,7 +228,8 @@ blisp_return_t blisp_flash_firmware() {
goto exit2; goto exit2;
} }
printf("Flashing the firmware @ 0x%08X...\n", parsed_file.payload_address); printf("Flashing the firmware %d bytes @ 0x%08X...\n",
parsed_file.payload_length, parsed_file.payload_address);
struct blisp_easy_transport data_transport = struct blisp_easy_transport data_transport =
blisp_easy_transport_new_from_memory(parsed_file.payload, blisp_easy_transport_new_from_memory(parsed_file.payload,
parsed_file.payload_length); parsed_file.payload_length);

View File

@ -3,6 +3,11 @@
// //
#include "dfu_file.h" #include "dfu_file.h"
#include <stdlib.h> #include <stdlib.h>
#include "parse_file.h"
#if defined(_MSC_VER)
#include <sys\types.h>
#endif
#define DFU_SUFFIX_LENGTH 16 #define DFU_SUFFIX_LENGTH 16
#define LMDFU_PREFIX_LENGTH 8 #define LMDFU_PREFIX_LENGTH 8
@ -73,11 +78,17 @@ int dfu_file_parse(const char* file_path_on_disk,
size_t* payload_address) { size_t* payload_address) {
uint8_t* dfu_file_contents = NULL; uint8_t* dfu_file_contents = NULL;
ssize_t file_size = get_file_contents(file_path_on_disk, &dfu_file_contents); ssize_t file_size = get_file_contents(file_path_on_disk, &dfu_file_contents);
if (file_size <= 0 || dfu_file_contents == NULL) { if (file_size < 0) {
return -1; return file_size;
}
if (file_size == 0 || dfu_file_contents == NULL) {
return PARSED_ERROR_CANT_OPEN_FILE;
} }
// Parse DFU data // Parse DFU data
struct dfu_file dfu_info = parse_dfu_suffix(dfu_file_contents, file_size); struct dfu_file dfu_info = parse_dfu_suffix(dfu_file_contents, file_size);
if (dfu_info.size.firmware == 0) {
return PARSED_ERROR_BAD_DFU;
}
// Check if its for a BL* chip // Check if its for a BL* chip
// if (dfu_info.idVendor != 0x28E9) { // if (dfu_info.idVendor != 0x28E9) {
// free(dfu_file_contents); // free(dfu_file_contents);
@ -200,7 +211,8 @@ struct dfu_file parse_dfu_suffix(const uint8_t* file_contents,
if (file_contents_length < DFU_SUFFIX_LENGTH) { if (file_contents_length < DFU_SUFFIX_LENGTH) {
reason = "File too short for DFU suffix"; reason = "File too short for DFU suffix";
missing_suffix = 1; missing_suffix = 1;
goto checked; output.size.firmware = 0;
return output;
} }
dfu_suffix = file_contents + file_contents_length - DFU_SUFFIX_LENGTH; dfu_suffix = file_contents + file_contents_length - DFU_SUFFIX_LENGTH;
@ -208,7 +220,8 @@ struct dfu_file parse_dfu_suffix(const uint8_t* file_contents,
if (dfu_suffix[10] != 'D' || dfu_suffix[9] != 'F' || dfu_suffix[8] != 'U') { if (dfu_suffix[10] != 'D' || dfu_suffix[9] != 'F' || dfu_suffix[8] != 'U') {
reason = "Invalid DFU suffix signature"; reason = "Invalid DFU suffix signature";
missing_suffix = 1; missing_suffix = 1;
goto checked; output.size.firmware = 0;
return output;
} }
// Calculate contents CRC32 // Calculate contents CRC32
for (int i = 0; i < file_contents_length - 4; i++) { for (int i = 0; i < file_contents_length - 4; i++) {
@ -221,7 +234,9 @@ struct dfu_file parse_dfu_suffix(const uint8_t* file_contents,
if (output.dwCRC != crc) { if (output.dwCRC != crc) {
reason = "DFU suffix CRC does not match"; reason = "DFU suffix CRC does not match";
missing_suffix = 1; missing_suffix = 1;
goto checked;
output.size.firmware = 0;
return output;
} }
/* At this point we believe we have a DFU suffix /* At this point we believe we have a DFU suffix
@ -233,18 +248,21 @@ struct dfu_file parse_dfu_suffix(const uint8_t* file_contents,
if (output.size.suffix < DFU_SUFFIX_LENGTH) { if (output.size.suffix < DFU_SUFFIX_LENGTH) {
fprintf(stderr, "Unsupported DFU suffix length %d", output.size.suffix); fprintf(stderr, "Unsupported DFU suffix length %d", output.size.suffix);
output.size.firmware = 0;
return output;
} }
if (output.size.suffix > file_contents_length) { if (output.size.suffix > file_contents_length) {
fprintf(stderr, "Invalid DFU suffix length %d", output.size.suffix); fprintf(stderr, "Invalid DFU suffix length %d", output.size.suffix);
output.size.firmware = 0;
return output;
} }
output.idVendor = (dfu_suffix[5] << 8) + dfu_suffix[4]; output.idVendor = (dfu_suffix[5] << 8) + dfu_suffix[4];
output.idProduct = (dfu_suffix[3] << 8) + dfu_suffix[2]; output.idProduct = (dfu_suffix[3] << 8) + dfu_suffix[2];
output.bcdDevice = (dfu_suffix[1] << 8) + dfu_suffix[0]; output.bcdDevice = (dfu_suffix[1] << 8) + dfu_suffix[0];
checked: const int res = probe_prefix(&output);
int res = probe_prefix(&output);
if (output.size.prefix) { if (output.size.prefix) {
const uint8_t* data = file_contents; const uint8_t* data = file_contents;

View File

@ -1,6 +1,11 @@
#pragma once #ifndef PARSE_FILE_H_
#define PARSE_FILE_H_
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
#include "parsed_firmware_file.h" #include "parsed_firmware_file.h"
#define PARSED_ERROR_INVALID_FILETYPE -0x1000 #define PARSED_ERROR_INVALID_FILETYPE -0x1000
@ -18,4 +23,6 @@ int parse_firmware_file(const char* file_path_on_disk,
// Internal util // Internal util
ssize_t get_file_contents(const char* file_path_on_disk, ssize_t get_file_contents(const char* file_path_on_disk,
uint8_t** file_contents); uint8_t** file_contents);
#endif // PARSE_FILE_H_

View File

@ -1,4 +1,5 @@
#pragma once #ifndef PARSED_FIRMWARE_H_
#define PARSED_FIRMWARE_H_
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -13,3 +14,4 @@ typedef struct {
size_t payload_length; // Size of the payload size_t payload_length; // Size of the payload
size_t payload_address; // Start address of the payload size_t payload_address; // Start address of the payload
} parsed_firmware_file_t; } parsed_firmware_file_t;
#endif // PARSED_FIRMWARE_H_