Skip to content

Commit fcfb03e

Browse files
John Doeigrr
authored andcommitted
make eboot erase/read/write sector by sector
that makes possible having sketches with size up to the free size
1 parent 4b7f7c1 commit fcfb03e

File tree

7 files changed

+49
-52
lines changed

7 files changed

+49
-52
lines changed

hardware/esp8266com/esp8266/bootloaders/eboot/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
XTENSA_TOOLCHAIN ?=
1+
XTENSA_TOOLCHAIN ?= ../../tools/xtensa-lx106-elf/bin/
2+
ESPTOOL ?= ../../tools/esptool
23

34
BIN_DIR := ./
45
TARGET_DIR := ./

hardware/esp8266com/esp8266/bootloaders/eboot/eboot.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,38 +76,31 @@ int copy_raw(const uint32_t src_addr,
7676
const uint32_t dst_addr,
7777
const uint32_t size)
7878
{
79-
ets_putc('\n');
80-
ets_putc('c');
81-
ets_putc('p');
82-
ets_putc('\n');
8379
// require regions to be aligned
8480
if (src_addr & 0xfff != 0 ||
8581
dst_addr & 0xfff != 0) {
8682
return 1;
8783
}
8884

89-
if (SPIEraseAreaEx(dst_addr, size)) {
90-
return 2;
91-
}
92-
93-
const uint32_t buffer_size = 4096;
85+
const uint32_t buffer_size = FLASH_SECTOR_SIZE;
9486
uint8_t buffer[buffer_size];
95-
96-
const uint32_t end = src_addr + size;
87+
uint32_t left = ((size+buffer_size-1) & ~(buffer_size-1));
9788
uint32_t saddr = src_addr;
9889
uint32_t daddr = dst_addr;
99-
uint32_t left = size;
100-
while (saddr < end) {
101-
uint32_t will_copy = (left < buffer_size) ? left : buffer_size;
102-
if (SPIRead(saddr, buffer, will_copy)) {
103-
return 3;
104-
}
105-
if (SPIWrite(daddr, buffer, will_copy)) {
106-
return 4;
107-
}
108-
saddr += will_copy;
109-
daddr += will_copy;
110-
left -= will_copy;
90+
91+
while (left) {
92+
if (SPIEraseSector(daddr/buffer_size)) {
93+
return 2;
94+
}
95+
if (SPIRead(saddr, buffer, buffer_size)) {
96+
return 3;
97+
}
98+
if (SPIWrite(daddr, buffer, buffer_size)) {
99+
return 4;
100+
}
101+
saddr += buffer_size;
102+
daddr += buffer_size;
103+
left -= buffer_size;
111104
}
112105

113106
return 0;
@@ -123,30 +116,31 @@ void main()
123116
if (eboot_command_read(&cmd)) {
124117
cmd.action = ACTION_LOAD_APP;
125118
cmd.args[0] = 0;
126-
ets_putc('e');
119+
ets_putc('~');
127120
} else {
128121
ets_putc('@');
129122
}
130123
eboot_command_clear();
131-
124+
132125
if (cmd.action == ACTION_COPY_RAW) {
126+
ets_putc('c'); ets_putc('p'); ets_putc(':');
133127
res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2]);
128+
ets_putc((char)0x30+res); ets_putc('\n');
134129
if (res == 0) {
135130
cmd.action = ACTION_LOAD_APP;
136131
cmd.args[0] = cmd.args[1];
137132
}
138133
}
139134

140135
if (cmd.action == ACTION_LOAD_APP) {
141-
res = load_app_from_flash_raw(cmd.args[0]);
136+
ets_putc('l'); ets_putc('d'); ets_putc('\n');
137+
res = load_app_from_flash_raw(cmd.args[0]);
138+
//we will get to this only on load fail
139+
ets_putc('e'); ets_putc(':'); ets_putc((char)0x30+res); ets_putc('\n');
142140
}
143141

144142
if (res) {
145-
ets_putc('\n');
146-
ets_putc('#');
147-
ets_putc('0' + res);
148-
ets_putc('\n');
149-
SWRST;
143+
SWRST;
150144
}
151145

152146
while(true){}
Binary file not shown.

hardware/esp8266com/esp8266/bootloaders/eboot/flash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Redistribution and use is permitted according to the conditions of the
55
* 3-clause BSD license to be found in the LICENSE file.
66
*/
7-
7+
/*
88
#include <stddef.h>
99
#include <stdint.h>
1010
#include <stdbool.h>
@@ -46,4 +46,4 @@ int SPIEraseAreaEx(const uint32_t start, const uint32_t size)
4646
4747
return 0;
4848
}
49-
49+
*/

