1# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2022 Google LLC
3#
4"""Bintool implementation for fiptool
5
6fiptool provides a way to package firmware in an ARM Trusted Firmware Firmware
7Image Package (ATF FIP) format. It is used with Trusted Firmware A, for example.
8
9Documentation is at:
10https://trustedfirmware-a.readthedocs.io/en/latest/getting_started/tools-build.html?highlight=fiptool#building-and-using-the-fip-tool
11
12Source code is at:
13https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git
14
15Here is the help:
16
17usage: fiptool [--verbose] <command> [<args>]
18Global options supported:
19  --verbose	Enable verbose output for all commands.
20
21Commands supported:
22  info		List images contained in FIP.
23  create	Create a new FIP with the given images.
24  update	Update an existing FIP with the given images.
25  unpack	Unpack images from FIP.
26  remove	Remove images from FIP.
27  version	Show fiptool version.
28  help		Show help for given command.
29
30"""
31
32from binman import bintool
33
34class Bintoolfiptool(bintool.Bintool):
35    """Image generation for ARM Trusted Firmware
36
37    This bintool supports running `fiptool` with some basic parameters as
38    neeed by binman.
39
40    It also supports build fiptool from source.
41
42    fiptool provides a way to package firmware in an ARM Trusted Firmware
43    Firmware Image Package (ATF FIP) format. It is used with Trusted Firmware A,
44    for example.
45
46    See `TF-A FIP tool documentation`_ for more information.
47
48    .. _`TF-A FIP tool documentation`:
49        https://trustedfirmware-a.readthedocs.io/en/latest/getting_started/tools-build.html?highlight=fiptool#building-and-using-the-fip-tool
50    """
51    def __init__(self, name):
52        super().__init__(name, 'Manipulate ATF FIP files', r'^(.*)$', 'version')
53
54    def info(self, fname):
55        """Get info on a FIP image
56
57        Args:
58            fname (str): Filename to check
59
60        Returns:
61            str: Tool output
62        """
63        args = ['info', fname]
64        return self.run_cmd(*args)
65
66    # pylint: disable=R0913
67    def create_new(self, fname, align, plat_toc_flags, fwu, tb_fw, blob_uuid,
68                   blob_file):
69        """Create a new FIP
70
71        Args:
72            fname (str): Filename to write to
73            align (int): Alignment to use for entries
74            plat_toc_flags (int): Flags to use for the TOC header
75            fwu (str): Filename for the fwu entry
76            tb_fw (str): Filename for the tb_fw entry
77            blob_uuid (str): UUID for the blob entry
78            blob_file (str): Filename for the blob entry
79
80        Returns:
81            str: Tool output
82        """
83        args = [
84            'create',
85            '--align', f'{align:x}',
86            '--plat-toc-flags', f'{plat_toc_flags:#x}',
87            '--fwu', fwu,
88            '--tb-fw', tb_fw,
89            '--blob', f'uuid={blob_uuid},file={blob_file}',
90            fname]
91        return self.run_cmd(*args)
92
93    def create_bad(self):
94        """Run fiptool with invalid arguments"""
95        args = ['create', '--fred']
96        return self.run_cmd_result(*args)
97
98    def fetch(self, method):
99        """Fetch handler for fiptool
100
101        This builds the tool from source
102
103        Returns:
104            tuple:
105                str: Filename of fetched file to copy to a suitable directory
106                str: Name of temp directory to remove, or None
107        """
108        if method != bintool.FETCH_BUILD:
109            return None
110        result = self.build_from_git(
111            'https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git',
112            ['fiptool'],
113            'tools/fiptool/fiptool')
114        return result
115