blob: 13fddbd4780c5c7f29e7b4a18785b45b82224edc [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# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License version 2 as
6# published by the Free Software Foundation.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16#
17# DESCRIPTION
18# This implements the 'bootimg-partition' source plugin class for
19# 'wic'. The plugin creates an image of boot partition, copying over
20# files listed in IMAGE_BOOT_FILES bitbake variable.
21#
22# AUTHORS
23# Maciej Borzecki <maciej.borzecki (at] open-rnd.pl>
24#
25
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027import os
28import re
29
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030from glob import glob
31
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032from wic import WicError
33from wic.pluginbase import SourcePlugin
34from wic.utils.misc import exec_cmd, get_bitbake_var
35
36logger = logging.getLogger('wic')
37
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038class BootimgPartitionPlugin(SourcePlugin):
39 """
40 Create an image of boot partition, copying over files
41 listed in IMAGE_BOOT_FILES bitbake variable.
42 """
43
44 name = 'bootimg-partition'
45
46 @classmethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
48 oe_builddir, bootimg_dir, kernel_dir,
49 rootfs_dir, native_sysroot):
50 """
51 Called to do the actual content population for a partition i.e. it
52 'prepares' the partition to be incorporated into the image.
53 In this case, does the following:
54 - sets up a vfat partition
55 - copies all files listed in IMAGE_BOOT_FILES variable
56 """
57 hdddir = "%s/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 install_cmd = "install -d %s" % hdddir
59 exec_cmd(install_cmd)
60
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 if not kernel_dir:
62 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
63 if not kernel_dir:
64 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
Brad Bishop6e60e8b2018-02-01 10:27:11 -050066 logger.debug('Kernel dir: %s', bootimg_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68 boot_files = get_bitbake_var("IMAGE_BOOT_FILES")
69
70 if not boot_files:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071 raise WicError('No boot files defined, IMAGE_BOOT_FILES unset')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 logger.debug('Boot files: %s', boot_files)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
75 # list of tuples (src_name, dst_name)
76 deploy_files = []
77 for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files):
78 if ';' in src_entry:
79 dst_entry = tuple(src_entry.split(';'))
80 if not dst_entry[0] or not dst_entry[1]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 raise WicError('Malformed boot file entry: %s' % src_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 else:
83 dst_entry = (src_entry, src_entry)
84
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 logger.debug('Destination entry: %r', dst_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 deploy_files.append(dst_entry)
87
88 for deploy_entry in deploy_files:
89 src, dst = deploy_entry
90 install_task = []
91 if '*' in src:
92 # by default install files under their basename
93 entry_name_fn = os.path.basename
94 if dst != src:
95 # unless a target name was given, then treat name
96 # as a directory and append a basename
97 entry_name_fn = lambda name: \
98 os.path.join(dst,
99 os.path.basename(name))
100
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 srcs = glob(os.path.join(kernel_dir, src))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 logger.debug('Globbed sources: %s', ', '.join(srcs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 for entry in srcs:
105 entry_dst_name = entry_name_fn(entry)
106 install_task.append((entry,
107 os.path.join(hdddir,
108 entry_dst_name)))
109 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 install_task = [(os.path.join(kernel_dir, src),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 os.path.join(hdddir, dst))]
112
113 for task in install_task:
114 src_path, dst_path = task
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 logger.debug('Install %s as %s',
116 os.path.basename(src_path), dst_path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 install_cmd = "install -m 0644 -D %s %s" \
118 % (src_path, dst_path)
119 exec_cmd(install_cmd)
120
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 logger.debug('Prepare boot partition using rootfs in %s', hdddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
123 native_sysroot)