RDKB: Add hello world kernel module

In this section, you are going to learn

How to create a simple kernel module in rdkb ?

How to generate full rdkb image for RPI-4B ?

How to flash full rdkb image for RPI-4B ?

How to run minicom and work remotely with RPI-4B ?

  • Step 1: Install all the required packages

     $ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib \
       build-essential chrpath socat cpio python python3 python3-pip python3-pexpect \
       xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev \
       pylint3 xterm bmap-tools
    
    $ sudo apt-get install git cmake autoconf texinfo openjdk-8-jdk openjdk-8-jre \
       m4 libtool libtool-bin curl pkg-config lib32z1 doxygen
    
  • Step 2: Setup the repository

    $ mkdir ~/bin
    $ PATH=~/bin:$PATH
    
  • Step 3: Download the repo tool and ensure that it works

    $ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    $ chmod a+x ~/bin/repo
    
  • Step 4: Edit the netrc file and add the following contents

    $ vi ~/.netrc
    
    machine code.rdkcentral.com
    login <YOUR_USERNAME>
    password <YOUR_PASSWORD>
    
  • NOTE: In order to repo the rdk source code, accounts need to be created in rdkcentral.com and that ssh key needs to be added.

  • Step 5: Create a repo file and replace it in ~/bin/repo

    $ vi repo
    $ cp -avrf bin ~/bin/repo
    
  • Step 6: Create the workspace directory

$ mkdir rdkb_build
$ cd rdbk_build
  • Step 7: Build steps for base rdkb image.

    • Initiate and sync the repository.

    $ repo init -u https://code.rdkcentral.com/r/manifests -b dunfell -m rdkb-extsrc.xml
    $ repo sync -j nproc --no-clone-bundle
    
    • Add the machine information using the following command

    $ MACHINE=raspberrypi4-64-rdk-broadband source meta-cmf-raspberrypi/setup-environment
    
    • Run bitbake to build the rdkb image.

    $ bitbake rdk-generic-broadband-image
    
    • Once build is done the image will be generated in tmp/deploy/raspberrypi4-64/images/

  • Step 8: Flash the image

    • Extract the .wic image using the bzip2 command.

    $ bzip2 -d rdk-generic-broadband-image-raspberrypi4-64-rdk-broadband.wic.bz2
    
    • Flash the image into sd card using this command

    $ sudo -E bmaptool copy --nobmap rdk-generic-broadband-image-raspberrypi4-64-rdk-broadband.wic /dev/sdb
    

In this section you will learn how to add custom kernel module

$ pwd
$HOME/rdkb/build
  • Create a new meta layer using create layer and add layer command.

bitbake-layers create-layer ../meta-mykernelmodule
bitbake-layers add-layer meta-mykernelmodule
  • Create the recipes directory inside the meta-mykernelmodule.

$ mkdir recipes-kernel
$ cd recipes-kernel
$ mkdir hello-mod
$ cd hello-mod
$ mkdir files
$ vi meta-mykernelmodule/recipes-kernel/hello-mod/files/hello.c
#include "linux/init.h"
#include "linux/kernel.h"
#include "linux/module.h"

void module_function(void)
{
      printk(KERN_INFO "\n Module Function\n");
}

static int __init start_init(void)
{

    printk(KERN_INFO "\n\nStarted Kernel Module\n\n");
    printk(KERN_INFO "\nHello, world\n");
    module_function();
    return 0;
}

static void __exit stop_exit(void)
{
    printk(KERN_INFO "\n\nStopping Kernel Module\n\n");
}

module_init(start_init);
module_exit(stop_exit);

MODULE_LICENSE("GPL");
$ vi meta-mykernelmodule/recipes-kernel/hello-mod/hello-mod.bb
SUMMARY = "Hello World Kernel Module"
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=12f884d2ae1ff87c09e5b7ccc2c4ca7e"

inherit module

SRC_URI = "file://Makefile \
           file://hello.c \
           file://COPYING \
          "

S = "${WORKDIR}"

# The inherit of module.bbclass will automatically name module packages with
# "kernel-module-" prefix as required by the oe-core build environment.

RPROVIDES_${PN} += "kernel-module-hello"
SUMMARY = "Hello World Kernel Module"
LICENSE = "GPLv2"
  • Summary gives a basic definition about the kernel module.

  • License is used to specify the license related information for the recipe.

LIC_FILES_CHKSUM = "file://COPYING;md5=12f884d2ae1ff87c09e5b7ccc2c4ca7e"

SRC_URI = "file://Makefile \
           file://hello.c \
           file://COPYING \
        "

S = "${WORKDIR}"
  • SRC_URI is used to indicate the uri for the recipe source files.

  • file://” denote that the source code is present in the same directory where the recipe file is.

  • S is source directory, derived from the WORKDIR and used during the build process.

  • LIC_FILES_CHKSUM is checksum of the recipe’s license file.

