blob: 83a7e189ed91dc5d4734d9fa0241eb72c6896ab3 [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# Copyright (c) 2014, Intel Corporation.
5# All rights reserved.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# DESCRIPTION
21# This implements the 'bootimg-efi' source plugin class for 'wic'
22#
23# AUTHORS
24# Tom Zanussi <tom.zanussi (at] linux.intel.com>
25#
26
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028import os
29import shutil
30
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031from wic import WicError
32from wic.engine import get_custom_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033from wic.pluginbase import SourcePlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034from wic.misc import (exec_cmd, exec_native_cmd,
35 get_bitbake_var, BOOTDD_EXTRA_SPACE)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036
37logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39class BootimgEFIPlugin(SourcePlugin):
40 """
41 Create EFI boot partition.
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042 This plugin supports GRUB 2 and systemd-boot bootloaders.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043 """
44
45 name = 'bootimg-efi'
46
47 @classmethod
Brad Bishopd5ae7d92018-06-14 09:52:03 -070048 def do_configure_grubefi(cls, hdddir, creator, cr_workdir, source_params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 """
50 Create loader-specific (grub-efi) config
51 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050052 configfile = creator.ks.bootloader.configfile
53 custom_cfg = None
54 if configfile:
55 custom_cfg = get_custom_config(configfile)
56 if custom_cfg:
57 # Use a custom configuration for grub
58 grubefi_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059 logger.debug("Using custom configuration file "
60 "%s for grub.cfg", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 raise WicError("configfile is specified but failed to "
63 "get it from %s." % configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064
Brad Bishopd5ae7d92018-06-14 09:52:03 -070065 initrd = source_params.get('initrd')
66
67 if initrd:
68 bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
69 if not bootimg_dir:
70 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
71
72 cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
73 exec_cmd(cp_cmd, True)
74 else:
75 logger.debug("Ignoring missing initrd")
76
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077 if not custom_cfg:
78 # Create grub configuration using parameters from wks file
79 bootloader = creator.ks.bootloader
Brad Bishop19323692019-04-05 15:28:33 -040080 title = source_params.get('title')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 grubefi_conf = ""
83 grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n"
84 grubefi_conf += "default=boot\n"
85 grubefi_conf += "timeout=%s\n" % bootloader.timeout
Brad Bishop19323692019-04-05 15:28:33 -040086 grubefi_conf += "menuentry '%s'{\n" % (title if title else "boot")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 kernel = "/bzImage"
89
90 grubefi_conf += "linux %s root=%s rootwait %s\n" \
91 % (kernel, creator.rootdev, bootloader.append)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070092
93 if initrd:
94 grubefi_conf += "initrd /%s\n" % initrd
95
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096 grubefi_conf += "}\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097
Brad Bishop6e60e8b2018-02-01 10:27:11 -050098 logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg",
99 cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w")
101 cfg.write(grubefi_conf)
102 cfg.close()
103
104 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600107 Create loader-specific systemd-boot/gummiboot config
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 """
109 install_cmd = "install -d %s/loader" % hdddir
110 exec_cmd(install_cmd)
111
112 install_cmd = "install -d %s/loader/entries" % hdddir
113 exec_cmd(install_cmd)
114
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500115 bootloader = creator.ks.bootloader
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117 loader_conf = ""
118 loader_conf += "default boot\n"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 loader_conf += "timeout %d\n" % bootloader.timeout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 initrd = source_params.get('initrd')
122
123 if initrd:
124 # obviously we need to have a common common deploy var
125 bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
126 if not bootimg_dir:
127 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
128
129 cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
130 exec_cmd(cp_cmd, True)
131 else:
132 logger.debug("Ignoring missing initrd")
133
134 logger.debug("Writing systemd-boot config "
135 "%s/hdd/boot/loader/loader.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
137 cfg.write(loader_conf)
138 cfg.close()
139
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140 configfile = creator.ks.bootloader.configfile
141 custom_cfg = None
142 if configfile:
143 custom_cfg = get_custom_config(configfile)
144 if custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 # Use a custom configuration for systemd-boot
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 boot_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 logger.debug("Using custom configuration file "
148 "%s for systemd-boots's boot.conf", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 raise WicError("configfile is specified but failed to "
151 "get it from %s.", configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153 if not custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 # Create systemd-boot configuration using parameters from wks file
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500155 kernel = "/bzImage"
Brad Bishop19323692019-04-05 15:28:33 -0400156 title = source_params.get('title')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157
158 boot_conf = ""
Brad Bishop19323692019-04-05 15:28:33 -0400159 boot_conf += "title %s\n" % (title if title else "boot")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160 boot_conf += "linux %s\n" % kernel
161 boot_conf += "options LABEL=Boot root=%s %s\n" % \
162 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 if initrd:
165 boot_conf += "initrd /%s\n" % initrd
166
167 logger.debug("Writing systemd-boot config "
168 "%s/hdd/boot/loader/entries/boot.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
170 cfg.write(boot_conf)
171 cfg.close()
172
173
174 @classmethod
175 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
176 oe_builddir, bootimg_dir, kernel_dir,
177 native_sysroot):
178 """
179 Called before do_prepare_partition(), creates loader-specific config
180 """
181 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
183 install_cmd = "install -d %s/EFI/BOOT" % hdddir
184 exec_cmd(install_cmd)
185
186 try:
187 if source_params['loader'] == 'grub-efi':
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700188 cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500189 elif source_params['loader'] == 'systemd-boot':
190 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195
196
197 @classmethod
198 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
199 oe_builddir, bootimg_dir, kernel_dir,
200 rootfs_dir, native_sysroot):
201 """
202 Called to do the actual content population for a partition i.e. it
203 'prepares' the partition to be incorporated into the image.
204 In this case, prepare content for an EFI (grub) boot partition.
205 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500206 if not kernel_dir:
207 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
208 if not kernel_dir:
209 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500210
211 staging_kernel_dir = kernel_dir
212
213 hdddir = "%s/hdd/boot" % cr_workdir
214
215 install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \
216 (staging_kernel_dir, hdddir)
217 exec_cmd(install_cmd)
218
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500219
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 try:
221 if source_params['loader'] == 'grub-efi':
222 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
223 "%s/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]:
225 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:])
226 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 shutil.move("%s/grub.cfg" % cr_workdir,
228 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500229 elif source_params['loader'] == 'systemd-boot':
230 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]:
231 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:])
232 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 raise WicError("unrecognized bootimg-efi loader: %s" %
235 source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 startup = os.path.join(kernel_dir, "startup.nsh")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600240 if os.path.exists(startup):
241 cp_cmd = "cp %s %s/" % (startup, hdddir)
242 exec_cmd(cp_cmd, True)
243
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500244 du_cmd = "du -bks %s" % hdddir
245 out = exec_cmd(du_cmd)
246 blocks = int(out.split()[0])
247
248 extra_blocks = part.get_extra_block_count(blocks)
249
250 if extra_blocks < BOOTDD_EXTRA_SPACE:
251 extra_blocks = BOOTDD_EXTRA_SPACE
252
253 blocks += extra_blocks
254
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
256 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 # dosfs image, created by mkdosfs
259 bootimg = "%s/boot.img" % cr_workdir
260
Brad Bishop316dfdd2018-06-25 12:45:53 -0400261 dosfs_cmd = "mkdosfs -n efi -i %s -C %s %d" % \
262 (part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263 exec_native_cmd(dosfs_cmd, native_sysroot)
264
265 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
266 exec_native_cmd(mcopy_cmd, native_sysroot)
267
268 chmod_cmd = "chmod 644 %s" % bootimg
269 exec_cmd(chmod_cmd)
270
271 du_cmd = "du -Lbks %s" % bootimg
272 out = exec_cmd(du_cmd)
273 bootimg_size = out.split()[0]
274
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500275 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500276 part.source_file = bootimg