Skip to content
Boris Lovosevic edited this page Oct 29, 2018 · 7 revisions

Partition layout & OTA update

Three different partition layouts can be used, depending on MicroPython configuration and available Flash size.

The partition layout table is generated automaticaly by BUILD.sh script.

The minimal size of the application (firmware) partition (fw_size) is 1 MB (can be owerwritten by BUILD.sh option --appsize).
If the built firmware is larger than 1 MB or the size given by --appsize argument, the partition size will be automatically enlarged.

The file system size (fs_size) is by default calculated by BUILD.sh to fit the available Flash size
FS size can be explicitly given by BUILD.sh option --fssize. If that size does not fit the available Flash size, it will be automatically adjusted to the nearest smaller size.

Warning:

If you flash the firmware with partition layout different from the currently flashed layout, always erase the flash before flashing!

./BUILD.sh erase
./BUILD.sh flash

Single partition layout, no OTA used

Name Type SubType Offset Size
nvs data nvs 0x9000 24K
phy_init data phy 0xf000 4K
MicroPython app factory 0x10000 fw_size
internalfs data fat fs_size

This partition layout is generated when no OTA is configured in menuconfig.

OTA used, 2 partitions layout

Name Type SubType Offset Size
nvs data nvs 0x9000 16K
otadata data ota 0xd000 8K
phy_init data phy 0xf000 4K
MicroPython_1 app ota_0 0x10000 fw_size
MicroPython_2 app ota_1 fw_size
internalfs data fat fs_size

This partition layout is generated when OTA is configured in menuconfig and the Flash size is 4 or 8 MB or --force2p option is used.

Every OTA update will update currently not used partition and set it active for next boot.

OTA used, 3 partitions layout

Name Type SubType Offset Size
nvs data nvs 0x9000 16K
otadata data ota 0xd000 8K
phy_init data phy 0xf000 4K
MicroPython app factory 0x10000 fw_size
MicroPython_1 app ota_0 fw_size
MicroPython_2 app ota_1 fw_size
internalfs data fat fs_size

This partition layout is generated when OTA is configured in menuconfig and the Flash size is 16 MB or --force3p option is used. This layout cannot be used if the Flash size is 4 MB.

Every OTA update will update currently not used ota partition and set it active for next boot.
The factory partition will never be updated, unless forceFactory is set to True in ota.start() or ota.fromfile() functions.

OTA Module

OTA Module can be used to update the MicroPython from http server or update file on local filesystem.

MD5 checksum file can be used to validate the update file.

Methods

ota.start(url [, file, restart, md5, forceFactory, certificate])

Perform the update from http/https server and optionally restart the system.

Argument Function
url url of the firmware binnary.
https (SSL) protocol can be used for secure update.
Full url must be given, e.g. htps://loboris.eu/ESP32/MicroPython.bin.
port optional; default: 80; http server port
file optional; default: "/MicroPython.bin"; path to update file on server
restart optional; default: False; restart the system after successful update
md5 optional; default: False; use md5 file to validate the update.
The md5 file name must be the same as update file name + .md5 extension.
forceFactory optional; default: False; force update of the Factory partition
Only possible if 3-partitions layout is used.
certificate optional; Name of the certificate file.
If https is used, server's certificate can be given to verify the server.

All arguments except url must be of kw type).

>>> import ota
>>> ota.start(url="http://loboris.eu/ESP32/MicroPython.bin", md5=True)
I (141279) OTA_UPDATE: Starting OTA update from 'MicroPython' to 'MicroPython_1' partition
I (141350) OTA_UPDATE: Connected to http server, requesting '/ESP32/MicroPython.bin.md5'
I (141401) OTA_UPDATE: Received remote MD5
I (141448) OTA_UPDATE: Connected to http server, requesting '/ESP32/MicroPython.bin'
I (141449) OTA_UPDATE: Send GET request to server succeeded
I (141498) OTA_UPDATE: Update image size: 1709104 bytes
I (141499) OTA_UPDATE: Writing to 'MicroPython_1' partition at offset 0x1c0000
I (157906) OTA_UPDATE: Connection closed, all packets received
I (157912) OTA_UPDATE: Image written, total length = 1709104 bytes

I (157919) OTA_UPDATE: MD5 Checksum check PASSED.
W (159782) OTA_UPDATE: On next reboot the system will be started from 'MicroPython_1' partition
True
>>> 

ota.fromfile(file [, restart, forceFactory])

Perform the update from the file on local file system (internal or SD Card) and optionally restart the system.

If md5 file (file with the same name as update file name + .md5 extension) is present, it will be used to validate the update.

Arguments:

Argument Function
file update file name
restart optional; default: False; restart the system after successful update
forceFactory optional; default: False; force update of the Factory partition
Only possible if 3-partitions layout is used.
>>> import ota
>>> ota.fromfile('MicroPython.bin', restart=True)
I (236099) OTA_UPDATE: Starting OTA update from 'MicroPython_1' to 'MicroPython_2' partition
I (236263) OTA_UPDATE: MD5 file NOT found
I (236326) OTA_UPDATE: Update image size: 1709104 bytes
I (236395) OTA_UPDATE: Writing to 'MicroPython_2' partition at offset 0x370000
I (237152) OTA_UPDATE: Image written, total length = 1709104 bytes

W (239115) OTA_UPDATE: On next reboot the system will be started from 'MicroPython_2' partition

# --- REBOOT ---
...
...

FreeRTOS running on BOTH CORES, MicroPython task started on App Core.
Running from partition at 370000, type 11 [MicroPython_2].

 Reset reason: Soft CPU reset
    uPY stack: 19456 bytes
     uPY heap: 80000/5712/74288 bytes

MicroPython ESP32_LoBo_v2.1.0 - 2017-11-21 on ESP32 board with ESP32
Type "help()" for more information.
>>> 

ota.set_bootpart(partition)

Force boot from specific partition.

The partition name must be given as argument ("MicroPython", "MicroPython_1" or "MicroPython_2").
"MicroPython" is the name of the factory partition and can be used only with 3-partition layout.
If the selected partition does not contain valid application (MicroPython firmware), ota.set_bootpart() will fail.

>>> import ota, machine
>>> ota.set_bootpart('MicroPython')
W (727602) OTA_UPDATE: On next reboot the system will be started from 'MicroPython' partition
True
>>> machine.reset()

# --- REBOOT ---
...
...

FreeRTOS running on BOTH CORES, MicroPython task started on App Core.
Running from partition at 10000, type 0 [MicroPython].

 Reset reason: Soft CPU reset
    uPY stack: 19456 bytes
     uPY heap: 80000/5712/74288 bytes

MicroPython ESP32_LoBo_v2.1.0 - 2017-11-21 on ESP32 board with ESP32
Type "help()" for more information.
>>>