| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 17 | # ${GRUB_ROOT} - grub's root device. | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 |  | 
|  | 19 | do_bootimg[depends] += "${MLPREFIX}grub-efi:do_deploy" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 |  | 
|  | 21 | GRUB_SERIAL ?= "console=ttyS0,115200" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 22 | GRUB_CFG_VM = "${S}/grub_vm.cfg" | 
|  | 23 | GRUB_CFG_LIVE = "${S}/grub_live.cfg" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | GRUB_TIMEOUT ?= "10" | 
|  | 25 | #FIXME: build this from the machine config | 
|  | 26 | GRUB_OPTS ?= "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1" | 
|  | 27 |  | 
|  | 28 | EFIDIR = "/EFI/BOOT" | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 29 | GRUB_ROOT ?= "${ROOT}" | 
|  | 30 | APPEND ?= "" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 |  | 
|  | 32 | # Need UUID utility code. | 
|  | 33 | inherit fs-uuid | 
|  | 34 |  | 
|  | 35 | efi_populate() { | 
|  | 36 | # DEST must be the root of the image so that EFIDIR is not | 
|  | 37 | # nested under a top level directory. | 
|  | 38 | DEST=$1 | 
|  | 39 |  | 
|  | 40 | install -d ${DEST}${EFIDIR} | 
|  | 41 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | GRUB_IMAGE="grub-efi-bootia32.efi" | 
|  | 43 | DEST_IMAGE="bootia32.efi" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | if [ "${TARGET_ARCH}" = "x86_64" ]; then | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | GRUB_IMAGE="grub-efi-bootx64.efi" | 
|  | 46 | DEST_IMAGE="bootx64.efi" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | fi | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | install -m 0644 ${DEPLOY_DIR_IMAGE}/${GRUB_IMAGE} ${DEST}${EFIDIR}/${DEST_IMAGE} | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 49 | EFIPATH=$(echo "${EFIDIR}" | sed 's/\//\\/g') | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | printf 'fs0:%s\%s\n' "$EFIPATH" "$DEST_IMAGE" >${DEST}/startup.nsh | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 52 | install -m 0644 ${GRUB_CFG} ${DEST}${EFIDIR}/grub.cfg | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | efi_iso_populate() { | 
|  | 56 | iso_dir=$1 | 
|  | 57 | efi_populate $iso_dir | 
|  | 58 | # Build a EFI directory to create efi.img | 
|  | 59 | mkdir -p ${EFIIMGDIR}/${EFIDIR} | 
|  | 60 | cp $iso_dir/${EFIDIR}/* ${EFIIMGDIR}${EFIDIR} | 
|  | 61 | cp $iso_dir/vmlinuz ${EFIIMGDIR} | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 62 | EFIPATH=$(echo "${EFIDIR}" | sed 's/\//\\/g') | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | printf 'fs0:%s\%s\n' "$EFIPATH" "$GRUB_IMAGE" > ${EFIIMGDIR}/startup.nsh | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | if [ -f "$iso_dir/initrd" ] ; then | 
|  | 65 | cp $iso_dir/initrd ${EFIIMGDIR} | 
|  | 66 | fi | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | efi_hddimg_populate() { | 
|  | 70 | efi_populate $1 | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | python build_efi_cfg() { | 
|  | 74 | import sys | 
|  | 75 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | workdir = d.getVar('WORKDIR') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | if not workdir: | 
|  | 78 | bb.error("WORKDIR not defined, unable to package") | 
|  | 79 | return | 
|  | 80 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | gfxserial = d.getVar('GRUB_GFXSERIAL') or "" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | labels = d.getVar('LABELS') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | if not labels: | 
|  | 85 | bb.debug(1, "LABELS not defined, nothing to do") | 
|  | 86 | return | 
|  | 87 |  | 
|  | 88 | if labels == []: | 
|  | 89 | bb.debug(1, "No labels, nothing to do") | 
|  | 90 | return | 
|  | 91 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | cfile = d.getVar('GRUB_CFG') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | if not cfile: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 94 | bb.fatal('Unable to read GRUB_CFG') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 |  | 
|  | 96 | try: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | cfgfile = open(cfile, 'w') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | except OSError: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | bb.fatal('Unable to open %s' % cfile) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 |  | 
|  | 101 | cfgfile.write('# Automatically created by OE\n') | 
|  | 102 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 103 | opts = d.getVar('GRUB_OPTS') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | if opts: | 
|  | 105 | for opt in opts.split(';'): | 
|  | 106 | cfgfile.write('%s\n' % opt) | 
|  | 107 |  | 
|  | 108 | cfgfile.write('default=%s\n' % (labels.split()[0])) | 
|  | 109 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | timeout = d.getVar('GRUB_TIMEOUT') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | if timeout: | 
|  | 112 | cfgfile.write('timeout=%s\n' % timeout) | 
|  | 113 | else: | 
|  | 114 | cfgfile.write('timeout=50\n') | 
|  | 115 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 116 | root = d.getVar('GRUB_ROOT') | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 117 | if not root: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | bb.fatal('GRUB_ROOT not defined') | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | if gfxserial == "1": | 
|  | 121 | btypes = [ [ " graphics console", "" ], | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 122 | [ " serial console", d.getVar('GRUB_SERIAL') or "" ] ] | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | else: | 
|  | 124 | btypes = [ [ "", "" ] ] | 
|  | 125 |  | 
|  | 126 | for label in labels.split(): | 
|  | 127 | localdata = d.createCopy() | 
|  | 128 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 129 | overrides = localdata.getVar('OVERRIDES') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | if not overrides: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | bb.fatal('OVERRIDES not defined') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 |  | 
|  | 133 | for btype in btypes: | 
|  | 134 | localdata.setVar('OVERRIDES', label + ':' + overrides) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 |  | 
|  | 136 | cfgfile.write('\nmenuentry \'%s%s\'{\n' % (label, btype[0])) | 
|  | 137 | lb = label | 
|  | 138 | if label == "install": | 
|  | 139 | lb = "install-efi" | 
|  | 140 | cfgfile.write('linux /vmlinuz LABEL=%s' % (lb)) | 
|  | 141 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 142 | cfgfile.write(' %s' % replace_rootfs_uuid(d, root)) | 
|  | 143 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | append = localdata.getVar('APPEND') | 
|  | 145 | initrd = localdata.getVar('INITRD') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 146 |  | 
|  | 147 | if append: | 
|  | 148 | append = replace_rootfs_uuid(d, append) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 149 | cfgfile.write(' %s' % (append)) | 
|  | 150 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | cfgfile.write(' %s' % btype[1]) | 
|  | 152 | cfgfile.write('\n') | 
|  | 153 |  | 
|  | 154 | if initrd: | 
|  | 155 | cfgfile.write('initrd /initrd') | 
|  | 156 | cfgfile.write('\n}\n') | 
|  | 157 |  | 
|  | 158 | cfgfile.close() | 
|  | 159 | } |