Coral from Google - Camera Drivers - How to recompile kernel

From RidgeRun Developer Wiki


Previous: Camera_Drivers/How_to_flash_Google_Coral Index Next: Camera_Drivers/Adding_a_new_camera_driver





Now you can install the kernel, first create a directory to download the python script used to download the source code

mkdir -p <PATH_SCRIPT>
export PATH=$PATH:PATH_SCRIPT
curl https://storage.googleapis.com/git-repo-downloads/repo > PATH_SCRIPT/repo
chmod a+x PATH_SCRIPT/repo

Now go to where the script was downloaded and run the repo script to initialize the code

cd PATH_SCRIPT
repo init -u https://coral.googlesource.com/manifest
repo sync -j$(nproc)

Automatic kernel compilation

Install the dependencies to build the kernel

sudo apt-get install qemu-user-static
sudo apt-get install docker.io
sudo adduser $USER docker
sudo apt-get install android-tools-adb android-tools-fastboot
sudo apt-get install build-essential qemu-user-static bc

Now use the source and build the kernel

source build/setup.sh
m docker-linux-imx

If the following error comes up:

wget -O debbootstrap.deb http://ftp.us.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.89_all.deb
--2023-05-04 17:47:41--  http://ftp.us.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.89_all.deb
Resolving ftp.us.debian.org (ftp.us.debian.org)... 64.50.236.52, 64.50.233.100, 208.80.154.139, ...
Connecting to ftp.us.debian.org (ftp.us.debian.org)|64.50.236.52|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2023-05-04 17:47:42 ERROR 404: Not Found.

Replace the debootsrap URL with a working mirror in the build/prereqs.mk file, for example:

sed -i 's,http://ftp.us.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.89_all.deb,http://mirror.nus.edu.sg/Debian/pool/main/d/debootstrap/debootstrap_1.0.89_all.deb,g' build/prereqs.mk

After that, rebuild the kernel:

m docker-linux-imx

You are going to see in the output of the process the following lines, these are the packages that you can install on the board.

dpkg-deb: building package 'linux-headers-4.14.98-imx' in '../linux-headers-4.14.98-imx_12-4_arm64.deb'.
dpkg-deb: building package 'linux-kbuild-4.14.98-imx-dbgsym' in '../linux-kbuild-4.14.98-imx-dbgsym_12-4_arm64.deb'.
dpkg-deb: building package 'linux-kbuild-4.14.98-imx' in '../linux-kbuild-4.14.98-imx_12-4_arm64.deb'.
dpkg-deb: building package 'linux-image-4.14.98-imx' in '../linux-image-4.14.98-imx_12-4_arm64.deb'.

To install the new kernel you can run the following commands

j product
cd packages/bsp/
mdt install linux-image-4.14.98-imx_12-4_arm64.deb && \
mdt install linux-kbuild-4.14.98-imx_12-4_arm64.deb && \
mdt install linux-headers-4.14.98-imx_12-4_arm64.deb 

Now your driver should be installed in the system

Manual kernel compilation

The process described above is very straight forward for deployment; however, it is not ideal for development since it is very time consuming and not incremental at all. Making a very small change in a file results in a 30min compilation process.

For development purposes it is recommended to use the following approach for a more efficient workflow.

This approach will also allow you to compile other versions of the kernel if you'd like since the coral uses an old version (4.14.98) of the kernel. Keep in mind that updating the kernel might affect compatibility with other modules required by the coral for hardware acceleration and such. Find the instructions below:

1. Getting the toolchain:

For the kernel crosscompilation we need an arm64 toolchain. Feel free to use any you like, in this wiki we use the Linaro toolchain.

Download the pre-built toolchain binaries from: http://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz

wget http://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz

Execute the following commands to extract the toolchain:

mkdir $HOME/linaro-gcc
cd $HOME/linaro-gcc
tar xf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz

2. Compiling the kernel:

cd linux-imx
export ARCH=arm64; export CROSS_COMPILE=/$HOME/linaro-gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-; export LOCALVERSION=-imx
make O=build defconfig 
make O=build menuconfig 
make O=build -j8 Image dtbs 
make O=build -j8 modules

During the menuconfig stage make sure that the local version auto generation is disabled and that the local version prefix is set to -imx

3. Using the new kernel:

To use the new kernel and dtb you can just replace the old ones with the new ones, for the coral those are

/boot/Image -> /boot/vmlinuz-4.14.98-imx
/boot/fsl-imx8mq-phanbell.dtb

You can find the new ones under:

linux-imx/build/arch/arm64/boot/Image
linux-imx/build/arch/arm64/boot/dts/freescale/fsl-imx8mq-phanbell.dtb

4. Kernel modules:

When developing a driver, it is useful to only update the driver module. For this move the .ko you need to the board and install it manually.

insmod <module>.ko

Making changes to the dtb or the kernel module should now only require minimal compilation time and moving the .dtb or .ko respectively, significantly accelerating the development time.

If you have the sources as part of a git repository and have uncommited changes, compilation will tag the build with -dirty at the end of the local version, resulting in possible mismatch between the modules and the image or dtb if only one is updated; to avoid this you can simply create a draft commit.


Previous: Camera_Drivers/How_to_flash_Google_Coral Index Next: Camera_Drivers/Adding_a_new_camera_driver