diff --git a/bootloaders/eboot/Makefile b/bootloaders/eboot/Makefile index 1364ac0d0..0872ee35f 100644 --- a/bootloaders/eboot/Makefile +++ b/bootloaders/eboot/Makefile @@ -6,6 +6,7 @@ TARGET_DIR := ./ TARGET_OBJ_FILES := \ eboot.o \ eboot_command.o \ + flash.o \ TARGET_OBJ_PATHS := $(addprefix $(TARGET_DIR)/,$(TARGET_OBJ_FILES)) diff --git a/bootloaders/eboot/eboot.c b/bootloaders/eboot/eboot.c index b3193e299..0b74fcdf0 100644 --- a/bootloaders/eboot/eboot.c +++ b/bootloaders/eboot/eboot.c @@ -9,9 +9,8 @@ #include #include #include -#include "eboot.h" +#include "flash.h" #include "eboot_command.h" -extern void* flashchip; #define SWRST do { (*((volatile uint32_t*) 0x60000700)) |= 0x80000000; } while(0); @@ -73,53 +72,21 @@ int load_app_from_flash_raw(const uint32_t flash_addr) -int erase(const uint32_t start, const uint32_t size) -{ - if (start & (FLASH_SECTOR_SIZE - 1) != 0) { - return 1; - } - - const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE; - uint32_t current_sector = start / FLASH_SECTOR_SIZE; - uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE; - const uint32_t end = current_sector + sector_count; - - for (; current_sector < end && (current_sector & (sectors_per_block-1)); - ++current_sector, --sector_count) { - if (SPIEraseSector(current_sector)) { - return 2; - } - } - - for (;current_sector + sectors_per_block <= end; - current_sector += sectors_per_block, - sector_count -= sectors_per_block) { - if (SPIEraseBlock(current_sector / sectors_per_block)) { - return 3; - } - } - - for (; current_sector < end; - ++current_sector, --sector_count) { - if (SPIEraseSector(current_sector)) { - return 4; - } - } - - return 0; -} - int copy_raw(const uint32_t src_addr, const uint32_t dst_addr, const uint32_t size) { + ets_putc('\n'); + ets_putc('c'); + ets_putc('p'); + ets_putc('\n'); // require regions to be aligned if (src_addr & 0xfff != 0 || dst_addr & 0xfff != 0) { return 1; } - if (erase(dst_addr, size)) { + if (SPIEraseAreaEx(dst_addr, size)) { return 2; } @@ -153,17 +120,25 @@ void main() int res = 9; struct eboot_command cmd; - eboot_command_read(&cmd); + if (eboot_command_read(&cmd)) { + cmd.action = ACTION_LOAD_APP; + cmd.args[0] = 0; + ets_putc('e'); + } else { + ets_putc('@'); + } + eboot_command_clear(); if (cmd.action == ACTION_COPY_RAW) { res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2]); if (res == 0) { cmd.action = ACTION_LOAD_APP; + cmd.args[0] = cmd.args[1]; } } if (cmd.action == ACTION_LOAD_APP) { - res = load_app_from_flash_raw(0); + res = load_app_from_flash_raw(cmd.args[0]); } if (res) { diff --git a/bootloaders/eboot/eboot.elf b/bootloaders/eboot/eboot.elf index 0637a0525..97e25c146 100755 Binary files a/bootloaders/eboot/eboot.elf and b/bootloaders/eboot/eboot.elf differ diff --git a/bootloaders/eboot/eboot_command.c b/bootloaders/eboot/eboot_command.c index 1e9cbed2c..648039e48 100644 --- a/bootloaders/eboot/eboot_command.c +++ b/bootloaders/eboot/eboot_command.c @@ -28,7 +28,7 @@ uint32_t eboot_command_calculate_crc32(const struct eboot_command* cmd) offsetof(struct eboot_command, crc32)); } -void eboot_command_read(struct eboot_command* cmd) +int eboot_command_read(struct eboot_command* cmd) { const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t); uint32_t* dst = (uint32_t *) cmd; @@ -39,9 +39,27 @@ void eboot_command_read(struct eboot_command* cmd) uint32_t crc32 = eboot_command_calculate_crc32(cmd); if (cmd->magic & EBOOT_MAGIC_MASK != EBOOT_MAGIC || cmd->crc32 != crc32) { - - cmd->action = ACTION_LOAD_APP; - cmd->args[0] = 0; + return 1; + } + + return 0; +} + +void eboot_command_write(struct eboot_command* cmd) +{ + cmd->magic = EBOOT_MAGIC; + cmd->crc32 = eboot_command_calculate_crc32(cmd); + + const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t); + const uint32_t* src = (const uint32_t *) cmd; + for (uint32_t i = 0; i < dw_count; ++i) { + RTC_MEM[i] = src[i]; } } +void eboot_command_clear() +{ + RTC_MEM[offsetof(struct eboot_command, magic) / sizeof(uint32_t)] = 0; + RTC_MEM[offsetof(struct eboot_command, crc32) / sizeof(uint32_t)] = 0; +} + diff --git a/bootloaders/eboot/eboot_command.h b/bootloaders/eboot/eboot_command.h index aa0fc11bb..cf40c135d 100644 --- a/bootloaders/eboot/eboot_command.h +++ b/bootloaders/eboot/eboot_command.h @@ -23,7 +23,8 @@ struct eboot_command { }; -void eboot_command_read(struct eboot_command* cmd); - +int eboot_command_read(struct eboot_command* cmd); +void eboot_command_write(struct eboot_command* cmd); +void eboot_command_clear(); #endif //EBOOT_COMMAND_H diff --git a/bootloaders/eboot/flash.c b/bootloaders/eboot/flash.c new file mode 100644 index 000000000..6f95a472d --- /dev/null +++ b/bootloaders/eboot/flash.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "flash.h" + + +int SPIEraseAreaEx(const uint32_t start, const uint32_t size) +{ + if (start & (FLASH_SECTOR_SIZE - 1) != 0) { + return 1; + } + + const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE; + uint32_t current_sector = start / FLASH_SECTOR_SIZE; + uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE; + const uint32_t end = current_sector + sector_count; + + for (; current_sector < end && (current_sector & (sectors_per_block-1)); + ++current_sector, --sector_count) { + if (SPIEraseSector(current_sector)) { + return 2; + } + } + + for (;current_sector + sectors_per_block <= end; + current_sector += sectors_per_block, + sector_count -= sectors_per_block) { + if (SPIEraseBlock(current_sector / sectors_per_block)) { + return 3; + } + } + + for (; current_sector < end; + ++current_sector, --sector_count) { + if (SPIEraseSector(current_sector)) { + return 4; + } + } + + return 0; +} + diff --git a/bootloaders/eboot/eboot.h b/bootloaders/eboot/flash.h similarity index 90% rename from bootloaders/eboot/eboot.h rename to bootloaders/eboot/flash.h index 973c616a8..ea8b65c1f 100644 --- a/bootloaders/eboot/eboot.h +++ b/bootloaders/eboot/flash.h @@ -5,15 +5,14 @@ * 3-clause BSD license to be found in the LICENSE file. */ -#ifndef EBOOT_H -#define EBOOT_H - +#ifndef FLASH_H +#define FLASH_H int SPIEraseBlock(uint32_t block); int SPIEraseSector(uint32_t sector); int SPIRead(uint32_t addr, void *dest, size_t size); int SPIWrite(uint32_t addr, void *src, size_t size); - +int SPIEraseAreaEx(const uint32_t start, const uint32_t size); #define FLASH_SECTOR_SIZE 0x1000 #define FLASH_BLOCK_SIZE 0x10000 @@ -41,4 +40,4 @@ typedef struct { -#endif //EBOOT_H +#endif //FLASH_H