Commit 0313fee2 authored by Raptor Engineering Development Team's avatar Raptor Engineering Development Team
Browse files

Add BMC/Flash firmware write support

parent 76bcf0b0
......@@ -12,14 +12,19 @@ LOG_MODULE_REGISTER(flash_filesystem, LOG_LEVEL_DBG);
#include <ctype.h>
#include <zephyr.h>
#include <device.h>
#include <sys/crc.h>
#include <fs/fs.h>
#include <fs/littlefs.h>
#include <drivers/flash.h>
#include <storage/flash_map.h>
#define WITH_ZEPHYR 1
#include "kestrel.h"
extern const struct flash_area *flash_map;
extern const int flash_map_entries;
// Matches LFS_NAME_MAX */
#define MAX_PATH_LEN 255
......@@ -103,3 +108,52 @@ fail:
return -1;
}
int write_firmware_to_flash(const char* device_name, int partition_number, const uint8_t* data, size_t length, int add_litex_header)
{
static struct flash_area const *flash_area;
int result;
// Find BMC firmware Flash map area
flash_area = NULL;
for (int i = 0; i < flash_map_entries; i++) {
if (strcmp(flash_map[i].fa_dev_name, "bmc") == 0) {
if (flash_map[i].fa_id == partition_number) {
flash_area = &flash_map[i];
break;
}
}
}
if (!flash_area) {
return -ENODEV;
}
if (length > flash_area->fa_size) {
length = flash_area->fa_size;
}
LOG_INF("Erasing %s firmware partition %d...", device_name, partition_number);
result = flash_area_erase(flash_area, 0x0, flash_area->fa_size);
if (result < 0) {
LOG_INF("Erase failed!");
return result;
}
LOG_INF("Writing %s firmware partition %d...", device_name, partition_number);
if (add_litex_header) {
result = flash_area_write(flash_area, 0x0, &length, 4);
if (!result) {
uint32_t crc = crc32_ieee(data, length);
result = flash_area_write(flash_area, 0x4, &crc, 4);
}
}
if (!result) {
result = flash_area_write(flash_area, (add_litex_header)?0x8:0x0, data, length);
}
if (result < 0) {
LOG_INF("Write failed!");
return result;
}
return 0;
}
\ No newline at end of file
......@@ -8,4 +8,6 @@
int initialize_flash_filesystem(void);
int write_firmware_to_flash(const char* device_name, int partition_number, const uint8_t* data, size_t length, int add_litex_header);
#endif // _FLASH_FILESYSTEM_H
\ No newline at end of file
......@@ -12,6 +12,7 @@
#define WITH_ZEPHYR 1
#include "kestrel.h"
#include "flash_filesystem.h"
#define KESTREL_CONSOLE_THREAD_PRIORITY K_PRIO_PREEMPT(CONFIG_NUM_PREEMPT_PRIORITIES - 1)
static K_THREAD_STACK_DEFINE(kestrel_console_thread_stack, 16384);
......@@ -94,7 +95,7 @@ static void print_kestrel_utility_command_usage(const struct shell *shell, char*
}
static void print_kestrel_reflash_command_usage(const struct shell *shell, char* command_name) {
shell_print(shell, "Usage: %s <pnor>", command_name);
shell_print(shell, "Usage: %s <fpga bmc pnor>", command_name);
}
static void print_kestrel_memdump_command_usage(const struct shell *shell, char* command_name) {
......@@ -138,6 +139,12 @@ static int kestrel_shell_cmd_reflash(const struct shell *shell, size_t argc, cha
return write_flash_buffer_to_flash();
}
else if (strcmp(argv[1], "bmc") == 0) {
return write_firmware_to_flash("bmc", 1, main_firmware_buffer.buffer_address, main_firmware_buffer.valid_bytes, 1);
}
else if (strcmp(argv[1], "fpga") == 0) {
return write_firmware_to_flash("bmc", 0, main_firmware_buffer.buffer_address, main_firmware_buffer.valid_bytes, 0);
}
else {
shell_print(shell, "%s: Invalid parameter", argv[0]);
print_kestrel_reflash_command_usage(shell, argv[0]);
......
......@@ -152,7 +152,7 @@ int firmware_upload_form_handler(struct mg_connection *conn, void *cbdata)
"enctype=\"multipart/form-data\">\n",
(const char *)cbdata);
mg_printf(conn, "<input type=\"file\" name=\"firmwarefile\" multiple>\n");
mg_printf(conn, "<input type=\"submit\" value=\"Submit\">\n");
mg_printf(conn, "<input type=\"submit\" value=\"Upload\">\n");
mg_printf(conn, "</form>\n");
SEND_STANDARD_FOOTER(conn);
......@@ -213,6 +213,9 @@ int firmware_upload_handler(struct mg_connection *conn, void *cbdata)
/* Call the form handler */
ret = mg_handle_form_request(conn, &fdh);
// Clear out the remainder of the buffer
memset(main_firmware_buffer.buffer_address + main_firmware_buffer.valid_bytes, 0xff, main_firmware_buffer.buffer_length - main_firmware_buffer.valid_bytes);
main_firmware_buffer.locked = 0;
mg_printf(conn,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment