parttool_example.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 subprocess
  22. import argparse
  23. IDF_PATH = os.path.expandvars("$IDF_PATH")
  24. PARTTOOL_PY = os.path.join(IDF_PATH, "components", "partition_table", "parttool.py")
  25. PARTITION_TABLE_OFFSET = 0x8000
  26. INVOKE_ARGS = [sys.executable, PARTTOOL_PY, "-q", "--partition-table-offset", str(PARTITION_TABLE_OFFSET)]
  27. def sized_file_compare(file1, file2):
  28. with open(file1, "rb") as f1:
  29. with open(file2, "rb") as f2:
  30. f1 = f1.read()
  31. f2 = f2.read()
  32. if len(f1) < len(f2):
  33. f2 = f2[:len(f1)]
  34. else:
  35. f1 = f1[:len(f2)]
  36. return f1 == f2
  37. def check(condition, message):
  38. if not condition:
  39. print("Error: " + message)
  40. sys.exit(1)
  41. def write_data_partition(size):
  42. print("Writing to data partition")
  43. with open("write.bin", "wb") as f:
  44. # Create a file to write to the data partition with randomly generated content
  45. f.write(os.urandom(int(size, 16)))
  46. # Invokes the command
  47. #
  48. # parttool.py --partition-table-offset 0x8000 -q --partition-name storage write_partition --input write.bin
  49. #
  50. # to write the contents of a file to a partition in the device.
  51. invoke_args = INVOKE_ARGS + ["--partition-name", "storage", "write_partition", "--input", f.name]
  52. subprocess.check_call(invoke_args)
  53. return f.name
  54. def read_data_partition():
  55. print("Reading data partition")
  56. # Invokes the command
  57. #
  58. # parttool.py --partition-table-offset 0x8000 -q --partition-name storage read_partition --output read.bin
  59. #
  60. # to read the contents of a partition in the device, which is then written to a file.
  61. f = "read.bin"
  62. invoke_args = INVOKE_ARGS + ["--partition-name", "storage", "read_partition", "--output", f]
  63. subprocess.check_call(invoke_args)
  64. return f
  65. def get_data_partition_info():
  66. print("Retrieving data partition offset and size")
  67. # Invokes the command
  68. #
  69. # parttool.py --partition-table-offset 0x8000 -q --partition-name storage get_partition_info --info offset size
  70. #
  71. # to get the offset and size of a partition named 'storage'.
  72. invoke_args = INVOKE_ARGS + ["--partition-name", "storage", "get_partition_info", "--info", "offset", "size"]
  73. (offset, size) = subprocess.check_output(invoke_args).strip().split(b" ")
  74. return (offset, size)
  75. def check_app(args):
  76. print("Checking if device app binary matches built binary")
  77. # Invokes the command
  78. #
  79. # parttool.py --partition-table-offset 0x8000 --partition-type app --partition-subtype factory read_partition --output app.bin"
  80. #
  81. # to read the app binary and write it to a file. The read app binary is compared to the built binary in the build folder.
  82. invoke_args = INVOKE_ARGS + ["--partition-type", "app", "--partition-subtype", "factory", "read_partition", "--output", "app.bin"]
  83. subprocess.check_call(invoke_args)
  84. app_same = sized_file_compare("app.bin", args.binary)
  85. check(app_same, "Device app binary does not match built binary")
  86. def check_partition_table():
  87. sys.path.append(os.path.join(IDF_PATH, "components", "partition_table"))
  88. import gen_esp32part as gen
  89. print("Checking if device partition table matches partition table csv")
  90. # Invokes the command
  91. #
  92. # parttool.py --partition-table-offset 0x8000 get_partition_info --table table.bin
  93. #
  94. # to read the device partition table and write it to a file. The read partition table is compared to
  95. # the partition table csv.
  96. invoke_args = INVOKE_ARGS + ["get_partition_info", "--table", "table.bin"]
  97. subprocess.check_call(invoke_args)
  98. with open("table.bin", "rb") as read:
  99. partition_table_csv = os.path.join(IDF_PATH, "examples", "storage", "parttool", "partitions_example.csv")
  100. with open(partition_table_csv, "r") as csv:
  101. read = gen.PartitionTable.from_binary(read.read())
  102. csv = gen.PartitionTable.from_csv(csv.read())
  103. check(read == csv, "Device partition table does not match csv partition table")
  104. def erase_data_partition():
  105. print("Erasing data partition")
  106. # Invokes the command
  107. #
  108. # parttool.py --partition-table-offset 0x8000 --partition-name storage erase_partition
  109. #
  110. # to erase the 'storage' partition.
  111. invoke_args = INVOKE_ARGS + ["--partition-name", "storage", "erase_partition"]
  112. subprocess.check_call(invoke_args)
  113. def generate_blank_data_file():
  114. print("Generating blank data partition file")
  115. # Invokes the command
  116. #
  117. # parttool.py --partition-table-offset 0x8000 --partition-name storage generate_blank_partition_file --output blank.bin
  118. #
  119. # to generate a blank partition file and write it to a file. The blank partition file has the same size as the
  120. # 'storage' partition.
  121. f = "blank.bin"
  122. invoke_args = INVOKE_ARGS + ["--partition-name", "storage", "generate_blank_partition_file", "--output", f]
  123. subprocess.check_call(invoke_args)
  124. return f
  125. def main():
  126. global INVOKE_ARGS
  127. parser = argparse.ArgumentParser("ESP-IDF Partitions Tool Example")
  128. parser.add_argument("--port", "-p", help="port where the device to perform operations on is connected")
  129. parser.add_argument("--binary", "-b", help="path to built example binary", default=os.path.join("build", "parttool.bin"))
  130. args = parser.parse_args()
  131. if args.port:
  132. INVOKE_ARGS += ["--port", args.port]
  133. # Before proceeding, do checks to verify whether the app and partition table in the device matches the built binary and
  134. # the generated partition table during build
  135. check_app(args)
  136. check_partition_table()
  137. # Get the offset and size of the data partition
  138. (offset, size) = get_data_partition_info()
  139. print("Found data partition at offset %s with size %s" % (offset, size))
  140. # Write a generated file of random bytes to the found data partition
  141. written = write_data_partition(size)
  142. # Read back the contents of the data partition
  143. read = read_data_partition()
  144. # Compare the written and read back data
  145. data_same = sized_file_compare(read, written)
  146. check(data_same, "Read contents of the data partition does not match written data")
  147. # Erase the data partition
  148. erase_data_partition()
  149. # Read back the erase data partition, which should be all 0xFF's after erasure
  150. read = read_data_partition()
  151. # Generate blank partition file (all 0xFF's)
  152. blank = generate_blank_data_file()
  153. # Verify that the partition has been erased by comparing the contents to the generated blank file
  154. data_same = sized_file_compare(read, blank)
  155. check(data_same, "Erased data partition contents does not match blank partition file")
  156. print("\nPartition tool operations performed successfully!")
  157. if __name__ == '__main__':
  158. main()