blob: 5cc5c8a6b80d140dd57615b4812700b75f2712c0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright (c) 2014, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
6# DESCRIPTION
7# This implements the 'bootimg-efi' source plugin class for 'wic'
8#
9# AUTHORS
10# Tom Zanussi <tom.zanussi (at] linux.intel.com>
11#
12
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014import os
15import shutil
16
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017from wic import WicError
18from wic.engine import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020from wic.misc import (exec_cmd, exec_native_cmd,
21 get_bitbake_var, BOOTDD_EXTRA_SPACE)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022
23logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024
25class BootimgEFIPlugin(SourcePlugin):
26 """
27 Create EFI boot partition.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050028 This plugin supports GRUB 2 and systemd-boot bootloaders.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029 """
30
31 name = 'bootimg-efi'
32
33 @classmethod
Brad Bishopd5ae7d92018-06-14 09:52:03 -070034 def do_configure_grubefi(cls, hdddir, creator, cr_workdir, source_params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 """
36 Create loader-specific (grub-efi) config
37 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038 configfile = creator.ks.bootloader.configfile
39 custom_cfg = None
40 if configfile:
41 custom_cfg = get_custom_config(configfile)
42 if custom_cfg:
43 # Use a custom configuration for grub
44 grubefi_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045 logger.debug("Using custom configuration file "
46 "%s for grub.cfg", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 raise WicError("configfile is specified but failed to "
49 "get it from %s." % configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
Brad Bishopd5ae7d92018-06-14 09:52:03 -070051 initrd = source_params.get('initrd')
52
53 if initrd:
54 bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
55 if not bootimg_dir:
56 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
57
Brad Bishopf3fd2882019-06-21 08:06:37 -040058 initrds = initrd.split(';')
59 for rd in initrds:
60 cp_cmd = "cp %s/%s %s" % (bootimg_dir, rd, hdddir)
61 exec_cmd(cp_cmd, True)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070062 else:
63 logger.debug("Ignoring missing initrd")
64
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050065 if not custom_cfg:
66 # Create grub configuration using parameters from wks file
67 bootloader = creator.ks.bootloader
Brad Bishop19323692019-04-05 15:28:33 -040068 title = source_params.get('title')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 grubefi_conf = ""
71 grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n"
72 grubefi_conf += "default=boot\n"
73 grubefi_conf += "timeout=%s\n" % bootloader.timeout
Brad Bishop19323692019-04-05 15:28:33 -040074 grubefi_conf += "menuentry '%s'{\n" % (title if title else "boot")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
Brad Bishop15ae2502019-06-18 21:44:24 -040076 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
77 if not kernel:
78 kernel = "bzImage"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050079
Brad Bishop15ae2502019-06-18 21:44:24 -040080 label = source_params.get('label')
81 label_conf = "root=%s" % creator.rootdev
82 if label:
83 label_conf = "LABEL=%s" % label
84
85 grubefi_conf += "linux /%s %s rootwait %s\n" \
86 % (kernel, label_conf, bootloader.append)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070087
88 if initrd:
Brad Bishopf3fd2882019-06-21 08:06:37 -040089 initrds = initrd.split(';')
90 grubefi_conf += "initrd"
91 for rd in initrds:
92 grubefi_conf += " /%s" % rd
93 grubefi_conf += "\n"
Brad Bishopd5ae7d92018-06-14 09:52:03 -070094
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 grubefi_conf += "}\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg",
98 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w")
100 cfg.write(grubefi_conf)
101 cfg.close()
102
103 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 Create loader-specific systemd-boot/gummiboot config
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 """
108 install_cmd = "install -d %s/loader" % hdddir
109 exec_cmd(install_cmd)
110
111 install_cmd = "install -d %s/loader/entries" % hdddir
112 exec_cmd(install_cmd)
113
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500114 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115
116 loader_conf = ""
117 loader_conf += "default boot\n"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 loader_conf += "timeout %d\n" % bootloader.timeout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500120 initrd = source_params.get('initrd')
121
122 if initrd:
123 # obviously we need to have a common common deploy var
124 bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
125 if not bootimg_dir:
126 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
127
Brad Bishopf3fd2882019-06-21 08:06:37 -0400128 initrds = initrd.split(';')
129 for rd in initrds:
130 cp_cmd = "cp %s/%s %s" % (bootimg_dir, rd, hdddir)
131 exec_cmd(cp_cmd, True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132 else:
133 logger.debug("Ignoring missing initrd")
134
135 logger.debug("Writing systemd-boot config "
136 "%s/hdd/boot/loader/loader.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
138 cfg.write(loader_conf)
139 cfg.close()
140
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500141 configfile = creator.ks.bootloader.configfile
142 custom_cfg = None
143 if configfile:
144 custom_cfg = get_custom_config(configfile)
145 if custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 # Use a custom configuration for systemd-boot
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500147 boot_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 logger.debug("Using custom configuration file "
149 "%s for systemd-boots's boot.conf", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 raise WicError("configfile is specified but failed to "
152 "get it from %s.", configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 if not custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500155 # Create systemd-boot configuration using parameters from wks file
Brad Bishop15ae2502019-06-18 21:44:24 -0400156 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
157 if not kernel:
158 kernel = "bzImage"
159
Brad Bishop19323692019-04-05 15:28:33 -0400160 title = source_params.get('title')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500161
162 boot_conf = ""
Brad Bishop19323692019-04-05 15:28:33 -0400163 boot_conf += "title %s\n" % (title if title else "boot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400164 boot_conf += "linux /%s\n" % kernel
165
166 label = source_params.get('label')
167 label_conf = "LABEL=Boot root=%s" % creator.rootdev
168 if label:
169 label_conf = "LABEL=%s" % label
170
171 boot_conf += "options %s %s\n" % \
172 (label_conf, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174 if initrd:
Brad Bishopf3fd2882019-06-21 08:06:37 -0400175 initrds = initrd.split(';')
176 for rd in initrds:
177 boot_conf += "initrd /%s\n" % rd
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178
179 logger.debug("Writing systemd-boot config "
180 "%s/hdd/boot/loader/entries/boot.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
182 cfg.write(boot_conf)
183 cfg.close()
184
185
186 @classmethod
187 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
188 oe_builddir, bootimg_dir, kernel_dir,
189 native_sysroot):
190 """
191 Called before do_prepare_partition(), creates loader-specific config
192 """
193 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194
195 install_cmd = "install -d %s/EFI/BOOT" % hdddir
196 exec_cmd(install_cmd)
197
198 try:
199 if source_params['loader'] == 'grub-efi':
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700200 cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500201 elif source_params['loader'] == 'systemd-boot':
202 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500206 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207
208
209 @classmethod
210 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
211 oe_builddir, bootimg_dir, kernel_dir,
212 rootfs_dir, native_sysroot):
213 """
214 Called to do the actual content population for a partition i.e. it
215 'prepares' the partition to be incorporated into the image.
216 In this case, prepare content for an EFI (grub) boot partition.
217 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500218 if not kernel_dir:
219 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
220 if not kernel_dir:
221 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222
223 staging_kernel_dir = kernel_dir
224
225 hdddir = "%s/hdd/boot" % cr_workdir
226
Brad Bishop15ae2502019-06-18 21:44:24 -0400227 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
228 if not kernel:
229 kernel = "bzImage"
230
231 install_cmd = "install -m 0644 %s/%s %s/%s" % \
232 (staging_kernel_dir, kernel, hdddir, kernel)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 exec_cmd(install_cmd)
234
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 try:
237 if source_params['loader'] == 'grub-efi':
238 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
239 "%s/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500240 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]:
241 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:])
242 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 shutil.move("%s/grub.cfg" % cr_workdir,
244 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500245 elif source_params['loader'] == 'systemd-boot':
246 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]:
247 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:])
248 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500250 raise WicError("unrecognized bootimg-efi loader: %s" %
251 source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500253 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 startup = os.path.join(kernel_dir, "startup.nsh")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 if os.path.exists(startup):
257 cp_cmd = "cp %s %s/" % (startup, hdddir)
258 exec_cmd(cp_cmd, True)
259
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 du_cmd = "du -bks %s" % hdddir
261 out = exec_cmd(du_cmd)
262 blocks = int(out.split()[0])
263
264 extra_blocks = part.get_extra_block_count(blocks)
265
266 if extra_blocks < BOOTDD_EXTRA_SPACE:
267 extra_blocks = BOOTDD_EXTRA_SPACE
268
269 blocks += extra_blocks
270
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
272 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500273
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274 # dosfs image, created by mkdosfs
275 bootimg = "%s/boot.img" % cr_workdir
276
Brad Bishopc342db32019-05-15 21:57:59 -0400277 label = part.label if part.label else "ESP"
278
279 dosfs_cmd = "mkdosfs -n %s -i %s -C %s %d" % \
280 (label, part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 exec_native_cmd(dosfs_cmd, native_sysroot)
282
283 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
284 exec_native_cmd(mcopy_cmd, native_sysroot)
285
286 chmod_cmd = "chmod 644 %s" % bootimg
287 exec_cmd(chmod_cmd)
288
289 du_cmd = "du -Lbks %s" % bootimg
290 out = exec_cmd(du_cmd)
291 bootimg_size = out.split()[0]
292
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500293 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500294 part.source_file = bootimg