Skip to content

Commit 73740d6

Browse files
committed
Clean up eboot
1 parent a4126b1 commit 73740d6

File tree

7 files changed

+88
-52
lines changed

7 files changed

+88
-52
lines changed

bootloaders/eboot/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TARGET_DIR := ./
66
TARGET_OBJ_FILES := \
77
eboot.o \
88
eboot_command.o \
9+
flash.o \
910

1011
TARGET_OBJ_PATHS := $(addprefix $(TARGET_DIR)/,$(TARGET_OBJ_FILES))
1112

bootloaders/eboot/eboot.c

+16-41
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#include <stddef.h>
1010
#include <stdint.h>
1111
#include <stdbool.h>
12-
#include "eboot.h"
12+
#include "flash.h"
1313
#include "eboot_command.h"
14-
extern void* flashchip;
1514

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

@@ -73,53 +72,21 @@ int load_app_from_flash_raw(const uint32_t flash_addr)
7372

7473

7574

76-
int erase(const uint32_t start, const uint32_t size)
77-
{
78-
if (start & (FLASH_SECTOR_SIZE - 1) != 0) {
79-
return 1;
80-
}
81-
82-
const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
83-
uint32_t current_sector = start / FLASH_SECTOR_SIZE;
84-
uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE;
85-
const uint32_t end = current_sector + sector_count;
86-
87-
for (; current_sector < end && (current_sector & (sectors_per_block-1));
88-
++current_sector, --sector_count) {
89-
if (SPIEraseSector(current_sector)) {
90-
return 2;
91-
}
92-
}
93-
94-
for (;current_sector + sectors_per_block <= end;
95-
current_sector += sectors_per_block,
96-
sector_count -= sectors_per_block) {
97-
if (SPIEraseBlock(current_sector / sectors_per_block)) {
98-
return 3;
99-
}
100-
}
101-
102-
for (; current_sector < end;
103-
++current_sector, --sector_count) {
104-
if (SPIEraseSector(current_sector)) {
105-
return 4;
106-
}
107-
}
108-
109-
return 0;
110-
}
111-
11275
int copy_raw(const uint32_t src_addr,
11376
const uint32_t dst_addr,
11477
const uint32_t size)
11578
{
79+
ets_putc('\n');
80+
ets_putc('c');
81+
ets_putc('p');
82+
ets_putc('\n');
11683
// require regions to be aligned
11784
if (src_addr & 0xfff != 0 ||
11885
dst_addr & 0xfff != 0) {
11986
return 1;
12087
}
12188

122-
if (erase(dst_addr, size)) {
89+
if (SPIEraseAreaEx(dst_addr, size)) {
12390
return 2;
12491
}
12592

@@ -153,17 +120,25 @@ void main()
153120
int res = 9;
154121
struct eboot_command cmd;
155122

156-
eboot_command_read(&cmd);
123+
if (eboot_command_read(&cmd)) {
124+
cmd.action = ACTION_LOAD_APP;
125+
cmd.args[0] = 0;
126+
ets_putc('e');
127+
} else {
128+
ets_putc('@');
129+
}
130+
eboot_command_clear();
157131

158132
if (cmd.action == ACTION_COPY_RAW) {
159133
res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2]);
160134
if (res == 0) {
161135
cmd.action = ACTION_LOAD_APP;
136+
cmd.args[0] = cmd.args[1];
162137
}
163138
}
164139

165140
if (cmd.action == ACTION_LOAD_APP) {
166-
res = load_app_from_flash_raw(0);
141+
res = load_app_from_flash_raw(cmd.args[0]);
167142
}
168143

169144
if (res) {

bootloaders/eboot/eboot.elf

1.38 KB
Binary file not shown.

bootloaders/eboot/eboot_command.c

+22-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ uint32_t eboot_command_calculate_crc32(const struct eboot_command* cmd)
2828
offsetof(struct eboot_command, crc32));
2929
}
3030

