Openembedded Core Discussions
 help / color / mirror / Atom feed
From: Vincent Davis Jr <vince@underview.tech>
To: openembedded-core@lists.openembedded.org
Cc: Vincent Davis Jr <vince@underview.tech>
Subject: [PATCH v8 6/9] bootimg_pcbios: add funcs to configure booting with grub
Date: Thu, 14 Aug 2025 00:25:49 -0400	[thread overview]
Message-ID: <20250814042552.21887-6-vince@underview.tech> (raw)
In-Reply-To: <20250814042552.21887-1-vince@underview.tech>

Functions added, but not executed during
wic image creation include:

_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 if none specified. Both
	   variables are required by grub-mkimage.
	   * GRUB_MKIMAGE_FORMAT_PC is used to define
	     target platform.
	   * GRUB_PREFIX_PATH is 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 bytes of the resulting wic image. dd grub
	   stage 1 to wic image. If this wics plugin is used
	   with GPT as partition table format and grub selected
	   as bootloader it's more than likely for grub hybrid
	   booting because bootimg_efi plugin should and more
	   than likely will be used in that case. So, boot.img
	   may be dd regardless if partition table format is
	   GPT or MBR.
	2. dd custom core.img (grub stage 1.5) with embedded
	   configuration to the resulting wic image starting
	   at byte 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(+)

diff --git a/scripts/lib/wic/plugins/source/bootimg_pcbios.py b/scripts/lib/wic/plugins/source/bootimg_pcbios.py
index a4fabec0ae..f50a5ae0e2 100644
--- a/scripts/lib/wic/plugins/source/bootimg_pcbios.py
+++ b/scripts/lib/wic/plugins/source/bootimg_pcbios.py
@@ -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)
-- 
2.34.1



  parent reply	other threads:[~2025-08-14  4:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-14  4:25 [PATCH v8 1/9] bootimg_pcbios: move syslinux install into seperate functions Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 2/9] bootimg_pcbios: move syslinux funcs to end of file Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 3/9] bootimg_pcbios: seperate bootloader config creation Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 4/9] bootimg_pcbios: cleanup _do_configure_syslinux function Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 5/9] bootimg_pcbios: cleanup prepare and install syslinux funcs Vincent Davis Jr
2025-08-14  4:25 ` Vincent Davis Jr [this message]
2025-08-14  4:25 ` [PATCH v8 7/9] bootimg_pcbios: include grub as an optional bootloader Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 8/9] bootimg_pcbios: add help and usage comments Vincent Davis Jr
2025-08-14  4:25 ` [PATCH v8 9/9] oe-selftest: add wic.Wic.test_grub_install_pcbios Vincent Davis Jr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250814042552.21887-6-vince@underview.tech \
    --to=vince@underview.tech \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox