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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-05-10 14:51:05 -04:00 committed by Eric Callahan
parent b0291b3cdb
commit 21469fea3e
6 changed files with 98 additions and 81 deletions

View File

@ -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

View File

@ -4,11 +4,11 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // memmove
#include <string.h> // 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;

View File

@ -1,9 +1,6 @@
#ifndef __CANBOOT_MAIN_H
#define __CANBOOT_MAIN_H
#include <stdint.h> // uint32_t
int flashcmd_is_in_transfer(void);
void canboot_main(void);
#endif // canboot_main.h

84
src/flashcmd.c Normal file
View File

@ -0,0 +1,84 @@
// Command handlers for flash requests
//
// Copyright (C) 2021 Eric Callahan <arksine.code@gmail.com>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // 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));
}

6
src/flashcmd.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __FLASHCMD_H
#define __FLASHCMD_H
int flashcmd_is_in_transfer(void);
#endif // flashcmd.h

View File

@ -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