Skip to content

Commit

Permalink
migration files for Shelly_Plus1Mini
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger committed Sep 17, 2023
1 parent d327c04 commit fbba7c3
Show file tree
Hide file tree
Showing 6 changed files with 768 additions and 45 deletions.
File renamed without changes.
63 changes: 52 additions & 11 deletions raw/esp32c3/Shelly_Plus1Mini/bootloader.be
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Flash bootloader from filesystem
# Flash bootloader from URL or filesystem
#

class bootloader
Expand All @@ -21,22 +21,52 @@ class bootloader
return nil
end

def flash()
#
# download from URL and store to `bootloader.bin`
#
def download(url)
# address to flash the bootloader
var addr = self.get_bootloader_address()
if addr == nil raise "internal_error", "can't find address for bootloader" end

var cl = webclient()
cl.begin(url)
var r = cl.GET()
if r != 200 raise "network_error", "GET returned "+str(r) end
var bl_size = cl.get_size()
if bl_size <= 8291 raise "internal_error", "wrong bootloader size "+str(bl_size) end
if bl_size > (0x8000 - addr) raise "internal_error", "bootloader is too large "+str(bl_size / 1024)+"kB" end

cl.write_file("bootloader.bin")
cl.close()
end

# returns true if ok
def flash(url)
var fname = "bootloader.bin" # default local name
if url != nil
if url[0..3] == "http" # if starts with 'http' download
self.download(url)
else
fname = url # else get from file system
end
end
# address to flash the bootloader
var addr = self.get_bootloader_address()
if addr == nil tasmota.log("OTA: can't find address for bootloader", 2) return false end

var bl = open("bootloader.bin", "r")
var bl = open(fname, "r")
if bl.readbytes(size(self._sign)) != self._sign
raise "value_error", "the file does not contain a bootloader signature"
tasmota.log("OTA: file does not contain a bootloader signature", 2)
return false
end
bl.seek(0) # reset to start of file

var bl_size = bl.size()
if bl_size <= 8291 raise "internal_error", "wrong bootloader size "+str(bl_size) end
if bl_size > (0x8000 - addr) raise "internal_error", "bootloader is too large "+str(bl_size / 1024)+"kB" end
if bl_size <= 8291 tasmota.log("OTA: wrong bootloader size "+str(bl_size), 2) return false end
if bl_size > (0x8000 - addr) tasmota.log("OTA: bootloader is too large "+str(bl_size / 1024)+"kB", 2) return false end

print("Flashing bootloader")
tasmota.log("OTA: Flashing bootloader", 2)
# from now on there is no turning back, any failure means a bricked device
import flash
# read current value for bytes 2/3
Expand All @@ -53,15 +83,26 @@ class bootloader
buf = bl.readbytes(0x1000) # read next chunk
end
bl.close()
print("Booloader flashed, please restart")

tasmota.log("OTA: Booloader flashed, please restart", 2)
return true
end
end

return bootloader

#-
### FLASH
import bootloader
bootloader().flash()
-#
bootloader().flash('https://raw.githubusercontent.com/espressif/arduino-esp32/master/tools/sdk/esp32/bin/bootloader_dio_40m.bin')
#bootloader().flash('https://raw.githubusercontent.com/espressif/arduino-esp32/master/tools/sdk/esp32/bin/bootloader_dout_40m.bin')
### FLASH from local file
bootloader().flash("bootloader-tasmota-c3.bin")
#### debug only
bl = bootloader()
print(format("0x%04X", bl.get_bootloader_address()))
-#
33 changes: 0 additions & 33 deletions raw/esp32c3/Shelly_Plus1Mini/cp_bootloader.be

This file was deleted.

2 changes: 1 addition & 1 deletion raw/esp32c3/Shelly_Plus1Mini/init.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Br load("Shelly_Plus1Mini.autoconf#cp_bootloader.be") load("Shelly_Plus1Mini.autoconf#bootloader.be") global.bootloader().flash()
Br load("Shelly_Plus1Mini.autoconf#migrate_shelly.be")
Template {"NAME":"Shelly Plus1Mini","GPIO":[320,0,0,0,0,0,0,224,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1,"CMND":"AdcParam1 2,5600,4700,3350"}
Module 0
70 changes: 70 additions & 0 deletions raw/esp32c3/Shelly_Plus1Mini/migrate_shelly.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# migration script for Shelly

# simple function to copy from autoconfig archive to filesystem
# return true if ok
def cp(from, to)
import path
if to == nil to = from end # to is optional
if !path.exists(to)
try
# tasmota.log("f_in="+tasmota.wd + from)
var f_in = open(tasmota.wd + from)
var f_content = f_in.readbytes()
f_in.close()
var f_out = open(to, "w")
f_out.write(f_content)
f_out.close()
except .. as e,m
tasmota.log("OTA: Couldn't copy "+to+" "+e+" "+m,2)
return false
end
return true
end
return true
end

# make some room if there are some leftovers from shelly
import path
path.remove("index.html.gz")

# copy some files from autoconf to filesystem
var ok
ok = cp("bootloader-tasmota-c3.bin")
ok = cp("Partition_Wizard.tapp")

# use an alternative to partition_core that can read Shelly's otadata
tasmota.log("OTA: loading "+tasmota.wd + "partition_core_shelly.be", 2)
load(tasmota.wd + "partition_core_shelly.be")

# load bootloader flasher
tasmota.log("OTA: loading "+tasmota.wd + "bootloader.be", 2)
load(tasmota.wd + "bootloader.be")


# all good
if ok
# do some basic check that the bootloader is not already in place
import flash
if flash.read(0x1000, 4) == bytes('CD3F6395')
tasmota.log("OTA: bootloader already in place, not flashing it")
else
ok = global.bootloader().flash("bootloader-tasmota-c3.bin")
end
if ok
var p = global.partition_core_shelly.Partition()
p.save() # save with otadata compatible with new bootloader
tasmota.log("OTA: Shelly migration successful", 2)
end
end

# dump logs to file
var lr = tasmota_log_reader()
var f_logs = open("migration_logs.txt", "w")
var logs = lr.get_log(2)
while logs != nil
f_logs.write(logs)
logs = lr.get_log(2)
end
f_logs.close()

# Done
Loading

0 comments on commit fbba7c3

Please sign in to comment.