Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 26 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | import os |
| 28 | import re |
| 29 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | from glob import glob |
| 31 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | from wic import WicError |
| 33 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 34 | from wic.misc import exec_cmd, get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 35 | |
| 36 | logger = logging.getLogger('wic') |
| 37 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | class 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | install_cmd = "install -d %s" % hdddir |
| 59 | exec_cmd(install_cmd) |
| 60 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 66 | logger.debug('Kernel dir: %s', bootimg_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | logger.debug('Boot files: %s', boot_files) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | |
| 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 90 | raise WicError('Malformed boot file entry: %s' % src_entry) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | else: |
| 92 | dst_entry = (src_entry, src_entry) |
| 93 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 94 | logger.debug('Destination entry: %r', dst_entry) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | srcs = glob(os.path.join(kernel_dir, src)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | logger.debug('Globbed sources: %s', ', '.join(srcs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 119 | install_task = [(os.path.join(kernel_dir, src), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | os.path.join(hdddir, dst))] |
| 121 | |
| 122 | for task in install_task: |
| 123 | src_path, dst_path = task |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | logger.debug('Install %s as %s', |
| 125 | os.path.basename(src_path), dst_path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | install_cmd = "install -m 0644 -D %s %s" \ |
| 127 | % (src_path, dst_path) |
| 128 | exec_cmd(install_cmd) |
| 129 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 130 | logger.debug('Prepare boot partition using rootfs in %s', hdddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 132 | native_sysroot, False) |