mirror of
https://github.com/pine64/blisp.git
synced 2024-12-22 22:40:15 +00:00
DFU file work
Just grabbing first one for now
Expose crc
Update CMakeLists.txt
Test DFU file
Split crc function
Fixup
Zero init struct
Dont null the pointer 🤦
correct fread
Adding tests
Create dfu_file.h
Format
Scratching out more parsing
Basic port parser
Scratching fread wrapper
This commit is contained in:
parent
048a724082
commit
98343db387
@ -1,10 +1,11 @@
|
|||||||
cmake_minimum_required(VERSION 3.22)
|
cmake_minimum_required(VERSION 3.22)
|
||||||
project(blisp C)
|
project(blisp C)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(CMAKE_C_STANDARD 23)
|
set(CMAKE_C_STANDARD 23)
|
||||||
|
|
||||||
option(BLISP_BUILD_CLI "Build CLI Tool" OFF)
|
option(BLISP_BUILD_CLI "Build CLI Tool" OFF)
|
||||||
option(BLISP_USE_SYSTEM_LIBRARIES "Use system-installed libraries" "${CMAKE_USE_SYSTEM_LIBRARIES}")
|
option(BLISP_USE_SYSTEM_LIBRARIES "Use system-installed libraries" "${CMAKE_USE_SYSTEM_LIBRARIES}")
|
||||||
|
option(COMPILE_TESTS "Compile the tests" OFF)
|
||||||
|
|
||||||
add_library(libblisp_obj OBJECT
|
add_library(libblisp_obj OBJECT
|
||||||
lib/blisp.c
|
lib/blisp.c
|
||||||
@ -94,3 +95,8 @@ install(TARGETS libblisp libblisp_static DESTINATION lib)
|
|||||||
if(BLISP_BUILD_CLI)
|
if(BLISP_BUILD_CLI)
|
||||||
add_subdirectory(tools/blisp)
|
add_subdirectory(tools/blisp)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
if(COMPILE_TESTS)
|
||||||
|
add_subdirectory(tools/blisp/src/cmd/dfu/tests)
|
||||||
|
endif(COMPILE_TESTS)
|
@ -2,7 +2,7 @@ set(ARGTABLE3_ENABLE_TESTS OFF CACHE BOOL "Enable unit tests")
|
|||||||
set(ARGTABLE3_ENABLE_EXAMPLES OFF CACHE BOOL "Enable examples")
|
set(ARGTABLE3_ENABLE_EXAMPLES OFF CACHE BOOL "Enable examples")
|
||||||
#set(ARGTABLE3_REPLACE_GETOPT OFF CACHE BOOL "Replace getopt in the system C library")
|
#set(ARGTABLE3_REPLACE_GETOPT OFF CACHE BOOL "Replace getopt in the system C library")
|
||||||
|
|
||||||
add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c)
|
add_executable(blisp src/main.c src/cmd/write.c src/util.c src/common.c src/cmd/iot.c src/cmd/dfu/dfu_file.c src/cmd/dfu/dfu_crc.c)
|
||||||
|
|
||||||
if(BLISP_USE_SYSTEM_LIBRARIES)
|
if(BLISP_USE_SYSTEM_LIBRARIES)
|
||||||
find_package(Argtable3 REQUIRED)
|
find_package(Argtable3 REQUIRED)
|
||||||
|
53
tools/blisp/src/cmd/dfu/dfu_crc.c
Normal file
53
tools/blisp/src/cmd/dfu/dfu_crc.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
//
|
||||||
|
// Created by ralim on 26/09/22.
|
||||||
|
//
|
||||||
|
#include "dfu_file.h"
|
||||||
|
static const unsigned long crc32_table[]
|
||||||
|
= { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||||
|
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||||
|
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||||
|
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||||
|
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||||
|
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||||
|
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||||
|
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||||
|
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||||
|
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||||
|
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||||
|
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||||
|
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||||
|
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||||
|
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||||
|
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||||
|
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
|
||||||
|
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||||
|
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
|
||||||
|
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||||
|
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||||
|
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||||
|
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
|
||||||
|
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||||
|
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||||
|
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||||
|
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||||
|
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||||
|
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
|
||||||
|
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||||
|
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
|
||||||
|
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||||
|
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||||
|
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||||
|
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||||
|
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||||
|
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
|
||||||
|
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||||
|
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||||
|
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||||
|
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||||
|
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||||
|
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d };
|
||||||
|
|
||||||
|
uint32_t
|
||||||
|
crc32_byte(uint32_t accum, uint8_t delta) {
|
||||||
|
return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8);
|
||||||
|
}
|
322
tools/blisp/src/cmd/dfu/dfu_file.c
Normal file
322
tools/blisp/src/cmd/dfu/dfu_file.c
Normal file
@ -0,0 +1,322 @@
|
|||||||
|
//
|
||||||
|
// Created by ralim on 25/09/22.
|
||||||
|
//
|
||||||
|
#include "dfu_file.h"
|
||||||
|
|
||||||
|
#define DFU_SUFFIX_LENGTH 16
|
||||||
|
#define LMDFU_PREFIX_LENGTH 8
|
||||||
|
#define LPCDFU_PREFIX_LENGTH 16
|
||||||
|
|
||||||
|
struct dfu_file {
|
||||||
|
/* File name */
|
||||||
|
const char* name;
|
||||||
|
/* Pointer to file loaded into memory */
|
||||||
|
const uint8_t* firmware;
|
||||||
|
/* Different sizes */
|
||||||
|
struct {
|
||||||
|
off_t total;
|
||||||
|
off_t firmware;
|
||||||
|
int prefix;
|
||||||
|
int suffix;
|
||||||
|
} size;
|
||||||
|
/* From prefix fields */
|
||||||
|
uint32_t lmdfu_address;
|
||||||
|
/* From prefix fields */
|
||||||
|
uint32_t prefix_type;
|
||||||
|
|
||||||
|
/* From DFU suffix fields */
|
||||||
|
uint32_t dwCRC;
|
||||||
|
uint16_t bcdDFU;
|
||||||
|
uint16_t idVendor;
|
||||||
|
uint16_t idProduct;
|
||||||
|
uint16_t bcdDevice;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum prefix_type {
|
||||||
|
ZERO_PREFIX,
|
||||||
|
DFUSE_PREFIX,
|
||||||
|
LMDFU_PREFIX,
|
||||||
|
LPCDFU_UNENCRYPTED_PREFIX
|
||||||
|
};
|
||||||
|
|
||||||
|
struct dfu_file parse_dfu_suffix(const uint8_t* file_contents,
|
||||||
|
size_t file_contents_length);
|
||||||
|
ssize_t parse_target(const uint8_t* data, uint8_t* out_ealt,
|
||||||
|
uint8_t** out_data, size_t* out_data_size,
|
||||||
|
size_t* out_data_address);
|
||||||
|
ssize_t get_file_contents(const char* file_path_on_disk,
|
||||||
|
uint8_t** file_contents);
|
||||||
|
|
||||||
|
/* Parse a .dfu file and extract its payload and metadata
|
||||||
|
* Returns 0 if file parsed correctly, negative on error
|
||||||
|
* Inputs:
|
||||||
|
* - File path to read from
|
||||||
|
*
|
||||||
|
* Outputs:
|
||||||
|
* - File payload contents
|
||||||
|
* - File payload start address
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* uint8_t* payload=NULL;
|
||||||
|
* size_t payload_length=0;
|
||||||
|
* int res = dfu_file_path("test.dfu",&payload,&payload_length);
|
||||||
|
* ...
|
||||||
|
* free(payload);
|
||||||
|
*/
|
||||||
|
|
||||||
|
int
|
||||||
|
dfu_file_parse(const char* file_path_on_disk, uint8_t** payload,
|
||||||
|
size_t* payload_length, size_t* payload_address) {
|
||||||
|
uint8_t* dfu_file_contents = NULL;
|
||||||
|
ssize_t file_size
|
||||||
|
= get_file_contents(file_path_on_disk, &dfu_file_contents);
|
||||||
|
if (file_size <= 0 || dfu_file_contents == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Parse DFU data
|
||||||
|
struct dfu_file dfu_info = parse_dfu_suffix(dfu_file_contents, file_size);
|
||||||
|
// Check if its for a BL* chip
|
||||||
|
// if (dfu_info.idVendor != 0x28E9) {
|
||||||
|
// free(dfu_file_contents);
|
||||||
|
// return -1;
|
||||||
|
// }
|
||||||
|
// Okay we have done validation, walk firmware and extract the blob and the
|
||||||
|
// offset
|
||||||
|
size_t data_consumed = 0;
|
||||||
|
while (data_consumed < dfu_info.size.firmware) {
|
||||||
|
uint8_t ealt = 0;
|
||||||
|
uint8_t* blob = NULL;
|
||||||
|
size_t blob_size = 0;
|
||||||
|
size_t blob_address = 0;
|
||||||
|
ssize_t res = parse_target(dfu_info.firmware + data_consumed, &ealt,
|
||||||
|
&blob, &blob_size, &blob_address);
|
||||||
|
if (res < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ealt == 0 && blob_size > 0) {
|
||||||
|
// Firmware slot, lets prep this and return
|
||||||
|
*payload = calloc(blob_size, 1);
|
||||||
|
*payload_length = blob_size;
|
||||||
|
*payload_address = blob_address;
|
||||||
|
memcpy(*payload, blob, blob_size);
|
||||||
|
free(dfu_file_contents);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
data_consumed += res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read next target, output data+size+alt. Returns bytes consumed
|
||||||
|
ssize_t
|
||||||
|
parse_target(const uint8_t* data, uint8_t* out_ealt, uint8_t** out_data,
|
||||||
|
size_t* out_data_size, size_t* out_data_address) {
|
||||||
|
if (data == NULL || out_ealt == NULL || out_data == NULL
|
||||||
|
|| out_data_size == NULL) {
|
||||||
|
return -99;
|
||||||
|
}
|
||||||
|
if (data[0] != 'T' || data[1] != 'a') {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_ealt = data[6];
|
||||||
|
uint8_t* tdata = data + 6 + 1 + 4 + 255;
|
||||||
|
uint32_t len_tdata = *((uint32_t*)tdata);
|
||||||
|
tdata += 4;
|
||||||
|
uint32_t num_images = *((uint32_t*)tdata);
|
||||||
|
tdata += 4;
|
||||||
|
ssize_t blob_length = 6 + 1 + 4 + 255 + 8 + len_tdata;
|
||||||
|
// Now read all the image blobs from this target
|
||||||
|
for (int i = 0; i < num_images; i++) {
|
||||||
|
uint32_t address = *((uint32_t*)tdata);
|
||||||
|
tdata += 4;
|
||||||
|
uint32_t len = *((uint32_t*)tdata);
|
||||||
|
tdata += 4;
|
||||||
|
*out_data = tdata;
|
||||||
|
*out_data_size = len;
|
||||||
|
*out_data_address = address;
|
||||||
|
return blob_length;
|
||||||
|
// tdata+=len;
|
||||||
|
}
|
||||||
|
return blob_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
probe_prefix(struct dfu_file* file) {
|
||||||
|
const uint8_t* prefix = file->firmware;
|
||||||
|
file->size.prefix = 0;
|
||||||
|
if (file->size.total < LMDFU_PREFIX_LENGTH)
|
||||||
|
return 1;
|
||||||
|
if (prefix[0] == 'D' && prefix[1] == 'f' && prefix[2] == 'u'
|
||||||
|
&& prefix[3] == 'S' && prefix[4] == 'e') {
|
||||||
|
// DfuSe header
|
||||||
|
// https://sourceforge.net/p/dfu-util/dfu-util/ci/master/tree/dfuse-pack.py#l110
|
||||||
|
|
||||||
|
file->size.prefix = 11;
|
||||||
|
file->prefix_type = DFUSE_PREFIX;
|
||||||
|
uint8_t numTargets = prefix[10];
|
||||||
|
printf("Number DFU Targets: %d\n", numTargets);
|
||||||
|
}
|
||||||
|
if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) {
|
||||||
|
uint32_t payload_length = (prefix[7] << 24) | (prefix[6] << 16)
|
||||||
|
| (prefix[5] << 8) | prefix[4];
|
||||||
|
uint32_t expected_payload_length = (uint32_t)file->size.total
|
||||||
|
- LMDFU_PREFIX_LENGTH
|
||||||
|
- file->size.suffix;
|
||||||
|
if (payload_length != expected_payload_length)
|
||||||
|
return 1;
|
||||||
|
file->prefix_type = LMDFU_PREFIX;
|
||||||
|
file->size.prefix = LMDFU_PREFIX_LENGTH;
|
||||||
|
file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]);
|
||||||
|
} else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f) == 0x3f)) {
|
||||||
|
file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX;
|
||||||
|
file->size.prefix = LPCDFU_PREFIX_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file->size.prefix + file->size.suffix > file->size.total)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dfu_file
|
||||||
|
parse_dfu_suffix(const uint8_t* file_contents,
|
||||||
|
const size_t file_contents_length) {
|
||||||
|
// This is nearly 1:1 based on
|
||||||
|
// https://sourceforge.net/p/dfu-util/dfu-util/ci/master/tree/src/dfu_file.c#l368
|
||||||
|
struct dfu_file output;
|
||||||
|
memset(&output, 0, sizeof(output));
|
||||||
|
output.firmware = file_contents;
|
||||||
|
output.size.total = (off_t)file_contents_length;
|
||||||
|
/* Check for possible DFU file suffix by trying to parse one */
|
||||||
|
|
||||||
|
uint32_t crc = 0xffffffff;
|
||||||
|
const uint8_t* dfu_suffix;
|
||||||
|
int missing_suffix = 0;
|
||||||
|
const char* reason;
|
||||||
|
|
||||||
|
if (file_contents_length < DFU_SUFFIX_LENGTH) {
|
||||||
|
reason = "File too short for DFU suffix";
|
||||||
|
missing_suffix = 1;
|
||||||
|
goto checked;
|
||||||
|
}
|
||||||
|
|
||||||
|
dfu_suffix = file_contents + file_contents_length - DFU_SUFFIX_LENGTH;
|
||||||
|
|
||||||
|
if (dfu_suffix[10] != 'D' || dfu_suffix[9] != 'F'
|
||||||
|
|| dfu_suffix[8] != 'U') {
|
||||||
|
reason = "Invalid DFU suffix signature";
|
||||||
|
missing_suffix = 1;
|
||||||
|
goto checked;
|
||||||
|
}
|
||||||
|
// Calculate contents CRC32
|
||||||
|
for (int i = 0; i < file_contents_length - 4; i++) {
|
||||||
|
crc = crc32_byte(crc, file_contents[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.dwCRC = (dfu_suffix[15] << 24) + (dfu_suffix[14] << 16)
|
||||||
|
+ (dfu_suffix[13] << 8) + dfu_suffix[12];
|
||||||
|
|
||||||
|
if (output.dwCRC != crc) {
|
||||||
|
reason = "DFU suffix CRC does not match";
|
||||||
|
missing_suffix = 1;
|
||||||
|
goto checked;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* At this point we believe we have a DFU suffix
|
||||||
|
so we require further checks to succeed */
|
||||||
|
|
||||||
|
output.bcdDFU = (dfu_suffix[7] << 8) + dfu_suffix[6];
|
||||||
|
|
||||||
|
output.size.suffix = dfu_suffix[11];
|
||||||
|
|
||||||
|
if (output.size.suffix < DFU_SUFFIX_LENGTH) {
|
||||||
|
fprintf(stderr, "Unsupported DFU suffix length %d",
|
||||||
|
output.size.suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (output.size.suffix > file_contents_length) {
|
||||||
|
fprintf(stderr, "Invalid DFU suffix length %d", output.size.suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.idVendor = (dfu_suffix[5] << 8) + dfu_suffix[4];
|
||||||
|
output.idProduct = (dfu_suffix[3] << 8) + dfu_suffix[2];
|
||||||
|
output.bcdDevice = (dfu_suffix[1] << 8) + dfu_suffix[0];
|
||||||
|
|
||||||
|
checked:
|
||||||
|
const int res = probe_prefix(&output);
|
||||||
|
|
||||||
|
if (output.size.prefix) {
|
||||||
|
const uint8_t* data = file_contents;
|
||||||
|
if (output.prefix_type == DFUSE_PREFIX) {
|
||||||
|
} else if (output.prefix_type == LMDFU_PREFIX) {
|
||||||
|
printf("Possible TI Stellaris DFU prefix with "
|
||||||
|
"the following properties\n"
|
||||||
|
"Address: 0x%08x\n"
|
||||||
|
"Payload length: %d\n",
|
||||||
|
output.lmdfu_address,
|
||||||
|
data[4] | (data[5] << 8) | (data[6] << 16)
|
||||||
|
| (data[7] << 24));
|
||||||
|
} else if (output.prefix_type == LPCDFU_UNENCRYPTED_PREFIX) {
|
||||||
|
printf("Possible unencrypted NXP LPC DFU prefix with "
|
||||||
|
"the following properties\n"
|
||||||
|
"Payload length: %d kiByte\n",
|
||||||
|
data[2] >> 1 | (data[3] << 7));
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Unknown DFU prefix type");
|
||||||
|
}
|
||||||
|
output.firmware
|
||||||
|
= output.firmware + output.size.prefix; // shift past prefix
|
||||||
|
}
|
||||||
|
output.size.firmware
|
||||||
|
= output.size.total - (output.size.suffix + output.size.prefix);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns file size _or_ negative on error
|
||||||
|
ssize_t
|
||||||
|
get_file_contents(const char* file_path_on_disk, uint8_t** file_contents) {
|
||||||
|
size_t read_count;
|
||||||
|
size_t file_size = 0;
|
||||||
|
size_t read_total = 0;
|
||||||
|
|
||||||
|
FILE* f;
|
||||||
|
|
||||||
|
if (file_contents == NULL) {
|
||||||
|
return -99;
|
||||||
|
}
|
||||||
|
|
||||||
|
f = fopen(file_path_on_disk, "rb");
|
||||||
|
if (f <= 0) {
|
||||||
|
fprintf(stderr, "Could not open file %s for reading",
|
||||||
|
file_path_on_disk);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
file_size = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
*file_contents = calloc(file_size, sizeof(uint8_t));
|
||||||
|
|
||||||
|
while (read_total < file_size) {
|
||||||
|
size_t to_read = file_size - read_total;
|
||||||
|
/* read() limit on Linux, slightly below MAX_INT on Windows */
|
||||||
|
if (to_read > 0x7ffff000)
|
||||||
|
to_read = 0x7ffff000;
|
||||||
|
read_count = fread((*file_contents) + read_total, 1, to_read, f);
|
||||||
|
if (read_count == 0)
|
||||||
|
break;
|
||||||
|
// If error and not end of file, break
|
||||||
|
if (read_count == -1 && !feof(f))
|
||||||
|
break;
|
||||||
|
read_total += read_count;
|
||||||
|
}
|
||||||
|
if (read_total != file_size) {
|
||||||
|
fprintf(stderr, "Could only read %lld of %lld bytes from %s",
|
||||||
|
(long long)read_total, (long long)file_size,
|
||||||
|
file_path_on_disk);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
return (ssize_t)file_size;
|
||||||
|
}
|
26
tools/blisp/src/cmd/dfu/dfu_file.h
Normal file
26
tools/blisp/src/cmd/dfu/dfu_file.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by ralim on 26/09/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BLISP_DFU_FILE_H
|
||||||
|
#define BLISP_DFU_FILE_H
|
||||||
|
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int dfu_file_parse(const char* file_path_on_disk, uint8_t** payload,
|
||||||
|
size_t* payload_length, size_t* payload_address);
|
||||||
|
// Internal
|
||||||
|
|
||||||
|
uint32_t crc32_byte(uint32_t accum, uint8_t delta);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // BLISP_DFU_FILE_H
|
31
tools/blisp/src/cmd/dfu/tests/CMakeLists.txt
Normal file
31
tools/blisp/src/cmd/dfu/tests/CMakeLists.txt
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
enable_language(CXX)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||||
|
GIT_TAG release-1.11.0
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
add_library(GTest::GTest INTERFACE IMPORTED)
|
||||||
|
target_link_libraries(GTest::GTest INTERFACE gtest_main)
|
||||||
|
|
||||||
|
|
||||||
|
add_executable(dfu_file_test test_dfu_file.cpp ../dfu_file.c ../dfu_crc.c)
|
||||||
|
|
||||||
|
target_link_libraries(dfu_file_test
|
||||||
|
PRIVATE
|
||||||
|
GTest::GTest
|
||||||
|
)
|
||||||
|
include_directories(dfu_file_test PRIVATE ../)
|
||||||
|
add_test(dfu_file_test_gtests dfu_file_test)
|
||||||
|
|
||||||
|
configure_file(Config.h.in ${CMAKE_BINARY_DIR}/Config.h)
|
||||||
|
include_directories(${CMAKE_BINARY_DIR})
|
||||||
|
|
||||||
|
set(TEST_APP_NAME dfu_file_tests)
|
||||||
|
|
||||||
|
#add_custom_command(TARGET ${TEST_APP_NAME} COMMAND ./${TEST_APP_NAME} POST_BUILD)
|
10
tools/blisp/src/cmd/dfu/tests/Config.h.in
Normal file
10
tools/blisp/src/cmd/dfu/tests/Config.h.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
//
|
||||||
|
// Created by ralim on 26/09/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BLISP_CONFIG_H_IN_H
|
||||||
|
#define BLISP_CONFIG_H_IN_H
|
||||||
|
|
||||||
|
#define SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
|
|
||||||
|
#endif // BLISP_CONFIG_H_IN_H
|
BIN
tools/blisp/src/cmd/dfu/tests/test.dfu
Normal file
BIN
tools/blisp/src/cmd/dfu/tests/test.dfu
Normal file
Binary file not shown.
17
tools/blisp/src/cmd/dfu/tests/test_dfu_file.cpp
Normal file
17
tools/blisp/src/cmd/dfu/tests/test_dfu_file.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by ralim on 26/09/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Config.h"
|
||||||
|
#include "dfu_file.h"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
TEST(DFU_FILE_PARSER, ParseTestFile) {
|
||||||
|
uint8_t* payload = nullptr;
|
||||||
|
size_t payload_size = 0;
|
||||||
|
size_t payload_address = 0;
|
||||||
|
int res = dfu_file_parse(SOURCE_DIR "/test.dfu", &payload, &payload_size,
|
||||||
|
&payload_address);
|
||||||
|
ASSERT_EQ(res, 1);
|
||||||
|
ASSERT_EQ(payload_size, 1337);
|
||||||
|
ASSERT_EQ(payload_address, 0x11223344);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user