blob: b239fc0b4cfe84c8a458a7f06b3b7de606632dc8 [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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034from wic.misc import exec_cmd, get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035
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 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068 boot_files = None
69 for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)):
70 if fmt:
71 var = fmt % id
72 else:
73 var = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishopd7bf8c12018-02-25 22:55:05 -050075 boot_files = get_bitbake_var("IMAGE_BOOT_FILES" + var)
76 if boot_files is not None:
77 break
78
79 if boot_files is None:
80 raise WicError('No boot files defined, IMAGE_BOOT_FILES unset for entry #%d' % part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 logger.debug('Boot files: %s', boot_files)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083
84 # list of tuples (src_name, dst_name)
85 deploy_files = []
86 for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files):
87 if ';' in src_entry:
88 dst_entry = tuple(src_entry.split(';'))
89 if not dst_entry[0] or not dst_entry[1]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 raise WicError('Malformed boot file entry: %s' % src_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 else:
92 dst_entry = (src_entry, src_entry)
93
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 logger.debug('Destination entry: %r', dst_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 deploy_files.append(dst_entry)
96
97 for deploy_entry in deploy_files:
98 src, dst = deploy_entry
99 install_task = []
100 if '*' in src:
101 # by default install files under their basename
102 entry_name_fn = os.path.basename
103 if dst != src:
104 # unless a target name was given, then treat name
105 # as a directory and append a basename
106 entry_name_fn = lambda name: \
107 os.path.join(dst,
108 os.path.basename(name))
109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 srcs = glob(os.path.join(kernel_dir, src))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 logger.debug('Globbed sources: %s', ', '.join(srcs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 for entry in srcs:
114 entry_dst_name = entry_name_fn(entry)
115 install_task.append((entry,
116 os.path.join(hdddir,
117 entry_dst_name)))
118 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 install_task = [(os.path.join(kernel_dir, src),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 os.path.join(hdddir, dst))]
121
122 for task in install_task:
123 src_path, dst_path = task
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 logger.debug('Install %s as %s',
125 os.path.basename(src_path), dst_path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 install_cmd = "install -m 0644 -D %s %s" \
127 % (src_path, dst_path)
128 exec_cmd(install_cmd)
129
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 logger.debug('Prepare boot partition using rootfs in %s', hdddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
Brad Bishop316dfdd2018-06-25 12:45:53 -0400132 native_sysroot, False)