blob: 56da468fb5cd734743a4380f582063ae37b8e89c [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.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050033from wic.misc import (exec_cmd, exec_native_cmd,
34 get_bitbake_var, BOOTDD_EXTRA_SPACE)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035
36logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38class BootimgPcbiosPlugin(SourcePlugin):
39 """
40 Create MBR boot partition and install syslinux on it.
41 """
42
43 name = 'bootimg-pcbios'
44
45 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 def _get_bootimg_dir(cls, bootimg_dir, dirname):
47 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048 Check if dirname exists in default bootimg_dir or in STAGING_DIR.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050049 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050 for result in (bootimg_dir, get_bitbake_var("STAGING_DATADIR")):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 if os.path.exists("%s/%s" % (result, dirname)):
52 return result
53
54 raise WicError("Couldn't find correct bootimg_dir, exiting")
55
56 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
58 bootimg_dir, kernel_dir, native_sysroot):
59 """
60 Called after all partitions have been prepared and assembled into a
61 disk image. In this case, we install the MBR.
62 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050063 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 mbrfile = "%s/syslinux/" % bootimg_dir
65 if creator.ptable_format == 'msdos':
66 mbrfile += "mbr.bin"
67 elif creator.ptable_format == 'gpt':
68 mbrfile += "gptmbr.bin"
69 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 raise WicError("Unsupported partition table: %s" %
71 creator.ptable_format)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
73 if not os.path.exists(mbrfile):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074 raise WicError("Couldn't find %s. If using the -e option, do you "
75 "have the right MACHINE set in local.conf? If not, "
76 "is the bootimg_dir path correct?" % mbrfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077
78 full_path = creator._full_path(workdir, disk_name, "direct")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 logger.debug("Installing MBR on disk %s as %s with size %s bytes",
80 disk_name, full_path, disk.min_size)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
83 exec_cmd(dd_cmd, native_sysroot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084
85 @classmethod
86 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
87 oe_builddir, bootimg_dir, kernel_dir,
88 native_sysroot):
89 """
90 Called before do_prepare_partition(), creates syslinux config
91 """
92 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093
94 install_cmd = "install -d %s" % hdddir
95 exec_cmd(install_cmd)
96
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 custom_cfg = None
100 if bootloader.configfile:
101 custom_cfg = get_custom_config(bootloader.configfile)
102 if custom_cfg:
103 # Use a custom configuration for grub
104 syslinux_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 logger.debug("Using custom configuration file %s "
106 "for syslinux.cfg", bootloader.configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 raise WicError("configfile is specified but failed to "
109 "get it from %s." % bootloader.configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500111 if not custom_cfg:
112 # Create syslinux configuration using parameters from wks file
113 splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
114 if os.path.exists(splash):
115 splashline = "menu background splash.jpg"
116 else:
117 splashline = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 syslinux_conf = ""
120 syslinux_conf += "PROMPT 0\n"
121 syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n"
122 syslinux_conf += "\n"
123 syslinux_conf += "ALLOWOPTIONS 1\n"
124 syslinux_conf += "SERIAL 0 115200\n"
125 syslinux_conf += "\n"
126 if splashline:
127 syslinux_conf += "%s\n" % splashline
128 syslinux_conf += "DEFAULT boot\n"
129 syslinux_conf += "LABEL boot\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131 kernel = "/vmlinuz"
132 syslinux_conf += "KERNEL " + kernel + "\n"
133
134 syslinux_conf += "APPEND label=boot root=%s %s\n" % \
135 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg",
138 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139 cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
140 cfg.write(syslinux_conf)
141 cfg.close()
142
143 @classmethod
144 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
145 oe_builddir, bootimg_dir, kernel_dir,
146 rootfs_dir, native_sysroot):
147 """
148 Called to do the actual content population for a partition i.e. it
149 'prepares' the partition to be incorporated into the image.
150 In this case, prepare content for legacy bios boot partition.
151 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
154 staging_kernel_dir = kernel_dir
155
156 hdddir = "%s/hdd/boot" % cr_workdir
157
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 cmds = ("install -m 0644 %s/bzImage %s/vmlinuz" %
159 (staging_kernel_dir, hdddir),
160 "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" %
161 (bootimg_dir, hdddir),
162 "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" %
163 (bootimg_dir, hdddir),
164 "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" %
165 (bootimg_dir, hdddir),
166 "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" %
167 (bootimg_dir, hdddir))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 for install_cmd in cmds:
170 exec_cmd(install_cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171
172 du_cmd = "du -bks %s" % hdddir
173 out = exec_cmd(du_cmd)
174 blocks = int(out.split()[0])
175
176 extra_blocks = part.get_extra_block_count(blocks)
177
178 if extra_blocks < BOOTDD_EXTRA_SPACE:
179 extra_blocks = BOOTDD_EXTRA_SPACE
180
181 blocks += extra_blocks
182
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500183 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
184 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 # dosfs image, created by mkdosfs
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500187 bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188
189 dosfs_cmd = "mkdosfs -n boot -S 512 -C %s %d" % (bootimg, blocks)
190 exec_native_cmd(dosfs_cmd, native_sysroot)
191
192 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
193 exec_native_cmd(mcopy_cmd, native_sysroot)
194
195 syslinux_cmd = "syslinux %s" % bootimg
196 exec_native_cmd(syslinux_cmd, native_sysroot)
197
198 chmod_cmd = "chmod 644 %s" % bootimg
199 exec_cmd(chmod_cmd)
200
201 du_cmd = "du -Lbks %s" % bootimg
202 out = exec_cmd(du_cmd)
203 bootimg_size = out.split()[0]
204
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500206 part.source_file = bootimg