| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # otatool is used to perform ota-level operations - flashing ota partition | 
					
						
							|  |  |  | # erasing ota partition and switching ota partition | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2021-05-10 03:48:25 +02:00
										 |  |  | # SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  | # SPDX-License-Identifier: Apache-2.0 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | from __future__ import division, print_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | import argparse | 
					
						
							|  |  |  | import binascii | 
					
						
							|  |  |  | import collections | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | import struct | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import sys | 
					
						
							|  |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     from parttool import PARTITION_TABLE_OFFSET, PartitionName, PartitionType, ParttoolTarget | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     COMPONENTS_PATH = os.path.expandvars(os.path.join('$IDF_PATH', 'components')) | 
					
						
							|  |  |  |     PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, 'partition_table') | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     sys.path.append(PARTTOOL_DIR) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     from parttool import PARTITION_TABLE_OFFSET, PartitionName, PartitionType, ParttoolTarget | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | __version__ = '2.0' | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | SPI_FLASH_SEC_SIZE = 0x2000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quiet = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def status(msg): | 
					
						
							|  |  |  |     if not quiet: | 
					
						
							|  |  |  |         print(msg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | class OtatoolTarget(): | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     OTADATA_PARTITION = PartitionType('data', 'ota') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 14:11:12 +02:00
										 |  |  |     def __init__(self, port=None, baud=None, partition_table_offset=PARTITION_TABLE_OFFSET, partition_table_file=None, | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |                  spi_flash_sec_size=SPI_FLASH_SEC_SIZE, esptool_args=[], esptool_write_args=[], | 
					
						
							|  |  |  |                  esptool_read_args=[], esptool_erase_args=[]): | 
					
						
							| 
									
										
										
										
											2019-07-10 14:11:12 +02:00
										 |  |  |         self.target = ParttoolTarget(port, baud, partition_table_offset, partition_table_file, esptool_args, | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |                                      esptool_write_args, esptool_read_args, esptool_erase_args) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         self.spi_flash_sec_size = spi_flash_sec_size | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         temp_file = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							|  |  |  |         temp_file.close() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.target.read_partition(OtatoolTarget.OTADATA_PARTITION, temp_file.name) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             with open(temp_file.name, 'rb') as f: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 self.otadata = f.read() | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.unlink(temp_file.name) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     def _check_otadata_partition(self): | 
					
						
							|  |  |  |         if not self.otadata: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise Exception('No otadata partition found') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     def erase_otadata(self): | 
					
						
							|  |  |  |         self._check_otadata_partition() | 
					
						
							|  |  |  |         self.target.erase_partition(OtatoolTarget.OTADATA_PARTITION) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     def _get_otadata_info(self): | 
					
						
							|  |  |  |         info = [] | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         otadata_info = collections.namedtuple('otadata_info', 'seq crc') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         for i in range(2): | 
					
						
							|  |  |  |             start = i * (self.spi_flash_sec_size >> 1) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |             seq = bytearray(self.otadata[start:start + 4]) | 
					
						
							|  |  |  |             crc = bytearray(self.otadata[start + 28:start + 32]) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 02:18:30 +08:00
										 |  |  |             seq = struct.unpack('I', seq) | 
					
						
							|  |  |  |             crc = struct.unpack('I', crc) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |             info.append(otadata_info(seq[0], crc[0])) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         return info | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     def _get_partition_id_from_ota_id(self, ota_id): | 
					
						
							|  |  |  |         if isinstance(ota_id, int): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             return PartitionType('app', 'ota_' + str(ota_id)) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             return PartitionName(ota_id) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     def switch_ota_partition(self, ota_id): | 
					
						
							|  |  |  |         self._check_otadata_partition() | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         import gen_esp32part as gen | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         def is_otadata_info_valid(status): | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             seq = status.seq % (1 << 32) | 
					
						
							| 
									
										
										
										
											2021-03-25 02:18:30 +08:00
										 |  |  |             crc = binascii.crc32(struct.pack('I', seq), 0xFFFFFFFF) % (1 << 32) | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             return seq < (int('0xFFFFFFFF', 16) % (1 << 32)) and status.crc == crc | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         partition_table = self.target.partition_table | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         ota_partitions = list() | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08: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-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 ota_partitions.append(list(ota_partition)[0]) | 
					
						
							|  |  |  |             except IndexError: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         ota_partitions = sorted(ota_partitions, key=lambda p: p.subtype) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         if not ota_partitions: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise Exception('No ota app partitions found') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         # Look for the app partition to switch to | 
					
						
							|  |  |  |         ota_partition_next = None | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |             if isinstance(ota_id, int): | 
					
						
							|  |  |  |                 ota_partition_next = filter(lambda p: p.subtype - gen.MIN_PARTITION_SUBTYPE_APP_OTA  == ota_id, ota_partitions) | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 ota_partition_next = filter(lambda p: p.name == ota_id, ota_partitions) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             ota_partition_next = list(ota_partition_next)[0] | 
					
						
							|  |  |  |         except IndexError: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise Exception('Partition to switch to not found') | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         otadata_info = self._get_otadata_info() | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # 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 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         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: | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |                 otadata_compute_base = 0 | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 otadata_compute_base = 1 | 
					
						
							|  |  |  |         # Only one copy is valid, use that | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         elif is_otadata_info_valid(otadata_info[0]): | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  |             otadata_compute_base = 0 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         elif is_otadata_info_valid(otadata_info[1]): | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  |             otadata_compute_base = 1 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         # Both are invalid (could be initial state - all 0xFF's) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         ota_seq_next = 0 | 
					
						
							|  |  |  |         ota_partitions_num = len(ota_partitions) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         target_seq = (ota_partition_next.subtype & 0x0F) + 1 | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         # Find the next ota sequence number | 
					
						
							|  |  |  |         if otadata_compute_base == 0 or otadata_compute_base == 1: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |             base_seq = otadata_info[otadata_compute_base].seq % (1 << 32) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             i = 0 | 
					
						
							|  |  |  |             while base_seq > target_seq % ota_partitions_num + i * ota_partitions_num: | 
					
						
							|  |  |  |                 i += 1 | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |             ota_seq_next = target_seq % ota_partitions_num + i * ota_partitions_num | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             ota_seq_next = target_seq | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         # Create binary data from computed values | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         ota_seq_next = struct.pack('I', ota_seq_next) | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  |         ota_seq_crc_next = binascii.crc32(ota_seq_next, 0xFFFFFFFF) % (1 << 32) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         ota_seq_crc_next = struct.pack('I', ota_seq_crc_next) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         temp_file = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							|  |  |  |         temp_file.close() | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             with open(temp_file.name, 'wb') as otadata_next_file: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 start = (1 if otadata_compute_base == 0 else 0) * (self.spi_flash_sec_size >> 1) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 otadata_next_file.write(self.otadata) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 otadata_next_file.seek(start) | 
					
						
							|  |  |  |                 otadata_next_file.write(ota_seq_next) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 otadata_next_file.seek(start + 28) | 
					
						
							|  |  |  |                 otadata_next_file.write(ota_seq_crc_next) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |                 otadata_next_file.flush() | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08: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 14:07:53 +08:00
										 |  |  |     otadata_info = target._get_otadata_info() | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     print('             {:8s} \t  {:8s} | \t  {:8s} \t   {:8s}'.format('OTA_SEQ', 'CRC', 'OTA_SEQ', 'CRC')) | 
					
						
							| 
									
										
										
										
											2021-03-25 02:18:30 +08:00
										 |  |  |     print('Firmware:  0x{:08x} \t0x{:08x} | \t0x{:08x} \t 0x{:08x}'.format(otadata_info[0].seq, otadata_info[0].crc, | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |           otadata_info[1].seq, otadata_info[1].crc)) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _erase_otadata(target): | 
					
						
							|  |  |  |     target.erase_otadata() | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     status('Erased ota_data partition contents') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | def _switch_ota_partition(target, ota_id): | 
					
						
							|  |  |  |     target.switch_ota_partition(ota_id) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | def _read_ota_partition(target, ota_id, output): | 
					
						
							|  |  |  |     target.read_ota_partition(ota_id, output) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     status('Read ota partition contents to file {}'.format(output)) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | def _write_ota_partition(target, ota_id, input): | 
					
						
							|  |  |  |     target.write_ota_partition(ota_id, input) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     status('Written contents of file {} to ota partition'.format(input)) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _erase_ota_partition(target, ota_id): | 
					
						
							|  |  |  |     target.erase_ota_partition(ota_id) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     status('Erased contents of ota partition') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     global quiet | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser = argparse.ArgumentParser('ESP-IDF OTA Partitions Tool') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--quiet', '-q', help='suppress stderr messages', action='store_true') | 
					
						
							|  |  |  |     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-16 05:00:27 +08: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. | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--port', '-p', help='port where the device to read the partition table from is attached') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--baud', '-b', help='baudrate to use', type=int) | 
					
						
							| 
									
										
										
										
											2019-07-10 14:11:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--partition-table-offset', '-o', help='offset to read the partition table from',  type=str) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08: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-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers = parser.add_subparsers(dest='operation', help='run otatool -h for additional help') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     spi_flash_sec_size = argparse.ArgumentParser(add_help=False) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     spi_flash_sec_size.add_argument('--spi-flash-sec-size', help='value of SPI_FLASH_SEC_SIZE macro', type=str) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  |     # Specify the supported operations | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers.add_parser('read_otadata', help='read otadata partition', parents=[spi_flash_sec_size]) | 
					
						
							|  |  |  |     subparsers.add_parser('erase_otadata', help='erase otadata partition') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     slot_or_name_parser = argparse.ArgumentParser(add_help=False) | 
					
						
							|  |  |  |     slot_or_name_parser_args = slot_or_name_parser.add_mutually_exclusive_group() | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     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') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers.add_parser('switch_ota_partition', help='switch otadata partition', parents=[slot_or_name_parser, spi_flash_sec_size]) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     read_ota_partition_subparser = subparsers.add_parser('read_ota_partition', help='read contents of an ota partition', parents=[slot_or_name_parser]) | 
					
						
							| 
									
										
										
										
											2021-03-03 16:57:35 +08:00
										 |  |  |     read_ota_partition_subparser.add_argument('--output', help='file to write the contents of the ota partition to', required=True) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     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') | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers.add_parser('erase_ota_partition', help='erase contents of an ota partition', parents=[slot_or_name_parser]) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     target_args = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if args.port: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['port'] = args.port | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.partition_table_file: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['partition_table_file'] = args.partition_table_file | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.partition_table_offset: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['partition_table_offset'] = int(args.partition_table_offset, 0) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         if args.spi_flash_sec_size: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             target_args['spi_flash_sec_size'] = int(args.spi_flash_sec_size, 0) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     except AttributeError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |     if args.esptool_args: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['esptool_args'] = args.esptool_args | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.esptool_write_args: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['esptool_write_args'] = args.esptool_write_args | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.esptool_read_args: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['esptool_read_args'] = args.esptool_read_args | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.esptool_erase_args: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['esptool_erase_args'] = args.esptool_erase_args | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 14:11:12 +02:00
										 |  |  |     if args.baud: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['baud'] = args.baud | 
					
						
							| 
									
										
										
										
											2019-07-10 14:11:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     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: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             ota_id = ['name'] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             if args.slot is not None: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 ota_id = ['slot'] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |     except AttributeError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     otatool_ops = { | 
					
						
							|  |  |  |         'read_otadata':(_read_otadata, []), | 
					
						
							|  |  |  |         'erase_otadata':(_erase_otadata, []), | 
					
						
							|  |  |  |         'switch_ota_partition':(_switch_ota_partition, ota_id), | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         'read_ota_partition':(_read_ota_partition, ['output'] + ota_id), | 
					
						
							|  |  |  |         'write_ota_partition':(_write_ota_partition, ['input'] + ota_id), | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         '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-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if quiet: | 
					
						
							|  |  |  |         # If exceptions occur, suppress and exit quietly | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |             op(**common_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  |         except Exception: | 
					
						
							|  |  |  |             sys.exit(2) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:08:28 +08:00
										 |  |  |         op(**common_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 05:00:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |