Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2020 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. |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 15 | """Flash an SILABS device. |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 16 | |
| 17 | This is layered so that a caller can perform individual operations |
| 18 | through an `Flasher` instance, or operations according to a command line. |
| 19 | For `Flasher`, see the class documentation. For the parse_command() |
| 20 | interface or standalone execution: |
| 21 | |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 22 | usage: silabs_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE] |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 23 | [--verify_application] [--reset] [--skip_reset] |
Kevin Schoedel | 7972765 | 2020-11-10 17:28:43 -0500 | [diff] [blame] | 24 | [--commander FILE] [--device DEVICE] |
Jonathan Mégevand | c2e98a9 | 2021-10-25 18:42:27 +0200 | [diff] [blame] | 25 | [--serialno SERIAL] [--ip ADDRESS] |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 26 | |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 27 | Flash SILABS device |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 28 | |
| 29 | optional arguments: |
| 30 | -h, --help show this help message and exit |
| 31 | |
| 32 | configuration: |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 33 | --verbose, -v Report more verbosely |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 34 | --commander FILE File name of the commander executable |
Kevin Schoedel | 7972765 | 2020-11-10 17:28:43 -0500 | [diff] [blame] | 35 | --device DEVICE, -d DEVICE |
| 36 | Device family or platform to target |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 37 | --serialno SERIAL, -s SERIAL |
| 38 | Serial number of device to flash |
Jonathan Mégevand | c2e98a9 | 2021-10-25 18:42:27 +0200 | [diff] [blame] | 39 | --ip ADDRESS, -a ADDRESS |
| 40 | Ip Address of the targeted flasher |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 41 | |
| 42 | operations: |
| 43 | --erase Erase device |
| 44 | --application FILE Flash an image |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 45 | --verify_application, --verify-application |
| 46 | Verify the image after flashing |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 47 | --reset Reset device after flashing |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 48 | --skip_reset, --skip-reset |
| 49 | Do not reset device after flashing |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 50 | """ |
| 51 | |
| 52 | import sys |
| 53 | |
| 54 | import firmware_utils |
| 55 | |
| 56 | # Additional options that can be use to configure an `Flasher` |
| 57 | # object (as dictionary keys) and/or passed as command line options. |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 58 | SILABS_OPTIONS = { |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 59 | # Configuration options define properties used in flashing operations. |
| 60 | 'configuration': { |
| 61 | # Tool configuration options. |
| 62 | 'commander': { |
| 63 | 'help': 'File name of the commander executable', |
| 64 | 'default': 'commander', |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 65 | 'argparse': { |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 66 | 'metavar': 'FILE' |
| 67 | }, |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 68 | 'verify': ['{commander}', '--version'], |
| 69 | 'error': |
| 70 | """\ |
| 71 | Unable to execute {commander}. |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 72 | |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 73 | Please ensure that this tool is installed and |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 74 | available. See the SILABS example README for |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 75 | installation instructions. |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 76 | |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 77 | """, |
| 78 | }, |
Kevin Schoedel | 7972765 | 2020-11-10 17:28:43 -0500 | [diff] [blame] | 79 | 'device': { |
mkardous-silabs | 89fc821 | 2022-09-26 09:29:04 -0400 | [diff] [blame] | 80 | 'help': 'Device family or platform to target (EFR32 or MGM240)', |
| 81 | 'default': None, |
Kevin Schoedel | 7972765 | 2020-11-10 17:28:43 -0500 | [diff] [blame] | 82 | 'alias': ['-d'], |
| 83 | 'argparse': { |
| 84 | 'metavar': 'DEVICE' |
| 85 | }, |
| 86 | }, |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 87 | 'serialno': { |
| 88 | 'help': 'Serial number of device to flash', |
| 89 | 'default': None, |
| 90 | 'alias': ['-s'], |
| 91 | 'argparse': { |
| 92 | 'metavar': 'SERIAL' |
| 93 | }, |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 94 | }, |
Jonathan Mégevand | c2e98a9 | 2021-10-25 18:42:27 +0200 | [diff] [blame] | 95 | 'ip': { |
| 96 | 'help': 'Ip Address of the probe connected to the target', |
| 97 | 'default': None, |
| 98 | 'alias': ['-a'], |
| 99 | 'argparse': { |
| 100 | 'metavar': 'ADDRESS' |
| 101 | }, |
| 102 | }, |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 103 | }, |
| 104 | } |
| 105 | |
| 106 | |
| 107 | class Flasher(firmware_utils.Flasher): |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 108 | """Manage silabs flashing.""" |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 109 | |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 110 | def __init__(self, **options): |
mkardous-silabs | a3786ac | 2023-11-14 21:02:59 -0500 | [diff] [blame] | 111 | super().__init__(platform='SILABS', module=__name__, **options) |
| 112 | self.define_options(SILABS_OPTIONS) |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 113 | |
| 114 | # Common command line arguments for commander device subcommands. |
Jonathan Mégevand | c2e98a9 | 2021-10-25 18:42:27 +0200 | [diff] [blame] | 115 | DEVICE_ARGUMENTS = [{'optional': 'serialno'}, { |
| 116 | 'optional': 'ip'}, {'optional': 'device'}] |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 117 | |
| 118 | def erase(self): |
| 119 | """Perform `commander device masserase`.""" |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 120 | return self.run_tool( |
| 121 | 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS], |
| 122 | name='Erase device') |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 123 | |
| 124 | def verify(self, image): |
| 125 | """Verify image.""" |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 126 | return self.run_tool( |
| 127 | 'commander', |
| 128 | ['verify', self.DEVICE_ARGUMENTS, image], |
| 129 | name='Verify', |
| 130 | pass_message='Verified', |
| 131 | fail_message='Not verified', |
| 132 | fail_level=2) |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 133 | |
| 134 | def flash(self, image): |
| 135 | """Flash image.""" |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 136 | return self.run_tool( |
| 137 | 'commander', |
| 138 | ['flash', self.DEVICE_ARGUMENTS, image], |
| 139 | name='Flash') |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 140 | |
| 141 | def reset(self): |
| 142 | """Reset the device.""" |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 143 | return self.run_tool( |
Song GUO | f031500 | 2021-08-12 23:31:58 +0800 | [diff] [blame] | 144 | 'commander', |
| 145 | ['device', 'reset', self.DEVICE_ARGUMENTS], |
| 146 | name='Reset') |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 147 | |
| 148 | def actions(self): |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 149 | """Perform actions on the device according to self.option.""" |
| 150 | self.log(3, 'Options:', self.option) |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 151 | |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 152 | if self.option.erase: |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 153 | if self.erase().err: |
| 154 | return self |
| 155 | |
Michael Spang | 69e96c9 | 2021-11-29 23:10:59 -0500 | [diff] [blame] | 156 | if self.option.application: |
| 157 | application = self.option.application |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 158 | if self.flash(application).err: |
| 159 | return self |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 160 | if self.option.verify_application: |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 161 | if self.verify(application).err: |
| 162 | return self |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 163 | if self.option.reset is None: |
| 164 | self.option.reset = True |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 165 | |
Kevin Schoedel | 3618933 | 2020-09-30 09:28:12 -0400 | [diff] [blame] | 166 | if self.option.reset: |
Kevin Schoedel | 3d90cf8 | 2020-09-11 14:07:06 -0400 | [diff] [blame] | 167 | if self.reset().err: |
| 168 | return self |
| 169 | |
| 170 | return self |
| 171 | |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | sys.exit(Flasher().flash_command(sys.argv)) |