Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # |
| 4 | # DESCRIPTION |
| 5 | # This implements the 'bootimg-partition' source plugin class for |
| 6 | # 'wic'. The plugin creates an image of boot partition, copying over |
| 7 | # files listed in IMAGE_BOOT_FILES bitbake variable. |
| 8 | # |
| 9 | # AUTHORS |
| 10 | # Maciej Borzecki <maciej.borzecki (at] open-rnd.pl> |
| 11 | # |
| 12 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | import os |
| 15 | import re |
| 16 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | from glob import glob |
| 18 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 19 | from wic import WicError |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 20 | from wic.engine import get_custom_config |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 21 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 22 | from wic.misc import exec_cmd, get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 23 | |
| 24 | logger = logging.getLogger('wic') |
| 25 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | class BootimgPartitionPlugin(SourcePlugin): |
| 27 | """ |
| 28 | Create an image of boot partition, copying over files |
| 29 | listed in IMAGE_BOOT_FILES bitbake variable. |
| 30 | """ |
| 31 | |
| 32 | name = 'bootimg-partition' |
| 33 | |
| 34 | @classmethod |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 35 | def do_configure_partition(cls, part, source_params, cr, cr_workdir, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | oe_builddir, bootimg_dir, kernel_dir, |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | native_sysroot): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | """ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 39 | Called before do_prepare_partition(), create u-boot specific boot config |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 41 | hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | install_cmd = "install -d %s" % hdddir |
| 43 | exec_cmd(install_cmd) |
| 44 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | if not kernel_dir: |
| 46 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
| 47 | if not kernel_dir: |
| 48 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 50 | boot_files = None |
| 51 | for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): |
| 52 | if fmt: |
| 53 | var = fmt % id |
| 54 | else: |
| 55 | var = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | boot_files = get_bitbake_var("IMAGE_BOOT_FILES" + var) |
| 58 | if boot_files is not None: |
| 59 | break |
| 60 | |
| 61 | if boot_files is None: |
| 62 | 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] | 63 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | logger.debug('Boot files: %s', boot_files) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | |
| 66 | # list of tuples (src_name, dst_name) |
| 67 | deploy_files = [] |
| 68 | for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files): |
| 69 | if ';' in src_entry: |
| 70 | dst_entry = tuple(src_entry.split(';')) |
| 71 | if not dst_entry[0] or not dst_entry[1]: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | raise WicError('Malformed boot file entry: %s' % src_entry) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | else: |
| 74 | dst_entry = (src_entry, src_entry) |
| 75 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | logger.debug('Destination entry: %r', dst_entry) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | deploy_files.append(dst_entry) |
| 78 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 79 | cls.install_task = []; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | for deploy_entry in deploy_files: |
| 81 | src, dst = deploy_entry |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | if '*' in src: |
| 83 | # by default install files under their basename |
| 84 | entry_name_fn = os.path.basename |
| 85 | if dst != src: |
| 86 | # unless a target name was given, then treat name |
| 87 | # as a directory and append a basename |
| 88 | entry_name_fn = lambda name: \ |
| 89 | os.path.join(dst, |
| 90 | os.path.basename(name)) |
| 91 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | srcs = glob(os.path.join(kernel_dir, src)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 94 | logger.debug('Globbed sources: %s', ', '.join(srcs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | for entry in srcs: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 96 | src = os.path.relpath(entry, kernel_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | entry_dst_name = entry_name_fn(entry) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 98 | cls.install_task.append((src, entry_dst_name)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 100 | cls.install_task.append((src, dst)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 102 | if source_params.get('loader') != "u-boot": |
| 103 | return |
| 104 | |
| 105 | configfile = cr.ks.bootloader.configfile |
| 106 | custom_cfg = None |
| 107 | if configfile: |
| 108 | custom_cfg = get_custom_config(configfile) |
| 109 | if custom_cfg: |
| 110 | # Use a custom configuration for extlinux.conf |
| 111 | extlinux_conf = custom_cfg |
| 112 | logger.debug("Using custom configuration file " |
| 113 | "%s for extlinux.cfg", configfile) |
| 114 | else: |
| 115 | raise WicError("configfile is specified but failed to " |
| 116 | "get it from %s." % configfile) |
| 117 | |
| 118 | if not custom_cfg: |
| 119 | # The kernel types supported by the sysboot of u-boot |
| 120 | kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"] |
| 121 | has_dtb = False |
| 122 | fdt_dir = '/' |
| 123 | kernel_name = None |
| 124 | |
| 125 | # Find the kernel image name, from the highest precedence to lowest |
| 126 | for image in kernel_types: |
| 127 | for task in cls.install_task: |
| 128 | src, dst = task |
| 129 | if re.match(image, src): |
| 130 | kernel_name = os.path.join('/', dst) |
| 131 | break |
| 132 | if kernel_name: |
| 133 | break |
| 134 | |
| 135 | for task in cls.install_task: |
| 136 | src, dst = task |
| 137 | # We suppose that all the dtb are in the same directory |
| 138 | if re.search(r'\.dtb', src) and fdt_dir == '/': |
| 139 | has_dtb = True |
| 140 | fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst)) |
| 141 | break |
| 142 | |
| 143 | if not kernel_name: |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 144 | raise WicError('No kernel file found') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 145 | |
| 146 | # Compose the extlinux.conf |
| 147 | extlinux_conf = "default Yocto\n" |
| 148 | extlinux_conf += "label Yocto\n" |
| 149 | extlinux_conf += " kernel %s\n" % kernel_name |
| 150 | if has_dtb: |
| 151 | extlinux_conf += " fdtdir %s\n" % fdt_dir |
| 152 | bootloader = cr.ks.bootloader |
| 153 | extlinux_conf += "append root=%s rootwait %s\n" \ |
| 154 | % (cr.rootdev, bootloader.append if bootloader.append else '') |
| 155 | |
| 156 | install_cmd = "install -d %s/extlinux/" % hdddir |
| 157 | exec_cmd(install_cmd) |
| 158 | cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w") |
| 159 | cfg.write(extlinux_conf) |
| 160 | cfg.close() |
| 161 | |
| 162 | |
| 163 | @classmethod |
| 164 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 165 | oe_builddir, bootimg_dir, kernel_dir, |
| 166 | rootfs_dir, native_sysroot): |
| 167 | """ |
| 168 | Called to do the actual content population for a partition i.e. it |
| 169 | 'prepares' the partition to be incorporated into the image. |
| 170 | In this case, does the following: |
| 171 | - sets up a vfat partition |
| 172 | - copies all files listed in IMAGE_BOOT_FILES variable |
| 173 | """ |
| 174 | hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) |
| 175 | |
| 176 | if not kernel_dir: |
| 177 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") |
| 178 | if not kernel_dir: |
| 179 | raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") |
| 180 | |
| 181 | logger.debug('Kernel dir: %s', bootimg_dir) |
| 182 | |
| 183 | |
| 184 | for task in cls.install_task: |
| 185 | src_path, dst_path = task |
| 186 | logger.debug('Install %s as %s', src_path, dst_path) |
| 187 | install_cmd = "install -m 0644 -D %s %s" \ |
| 188 | % (os.path.join(kernel_dir, src_path), |
| 189 | os.path.join(hdddir, dst_path)) |
| 190 | exec_cmd(install_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 192 | logger.debug('Prepare boot partition using rootfs in %s', hdddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 194 | native_sysroot, False) |