blob: df7fe18a79d81200b145c9a0175200161ef015e0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# grub-efi.bbclass
2# Copyright (c) 2011, Intel Corporation.
3# All rights reserved.
4#
5# Released under the MIT license (see packages/COPYING)
6
7# Provide grub-efi specific functions for building bootable images.
8
9# External variables
10# ${INITRD} - indicates a list of filesystem images to concatenate and use as an initrd (optional)
11# ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional)
12# ${GRUB_GFXSERIAL} - set this to 1 to have graphics and serial in the boot menu
13# ${LABELS} - a list of targets for the automatic config
14# ${APPEND} - an override list of append strings for each label
15# ${GRUB_OPTS} - additional options to add to the config, ';' delimited # (optional)
16# ${GRUB_TIMEOUT} - timeout before executing the deault label (optional)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050017# ${GRUB_ROOT} - grub's root device.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
19do_bootimg[depends] += "${MLPREFIX}grub-efi:do_deploy"
20do_bootdirectdisk[depends] += "${MLPREFIX}grub-efi:do_deploy"
21
22GRUB_SERIAL ?= "console=ttyS0,115200"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050023GRUB_CFG_VM = "${S}/grub_vm.cfg"
24GRUB_CFG_LIVE = "${S}/grub_live.cfg"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025GRUB_TIMEOUT ?= "10"
26#FIXME: build this from the machine config
27GRUB_OPTS ?= "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
28
29EFIDIR = "/EFI/BOOT"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030GRUB_ROOT ?= "${ROOT}"
31APPEND ?= ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032
33# Need UUID utility code.
34inherit fs-uuid
35
36efi_populate() {
37 # DEST must be the root of the image so that EFIDIR is not
38 # nested under a top level directory.
39 DEST=$1
40
41 install -d ${DEST}${EFIDIR}
42
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 GRUB_IMAGE="grub-efi-bootia32.efi"
44 DEST_IMAGE="bootia32.efi"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 if [ "${TARGET_ARCH}" = "x86_64" ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 GRUB_IMAGE="grub-efi-bootx64.efi"
47 DEST_IMAGE="bootx64.efi"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -050049 install -m 0644 ${DEPLOY_DIR_IMAGE}/${GRUB_IMAGE} ${DEST}${EFIDIR}/${DEST_IMAGE}
Patrick Williamsc0f7c042017-02-23 20:41:17 -060050 EFIPATH=$(echo "${EFIDIR}" | sed 's/\//\\/g')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 printf 'fs0:%s\%s\n' "$EFIPATH" "$DEST_IMAGE" >${DEST}/startup.nsh
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 install -m 0644 ${GRUB_CFG} ${DEST}${EFIDIR}/grub.cfg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054}
55
56efi_iso_populate() {
57 iso_dir=$1
58 efi_populate $iso_dir
59 # Build a EFI directory to create efi.img
60 mkdir -p ${EFIIMGDIR}/${EFIDIR}
61 cp $iso_dir/${EFIDIR}/* ${EFIIMGDIR}${EFIDIR}
62 cp $iso_dir/vmlinuz ${EFIIMGDIR}
Patrick Williamsf1e5d692016-03-30 15:21:19 -050063 EFIPATH=$(echo "${EFIDIR}" | sed 's/\//\\/g')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060064 printf 'fs0:%s\%s\n' "$EFIPATH" "$GRUB_IMAGE" > ${EFIIMGDIR}/startup.nsh
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 if [ -f "$iso_dir/initrd" ] ; then
66 cp $iso_dir/initrd ${EFIIMGDIR}
67 fi
68}
69
70efi_hddimg_populate() {
71 efi_populate $1
72}
73
74python build_efi_cfg() {
75 import sys
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 workdir = d.getVar('WORKDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 if not workdir:
79 bb.error("WORKDIR not defined, unable to package")
80 return
81
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 gfxserial = d.getVar('GRUB_GFXSERIAL') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 labels = d.getVar('LABELS')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 if not labels:
86 bb.debug(1, "LABELS not defined, nothing to do")
87 return
88
89 if labels == []:
90 bb.debug(1, "No labels, nothing to do")
91 return
92
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 cfile = d.getVar('GRUB_CFG')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 if not cfile:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 bb.fatal('Unable to read GRUB_CFG')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
97 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060098 cfgfile = open(cfile, 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 except OSError:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600100 bb.fatal('Unable to open %s' % cfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
102 cfgfile.write('# Automatically created by OE\n')
103
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 opts = d.getVar('GRUB_OPTS')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 if opts:
106 for opt in opts.split(';'):
107 cfgfile.write('%s\n' % opt)
108
109 cfgfile.write('default=%s\n' % (labels.split()[0]))
110
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 timeout = d.getVar('GRUB_TIMEOUT')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 if timeout:
113 cfgfile.write('timeout=%s\n' % timeout)
114 else:
115 cfgfile.write('timeout=50\n')
116
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 root = d.getVar('GRUB_ROOT')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 if not root:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600119 bb.fatal('GRUB_ROOT not defined')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 if gfxserial == "1":
122 btypes = [ [ " graphics console", "" ],
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 [ " serial console", d.getVar('GRUB_SERIAL') or "" ] ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 else:
125 btypes = [ [ "", "" ] ]
126
127 for label in labels.split():
128 localdata = d.createCopy()
129
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 overrides = localdata.getVar('OVERRIDES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 if not overrides:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600132 bb.fatal('OVERRIDES not defined')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134 for btype in btypes:
135 localdata.setVar('OVERRIDES', label + ':' + overrides)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
137 cfgfile.write('\nmenuentry \'%s%s\'{\n' % (label, btype[0]))
138 lb = label
139 if label == "install":
140 lb = "install-efi"
141 cfgfile.write('linux /vmlinuz LABEL=%s' % (lb))
142
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 cfgfile.write(' %s' % replace_rootfs_uuid(d, root))
144
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 append = localdata.getVar('APPEND')
146 initrd = localdata.getVar('INITRD')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 if append:
149 append = replace_rootfs_uuid(d, append)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150 cfgfile.write(' %s' % (append))
151
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 cfgfile.write(' %s' % btype[1])
153 cfgfile.write('\n')
154
155 if initrd:
156 cfgfile.write('initrd /initrd')
157 cfgfile.write('\n}\n')
158
159 cfgfile.close()
160}