31-
void eboot_command_read(struct eboot_command* cmd)
31+
int eboot_command_read(struct eboot_command* cmd)
3232
{
3333
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
3434
uint32_t* dst = (uint32_t *) cmd;
@@ -39,9 +39,27 @@ void eboot_command_read(struct eboot_command* cmd)
3939
uint32_t crc32 = eboot_command_calculate_crc32(cmd);
4040
if (cmd->magic & EBOOT_MAGIC_MASK != EBOOT_MAGIC ||
4141
cmd->crc32 != crc32) {
42-
43-
cmd->action = ACTION_LOAD_APP;
44-
cmd->args[0] = 0;
42+
return 1;
4543
}
44+
45+
return 0;
46+
}
47+
48+
void eboot_command_write(struct eboot_command* cmd)
49+
{
50+
cmd->magic = EBOOT_MAGIC;
51+
cmd->crc32 = eboot_command_calculate_crc32(cmd);
52+
53+
const uint32_t dw_count = sizeof(struct eboot_command) / sizeof(uint32_t);
54+
const uint32_t* src = (const uint32_t *) cmd;
55+
for (uint32_t i = 0; i < dw_count; ++i) {
56+
RTC_MEM[i] = src[i];
57+
}
58+
}
59+
60+
void eboot_command_clear()
61+
{
62+
RTC_MEM[offsetof(struct eboot_command, magic) / sizeof(uint32_t)] = 0;
63+
RTC_MEM[offsetof(struct eboot_command, crc32) / sizeof(uint32_t)] = 0;
4664
}
4765

bootloaders/eboot/eboot_command.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ struct eboot_command {
2323
};
2424

2525

26-
void eboot_command_read(struct eboot_command* cmd);
27-
26+
int eboot_command_read(struct eboot_command* cmd);
27+
void eboot_command_write(struct eboot_command* cmd);
28+
void eboot_command_clear();
2829

2930
#endif //EBOOT_COMMAND_H

bootloaders/eboot/flash.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
#include <stdbool.h>
4+
#include "flash.h"
5+
6+
7+
int SPIEraseAreaEx(const uint32_t start, const uint32_t size)
8+
{
9+
if (start & (FLASH_SECTOR_SIZE - 1) != 0) {
10+
return 1;
11+
}
12+
13+
const uint32_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
14+
uint32_t current_sector = start / FLASH_SECTOR_SIZE;
15+
uint32_t sector_count = (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE;
16+
const uint32_t end = current_sector + sector_count;
17+
18+
for (; current_sector < end && (current_sector & (sectors_per_block-1));
19+
++current_sector, --sector_count) {
20+
if (SPIEraseSector(current_sector)) {
21+
return 2;
22+
}
23+
}
24+
25+
for (;current_sector + sectors_per_block <= end;
26+
current_sector += sectors_per_block,
27+
sector_count -= sectors_per_block) {
28+
if (SPIEraseBlock(current_sector / sectors_per_block)) {
29+
return 3;
30+
}
31+
}
32+
33+
for (; current_sector < end;
34+
++current_sector, --sector_count) {
35+
if (SPIEraseSector(current_sector)) {
36+
return 4;
37+
}
38+
}
39+
40+
return 0;
41+
}
42+

bootloaders/eboot/eboot.h renamed to bootloaders/eboot/flash.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
* 3-clause BSD license to be found in the LICENSE file.
66
*/
77

8-
#ifndef EBOOT_H
9-
#define EBOOT_H
10-
8+
#ifndef FLASH_H
9+
#define FLASH_H
1110

1211
int SPIEraseBlock(uint32_t block);
1312
int SPIEraseSector(uint32_t sector);
1413
int SPIRead(uint32_t addr, void *dest, size_t size);
1514
int SPIWrite(uint32_t addr, void *src, size_t size);
16-
15+
int SPIEraseAreaEx(const uint32_t start, const uint32_t size);
1716

1817
#define FLASH_SECTOR_SIZE 0x1000
1918
#define FLASH_BLOCK_SIZE 0x10000
@@ -41,4 +40,4 @@ typedef struct {
4140

4241

4342

44-
#endif //EBOOT_H
43+
#endif //FLASH_H

0 commit comments

Comments
 (0)