hardware/esp8266com/esp8266/bootloaders/eboot/flash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int SPIEraseBlock(uint32_t block);
1212
int SPIEraseSector(uint32_t sector);
1313
int SPIRead(uint32_t addr, void *dest, size_t size);
1414
int SPIWrite(uint32_t addr, void *src, size_t size);
15-
int SPIEraseAreaEx(const uint32_t start, const uint32_t size);
15+
//int SPIEraseAreaEx(const uint32_t start, const uint32_t size);
1616

1717
#define FLASH_SECTOR_SIZE 0x1000
1818
#define FLASH_BLOCK_SIZE 0x10000

hardware/esp8266com/esp8266/cores/esp8266/Updater.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,33 @@ bool UpdaterClass::begin(size_t size){
2727
}
2828

2929
if(_buffer) os_free(_buffer);
30-
3130
_bufferLen = 0;
3231
_startAddress = 0;
3332
_currentAddress = 0;
3433
_size = 0;
3534
_error = 0;
3635

37-
uint32_t usedSize = ESP.getSketchSize();
38-
uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000 - (5 * FLASH_SECTOR_SIZE);
36+
//size of current sketch rounded to a sector
37+
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
38+
//address of the end of the space available for sketch and update (5 sectors are for EEPROM and init data)
39+
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000 - (5 * FLASH_SECTOR_SIZE);
40+
//size of the update rounded to a sector
3941
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
40-
uint32_t freeSpaceStart = freeSpaceEnd - roundedSize;
42+
//address where we will start writing the update
43+
uint32_t updateStartAddress = updateEndAddress - roundedSize;
4144

42-
//new sketch can not be more then half the size or more than the free space
43-
//this means that max sketch size is (1MB - 20KB) / 2 for flash 2MB and above
44-
//and the current sketch should not be more than that either
45-
if(freeSpaceStart < usedSize || roundedSize > (freeSpaceEnd/2)){
45+
//make sure that the size of both sketches is less than the total space (updateEndAddress)
46+
if(updateStartAddress < currentSketchSize){
4647
_error = UPDATE_ERROR_SPACE;
4748
#ifdef DEBUG_UPDATER
4849
printError(DEBUG_UPDATER);
4950
#endif
5051
return false;
5152
}
53+
54+
//erase the neede space
5255
noInterrupts();
53-
int rc = SPIEraseAreaEx(freeSpaceStart, roundedSize);
56+
int rc = SPIEraseAreaEx(updateStartAddress, roundedSize);
5457
interrupts();
5558
if (rc){
5659
_error = UPDATE_ERROR_ERASE;
@@ -59,7 +62,9 @@ bool UpdaterClass::begin(size_t size){
5962
#endif
6063
return false;
6164
}
62-
_startAddress = freeSpaceStart;
65+
66+
//initialize
67+
_startAddress = updateStartAddress;
6368
_currentAddress = _startAddress;
6469
_size = size;
6570
_buffer = (uint8_t*)os_malloc(FLASH_SECTOR_SIZE);

hardware/esp8266com/esp8266/tools/espota.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#!/usr/bin/env python
22
#
33
# this script will push an OTA update to the ESP
4-
#
5-
# use it like: python ota_server.py <ESP_IP_address> <sketch.bin>
6-
#
7-
# on the ESP side you need code like this: https://gist.github.com/igrr/43d5c52328e955bb6b09 to handle the update
8-
#
4+
# use it like: python espota.py <ESP_IP_address> <sketch.bin>
95

106
from __future__ import print_function
117
import socket
@@ -22,7 +18,7 @@ def serve(remoteAddr, remotePort, filename):
2218
sock.bind(server_address)
2319
sock.listen(1)
2420
except:
25-
print('Socket Failed', file=sys.stderr)
21+
print('Listen Failed', file=sys.stderr)
2622
return 1
2723

2824
content_size = os.path.getsize(filename)
@@ -80,10 +76,11 @@ def serve(remoteAddr, remotePort, filename):
8076
f.close()
8177
sock.close()
8278
return 1
83-
79+
8480
finally:
8581
connection.close()
8682
f.close()
83+
8784
sock.close()
8885
return 1
8986

0 commit comments

Comments
 (0)