esp-idf/components/app_update/otatool.py

389 wiersze
14 KiB
Python
Czysty Zwykły widok Historia

2018-11-15 21:00:27 +00:00
#!/usr/bin/env python
#
# otatool is used to perform ota-level operations - flashing ota partition
# erasing ota partition and switching ota partition
#
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function, division
import argparse
import os
import sys
import binascii
import tempfile
import collections
import struct
try:
from parttool import PartitionName, PartitionType, ParttoolTarget, PARTITION_TABLE_OFFSET
except ImportError:
COMPONENTS_PATH = os.path.expandvars(os.path.join("$IDF_PATH", "components"))
PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, "partition_table")
2018-11-15 21:00:27 +00:00
sys.path.append(PARTTOOL_DIR)
from parttool import PartitionName, PartitionType, ParttoolTarget, PARTITION_TABLE_OFFSET
2018-11-15 21:00:27 +00:00
__version__ = '2.0'
2018-11-15 21:00:27 +00:00
SPI_FLASH_SEC_SIZE = 0x2000
quiet = False
def status(msg):
if not quiet:
print(msg)
class OtatoolTarget():
2018-11-15 21:00:27 +00:00
OTADATA_PARTITION = PartitionType("data", "ota")
2018-11-15 21:00:27 +00:00
def __init__(self, port=None, baud=None, partition_table_offset=PARTITION_TABLE_OFFSET, partition_table_file=None,
2019-03-28 06:07:53 +00:00
spi_flash_sec_size=SPI_FLASH_SEC_SIZE, esptool_args=[], esptool_write_args=[],
esptool_read_args=[], esptool_erase_args=[]):
self.target = ParttoolTarget(port, baud, partition_table_offset, partition_table_file, esptool_args,
2019-03-28 06:07:53 +00:00
esptool_write_args, esptool_read_args, esptool_erase_args)
self.spi_flash_sec_size = spi_flash_sec_size
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.close()
try:
self.target.read_partition(OtatoolTarget.OTADATA_PARTITION, temp_file.name)
with open(temp_file.name, "rb") as f:
self.otadata = f.read()
except Exception:
self.otadata = None
finally:
os.unlink(temp_file.name)
2018-11-15 21:00:27 +00:00
def _check_otadata_partition(self):
if not self.otadata:
raise Exception("No otadata partition found")
2018-11-15 21:00:27 +00:00
def erase_otadata(self):
self._check_otadata_partition()
self.target.erase_partition(OtatoolTarget.OTADATA_PARTITION)
2018-11-15 21:00:27 +00:00
def _get_otadata_info(self):
info = []
2018-11-15 21:00:27 +00:00
otadata_info = collections.namedtuple("otadata_info", "seq crc")
2018-11-15 21:00:27 +00:00
for i in range(2):
start = i * (self.spi_flash_sec_size >> 1)
2018-11-15 21:00:27 +00:00
seq = bytearray(self.otadata[start:start + 4])
crc = bytearray(self.otadata[start + 28:start + 32])
2018-11-15 21:00:27 +00:00
seq = struct.unpack('>I', seq)
crc = struct.unpack('>I', crc)
2018-11-15 21:00:27 +00:00
info.append(otadata_info(seq[0], crc[0]))
2018-11-15 21:00:27 +00:00
return info
2018-11-15 21:00:27 +00:00
def _get_partition_id_from_ota_id(self, ota_id):
if isinstance(ota_id, int):
return PartitionType("app", "ota_" + str(ota_id))
else:
return PartitionName(ota_id)
2018-11-15 21:00:27 +00:00
def switch_ota_partition(self, ota_id):
self._check_otadata_partition()
2018-11-15 21:00:27 +00:00
sys.path.append(PARTTOOL_DIR)
import gen_esp32part as gen
2018-11-15 21:00:27 +00:00
def is_otadata_info_valid(status):
seq = status.seq % (1 << 32)
crc = hex(binascii.crc32(struct.pack("I", seq), 0xFFFFFFFF) % (1 << 32))
return seq < (int('0xFFFFFFFF', 16) % (1 << 32)) and status.crc == crc
2018-11-15 21:00:27 +00:00
partition_table = self.target.partition_table
2018-11-15 21:00:27 +00:00
ota_partitions = list()
2018-11-15 21:00:27 +00:00
for i in range(gen.NUM_PARTITION_SUBTYPE_APP_OTA):
ota_partition = filter(lambda p: p.subtype == (gen.MIN_PARTITION_SUBTYPE_APP_OTA + i), partition_table)
2018-11-15 21:00:27 +00:00
try:
ota_partitions.append(list(ota_partition)[0])
except IndexError:
break
2018-11-15 21:00:27 +00:00
ota_partitions = sorted(ota_partitions, key=lambda p: p.subtype)
2018-11-15 21:00:27 +00:00
if not ota_partitions:
raise Exception("No ota app partitions found")
2018-11-15 21:00:27 +00:00
# Look for the app partition to switch to
ota_partition_next = None
2018-11-15 21:00:27 +00:00
try:
if isinstance(ota_id, int):
ota_partition_next = filter(lambda p: p.subtype - gen.MIN_PARTITION_SUBTYPE_APP_OTA == ota_id, ota_partitions)
else:
ota_partition_next = filter(lambda p: p.name == ota_id, ota_partitions)
2018-11-15 21:00:27 +00:00
ota_partition_next = list(ota_partition_next)[0]
except IndexError:
raise Exception("Partition to switch to not found")
otadata_info = self._get_otadata_info()
# Find the copy to base the computation for ota sequence number on
otadata_compute_base = -1
# Both are valid, take the max as computation base
if is_otadata_info_valid(otadata_info[0]) and is_otadata_info_valid(otadata_info[1]):
if otadata_info[0].seq >= otadata_info[1].seq:
otadata_compute_base = 0
else:
otadata_compute_base = 1
# Only one copy is valid, use that
elif is_otadata_info_valid(otadata_info[0]):
2018-11-15 21:00:27 +00:00
otadata_compute_base = 0
elif is_otadata_info_valid(otadata_info[1]):
2018-11-15 21:00:27 +00:00
otadata_compute_base = 1
# Both are invalid (could be initial state - all 0xFF's)
else:
pass
2018-11-15 21:00:27 +00:00
ota_seq_next = 0
ota_partitions_num = len(ota_partitions)
2018-11-15 21:00:27 +00:00
target_seq = (ota_partition_next.subtype & 0x0F) + 1
2018-11-15 21:00:27 +00:00
# Find the next ota sequence number
if otadata_compute_base == 0 or otadata_compute_base == 1:
base_seq = otadata_info[otadata_compute_base].seq % (1 << 32)
2018-11-15 21:00:27 +00:00
i = 0
while base_seq > target_seq % ota_partitions_num + i * ota_partitions_num:
i += 1
2018-11-15 21:00:27 +00:00
ota_seq_next = target_seq % ota_partitions_num + i * ota_partitions_num
else:
ota_seq_next = target_seq
2018-11-15 21:00:27 +00:00
# Create binary data from computed values
ota_seq_next = struct.pack("I", ota_seq_next)
ota_seq_crc_next = binascii.crc32(ota_seq_next, 0xFFFFFFFF) % (1 << 32)
ota_seq_crc_next = struct.pack("I", ota_seq_crc_next)
2018-11-15 21:00:27 +00:00
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.close()
2018-11-15 21:00:27 +00:00
try:
with open(temp_file.name, "wb") as otadata_next_file:
start = (1 if otadata_compute_base == 0 else 0) * (self.spi_flash_sec_size >> 1)
2018-11-15 21:00:27 +00:00
otadata_next_file.write(self.otadata)
2018-11-15 21:00:27 +00:00
otadata_next_file.seek(start)
otadata_next_file.write(ota_seq_next)
2018-11-15 21:00:27 +00:00
otadata_next_file.seek(start + 28)
otadata_next_file.write(ota_seq_crc_next)
2018-11-15 21:00:27 +00:00
otadata_next_file.flush()
2018-11-15 21:00:27 +00:00
self.target.write_partition(OtatoolTarget.OTADATA_PARTITION, temp_file.name)
finally:
os.unlink(temp_file.name)
def read_ota_partition(self, ota_id, output):
self.target.read_partition(self._get_partition_id_from_ota_id(ota_id), output)
def write_ota_partition(self, ota_id, input):
self.target.write_partition(self._get_partition_id_from_ota_id(ota_id), input)
def erase_ota_partition(self, ota_id):
self.target.erase_partition(self._get_partition_id_from_ota_id(ota_id))
def _read_otadata(target):
target._check_otadata_partition()
2019-03-28 06:07:53 +00:00
otadata_info = target._get_otadata_info()
2019-03-28 06:07:53 +00:00
print(" {:8s} \t {:8s} | \t {:8s} \t {:8s}".format("OTA_SEQ", "CRC", "OTA_SEQ", "CRC"))
print("Firmware: 0x{:8x} \t0x{:8x} | \t0x{:8x} \t 0x{:8x}".format(otadata_info[0].seq, otadata_info[0].crc,
otadata_info[1].seq, otadata_info[1].crc))
def _erase_otadata(target):
target.erase_otadata()
status("Erased ota_data partition contents")
2018-11-15 21:00:27 +00:00
def _switch_ota_partition(target, ota_id):
target.switch_ota_partition(ota_id)
2018-11-15 21:00:27 +00:00
def _read_ota_partition(target, ota_id, output):
target.read_ota_partition(ota_id, output)
status("Read ota partition contents to file {}".format(output))
2018-11-15 21:00:27 +00:00
def _write_ota_partition(target, ota_id, input):
target.write_ota_partition(ota_id, input)
status("Written contents of file {} to ota partition".format(input))
2018-11-15 21:00:27 +00:00
def _erase_ota_partition(target, ota_id):
target.erase_ota_partition(ota_id)
2018-11-15 21:00:27 +00:00
status("Erased contents of ota partition")
def main():
global quiet
parser = argparse.ArgumentParser("ESP-IDF OTA Partitions Tool")
parser.add_argument("--quiet", "-q", help="suppress stderr messages", action="store_true")
2019-03-28 06:07:53 +00:00
parser.add_argument("--esptool-args", help="additional main arguments for esptool", nargs="+")
parser.add_argument("--esptool-write-args", help="additional subcommand arguments for esptool write_flash", nargs="+")
parser.add_argument("--esptool-read-args", help="additional subcommand arguments for esptool read_flash", nargs="+")
parser.add_argument("--esptool-erase-args", help="additional subcommand arguments for esptool erase_region", nargs="+")
2018-11-15 21:00:27 +00:00
# There are two possible sources for the partition table: a device attached to the host
# or a partition table CSV/binary file. These sources are mutually exclusive.
parser.add_argument("--port", "-p", help="port where the device to read the partition table from is attached")
2018-11-15 21:00:27 +00:00
parser.add_argument("--baud", "-b", help="baudrate to use", type=int)
parser.add_argument("--partition-table-offset", "-o", help="offset to read the partition table from", type=str)
2018-11-15 21:00:27 +00:00
parser.add_argument("--partition-table-file", "-f", help="file (CSV/binary) to read the partition table from; \
overrides device attached to specified port as the partition table source when defined")
2018-11-15 21:00:27 +00:00
subparsers = parser.add_subparsers(dest="operation", help="run otatool -h for additional help")
spi_flash_sec_size = argparse.ArgumentParser(add_help=False)
spi_flash_sec_size.add_argument("--spi-flash-sec-size", help="value of SPI_FLASH_SEC_SIZE macro", type=str)
2018-11-15 21:00:27 +00:00
# Specify the supported operations
subparsers.add_parser("read_otadata", help="read otadata partition", parents=[spi_flash_sec_size])
2018-11-15 21:00:27 +00:00
subparsers.add_parser("erase_otadata", help="erase otadata partition")
slot_or_name_parser = argparse.ArgumentParser(add_help=False)
slot_or_name_parser_args = slot_or_name_parser.add_mutually_exclusive_group()
slot_or_name_parser_args.add_argument("--slot", help="slot number of the ota partition", type=int)
slot_or_name_parser_args.add_argument("--name", help="name of the ota partition")
subparsers.add_parser("switch_ota_partition", help="switch otadata partition", parents=[slot_or_name_parser, spi_flash_sec_size])
2018-11-15 21:00:27 +00:00
read_ota_partition_subparser = subparsers.add_parser("read_ota_partition", help="read contents of an ota partition", parents=[slot_or_name_parser])
read_ota_partition_subparser.add_argument("--output", help="file to write the contents of the ota partition to")
write_ota_partition_subparser = subparsers.add_parser("write_ota_partition", help="write contents to an ota partition", parents=[slot_or_name_parser])
write_ota_partition_subparser.add_argument("--input", help="file whose contents to write to the ota partition")
subparsers.add_parser("erase_ota_partition", help="erase contents of an ota partition", parents=[slot_or_name_parser])
args = parser.parse_args()
quiet = args.quiet
# No operation specified, display help and exit
if args.operation is None:
if not quiet:
parser.print_help()
sys.exit(1)
target_args = {}
if args.port:
target_args["port"] = args.port
if args.partition_table_file:
target_args["partition_table_file"] = args.partition_table_file
if args.partition_table_offset:
target_args["partition_table_offset"] = int(args.partition_table_offset, 0)
try:
if args.spi_flash_sec_size:
target_args["spi_flash_sec_size"] = int(args.spi_flash_sec_size, 0)
except AttributeError:
pass
2019-03-28 06:07:53 +00:00
if args.esptool_args:
target_args["esptool_args"] = args.esptool_args
if args.esptool_write_args:
target_args["esptool_write_args"] = args.esptool_write_args
if args.esptool_read_args:
target_args["esptool_read_args"] = args.esptool_read_args
if args.esptool_erase_args:
target_args["esptool_erase_args"] = args.esptool_erase_args
if args.baud:
target_args["baud"] = args.baud
target = OtatoolTarget(**target_args)
# Create the operation table and execute the operation
common_args = {'target':target}
ota_id = []
try:
if args.name is not None:
ota_id = ["name"]
else:
if args.slot is not None:
ota_id = ["slot"]
except AttributeError:
pass
otatool_ops = {
'read_otadata':(_read_otadata, []),
'erase_otadata':(_erase_otadata, []),
'switch_ota_partition':(_switch_ota_partition, ota_id),
'read_ota_partition':(_read_ota_partition, ["output"] + ota_id),
'write_ota_partition':(_write_ota_partition, ["input"] + ota_id),
'erase_ota_partition':(_erase_ota_partition, ota_id)
}
(op, op_args) = otatool_ops[args.operation]
for op_arg in op_args:
common_args.update({op_arg:vars(args)[op_arg]})
try:
common_args['ota_id'] = common_args.pop('name')
except KeyError:
try:
common_args['ota_id'] = common_args.pop('slot')
except KeyError:
pass
2018-11-15 21:00:27 +00:00
if quiet:
# If exceptions occur, suppress and exit quietly
try:
op(**common_args)
2018-11-15 21:00:27 +00:00
except Exception:
sys.exit(2)
else:
op(**common_args)
2018-11-15 21:00:27 +00:00
if __name__ == '__main__':
main()