1# grub-efi.bbclass
2# Copyright (c) 2011, Intel Corporation.
3#
4# SPDX-License-Identifier: MIT
5
6# Provide grub-efi specific functions for building bootable images.
7
8# External variables
9# ${INITRD} - indicates a list of filesystem images to concatenate and use as an initrd (optional)
10# ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional)
11# ${GRUB_GFXSERIAL} - set this to 1 to have graphics and serial in the boot menu
12# ${LABELS} - a list of targets for the automatic config
13# ${APPEND} - an override list of append strings for each label
14# ${GRUB_OPTS} - additional options to add to the config, ';' delimited # (optional)
15# ${GRUB_TIMEOUT} - timeout before executing the deault label (optional)
16# ${GRUB_ROOT} - grub's root device.
17
18GRUB_SERIAL ?= "console=ttyS0,115200"
19GRUB_CFG_VM = "${S}/grub_vm.cfg"
20GRUB_CFG_LIVE = "${S}/grub_live.cfg"
21GRUB_TIMEOUT ?= "10"
22#FIXME: build this from the machine config
23GRUB_OPTS ?= "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
24
25GRUB_ROOT ?= "${ROOT}"
26APPEND ?= ""
27
28# Uses MACHINE specific KERNEL_IMAGETYPE
29PACKAGE_ARCH = "${MACHINE_ARCH}"
30
31# Need UUID utility code.
32inherit fs-uuid
33
34python build_efi_cfg() {
35    import sys
36
37    workdir = d.getVar('WORKDIR')
38    if not workdir:
39        bb.error("WORKDIR not defined, unable to package")
40        return
41
42    gfxserial = d.getVar('GRUB_GFXSERIAL') or ""
43
44    labels = d.getVar('LABELS')
45    if not labels:
46        bb.debug(1, "LABELS not defined, nothing to do")
47        return
48
49    if labels == []:
50        bb.debug(1, "No labels, nothing to do")
51        return
52
53    cfile = d.getVar('GRUB_CFG')
54    if not cfile:
55        bb.fatal('Unable to read GRUB_CFG')
56
57    try:
58         cfgfile = open(cfile, 'w')
59    except OSError:
60        bb.fatal('Unable to open %s' % cfile)
61
62    cfgfile.write('# Automatically created by OE\n')
63
64    opts = d.getVar('GRUB_OPTS')
65    if opts:
66        for opt in opts.split(';'):
67            cfgfile.write('%s\n' % opt)
68
69    cfgfile.write('default=%s\n' % (labels.split()[0]))
70
71    timeout = d.getVar('GRUB_TIMEOUT')
72    if timeout:
73        cfgfile.write('timeout=%s\n' % timeout)
74    else:
75        cfgfile.write('timeout=50\n')
76
77    root = d.getVar('GRUB_ROOT')
78    if not root:
79        bb.fatal('GRUB_ROOT not defined')
80
81    if gfxserial == "1":
82        btypes = [ [ " graphics console", "" ],
83            [ " serial console", d.getVar('GRUB_SERIAL') or "" ] ]
84    else:
85        btypes = [ [ "", "" ] ]
86
87    for label in labels.split():
88        localdata = d.createCopy()
89
90        overrides = localdata.getVar('OVERRIDES')
91        if not overrides:
92            bb.fatal('OVERRIDES not defined')
93
94        localdata.setVar('OVERRIDES', 'grub_' + label + ':' + overrides)
95
96        for btype in btypes:
97            cfgfile.write('\nmenuentry \'%s%s\'{\n' % (label, btype[0]))
98            lb = label
99            if label == "install":
100                lb = "install-efi"
101            kernel = localdata.getVar('KERNEL_IMAGETYPE')
102            cfgfile.write('linux /%s LABEL=%s' % (kernel, lb))
103
104            cfgfile.write(' %s' % replace_rootfs_uuid(d, root))
105
106            append = localdata.getVar('APPEND')
107            initrd = localdata.getVar('INITRD')
108
109            if append:
110                append = replace_rootfs_uuid(d, append)
111                cfgfile.write(' %s' % (append))
112
113            cfgfile.write(' %s' % btype[1])
114            cfgfile.write('\n')
115
116            if initrd:
117                cfgfile.write('initrd /initrd')
118            cfgfile.write('\n}\n')
119
120    cfgfile.close()
121}
122build_efi_cfg[vardepsexclude] += "OVERRIDES"
123