otatool_example.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env python
  2. #
  3. # Demonstrates the use of otatool.py, a tool for performing ota 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. import serial
  24. import re
  25. IDF_PATH = os.path.expandvars("$IDF_PATH")
  26. OTATOOL_PY = os.path.join(IDF_PATH, "components", "app_update", "otatool.py")
  27. ESPTOOL_PY = os.path.join(IDF_PATH, "components", "esptool_py", "esptool", "esptool.py")
  28. INVOKE_ARGS = [sys.executable, OTATOOL_PY, "-q"]
  29. def sized_file_compare(file1, file2):
  30. with open(file1, "rb") as f1:
  31. with open(file2, "rb") as f2:
  32. f1 = f1.read()
  33. f2 = f2.read()
  34. if len(f1) < len(f2):
  35. f2 = f2[:len(f1)]
  36. else:
  37. f1 = f1[:len(f2)]
  38. return f1 == f2
  39. def check(condition, message):
  40. if not condition:
  41. print("Error: " + message)
  42. sys.exit(1)
  43. def flash_example_firmware_to_ota_partitions(args):
  44. # Invokes the command
  45. #
  46. # otatool.py -q write_ota_partition --slot <part_slot> or
  47. # otatool.py -q write_ota_partition --name <part_name>
  48. #
  49. # to write the contents of a file to the specified ota partition (either using name or the slot number)
  50. print("Writing factory firmware to ota_0")
  51. invoke_args = INVOKE_ARGS + ["write_ota_partition", "--slot", "0", "--input", args.binary]
  52. subprocess.check_call(invoke_args)
  53. print("Writing factory firmware to ota_1")
  54. invoke_args = INVOKE_ARGS + ["write_ota_partition", "--name", "ota_1", "--input", args.binary]
  55. subprocess.check_call(invoke_args)
  56. # Verify that the contents of the two ota slots are the same as that of the factory partition
  57. print("Checking written firmware to ota_0 and ota_1 match factory firmware")
  58. # Invokes the command
  59. #
  60. # otatool.py -q read_ota_partition --slot <part_slot> or
  61. # otatool.py -q read_ota_partition --name <part_name>
  62. #
  63. # to read the contents of a specified ota partition (either using name or the slot number) and write to a file
  64. invoke_args = INVOKE_ARGS + ["read_ota_partition", "--slot", "0", "--output", "app_0.bin"]
  65. subprocess.check_call(invoke_args)
  66. invoke_args = INVOKE_ARGS + ["read_ota_partition", "--name", "ota_1", "--output", "app_1.bin"]
  67. subprocess.check_call(invoke_args)
  68. ota_same = sized_file_compare("app_0.bin", args.binary)
  69. check(ota_same, "Slot 0 app does not match factory app")
  70. ota_same = sized_file_compare("app_1.bin", args.binary)
  71. check(ota_same, "Slot 1 app does not match factory app")
  72. def check_running_ota_partition(expected, port=None):
  73. # Monitor the serial output of target device. The firmware outputs the currently
  74. # running partition. It should match the partition the otatool switched to.
  75. if expected == 0 or expected == "ota_0":
  76. expected = b"ota_0"
  77. elif expected == 1 or expected == "ota_1":
  78. expected = b"ota_1"
  79. else:
  80. expected = b"factory"
  81. sys.path.append(os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool'))
  82. import esptool
  83. baud = os.environ.get("ESPTOOL_BAUD", esptool.ESPLoader.ESP_ROM_BAUD)
  84. if not port:
  85. # Check what esptool.py finds on what port the device is connected to
  86. output = subprocess.check_output([sys.executable, ESPTOOL_PY, "chip_id"])
  87. pattern = r"Serial port ([\S]+)"
  88. pattern = re.compile(pattern.encode())
  89. port = re.search(pattern, output).group(1)
  90. serial_instance = serial.serial_for_url(port.decode("utf-8"), baud, do_not_open=True)
  91. serial_instance.dtr = False
  92. serial_instance.rts = False
  93. serial_instance.rts = True
  94. serial_instance.open()
  95. serial_instance.rts = False
  96. # Read until example end and find the currently running partition string
  97. content = serial_instance.read_until(b"Example end")
  98. pattern = re.compile(b"Running partition: ([a-z0-9_]+)")
  99. running = re.search(pattern, content).group(1)
  100. check(expected == running, "Running partition %s does not match expected %s" % (running, expected))
  101. def switch_partition(part, port):
  102. if isinstance(part, int):
  103. spec = "slot"
  104. else:
  105. spec = "name"
  106. print("Switching to ota partition %s %s" % (spec, str(part)))
  107. if str(part) == "factory":
  108. # Invokes the command
  109. #
  110. # otatool.py -q erase_otadata
  111. #
  112. # to erase the otadata partition, effectively setting boot firmware to
  113. # factory
  114. subprocess.check_call(INVOKE_ARGS + ["erase_otadata"])
  115. else:
  116. # Invokes the command
  117. #
  118. # otatool.py -q switch_otadata --slot <part_slot> or
  119. # otatool.py -q switch_otadata --name <part_name>
  120. #
  121. # to switch to the indicated ota partition (either using name or the slot number)
  122. subprocess.check_call(INVOKE_ARGS + ["switch_otadata", "--" + spec, str(part)])
  123. check_running_ota_partition(part, port)
  124. def main():
  125. global INVOKE_ARGS
  126. parser = argparse.ArgumentParser("ESP-IDF OTA Tool Example")
  127. parser.add_argument("--port", "-p", help="port where the device to perform operations on is connected")
  128. parser.add_argument("--binary", "-b", help="path to built example binary", default=os.path.join("build", "otatool.bin"))
  129. args = parser.parse_args()
  130. if args.port:
  131. INVOKE_ARGS += ["--port", args.port]
  132. # Flash the factory firmware to all ota partitions
  133. flash_example_firmware_to_ota_partitions(args)
  134. # Perform switching ota partitions
  135. switch_partition("factory", args.port)
  136. switch_partition("factory", args.port) # check switching to factory partition twice in a row
  137. switch_partition(0, args.port)
  138. switch_partition("ota_1", args.port)
  139. switch_partition(1, args.port) # check switching to ota_1 partition twice in a row
  140. switch_partition("ota_0", args.port)
  141. switch_partition(0, args.port) # check switching to ota_0 partition twice in a row
  142. switch_partition("factory", args.port)
  143. switch_partition(1, args.port) # check switching to ota_1 partition from factory
  144. print("\nOTA tool operations executed successfully!")
  145. if __name__ == '__main__':
  146. main()