blob: 9347aa7fcb7fd25dd41e20db771a0e1a4ec126fc [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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080029import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031from wic import WicError
32from wic.engine import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034from wic.misc import (exec_cmd, exec_native_cmd,
35 get_bitbake_var, BOOTDD_EXTRA_SPACE)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036
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 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049 Check if dirname exists in default bootimg_dir or in STAGING_DIR.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080051 staging_datadir = get_bitbake_var("STAGING_DATADIR")
52 for result in (bootimg_dir, staging_datadir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053 if os.path.exists("%s/%s" % (result, dirname)):
54 return result
55
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080056 # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled
57 # but dependency syslinux is still populated to original STAGING_DATADIR
58 nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir)
59 if os.path.exists(os.path.join(nonarch_datadir, dirname)):
60 return nonarch_datadir
61
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 raise WicError("Couldn't find correct bootimg_dir, exiting")
63
64 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
66 bootimg_dir, kernel_dir, native_sysroot):
67 """
68 Called after all partitions have been prepared and assembled into a
69 disk image. In this case, we install the MBR.
70 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 mbrfile = "%s/syslinux/" % bootimg_dir
73 if creator.ptable_format == 'msdos':
74 mbrfile += "mbr.bin"
75 elif creator.ptable_format == 'gpt':
76 mbrfile += "gptmbr.bin"
77 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 raise WicError("Unsupported partition table: %s" %
79 creator.ptable_format)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
81 if not os.path.exists(mbrfile):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 raise WicError("Couldn't find %s. If using the -e option, do you "
83 "have the right MACHINE set in local.conf? If not, "
84 "is the bootimg_dir path correct?" % mbrfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
86 full_path = creator._full_path(workdir, disk_name, "direct")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 logger.debug("Installing MBR on disk %s as %s with size %s bytes",
88 disk_name, full_path, disk.min_size)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
91 exec_cmd(dd_cmd, native_sysroot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
93 @classmethod
94 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
95 oe_builddir, bootimg_dir, kernel_dir,
96 native_sysroot):
97 """
98 Called before do_prepare_partition(), creates syslinux config
99 """
100 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
102 install_cmd = "install -d %s" % hdddir
103 exec_cmd(install_cmd)
104
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 custom_cfg = None
108 if bootloader.configfile:
109 custom_cfg = get_custom_config(bootloader.configfile)
110 if custom_cfg:
111 # Use a custom configuration for grub
112 syslinux_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 logger.debug("Using custom configuration file %s "
114 "for syslinux.cfg", bootloader.configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500115 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 raise WicError("configfile is specified but failed to "
117 "get it from %s." % bootloader.configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 if not custom_cfg:
120 # Create syslinux configuration using parameters from wks file
121 splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
122 if os.path.exists(splash):
123 splashline = "menu background splash.jpg"
124 else:
125 splashline = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127 syslinux_conf = ""
128 syslinux_conf += "PROMPT 0\n"
129 syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n"
130 syslinux_conf += "\n"
131 syslinux_conf += "ALLOWOPTIONS 1\n"
132 syslinux_conf += "SERIAL 0 115200\n"
133 syslinux_conf += "\n"
134 if splashline:
135 syslinux_conf += "%s\n" % splashline
136 syslinux_conf += "DEFAULT boot\n"
137 syslinux_conf += "LABEL boot\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139 kernel = "/vmlinuz"
140 syslinux_conf += "KERNEL " + kernel + "\n"
141
142 syslinux_conf += "APPEND label=boot root=%s %s\n" % \
143 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg",
146 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
148 cfg.write(syslinux_conf)
149 cfg.close()
150
151 @classmethod
152 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
153 oe_builddir, bootimg_dir, kernel_dir,
154 rootfs_dir, native_sysroot):
155 """
156 Called to do the actual content population for a partition i.e. it
157 'prepares' the partition to be incorporated into the image.
158 In this case, prepare content for legacy bios boot partition.
159 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161
162 staging_kernel_dir = kernel_dir
163
164 hdddir = "%s/hdd/boot" % cr_workdir
165
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 cmds = ("install -m 0644 %s/bzImage %s/vmlinuz" %
167 (staging_kernel_dir, hdddir),
168 "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" %
169 (bootimg_dir, hdddir),
170 "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" %
171 (bootimg_dir, hdddir),
172 "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" %
173 (bootimg_dir, hdddir),
174 "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" %
175 (bootimg_dir, hdddir))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 for install_cmd in cmds:
178 exec_cmd(install_cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179
180 du_cmd = "du -bks %s" % hdddir
181 out = exec_cmd(du_cmd)
182 blocks = int(out.split()[0])
183
184 extra_blocks = part.get_extra_block_count(blocks)
185
186 if extra_blocks < BOOTDD_EXTRA_SPACE:
187 extra_blocks = BOOTDD_EXTRA_SPACE
188
189 blocks += extra_blocks
190
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
192 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 # dosfs image, created by mkdosfs
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196
Brad Bishop316dfdd2018-06-25 12:45:53 -0400197 dosfs_cmd = "mkdosfs -n boot -i %s -S 512 -C %s %d" % \
198 (part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 exec_native_cmd(dosfs_cmd, native_sysroot)
200
201 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
202 exec_native_cmd(mcopy_cmd, native_sysroot)
203
204 syslinux_cmd = "syslinux %s" % bootimg
205 exec_native_cmd(syslinux_cmd, native_sysroot)
206
207 chmod_cmd = "chmod 644 %s" % bootimg
208 exec_cmd(chmod_cmd)
209
210 du_cmd = "du -Lbks %s" % bootimg
211 out = exec_cmd(du_cmd)
212 bootimg_size = out.split()[0]
213
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500215 part.source_file = bootimg