NVIDIA Jetson Orin - Compiling Source Code
Learning to build the BSP and OS components enables developers to customize the software for their specific project needs.
This section will guide you through the process of building the BSP and OS components for the NVIDIA Jetson Orin. For this section you are going to need Jetpack 5.0 for Orin AGX installed on your host computer, we assume that you got this installed following through our Getting Started and Installing Jetpack sections.
Assumptions
- The build of the BSP/OS components source code will be performed by cross-compiling on a host computer running a Linux OS.
- The host computer used for the cross-compilation has Jetpack 5.0 (L4T 34.1) for Orin AGX installed. The Jetpack path is:
/home/$USER/nvidia/nvidia_sdk/JetPack_5.0_DP_Linux_DP_JETSON_AGX_ORIN_TARGETS/Linux_for_Tegra
This is the default path where the SDK Manager installs the Jetpack 5.0 tools for the Orin AGX. If you chose to install Jetpack on a different path, make sure to adjust the instructions accordingly. - Make sure the following dependencies are installed on your system:
- wget
- lbzip2
- build-essential
- bc
- zip
- libgmp-dev
- libmpfr-dev
- libmpc-dev
- vim-common # For xxd
In Debian based systems you can run the following:
sudo apt install wget lbzip2 build-essential bc zip libgmp-dev libmpfr-dev libmpc-dev vim-common
Downloading and compiling
Download the JetPack sources
You can obtain the JetPack 5.0.2 DP sources by doing the following:
1. Go to https://developer.nvidia.com/embedded/jetson-linux-archive and download the source files for your release.
2. Extract the .tbz2 file:
tar -xjf public_sources.tbz2
3. Extract the kernel source file:
cd Linux_for_Tegra/source/public JETSON_SOURCES=$PWD tar –xjf kernel_src.tbz2
This extracts the kernel source to the kernel/ subdirectory.
Patch instructions
Download the patches
RidgeRun has created a repository from where you can download the patch files.
You need to clone it and use the branch for the jetpack.
cd $HOME git clone https://github.com/RidgeRun/NVIDIA-Jetson-IMX477-RPIV3.git -b jetpack-5.0.2
Once you have downloaded the repository, copy the files to the $JETSON_SOURCES
directory:
KERNEL_PATCH=$(pwd)/NVIDIA-Jetson-IMX477-RPIV3/ cp $KERNEL_PATCH/patches_orin/5.0.2_agx_imx477_v0.1.patch $JETSON_SOURCES/
Apply the patches
cd $JETSON_SOURCES git apply 5.0.2_agx_imx477_v0.1.patch
Kernel build instructions
Once the sources have been patched, perform the following steps in order to build and install the driver.
Install the Toolchain
First download the Bootlin toolchain binaries from the NVIDIA Jetson Linux home page. Go to the bottom of the page and click on the Bootlin Toolchain gcc option as shown in the image below:
After that do:
mkdir $HOME/l4t-gcc cd $HOME/l4t-gcc tar xf <path_to_toolchain_archive>
Build the kernel
- Establish the building paths anc reate directories:
KERNEL_OUT=$JETSON_SOURCES/kernel_out MODULES_OUT=$JETSON_SOURCES/modules_out mkdir -p $KERNEL_OUT mkdir -p $MODULES_OUT
- Use the following command to install dependencies on Ubuntu:
sudo apt install libncurses-dev
- Export environment variables:
export CROSS_COMPILE_AARCH64_PATH=$HOME/l4t-gcc export CROSS_COMPILE_AARCH64=$HOME/l4t-gcc/bin/aarch64-buildroot-linux-gnu-
- Compile kernel:
cd $JETSON_SOURCES ./nvbuild.sh -o $KERNEL_OUT
- Install the modules
make -C $JETSON_SOURCES/kernel/kernel-5.10/ ARCH=arm64 O=$KERNEL_OUT LOCALVERSION=-tegra INSTALL_MOD_PATH=$MODULES_OUT modules_install
Flash the Jetson
We present two methods to install the customized kernel, one requires a complete flash, and the other is for quick updating while developing.
Complete flash
Update the Linux_for_Tegra directory
# Copy the new compiled kernel. cp -r $KERNEL_OUT/arch/arm64/boot/Image $JETPACK_DIR/Linux_for_Tegra/kernel/ # Copy all new compiled DTBs and DTB overlays. cp -r $KERNEL_OUT/arch/arm64/boot/dts/nvidia/* $JETPACK_DIR/Linux_for_Tegra/kernel/dtb/ # Update rootfs with compiled modules, which include the driver. rsync -azPu $MODULES_OUT/lib/modules/5.10.104-tegra $JETPACK_DIR/Linux_for_Tegra/rootfs/lib/modules/ # Update the rootfs with all the binaries (to make sure the /boot/ folder is updated too). cd $JETPACK_DIR/Linux_for_Tegra sudo ./apply_binaries.sh # Copy the module to the correct location. sudo cp $KERNEL_OUT/drivers/media/i2c/nv_imx477.ko $JETPACK_DIR/Linux_for_Tegra/rootfs/lib/modules/5.10.104-tegra/kernel/drivers/media/i2c/
Flash
Finally, flash the driver
sudo ./flash.sh jetson-agx-orin-devkit mmcblk0p1
Manual installation
Update the kernel and DTB files used by /boot/extlinux/extlinux.conf when there is an already running board. This assumes that the Jetson can be reach by SSH using:
ssh jetson
Updating the kernel
Updating the kernel is as simple as copying into the board:
# Update Image on Linux_for_Tegra folder just to sync the flash folders sudo cp -rfv $KERNEL_OUT/arch/arm64/boot/Image $JETPACK_DIR/Linux_for_Tegra/kernel/ # Copy the Image to the tmp folder on the board scp $JETPACK_DIR/Linux_for_Tegra/kernel/Image jetson:/tmp/ # Copy the Image to the boot directory on the board sudo mv /tmp/Image /boot/Image
Updating the DTB
The new JetPack uses the FDT property of the /boot/extlinux/extlinux.conf file, and it sets it to a device tree located in /boot/dtb/ with a prefix of kernel_tegra. We can update this DTB with the following:
# Update on Linux_for_Tegra folder with the new DTBs and overlays sudo cp -rfv $KERNEL_OUT/arch/arm64/boot/dts/nvidia/* $JETPACK_DIR/Linux_for_Tegra/kernel/dtb/ # Generate required prefixed DTB using flash script with no flash option cd $JETPACK_DIR/Linux_for_Tegra sudo ./flash.sh -r --no-flash jetson-agx-orin-devkit mmcblk0p1 # Copy the device tree to the board scp $JETPACK_DIR/Linux_for_Tegra/bootloader/kernel_tegra234-p3701-0000-p3737-0000.dtb jetson:/tmp/ # Update the device tree on the board sudo mv /tmp/kernel_tegra234-p3701-0000-p3737-0000.dtb /boot/dtb/
Updating modules
Inside the board run the following to update the modules folder, copying them from the host:
HOST_NAME=host HOST_IP=192.168.55.100 cd /lib/modules/ sudo rsync -azPu $HOST_NAME@$HOST_IP:$MODULES_OUT/lib/modules/5.10.104-tegra .
Define the Environment Variables
Open a terminal and run the following commands to export the environment variables that will be used in the next steps:
export TOOLCHAIN_SRC=bootlin-toolchain-gcc-93 export TOOLCHAIN_DIR=gcc-9.3-glibc-2.31 export KERNEL_SRC=l4t-sources-34-1 export KERNEL_DIR=kernel-5.10 export CROSS_COMPILE=$HOME/l4t-gcc/bin/aarch64-buildroot-linux-gnu- export JETPACK=$HOME/nvidia/nvidia_sdk/JetPack_5.0.1_DP_Linux_JETSON_AGX_ORIN_TARGETS/Linux_for_Tegra export KERNEL_OUT=$JETPACK/images export KERNEL_MODULES_OUT=$JETPACK/images/modules
Get the Toolchain
If you haven't already, download the toolchain. The toolchain is the set of tools required to cross-compile the Linux kernel. You can get the toolchain running the following snippet in the terminal:
cd $HOME mkdir -p $HOME/l4t-gcc cd $HOME/l4t-gcc # Reuse existing download, if any if ! test -e ${TOOLCHAIN_SRC}.tar.gz; then wget -O ${TOOLCHAIN_SRC}.tar.gz https://developer.nvidia.com/embedded/jetson-linux/bootlin-toolchain-gcc-93 tar -xf ${TOOLCHAIN_SRC}.tar.gz fi
Please note that as a new kernel version becomes available, it might be necessary to use updated toolchain versions as well.
Download BSP sources
The BSP sources are provided by NVIDIA and include the kernel, modules, and device tree source files. At RidgeRun we often need to modify and rebuild these sources to develop our customer's projects. You can use one of the two following methods to obtain the BSP sources:
Method #1: Download via the sources_sync script
NVIDIA provides a script to clone the BSP sources, this script is included in Jetpack. To download kernel sources, execute the following commands in a terminal:
cd $JETPACK ./source_sync.sh -k jetson_34.1.1
Method #2: Download via the NVIDIA web page
The BSP sources can be downloaded from the Jetson Download Center, in a package called plubic_sources.tbz2. The following commands can be executed in the terminal to download and extract the BSP sources into your Jetpack directory:
cd $JETPACK mkdir -p sources && cd sources wget https://developer.nvidia.com/embedded/l4t/r34_release_v1.1/sources/public_sources.tbz2 tar -xvf public_sources.tbz2 Linux_for_Tegra/source/public/kernel_src.tbz2 --strip-components=3 tar -xvf kernel_src.tbz2
Build the Kernel, Modules, and DTB
This subsection will guide you through the steps of building the BSP sources to generate the kernel Image, the external modules, and the device tree blob.
Note: Before launching the sources build, check our known issues subsection (at the end of this section) to work around any issues that apply to your Jetpack version and avoid build failures. |
1. Create the directories for the build outputs:
mkdir -p $KERNEL_MODULES_OUT
2. Clean the environment:
cd $JETPACK/sources/kernel/$KERNEL_DIR make mrproper
3. Setup the default configuration:
make ARCH=arm64 O=$KERNEL_OUT tegra_defconfig
4. Optionally, you can customize the kernel configuration by modifying the settings in the menuconfig. The menuconfig can be opened by running the following command:
make ARCH=arm64 O=$KERNEL_OUT menuconfig
5. Build the BSP:
make ARCH=arm64 O=$KERNEL_OUT CROSS_COMPILE=$CROSS_COMPILE -j4
Note: the number next to the -j flag in the previous command tells make how many recipes to execute simultaneously. You can increase this value for the kernel build to finish faster but using more system resources. |
6. Install the modules to the output directory created in step 1:
make modules_install ARCH=arm64 O=$KERNEL_OUT CROSS_COMPILE=$CROSS_COMPILE INSTALL_MOD_PATH=$KERNEL_MODULES_OUT
7. Backup the binaries that come installed by default in Jetpack:
BKUP_DATE=`date "+%Y_%m_%d_%H_%M_%S"` mv $JETPACK/kernel/Image{,.$BKUP_DATE} mv $JETPACK/kernel/kernel_supplements.tbz2{,.$BKUP_DATE} mv $JETPACK/kernel/dtb{,.$BKUP_DATE}
8. Copy the binaries built to the default locations expected by the flashing tool:
cd $KERNEL_OUT cp ./arch/arm64/boot/Image $JETPACK/kernel/ cp -r ./arch/arm64/boot/dts $JETPACK/kernel/dtb
9. Update the kernel modules in the kernel supplements tarball:
cd $KERNEL_MODULES_OUT tar --owner root --group root -cjf $JETPACK/kernel/kernel_supplements.tbz2 lib/modules
10. Install tegra binaries:
cd $JETPACK sudo ./apply_binaries.sh
At this point you have the kernel Image, external modules and dtb built and ready to flash.