1# Handle U-Boot config for a machine
2#
3# The format to specify it, in the machine, is:
4#
5# UBOOT_CONFIG ??= <default>
6# UBOOT_CONFIG[foo] = "config,images,binary"
7#
8# or
9#
10# UBOOT_MACHINE = "config"
11#
12# Copyright 2013, 2014 (C) O.S. Systems Software LTDA.
13#
14# SPDX-License-Identifier: MIT
15
16
17def removesuffix(s, suffix):
18    if suffix and s.endswith(suffix):
19        return s[:-len(suffix)]
20    return s
21
22UBOOT_ENTRYPOINT ?= "20008000"
23UBOOT_LOADADDRESS ?= "${UBOOT_ENTRYPOINT}"
24
25# Some versions of u-boot use .bin and others use .img.  By default use .bin
26# but enable individual recipes to change this value.
27UBOOT_SUFFIX ??= "bin"
28UBOOT_BINARY ?= "u-boot.${UBOOT_SUFFIX}"
29UBOOT_BINARYNAME ?= "${@os.path.splitext(d.getVar("UBOOT_BINARY"))[0]}"
30UBOOT_IMAGE ?= "${UBOOT_BINARYNAME}-${MACHINE}-${PV}-${PR}.${UBOOT_SUFFIX}"
31UBOOT_SYMLINK ?= "${UBOOT_BINARYNAME}-${MACHINE}.${UBOOT_SUFFIX}"
32UBOOT_MAKE_TARGET ?= "all"
33
34# Output the ELF generated. Some platforms can use the ELF file and directly
35# load it (JTAG booting, QEMU) additionally the ELF can be used for debugging
36# purposes.
37UBOOT_ELF ?= ""
38UBOOT_ELF_SUFFIX ?= "elf"
39UBOOT_ELF_IMAGE ?= "u-boot-${MACHINE}-${PV}-${PR}.${UBOOT_ELF_SUFFIX}"
40UBOOT_ELF_BINARY ?= "u-boot.${UBOOT_ELF_SUFFIX}"
41UBOOT_ELF_SYMLINK ?= "u-boot-${MACHINE}.${UBOOT_ELF_SUFFIX}"
42
43# Some versions of u-boot build an SPL (Second Program Loader) image that
44# should be packaged along with the u-boot binary as well as placed in the
45# deploy directory.  For those versions they can set the following variables
46# to allow packaging the SPL.
47SPL_SUFFIX ?= ""
48SPL_BINARY ?= ""
49SPL_DELIMITER  ?= "${@'.' if d.getVar("SPL_SUFFIX") else ''}"
50SPL_BINARYFILE ?= "${@os.path.basename(d.getVar("SPL_BINARY"))}"
51SPL_BINARYNAME ?= "${@removesuffix(d.getVar("SPL_BINARYFILE"), "." + d.getVar("SPL_SUFFIX"))}"
52SPL_IMAGE ?= "${SPL_BINARYNAME}-${MACHINE}-${PV}-${PR}${SPL_DELIMITER}${SPL_SUFFIX}"
53SPL_SYMLINK ?= "${SPL_BINARYNAME}-${MACHINE}${SPL_DELIMITER}${SPL_SUFFIX}"
54
55# Additional environment variables or a script can be installed alongside
56# u-boot to be used automatically on boot.  This file, typically 'uEnv.txt'
57# or 'boot.scr', should be packaged along with u-boot as well as placed in the
58# deploy directory.  Machine configurations needing one of these files should
59# include it in the SRC_URI and set the UBOOT_ENV parameter.
60UBOOT_ENV_SUFFIX ?= "txt"
61UBOOT_ENV ?= ""
62UBOOT_ENV_SRC_SUFFIX ?= "cmd"
63UBOOT_ENV_SRC ?= "${UBOOT_ENV}.${UBOOT_ENV_SRC_SUFFIX}"
64UBOOT_ENV_BINARY ?= "${UBOOT_ENV}.${UBOOT_ENV_SUFFIX}"
65UBOOT_ENV_IMAGE ?= "${UBOOT_ENV}-${MACHINE}-${PV}-${PR}.${UBOOT_ENV_SUFFIX}"
66UBOOT_ENV_SYMLINK ?= "${UBOOT_ENV}-${MACHINE}.${UBOOT_ENV_SUFFIX}"
67
68# U-Boot EXTLINUX variables. U-Boot searches for /boot/extlinux/extlinux.conf
69# to find EXTLINUX conf file.
70UBOOT_EXTLINUX_INSTALL_DIR ?= "/boot/extlinux"
71UBOOT_EXTLINUX_CONF_NAME ?= "extlinux.conf"
72UBOOT_EXTLINUX_SYMLINK ?= "${UBOOT_EXTLINUX_CONF_NAME}-${MACHINE}-${PR}"
73
74# Options for the device tree compiler passed to mkimage '-D' feature:
75UBOOT_MKIMAGE_DTCOPTS ??= ""
76SPL_MKIMAGE_DTCOPTS ??= ""
77
78# mkimage command
79UBOOT_MKIMAGE ?= "uboot-mkimage"
80UBOOT_MKIMAGE_SIGN ?= "${UBOOT_MKIMAGE}"
81
82# Signature activation - this requires KERNEL_IMAGETYPE = "fitImage"
83UBOOT_SIGN_ENABLE ?= "0"
84
85# Arguments passed to mkimage for signing
86UBOOT_MKIMAGE_SIGN_ARGS ?= ""
87SPL_MKIMAGE_SIGN_ARGS ?= ""
88
89# Options to deploy the u-boot device tree
90UBOOT_DTB ?= ""
91UBOOT_DTB_BINARY ??= ""
92
93# uboot-fit_check_sign command
94UBOOT_FIT_CHECK_SIGN ?= "uboot-fit_check_sign"
95
96python () {
97    ubootmachine = d.getVar("UBOOT_MACHINE")
98    ubootconfigflags = d.getVarFlags('UBOOT_CONFIG')
99    ubootbinary = d.getVar('UBOOT_BINARY')
100    ubootbinaries = d.getVar('UBOOT_BINARIES')
101    # The "doc" varflag is special, we don't want to see it here
102    ubootconfigflags.pop('doc', None)
103    ubootconfig = (d.getVar('UBOOT_CONFIG') or "").split()
104
105    if not ubootmachine and not ubootconfig:
106        PN = d.getVar("PN")
107        FILE = os.path.basename(d.getVar("FILE"))
108        bb.debug(1, "To build %s, see %s for instructions on \
109                 setting up your machine config" % (PN, FILE))
110        raise bb.parse.SkipRecipe("Either UBOOT_MACHINE or UBOOT_CONFIG must be set in the %s machine configuration." % d.getVar("MACHINE"))
111
112    if ubootmachine and ubootconfig:
113        raise bb.parse.SkipRecipe("You cannot use UBOOT_MACHINE and UBOOT_CONFIG at the same time.")
114
115    if ubootconfigflags and ubootbinaries:
116        raise bb.parse.SkipRecipe("You cannot use UBOOT_BINARIES as it is internal to uboot_config.bbclass.")
117
118    if len(ubootconfig) > 0:
119        for config in ubootconfig:
120            found = False
121            for f, v in ubootconfigflags.items():
122                if config == f:
123                    found = True
124                    items = v.split(',')
125                    if items[0] and len(items) > 3:
126                        raise bb.parse.SkipRecipe('Only config,images,binary can be specified!')
127                    d.appendVar('UBOOT_MACHINE', ' ' + items[0])
128                    # IMAGE_FSTYPES appending
129                    if len(items) > 1 and items[1]:
130                        bb.debug(1, "Appending '%s' to IMAGE_FSTYPES." % items[1])
131                        d.appendVar('IMAGE_FSTYPES', ' ' + items[1])
132                    if len(items) > 2 and items[2]:
133                        bb.debug(1, "Appending '%s' to UBOOT_BINARIES." % items[2])
134                        d.appendVar('UBOOT_BINARIES', ' ' + items[2])
135                    else:
136                        bb.debug(1, "Appending '%s' to UBOOT_BINARIES." % ubootbinary)
137                        d.appendVar('UBOOT_BINARIES', ' ' + ubootbinary)
138                    break
139
140            if not found:
141                raise bb.parse.SkipRecipe("The selected UBOOT_CONFIG key %s has no match in %s." % (ubootconfig, ubootconfigflags.keys()))
142
143            if len(ubootconfig) == 1:
144                d.setVar('KCONFIG_CONFIG_ROOTDIR', os.path.join(d.getVar("B"), d.getVar("UBOOT_MACHINE").strip()))
145            else:
146                # Disable menuconfig for multiple configs
147                d.setVar('KCONFIG_CONFIG_ENABLE_MENUCONFIG', "false")
148}
149