qpg_firmware_utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020-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 a QPG device on a DK board.
  16. Currently only support CMSIS mBed Drag and Drop for public use.
  17. usage: qpg_firmware_utils.py [-h] [--application FILE] [--drive DRIVE]
  18. optional arguments:
  19. -h, --help show this help message and exit
  20. configuration:
  21. --verbose Report more verbosely
  22. --drive Connected mBed USB mount to copy to
  23. operations:
  24. --application FILE Flash an image through drag and drop
  25. """
  26. import os
  27. import shutil
  28. import sys
  29. import firmware_utils
  30. # Additional options that can be use to configure an `Flasher`
  31. # object (as dictionary keys) and/or passed as command line options.
  32. QPG_OPTIONS = {
  33. # Configuration options define properties used in flashing operations.
  34. 'configuration': {
  35. # Tool configuration options.
  36. 'drive': {
  37. 'help': 'Location of the mBed mount',
  38. 'default': '/mnt/e',
  39. 'alias': ['-d'],
  40. 'argparse': {
  41. 'metavar': 'FILE'
  42. },
  43. },
  44. },
  45. }
  46. class Flasher(firmware_utils.Flasher):
  47. """Manage flashing."""
  48. def __init__(self, **options):
  49. super().__init__(platform='QPG', module=__name__, **options)
  50. self.define_options(QPG_OPTIONS)
  51. def erase(self):
  52. """Not supported"""
  53. self.log(0, "Erase not supported")
  54. return self
  55. def verify(self, image):
  56. """Not supported"""
  57. self.log(0, "Verify not supported")
  58. return self
  59. def flash(self, image):
  60. """Flash image."""
  61. self.log(1, "Copying to {} drive {}".format(
  62. image, self.option.drive or "None"))
  63. if not self.option.drive:
  64. self.log(0, "--drive or -d required for copy action")
  65. self.err = 1
  66. return self
  67. # Check for drive mount
  68. if not os.path.exists(self.option.drive):
  69. self.log(0, "Drive '{}' does not exist. Is the USB device mounted correctly ?".format(
  70. self.option.drive))
  71. self.err = 2
  72. return self
  73. # Check for valid mBed device
  74. mbed_marker = os.path.join(self.option.drive, 'MBED.HTM')
  75. if not os.path.exists(mbed_marker):
  76. self.log(0, "Drive '{}' not a path to an MBED device".format(
  77. self.option.drive))
  78. self.err = 3
  79. return self
  80. shutil.copyfile(image, os.path.join(
  81. self.option.drive, os.path.basename(image)))
  82. return self
  83. def reset(self):
  84. """Not supported"""
  85. self.log(0, "Reset is triggered automatically after completion of mBed copy.")
  86. return self
  87. def actions(self):
  88. """Perform actions on the device according to self.option."""
  89. self.log(3, 'Options:', self.option)
  90. if self.option.erase:
  91. if self.erase().err:
  92. return self
  93. if self.option.application:
  94. application = self.option.application
  95. if self.flash(application).err:
  96. return self
  97. if self.option.verify_application:
  98. if self.verify(application).err:
  99. return self
  100. if self.option.reset is None:
  101. self.option.reset = True
  102. if self.option.reset:
  103. if self.reset().err:
  104. return self
  105. return self
  106. if __name__ == '__main__':
  107. sys.exit(Flasher().flash_command(sys.argv))