1# Copyright (c) 2024 Advanced Micro Devices, Inc.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5"""Runner for flashing with xsdb CLI, the official programming
6   utility from AMD platforms.
7"""
8import argparse
9import os
10
11from runners.core import RunnerCaps, RunnerConfig, ZephyrBinaryRunner
12
13
14class XSDBBinaryRunner(ZephyrBinaryRunner):
15    def __init__(self, cfg: RunnerConfig, config=None, bitstream=None,
16            fsbl=None):
17        super().__init__(cfg)
18        self.elf_file = cfg.elf_file
19        if not config:
20            cfgfile_path = os.path.join(cfg.board_dir, 'support')
21            default = os.path.join(cfgfile_path, 'xsdb.cfg')
22            if os.path.exists(default):
23                config = default
24        self.xsdb_cfg_file = config
25        self.bitstream = bitstream
26        self.fsbl = fsbl
27
28    @classmethod
29    def name(cls):
30        return 'xsdb'
31
32    @classmethod
33    def capabilities(cls):
34        return RunnerCaps(flash_addr=True)
35
36    @classmethod
37    def do_add_parser(cls, parser):
38        parser.add_argument('--config', help='if given, override default config file')
39        parser.add_argument('--bitstream', help='path to the bitstream file')
40        parser.add_argument('--fsbl', help='path to the fsbl elf file')
41
42    @classmethod
43    def do_create(
44        cls, cfg: RunnerConfig, args: argparse.Namespace
45    ) -> "XSDBBinaryRunner":
46        return XSDBBinaryRunner(cfg, config=args.config,
47                bitstream=args.bitstream, fsbl=args.fsbl)
48
49    def do_run(self, command, **kwargs):
50        if self.bitstream and self.fsbl:
51            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.bitstream, self.fsbl]
52        elif self.bitstream:
53            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.bitstream]
54        elif self.fsbl:
55            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.fsbl]
56        else:
57            cmd = ['xsdb', self.xsdb_cfg_file]
58        self.check_call(cmd)
59