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