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