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, pdi=None, bl31=None, dtb=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        self.pdi = pdi
28        self.bl31 = bl31
29        self.dtb = dtb
30
31    @classmethod
32    def name(cls):
33        return 'xsdb'
34
35    @classmethod
36    def capabilities(cls):
37        return RunnerCaps(flash_addr=True)
38
39    @classmethod
40    def do_add_parser(cls, parser):
41        parser.add_argument('--config', help='if given, override default config file')
42        parser.add_argument('--bitstream', help='path to the bitstream file')
43        parser.add_argument('--fsbl', help='path to the fsbl elf file')
44        parser.add_argument('--pdi', help='path to the base/boot pdi file')
45        parser.add_argument('--bl31', help='path to the bl31(ATF) elf file')
46        parser.add_argument('--system-dtb', help='path to the system.dtb file')
47
48    @classmethod
49    def do_create(
50        cls, cfg: RunnerConfig, args: argparse.Namespace
51    ) -> "XSDBBinaryRunner":
52        return XSDBBinaryRunner(cfg, config=args.config,
53                bitstream=args.bitstream, fsbl=args.fsbl,
54                pdi=args.pdi, bl31=args.bl31, dtb=args.system_dtb)
55
56    def do_run(self, command, **kwargs):
57        if self.bitstream and self.fsbl:
58            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.bitstream, self.fsbl]
59        elif self.bitstream:
60            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.bitstream]
61        elif self.fsbl:
62            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.fsbl]
63        elif self.pdi and self.bl31 and self.dtb:
64            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.pdi, self.bl31, self.dtb]
65        elif self.pdi and self.bl31:
66            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.pdi, self.bl31]
67        elif self.pdi:
68            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file, self.pdi]
69        else:
70            cmd = ['xsdb', self.xsdb_cfg_file, self.elf_file]
71        self.check_call(cmd)
72