2019-05-26 23:10:00 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Demonstrates the use of otatool.py, a tool for performing ota partition level
|
|
|
|
# operations.
|
|
|
|
#
|
2022-05-13 09:42:37 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2021-01-25 21:49:01 -05:00
|
|
|
import argparse
|
2019-05-26 23:10:00 -04:00
|
|
|
import os
|
|
|
|
import re
|
2021-01-25 21:49:01 -05:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-05-26 23:10:00 -04:00
|
|
|
from subprocess import CalledProcessError
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
import serial
|
|
|
|
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
def get_running_partition(port=None):
|
|
|
|
# Monitor the serial output of target device. The firmware outputs the currently
|
|
|
|
# running partition
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
IDF_PATH = os.path.expandvars('$IDF_PATH')
|
2019-05-26 23:10:00 -04:00
|
|
|
sys.path.append(os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool'))
|
2022-05-13 09:42:37 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
# esptool>=4.0
|
|
|
|
from esptool.loader import ESPLoader
|
|
|
|
except (AttributeError, ModuleNotFoundError):
|
|
|
|
# esptool<4.0
|
|
|
|
from esptool import ESPLoader
|
2019-05-26 23:10:00 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
ESPTOOL_PY = os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool', 'esptool.py')
|
2019-05-26 23:10:00 -04:00
|
|
|
|
2022-05-13 09:42:37 -04:00
|
|
|
baud = os.environ.get('ESPTOOL_BAUD', ESPLoader.ESP_ROM_BAUD)
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
if not port:
|
2021-01-25 21:49:01 -05:00
|
|
|
error_message = 'Unable to obtain default target device port.\nSerial log:\n\n'
|
2019-05-26 23:10:00 -04:00
|
|
|
try:
|
|
|
|
# Check what esptool.py finds on what port the device is connected to
|
2021-01-25 21:49:01 -05:00
|
|
|
output = subprocess.check_output([sys.executable, ESPTOOL_PY, 'chip_id']) # may raise CalledProcessError
|
|
|
|
pattern = r'Serial port ([\S]+)'
|
2019-05-26 23:10:00 -04:00
|
|
|
pattern = re.compile(pattern.encode())
|
|
|
|
|
|
|
|
port = re.search(pattern, output).group(1) # may raise AttributeError
|
|
|
|
except CalledProcessError as e:
|
|
|
|
raise Exception(error_message + e.output)
|
|
|
|
except AttributeError:
|
|
|
|
raise Exception(error_message + output)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
serial_instance = serial.serial_for_url(port.decode('utf-8'), baud, do_not_open=True)
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
serial_instance.dtr = False
|
|
|
|
serial_instance.rts = False
|
|
|
|
|
|
|
|
serial_instance.rts = True
|
|
|
|
serial_instance.open()
|
|
|
|
serial_instance.rts = False
|
|
|
|
|
|
|
|
# Read until example end and find the currently running partition string
|
2021-01-25 21:49:01 -05:00
|
|
|
content = serial_instance.read_until(b'Example end')
|
|
|
|
pattern = re.compile(b'Running partition: ([a-z0-9_]+)')
|
2019-05-26 23:10:00 -04:00
|
|
|
running = re.search(pattern, content).group(1)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
return running.decode('utf-8')
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-01-25 21:49:01 -05:00
|
|
|
parser.add_argument('--port', default=None)
|
2019-05-26 23:10:00 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = get_running_partition(args.port)
|
|
|
|
except Exception as e:
|
|
|
|
print(e.message)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print(res)
|
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if __name__ == '__main__':
|
2019-05-26 23:10:00 -04:00
|
|
|
main()
|