From 21469fea3ec55c5f7d850777f7db505bd8889a88 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 10 May 2022 14:51:05 -0400 Subject: [PATCH] flashcmd: Move flash command handlers to new flashcmd.c file Move the flashing command processing code from canboot_main.c to a new flashcmd.c file. Signed-off-by: Kevin O'Connor --- src/Makefile | 2 +- src/canboot_main.c | 82 ++++---------------------------------------- src/canboot_main.h | 3 -- src/flashcmd.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ src/flashcmd.h | 6 ++++ src/led.c | 2 +- 6 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/flashcmd.c create mode 100644 src/flashcmd.h diff --git a/src/Makefile b/src/Makefile index 21d5eea..89e6a06 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ # Main code build rules -src-y += canboot_main.c sched.c command.c +src-y += canboot_main.c sched.c command.c flashcmd.c src-$(CONFIG_ENABLE_LED) += led.c diff --git a/src/canboot_main.c b/src/canboot_main.c index 4279df8..fcf4868 100644 --- a/src/canboot_main.c +++ b/src/canboot_main.c @@ -4,11 +4,11 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. -#include // memmove +#include // strlen #include "autoconf.h" // CONFIG_* -#include "board/misc.h" // delay_ms +#include "board/misc.h" // udelay #include "board/canbus.h" // canbus_send -#include "board/flash.h" // write_page +#include "board/flash.h" // flash_read_block #include "board/gpio.h" // gpio_in_setup #include "canboot_main.h" // canboot_main #include "command.h" // command_respond_ack @@ -18,79 +18,8 @@ #define REQUEST_SIG 0x5984E3FA6CA1589B // Random request sig -static uint8_t page_buffer[CONFIG_MAX_FLASH_PAGE_SIZE]; -// Page Tracking -static uint32_t last_page_address = 0; -static uint8_t page_pending = 0; -static uint8_t is_in_transfer; static uint8_t complete = 0; -int -flashcmd_is_in_transfer(void) -{ - return is_in_transfer; -} - -static void -write_page(uint32_t page_address) -{ - flash_write_page(page_address, (uint16_t*)page_buffer); - memset(page_buffer, 0xFF, sizeof(page_buffer)); - last_page_address = page_address; - page_pending = 0; -} - -void -command_read_block(uint32_t *data) -{ - is_in_transfer = 1; - uint32_t block_address = le32_to_cpu(data[1]); - uint32_t out[CONFIG_BLOCK_SIZE / 4 + 2 + 2]; - out[2] = cpu_to_le32(block_address); - flash_read_block(block_address, &out[3]); - command_respond_ack(CMD_REQ_BLOCK, out, ARRAY_SIZE(out)); -} - -void -command_write_block(uint32_t *data) -{ - is_in_transfer = 1; - if (command_get_arg_count(data) != (CONFIG_BLOCK_SIZE / 4) + 1) { - command_respond_command_error(); - return; - } - uint32_t block_address = le32_to_cpu(data[1]); - if (block_address < CONFIG_APPLICATION_START) { - command_respond_command_error(); - return; - } - uint32_t flash_page_size = flash_get_page_size(); - uint32_t page_pos = block_address % flash_page_size; - memcpy(&page_buffer[page_pos], (uint8_t *)&data[2], CONFIG_BLOCK_SIZE); - page_pending = 1; - if (page_pos + CONFIG_BLOCK_SIZE == flash_page_size) - write_page(block_address - page_pos); - uint32_t out[4]; - out[2] = cpu_to_le32(block_address); - command_respond_ack(CMD_RX_BLOCK, out, ARRAY_SIZE(out)); -} - -void -command_eof(uint32_t *data) -{ - is_in_transfer = 0; - uint32_t flash_page_size = flash_get_page_size(); - if (page_pending) { - write_page(last_page_address + flash_page_size); - } - flash_complete(); - uint32_t out[4]; - out[2] = cpu_to_le32( - ((last_page_address - CONFIG_APPLICATION_START) - / flash_page_size) + 1); - command_respond_ack(CMD_RX_EOF, out, ARRAY_SIZE(out)); -} - void command_complete(uint32_t *data) { @@ -117,9 +46,10 @@ check_application_code(void) { // Read the first block of memory, if it // is all 0xFF then no application has been flashed - flash_read_block(CONFIG_APPLICATION_START, (uint32_t*)page_buffer); + uint8_t buf[CONFIG_BLOCK_SIZE]; + flash_read_block(CONFIG_APPLICATION_START, (uint32_t*)buf); for (uint8_t i = 0; i < CONFIG_BLOCK_SIZE; i++) { - if (page_buffer[i] != 0xFF) + if (buf[i] != 0xFF) return 1; } return 0; diff --git a/src/canboot_main.h b/src/canboot_main.h index 91c6788..50608bc 100644 --- a/src/canboot_main.h +++ b/src/canboot_main.h @@ -1,9 +1,6 @@ #ifndef __CANBOOT_MAIN_H #define __CANBOOT_MAIN_H -#include // uint32_t - -int flashcmd_is_in_transfer(void); void canboot_main(void); #endif // canboot_main.h diff --git a/src/flashcmd.c b/src/flashcmd.c new file mode 100644 index 0000000..d58d6a7 --- /dev/null +++ b/src/flashcmd.c @@ -0,0 +1,84 @@ +// Command handlers for flash requests +// +// Copyright (C) 2021 Eric Callahan +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include // memmove +#include "autoconf.h" // CONFIG_BLOCK_SIZE +#include "board/flash.h" // flash_write_page +#include "byteorder.h" // cpu_to_le32 +#include "command.h" // command_respond_ack +#include "flashcmd.h" // flashcmd_is_in_transfer + +static uint8_t page_buffer[CONFIG_MAX_FLASH_PAGE_SIZE]; +// Page Tracking +static uint32_t last_page_address = 0; +static uint8_t page_pending = 0; +static uint8_t is_in_transfer; + +int +flashcmd_is_in_transfer(void) +{ + return is_in_transfer; +} + +static void +write_page(uint32_t page_address) +{ + flash_write_page(page_address, (uint16_t*)page_buffer); + memset(page_buffer, 0xFF, sizeof(page_buffer)); + last_page_address = page_address; + page_pending = 0; +} + +void +command_read_block(uint32_t *data) +{ + is_in_transfer = 1; + uint32_t block_address = le32_to_cpu(data[1]); + uint32_t out[CONFIG_BLOCK_SIZE / 4 + 2 + 2]; + out[2] = cpu_to_le32(block_address); + flash_read_block(block_address, &out[3]); + command_respond_ack(CMD_REQ_BLOCK, out, ARRAY_SIZE(out)); +} + +void +command_write_block(uint32_t *data) +{ + is_in_transfer = 1; + if (command_get_arg_count(data) != (CONFIG_BLOCK_SIZE / 4) + 1) { + command_respond_command_error(); + return; + } + uint32_t block_address = le32_to_cpu(data[1]); + if (block_address < CONFIG_APPLICATION_START) { + command_respond_command_error(); + return; + } + uint32_t flash_page_size = flash_get_page_size(); + uint32_t page_pos = block_address % flash_page_size; + memcpy(&page_buffer[page_pos], (uint8_t *)&data[2], CONFIG_BLOCK_SIZE); + page_pending = 1; + if (page_pos + CONFIG_BLOCK_SIZE == flash_page_size) + write_page(block_address - page_pos); + uint32_t out[4]; + out[2] = cpu_to_le32(block_address); + command_respond_ack(CMD_RX_BLOCK, out, ARRAY_SIZE(out)); +} + +void +command_eof(uint32_t *data) +{ + is_in_transfer = 0; + uint32_t flash_page_size = flash_get_page_size(); + if (page_pending) { + write_page(last_page_address + flash_page_size); + } + flash_complete(); + uint32_t out[4]; + out[2] = cpu_to_le32( + ((last_page_address - CONFIG_APPLICATION_START) + / flash_page_size) + 1); + command_respond_ack(CMD_RX_EOF, out, ARRAY_SIZE(out)); +} diff --git a/src/flashcmd.h b/src/flashcmd.h new file mode 100644 index 0000000..9e48f97 --- /dev/null +++ b/src/flashcmd.h @@ -0,0 +1,6 @@ +#ifndef __FLASHCMD_H +#define __FLASHCMD_H + +int flashcmd_is_in_transfer(void); + +#endif // flashcmd.h diff --git a/src/led.c b/src/led.c index aa687bf..f7ad600 100644 --- a/src/led.c +++ b/src/led.c @@ -7,8 +7,8 @@ #include "autoconf.h" // CONFIG_ENABLE_LED #include "board/gpio.h" // gpio_out_setup #include "board/misc.h" // timer_read_time -#include "canboot_main.h" // flashcmd_is_in_transfer #include "ctr.h" // DECL_CTR +#include "flashcmd.h" // flashcmd_is_in_transfer #include "sched.h" // DECL_INIT #define WAIT_BLINK_TIME 1000000