2.5. Using Traditional Kernel Development to Patch the Kernel

The steps in this procedure show you how you can patch the kernel using traditional kernel development (i.e. not using devtool and the extensible SDK as described in the "Using devtool to Patch the Kernel" section).

Note

Before attempting this procedure, be sure you have performed the steps to get ready for updating the kernel as described in the "Getting Ready for Traditional Kernel Development" section.

Patching the kernel involves changing or adding configurations to an existing kernel, changing or adding recipes to the kernel that are needed to support specific hardware features, or even altering the source code itself.

The example in this section creates a simple patch by adding some QEMU emulator console output at boot time through printk statements in the kernel's calibrate.c source code file. Applying the patch and booting the modified image causes the added messages to appear on the emulator's console. The example is a continuation of the setup procedure found in the "Getting Ready for Traditional Kernel Development" Section.

  1. Edit the Source Files Prior to this step, you should have used Git to create a local copy of the repository for your kernel. Assuming you created the repository as directed in the "Getting Ready for Traditional Kernel Development" section, use the following commands to edit the calibrate.c file:

    1. Change the working directory: You need to locate the source files in the local copy of the kernel Git repository: Change to where the kernel source code is before making your edits to the calibrate.c file:

           $ cd ~/linux-yocto-4.12/init
                                  

    2. Edit the source file: Edit the calibrate.c file to have the following changes:

           void calibrate_delay(void)
           {
               unsigned long lpj;
               static bool printed;
               int this_cpu = smp_processor_id();
      
               printk("*************************************\n");
               printk("*                                   *\n");
               printk("*        HELLO YOCTO KERNEL         *\n");
               printk("*                                   *\n");
               printk("*************************************\n");
      
           	if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
                     .
                     .
                     .
                                  

  2. Stage and Commit Your Changes: Use standard Git commands to stage and commit the changes you just made:

         $ git add calibrate.c
         $ git commit -m "calibrate.c - Added some printk statements"
                        

    If you do not stage and commit your changes, the OpenEmbedded Build System will not pick up the changes.

  3. Update Your local.conf File to Point to Your Source Files: In addition to your local.conf file specifying to use "kernel-modules" and the "qemux86" machine, it must also point to the updated kernel source files. Add SRC_URI and SRCREV statements similar to the following to your local.conf:

         $ cd ~/poky/build/conf
                        

    Add the following to the local.conf:

         SRC_URI_pn-linux-yocto = "git:///path-to/linux-yocto-4.12;protocol=file;name=machine;branch=standard/base; \
                                   git:///path-to/yocto-kernel-cache;protocol=file;type=kmeta;name=meta;branch=yocto-4.12;destsuffix=${KMETA}"
         SRCREV_meta_qemux86 = "${AUTOREV}"
         SRCREV_machine_qemux86 = "${AUTOREV}"
                        

    Note

    Be sure to replace path-to with the pathname to your local Git repositories. Also, you must be sure to specify the correct branch and machine types. For this example, the branch is standard/base and the machine is "qemux86".

  4. Build the Image: With the source modified, your changes staged and committed, and the local.conf file pointing to the kernel files, you can now use BitBake to build the image:

         $ cd ~/poky/build
         $ bitbake core-image-minimal
                        

  5. Boot the image: Boot the modified image in the QEMU emulator using this command. When prompted to login to the QEMU console, use "root" with no password:

         $ cd ~/poky/build
         $ runqemu qemux86
                        

  6. Look for Your Changes: As QEMU booted, you might have seen your changes rapidly scroll by. If not, use these commands to see your changes:

         # dmesg | less
                        

    You should see the results of your printk statements as part of the output when you scroll down the console window.

  7. Generate the Patch File: Once you are sure that your patch works correctly, you can generate a *.patch file in the kernel source repository:

         $ cd ~/linux-yocto-4.12/init
         $ git format-patch -1
         0001-calibrate.c-Added-some-printk-statements.patch
                        

  8. Move the Patch File to Your Layer: In order for subsequent builds to pick up patches, you need to move the patch file you created in the previous step to your layer meta-mylayer. For this example, the layer created earlier is located in your home directory as meta-mylayer. When the layer was created using the yocto-create script, no additional hierarchy was created to support patches. Before moving the patch file, you need to add additional structure to your layer using the following commands:

         $ cd ~/meta-mylayer
         $ mkdir recipes-kernel
         $ mkdir recipes-kernel/linux
         $ mkdir recipes-kernel/linux/linux-yocto
                        

    Once you have created this hierarchy in your layer, you can move the patch file using the following command:

         $ mv ~/linux-yocto-4.12/init/0001-calibrate.c-Added-some-printk-statements.patch ~/meta-mylayer/recipes-kernel/linux/linux-yocto
                        

  9. Create the Append File: Finally, you need to create the linux-yocto_4.12.bbappend file and insert statements that allow the OpenEmbedded build system to find the patch. The append file needs to be in your layer's recipes-kernel/linux directory and it must be named linux-yocto_4.12.bbappend and have the following contents:

         FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
    
         SRC_URI_append = " file://0001-calibrate.c-Added-some-printk-statements.patch"
                        

    The FILESEXTRAPATHS and SRC_URI statements enable the OpenEmbedded build system to find the patch file.

    For more information on append files and patches, see the "Creating the Append File" and "Applying Patches" sections. You can also see the "Using .bbappend Files in Your Layer"" section in the Yocto Project Development Tasks Manual.

    Note

    To build core-image-minimal again and see the effects of your patch, you can essentially eliminate the temporary source files saved in poky/build/tmp/work/... and residual effects of the build by entering the following sequence of commands:
         $ cd ~/poky/build
         $ bitbake -c cleanall yocto-linux
         $ bitbake core-image-minimal -c cleanall
         $ bitbake core-image-minimal
         $ runqemu qemux86