blob: 2cfdc10ecd59990e3499693a906f0c87fde3ecdd [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")
Brad Bishop96ff1982019-08-19 13:50:42 -040077 if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1":
78 if get_bitbake_var("INITRAMFS_IMAGE"):
79 kernel = "%s-%s.bin" % \
80 (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081
Brad Bishop15ae2502019-06-18 21:44:24 -040082 label = source_params.get('label')
83 label_conf = "root=%s" % creator.rootdev
84 if label:
85 label_conf = "LABEL=%s" % label
86
87 grubefi_conf += "linux /%s %s rootwait %s\n" \
88 % (kernel, label_conf, bootloader.append)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070089
90 if initrd:
Brad Bishopf3fd2882019-06-21 08:06:37 -040091 initrds = initrd.split(';')
92 grubefi_conf += "initrd"
93 for rd in initrds:
94 grubefi_conf += " /%s" % rd
95 grubefi_conf += "\n"
Brad Bishopd5ae7d92018-06-14 09:52:03 -070096
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 grubefi_conf += "}\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg",
100 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w")
102 cfg.write(grubefi_conf)
103 cfg.close()
104
105 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600108 Create loader-specific systemd-boot/gummiboot config
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 """
110 install_cmd = "install -d %s/loader" % hdddir
111 exec_cmd(install_cmd)
112
113 install_cmd = "install -d %s/loader/entries" % hdddir
114 exec_cmd(install_cmd)
115
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500116 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117
118 loader_conf = ""
119 loader_conf += "default boot\n"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 loader_conf += "timeout %d\n" % bootloader.timeout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 initrd = source_params.get('initrd')
123
124 if initrd:
125 # obviously we need to have a common common deploy var
126 bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
127 if not bootimg_dir:
128 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
129
Brad Bishopf3fd2882019-06-21 08:06:37 -0400130 initrds = initrd.split(';')
131 for rd in initrds:
132 cp_cmd = "cp %s/%s %s" % (bootimg_dir, rd, hdddir)
133 exec_cmd(cp_cmd, True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134 else:
135 logger.debug("Ignoring missing initrd")
136
137 logger.debug("Writing systemd-boot config "
138 "%s/hdd/boot/loader/loader.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139 cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
140 cfg.write(loader_conf)
141 cfg.close()
142
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 configfile = creator.ks.bootloader.configfile
144 custom_cfg = None
145 if configfile:
146 custom_cfg = get_custom_config(configfile)
147 if custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 # Use a custom configuration for systemd-boot
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 boot_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 logger.debug("Using custom configuration file "
151 "%s for systemd-boots's boot.conf", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 raise WicError("configfile is specified but failed to "
154 "get it from %s.", configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156 if not custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157 # Create systemd-boot configuration using parameters from wks file
Brad Bishop15ae2502019-06-18 21:44:24 -0400158 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
Brad Bishop96ff1982019-08-19 13:50:42 -0400159 if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1":
160 if get_bitbake_var("INITRAMFS_IMAGE"):
161 kernel = "%s-%s.bin" % \
162 (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME"))
Brad Bishop15ae2502019-06-18 21:44:24 -0400163
Brad Bishop19323692019-04-05 15:28:33 -0400164 title = source_params.get('title')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165
166 boot_conf = ""
Brad Bishop19323692019-04-05 15:28:33 -0400167 boot_conf += "title %s\n" % (title if title else "boot")
Brad Bishop15ae2502019-06-18 21:44:24 -0400168 boot_conf += "linux /%s\n" % kernel
169
170 label = source_params.get('label')
171 label_conf = "LABEL=Boot root=%s" % creator.rootdev
172 if label:
173 label_conf = "LABEL=%s" % label
174
175 boot_conf += "options %s %s\n" % \
176 (label_conf, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178 if initrd:
Brad Bishopf3fd2882019-06-21 08:06:37 -0400179 initrds = initrd.split(';')
180 for rd in initrds:
181 boot_conf += "initrd /%s\n" % rd
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182
183 logger.debug("Writing systemd-boot config "
184 "%s/hdd/boot/loader/entries/boot.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185 cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
186 cfg.write(boot_conf)
187 cfg.close()
188
189
190 @classmethod
191 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
192 oe_builddir, bootimg_dir, kernel_dir,
193 native_sysroot):
194 """
195 Called before do_prepare_partition(), creates loader-specific config
196 """
197 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
199 install_cmd = "install -d %s/EFI/BOOT" % hdddir
200 exec_cmd(install_cmd)
201
202 try:
203 if source_params['loader'] == 'grub-efi':
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700204 cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 elif source_params['loader'] == 'systemd-boot':
206 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500208 raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211
212
213 @classmethod
214 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
215 oe_builddir, bootimg_dir, kernel_dir,
216 rootfs_dir, native_sysroot):
217 """
218 Called to do the actual content population for a partition i.e. it
219 'prepares' the partition to be incorporated into the image.
220 In this case, prepare content for an EFI (grub) boot partition.
221 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222 if not kernel_dir:
223 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
224 if not kernel_dir:
225 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226
227 staging_kernel_dir = kernel_dir
228
229 hdddir = "%s/hdd/boot" % cr_workdir
230
Brad Bishop15ae2502019-06-18 21:44:24 -0400231 kernel = get_bitbake_var("KERNEL_IMAGETYPE")
Brad Bishop96ff1982019-08-19 13:50:42 -0400232 if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1":
233 if get_bitbake_var("INITRAMFS_IMAGE"):
234 kernel = "%s-%s.bin" % \
235 (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME"))
Brad Bishop15ae2502019-06-18 21:44:24 -0400236
237 install_cmd = "install -m 0644 %s/%s %s/%s" % \
238 (staging_kernel_dir, kernel, hdddir, kernel)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239 exec_cmd(install_cmd)
240
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 try:
243 if source_params['loader'] == 'grub-efi':
244 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
245 "%s/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500246 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]:
247 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:])
248 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 shutil.move("%s/grub.cfg" % cr_workdir,
250 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500251 elif source_params['loader'] == 'systemd-boot':
252 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]:
253 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:])
254 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500256 raise WicError("unrecognized bootimg-efi loader: %s" %
257 source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 startup = os.path.join(kernel_dir, "startup.nsh")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 if os.path.exists(startup):
263 cp_cmd = "cp %s %s/" % (startup, hdddir)
264 exec_cmd(cp_cmd, True)
265
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 du_cmd = "du -bks %s" % hdddir
267 out = exec_cmd(du_cmd)
268 blocks = int(out.split()[0])
269
270 extra_blocks = part.get_extra_block_count(blocks)
271
272 if extra_blocks < BOOTDD_EXTRA_SPACE:
273 extra_blocks = BOOTDD_EXTRA_SPACE
274
275 blocks += extra_blocks
276
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500277 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
278 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280 # dosfs image, created by mkdosfs
281 bootimg = "%s/boot.img" % cr_workdir
282
Brad Bishopc342db32019-05-15 21:57:59 -0400283 label = part.label if part.label else "ESP"
284
285 dosfs_cmd = "mkdosfs -n %s -i %s -C %s %d" % \
286 (label, part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287 exec_native_cmd(dosfs_cmd, native_sysroot)
288
289 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
290 exec_native_cmd(mcopy_cmd, native_sysroot)
291
292 chmod_cmd = "chmod 644 %s" % bootimg
293 exec_cmd(chmod_cmd)
294
295 du_cmd = "du -Lbks %s" % bootimg
296 out = exec_cmd(du_cmd)
297 bootimg_size = out.split()[0]
298
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500299 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500300 part.source_file = bootimg