blob: ddc880be366f4e355c1c402a84f52357a071f29a [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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080033from wic.engine import get_custom_config
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035from wic.misc import exec_cmd, get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036
37logger = logging.getLogger('wic')
38
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039class BootimgPartitionPlugin(SourcePlugin):
40 """
41 Create an image of boot partition, copying over files
42 listed in IMAGE_BOOT_FILES bitbake variable.
43 """
44
45 name = 'bootimg-partition'
46
47 @classmethod
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048 def do_configure_partition(cls, part, source_params, cr, cr_workdir,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 oe_builddir, bootimg_dir, kernel_dir,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080050 native_sysroot):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 """
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080052 Called before do_prepare_partition(), create u-boot specific boot config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 install_cmd = "install -d %s" % hdddir
56 exec_cmd(install_cmd)
57
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 if not kernel_dir:
59 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
60 if not kernel_dir:
61 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 boot_files = None
64 for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)):
65 if fmt:
66 var = fmt % id
67 else:
68 var = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 boot_files = get_bitbake_var("IMAGE_BOOT_FILES" + var)
71 if boot_files is not None:
72 break
73
74 if boot_files is None:
75 raise WicError('No boot files defined, IMAGE_BOOT_FILES unset for entry #%d' % part.lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 logger.debug('Boot files: %s', boot_files)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79 # list of tuples (src_name, dst_name)
80 deploy_files = []
81 for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files):
82 if ';' in src_entry:
83 dst_entry = tuple(src_entry.split(';'))
84 if not dst_entry[0] or not dst_entry[1]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 raise WicError('Malformed boot file entry: %s' % src_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 else:
87 dst_entry = (src_entry, src_entry)
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 logger.debug('Destination entry: %r', dst_entry)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090 deploy_files.append(dst_entry)
91
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080092 cls.install_task = [];
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 for deploy_entry in deploy_files:
94 src, dst = deploy_entry
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 if '*' in src:
96 # by default install files under their basename
97 entry_name_fn = os.path.basename
98 if dst != src:
99 # unless a target name was given, then treat name
100 # as a directory and append a basename
101 entry_name_fn = lambda name: \
102 os.path.join(dst,
103 os.path.basename(name))
104
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 srcs = glob(os.path.join(kernel_dir, src))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 logger.debug('Globbed sources: %s', ', '.join(srcs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 for entry in srcs:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109 src = os.path.relpath(entry, kernel_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 entry_dst_name = entry_name_fn(entry)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800111 cls.install_task.append((src, entry_dst_name))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 else:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800113 cls.install_task.append((src, dst))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800115 if source_params.get('loader') != "u-boot":
116 return
117
118 configfile = cr.ks.bootloader.configfile
119 custom_cfg = None
120 if configfile:
121 custom_cfg = get_custom_config(configfile)
122 if custom_cfg:
123 # Use a custom configuration for extlinux.conf
124 extlinux_conf = custom_cfg
125 logger.debug("Using custom configuration file "
126 "%s for extlinux.cfg", configfile)
127 else:
128 raise WicError("configfile is specified but failed to "
129 "get it from %s." % configfile)
130
131 if not custom_cfg:
132 # The kernel types supported by the sysboot of u-boot
133 kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"]
134 has_dtb = False
135 fdt_dir = '/'
136 kernel_name = None
137
138 # Find the kernel image name, from the highest precedence to lowest
139 for image in kernel_types:
140 for task in cls.install_task:
141 src, dst = task
142 if re.match(image, src):
143 kernel_name = os.path.join('/', dst)
144 break
145 if kernel_name:
146 break
147
148 for task in cls.install_task:
149 src, dst = task
150 # We suppose that all the dtb are in the same directory
151 if re.search(r'\.dtb', src) and fdt_dir == '/':
152 has_dtb = True
153 fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst))
154 break
155
156 if not kernel_name:
157 raise WicError('No kernel file founded')
158
159 # Compose the extlinux.conf
160 extlinux_conf = "default Yocto\n"
161 extlinux_conf += "label Yocto\n"
162 extlinux_conf += " kernel %s\n" % kernel_name
163 if has_dtb:
164 extlinux_conf += " fdtdir %s\n" % fdt_dir
165 bootloader = cr.ks.bootloader
166 extlinux_conf += "append root=%s rootwait %s\n" \
167 % (cr.rootdev, bootloader.append if bootloader.append else '')
168
169 install_cmd = "install -d %s/extlinux/" % hdddir
170 exec_cmd(install_cmd)
171 cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w")
172 cfg.write(extlinux_conf)
173 cfg.close()
174
175
176 @classmethod
177 def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
178 oe_builddir, bootimg_dir, kernel_dir,
179 rootfs_dir, native_sysroot):
180 """
181 Called to do the actual content population for a partition i.e. it
182 'prepares' the partition to be incorporated into the image.
183 In this case, does the following:
184 - sets up a vfat partition
185 - copies all files listed in IMAGE_BOOT_FILES variable
186 """
187 hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
188
189 if not kernel_dir:
190 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
191 if not kernel_dir:
192 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
193
194 logger.debug('Kernel dir: %s', bootimg_dir)
195
196
197 for task in cls.install_task:
198 src_path, dst_path = task
199 logger.debug('Install %s as %s', src_path, dst_path)
200 install_cmd = "install -m 0644 -D %s %s" \
201 % (os.path.join(kernel_dir, src_path),
202 os.path.join(hdddir, dst_path))
203 exec_cmd(install_cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 logger.debug('Prepare boot partition using rootfs in %s', hdddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
Brad Bishop316dfdd2018-06-25 12:45:53 -0400207 native_sysroot, False)