Skip to content

Commit

Permalink
Clean up eboot
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Jun 5, 2015
1 parent a4126b1 commit 73740d6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 52 deletions.
1 change: 1 addition & 0 deletions bootloaders/eboot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
57 changes: 16 additions & 41 deletions bootloaders/eboot/eboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "eboot.h"
#include "flash.h"
#include "eboot_command.h"
extern void* flashchip;

#define SWRST do { (*((volatile uint32_t*) 0x60000700)) |= 0x80000000; } while(0);

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
Binary file modified bootloaders/eboot/eboot.elf
Binary file not shown.
26 changes: 22 additions & 4 deletions bootloaders/eboot/eboot_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

5 changes: 3 additions & 2 deletions bootloaders/eboot/eboot_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions bootloaders/eboot/flash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#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;
}

9 changes: 4 additions & 5 deletions bootloaders/eboot/eboot.h → bootloaders/eboot/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,4 +40,4 @@ typedef struct {



#endif //EBOOT_H
#endif //FLASH_H

0 comments on commit 73740d6

Please sign in to comment.