summaryrefslogtreecommitdiff
path: root/meta/classes/grub-efi.bbclass
blob: 333e6c53c72496535044d42bc842daf1df116bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# grub-efi.bbclass
# Copyright (c) 2011, Intel Corporation.
# All rights reserved.
#
# Released under the MIT license (see packages/COPYING)

# Provide grub-efi specific functions for building bootable images.

# External variables
# ${INITRD} - indicates a filesystem image to use as an initrd (optional)
# ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional)
# ${LABELS} - a list of targets for the automatic config
# ${APPEND} - an override list of append strings for each label
# ${GRUB_OPTS} - additional options to add to the config, ';' delimited # (optional)
# ${GRUB_TIMEOUT} - timeout before executing the deault label (optional)

do_bootimg[depends] += "grub-efi-${TARGET_ARCH}-native:do_deploy"

GRUBCFG = "grub.cfg"
GRUB_TIMEOUT ?= "10"
#FIXME: build this from the machine config
GRUB_OPTS ?= "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"

# FIXME: add EFI/BOOT to GRUB_HDDDIR once the mkdosfs subdir bug is resolved
#        http://bugzilla.yoctoproject.org/show_bug.cgi?id=1783
EFIDIR = "/EFI/BOOT"
GRUB_HDDDIR = "${HDDDIR}"
GRUB_ISODIR = "${ISODIR}${EFIDIR}"

grubefi_populate() {
	DEST=$1

	install -d ${DEST}

	install -m 0644 ${STAGING_DIR_HOST}/kernel/bzImage ${DEST}/vmlinuz

	if [ -n "${INITRD}" ] && [ -s "${INITRD}" ]; then 
    		install -m 0644 ${INITRD} ${DEST}/initrd
	fi

	if [ -n "${ROOTFS}" ] && [ -s "${ROOTFS}" ]; then 
    		install -m 0644 ${ROOTFS} ${DEST}/rootfs.img
	fi

	GRUB_IMAGE="bootia32.efi"
	if [ "${TARGET_ARCH}" = "x86_64" ]; then
		GRUB_IMAGE="bootx64.efi"
	fi
	install -m 0644 ${DEPLOY_DIR_IMAGE}/${GRUB_IMAGE} ${DEST}

	install -m 0644 ${GRUBCFG} ${DEST}
}

grubefi_iso_populate() {
	grubefi_populate ${GRUB_ISODIR}

	# FIXUP the <EFIDIR> token in the config
	# FIXME: This can be dropped once mkdosfs is fixed
	sed -i "s@<EFIDIR>@${EFIDIR}@g" ${GRUB_ISODIR}/${GRUBCFG}
}

grubefi_hddimg_populate() {
	grubefi_populate ${GRUB_HDDDIR}

	# FIXUP the <EFIDIR> token in the config
	# FIXME: This can be dropped once mkdosfs is fixed
	sed -i "s@<EFIDIR>@@g" ${GRUB_HDDDIR}/${GRUBCFG}
}

# FIXME: The <EFIDIR> token can be replaced with ${EFIDIR} once the
#        mkdosfs bug is resolved.
python build_grub_cfg() {
    import sys

    workdir = d.getVar('WORKDIR', True)
    if not workdir:
        bb.error("WORKDIR not defined, unable to package")
        return
       
    labels = d.getVar('LABELS', True)
    if not labels:
        bb.debug(1, "LABELS not defined, nothing to do")
        return
   
    if labels == []:
        bb.debug(1, "No labels, nothing to do")
        return

    cfile = d.getVar('GRUBCFG', True)
    if not cfile:
        raise bb.build.FuncFailed('Unable to read GRUBCFG')

    #bb.mkdirhier(os.path.dirname(cfile))

    try:
         cfgfile = file(cfile, 'w')
    except OSError:
        raise bb.build.funcFailed('Unable to open %s' % (cfile))

    cfgfile.write('# Automatically created by OE\n')

    opts = d.getVar('GRUB_OPTS', True)
    if opts:
        for opt in opts.split(';'):
            cfgfile.write('%s\n' % opt)

    cfgfile.write('default=%s\n' % (labels.split()[0]))

    timeout = d.getVar('GRUB_TIMEOUT', True)
    if timeout:
        cfgfile.write('timeout=%s\n' % timeout)
    else:
        cfgfile.write('timeout=50\n')

    for label in labels.split():
        localdata = d.createCopy()

        overrides = localdata.getVar('OVERRIDES', True)
        if not overrides:
            raise bb.build.FuncFailed('OVERRIDES not defined')

        localdata.setVar('OVERRIDES', label + ':' + overrides)
        bb.data.update_data(localdata)

        cfgfile.write('\nmenuentry \'%s\'{\n' % (label))
        cfgfile.write('linux <EFIDIR>/vmlinuz LABEL=%s' % (label))

        append = localdata.getVar('APPEND', True)
        initrd = localdata.getVar('INITRD', True)

        if append:
            cfgfile.write('%s' % (append))
        cfgfile.write('\n')
   
        if initrd:
            cfgfile.write('initrd <EFIDIR>/initrd')
        cfgfile.write('\n}\n')

    cfgfile.close()
}