mirror of
https://github.com/pine64/blisp.git
synced 2026-09-18 21:01:29 +00:00
Pull in the generic parser
Fixup .
This commit is contained in:
+48
-45
@@ -9,6 +9,7 @@
|
|||||||
#include "../cmd.h"
|
#include "../cmd.h"
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
|
#include "parse_file.h"
|
||||||
|
|
||||||
#define REG_EXTENDED 1
|
#define REG_EXTENDED 1
|
||||||
#define REG_ICASE (REG_EXTENDED << 1)
|
#define REG_ICASE (REG_EXTENDED << 1)
|
||||||
@@ -177,61 +178,63 @@ blisp_return_t blisp_flash_firmware() {
|
|||||||
// TODO: Error handling
|
// TODO: Error handling
|
||||||
goto exit1;
|
goto exit1;
|
||||||
}
|
}
|
||||||
|
parsed_firmware_file_t parsed_file;
|
||||||
|
memset(&parsed_file, 0, sizeof(parsed_file));
|
||||||
|
int parsed_result =
|
||||||
|
parse_firmware_file(binary_to_write->filename[0], &parsed_file);
|
||||||
|
|
||||||
// Open the file to be flashed; to determine the size of the section of flash
|
// If we are injecting a bootloader section, make it, erase flash, and flash
|
||||||
// to erase
|
// it. Then when we do firmware later on; it will be located afterwards
|
||||||
int64_t firmware_file_size = 0;
|
// the header filles up to a flash erase boundry so this stack should be safe
|
||||||
const uint32_t firmware_base_address_offset =
|
// __should__
|
||||||
0x2000; // Firmware files start 0x2000 offset into flash to skip the boot
|
|
||||||
// header
|
|
||||||
int64_t firmware_file_start_address = 0;
|
|
||||||
|
|
||||||
|
if (parsed_file.needs_boot_struct) {
|
||||||
|
// Create a default boot header section in ram to be written out
|
||||||
|
struct bfl_boot_header boot_header;
|
||||||
|
fill_up_boot_header(&boot_header);
|
||||||
|
printf("Erasing flash to flash boot header\r\n");
|
||||||
|
ret = blisp_device_flash_erase(&device, 0x0000,
|
||||||
|
sizeof(struct bfl_boot_header));
|
||||||
|
if (ret != BLISP_OK) {
|
||||||
|
fprintf(stderr, "Failed to erase flash.\n");
|
||||||
|
goto exit2;
|
||||||
|
}
|
||||||
|
// Now burn the header
|
||||||
|
|
||||||
|
printf("Flashing boot header...\n");
|
||||||
FILE* firmware_file = fopen(binary_to_write->filename[0], "rb");
|
ret = blisp_device_flash_write(&device, 0x0000, (uint8_t*)&boot_header,
|
||||||
if (firmware_file == NULL) {
|
sizeof(struct bfl_boot_header));
|
||||||
fprintf(stderr, "Failed to open firmware file \"%s\".\n",
|
if (ret != BLISP_OK) {
|
||||||
binary_to_write->filename[0]);
|
fprintf(stderr, "Failed to write boot header.\n");
|
||||||
goto exit1;
|
goto exit2;
|
||||||
|
}
|
||||||
|
// Move the firmware to-be-flashed beyond the boot header area
|
||||||
|
parsed_file.payload_address += 0x2000;
|
||||||
}
|
}
|
||||||
fseek(firmware_file, 0, SEEK_END);
|
// Now that optional boot header is done, we clear out the flash for the new
|
||||||
firmware_file_size = ftell(firmware_file);
|
// firmware; and flash it in.
|
||||||
rewind(firmware_file);
|
|
||||||
|
|
||||||
// Create a default boot header section in ram to be written out
|
printf("Erasing flash for firmware, this might take a while...\n");
|
||||||
struct bfl_boot_header boot_header;
|
ret = blisp_device_flash_erase(
|
||||||
fill_up_boot_header(&boot_header);
|
&device, parsed_file.payload_address,
|
||||||
|
parsed_file.payload_address + parsed_file.payload_length + 1);
|
||||||
const uint32_t firmware_base_address = 0x2000;
|
|
||||||
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) {
|
if (ret != BLISP_OK) {
|
||||||
fprintf(stderr, "Failed to erase flash.\n");
|
fprintf(stderr,
|
||||||
goto exit2;
|
"Failed to erase flash. Tried to erase from 0x%08X to 0x%08X\n",
|
||||||
}
|
parsed_file.payload_address,
|
||||||
ret =
|
parsed_file.payload_address + parsed_file.payload_length + 1);
|
||||||
blisp_device_flash_erase(&device, 0x0000, sizeof(struct bfl_boot_header));
|
|
||||||
if (ret != BLISP_OK) {
|
|
||||||
fprintf(stderr, "Failed to erase flash.\n");
|
|
||||||
goto exit2;
|
goto exit2;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, "Failed to write boot header.\n");
|
|
||||||
goto exit2;
|
|
||||||
}
|
|
||||||
printf("Flashing the firmware...\n");
|
printf("Flashing the firmware...\n");
|
||||||
struct blisp_easy_transport data_transport =
|
struct blisp_easy_transport data_transport =
|
||||||
blisp_easy_transport_new_from_file(firmware_file);
|
blisp_easy_transport_new_from_memory(parsed_file.payload,
|
||||||
|
parsed_file.payload_length);
|
||||||
|
|
||||||
|
ret = blisp_easy_flash_write(
|
||||||
|
&device, &data_transport, parsed_file.payload_address,
|
||||||
|
parsed_file.payload_length, blisp_common_progress_callback);
|
||||||
|
|
||||||
ret = blisp_easy_flash_write(&device, &data_transport, firmware_base_address,
|
|
||||||
firmware_file_size,
|
|
||||||
blisp_common_progress_callback);
|
|
||||||
if (ret < BLISP_OK) {
|
if (ret < BLISP_OK) {
|
||||||
fprintf(stderr, "Failed to write app to flash.\n");
|
fprintf(stderr, "Failed to write app to flash.\n");
|
||||||
goto exit2;
|
goto exit2;
|
||||||
@@ -254,8 +257,8 @@ blisp_return_t blisp_flash_firmware() {
|
|||||||
printf("Flash complete!\n");
|
printf("Flash complete!\n");
|
||||||
|
|
||||||
exit2:
|
exit2:
|
||||||
if (firmware_file != NULL)
|
if (parsed_file.payload != NULL)
|
||||||
fclose(firmware_file);
|
free(parsed_file.payload);
|
||||||
exit1:
|
exit1:
|
||||||
blisp_device_close(&device);
|
blisp_device_close(&device);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "parse_file.h"
|
#include "parse_file.h"
|
||||||
// Returns file size _or_ negative on error
|
// Returns file size _or_ negative on error
|
||||||
ssize_t get_file_contents(const char* file_path_on_disk,
|
ssize_t get_file_contents(const char* file_path_on_disk,
|
||||||
@@ -15,7 +16,8 @@ ssize_t get_file_contents(const char* file_path_on_disk,
|
|||||||
|
|
||||||
f = fopen(file_path_on_disk, "rb");
|
f = fopen(file_path_on_disk, "rb");
|
||||||
if (f <= 0) {
|
if (f <= 0) {
|
||||||
fprintf(stderr, "Could not open file %s for reading", file_path_on_disk);
|
fprintf(stderr, "Could not open file %s for reading\r\n",
|
||||||
|
file_path_on_disk);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,18 +18,19 @@ int parse_firmware_file(const char* file_path_on_disk,
|
|||||||
if (strncmp(ext, "dfu", 3) == 0 || strncmp(ext, "DFU", 3) == 0) {
|
if (strncmp(ext, "dfu", 3) == 0 || strncmp(ext, "DFU", 3) == 0) {
|
||||||
printf("Input file identified as a .dfu file\r\n");
|
printf("Input file identified as a .dfu file\r\n");
|
||||||
// Handle as a .dfu file
|
// Handle as a .dfu file
|
||||||
res = dfu_file_parse("test.dfu", &parsed_results->payload,
|
res = dfu_file_parse(file_path_on_disk, &parsed_results->payload,
|
||||||
&parsed_results->payload_length,
|
&parsed_results->payload_length,
|
||||||
&parsed_results->payload_address);
|
&parsed_results->payload_address);
|
||||||
} else if (strncmp(ext, "bin", 3) == 0 || strncmp(ext, "BIN", 3) == 0) {
|
} else if (strncmp(ext, "bin", 3) == 0 || strncmp(ext, "BIN", 3) == 0) {
|
||||||
printf("Input file identified as a .bin file\r\n");
|
printf("Input file identified as a .bin file\r\n");
|
||||||
// Raw binary file
|
// Raw binary file
|
||||||
res = bin_file_parse("test.dfu", &parsed_results->payload,
|
res = bin_file_parse(file_path_on_disk, &parsed_results->payload,
|
||||||
&parsed_results->payload_length,
|
&parsed_results->payload_length,
|
||||||
&parsed_results->payload_address);
|
&parsed_results->payload_address);
|
||||||
}
|
}
|
||||||
// If we wanted to support hex files, here would be where
|
// If we wanted to support hex files, here would be where
|
||||||
|
|
||||||
|
// If the firmware starts at "0" we need to pre-pend a boot sector later on
|
||||||
parsed_results->needs_boot_struct = parsed_results->payload_address == 0;
|
parsed_results->needs_boot_struct = parsed_results->payload_address == 0;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user