parttool_example.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. #
  3. # Demonstrates the use of parttool.py, a tool for performing partition level
  4. # operations.
  5. #
  6. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http:#www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. import os
  20. import sys
  21. import argparse
  22. PARTITION_TABLE_DIR = os.path.join("components", "partition_table", "")
  23. def assert_file_same(file1, file2, err):
  24. with open(file1, "rb") as f1:
  25. with open(file2, "rb") as f2:
  26. f1 = f1.read()
  27. f2 = f2.read()
  28. if len(f1) < len(f2):
  29. f2 = f2[:len(f1)]
  30. else:
  31. f1 = f1[:len(f2)]
  32. if not f1 == f2:
  33. raise Exception(err)
  34. def main():
  35. COMPONENTS_PATH = os.path.expandvars(os.path.join("$IDF_PATH", "components"))
  36. PARTTOOL_DIR = os.path.join(COMPONENTS_PATH, "partition_table")
  37. sys.path.append(PARTTOOL_DIR)
  38. from parttool import PartitionName, PartitionType, ParttoolTarget
  39. from gen_empty_partition import generate_blanked_file
  40. parser = argparse.ArgumentParser("ESP-IDF Partitions Tool Example")
  41. parser.add_argument("--port", "-p", help="port where the device to perform operations on is connected")
  42. parser.add_argument("--binary", "-b", help="path to built example binary", default=os.path.join("build", "parttool.bin"))
  43. args = parser.parse_args()
  44. target = ParttoolTarget(args.port)
  45. # Read app partition and save the contents to a file. The app partition is identified
  46. # using type-subtype combination
  47. print("Checking if device app binary matches built binary")
  48. factory = PartitionType("app", "factory")
  49. target.read_partition(factory, "app.bin")
  50. assert_file_same(args.binary, "app.bin", "Device app binary does not match built binary")
  51. # Retrieve info on data storage partition, this time identifying it by name.
  52. storage = PartitionName("storage")
  53. storage_info = target.get_partition_info(storage)
  54. print("Found data partition at offset 0x{:x} with size 0x{:x}".format(storage_info.offset, storage_info.size))
  55. # Create a file whose contents will be written to the storage partition
  56. with open("write.bin", "wb") as f:
  57. # Create a file to write to the data partition with randomly generated content
  58. f.write(os.urandom(storage_info.size))
  59. # Write the contents of the created file to storage partition
  60. print("Writing to data partition")
  61. target.write_partition(storage, "write.bin")
  62. # Read back the contents of the storage partition
  63. print("Reading data partition")
  64. target.read_partition(storage, "read.bin")
  65. assert_file_same("write.bin", "read.bin", "Read contents of storage partition does not match source file contents")
  66. # Erase contents of the storage partition
  67. print("Erasing data partition")
  68. target.erase_partition(storage)
  69. # Read back the erased data partition
  70. print("Reading data partition")
  71. target.read_partition(storage, "read.bin")
  72. # Generate a file of all 0xFF
  73. generate_blanked_file(storage_info.size, "blank.bin")
  74. assert_file_same("blank.bin", "read.bin", "Contents of storage partition not fully erased")
  75. # Example end and cleanup
  76. print("\nPartition tool operations performed successfully!")
  77. clean_files = ["app.bin", "read.bin", "blank.bin", "write.bin"]
  78. for clean_file in clean_files:
  79. os.unlink(clean_file)
  80. if __name__ == '__main__':
  81. main()