blob: 0eb86a079f4b96ee4f5725e8a4dd6f1955b7c89e [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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081 grubefi_conf = ""
82 grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n"
83 grubefi_conf += "default=boot\n"
84 grubefi_conf += "timeout=%s\n" % bootloader.timeout
85 grubefi_conf += "menuentry 'boot'{\n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087 kernel = "/bzImage"
88
89 grubefi_conf += "linux %s root=%s rootwait %s\n" \
90 % (kernel, creator.rootdev, bootloader.append)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070091
92 if initrd:
93 grubefi_conf += "initrd /%s\n" % initrd
94
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
128 cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
129 exec_cmd(cp_cmd, True)
130 else:
131 logger.debug("Ignoring missing initrd")
132
133 logger.debug("Writing systemd-boot config "
134 "%s/hdd/boot/loader/loader.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
136 cfg.write(loader_conf)
137 cfg.close()
138
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139 configfile = creator.ks.bootloader.configfile
140 custom_cfg = None
141 if configfile:
142 custom_cfg = get_custom_config(configfile)
143 if custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144 # Use a custom configuration for systemd-boot
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500145 boot_conf = custom_cfg
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 logger.debug("Using custom configuration file "
147 "%s for systemd-boots's boot.conf", configfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 raise WicError("configfile is specified but failed to "
150 "get it from %s.", configfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152 if not custom_cfg:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 # Create systemd-boot configuration using parameters from wks file
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 kernel = "/bzImage"
155
156 boot_conf = ""
157 boot_conf += "title boot\n"
158 boot_conf += "linux %s\n" % kernel
159 boot_conf += "options LABEL=Boot root=%s %s\n" % \
160 (creator.rootdev, bootloader.append)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 if initrd:
163 boot_conf += "initrd /%s\n" % initrd
164
165 logger.debug("Writing systemd-boot config "
166 "%s/hdd/boot/loader/entries/boot.conf", cr_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
168 cfg.write(boot_conf)
169 cfg.close()
170
171
172 @classmethod
173 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
174 oe_builddir, bootimg_dir, kernel_dir,
175 native_sysroot):
176 """
177 Called before do_prepare_partition(), creates loader-specific config
178 """
179 hdddir = "%s/hdd/boot" % cr_workdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180
181 install_cmd = "install -d %s/EFI/BOOT" % hdddir
182 exec_cmd(install_cmd)
183
184 try:
185 if source_params['loader'] == 'grub-efi':
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700186 cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500187 elif source_params['loader'] == 'systemd-boot':
188 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190 raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194
195 @classmethod
196 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
197 oe_builddir, bootimg_dir, kernel_dir,
198 rootfs_dir, native_sysroot):
199 """
200 Called to do the actual content population for a partition i.e. it
201 'prepares' the partition to be incorporated into the image.
202 In this case, prepare content for an EFI (grub) boot partition.
203 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 if not kernel_dir:
205 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
206 if not kernel_dir:
207 raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208
209 staging_kernel_dir = kernel_dir
210
211 hdddir = "%s/hdd/boot" % cr_workdir
212
213 install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \
214 (staging_kernel_dir, hdddir)
215 exec_cmd(install_cmd)
216
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218 try:
219 if source_params['loader'] == 'grub-efi':
220 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
221 "%s/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]:
223 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:])
224 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 shutil.move("%s/grub.cfg" % cr_workdir,
226 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500227 elif source_params['loader'] == 'systemd-boot':
228 for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]:
229 cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:])
230 exec_cmd(cp_cmd, True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232 raise WicError("unrecognized bootimg-efi loader: %s" %
233 source_params['loader'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235 raise WicError("bootimg-efi requires a loader, none specified")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 startup = os.path.join(kernel_dir, "startup.nsh")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600238 if os.path.exists(startup):
239 cp_cmd = "cp %s %s/" % (startup, hdddir)
240 exec_cmd(cp_cmd, True)
241
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242 du_cmd = "du -bks %s" % hdddir
243 out = exec_cmd(du_cmd)
244 blocks = int(out.split()[0])
245
246 extra_blocks = part.get_extra_block_count(blocks)
247
248 if extra_blocks < BOOTDD_EXTRA_SPACE:
249 extra_blocks = BOOTDD_EXTRA_SPACE
250
251 blocks += extra_blocks
252
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500253 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
254 extra_blocks, part.mountpoint, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 # dosfs image, created by mkdosfs
257 bootimg = "%s/boot.img" % cr_workdir
258
Brad Bishop316dfdd2018-06-25 12:45:53 -0400259 dosfs_cmd = "mkdosfs -n efi -i %s -C %s %d" % \
260 (part.fsuuid, bootimg, blocks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500261 exec_native_cmd(dosfs_cmd, native_sysroot)
262
263 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
264 exec_native_cmd(mcopy_cmd, native_sysroot)
265
266 chmod_cmd = "chmod 644 %s" % bootimg
267 exec_cmd(chmod_cmd)
268
269 du_cmd = "du -Lbks %s" % bootimg
270 out = exec_cmd(du_cmd)
271 bootimg_size = out.split()[0]
272
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500273 part.size = int(bootimg_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500274 part.source_file = bootimg