1# 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
30from wic import kickstart, msger
31from wic.utils import runner
32from wic.pluginbase import SourcePlugin
33from wic.utils.oe.misc import exec_cmd, exec_native_cmd, \
34                              get_bitbake_var, BOOTDD_EXTRA_SPACE
35
36class BootimgPcbiosPlugin(SourcePlugin):
37    """
38    Create MBR boot partition and install syslinux on it.
39    """
40
41    name = 'bootimg-pcbios'
42
43    @classmethod
44    def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
45                        bootimg_dir, kernel_dir, native_sysroot):
46        """
47        Called after all partitions have been prepared and assembled into a
48        disk image.  In this case, we install the MBR.
49        """
50        mbrfile = "%s/syslinux/" % bootimg_dir
51        if creator.ptable_format == 'msdos':
52            mbrfile += "mbr.bin"
53        elif creator.ptable_format == 'gpt':
54            mbrfile += "gptmbr.bin"
55        else:
56            msger.error("Unsupported partition table: %s" % creator.ptable_format)
57
58        if not os.path.exists(mbrfile):
59            msger.error("Couldn't find %s.  If using the -e option, do you "
60                        "have the right MACHINE set in local.conf?  If not, "
61                        "is the bootimg_dir path correct?" % mbrfile)
62
63        full_path = creator._full_path(workdir, disk_name, "direct")
64        msger.debug("Installing MBR on disk %s as %s with size %s bytes" \
65                    % (disk_name, full_path, disk['min_size']))
66
67        rcode = runner.show(['dd', 'if=%s' % mbrfile,
68                             'of=%s' % full_path, 'conv=notrunc'])
69        if rcode != 0:
70            raise ImageError("Unable to set MBR to %s" % full_path)
71
72    @classmethod
73    def do_configure_partition(cls, part, source_params, creator, cr_workdir,
74                               oe_builddir, bootimg_dir, kernel_dir,
75                               native_sysroot):
76        """
77        Called before do_prepare_partition(), creates syslinux config
78        """
79        hdddir = "%s/hdd/boot" % cr_workdir
80        rm_cmd = "rm -rf " + cr_workdir
81        exec_cmd(rm_cmd)
82
83        install_cmd = "install -d %s" % hdddir
84        exec_cmd(install_cmd)
85
86        splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
87        if os.path.exists(splash):
88            splashline = "menu background splash.jpg"
89        else:
90            splashline = ""
91
92        options = creator.ks.handler.bootloader.appendLine
93
94        syslinux_conf = ""
95        syslinux_conf += "PROMPT 0\n"
96        timeout = kickstart.get_timeout(creator.ks)
97        if not timeout:
98            timeout = 0
99        syslinux_conf += "TIMEOUT " + str(timeout) + "\n"
100        syslinux_conf += "\n"
101        syslinux_conf += "ALLOWOPTIONS 1\n"
102        syslinux_conf += "SERIAL 0 115200\n"
103        syslinux_conf += "\n"
104        if splashline:
105            syslinux_conf += "%s\n" % splashline
106        syslinux_conf += "DEFAULT boot\n"
107        syslinux_conf += "LABEL boot\n"
108
109        kernel = "/vmlinuz"
110        syslinux_conf += "KERNEL " + kernel + "\n"
111
112        syslinux_conf += "APPEND label=boot root=%s %s\n" % \
113                             (creator.rootdev, options)
114
115        msger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg" \
116                    % cr_workdir)
117        cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
118        cfg.write(syslinux_conf)
119        cfg.close()
120
121    @classmethod
122    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
123                             oe_builddir, bootimg_dir, kernel_dir,
124                             rootfs_dir, native_sysroot):
125        """
126        Called to do the actual content population for a partition i.e. it
127        'prepares' the partition to be incorporated into the image.
128        In this case, prepare content for legacy bios boot partition.
129        """
130        def _has_syslinux(dirname):
131            if dirname:
132                syslinux = "%s/syslinux" % dirname
133                if os.path.exists(syslinux):
134                    return True
135            return False
136
137        if not _has_syslinux(bootimg_dir):
138            bootimg_dir = get_bitbake_var("STAGING_DATADIR")
139            if not bootimg_dir:
140                msger.error("Couldn't find STAGING_DATADIR, exiting\n")
141            if not _has_syslinux(bootimg_dir):
142                msger.error("Please build syslinux first\n")
143            # just so the result notes display it
144            creator.set_bootimg_dir(bootimg_dir)
145
146        staging_kernel_dir = kernel_dir
147
148        hdddir = "%s/hdd/boot" % cr_workdir
149
150        install_cmd = "install -m 0644 %s/bzImage %s/vmlinuz" \
151            % (staging_kernel_dir, hdddir)
152        exec_cmd(install_cmd)
153
154        install_cmd = "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" \
155            % (bootimg_dir, hdddir)
156        exec_cmd(install_cmd)
157
158        du_cmd = "du -bks %s" % hdddir
159        out = exec_cmd(du_cmd)
160        blocks = int(out.split()[0])
161
162        extra_blocks = part.get_extra_block_count(blocks)
163
164        if extra_blocks < BOOTDD_EXTRA_SPACE:
165            extra_blocks = BOOTDD_EXTRA_SPACE
166
167        blocks += extra_blocks
168
169        msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
170                    (extra_blocks, part.mountpoint, blocks))
171
172        # Ensure total sectors is an integral number of sectors per
173        # track or mcopy will complain. Sectors are 512 bytes, and we
174        # generate images with 32 sectors per track. This calculation is
175        # done in blocks, thus the mod by 16 instead of 32.
176        blocks += (16 - (blocks % 16))
177
178        # dosfs image, created by mkdosfs
179        bootimg = "%s/boot.img" % cr_workdir
180
181        dosfs_cmd = "mkdosfs -n boot -S 512 -C %s %d" % (bootimg, blocks)
182        exec_native_cmd(dosfs_cmd, native_sysroot)
183
184        mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
185        exec_native_cmd(mcopy_cmd, native_sysroot)
186
187        syslinux_cmd = "syslinux %s" % bootimg
188        exec_native_cmd(syslinux_cmd, native_sysroot)
189
190        chmod_cmd = "chmod 644 %s" % bootimg
191        exec_cmd(chmod_cmd)
192
193        du_cmd = "du -Lbks %s" % bootimg
194        out = exec_cmd(du_cmd)
195        bootimg_size = out.split()[0]
196
197        part.set_size(bootimg_size)
198        part.set_source_file(bootimg)
199
200
201