1# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2021 Google LLC
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Entry-type module for BSS padding for tpl/u-boot-tpl.bin. This padding
6# can be added after the TPL binary to ensure that anything concatenated
7# to it will appear to TPL to be at the end of BSS rather than the start.
8#
9
10from binman import elf
11from binman.entry import Entry
12from binman.etype.blob import Entry_blob
13from u_boot_pylib import tools
14
15class Entry_u_boot_tpl_bss_pad(Entry_blob):
16    """U-Boot TPL binary padded with a BSS region
17
18    Properties / Entry arguments:
19        None
20
21    This holds the padding added after the TPL binary to cover the BSS (Block
22    Started by Symbol) region. This region holds the various variables used by
23    TPL. It is set to 0 by TPL when it starts up. If you want to append data to
24    the TPL image (such as a device tree file), you must pad out the BSS region
25    to avoid the data overlapping with U-Boot variables. This entry is useful in
26    that case. It automatically pads out the entry size to cover both the code,
27    data and BSS.
28
29    The contents of this entry will a certain number of zero bytes, determined
30    by __bss_size
31
32    The ELF file 'tpl/u-boot-tpl' must also be available for this to work, since
33    binman uses that to look up the BSS address.
34    """
35    def __init__(self, section, etype, node):
36        super().__init__(section, etype, node)
37
38    def ObtainContents(self):
39        fname = tools.get_input_filename('tpl/u-boot-tpl')
40        bss_size = elf.GetSymbolAddress(fname, '__bss_size')
41        if bss_size is None:
42            self.Raise('Expected __bss_size symbol in tpl/u-boot-tpl')
43        self.SetContents(tools.get_bytes(0, bss_size))
44        return True
45