2018-11-15 21:02:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Demonstrates the use of parttool.py, a tool for performing partition level
|
|
|
|
# operations.
|
|
|
|
#
|
|
|
|
# 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 02:49:01 +00:00
|
|
|
import argparse
|
2018-11-15 21:02:34 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
PARTITION_TABLE_DIR = os.path.join('components', 'partition_table', '')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
def assert_file_same(file1, file2, err):
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(file1, 'rb') as f1:
|
|
|
|
with open(file2, 'rb') as f2:
|
2018-11-15 21:02:34 +00:00
|
|
|
f1 = f1.read()
|
|
|
|
f2 = f2.read()
|
|
|
|
|
|
|
|
if len(f1) < len(f2):
|
|
|
|
f2 = f2[:len(f1)]
|
|
|
|
else:
|
|
|
|
f1 = f1[:len(f2)]
|
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
if not f1 == f2:
|
|
|
|
raise Exception(err)
|
2018-11-15 21:02:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-01-26 02:49:01 +00:00
|
|
|
COMPONENTS_PATH = os.path.expandvars(os.path.join('$IDF_PATH', 'components'))
|
|
|
|
PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, 'partition_table')
|
2019-05-27 03:09:35 +00:00
|
|
|
|
|
|
|
sys.path.append(PARTTOOL_DIR)
|
|
|
|
from gen_empty_partition import generate_blanked_file
|
2021-01-26 02:49:01 +00:00
|
|
|
from parttool import PartitionName, PartitionType, ParttoolTarget
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
parser = argparse.ArgumentParser('ESP-IDF Partitions Tool Example')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
parser.add_argument('--port', '-p', help='port where the device to perform operations on is connected')
|
|
|
|
parser.add_argument('--binary', '-b', help='path to built example binary', default=os.path.join('build', 'parttool.bin'))
|
2018-11-15 21:02:34 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
target = ParttoolTarget(args.port)
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Read app partition and save the contents to a file. The app partition is identified
|
|
|
|
# using type-subtype combination
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Checking if device app binary matches built binary')
|
|
|
|
factory = PartitionType('app', 'factory')
|
|
|
|
target.read_partition(factory, 'app.bin')
|
|
|
|
assert_file_same(args.binary, 'app.bin', 'Device app binary does not match built binary')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Retrieve info on data storage partition, this time identifying it by name.
|
2021-01-26 02:49:01 +00:00
|
|
|
storage = PartitionName('storage')
|
2019-05-27 03:09:35 +00:00
|
|
|
storage_info = target.get_partition_info(storage)
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Found data partition at offset 0x{:x} with size 0x{:x}'.format(storage_info.offset, storage_info.size))
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Create a file whose contents will be written to the storage partition
|
2021-01-26 02:49:01 +00:00
|
|
|
with open('write.bin', 'wb') as f:
|
2019-05-27 03:09:35 +00:00
|
|
|
# Create a file to write to the data partition with randomly generated content
|
|
|
|
f.write(os.urandom(storage_info.size))
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Write the contents of the created file to storage partition
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Writing to data partition')
|
|
|
|
target.write_partition(storage, 'write.bin')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Read back the contents of the storage partition
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Reading data partition')
|
|
|
|
target.read_partition(storage, 'read.bin')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
assert_file_same('write.bin', 'read.bin', 'Read contents of storage partition does not match source file contents')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Erase contents of the storage partition
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Erasing data partition')
|
2019-05-27 03:09:35 +00:00
|
|
|
target.erase_partition(storage)
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Read back the erased data partition
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Reading data partition')
|
|
|
|
target.read_partition(storage, 'read.bin')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Generate a file of all 0xFF
|
2021-01-26 02:49:01 +00:00
|
|
|
generate_blanked_file(storage_info.size, 'blank.bin')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
assert_file_same('blank.bin', 'read.bin', 'Contents of storage partition not fully erased')
|
2018-11-15 21:02:34 +00:00
|
|
|
|
2019-05-27 03:09:35 +00:00
|
|
|
# Example end and cleanup
|
2021-01-26 02:49:01 +00:00
|
|
|
print('\nPartition tool operations performed successfully!')
|
|
|
|
clean_files = ['app.bin', 'read.bin', 'blank.bin', 'write.bin']
|
2019-05-27 03:09:35 +00:00
|
|
|
for clean_file in clean_files:
|
|
|
|
os.unlink(clean_file)
|
2018-11-15 21:02:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|