RPROVIDES_${PN} += "kernel-module-hello"
  • RPROVIDES_${PN} lists the files to be included in the final package, including binaries and the systemd service file.

  • Create makefile for the kernel module and add the following instructions.

$ vi meta-mykernelmodule/recipes-kernel/hello-mod/Makefile
obj-m := hello.o

SRC := $(shell pwd)

all:
        $(MAKE) -C $(KERNEL_SRC) M=$(SRC)

modules_install:
        $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install

clean:
        rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c
        rm -f Module.markers Module.symvers modules.order
        rm -rf .tmp_versions Modules.symvers

In this section you will learn how to add configurations for our sample kernel module.

  • Add the following changes in the configurations file.

$ vi $HOME/rdkb/build/conf/bblayers.conf
${RDKROOT}/meta-mykernelmodule \

In this section you will learn how to do Pre-build checks

  • Below line should be present in /meta-cmf-raspberrypi/conf/bblayers.conf file

${RDKROOT}/meta-mykernelmodule \
$ cd build/tmp/hosttools/
$ bitbake hello-mod

In this section you will learn about post build checks

$ cd build/tmp/work/raspberrypi4_64_rdk_broadband-rdk-linux/hello-mod/2.36-r0/sysroot-destdir/lib/modules/5.10.52-v8/extra/hello.ko

See that binary called sampleapp is present inside directory ?

  • objdump -t : Displays the symbols of kernel module binary file

  • Check if “module_function” is present or not in the “objdump”

$HOME/rdkb/build/tmp/hosttools/objdump -t hello.ko | grep -i module_function

0000000000000000 g     F .text  0000000000000017 module_function
  • Symbols “module_function” is present in kernel module binary file

  • Hence we can confirm kernel module is compiled successfully

What is “-t” option is used in “objdump” command ?

What “objdump” command does ?

  • objdump -S : Display source code intermixed with disassembly

  • Check if “module_function” is present or not in the “objdump”

    $HOME/rdkb/build/tmp/hosttools/objdump -S hello.ko | grep -i module_function
    
    0000000000000000 <module_function>:
     0:     e8 00 00 00 00          call    5 <module_function+0x5>
    10:     e8 00 00 00 00          call   15 <module_function+0x15>
    
  • Symbols “module_function” is present in kernel module binary file

  • Hence we can confirm kernel module is compiled successfully

What is “-S” option is used in “objdump” command ?

What is the purpose of “objdump” command ?

  • readelf -s: Display the symbol table

  • Check if “module_function” is present or not in the “readelf”

$HOME/rdkb/build/tmp/hosttools/readelf -s hello.ko | grep -i module_function

32: 0000000000000000    23 FUNC    GLOBAL DEFAULT    2 module_function
  • Symbols “module_function” is present in kernel module binary file

  • Hence we can confirm kernel module is compiled successfully

What is “-s” option is used in “readelf” command ?

What “readelf” command does ?

What is the purpose of “readelf” command ?

  • nm -S: Print both value and size of defined symbols for the “bsd” output style

  • Check if “module_function” is present or not in the “nm”

$HOME/rdkb/build/tmp/hosttools/nm -S hello.ko | grep -i module_function

0000000000000000 0000000000000017 T module_function
  • Symbols “module_function” is present in kernel module binary file

  • Hence we can confirm kernel module is compiled successfully

What is “-S” option is used in “nm” command ?

What “nm” command does ?

What is the purpose of “nm” command ?

  • nm -s: When listing symbols from archive members, include the index: a mapping of which modules contain definitions for which names.

  • Check if “module_function” is present or not in the “nm”

$HOME/rdkb/build/tmp/hosttools/nm -s hello.ko | grep -i module_function

0000000000000000 T module_function
  • Symbols “module_function” is present in kernel module binary file

  • Hence we can confirm kernel module is compiled successfully

What is “-s” option is used in “nm” command ?

  • file: Determine file tye

$ file hello.ko

hello.ko: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-aarch64.so.1, for GNU/Linux3.7.0, not stripped

Why “file” command is used ?

  • size: List section sizes and total size of binary files

$ size hello.ko

text       data     bss     dec     hex filename
585         896       0    1481     5c9 hello.ko

What “size” command does ?

  • strings - print the sequences of printable characters in files

  • As we can see string output are added in the program are confirmed.

$ strings hello.ko | grep -i Module
Module Function

What “strings” command does ?

In this section you will learn how to build full rdkb

  • Make sure the current directory “build”

$ cd $HOME/rdkb/build
$ bitbake rdk-generic-broadband-image
  • It downloads, compiles and configures all the necessary packages required for the build.

  • Make sure the current directory “build”

  • To check package is successfully compiled, below is the path

$ cd tmp/deploy/images/raspberrypi4/
$ ls
-rw-r--r-- 1 test test 1704951 Jan 11 11:16 core-image-base-raspberrypi4.wic.bz2
  • As we can see image is present in the tmp/deploy/images/raspberrypi4 directory

  • Image file is core-image-base-raspberrypi4.wic.bz