Difference between revisions of "NVIDIA Jetson Orin/JetPack 5.0.2/Compiling Code"

From RidgeRun Developer Connection
Jump to: navigation, search
m (Install the Toolchain)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
<noinclude>
 
<noinclude>
{{NVIDIA Jetson Orin/Head|previous=JetPack 5.0‎/Getting_Started/Components|next=JetPack_5.0/Flashing_Board|keywords=jetpack,compiling,compile,building}}
+
{{NVIDIA Jetson Orin/Head|previous=JetPack 5.0‎.2/Getting_Started/Components|next=JetPack_5.0.2/Flashing_Board|metakeywords=jetpack,compiling,compile,building,Jetpack 5.0.2}}
  
 
{{DISPLAYTITLE:NVIDIA Jetson Orin - Compiling Source Code|noerror}}
 
{{DISPLAYTITLE:NVIDIA Jetson Orin - Compiling Source Code|noerror}}
Line 7: Line 7:
 
<br>
 
<br>
  
 
+
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.2 for Orin AGX installed on your host computer, we assume that you got this installed following through our [[NVIDIA_Jetson_Orin/JetPack_5.0.2/Getting_Started|Getting Started]] and [[NVIDIA_Jetson_Orin/JetPack_5.0.2/Getting_Started/Installing_Jetpack|Installing Jetpack]] sections.
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 [[NVIDIA_Jetson_Orin/JetPack_5.0/Getting_Started|Getting Started]] and [[NVIDIA_Jetson_Orin/JetPack_5.0/Getting_Started/Installing_Jetpack|Installing Jetpack]] sections.
 
 
<br>
 
<br>
 
<!--
 
We divide this section into the following components:
 
 
# [[NVIDIA Jetson Orin/JetPack_5.0/Compiling_Code/Kernel | Build the kernel, modules and dtb]]
 
# [[NVIDIA Jetson Orin/JetPack_5.0/Compiling_Code/Bootloader | Build the bootloader]]
 
# [[NVIDIA Jetson Orin/JetPack_5.0/Compiling_Code/Bootloader | Customize the Root Filesystem]]
 
-->
 
 
  
 
==Assumptions==
 
==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 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: <syntaxhighlight lang=bash> /home/$USER/nvidia/nvidia_sdk/JetPack_5.0_DP_Linux_DP_JETSON_AGX_ORIN_TARGETS/Linux_for_Tegra </syntaxhighlight> 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:
 
# Make sure the following dependencies are installed on your system:
 
** wget
 
** wget
Line 55: Line 44:
 
cd Linux_for_Tegra/source/public
 
cd Linux_for_Tegra/source/public
 
JETSON_SOURCES=$PWD
 
JETSON_SOURCES=$PWD
tar –xjf kernel_src.tbz2
+
tar -xjf kernel_src.tbz2
 
</pre>
 
</pre>
 
This extracts the kernel source to the kernel/ subdirectory.
 
This extracts the kernel source to the kernel/ subdirectory.
Line 64: Line 53:
  
 
====Install the Toolchain====
 
