@@ -13,7 +13,9 @@
import logging
import os
import re
+import shutil
+from glob import glob
from wic import WicError
from wic.engine import get_custom_config
from wic.pluginbase import SourcePlugin
@@ -72,6 +74,27 @@ class BootimgPcbiosPlugin(SourcePlugin):
cls._do_prepare_syslinux(part, cr_workdir, bootimg_dir,
kernel_dir, native_sysroot)
+ @classmethod
+ def _get_staging_libdir(cls):
+ """
+ For unknown reasons when running test with poky
+ STAGING_LIBDIR gets unset when wic create is executed.
+ Bellow is a hack to determine what STAGING_LIBDIR should
+ be if not specified.
+ """
+
+ staging_libdir = get_bitbake_var('STAGING_LIBDIR')
+ staging_dir_target = get_bitbake_var('STAGING_DIR_TARGET')
+
+ if not staging_libdir:
+ staging_libdir = '%s/usr/lib64' % staging_dir_target
+ if not os.path.isdir(staging_libdir):
+ staging_libdir = '%s/usr/lib32' % staging_dir_target
+ if not os.path.isdir(staging_libdir):
+ staging_libdir = '%s/usr/lib' % staging_dir_target
+
+ return staging_libdir
+
@classmethod
def _get_bootloader_config(cls, bootloader, loader):
custom_cfg = None
@@ -243,3 +266,147 @@ class BootimgPcbiosPlugin(SourcePlugin):
dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
exec_cmd(dd_cmd, native_sysroot)
+
+ @classmethod
+ def _do_configure_grub(cls, part, creator, cr_workdir):
+ hdddir = "%s/hdd" % cr_workdir
+ bootloader = creator.ks.bootloader
+
+ grub_conf = cls._get_bootloader_config(bootloader, 'grub')
+
+ grub_prefix_path = get_bitbake_var('GRUB_PREFIX_PATH')
+ if not grub_prefix_path:
+ grub_prefix_path = '/boot/grub'
+
+ grub_path = "%s/%s" %(hdddir, grub_prefix_path)
+ install_cmd = "install -d %s" % grub_path
+ exec_cmd(install_cmd)
+
+ if not grub_conf:
+ # Set a default timeout if none specified to avoid
+ # 'None' being the value placed within the configuration
+ # file.
+ if not bootloader.timeout:
+ bootloader.timeout = 500
+
+ # Set a default kernel params string if none specified
+ # to avoid 'None' being the value placed within the
+ # configuration file.
+ if not bootloader.append:
+ bootloader.append = "rootwait rootfstype=%s " % (part.fstype)
+ bootloader.append += "console=ttyS0,115200 console=tty0"
+
+ kernel = "/boot/" + get_bitbake_var("KERNEL_IMAGETYPE")
+
+ grub_conf = 'serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n'
+ grub_conf += 'set gfxmode=auto\n'
+ grub_conf += 'set gfxpayload=keep\n\n'
+ grub_conf += 'set default=0\n\n'
+ grub_conf += '# Boot automatically after %d secs.\n' % (bootloader.timeout)
+ grub_conf += 'set timeout=%d\n\n' % (bootloader.timeout)
+ grub_conf += 'menuentry \'default\' {\n'
+ grub_conf += '\tsearch --no-floppy --set=root --file %s\n' % (kernel)
+ grub_conf += '\tprobe --set partuuid --part-uuid ($root)\n'
+ grub_conf += '\tlinux %s root=PARTUUID=$partuuid %s\n}\n' % \
+ (kernel, bootloader.append)
+
+ logger.debug("Writing grub config %s/grub.cfg", grub_path)
+ cfg = open("%s/grub.cfg" % grub_path, "w")
+ cfg.write(grub_conf)
+ cfg.close()
+
+ @classmethod
+ def _do_prepare_grub(cls, part, cr_workdir, oe_builddir,
+ kernel_dir, rootfs_dir, native_sysroot):
+ """
+ 1. Generate embed.cfg that'll later be embedded into core.img.
+ So, that core.img knows where to search for grub.cfg.
+ 2. Generate core.img or grub stage 1.5.
+ 3. Copy modules into partition.
+ 4. Create partition rootfs file.
+ """
+
+ hdddir = "%s/hdd" % cr_workdir
+
+ copy_types = [ '*.mod', '*.o', '*.lst' ]
+
+ builtin_modules = 'boot linux ext2 fat serial part_msdos part_gpt \
+ normal multiboot probe biosdisk msdospart configfile search loadenv test'
+
+ staging_libdir = cls._get_staging_libdir()
+
+ grub_format = get_bitbake_var('GRUB_MKIMAGE_FORMAT_PC')
+ if not grub_format:
+ grub_format = 'i386-pc'
+
+ grub_prefix_path = get_bitbake_var('GRUB_PREFIX_PATH')
+ if not grub_prefix_path:
+ grub_prefix_path = '/boot/grub'
+
+ grub_path = "%s/%s" %(hdddir, grub_prefix_path)
+ core_img = '%s/grub-bios-core.img' % (kernel_dir)
+ grub_mods_path = '%s/grub/%s' % (staging_libdir, grub_format)
+
+ # Generate embedded grub config
+ embed_cfg_str = 'search.file %s/grub.cfg root\n' % (grub_prefix_path)
+ embed_cfg_str += 'set prefix=($root)%s\n' % (grub_prefix_path)
+ embed_cfg_str += 'configfile ($root)%s/grub.cfg\n' % (grub_prefix_path)
+ cfg = open('%s/embed.cfg' % (kernel_dir), 'w+')
+ cfg.write(embed_cfg_str)
+ cfg.close()
+
+ # core.img doesn't get included into boot partition
+ # it's later dd onto the resulting wic image.
+ grub_mkimage = 'grub-mkimage \
+ --prefix=%s \
+ --format=%s \
+ --config=%s/embed.cfg \
+ --directory=%s \
+ --output=%s %s' % \
+ (grub_prefix_path, grub_format, kernel_dir,
+ grub_mods_path, core_img, builtin_modules)
+ exec_native_cmd(grub_mkimage, native_sysroot)
+
+ # Copy grub modules
+ install_dir = '%s/%s/%s' % (hdddir, grub_prefix_path, grub_format)
+ os.makedirs(install_dir, exist_ok=True)
+
+ for ctype in copy_types:
+ files = glob('%s/grub/%s/%s' % \
+ (staging_libdir, grub_format, ctype))
+ for file in files:
+ shutil.copy2(file, install_dir, follow_symlinks=True)
+
+ # Create boot partition
+ logger.debug('Prepare partition using rootfs in %s', hdddir)
+ part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
+ native_sysroot, False)
+
+ @classmethod
+ def _do_install_grub(cls, creator, kernel_dir,
+ native_sysroot, full_path):
+ core_img = '%s/grub-bios-core.img' % (kernel_dir)
+
+ staging_libdir = cls._get_staging_libdir()
+
+ grub_format = get_bitbake_var('GRUB_MKIMAGE_FORMAT_PC')
+ if not grub_format:
+ grub_format = 'i386-pc'
+
+ boot_img = '%s/grub/%s/boot.img' % (staging_libdir, grub_format)
+ if not os.path.exists(boot_img):
+ raise WicError("Couldn't find %s. Did you include "
+ "do_image_wic[depends] += \"grub:do_populate_sysroot\" "
+ "in your image recipe" % boot_img)
+
+ # Install boot.img or grub stage 1
+ dd_cmd = "dd if=%s of=%s conv=notrunc bs=1 seek=0 count=440" % (boot_img, full_path)
+ exec_cmd(dd_cmd, native_sysroot)
+
+ if creator.ptable_format == 'msdos':
+ # Install core.img or grub stage 1.5
+ dd_cmd = "dd if=%s of=%s conv=notrunc bs=1 seek=512" % (core_img, full_path)
+ exec_cmd(dd_cmd, native_sysroot)
+ else:
+ raise WicError("Unsupported partition table: %s" %
+ creator.ptable_format)
Functions added, but not included into SourcePlugin overridable functions. _get_staging_libdir * Finds target lib directory if for some reason STAGING_LIBDIR isn't set. do_configure_grub * Will search for a grub configuration passed via bootloader --configfile. If not found build a default one which searches for partition that contains the given the kernel name via grub search module. do_prepare_grub 1. Sets default values for GRUB_MKIMAGE_FORMAT_PC and GRUB_PREFIX_PATH. Both variables are required by grub-mkimage. * GRUB_MKIMAGE_FORMAT_PC used to define target platform. * GRUB_PREFIX_PATH used to define which directory grub config and modules are going to reside in. 2. Generates grub config to embed into core.img. This config is used to search for partition containing grub config. 3. Creates a custom core.img or grub stage 1.5 with an embedded grub config. 4. Copies all the target built grub modules into GRUB_PREFIX_PATH directory. 5. Creates boot partition do_install_grub 1. dd target platform specific boot.img to the first 0-440 sectors of the resulting wic image. dd grub stage 1 to wic image. 2. dd custom core.img (grub stage 1.5) with embedded configuration to the resulting wic image starting at sector 512 up to sizeof(core.img). 3. Both boot.img and core.img are required for legacy bios boot. See grub Wiki for more details on boot.img and core.img. https://en.wikipedia.org/wiki/GNU_GRUB Commit also imports python modules required by the above implemented functions. Signed-off-by: Vincent Davis Jr <vince@underview.tech> --- .../lib/wic/plugins/source/bootimg_pcbios.py | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+)