blob: 5b719bf3bb8ffc22517ec9da5322518097df85a0 [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
27import os
28
29from wic.utils.errors import ImageError
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030from wic import msger
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031from wic.utils import runner
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032from wic.utils.misc import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033from wic.pluginbase import SourcePlugin
34from wic.utils.oe.misc import exec_cmd, exec_native_cmd, \
35 get_bitbake_var, BOOTDD_EXTRA_SPACE
36
37class BootimgPcbiosPlugin(SourcePlugin):
38 """
39 Create MBR boot partition and install syslinux on it.
40 """
41
42 name = 'bootimg-pcbios'
43
44 @classmethod
45 def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
46 bootimg_dir, kernel_dir, native_sysroot):
47 """
48 Called after all partitions have been prepared and assembled into a
49 disk image. In this case, we install the MBR.
50 """
51 mbrfile = "%s/syslinux/" % bootimg_dir
52 if creator.ptable_format == 'msdos':
53 mbrfile += "mbr.bin"
54 elif creator.ptable_format == 'gpt':
55 mbrfile += "gptmbr.bin"
56 else:
57 msger.error("Unsupported partition table: %s" % creator.ptable_format)
58
59 if not os.path.exists(mbrfile):
60 msger.error("Couldn't find %s. If using the -e option, do you "
61 "have the right MACHINE set in local.conf? If not, "
62 "is the bootimg_dir path correct?" % mbrfile)
63
64 full_path = creator._full_path(workdir, disk_name, "direct")
65 msger.debug("Installing MBR on disk %s as %s with size %s bytes" \
66 % (disk_name, full_path, disk['min_size']))
67
68 rcode = runner.show(['dd', 'if=%s' % mbrfile,
69 'of=%s' % full_path, 'conv=notrunc'])
70 if rcode != 0:
71 raise ImageError("Unable to set MBR to %s" % full_path)
72
73 @classmethod
74 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
75 oe_builddir, bootimg_dir, kernel_dir,
76 native_sysroot):
77 """
78 Called before do_prepare_partition(), creates syslinux config
79 """
80 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
82 install_cmd = "install -d %s" % hdddir
83 exec_cmd(install_cmd)
84
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087 custom_cfg = None
88 if bootloader.configfile:
89 custom_cfg = get_custom_config(bootloader.configfile)
90 if custom_cfg:
91 # Use a custom configuration for grub
92 syslinux_conf = custom_cfg
93 msger.debug("Using custom configuration file "
94 "%s for syslinux.cfg" % bootloader.configfile)
95 else:
96 msger.error("configfile is specified but failed to "
97 "get it from %s." % bootloader.configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 if not custom_cfg:
100 # Create syslinux configuration using parameters from wks file
101 splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
102 if os.path.exists(splash):
103 splashline = "menu background splash.jpg"
104 else:
105 splashline = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 syslinux_conf = ""
108 syslinux_conf += "PROMPT 0\n"
109 syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n"
110 syslinux_conf += "\n"
111 syslinux_conf += "ALLOWOPTIONS 1\n"
112 syslinux_conf += "SERIAL 0 115200\n"
113 syslinux_conf += "\n"
114 if splashline:
115 syslinux_conf += "%s\n" % splashline
116 syslinux_conf += "DEFAULT boot\n"
117 syslinux_conf += "LABEL boot\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 kernel = "/vmlinuz"
120 syslinux_conf += "KERNEL " + kernel + "\n"
121
122 syslinux_conf += "APPEND label=boot root=%s %s\n" % \
123 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125 msger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg" \
126 % cr_workdir)
127 cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
128 cfg.write(syslinux_conf)
129 cfg.close()
130
131 @classmethod
132 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
133 oe_builddir, bootimg_dir, kernel_dir,
134 rootfs_dir, native_sysroot):
135 """
136 Called to do the actual content population for a partition i.e. it
137 'prepares' the partition to be incorporated into the image.
138 In this case, prepare content for legacy bios boot partition.
139 """
140 def _has_syslinux(dirname):
141 if dirname:
142 syslinux = "%s/syslinux" % dirname
143 if os.path.exists(syslinux):
144 return True
145 return False
146
147 if not _has_syslinux(bootimg_dir):
148 bootimg_dir = get_bitbake_var("STAGING_DATADIR")
149 if not bootimg_dir:
150 msger.error("Couldn't find STAGING_DATADIR, exiting\n")
151 if not _has_syslinux(bootimg_dir):
152 msger.error("Please build syslinux first\n")
153 # just so the result notes display it
154 creator.set_bootimg_dir(bootimg_dir)
155
156 staging_kernel_dir = kernel_dir
157
158 hdddir = "%s/hdd/boot" % cr_workdir
159
160 install_cmd = "install -m 0644 %s/bzImage %s/vmlinuz" \
161 % (staging_kernel_dir, hdddir)
162 exec_cmd(install_cmd)
163
164 install_cmd = "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" \
165 % (bootimg_dir, hdddir)
166 exec_cmd(install_cmd)
167
168 du_cmd = "du -bks %s" % hdddir
169 out = exec_cmd(du_cmd)
170 blocks = int(out.split()[0])
171
172 extra_blocks = part.get_extra_block_count(blocks)
173
174 if extra_blocks < BOOTDD_EXTRA_SPACE:
175 extra_blocks = BOOTDD_EXTRA_SPACE
176
177 blocks += extra_blocks
178
179 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
180 (extra_blocks, part.mountpoint, blocks))
181
182 # Ensure total sectors is an integral number of sectors per
183 # track or mcopy will complain. Sectors are 512 bytes, and we
184 # generate images with 32 sectors per track. This calculation is
185 # done in blocks, thus the mod by 16 instead of 32.
186 blocks += (16 - (blocks % 16))
187
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207 part.size = int(out.split()[0])
208 part.source_file = bootimg
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209
210