blob: 5890c1267bc50f83b473c0cd91c67740c062d57d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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 Bishop6e60e8b2018-02-01 10:27:11 -050027import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028import os
29
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030from wic import WicError
31from wic.engine import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032from wic.utils import runner
33from wic.pluginbase import SourcePlugin
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034from wic.utils.misc import (exec_cmd, exec_native_cmd,
35 get_bitbake_var, BOOTDD_EXTRA_SPACE)
36
37logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39class BootimgPcbiosPlugin(SourcePlugin):
40 """
41 Create MBR boot partition and install syslinux on it.
42 """
43
44 name = 'bootimg-pcbios'
45
46 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 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 Williamsc124f4f2015-09-15 14:41:29 -050059 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 Bishop6e60e8b2018-02-01 10:27:11 -050065 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 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 Bishop6e60e8b2018-02-01 10:27:11 -050072 raise WicError("Unsupported partition table: %s" %
73 creator.ptable_format)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
75 if not os.path.exists(mbrfile):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 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 Williamsc124f4f2015-09-15 14:41:29 -050079
80 full_path = creator._full_path(workdir, disk_name, "direct")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 logger.debug("Installing MBR on disk %s as %s with size %s bytes",
82 disk_name, full_path, disk.min_size)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
85 exec_cmd(dd_cmd, native_sysroot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
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 Williamsc124f4f2015-09-15 14:41:29 -050095
96 install_cmd = "install -d %s" % hdddir
97 exec_cmd(install_cmd)
98
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500101 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 Bishop6e60e8b2018-02-01 10:27:11 -0500107 logger.debug("Using custom configuration file %s "
108 "for syslinux.cfg", bootloader.configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500109 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 raise WicError("configfile is specified but failed to "
111 "get it from %s." % bootloader.configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 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 Williamsc124f4f2015-09-15 14:41:29 -0500120
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500121 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 Williamsc124f4f2015-09-15 14:41:29 -0500132
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133 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 Williamsc124f4f2015-09-15 14:41:29 -0500138
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg",
140 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 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 Bishop6e60e8b2018-02-01 10:27:11 -0500154 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155
156 staging_kernel_dir = kernel_dir
157
158 hdddir = "%s/hdd/boot" % cr_workdir
159
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 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 Williamsc124f4f2015-09-15 14:41:29 -0500170
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 for install_cmd in cmds:
172 exec_cmd(install_cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
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 Bishop6e60e8b2018-02-01 10:27:11 -0500185 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
186 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 # 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 Bishop6e60e8b2018-02-01 10:27:11 -0500207 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500208 part.source_file = bootimg