bl602_firmware_utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Flash an BL602 device.
  16. This is layered so that a caller can perform individual operations
  17. through an `Flasher` instance, or operations according to a command line.
  18. For `Flasher`, see the class documentation. For the parse_command()
  19. interface or standalone execution:
  20. usage: bl602_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
  21. [--verify_application] [--reset] [--skip_reset]
  22. [--commander FILE] [--device DEVICE]
  23. [--serialno SERIAL]
  24. Flash BL602 device
  25. optional arguments:
  26. -h, --help show this help message and exit
  27. configuration:
  28. --verbose, -v Report more verbosely
  29. --commander FILE File name of the commander executable
  30. --device DEVICE, -d DEVICE
  31. Device family or platform to target
  32. --serialno SERIAL, -s SERIAL
  33. Serial number of device to flash
  34. operations:
  35. --erase Erase device
  36. --application FILE Flash an image
  37. --verify_application, --verify-application
  38. Verify the image after flashing
  39. --reset Reset device after flashing
  40. --skip_reset, --skip-reset
  41. Do not reset device after flashing
  42. """
  43. import sys
  44. import firmware_utils
  45. # Additional options that can be use to configure an `Flasher`
  46. # object (as dictionary keys) and/or passed as command line options.
  47. BL602_OPTIONS = {
  48. # Configuration options define properties used in flashing operations.
  49. 'configuration': {
  50. # Tool configuration options.
  51. 'commander': {
  52. 'help': 'File name of the commander executable',
  53. 'default': 'commander',
  54. 'argparse': {
  55. 'metavar': 'FILE'
  56. },
  57. 'verify': ['{commander}', '--version'],
  58. 'error':
  59. """\
  60. Unable to execute {commander}.
  61. Please ensure that this tool is installed and
  62. available. See the BL602 example README for
  63. installation instructions.
  64. """,
  65. },
  66. 'device': {
  67. 'help': 'Device family or platform to target',
  68. 'default': 'BL602',
  69. 'alias': ['-d'],
  70. 'argparse': {
  71. 'metavar': 'DEVICE'
  72. },
  73. },
  74. 'serialno': {
  75. 'help': 'Serial number of device to flash',
  76. 'default': None,
  77. 'alias': ['-s'],
  78. 'argparse': {
  79. 'metavar': 'SERIAL'
  80. },
  81. },
  82. },
  83. }
  84. class Flasher(firmware_utils.Flasher):
  85. """Manage bl602 flashing."""
  86. def __init__(self, **options):
  87. super().__init__(platform='BL602', module=__name__, **options)
  88. self.define_options(BL602_OPTIONS)
  89. # Common command line arguments for commander device subcommands.
  90. DEVICE_ARGUMENTS = [{'optional': 'serialno'}, {'optional': 'device'}]
  91. def erase(self):
  92. """Perform `commander device masserase`."""
  93. return self.run_tool(
  94. 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
  95. name='Erase device')
  96. def verify(self, image):
  97. """Verify image."""
  98. return self.run_tool(
  99. 'commander',
  100. ['verify', self.DEVICE_ARGUMENTS, image],
  101. name='Verify',
  102. pass_message='Verified',
  103. fail_message='Not verified',
  104. fail_level=2)
  105. def flash(self, image):
  106. """Flash image."""
  107. return self.run_tool(
  108. 'commander',
  109. ['flash', self.DEVICE_ARGUMENTS, image],
  110. name='Flash')
  111. def reset(self):
  112. """Reset the device."""
  113. return self.run_tool(
  114. 'commander',
  115. ['device', 'reset', self.DEVICE_ARGUMENTS],
  116. name='Reset')
  117. def actions(self):
  118. """Perform actions on the device according to self.option."""
  119. self.log(3, 'Options:', self.option)
  120. if self.option.erase:
  121. if self.erase().err:
  122. return self
  123. application = self.optional_file(self.option.application)
  124. if application:
  125. if self.flash(application).err:
  126. return self
  127. if self.option.verify_application:
  128. if self.verify(application).err:
  129. return self
  130. if self.option.reset is None:
  131. self.option.reset = True
  132. if self.option.reset:
  133. if self.reset().err:
  134. return self
  135. return self
  136. if __name__ == '__main__':
  137. sys.exit(Flasher().flash_command(sys.argv))