summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/plugins/source/bootimg-efi.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/plugins/source/bootimg-efi.py')
-rw-r--r--scripts/lib/wic/plugins/source/bootimg-efi.py236
1 files changed, 236 insertions, 0 deletions
diff --git a/scripts/lib/wic/plugins/source/bootimg-efi.py b/scripts/lib/wic/plugins/source/bootimg-efi.py
new file mode 100644
index 0000000000..e4067b6dbf
--- /dev/null
+++ b/scripts/lib/wic/plugins/source/bootimg-efi.py
@@ -0,0 +1,236 @@
1# 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
27import os
28import shutil
29import re
30import tempfile
31
32from wic import kickstart, msger
33from wic.utils import misc, fs_related, errors, runner, cmdln
34from wic.conf import configmgr
35from wic.plugin import pluginmgr
36import wic.imager.direct as direct
37from wic.pluginbase import SourcePlugin
38from wic.utils.oe.misc import *
39from wic.imager.direct import DirectImageCreator
40
41class BootimgEFIPlugin(SourcePlugin):
42 name = 'bootimg-efi'
43
44 @classmethod
45 def do_configure_grubefi(self, hdddir, cr, cr_workdir):
46 """
47 Create loader-specific (grub-efi) config
48 """
49 splash = os.path.join(cr_workdir, "/EFI/boot/splash.jpg")
50 if os.path.exists(splash):
51 splashline = "menu background splash.jpg"
52 else:
53 splashline = ""
54
55 (rootdev, root_part_uuid) = cr._get_boot_config()
56 options = cr.ks.handler.bootloader.appendLine
57
58 grubefi_conf = ""
59 grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n"
60 grubefi_conf += "default=boot\n"
61 timeout = kickstart.get_timeout(cr.ks)
62 if not timeout:
63 timeout = 0
64 grubefi_conf += "timeout=%s\n" % timeout
65 grubefi_conf += "menuentry 'boot'{\n"
66
67 kernel = "/bzImage"
68
69 if cr._ptable_format == 'msdos':
70 rootstr = rootdev
71 else:
72 raise ImageError("Unsupported partition table format found")
73
74 grubefi_conf += "linux %s root=%s rootwait %s\n" \
75 % (kernel, rootstr, options)
76 grubefi_conf += "}\n"
77 if splashline:
78 syslinux_conf += "%s\n" % splashline
79
80 msger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg" \
81 % cr_workdir)
82 cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w")
83 cfg.write(grubefi_conf)
84 cfg.close()
85
86 @classmethod
87 def do_configure_gummiboot(self, hdddir, cr, cr_workdir):
88 """
89 Create loader-specific (gummiboot) config
90 """
91 install_cmd = "install -d %s/loader" % hdddir
92 exec_cmd(install_cmd)
93
94 install_cmd = "install -d %s/loader/entries" % hdddir
95 exec_cmd(install_cmd)
96
97 (rootdev, root_part_uuid) = cr._get_boot_config()
98 options = cr.ks.handler.bootloader.appendLine
99
100 timeout = kickstart.get_timeout(cr.ks)
101 if not timeout:
102 timeout = 0
103
104 loader_conf = ""
105 loader_conf += "default boot\n"
106 loader_conf += "timeout %d\n" % timeout
107
108 msger.debug("Writing gummiboot config %s/hdd/boot/loader/loader.conf" \
109 % cr_workdir)
110 cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
111 cfg.write(loader_conf)
112 cfg.close()
113
114 kernel = "/bzImage"
115
116 if cr._ptable_format == 'msdos':
117 rootstr = rootdev
118 else:
119 raise ImageError("Unsupported partition table format found")
120
121 boot_conf = ""
122 boot_conf += "title boot\n"
123 boot_conf += "linux %s\n" % kernel
124 boot_conf += "options LABEL=Boot root=%s %s\n" \
125 % (rootstr, options)
126
127 msger.debug("Writing gummiboot config %s/hdd/boot/loader/entries/boot.conf" \
128 % cr_workdir)
129 cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
130 cfg.write(boot_conf)
131 cfg.close()
132
133
134 @classmethod
135 def do_configure_partition(self, part, source_params, cr, cr_workdir,
136 oe_builddir, bootimg_dir, kernel_dir,
137 native_sysroot):
138 """
139 Called before do_prepare_partition(), creates loader-specific config
140 """
141 hdddir = "%s/hdd/boot" % cr_workdir
142 rm_cmd = "rm -rf %s" % cr_workdir
143 exec_cmd(rm_cmd)
144
145 install_cmd = "install -d %s/EFI/BOOT" % hdddir
146 exec_cmd(install_cmd)
147
148 try:
149 if source_params['loader'] == 'grub-efi':
150 self.do_configure_grubefi(hdddir, cr, cr_workdir)
151 elif source_params['loader'] == 'gummiboot':
152 self.do_configure_gummiboot(hdddir, cr, cr_workdir)
153 else:
154 msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader'])
155 except KeyError:
156 msger.error("bootimg-efi requires a loader, none specified")
157
158
159 @classmethod
160 def do_prepare_partition(self, part, source_params, cr, cr_workdir,
161 oe_builddir, bootimg_dir, kernel_dir,
162 rootfs_dir, native_sysroot):
163 """
164 Called to do the actual content population for a partition i.e. it
165 'prepares' the partition to be incorporated into the image.
166 In this case, prepare content for an EFI (grub) boot partition.
167 """
168 if not bootimg_dir:
169 bootimg_dir = get_bitbake_var("HDDDIR")
170 if not bootimg_dir:
171 msger.error("Couldn't find HDDDIR, exiting\n")
172 # just so the result notes display it
173 cr.set_bootimg_dir(bootimg_dir)
174
175 staging_kernel_dir = kernel_dir
176
177 hdddir = "%s/hdd/boot" % cr_workdir
178
179 install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \
180 (staging_kernel_dir, hdddir)
181 exec_cmd(install_cmd)
182
183 try:
184 if source_params['loader'] == 'grub-efi':
185 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
186 "%s/grub.cfg" % cr_workdir)
187 cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir)
188 exec_cmd(cp_cmd, True)
189 shutil.move("%s/grub.cfg" % cr_workdir,
190 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
191 elif source_params['loader'] == 'gummiboot':
192 cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir)
193 exec_cmd(cp_cmd, True)
194 else:
195 msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader'])
196 except KeyError:
197 msger.error("bootimg-efi requires a loader, none specified")
198
199 du_cmd = "du -bks %s" % hdddir
200 out = exec_cmd(du_cmd)
201 blocks = int(out.split()[0])
202
203 extra_blocks = part.get_extra_block_count(blocks)
204
205 if extra_blocks < BOOTDD_EXTRA_SPACE:
206 extra_blocks = BOOTDD_EXTRA_SPACE
207
208 blocks += extra_blocks
209
210 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
211 (extra_blocks, part.mountpoint, blocks))
212
213 # Ensure total sectors is an integral number of sectors per
214 # track or mcopy will complain. Sectors are 512 bytes, and we
215 # generate images with 32 sectors per track. This calculation is
216 # done in blocks, thus the mod by 16 instead of 32.
217 blocks += (16 - (blocks % 16))
218
219 # dosfs image, created by mkdosfs
220 bootimg = "%s/boot.img" % cr_workdir
221
222 dosfs_cmd = "mkdosfs -n efi -C %s %d" % (bootimg, blocks)
223 exec_native_cmd(dosfs_cmd, native_sysroot)
224
225 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
226 exec_native_cmd(mcopy_cmd, native_sysroot)
227
228 chmod_cmd = "chmod 644 %s" % bootimg
229 exec_cmd(chmod_cmd)
230
231 du_cmd = "du -Lbms %s" % bootimg
232 out = exec_cmd(du_cmd)
233 bootimg_size = out.split()[0]
234
235 part.set_size(bootimg_size)
236 part.set_source_file(bootimg)