blob: 5e0a689f5a62f4eeb61f060e8001c97263311216 [file] [log] [blame]
Kevin Schoedel3d90cf82020-09-11 14:07:06 -04001#!/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-silabsa3786ac2023-11-14 21:02:59 -050015"""Flash an SILABS device.
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040016
17This is layered so that a caller can perform individual operations
18through an `Flasher` instance, or operations according to a command line.
19For `Flasher`, see the class documentation. For the parse_command()
20interface or standalone execution:
21
mkardous-silabsa3786ac2023-11-14 21:02:59 -050022usage: silabs_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
Kevin Schoedel36189332020-09-30 09:28:12 -040023 [--verify_application] [--reset] [--skip_reset]
Kevin Schoedel79727652020-11-10 17:28:43 -050024 [--commander FILE] [--device DEVICE]
Jonathan Mégevandc2e98a92021-10-25 18:42:27 +020025 [--serialno SERIAL] [--ip ADDRESS]
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040026
mkardous-silabsa3786ac2023-11-14 21:02:59 -050027Flash SILABS device
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040028
29optional arguments:
30 -h, --help show this help message and exit
31
32configuration:
Kevin Schoedel36189332020-09-30 09:28:12 -040033 --verbose, -v Report more verbosely
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040034 --commander FILE File name of the commander executable
Kevin Schoedel79727652020-11-10 17:28:43 -050035 --device DEVICE, -d DEVICE
36 Device family or platform to target
Kevin Schoedel36189332020-09-30 09:28:12 -040037 --serialno SERIAL, -s SERIAL
38 Serial number of device to flash
Jonathan Mégevandc2e98a92021-10-25 18:42:27 +020039 --ip ADDRESS, -a ADDRESS
40 Ip Address of the targeted flasher
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040041
42operations:
43 --erase Erase device
44 --application FILE Flash an image
Kevin Schoedel36189332020-09-30 09:28:12 -040045 --verify_application, --verify-application
46 Verify the image after flashing
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040047 --reset Reset device after flashing
Kevin Schoedel36189332020-09-30 09:28:12 -040048 --skip_reset, --skip-reset
49 Do not reset device after flashing
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040050"""
51
52import sys
53
54import 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-silabsa3786ac2023-11-14 21:02:59 -050058SILABS_OPTIONS = {
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040059 # 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 Schoedel36189332020-09-30 09:28:12 -040065 'argparse': {
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040066 'metavar': 'FILE'
67 },
Kevin Schoedel36189332020-09-30 09:28:12 -040068 'verify': ['{commander}', '--version'],
69 'error':
70 """\
71 Unable to execute {commander}.
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040072
Kevin Schoedel36189332020-09-30 09:28:12 -040073 Please ensure that this tool is installed and
mkardous-silabsa3786ac2023-11-14 21:02:59 -050074 available. See the SILABS example README for
Kevin Schoedel36189332020-09-30 09:28:12 -040075 installation instructions.
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040076
Kevin Schoedel36189332020-09-30 09:28:12 -040077 """,
78 },
Kevin Schoedel79727652020-11-10 17:28:43 -050079 'device': {
mkardous-silabs89fc8212022-09-26 09:29:04 -040080 'help': 'Device family or platform to target (EFR32 or MGM240)',
81 'default': None,
Kevin Schoedel79727652020-11-10 17:28:43 -050082 'alias': ['-d'],
83 'argparse': {
84 'metavar': 'DEVICE'
85 },
86 },
Kevin Schoedel36189332020-09-30 09:28:12 -040087 'serialno': {
88 'help': 'Serial number of device to flash',
89 'default': None,
90 'alias': ['-s'],
91 'argparse': {
92 'metavar': 'SERIAL'
93 },
Kevin Schoedel3d90cf82020-09-11 14:07:06 -040094 },
Jonathan Mégevandc2e98a92021-10-25 18:42:27 +020095 '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 Schoedel3d90cf82020-09-11 14:07:06 -0400103 },
104}
105
106
107class Flasher(firmware_utils.Flasher):
mkardous-silabsa3786ac2023-11-14 21:02:59 -0500108 """Manage silabs flashing."""
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400109
Kevin Schoedel36189332020-09-30 09:28:12 -0400110 def __init__(self, **options):
mkardous-silabsa3786ac2023-11-14 21:02:59 -0500111 super().__init__(platform='SILABS', module=__name__, **options)
112 self.define_options(SILABS_OPTIONS)
Kevin Schoedel36189332020-09-30 09:28:12 -0400113
114 # Common command line arguments for commander device subcommands.
Jonathan Mégevandc2e98a92021-10-25 18:42:27 +0200115 DEVICE_ARGUMENTS = [{'optional': 'serialno'}, {
116 'optional': 'ip'}, {'optional': 'device'}]
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400117
118 def erase(self):
119 """Perform `commander device masserase`."""
Kevin Schoedel36189332020-09-30 09:28:12 -0400120 return self.run_tool(
121 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
122 name='Erase device')
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400123
124 def verify(self, image):
125 """Verify image."""
Kevin Schoedel36189332020-09-30 09:28:12 -0400126 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 Schoedel3d90cf82020-09-11 14:07:06 -0400133
134 def flash(self, image):
135 """Flash image."""
Kevin Schoedel36189332020-09-30 09:28:12 -0400136 return self.run_tool(
137 'commander',
138 ['flash', self.DEVICE_ARGUMENTS, image],
139 name='Flash')
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400140
141 def reset(self):
142 """Reset the device."""
Kevin Schoedel36189332020-09-30 09:28:12 -0400143 return self.run_tool(
Song GUOf0315002021-08-12 23:31:58 +0800144 'commander',
145 ['device', 'reset', self.DEVICE_ARGUMENTS],
146 name='Reset')
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400147
148 def actions(self):
Kevin Schoedel36189332020-09-30 09:28:12 -0400149 """Perform actions on the device according to self.option."""
150 self.log(3, 'Options:', self.option)
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400151
Kevin Schoedel36189332020-09-30 09:28:12 -0400152 if self.option.erase:
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400153 if self.erase().err:
154 return self
155
Michael Spang69e96c92021-11-29 23:10:59 -0500156 if self.option.application:
157 application = self.option.application
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400158 if self.flash(application).err:
159 return self
Kevin Schoedel36189332020-09-30 09:28:12 -0400160 if self.option.verify_application:
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400161 if self.verify(application).err:
162 return self
Kevin Schoedel36189332020-09-30 09:28:12 -0400163 if self.option.reset is None:
164 self.option.reset = True
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400165
Kevin Schoedel36189332020-09-30 09:28:12 -0400166 if self.option.reset:
Kevin Schoedel3d90cf82020-09-11 14:07:06 -0400167 if self.reset().err:
168 return self
169
170 return self
171
172
173if __name__ == '__main__':
174 sys.exit(Flasher().flash_command(sys.argv))