====Install the Toolchain====
First download the [https://developer.nvidia.com/embedded/jetson-linux 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:
+
First, download the [https://developer.nvidia.com/embedded/jetson-linux 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:
 
+
<br>
 +
<br>
 
[[File:Toolchain.png|thumb|center|650px]]
 
[[File:Toolchain.png|thumb|center|650px]]
 
+
<br>
 
After that do:
 
After that do:
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
Line 106: Line 96:
 
make -C $JETSON_SOURCES/kernel/kernel-5.10/ ARCH=arm64 O=$KERNEL_OUT LOCALVERSION=-tegra INSTALL_MOD_PATH=$MODULES_OUT modules_install
 
make -C $JETSON_SOURCES/kernel/kernel-5.10/ ARCH=arm64 O=$KERNEL_OUT LOCALVERSION=-tegra INSTALL_MOD_PATH=$MODULES_OUT modules_install
 
</pre>
 
</pre>
 
===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'''
 
 
<syntaxhighlight lang=bash>
 
# 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/
 
</syntaxhighlight>
 
 
'''Flash'''
 
 
Finally, flash the driver
 
<syntaxhighlight lang=bash>
 
sudo ./flash.sh jetson-agx-orin-devkit mmcblk0p1
 
</syntaxhighlight>
 
 
==== 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:
 
 
<pre>
 
ssh jetson
 
</pre>
 
 
'''Updating the kernel'''
 
 
Updating the kernel is as simple as copying into the board:
 
 
<syntaxhighlight lang=bash>
 
# 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
 
</syntaxhighlight>
 
'''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:
 
<syntaxhighlight lang=bash>
 
# 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/
 
</syntaxhighlight>
 
 
==== Updating modules ====
 
 
Inside the board run the following to update the modules folder, copying them from the host:
 
 
<syntaxhighlight lang=bash>
 
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 .
 
</syntaxhighlight>
 
 
 
== 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:
 
 
<syntaxhighlight lang=bash>
 
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
 
</syntaxhighlight>
 
 
== 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:
 
 
<syntaxhighlight lang=bash>
 
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
 
</syntaxhighlight>
 
 
Please note that as a new kernel version becomes available, it might be necessary to use updated toolchain versions as well.
 
<br>
 
 
 
== 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:
 
 
<syntaxhighlight lang=bash>
 
cd $JETPACK
 
./source_sync.sh -k jetson_34.1.1
 
</syntaxhighlight>
 
 
=== Method #2: Download via the NVIDIA web page ===
 
 
The BSP sources can be downloaded from the [https://developer.nvidia.com/embedded/l4t/r34_release_v1.1/sources/public_sources.tbz2 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:
 
 
<syntaxhighlight lang=bash>
 
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
 
</syntaxhighlight>
 
 
== 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.
 
 
{{Ambox
 
|type=notice
 
|small=left
 
|issue='''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.
 
|style=width:unset;
 
}}
 
 
1. Create the directories for the build outputs:
 
<syntaxhighlight lang=bash>
 
mkdir -p $KERNEL_MODULES_OUT
 
</syntaxhighlight>
 
 
2. Clean the environment:
 
<syntaxhighlight lang=bash>
 
cd $JETPACK/sources/kernel/$KERNEL_DIR
 
make mrproper
 
</syntaxhighlight>
 
 
 
3. Setup the default configuration:
 
<syntaxhighlight lang=bash>
 
make ARCH=arm64 O=$KERNEL_OUT tegra_defconfig
 
</syntaxhighlight>
 
 
 
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:
 
<syntaxhighlight lang=bash>
 
make ARCH=arm64 O=$KERNEL_OUT menuconfig
 
</syntaxhighlight>
 
 
 
5. Build the BSP:
 
<syntaxhighlight lang=bash>
 
make ARCH=arm64 O=$KERNEL_OUT CROSS_COMPILE=$CROSS_COMPILE -j4
 
</syntaxhighlight>
 
 
{{Ambox
 
|type=notice
 
|small=left
 
|issue=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.
 
|style=width:unset;
 
}}
 
 
 
6. Install the modules to the output directory created in step 1:
 
<syntaxhighlight lang=bash>
 
make modules_install ARCH=arm64 O=$KERNEL_OUT CROSS_COMPILE=$CROSS_COMPILE INSTALL_MOD_PATH=$KERNEL_MODULES_OUT
 
</syntaxhighlight>
 
 
 
7. Backup the binaries that come installed by default in Jetpack:
 
<syntaxhighlight lang=bash>
 
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}
 
</syntaxhighlight>
 
 
 
8. Copy the binaries built to the default locations expected by the flashing tool:
 
<syntaxhighlight lang=bash>
 
cd $KERNEL_OUT
 
cp ./arch/arm64/boot/Image $JETPACK/kernel/
 
cp -r ./arch/arm64/boot/dts $JETPACK/kernel/dtb
 
</syntaxhighlight>
 
 
 
9. Update the kernel modules in the kernel supplements tarball:
 
<syntaxhighlight lang=bash>
 
cd $KERNEL_MODULES_OUT
 
tar --owner root --group root -cjf $JETPACK/kernel/kernel_supplements.tbz2 lib/modules
 
</syntaxhighlight>
 
 
 
10. Install tegra binaries:
 
<syntaxhighlight lang=bash>
 
cd $JETPACK
 
sudo ./apply_binaries.sh
 
</syntaxhighlight>
 
 
At this point you have the kernel Image, external modules and dtb built and ready to flash.
 
<!--
 
= Re-build the DTB only =
 
 
Sometimes during development, only the device tree is modified. In order to rebuild only the device tree, you can use the following commands:
 
 
<syntaxhighlight lang=bash>
 
cd $JETPACK/sources/kernel/$KERNEL_DIR
 
make O=$KERNEL_OUT -j4 dtbs
 
</syntaxhighlight>
 
-->
 
  
 
<noinclude>
 
<noinclude>
{{NVIDIA Jetson Orin/Foot|JetPack 5.0‎/Getting_Started/Components|JetPack_5.0/Flashing_Board}}
+
{{NVIDIA Jetson Orin/Foot|JetPack 5.0‎.2/Getting_Started/Components|JetPack_5.0.2/Flashing_Board}}
 
</noinclude>
 
</noinclude>

Latest revision as of 13:24, 17 August 2023



Previous: JetPack 5.0‎.2/Getting_Started/Components Index Next: JetPack_5.0.2/Flashing_Board


Nvidia-preferred-partner-badge-rgb-for-screen.png




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.2 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

  1. The build of the BSP/OS components source code will be performed by cross-compiling on a host computer running a Linux OS.
  2. 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.

Kernel build instructions

Once the sources have been downloaded, perform the following steps in order to build.

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:

Toolchain.png


After that do:

mkdir $HOME/l4t-gcc
cd $HOME/l4t-gcc
tar xf <path_to_toolchain_archive>

Build the kernel

  • Establish the building paths and create 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



Previous: JetPack 5.0‎.2/Getting_Started/Components Index Next: JetPack_5.0.2/Flashing_Board