1# Copyright (c) 2019, Nordic Semiconductor ASA
2#
3# SPDX-License-Identifier: Apache-2.0
4
5'''Catch-all module for miscellaneous devices which can't use a
6generic or widely used tool like J-Link, OpenOCD, etc.
7
8Please use this sparingly and only when your setup is exotic and
9you're willing to handle requests for help. E.g. if your "board" is a
10core on a special-purpose SoC which requires a complicated script to
11network boot.'''
12
13import argparse
14
15from runners.core import RunnerCaps, ZephyrBinaryRunner
16
17
18class MiscFlasher(ZephyrBinaryRunner):
19    '''Runner for handling special purpose flashing commands.'''
20
21    def __init__(self, cfg, cmd, args):
22        super().__init__(cfg)
23        if not cmd:
24            # This is a board definition error, not a user error,
25            # so we can do it now and not in do_run().
26            raise ValueError('no command was given')
27        self.cmd = cmd
28        self.args = args
29
30    @classmethod
31    def name(cls):
32        return 'misc-flasher'
33
34    @classmethod
35    def capabilities(cls):
36        return RunnerCaps(commands={'flash'})
37
38    @classmethod
39    def do_add_parser(cls, parser):
40        parser.add_argument('cmd',
41                            help='''command to run; it will be passed the
42                            build directory as its first argument''')
43        parser.add_argument('args', nargs=argparse.REMAINDER,
44                            help='''additional arguments to pass after the build
45                            directory''')
46
47    @classmethod
48    def do_create(cls, cfg, args):
49        return MiscFlasher(cfg, args.cmd, args.args)
50
51    def do_run(self, *args, **kwargs):
52        self.require(self.cmd)
53        self.check_call([self.cmd, self.cfg.build_dir] + self.args)
54