Compiling the Kernel (Kernel 5.15)

This topic contains instructions for compiling the Linux kernel source in your Linux SDK product.

Prerequisites

You have installed the Kernel source code (already installed in Docker) and the toolchain:

sudo -E apt install  nv-driveos-linux-*_amd64.deb
sudo -E apt install nv-driveos-foundation-toolchains-*_amd64.deb

The top-level directory layout looks as follows:

drive-foundation
drive-linux
Drive-linux_src
toolchains                

To Compile the Kernel

  1. Set up the environment macros: The LOCAL_VERSION environment variable appends -tegra to the kernel version and modules.

    export ARCH=arm64
    export CROSS_COMPILE=${PWD}/toolchains/aarch64--glibc--stable-2022.03-1/bin/aarch64-buildroot-linux-gnu-
    export LOCALVERSION="-tegra"
  2. Set the kernel source directory as the current working directory:

    cd <top>/drive-linux/kernel/source/oss_src
  3. Enter the following command to apply the rt-patches.

    cd kernel/scripts
    bash generic-rt-patch.sh apply-patches
    cd ../../ 
  4. Create an output directory, and then configure the kernel for the board:

    mkdir out-linux
    make -C kernel O=${PWD}/out-linux clean
    If prompted, install the packages:
    1. sudo apt-get install imagemagick graphviz dvipng python3-venv fonts-noto-cjk latexmk
                  librsvg2-bin texlive-xetex
    2. /usr/bin/python3 -m venv sphinx_2.4.4
      . sphinx_2.4.4/bin/activate
    3. pip install -r  <top>/drive-linux/kernel/source/oss_src/kernel/Documentation/sphinx/requirements.txt
    4. sudo apt-get install flex
    5. sudo apt-get install bison
  5. Add the following command:

    make -C kernel O=${PWD}/out-linux defconfig
  6. Build the kernel:

    make -j3 -C kernel O=${PWD}/out-linux
    Note: If the preceding command fails, enter the make command without the j<number> option.
  7. Build OOT modules:
    make -C ${PWD}/out-linux M=${PWD}/nvidia-oot/  srctree.nvidia-oot=${PWD}/nvidia-oot/ 
    srctree.nvidia=${PWD}/nvidia-oot  V=1 modules CONFIG_TEGRA_OOT_MODULE=m
  8. To flash the built images, copy the uncompressed (Image) kernel images to the kernel top directory with the following command:
    sudo cp ${PWD}/out-linux/arch/arm64/boot/Image
    <top>/drive-linux/kernel/preempt_rt/images/
    CAUTION: Before copying the new kernel images, create a backup of the default kernels provided.
  9. Consolidate the built kernel modules in the build directory with the following commands:
    export INSTALL_MOD_PATH=${PWD}/out-linux
    make -C kernel O=${PWD}/out-linux modules_install
    make -C ${PWD}/out-t186ref-linux M=${PWD}/nvidia-oot/ modules_install
  10. Copy the built modules to the root file system path with the following commands:
    1. Copy the built modules to the SDK kernel modules path:
      sudo cp -a ${PWD}/out-linux/lib/modules/* <top>/drive-
      linux/kernel/preempt_rt/modules
    2. To copy the updated modules into the root file system, use the following Build-FS steps:
      1. Create a Build-FS JSON with the following content:
        {
            "OS": "linux",
            "Output": "driveos-updated-rfs",
            "Base": "\${BASE_DIR}/targetfs.img",
            "CopyTargets": [
                "\${COPYTARGETYAML_DIR}/copytarget-kernel-modules.yaml"
            ]
        }
      2. Execute Build-FS with the preceding configuration to rebuild the filesystem. Set NV_WORKSPACE to opt of SDK install directory (containing drive-linux directory).
        $ sudo -E /usr/bin/python3 -B /opt/nvidia/driveos/common/filesystems/build-
          fs/17/bin/build_fs.py -w ${NV_WORKSPACE}/ -i $PWD/update_rfs.CONFIG.json -o ${NV_WORKSPACE}/drive-linux/filesystem/targetfs-images/
        $ sudo rm -f ${NV_WORKSPACE}/drive-linux/filesystem/targetfs-images/targetfs.img
        $ sudo ln -s ${NV_WORKSPACE}/drive-linux/filesystem/targetfs-images/driveos-updated-rfs.img
        ${NV_WORKSPACE}/drive-linux/filesystem/targetfs-images/targetfs.img
      3. Flash the target using the SDK Bootburn tool.
Note: To ensure that all modules are loaded, execute the following commands at the target side:
depmod -a
sync
The sync command must be executed as superuser. From the next boot onward, the kernel modules are auto-loaded on boot.