Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | # |
| 4 | # Copyright (c) 2014, Intel Corporation. |
| 5 | # All rights reserved. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License version 2 as |
| 9 | # published by the Free Software Foundation. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License along |
| 17 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | # |
| 20 | # DESCRIPTION |
| 21 | # This implements the 'bootimg-pcbios' source plugin class for 'wic' |
| 22 | # |
| 23 | # AUTHORS |
| 24 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> |
| 25 | # |
| 26 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 27 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | import os |
| 29 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 30 | from wic import WicError |
| 31 | from wic.engine import get_custom_config |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | from wic.utils import runner |
| 33 | from wic.pluginbase import SourcePlugin |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 34 | from wic.utils.misc import (exec_cmd, exec_native_cmd, |
| 35 | get_bitbake_var, BOOTDD_EXTRA_SPACE) |
| 36 | |
| 37 | logger = logging.getLogger('wic') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
| 39 | class BootimgPcbiosPlugin(SourcePlugin): |
| 40 | """ |
| 41 | Create MBR boot partition and install syslinux on it. |
| 42 | """ |
| 43 | |
| 44 | name = 'bootimg-pcbios' |
| 45 | |
| 46 | @classmethod |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 47 | def _get_bootimg_dir(cls, bootimg_dir, dirname): |
| 48 | """ |
| 49 | Check if dirname exists in default bootimg_dir or |
| 50 | in wic-tools STAGING_DIR. |
| 51 | """ |
| 52 | for result in (bootimg_dir, get_bitbake_var("STAGING_DATADIR", "wic-tools")): |
| 53 | if os.path.exists("%s/%s" % (result, dirname)): |
| 54 | return result |
| 55 | |
| 56 | raise WicError("Couldn't find correct bootimg_dir, exiting") |
| 57 | |
| 58 | @classmethod |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, |
| 60 | bootimg_dir, kernel_dir, native_sysroot): |
| 61 | """ |
| 62 | Called after all partitions have been prepared and assembled into a |
| 63 | disk image. In this case, we install the MBR. |
| 64 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 65 | bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | mbrfile = "%s/syslinux/" % bootimg_dir |
| 67 | if creator.ptable_format == 'msdos': |
| 68 | mbrfile += "mbr.bin" |
| 69 | elif creator.ptable_format == 'gpt': |
| 70 | mbrfile += "gptmbr.bin" |
| 71 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 72 | raise WicError("Unsupported partition table: %s" % |
| 73 | creator.ptable_format) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
| 75 | if not os.path.exists(mbrfile): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 76 | raise WicError("Couldn't find %s. If using the -e option, do you " |
| 77 | "have the right MACHINE set in local.conf? If not, " |
| 78 | "is the bootimg_dir path correct?" % mbrfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | |
| 80 | full_path = creator._full_path(workdir, disk_name, "direct") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 81 | logger.debug("Installing MBR on disk %s as %s with size %s bytes", |
| 82 | disk_name, full_path, disk.min_size) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 84 | dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) |
| 85 | exec_cmd(dd_cmd, native_sysroot) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
| 87 | @classmethod |
| 88 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, |
| 89 | oe_builddir, bootimg_dir, kernel_dir, |
| 90 | native_sysroot): |
| 91 | """ |
| 92 | Called before do_prepare_partition(), creates syslinux config |
| 93 | """ |
| 94 | hdddir = "%s/hdd/boot" % cr_workdir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | |
| 96 | install_cmd = "install -d %s" % hdddir |
| 97 | exec_cmd(install_cmd) |
| 98 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 99 | bootloader = creator.ks.bootloader |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 101 | custom_cfg = None |
| 102 | if bootloader.configfile: |
| 103 | custom_cfg = get_custom_config(bootloader.configfile) |
| 104 | if custom_cfg: |
| 105 | # Use a custom configuration for grub |
| 106 | syslinux_conf = custom_cfg |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 107 | logger.debug("Using custom configuration file %s " |
| 108 | "for syslinux.cfg", bootloader.configfile) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 109 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 110 | raise WicError("configfile is specified but failed to " |
| 111 | "get it from %s." % bootloader.configfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 113 | if not custom_cfg: |
| 114 | # Create syslinux configuration using parameters from wks file |
| 115 | splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") |
| 116 | if os.path.exists(splash): |
| 117 | splashline = "menu background splash.jpg" |
| 118 | else: |
| 119 | splashline = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 121 | syslinux_conf = "" |
| 122 | syslinux_conf += "PROMPT 0\n" |
| 123 | syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n" |
| 124 | syslinux_conf += "\n" |
| 125 | syslinux_conf += "ALLOWOPTIONS 1\n" |
| 126 | syslinux_conf += "SERIAL 0 115200\n" |
| 127 | syslinux_conf += "\n" |
| 128 | if splashline: |
| 129 | syslinux_conf += "%s\n" % splashline |
| 130 | syslinux_conf += "DEFAULT boot\n" |
| 131 | syslinux_conf += "LABEL boot\n" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 133 | kernel = "/vmlinuz" |
| 134 | syslinux_conf += "KERNEL " + kernel + "\n" |
| 135 | |
| 136 | syslinux_conf += "APPEND label=boot root=%s %s\n" % \ |
| 137 | (creator.rootdev, bootloader.append) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 139 | logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg", |
| 140 | cr_workdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") |
| 142 | cfg.write(syslinux_conf) |
| 143 | cfg.close() |
| 144 | |
| 145 | @classmethod |
| 146 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, |
| 147 | oe_builddir, bootimg_dir, kernel_dir, |
| 148 | rootfs_dir, native_sysroot): |
| 149 | """ |
| 150 | Called to do the actual content population for a partition i.e. it |
| 151 | 'prepares' the partition to be incorporated into the image. |
| 152 | In this case, prepare content for legacy bios boot partition. |
| 153 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 154 | bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
| 156 | staging_kernel_dir = kernel_dir |
| 157 | |
| 158 | hdddir = "%s/hdd/boot" % cr_workdir |
| 159 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 160 | cmds = ("install -m 0644 %s/bzImage %s/vmlinuz" % |
| 161 | (staging_kernel_dir, hdddir), |
| 162 | "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % |
| 163 | (bootimg_dir, hdddir), |
| 164 | "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % |
| 165 | (bootimg_dir, hdddir), |
| 166 | "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % |
| 167 | (bootimg_dir, hdddir), |
| 168 | "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % |
| 169 | (bootimg_dir, hdddir)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 170 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 171 | for install_cmd in cmds: |
| 172 | exec_cmd(install_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 173 | |
| 174 | du_cmd = "du -bks %s" % hdddir |
| 175 | out = exec_cmd(du_cmd) |
| 176 | blocks = int(out.split()[0]) |
| 177 | |
| 178 | extra_blocks = part.get_extra_block_count(blocks) |
| 179 | |
| 180 | if extra_blocks < BOOTDD_EXTRA_SPACE: |
| 181 | extra_blocks = BOOTDD_EXTRA_SPACE |
| 182 | |
| 183 | blocks += extra_blocks |
| 184 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 185 | logger.debug("Added %d extra blocks to %s to get to %d total blocks", |
| 186 | extra_blocks, part.mountpoint, blocks) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 187 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | # dosfs image, created by mkdosfs |
| 189 | bootimg = "%s/boot.img" % cr_workdir |
| 190 | |
| 191 | dosfs_cmd = "mkdosfs -n boot -S 512 -C %s %d" % (bootimg, blocks) |
| 192 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 193 | |
| 194 | mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) |
| 195 | exec_native_cmd(mcopy_cmd, native_sysroot) |
| 196 | |
| 197 | syslinux_cmd = "syslinux %s" % bootimg |
| 198 | exec_native_cmd(syslinux_cmd, native_sysroot) |
| 199 | |
| 200 | chmod_cmd = "chmod 644 %s" % bootimg |
| 201 | exec_cmd(chmod_cmd) |
| 202 | |
| 203 | du_cmd = "du -Lbks %s" % bootimg |
| 204 | out = exec_cmd(du_cmd) |
| 205 | bootimg_size = out.split()[0] |
| 206 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 207 | part.size = int(bootimg_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 208 | part.source_file = bootimg |