otatool_example.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 argparse
  22. from get_running_partition import get_running_partition
  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 assert_running_partition(expected, port=None):
  35. running = get_running_partition(port)
  36. if running != expected:
  37. raise Exception("Running partition %s does not match expected %s" % (running, expected))
  38. def main():
  39. COMPONENTS_PATH = os.path.expandvars(os.path.join("$IDF_PATH", "components"))
  40. OTATOOL_DIR = os.path.join(COMPONENTS_PATH, "app_update")
  41. sys.path.append(OTATOOL_DIR)
  42. from otatool import OtatoolTarget
  43. parser = argparse.ArgumentParser("ESP-IDF OTA Tool Example")
  44. parser.add_argument("--port", "-p", help="port where the device to perform operations on is connected")
  45. parser.add_argument("--binary", "-b", help="path to built example binary", default=os.path.join("build", "otatool.bin"))
  46. args = parser.parse_args()
  47. target = OtatoolTarget(args.port)
  48. print("Writing factory firmware to ota_0")
  49. target.write_ota_partition(0, args.binary)
  50. print("Writing factory firmware to ota_1")
  51. target.write_ota_partition("ota_1", args.binary)
  52. # Verify that the contents of the two ota slots are the same as that of the factory partition
  53. print("Checking written firmware to ota_0 and ota_1 match factory firmware")
  54. target.read_ota_partition("ota_0", "app0.bin")
  55. target.read_ota_partition(1, "app1.bin")
  56. assert_file_same("app0.bin", args.binary, "Slot 0 app does not match factory app")
  57. assert_file_same("app1.bin", args.binary, "Slot 1 app does not match factory app")
  58. # Switch to factory app
  59. print("Switching to factory app")
  60. target.erase_otadata()
  61. assert_running_partition("factory")
  62. # Switch to slot 0
  63. print("Switching to OTA slot 0")
  64. target.switch_ota_partition(0)
  65. assert_running_partition("ota_0")
  66. # Switch to slot 1 twice in a row
  67. print("Switching to OTA slot 1 (twice in a row)")
  68. target.switch_ota_partition(1)
  69. assert_running_partition("ota_1")
  70. target.switch_ota_partition("ota_1")
  71. assert_running_partition("ota_1")
  72. # Switch to slot 0 twice in a row
  73. print("Switching to OTA slot 0 (twice in a row)")
  74. target.switch_ota_partition(0)
  75. assert_running_partition("ota_0")
  76. target.switch_ota_partition("ota_0")
  77. assert_running_partition("ota_0")
  78. # Switch to factory app
  79. print("Switching to factory app")
  80. target.erase_otadata()
  81. assert_running_partition("factory")
  82. # Switch to slot 1
  83. print("Switching to OTA slot 1")
  84. target.switch_ota_partition(1)
  85. assert_running_partition("ota_1")
  86. # Example end and cleanup
  87. print("\nOTA tool operations executed successfully!")
  88. clean_files = ["app0.bin", "app1.bin"]
  89. for clean_file in clean_files:
  90. os.unlink(clean_file)
  91. if __name__ == '__main__':
  92. main()