Compile the kernel (debian)

Here is how to use linux to customize your own kernel. The recommended distribution is Ubuntu 20.04.

required packages

Different distributions may have different names for software packages. Here, the naming of Ubuntu 20.04 is used as an example.

  • binfmt-support
  • qemu-user-static
  • gcc-10-aarch64-linux-gnu
  • kernel-package
  • fakeroot
  • simg2img
  • img2simg
  • mkbootimg
  • bison

generate kernel debian package

Clone the linux kernel, here choose a depth of 1 to reduce the volume.

$ git clone https://github.com/OpenStick/linux --depth=1

start compiling

$ export CROSS_COMPILE=aarch64-linux-gnu-
$ export ARCH=arm64
$ make msm8916_defconfig
$ make menuconfig
$ make -j16

Generate packages in debian format

$ fakeroot make-kpkg  --initrd --cross-compile aarch64-linux-gnu- --arch arm64  kernel_image kernel_headers

Afterwards, the generated deb package (will be generated on the upper layer of the code directory), and arch/arm64/boot/Image.gzreserved for future use.
Convert rootfs.img to img format and mount it

$ simg2img rootfs.img root.img
$ sudo mount root.img /mnt

In the chroot environment, just install the deb installation packages such as linux-image generated earlier. Pay attention to take out the generated boot/initrd.imgones after the installation is complete (it is best to uninstall the original software packages such as linux-image before installation).

$ sudo  mount --bind /proc /mnt/proc 
$ sudo  mount --bind /dev /mnt/dev
$ sudo  mount --bind /dev/pts /mnt/dev/pts
$ sudo  mount --bind /sys /mnt/sys
$ sudo  chroot /mnt

After the installation is complete, unmount all mounts and convert img to simg again

$ img2simg root.img rootfs.img

Generate boot.img

Take the previous file system after installing the new kernel /boot/initrd.img**and the file generated in the kernel compilation stage Image.gzand the backup arch/arm64/boot/dts/qcom/of the corresponding device. Attach devicetree (dtb) after Image.gz*.dtb

 cat Image.gz dtb > kernel-dtb

Generate boot.img

$ mkbootimg \     
        --base 0x80000000 \
        --kernel_offset 0x00080000 \
        --ramdisk_offset 0x02000000 \
        --tags_offset 0x01e00000 \
        --pagesize 2048 \
        --second_offset 0x00f00000 \
        --ramdisk initrd.img \
        --cmdline "earlycon root=PARTUUID=a7ab80e8-e9d1-e8cd-f157-93f69b1d141e console=ttyMSM0,115200 no_framebuffer=true rw"\
        --kernel kernel-dtb -o boot.img

Then brush the two img files into the corresponding partitions respectively.