def main(): COMPONENTS_PATH = os.path.expandvars( os.path.join("$IDF_PATH", "components")) PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, "partition_table") sys.path.append(PARTTOOL_DIR) from parttool import PartitionName, PartitionType, ParttoolTarget from gen_empty_partition import generate_blanked_file parser = argparse.ArgumentParser("ESP-IDF Partitions Tool Example") 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")) args = parser.parse_args() target = ParttoolTarget(args.port) # Read app partition and save the contents to a file. The app partition is identified # using type-subtype combination 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") # Retrieve info on data storage partition, this time identifying it by name. storage = PartitionName("storage") storage_info = target.get_partition_info(storage) print("Found data partition at offset 0x{:x} with size 0x{:x}".format( storage_info.offset, storage_info.size)) # Create a file whose contents will be written to the storage partition with open("write.bin", "wb") as f: # Create a file to write to the data partition with randomly generated content f.write(os.urandom(storage_info.size)) # Write the contents of the created file to storage partition print("Writing to data partition") target.write_partition(storage, "write.bin") # Read back the contents of the storage partition print("Reading data partition") target.read_partition(storage, "read.bin") assert_file_same( "write.bin", "read.bin", "Read contents of storage partition does not match source file contents" ) # Erase contents of the storage partition print("Erasing data partition") target.erase_partition(storage) # Read back the erased data partition print("Reading data partition") target.read_partition(storage, "read.bin") # Generate a file of all 0xFF generate_blanked_file(storage_info.size, "blank.bin") assert_file_same("blank.bin", "read.bin", "Contents of storage partition not fully erased") # Example end and cleanup print("\nPartition tool operations performed successfully!") clean_files = ["app.bin", "read.bin", "blank.bin", "write.bin"] for clean_file in clean_files: os.unlink(clean_file)
def main(): COMPONENTS_PATH = os.path.expandvars( os.path.join('$IDF_PATH', 'components')) PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, 'partition_table') sys.path.append(PARTTOOL_DIR) from gen_empty_partition import generate_blanked_file from parttool import PartitionName, PartitionType, ParttoolTarget parser = argparse.ArgumentParser('ESP-IDF Partitions Tool Example') 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')) args = parser.parse_args() target = ParttoolTarget(args.port) # Read app partition and save the contents to a file. The app partition is identified # using type-subtype combination 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') # Retrieve info on data storage partition, this time identifying it by name. storage = PartitionName('storage') storage_info = target.get_partition_info(storage) print('Found data partition at offset 0x{:x} with size 0x{:x}'.format( storage_info.offset, storage_info.size)) # Create a file whose contents will be written to the storage partition with open('write.bin', 'wb') as f: # Create a file to write to the data partition with randomly generated content f.write(os.urandom(storage_info.size)) # Write the contents of the created file to storage partition print('Writing to data partition') target.write_partition(storage, 'write.bin') # Read back the contents of the storage partition print('Reading data partition') target.read_partition(storage, 'read.bin') assert_file_same( 'write.bin', 'read.bin', 'Read contents of storage partition does not match source file contents' ) # Erase contents of the storage partition print('Erasing data partition') target.erase_partition(storage) # Read back the erased data partition print('Reading data partition') target.read_partition(storage, 'read.bin') # Generate a file of all 0xFF generate_blanked_file(storage_info.size, 'blank.bin') assert_file_same('blank.bin', 'read.bin', 'Contents of storage partition not fully erased') # Example end and cleanup print('\nPartition tool operations performed successfully!') clean_files = ['app.bin', 'read.bin', 'blank.bin', 'write.bin'] for clean_file in clean_files: os.unlink(clean_file)