blob: 670d34774773fa08341a0460a5c1b6015adbf5c6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright (c) 2014, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
6# DESCRIPTION
7# This implements the 'bootimg-pcbios' source plugin class for 'wic'
8#
9# AUTHORS
10# Tom Zanussi <tom.zanussi (at] linux.intel.com>
11#
12
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014import os
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080015import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017from wic import WicError
18from wic.engine import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020from wic.misc import (exec_cmd, exec_native_cmd,
21 get_bitbake_var, BOOTDD_EXTRA_SPACE)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022
23logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024
25class BootimgPcbiosPlugin(SourcePlugin):
26 """
27 Create MBR boot partition and install syslinux on it.
28 """
29
30 name = 'bootimg-pcbios'
31
32 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033 def _get_bootimg_dir(cls, bootimg_dir, dirname):
34 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035 Check if dirname exists in default bootimg_dir or in STAGING_DIR.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 staging_datadir = get_bitbake_var("STAGING_DATADIR")
38 for result in (bootimg_dir, staging_datadir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 if os.path.exists("%s/%s" % (result, dirname)):
40 return result
41
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080042 # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled
43 # but dependency syslinux is still populated to original STAGING_DATADIR
44 nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir)
45 if os.path.exists(os.path.join(nonarch_datadir, dirname)):
46 return nonarch_datadir
47
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 raise WicError("Couldn't find correct bootimg_dir, exiting")
49
50 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
52 bootimg_dir, kernel_dir, native_sysroot):
53 """
54 Called after all partitions have been prepared and assembled into a
55 disk image. In this case, we install the MBR.
56 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 mbrfile = "%s/syslinux/" % bootimg_dir
59 if creator.ptable_format == 'msdos':
60 mbrfile += "mbr.bin"
61 elif creator.ptable_format == 'gpt':
62 mbrfile += "gptmbr.bin"
63 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 raise WicError("Unsupported partition table: %s" %
65 creator.ptable_format)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
67 if not os.path.exists(mbrfile):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 raise WicError("Couldn't find %s. If using the -e option, do you "
69 "have the right MACHINE set in local.conf? If not, "
70 "is the bootimg_dir path correct?" % mbrfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071
72 full_path = creator._full_path(workdir, disk_name, "direct")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 logger.debug("Installing MBR on disk %s as %s with size %s bytes",
74 disk_name, full_path, disk.min_size)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
77 exec_cmd(dd_cmd, native_sysroot)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79 @classmethod
80 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
81 oe_builddir, bootimg_dir, kernel_dir,
82 native_sysroot):
83 """
84 Called before do_prepare_partition(), creates syslinux config
85 """
86 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
88 install_cmd = "install -d %s" % hdddir
89 exec_cmd(install_cmd)
90
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050091 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 custom_cfg = None
94 if bootloader.configfile:
95 custom_cfg = get_custom_config(bootloader.configfile)
96 if custom_cfg:
97 # Use a custom configuration for grub
98 syslinux_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 logger.debug("Using custom configuration file %s "
100 "for syslinux.cfg", bootloader.configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500101 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 raise WicError("configfile is specified but failed to "
103 "get it from %s." % bootloader.configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 if not custom_cfg:
106 # Create syslinux configuration using parameters from wks file
107 splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
108 if os.path.exists(splash):
109 splashline = "menu background splash.jpg"
110 else:
111 splashline = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 syslinux_conf = ""
114 syslinux_conf += "PROMPT 0\n"
115 syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n"
116 syslinux_conf += "\n"
117 syslinux_conf += "ALLOWOPTIONS 1\n"
118 syslinux_conf += "SERIAL 0 115200\n"
119 syslinux_conf += "\n"
120 if splashline:
121 syslinux_conf += "%s\n" % splashline
122 syslinux_conf += "DEFAULT boot\n"
123 syslinux_conf += "LABEL boot\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500125 kernel = "/vmlinuz"
126 syslinux_conf += "KERNEL " + kernel + "\n"
127
128 syslinux_conf += "APPEND label=boot root=%s %s\n" % \
129 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg",
132 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
134 cfg.write(syslinux_conf)
135 cfg.close()
136
137 @classmethod
138 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
139 oe_builddir, bootimg_dir, kernel_dir,
140 rootfs_dir, native_sysroot):
141 """
142 Called to do the actual content population for a partition i.e. it
143 'prepares' the partition to be incorporated into the image.
144 In this case, prepare content for legacy bios boot partition.
145 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 staging_kernel_dir = kernel_dir
149
150 hdddir = "%s/hdd/boot" % cr_workdir
151
Brad Bishop15ae2502019-06-18 21:44:24 -0400152 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
153 if not kernel:
154 kernel = "bzImage"
155
156 cmds = ("install -m 0644 %s/%s %s/vmlinuz" %
157 (staging_kernel_dir, kernel, hdddir),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" %
159 (bootimg_dir, hdddir),
160 "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" %
161 (bootimg_dir, hdddir),
162 "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" %
163 (bootimg_dir, hdddir),
164 "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" %
165 (bootimg_dir, hdddir))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 for install_cmd in cmds:
168 exec_cmd(install_cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169
170 du_cmd = "du -bks %s" % hdddir
171 out = exec_cmd(du_cmd)
172 blocks = int(out.split()[0])
173
174 extra_blocks = part.get_extra_block_count(blocks)
175
176 if extra_blocks < BOOTDD_EXTRA_SPACE:
177 extra_blocks = BOOTDD_EXTRA_SPACE
178
179 blocks += extra_blocks
180
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
182 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 # dosfs image, created by mkdosfs
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500185 bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186
Brad Bishop316dfdd2018-06-25 12:45:53 -0400187 dosfs_cmd = "mkdosfs -n boot -i %s -S 512 -C %s %d" % \
188 (part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 exec_native_cmd(dosfs_cmd, native_sysroot)
190
191 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
192 exec_native_cmd(mcopy_cmd, native_sysroot)
193
194 syslinux_cmd = "syslinux %s" % bootimg
195 exec_native_cmd(syslinux_cmd, native_sysroot)
196
197 chmod_cmd = "chmod 644 %s" % bootimg
198 exec_cmd(chmod_cmd)
199
200 du_cmd = "du -Lbks %s" % bootimg
201 out = exec_cmd(du_cmd)
202 bootimg_size = out.split()[0]
203
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500205 part.source_file = bootimg