| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | # parttool is used to perform partition level operations - reading, | 
					
						
							|  |  |  | # writing, erasing and getting info about the partition. | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | # | 
					
						
							|  |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | from __future__ import division, print_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | import argparse | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | import subprocess | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import gen_esp32part as gen | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | __version__ = '2.0' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | COMPONENTS_PATH = os.path.expandvars(os.path.join('$IDF_PATH', 'components')) | 
					
						
							|  |  |  | ESPTOOL_PY = os.path.join(COMPONENTS_PATH, 'esptool_py', 'esptool', 'esptool.py') | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | PARTITION_TABLE_OFFSET = 0x8000 | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | quiet = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 16:39:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | def status(msg): | 
					
						
							|  |  |  |     if not quiet: | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  |         print(msg) | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | class _PartitionId(): | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |     def __init__(self, name=None, p_type=None, subtype=None, part_list=None): | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         self.name = name | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |         self.type = p_type | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         self.subtype = subtype | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |         self.part_list = part_list | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | class PartitionName(_PartitionId): | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def __init__(self, name): | 
					
						
							|  |  |  |         _PartitionId.__init__(self, name=name) | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | class PartitionType(_PartitionId): | 
					
						
							| 
									
										
										
										
											2019-01-08 15:47:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 09:46:51 +08:00
										 |  |  |     def __init__(self, p_type, subtype, part_list=None): | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |         _PartitionId.__init__(self, p_type=p_type, subtype=subtype, part_list=part_list) | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-20 18:24:18 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | PARTITION_BOOT_DEFAULT = _PartitionId() | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | class ParttoolTarget(): | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +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
										 |  |  |                  esptool_args=[], esptool_write_args=[], esptool_read_args=[], esptool_erase_args=[]): | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         self.port = port | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +02:00
										 |  |  |         self.baud = baud | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         gen.offset_part_table = partition_table_offset | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |         def parse_esptool_args(esptool_args): | 
					
						
							|  |  |  |             results = list() | 
					
						
							|  |  |  |             for arg in esptool_args: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 pattern = re.compile(r'(.+)=(.+)') | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |                 result = pattern.match(arg) | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     key = result.group(1) | 
					
						
							|  |  |  |                     value = result.group(2) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     results.extend(['--' + key, value]) | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |                 except AttributeError: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     results.extend(['--' + arg]) | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |             return results | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.esptool_args = parse_esptool_args(esptool_args) | 
					
						
							|  |  |  |         self.esptool_write_args = parse_esptool_args(esptool_write_args) | 
					
						
							|  |  |  |         self.esptool_read_args = parse_esptool_args(esptool_read_args) | 
					
						
							|  |  |  |         self.esptool_erase_args = parse_esptool_args(esptool_erase_args) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         if partition_table_file: | 
					
						
							| 
									
										
										
										
											2019-12-16 18:18:20 +11:00
										 |  |  |             partition_table = None | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             with open(partition_table_file, 'rb') as f: | 
					
						
							| 
									
										
										
										
											2019-12-16 18:18:20 +11:00
										 |  |  |                 input_is_binary = (f.read(2) == gen.PartitionDefinition.MAGIC_BYTES) | 
					
						
							| 
									
										
										
										
											2019-12-20 19:09:10 +11:00
										 |  |  |                 f.seek(0) | 
					
						
							| 
									
										
										
										
											2019-12-16 18:18:20 +11:00
										 |  |  |                 if input_is_binary: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                     partition_table = gen.PartitionTable.from_binary(f.read()) | 
					
						
							| 
									
										
										
										
											2019-12-16 18:18:20 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if partition_table is None: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 with open(partition_table_file, 'r') as f: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                     f.seek(0) | 
					
						
							|  |  |  |                     partition_table = gen.PartitionTable.from_csv(f.read()) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             temp_file = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							|  |  |  |             temp_file.close() | 
					
						
							| 
									
										
										
										
											2018-06-22 11:14:22 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 self._call_esptool(['read_flash', str(partition_table_offset), str(gen.MAX_PARTITION_LENGTH), temp_file.name]) | 
					
						
							|  |  |  |                 with open(temp_file.name, 'rb') as f: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                     partition_table = gen.PartitionTable.from_binary(f.read()) | 
					
						
							|  |  |  |             finally: | 
					
						
							|  |  |  |                 os.unlink(temp_file.name) | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         self.partition_table = partition_table | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 13:08:08 +02:00
										 |  |  |     # set `out` to None to redirect the output to the STDOUT | 
					
						
							|  |  |  |     # otherwise set `out` to file descriptor | 
					
						
							|  |  |  |     # beware that the method does not close the file descriptor | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def _call_esptool(self, args, out=None): | 
					
						
							| 
									
										
										
										
											2019-03-28 14:07:53 +08:00
										 |  |  |         esptool_args = [sys.executable, ESPTOOL_PY] + self.esptool_args | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         if self.port: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             esptool_args += ['--port', self.port] | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +02:00
										 |  |  |         if self.baud: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             esptool_args += ['--baud', str(self.baud)] | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         esptool_args += args | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         print('Running %s...' % (' '.join(esptool_args))) | 
					
						
							| 
									
										
										
										
											2020-09-15 13:08:08 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             subprocess.check_call(esptool_args, stdout=out, stderr=subprocess.STDOUT) | 
					
						
							|  |  |  |         except subprocess.CalledProcessError as e: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             print('An exception: **', str(e), '** occurred in _call_esptool.', file=out) | 
					
						
							| 
									
										
										
										
											2020-09-15 13:08:08 +02:00
										 |  |  |             raise e | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def get_partition_info(self, partition_id): | 
					
						
							|  |  |  |         partition = None | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         if partition_id.name: | 
					
						
							|  |  |  |             partition = self.partition_table.find_by_name(partition_id.name) | 
					
						
							|  |  |  |         elif partition_id.type and partition_id.subtype: | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |             partition = list(self.partition_table.find_by_type(partition_id.type, partition_id.subtype)) | 
					
						
							|  |  |  |             if not partition_id.part_list: | 
					
						
							|  |  |  |                 partition = partition[0] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         else:  # default boot partition | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             search = ['factory'] + ['ota_{}'.format(d) for d in range(16)] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |             for subtype in search: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 partition = next(self.partition_table.find_by_type('app', subtype), None) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                 if partition: | 
					
						
							|  |  |  |                     break | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         if not partition: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise Exception('Partition does not exist') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         return partition | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def erase_partition(self, partition_id): | 
					
						
							|  |  |  |         partition = self.get_partition_info(partition_id) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         self._call_esptool(['erase_region', str(partition.offset),  str(partition.size)] + self.esptool_erase_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def read_partition(self, partition_id, output): | 
					
						
							|  |  |  |         partition = self.get_partition_info(partition_id) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         self._call_esptool(['read_flash', str(partition.offset), str(partition.size), output] + self.esptool_read_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     def write_partition(self, partition_id, input): | 
					
						
							|  |  |  |         self.erase_partition(partition_id) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |         partition = self.get_partition_info(partition_id) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         with open(input, 'rb') as input_file: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |             content_len = len(input_file.read()) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |             if content_len > partition.size: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 raise Exception('Input file size exceeds partition size') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         self._call_esptool(['write_flash', str(partition.offset), input] + self.esptool_write_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | def _write_partition(target, partition_id, input): | 
					
						
							|  |  |  |     target.write_partition(partition_id, input) | 
					
						
							|  |  |  |     partition = target.get_partition_info(partition_id) | 
					
						
							|  |  |  |     status("Written contents of file '{}' at offset 0x{:x}".format(input, partition.offset)) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | def _read_partition(target, partition_id, output): | 
					
						
							|  |  |  |     target.read_partition(partition_id, output) | 
					
						
							|  |  |  |     partition = target.get_partition_info(partition_id) | 
					
						
							|  |  |  |     status("Read partition '{}' contents from device at offset 0x{:x} to file '{}'" | 
					
						
							|  |  |  |            .format(partition.name, partition.offset, output)) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | def _erase_partition(target, partition_id): | 
					
						
							|  |  |  |     target.erase_partition(partition_id) | 
					
						
							|  |  |  |     partition = target.get_partition_info(partition_id) | 
					
						
							|  |  |  |     status("Erased partition '{}' at offset 0x{:x}".format(partition.name, partition.offset)) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | def _get_partition_info(target, partition_id, info): | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |         partitions = target.get_partition_info(partition_id) | 
					
						
							|  |  |  |         if not isinstance(partitions, list): | 
					
						
							|  |  |  |             partitions = [partitions] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     except Exception: | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     infos = [] | 
					
						
							| 
									
										
										
										
											2018-04-19 09:42:26 +05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |         for p in partitions: | 
					
						
							|  |  |  |             info_dict = { | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 'name': '{}'.format(p.name), | 
					
						
							|  |  |  |                 'type': '{}'.format(p.type), | 
					
						
							|  |  |  |                 'subtype': '{}'.format(p.subtype), | 
					
						
							|  |  |  |                 'offset': '0x{:x}'.format(p.offset), | 
					
						
							|  |  |  |                 'size': '0x{:x}'.format(p.size), | 
					
						
							|  |  |  |                 'encrypted': '{}'.format(p.encrypted) | 
					
						
							| 
									
										
										
										
											2020-10-01 15:10:00 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             for i in info: | 
					
						
							|  |  |  |                 infos += [info_dict[i]] | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     except KeyError: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         raise RuntimeError('Request for unknown partition info {}'.format(i)) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     print(' '.join(infos)) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     global quiet | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser = argparse.ArgumentParser('ESP-IDF Partitions Tool') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +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 when writing to flash', nargs='+') | 
					
						
							|  |  |  |     parser.add_argument('--esptool-read-args', help='additional subcommand arguments when reading flash', nargs='+') | 
					
						
							|  |  |  |     parser.add_argument('--esptool-erase-args', help='additional subcommand arguments when erasing regions of flash', nargs='+') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     # By default the device attached to the specified port is queried for the partition table. If a partition table file | 
					
						
							|  |  |  |     # is specified, that is used instead. | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--port', '-p', help='port where the target device of the command is connected to; the partition table is sourced from this device \
 | 
					
						
							|  |  |  |                                             when the partition table file is not defined') | 
					
						
							|  |  |  |     parser.add_argument('--baud', '-b', help='baudrate to use', type=int) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08: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) | 
					
						
							|  |  |  |     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 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     partition_selection_parser = argparse.ArgumentParser(add_help=False) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Specify what partition to perform the operation on. This can either be specified using the | 
					
						
							|  |  |  |     # partition name or the first partition that matches the specified type/subtype | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     partition_selection_args = partition_selection_parser.add_mutually_exclusive_group() | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     partition_selection_args.add_argument('--partition-name', '-n', help='name of the partition') | 
					
						
							|  |  |  |     partition_selection_args.add_argument('--partition-type', '-t', help='type of the partition') | 
					
						
							|  |  |  |     partition_selection_args.add_argument('--partition-boot-default', '-d', help='select the default boot partition \
 | 
					
						
							|  |  |  |                                            using the same fallback logic as the IDF bootloader', action='store_true') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     partition_selection_parser.add_argument('--partition-subtype', '-s', help='subtype of the partition') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers = parser.add_subparsers(dest='operation', help='run parttool -h for additional help') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Specify the supported operations | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     read_part_subparser = subparsers.add_parser('read_partition', help='read partition from device and dump contents into a file', | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                                                 parents=[partition_selection_parser]) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     read_part_subparser.add_argument('--output', help='file to dump the read partition contents to') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_part_subparser = subparsers.add_parser('write_partition', help='write contents of a binary file to partition on device', | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |                                                  parents=[partition_selection_parser]) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     write_part_subparser.add_argument('--input', help='file whose contents are to be written to the partition offset') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     subparsers.add_parser('erase_partition', help='erase the contents of a partition on the device', parents=[partition_selection_parser]) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     print_partition_info_subparser = subparsers.add_parser('get_partition_info', help='get partition information', parents=[partition_selection_parser]) | 
					
						
							|  |  |  |     print_partition_info_subparser.add_argument('--info', help='type of partition information to get', | 
					
						
							|  |  |  |                                                 choices=['name', 'type', 'subtype', 'offset', 'size', 'encrypted'], default=['offset', 'size'], nargs='+') | 
					
						
							|  |  |  |     print_partition_info_subparser.add_argument('--part_list', help='Get a list of partitions suitable for a given type', action='store_true') | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +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:07:54 +08:00
										 |  |  |     # Prepare the partition to perform operation on | 
					
						
							|  |  |  |     if args.partition_name: | 
					
						
							|  |  |  |         partition_id = PartitionName(args.partition_name) | 
					
						
							|  |  |  |     elif args.partition_type: | 
					
						
							|  |  |  |         if not args.partition_subtype: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise RuntimeError('--partition-subtype should be defined when --partition-type is defined') | 
					
						
							| 
									
										
										
										
											2020-11-25 19:30:44 +01:00
										 |  |  |         partition_id = PartitionType(args.partition_type, args.partition_subtype, getattr(args, 'part_list', None)) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     elif args.partition_boot_default: | 
					
						
							|  |  |  |         partition_id = PARTITION_BOOT_DEFAULT | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         raise RuntimeError('Partition to operate on should be defined using --partition-name OR \
 | 
					
						
							|  |  |  |                             partition-type,--partition-subtype OR partition-boot-default') | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Prepare the device to perform operation on | 
					
						
							|  |  |  |     target_args = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if args.port: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['port'] = args.port | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +02:00
										 |  |  |     if args.baud: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         target_args['baud'] = args.baud | 
					
						
							| 
									
										
										
										
											2019-06-27 14:54:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +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:07:54 +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:07:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-27 11:07:54 +08:00
										 |  |  |     target = ParttoolTarget(**target_args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Create the operation table and execute the operation | 
					
						
							|  |  |  |     common_args = {'target':target, 'partition_id':partition_id} | 
					
						
							|  |  |  |     parttool_ops = { | 
					
						
							|  |  |  |         'erase_partition':(_erase_partition, []), | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         'read_partition':(_read_partition, ['output']), | 
					
						
							|  |  |  |         'write_partition':(_write_partition, ['input']), | 
					
						
							|  |  |  |         'get_partition_info':(_get_partition_info, ['info']) | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (op, op_args) = parttool_ops[args.operation] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for op_arg in op_args: | 
					
						
							|  |  |  |         common_args.update({op_arg:vars(args)[op_arg]}) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if quiet: | 
					
						
							|  |  |  |         # If exceptions occur, suppress and exit quietly | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-27 11:07:54 +08:00
										 |  |  |             op(**common_args) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  |         except Exception: | 
					
						
							|  |  |  |             sys.exit(2) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-12-16 18:18:20 +11:00
										 |  |  |         try: | 
					
						
							|  |  |  |             op(**common_args) | 
					
						
							|  |  |  |         except gen.InputError as e: | 
					
						
							|  |  |  |             print(e, file=sys.stderr) | 
					
						
							|  |  |  |             sys.exit(2) | 
					
						
							| 
									
										
										
										
											2018-11-16 04:59:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2018-11-30 16:39:55 +08:00
										 |  |  |     main() |