Skip to content

Commit 5747fa8

Browse files
committed
Code review
1 parent 0cadbe2 commit 5747fa8

File tree

8 files changed

+67
-59
lines changed

8 files changed

+67
-59
lines changed

hardware/esp8266com/esp8266/bootloaders/eboot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TARGET_DIR := ./
77
TARGET_OBJ_FILES := \
88
eboot.o \
99
eboot_command.o \
10-
flash.o \
10+
1111

1212
TARGET_OBJ_PATHS := $(addprefix $(TARGET_DIR)/,$(TARGET_OBJ_FILES))
1313

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void main()
125125
if (cmd.action == ACTION_COPY_RAW) {
126126
ets_putc('c'); ets_putc('p'); ets_putc(':');
127127
res = copy_raw(cmd.args[0], cmd.args[1], cmd.args[2]);
128-
ets_putc((char)0x30+res); ets_putc('\n');
128+
ets_putc('0'+res); ets_putc('\n');
129129
if (res == 0) {
130130
cmd.action = ACTION_LOAD_APP;
131131
cmd.args[0] = cmd.args[1];
@@ -136,7 +136,7 @@ void main()
136136
ets_putc('l'); ets_putc('d'); ets_putc('\n');
137137
res = load_app_from_flash_raw(cmd.args[0]);
138138
//we will get to this only on load fail
139-
ets_putc('e'); ets_putc(':'); ets_putc((char)0x30+res); ets_putc('\n');
139+
ets_putc('e'); ets_putc(':'); ets_putc('0'+res); ets_putc('\n');
140140
}
141141

142142
if (res) {
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/Esp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ uint32_t EspClass::getFreeSketchSpace() {
350350
uint32_t usedSize = getSketchSize();
351351
// round one sector up
352352
uint32_t freeSpaceStart = (usedSize + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
353-
uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000 - (5 * FLASH_SECTOR_SIZE);
353+
uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000;
354354

355355
#ifdef DEBUG_SERIAL
356356
DEBUG_SERIAL.printf("usedSize=%u freeSpaceStart=%u freeSpaceEnd=%u\r\n", usedSize, freeSpaceStart, freeSpaceEnd);

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

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
#include "Updater.h"
22
#include "Arduino.h"
33
#include "eboot_command.h"
4-
extern "C"{
5-
#include "mem.h"
6-
}
4+
75
//#define DEBUG_UPDATER Serial
86

97
extern "C" uint32_t _SPIFFS_start;
108

11-
UpdaterClass::UpdaterClass() : _error(0), _buffer(0), _bufferLen(0), _size(0), _startAddress(0), _currentAddress(0) {}
9+
UpdaterClass::UpdaterClass()
10+
: _error(0)
11+
, _buffer(0)
12+
, _bufferLen(0)
13+
, _size(0)
14+
, _startAddress(0)
15+
, _currentAddress(0)
16+
{
17+
}
18+
19+
void UpdaterClass::_reset() {
20+
if (_buffer)
21+
delete[] _buffer;
22+
_buffer = 0;
23+
_bufferLen = 0;
24+
_startAddress = 0;
25+
_currentAddress = 0;
26+
_size = 0;
27+
}
1228

1329
bool UpdaterClass::begin(size_t size){
1430
if(_size > 0){
@@ -26,17 +42,13 @@ bool UpdaterClass::begin(size_t size){
2642
return false;
2743
}
2844

29-
if(_buffer) os_free(_buffer);
30-
_bufferLen = 0;
31-
_startAddress = 0;
32-
_currentAddress = 0;
33-
_size = 0;
45+
_reset();
3446
_error = 0;
3547

3648
//size of current sketch rounded to a sector
3749
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);
50+
//address of the end of the space available for sketch and update
51+
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
4052
//size of the update rounded to a sector
4153
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
4254
//address where we will start writing the update
@@ -55,7 +67,7 @@ bool UpdaterClass::begin(size_t size){
5567
_startAddress = updateStartAddress;
5668
_currentAddress = _startAddress;
5769
_size = size;
58-
_buffer = (uint8_t*)os_malloc(FLASH_SECTOR_SIZE);
70+
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
5971

6072
return true;
6173
}
@@ -72,11 +84,8 @@ bool UpdaterClass::end(bool evenIfRemaining){
7284
#ifdef DEBUG_UPDATER
7385
DEBUG_UPDATER.printf("premature end: res:%u, pos:%u/%u\n", getError(), progress(), _size);
7486
#endif
75-
if(_buffer) os_free(_buffer);
76-
_bufferLen = 0;
77-
_currentAddress = 0;
78-
_startAddress = 0;
79-
_size = 0;
87+
88+
_reset();
8089
return false;
8190
}
8291

@@ -86,9 +95,6 @@ bool UpdaterClass::end(bool evenIfRemaining){
8695
}
8796
_size = progress();
8897
}
89-
if(_buffer) os_free(_buffer);
90-
_bufferLen = 0;
91-
_currentAddress = 0;
9298

9399
eboot_command ebcmd;
94100
ebcmd.action = ACTION_COPY_RAW;
@@ -100,19 +106,19 @@ bool UpdaterClass::end(bool evenIfRemaining){
100106
#ifdef DEBUG_UPDATER
101107
DEBUG_UPDATER.printf("Staged: address:0x%08X, size:0x%08X\n", _startAddress, _size);
102108
#endif
103-
104-
_startAddress = 0;
105-
_size = 0;
106-
_error = UPDATE_ERROR_OK;
109+
110+
_reset();
107111
return true;
108112
}
109113

110114
bool UpdaterClass::_writeBuffer(){
111115
noInterrupts();
112116
int rc = SPIEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
113-
if(!rc) rc = SPIWrite(_currentAddress, _buffer, _bufferLen);
117+
if (!rc) {
118+
rc = SPIWrite(_currentAddress, _buffer, _bufferLen);
119+
}
114120
interrupts();
115-
if (rc){
121+
if (rc) {
116122
_error = UPDATE_ERROR_WRITE;
117123
_currentAddress = (_startAddress + _size);
118124
#ifdef DEBUG_UPDATER
@@ -125,15 +131,15 @@ bool UpdaterClass::_writeBuffer(){
125131
return true;
126132
}
127133

128-
size_t UpdaterClass::write(uint8_t *data, size_t len){
134+
size_t UpdaterClass::write(uint8_t *data, size_t len) {
129135
size_t left = len;
130-
if(hasError()||!isRunning())
136+
if(hasError() || !isRunning())
131137
return 0;
132138

133139
if(len > remaining())
134140
len = remaining();
135141

136-
while((_bufferLen + left) > FLASH_SECTOR_SIZE){
142+
while((_bufferLen + left) > FLASH_SECTOR_SIZE) {
137143
size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen;
138144
memcpy(_buffer + _bufferLen, data + (len - left), toBuff);
139145
_bufferLen += toBuff;
@@ -155,13 +161,13 @@ size_t UpdaterClass::write(uint8_t *data, size_t len){
155161
return len;
156162
}
157163

158-
size_t UpdaterClass::writeStream(Stream &data){
164+
size_t UpdaterClass::writeStream(Stream &data) {
159165
size_t written = 0;
160166
size_t toRead = 0;
161-
if(hasError()||!isRunning())
167+
if(hasError() || !isRunning())
162168
return 0;
163169

164-
while(remaining()){
170+
while(remaining()) {
165171
toRead = FLASH_SECTOR_SIZE - _bufferLen;
166172
toRead = data.readBytes(_buffer + _bufferLen, toRead);
167173
if(toRead == 0){ //Timeout

hardware/esp8266com/esp8266/cores/esp8266/Updater.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UpdaterClass {
2727
size_t write(uint8_t *data, size_t len);
2828

2929
/*
30-
Writes the remaining bytes from the Sream to the flash
30+
Writes the remaining bytes from the Stream to the flash
3131
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
3232
Returns the bytes written
3333
Should be equal to the remaining bytes when called
@@ -53,32 +53,33 @@ class UpdaterClass {
5353
void printError(Stream &out);
5454

5555
//Helpers
56-
inline uint8_t getError(){ return _error; }
57-
inline void clearError(){ _error = UPDATE_ERROR_OK; }
58-
inline bool hasError(){ return _error != UPDATE_ERROR_OK; }
59-
inline bool isRunning(){ return _size > 0; }
60-
inline bool isFinished(){ return _currentAddress == (_startAddress + _size); }
61-
inline size_t size(){ return _size; }
62-
inline size_t progress(){ return _currentAddress - _startAddress; }
63-
inline size_t remaining(){ return _size - (_currentAddress - _startAddress); }
56+
uint8_t getError(){ return _error; }
57+
void clearError(){ _error = UPDATE_ERROR_OK; }
58+
bool hasError(){ return _error != UPDATE_ERROR_OK; }
59+
bool isRunning(){ return _size > 0; }
60+
bool isFinished(){ return _currentAddress == (_startAddress + _size); }
61+
size_t size(){ return _size; }
62+
size_t progress(){ return _currentAddress - _startAddress; }
63+
size_t remaining(){ return _size - (_currentAddress - _startAddress); }
6464

6565
/*
6666
Template to write from objects that expose
6767
available() and read(uint8_t*, size_t) methods
68-
faster than the readStream method
68+
faster than the writeStream method
6969
writes only what is available
7070
*/
7171
template<typename T>
7272
size_t write(T &data){
7373
size_t written = 0;
74-
if(hasError()||!isRunning())
74+
if (hasError() || !isRunning())
7575
return 0;
76+
7677
size_t available = data.available();
77-
while(available){
78-
if((_bufferLen + available) > remaining()){
79-
available = (remaining() - _bufferLen);
78+
while(available) {
79+
if(_bufferLen + available > remaining()){
80+
available = remaining() - _bufferLen;
8081
}
81-
if((_bufferLen + available) > FLASH_SECTOR_SIZE){
82+
if(_bufferLen + available > FLASH_SECTOR_SIZE) {
8283
size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen;
8384
data.read(_buffer + _bufferLen, toBuff);
8485
_bufferLen += toBuff;
@@ -89,8 +90,8 @@ class UpdaterClass {
8990
data.read(_buffer + _bufferLen, available);
9091
_bufferLen += available;
9192
written += available;
92-
if(_bufferLen == remaining()){
93-
if(!_writeBuffer()){
93+
if(_bufferLen == remaining()) {
94+
if(!_writeBuffer()) {
9495
return written;
9596
}
9697
}
@@ -104,14 +105,15 @@ class UpdaterClass {
104105
}
105106

106107
private:
108+
void _reset();
109+
bool _writeBuffer();
110+
107111
uint8_t *_buffer;
108-
uint8_t _error;
109112
size_t _bufferLen;
110113
size_t _size;
111114
uint32_t _startAddress;
112115
uint32_t _currentAddress;
113-
114-
bool _writeBuffer();
116+
uint8_t _error;
115117
};
116118

117119
extern UpdaterClass Update;

0 commit comments

Comments
 (0)