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