xref: /openbmc/openbmc/poky/meta/classes-recipe/uboot-extlinux-config.bbclass (revision 96e4b4e121e0e2da1535d7d537d6a982a6ff5bc0)
1# uboot-extlinux-config.bbclass
2#
3# This class allow the extlinux.conf generation for U-Boot use. The
4# U-Boot support for it is given to allow the Generic Distribution
5# Configuration specification use by OpenEmbedded-based products.
6#
7# External variables:
8#
9# UBOOT_EXTLINUX                   - Set to "1" to enable generation
10#                                    of extlinux.conf using this class.
11# UBOOT_EXTLINUX_CONSOLE           - Set to "console=ttyX" to change kernel boot
12#                                    default console.
13# UBOOT_EXTLINUX_LABELS            - A list of targets for the automatic config.
14# UBOOT_EXTLINUX_KERNEL_ARGS       - Add additional kernel arguments.
15# UBOOT_EXTLINUX_KERNEL_IMAGE      - Kernel image name.
16# UBOOT_EXTLINUX_FDTDIR            - Device tree directory.
17# UBOOT_EXTLINUX_FDT               - Device tree file.
18# UBOOT_EXTLINUX_FDTOVERLAYS       - Device tree overlay files. Space-separated list.
19# UBOOT_EXTLINUX_INITRD            - Indicates a list of filesystem images to
20#                                    concatenate and use as an initrd (optional).
21# UBOOT_EXTLINUX_MENU_DESCRIPTION  - Name to use as description.
22# UBOOT_EXTLINUX_ROOT              - Root kernel cmdline.
23# UBOOT_EXTLINUX_TIMEOUT           - Timeout before DEFAULT selection is made.
24#                                    Measured in 1/10 of a second.
25# UBOOT_EXTLINUX_DEFAULT_LABEL     - Target to be selected by default after
26#                                    the timeout period.
27# UBOOT_EXTLINUX_MENU_TITLE        - Menu title. If empty, MENU TITLE entry
28#                                    will not be added to the output file.
29# UBOOT_EXTLINUX_CONFIG            - Output file.
30#
31# If there's only one label system will boot automatically and menu won't be
32# created. If you want to use more than one labels, e.g linux and alternate,
33# use overrides to set menu description, console and others variables.
34#
35# Ex:
36#
37# UBOOT_EXTLINUX_LABELS ??= "default fallback"
38#
39# UBOOT_EXTLINUX_DEFAULT_LABEL ??= "Linux Default"
40# UBOOT_EXTLINUX_TIMEOUT ??= "30"
41#
42# UBOOT_EXTLINUX_KERNEL_IMAGE:default ??= "../zImage"
43# UBOOT_EXTLINUX_MENU_DESCRIPTION:default ??= "Linux Default"
44#
45# UBOOT_EXTLINUX_KERNEL_IMAGE:fallback ??= "../zImage-fallback"
46# UBOOT_EXTLINUX_MENU_DESCRIPTION:fallback ??= "Linux Fallback"
47#
48# Results:
49#
50# menu title Select the boot mode
51# TIMEOUT 30
52# DEFAULT Linux Default
53# LABEL Linux Default
54#   KERNEL ../zImage
55#   FDTDIR ../
56#   APPEND root=/dev/mmcblk2p2 rootwait rw console=${console}
57# LABEL Linux Fallback
58#   KERNEL ../zImage-fallback
59#   FDTDIR ../
60#   APPEND root=/dev/mmcblk2p2 rootwait rw console=${console}
61#
62# Copyright (C) 2016, O.S. Systems Software LTDA.  All Rights Reserved
63# SPDX-License-Identifier: MIT
64#
65# The kernel has an internal default console, which you can override with
66# a console=...some_tty...
67UBOOT_EXTLINUX_CONSOLE ??= "console=${console},${baudrate}"
68UBOOT_EXTLINUX_LABELS ??= "linux"
69UBOOT_EXTLINUX_FDT ??= ""
70UBOOT_EXTLINUX_FDTOVERLAYS ??= ""
71UBOOT_EXTLINUX_FDTDIR ??= "../"
72UBOOT_EXTLINUX_KERNEL_IMAGE ??= "../${KERNEL_IMAGETYPE}"
73UBOOT_EXTLINUX_KERNEL_ARGS ??= "rootwait rw"
74UBOOT_EXTLINUX_MENU_DESCRIPTION:linux ??= "${DISTRO_NAME}"
75UBOOT_EXTLINUX_MENU_TITLE ??= "Select the boot mode"
76
77UBOOT_EXTLINUX_CONFIG = "${B}/extlinux.conf"
78
79python do_create_extlinux_config() {
80    if d.getVar("UBOOT_EXTLINUX") != "1":
81      return
82
83    if not d.getVar('WORKDIR'):
84        bb.error("WORKDIR not defined, unable to package")
85
86    labels = d.getVar('UBOOT_EXTLINUX_LABELS')
87    if not labels:
88        bb.fatal("UBOOT_EXTLINUX_LABELS not defined, nothing to do")
89
90    if not labels.strip():
91        bb.fatal("No labels, nothing to do")
92
93    cfile = d.getVar('UBOOT_EXTLINUX_CONFIG')
94    if not cfile:
95        bb.fatal('Unable to read UBOOT_EXTLINUX_CONFIG')
96
97    localdata = bb.data.createCopy(d)
98
99    try:
100        with open(cfile, 'w') as cfgfile:
101            cfgfile.write('# Generic Distro Configuration file generated by OpenEmbedded\n')
102
103            menu_title = localdata.getVar('UBOOT_EXTLINUX_MENU_TITLE')
104            if len(labels.split()) > 1 and menu_title:
105                cfgfile.write('MENU TITLE %s\n' % (menu_title))
106
107            timeout = localdata.getVar('UBOOT_EXTLINUX_TIMEOUT')
108            if timeout:
109                cfgfile.write('TIMEOUT %s\n' % (timeout))
110
111            if len(labels.split()) > 1:
112                default = localdata.getVar('UBOOT_EXTLINUX_DEFAULT_LABEL')
113                if default:
114                    cfgfile.write('DEFAULT %s\n' % (default))
115
116            # Need to deconflict the labels with existing overrides
117            label_overrides = labels.split()
118            default_overrides = localdata.getVar('OVERRIDES').split(':')
119            # We're keeping all the existing overrides that aren't used as a label
120            # an override for that label will be added back in while we're processing that label
121            keep_overrides = list(filter(lambda x: x not in label_overrides, default_overrides))
122
123            for label in labels.split():
124
125                localdata.setVar('OVERRIDES', ':'.join(keep_overrides + [label]))
126
127                extlinux_console = localdata.getVar('UBOOT_EXTLINUX_CONSOLE')
128
129                menu_description = localdata.getVar('UBOOT_EXTLINUX_MENU_DESCRIPTION')
130                if not menu_description:
131                    menu_description = label
132
133                root = localdata.getVar('UBOOT_EXTLINUX_ROOT')
134                if not root:
135                    bb.fatal('UBOOT_EXTLINUX_ROOT not defined')
136
137                kernel_image = localdata.getVar('UBOOT_EXTLINUX_KERNEL_IMAGE')
138                fdtdir = localdata.getVar('UBOOT_EXTLINUX_FDTDIR')
139
140                fdt = localdata.getVar('UBOOT_EXTLINUX_FDT')
141                fdtoverlays = localdata.getVar('UBOOT_EXTLINUX_FDTOVERLAYS')
142
143                cfgfile.write('LABEL %s\n\tKERNEL %s\n' % (menu_description, kernel_image))
144
145                if fdt:
146                    cfgfile.write('\tFDT %s\n' % (fdt))
147                elif fdtdir:
148                    cfgfile.write('\tFDTDIR %s\n' % (fdtdir))
149
150                if fdtoverlays:
151                    cfgfile.write('\tFDTOVERLAYS %s\n' % (' '.join(fdtoverlays.split())))
152
153                kernel_args = localdata.getVar('UBOOT_EXTLINUX_KERNEL_ARGS')
154
155                initrd = localdata.getVar('UBOOT_EXTLINUX_INITRD')
156                if initrd:
157                    cfgfile.write('\tINITRD %s\n'% initrd)
158
159                kernel_args = root + " " + kernel_args
160                cfgfile.write('\tAPPEND %s %s\n' % (kernel_args, extlinux_console))
161
162    except OSError:
163        bb.fatal('Unable to open %s' % (cfile))
164}
165UBOOT_EXTLINUX_VARS = "CONSOLE MENU_DESCRIPTION ROOT KERNEL_IMAGE FDTDIR FDT FDTOVERLAYS KERNEL_ARGS INITRD"
166do_create_extlinux_config[vardeps] += "${@' '.join(['UBOOT_EXTLINUX_%s:%s' % (v, l) for v in d.getVar('UBOOT_EXTLINUX_VARS').split() for l in d.getVar('UBOOT_EXTLINUX_LABELS').split()])}"
167do_create_extlinux_config[vardepsexclude] += "OVERRIDES"
168
169addtask create_extlinux_config before do_install do_deploy after do_compile
170