ESP8266 building OTA firmware for 2MB boards

Page content

During the past weeks i’ve worked on getting the FOTA upgrades work on the 2MB boards by Olimex.

The wonderful esp-link project by Thorsten von Eicken was a great example of  two things:

  1. How to concatenate the espfs filesystem image with the firmware images.
  2. How to properly write a new image to the flash.

It was a nice example to start with.

So after a lot of fiddling with  Makefiles, cgi routines and esptool  – i’ve finally got the OTA working.

Gotchas:

  • esptool.py modifies the images when writting – you have to pass the correct options for the flash split you want to use. Why on earth it modifies a already correctly build images with app_genbin.py, when not requested too is subject to be discussed in another post.
  • When using 512 KB or 1024 KB flash the two firmware images are both mapped at 0x40200000 so when using user2.bin you need correct offset + 0x8000 for the 512KB case.
  • When using 1024KB images you do not need offset – second MB of the flash is mapped at 0x40200000 too. So no need to have offset and essentially that makes the user1.bin same as user2.bin /not tested yet/. But this means doing less builds and if you plan to distribute upgrades less content.
ifeq ("$(ESP_FLASH_SIZE)","512")
build/eagle.esphttpd.v6.ld: $(SDK_LDDIR)/eagle.app.v6.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
            $(SDK_LDDIR)/eagle.app.v6.ld >$@
build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  irom0_0_seg/ s/2B000/38000/' \
            $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld >$@
build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  irom0_0_seg/ s/2B000/38000/' \
            $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld >$@

endif
ifeq ("$(ESP_FLASH_SIZE)","1024")
build/eagle.esphttpd.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld >$@
build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  irom0_0_seg/ s/6B000/7B000/' \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld >$@
build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  irom0_0_seg/ s/6B000/7B000/' \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld >$@

endif
ifeq ("$(ESP_FLASH_SIZE)","2048")
build/eagle.esphttpd.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld >$@
build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  iram1_0_seg/ s/8000/10000/' \
                        -e '/^  irom0_0_seg/ s/6B000/E0000/' \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld >$@
build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld
        $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}'  \
                        -e '/^  iram1_0_seg/ s/8000/10000/' \
                        -e '/^  irom0_0_seg/ s/6B000/E0000/' \
                        -e '/^  irom0_0_seg/ s/40281010/40201010/' \
            $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld >$@

endif

ifeq ("$(ESP_FLASH_SIZE)","512")
flash: all
$(Q) $(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash \
0x00000 "$(SDK_BASE)/bin/boot_v1.4(b1).bin" 0x01000 $(FW_BASE)/user1.bin \
0x7E000 $(SDK_BASE)/bin/blank.bin
endif

ifeq ("$(ESP_FLASH_SIZE)","1024")
flash: all
$(Q) $(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash \
0x00000 "$(SDK_BASE)/bin/boot_v1.4(b1).bin" 0x01000 $(FW_BASE)/user1.bin \
0xfc000 $(SDK_BASE)/bin/blank.bin
endif

ifeq ("$(ESP_FLASH_SIZE)","2048")
flash: all
$(Q) $(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash \
0x00000 "$(SDK_BASE)/bin/boot_v1.4(b1).bin" 0x01000 $(FW_BASE)/user1.bin
# 0x1fe000 blank.bin
# 0x1FC000 esp_init_data_default.bin

endif

I’ve added  ESP_FLASH_SIZE variable to build correct linker scripts and flash commands. I’m working on a template project which will be available on github. But you’ve got the idea of how to do it. One nice addition i’m working on is the ability to have different output directories.

The other minor modification you will need is to properly initialize EspFs in your user_main and link the espfs_img.o in your final linking step.

Now there are two upgrade scenarios – user initiated for consumer devices and automatic for industrial deployment. And the two transports for them HTTP and MQTT. HTTP was easy, MQTT is next – stay tuned.