get_running_partition.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 serial
  22. import subprocess
  23. import re
  24. import argparse
  25. from subprocess import CalledProcessError
  26. def get_running_partition(port=None):
  27. # Monitor the serial output of target device. The firmware outputs the currently
  28. # running partition
  29. IDF_PATH = os.path.expandvars("$IDF_PATH")
  30. sys.path.append(os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool'))
  31. import esptool
  32. ESPTOOL_PY = os.path.join(IDF_PATH, "components", "esptool_py", "esptool", "esptool.py")
  33. baud = os.environ.get("ESPTOOL_BAUD", esptool.ESPLoader.ESP_ROM_BAUD)
  34. if not port:
  35. error_message = "Unable to obtain default target device port.\nSerial log:\n\n"
  36. try:
  37. # Check what esptool.py finds on what port the device is connected to
  38. output = subprocess.check_output([sys.executable, ESPTOOL_PY, "chip_id"]) # may raise CalledProcessError
  39. pattern = r"Serial port ([\S]+)"
  40. pattern = re.compile(pattern.encode())
  41. port = re.search(pattern, output).group(1) # may raise AttributeError
  42. except CalledProcessError as e:
  43. raise Exception(error_message + e.output)
  44. except AttributeError:
  45. raise Exception(error_message + output)
  46. serial_instance = serial.serial_for_url(port.decode("utf-8"), baud, do_not_open=True)
  47. serial_instance.dtr = False
  48. serial_instance.rts = False
  49. serial_instance.rts = True
  50. serial_instance.open()
  51. serial_instance.rts = False
  52. # Read until example end and find the currently running partition string
  53. content = serial_instance.read_until(b"Example end")
  54. pattern = re.compile(b"Running partition: ([a-z0-9_]+)")
  55. running = re.search(pattern, content).group(1)
  56. return running.decode("utf-8")
  57. def main():
  58. parser = argparse.ArgumentParser()
  59. parser.add_argument("--port", default=None)
  60. args = parser.parse_args()
  61. try:
  62. res = get_running_partition(args.port)
  63. except Exception as e:
  64. print(e.message)
  65. sys.exit(1)
  66. print(res)
  67. if __name__ == "__main__":
  68. main()