Compiling the Kernel (NVIDIA DRIVE OS Linux)

This topic contains instructions for compiling the Linux Kernel Source in your Linux SDKproduct.

Prerequisites

  • You have installed the Kernel Source Code (already installed in Docker) and the toolchains:
sudo -E apt install  nv-driveos-linux-*_amd64.deb
sudo -E apt install nv-driveos-foundation-toolchains-*_amd64.deb

The top-level directory layout has this appearance:

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"
    export NV_BUILD_KERNEL_OPTIONS="5.10"
  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.
    bash kernel/scripts/rt-patch.sh apply-patches
  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:

    sudo apt-get install imagemagick graphviz dvipng python3-venv fonts-noto-cjk latexmk librsvg2-bin texlive-xetex flex bison -y

    /usr/bin/python3 -m venv sphinx_2.4.4

    . sphinx_2.4.4/bin/activate

    pip install -r <top>/drive-linux/kernel/source/oss_src/kernel/Documentation/sphinx/requirements.txt

    sudo apt-get install flex

    sudo apt-get install bison

  5. Add the following command:
    make -C kernel O=${PWD}/out-linux tegra_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. 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.

  8. 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
  9. Deactivate Sphinx with the following command:
    $ deactivate
  10. Copy the built modules to the root filesystem 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 filesystem, use the following Build-FS steps:
      1. Create a Build-FS JSON with the following content:
        $ vim update_rfs.CONFIG.json
        {
            "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.img
        $ sudo ln -s ${NV_WORKSPACE}/drive-linux/filesystem/targetfs-images/driveos-updated-rfs.img ${NV_WORKSPACE}/drive-linux/filesystem/targetfs.img
      3. Flash the target using the SDK Bootburn tool.
    Note:
    To ensure that all modules are loaded, at the target side, execute these commands:
    depmod -a
    sync

    The sync command must be executed as superuser. From the next boot onward, the kernel modules are auto-loaded on boot.

  11. If there is a change in the device tree source files, copy the newly generated DTB files:
    sudo cp ${PWD}/out-linux/arch/arm64/boot/dts/nvidia/<dtb_file>
    <top>/drive-linux/kernel/preempt_rt/