[NVIDIA] 从零入手 Jetson Xavier NX 内核编译、源码编译流程
–
***注明:感谢网友提醒,我当时买的时候nx套件还没开售,因为核心板兼容b01,先买了核心板和b01的板子合在一起用的,所以本文是基于jetson xavier nx模块 + B01的,社友请知悉。
本文主要讲述,基于 Jetson Xavier NX 开发者套件: 环境配置——>内核剪裁——>重新编译——>烧录流程。
What is Jetson Xavier NX
NVIDIA® Jetson Xavier™ NX 是体型超小的AI超级计算机,适用于嵌入式系统和边缘系统。
高达 21 TOPS 加速计算能力,可以并行运行现代神经网络并处理来自多个高分辨率传感器的数据。
Jetson Xavier NX 是生产就绪型产品,可支持所有热门 AI 框架。
1、先按照我的另一篇文章,配置基础环境并完成第一次NX的刷机:
NVIDIA 入手Jetson Xavier NX 刷机+开机配置+系统烧录+Bring up
2、则你现在应该拥有一个工作路径和基础的一些代码环境,如下所示:
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra$ ls
apply_binaries.sh jetson-xavier-slvs-ec.conf p2972-0000-devkit-maxn.conf
bootloader kernel p2972-0000-devkit-slvs-ec.conf
build_l4t_bup.sh l4t_generate_soc_bup.sh p2972-0006-devkit.conf
flash.sh l4t_sign_image.sh p2972-as-galen-8gb.conf
jetson-tx2-4GB.conf nvmassflashgen.sh p3449-0000+p3668-0000-qspi-sd.conf
jetson-tx2-as-4GB.conf nv_tegra p3449-0000+p3668-0001-qspi-emmc.conf
jetson-tx2.conf p2771-0000-0888.conf p3509-0000+p3668-0000-qspi.conf
jetson-tx2i.conf p2771-0000-as-0888.conf p3509-0000+p3668-0000-qspi-sd.conf
jetson-xavier-8gb.conf p2771-0000.conf.common p3509-0000+p3668-0001-qspi-emmc.conf
jetson-xavier-as-8gb.conf p2771-0000-devkit.conf p3668.conf.common
jetson-xavier-as-xavier-nx.conf p2771-0000-dsi-hdmi-dp.conf README_Massflash.txt
jetson-xavier.conf p2771-3489-ucm1.conf rootfs
jetson-xavier-maxn.conf p2771-3489-ucm2.conf source
jetson-xavier-nx-devkit.conf p2822+p2888-0001-as-p3668-0001.conf source_sync.sh
jetson-xavier-nx-devkit-emmc.conf p2972-0000.conf.common tools
jetson-xavier-nx-devkit-qspi.conf p2972-0000-devkit.conf
3、切到前一级路径,并把英伟达官方下载的kernel源码压缩包 “public_sources.tbz2”,拷贝到这
注:源码包下载路径如下:
zhong@zhong:~/workspace/xa_nx/linux$ ls
Linux_for_Tegra public_sources.tbz2
4、执行如下解压命令
zhong@zhong:~/workspace/xa_nx/linux$ tar -xjf public_sources.tbz2
5、在你的source路径下,就会多出来一些源码包了:
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source$ ls
nv_src_build.sh public
6、进入kernel源码路径,执行解压
~/workspace/xa_nx/linux/Linux_for_Tegra/source/public$ tar -xjf kernel_src.tbz2
7、可以观察到多了kernel源码文件夹:
8、进入路径就可以看到kernel源码结构了:
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ ls
arch build.config.goldfish.x86 drivers Kconfig NVIDIA-REVIEWERS signing_key
block build.config.goldfish.x86_64 firmware kernel README signing_key.x509
build.config.cuttlefish.x86_64 certs fs lib REPORTING-BUGS sound
build.config.goldfish.arm COPYING include MAINTAINERS rt-patches tools
build.config.goldfish.arm64 CREDITS init Makefile samples usr
build.config.goldfish.mips crypto ipc mm scripts verity_dev_keys.x509
build.config.goldfish.mips64 Documentation Kbuild net security virt
9、在linux操作系统上只使用gcc编译是不够的,还要安装编译程序必须的软件包,使用下面的命令:
sudo apt install build-essential bc
10、配置编译环境变量,避免重复配置,这边直接修改 “~/.bashrc”
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ vim ~/.bashrc
11、在 “.bashrc” 末尾添加如下配置(路径注意和自己的电脑保持一致),保存退出:
export LOCALVERSION=-tegraexport TEGRA_KERNEL_OUT=/home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernelexport CROSS_COMPILE=/home/zhong/workspace/xa_nx/tools/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
12、执行source命令
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ source ~/.bashrc
13、在源码路径下,执行命令:mkdir -p $TEGRA_KERNEL_OUT,建立编译输出路径
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ mkdir -p $TEGRA_KERNEL_OUT
14、make defconfig , 执行以下命令
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ make ARCH=arm64 O=$TEGRA_KERNEL_OUT tegra_defconfig
15、则有如下打印:
make[1]: Entering directory '/home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel'HOSTCC scripts/basic/fixdepGEN ./MakefileHOSTCC scripts/kconfig/conf.oSHIPPED scripts/kconfig/zconf.tab.cSHIPPED scripts/kconfig/zconf.lex.cSHIPPED scripts/kconfig/zconf.hash.cHOSTCC scripts/kconfig/zconf.tab.oHOSTLD scripts/kconfig/conf
#
# configuration written to .config
#
make[1]: Leaving directory '/home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel'
16、执行以下命令,开始编译kernel,包含所有DTBs and modules.(-j8 是虚拟机处理器有8个核,不一样修改下,如4核 -j4 )
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ make ARCH=arm64 O=$TEGRA_KERNEL_OUT -j8
备注: 在编译过程中可能会报错:“… is not clean, please run ‘make mrproper’”,则在kernel-4.9路径下执行“make mrproper” 命令即可。
17、编译完成后会生成 Image、 dtb 文件
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel/arch/arm64/boot$ ls
dts Image Image.gz zImage
18、将新编译好的 Image 和 dtb 文件替换至烧录位置:
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel/arch/arm64/boot$ cp Image /home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/kernel/Imagezhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel/arch/arm64/boot$ cp dts/* /home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/kernel/dtb/ -rf
19、执行以下命令来安装 kernel modules:
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/source/public/kernel/kernel-4.9$ sudo make ARCH=arm64 O=$TEGRA_KERNEL_OUT modules_install INSTALL_MOD_PATH=/home/zhong/workspace/xa_nx/linux/Linux_for_Tegra/rootfs
20、(可选)在rootfs路径下:
(1)使用以下命令归档已安装的内核模块,执行完后可以看到多了“kernel_supplements.tbz2”
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/rootfs$ sudo tar --owner root --group root -cjf kernel_supplements.tbz2 lib/moduleszhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/rootfs$ ls
bin dev home lib mnt proc root sbin srv tmp var
boot etc kernel_supplements.tbz2 media opt README.txt run snap sys usr
(2)可以使用已安装的模块在目标系统上提供 /lib/modules/< kernel_version>的内容。
(3)将 “kernel_supplements.tbz2”替换到“zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/kernel$”路径下
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra/rootfs$ sudo cp kernel_supplements.tbz2 ../kernel/kernel_supplements.tbz2
21、执行 apply_binaries.sh 脚本
zhong@zhong:~/workspace/xa_nx/linux/Linux_for_Tegra$ sudo ./apply_binaries.sh
备注:按照我的另一篇文章( [NVIDIA 入手Jetson Xavier NX 刷机+开机配置+系统烧录+Bring up]),配置基础环境并完成第一次NX的刷机时,会执行上述脚本,在这里执行就会报错,因此需要内核编译剪裁的同学就 不! 要! 提前! 执行那个脚本了,在这个地方再执行。
22、Jetson Xavier NX 触发recovery模式,重新上电。
23、烧录固件
sudo ./flash.sh p3449-0000+p3668-0001-qspi-emmc mmcblk0p1
24、等待烧录完成,即可进入系统,使用鼠标键盘初始化系统后即完成整个剪裁烧录过程,从debug串口观察系统启动log,编译用户名和编译时间已经更新,验证重新编译的内核确实生效:
Linux version 4.9.140-tegra (zhong@zhong) (gcc version 7.3.1 20180425 [linaro-7.3-2018.05 revision d29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05) ) #3 SMP PREEMPT Thu May 7 15:28:44 CST 2020
25、以上,完成了NX内核的内核源码编译流程,关于如何进行内核剪裁就比较简单了,但是每个人根据项目的不同,会有所差异,因此内核剪裁又是一件比较麻烦的事,后面开一个新的篇幅写下,稍等哦!~