2.4. Using devtool to Patch the Kernel

The steps in this procedure show you how you can patch the kernel using the extensible SDK and devtool.

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 to Develop Using devtool" 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.

This example 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 to Develop Using devtool" Section.

  1. Check Out the Kernel Source Files: First you must use devtool to checkout the kernel source code in its workspace. Be sure you are in the terminal set up to do work with the extensible SDK.

    Note

    See this step in the "Getting Ready to Develop Using devtool" section for more information.

    Use the following devtool command to check out the code:

         $ devtool modify linux-yocto
                        

    Note

    During the checkout operation, a bug exists that could cause errors such as the following to appear:
         ERROR: Taskhash mismatch 2c793438c2d9f8c3681fd5f7bc819efa versus
                be3a89ce7c47178880ba7bf6293d7404 for
                /path/to/esdk/layers/poky/meta/recipes-kernel/linux/linux-yocto_4.10.bb.do_unpack
                            
    You can safely ignore these messages. The source code is correctly checked out.

  2. Edit the Source Files Follow these steps to make some simple changes to the source files:

    1. Change the working directory: In the previous step, the output noted where you can find the source files (e.g. ~/poky_sdk/workspace/sources/linux-yocto). Change to where the kernel source code is before making your edits to the calibrate.c file:

           $ cd ~/poky_sdk/workspace/sources/linux-yocto
                                  

    2. Edit the source file: Edit the init/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)) {
                     .
                     .
                     .
                                  

  3. Build the Updated Kernel Source: To build the updated kernel source, use devtool:

         $ devtool build linux-yocto
                        

  4. Create the Image With the New Kernel: Use the devtool build-image command to create a new image that has the new kernel.

    Note

    If the image you originally created resulted in a Wic file, you can use an alternate method to create the new image with the updated kernel. For an example, see the steps in the TipsAndTricks/KernelDevelopmentWithEsdk Wiki Page.

         $ cd ~
         $ devtool build-image core-image-minimal
                        

  5. Test the New Image: For this example, you can run the new image using QEMU to verify your changes:

    1. Boot the image: Boot the modified image in the QEMU emulator using this command:

           $ runqemu qemux86
                                  

    2. Verify the changes: Log into the machine using root with no password and then use the following shell command to scroll through the console's boot output.

           # dmesg | less
                                  

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

  6. Stage and commit your changes: Within your eSDK terminal, change your working directory to where you modified the calibrate.c file and use these Git commands to stage and commit your changes:

         $ cd ~/poky_sdk/workspace/sources/linux-yocto
         $ git status
         $ git add init/calibrate.c
         $ git commit -m "calibrate: Add printk example"
                        

  7. Export the Patches and Create an Append File: To export your commits as patches and create a .bbappend file, use the following command in the terminal used to work with the extensible SDK. This example uses the previously established layer named meta-mylayer.

    Note

    See Step 3 of the "Getting Ready to Develop Using devtool" section for information on setting up this layer.

         $ devtool finish linux-yocto ~/meta-mylayer
                        

    Once the command finishes, the patches and the .bbappend file are located in the ~/meta-mylayer/recipes-kernel/linux directory.

  8. Build the Image With Your Modified Kernel: You can now build an image that includes your kernel patches. Execute the following command from your Build Directory in the terminal set up to run BitBake:

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