Creating custom Yocto partition with source plugin

Recently I had to create a custom Yocto partition with just one file containing the autoboot.txt file (for RPI).
So I created custom Yocto Source plugin with following contents:

import logging
import os
import re

from wic import WicError
from wic.engine import get_custom_config
from wic.pluginbase import SourcePlugin
from wic.misc import (exec_cmd, exec_native_cmd,
                      get_bitbake_var, BOOTDD_EXTRA_SPACE)

logger = logging.getLogger('wic')

from time import sleep

class AutobootPlugin(SourcePlugin):
    name = 'autoboot'

    ### cr_workdir      ./build/tmp/work/raspberrypi5-poky-linux/core-image/1.0/tmp-wic
    ### oe_builddir     ./build
    ### bootimg_dir     ./build/tmp/work/raspberrypi5-poky-linux/core-image/1.0/recipe-sysroot/usr/share
    ### kernel_dir      ./build/tmp/deploy/images/raspberrypi5
    ### rootfs_dir      {'ROOTFS_DIR':'./build/tmp/work/raspberrypi5-poky-linux/core-image/1.0/rootfs'}
    ### native_sysroot  ./build/tmp/work/raspberrypi5-poky-linux/core-image/1.0/recipe-sysroot-native


    @staticmethod
    def create_dir(path, cr_workdir):
        # Create hdd image dir
        newdir = path % cr_workdir
        install_cmd = "install -d %s" % newdir
        exec_cmd(install_cmd)
        return newdir

    @staticmethod
    def create_partition(part, hdddir, native_sysroot):
        sector_size = 512
        label = part.label if part.label else "autoboot"
        bootimg = "%s/boot%s.img" % (hdddir, part.lineno)
        blocks = 5 * 1024 * 1024 / sector_size
        dosfs_cmd = "mkdosfs -n %s -i %s -S 512 -C %s %d" % \
                    (label, part.fsuuid, bootimg, blocks)
        exec_native_cmd(dosfs_cmd, native_sysroot)
        return bootimg

    @staticmethod
    def write_autoboot_file(part_dir, native_sysroot):
        # https://forums.raspberrypi.com/viewtopic.php?t=341372#p2045732
        contents = """
[all]
tryboot_a_b=1
boot_partition=2
[tryboot]
boot_partition=3
        """
        autoboot_path = "%s/autoboot.txt" % part_dir
        logger.info("Writing autoboot contents to %s" % autoboot_path)
        with open(autoboot_path, "w+") as f:
            f.write(contents)


    @staticmethod
    def copy_files_to_fs_image(bootimg, part_dir, native_sysroot):
        mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, part_dir)
        exec_native_cmd(mcopy_cmd, native_sysroot)

    @classmethod
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
                             native_sysroot):
        # Create hdd image dir
        hdddir = cls.create_dir("%s/hdd/boot", cr_workdir)
        part_dir = cls.create_dir("%s/part", cr_workdir)

        # Create dos partition
        bootimg_path = cls.create_partition(part, hdddir, native_sysroot)
        part.source_file = bootimg_path
        cls.write_autoboot_file(part_dir, native_sysroot)
        cls.copy_files_to_fs_image(bootimg_path, part_dir, native_sysroot)

    @classmethod
    def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
                        bootimg_dir, kernel_dir, native_sysroot):
        """
        Called after all partitions have been prepared and assembled into a
        disk image.  This provides a hook to allow finalization of a
        disk image e.g. to write an MBR to it.
        """
        logger.debug("SourcePlugin: do_install_disk: disk: %s %s", disk, disk_name)

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *