Debian system firmware compilation for portable wifi

Record the compilation process of Debian system firmware for portable WiFi devices, including source code preparation, kernel and rootfs construction, flash verification and common problems.

System preparation

  1. Install Ubuntu 22.04.2 LTS
  2. Install the software package
1
sudo apt install binfmt-support qemu-user-static gcc-10-aarch64-linux-gnu kernel-package fakeroot simg2img img2simg mkbootimg bison flex gcc-aarch64-linux-gnu pkg-config libncurses-dev libssl-dev unzip git

Download code

Clone the repository code

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

Kernel compilation

Kernel overclocking

Modify the file linux/drivers/clk/qcom/a53-pll.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
static const struct pll_freq_tbl a53pll_freq[] = {
	{  998400000, 52, 0x0, 0x1, 0 },
	{ 1094400000, 57, 0x0, 0x1, 0 },
	{ 1152000000, 62, 0x0, 0x1, 0 },
	{ 1209600000, 63, 0x0, 0x1, 0 },
	{ 1248000000, 65, 0x0, 0x1, 0 },
	{ 1363200000, 71, 0x0, 0x1, 0 },
	{ 1401600000, 73, 0x0, 0x1, 0 },
	{ 1621600000, 84, 0x0, 0x1, 0 },
	{ 1841600000, 96, 0x0, 0x1, 0 },
	{ 1951600000, 103, 0x0, 0x1, 0 },
	{ }
};

The first column is the operating frequency, and the second column is the supply voltage.

According to the previous rules, it can be roughly judged that the voltage value is increased by 10 for every 200Mhz increase. However, since the chip design frequency is exceeded, power consumption and heat generation must be considered at the same time, so the voltage value must be slightly increased when increasing later. The data after 1401600000 is added according to the above rules.

Modify the file linux/arch/arm64/boot/dts/qcom/msm8916.dtsi. The default frequency appears around line 220. After that, increase the frequency. The increased frequency needs to be added in the previous file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
        opp-1363200000 {
			opp-hz = /bits/ 64 <1363200000>;
		};
		opp-1401600000 {
			opp-hz = /bits/ 64 <1401600000>;
		};
		opp-1621600000 {
			opp-hz = /bits/ 64 <1621600000>;
		};
		opp-1841600000 {
			opp-hz = /bits/ 64 <1841600000>;
		};
		opp-1951600000 {
			opp-hz = /bits/ 64 <1951600000>;
		};

Compile configuration

1
2
3
4
5
cd linux
export CROSS_COMPILE=aarch64-linux-gnu-
export ARCH=arm64
make msm8916_defconfig 
make menuconfig

make msm8916_defconfig imports preset compilation parameters make menuconfig configuration options

Add Klipper USB serial driver

Applicable to the problem that USB devices can be found, but the serial directory and device cannot be found in /dev

  1. Install lsub sudo apt-get install usbutils
  2. Switch the USB port to host mode (run as root) echo host > /sys/kernel/debug/usb/ci_hdrc.0/role
  3. View USB devices Run the lsusb command and you should be able to see the corresponding USB device. If there is no corresponding USB device, it may be a communication problem, a firmware problem, or the board itself is abnormal.
  4. After all the above problems are solved, add the serial port driver
1
2
3
4
5
Device Drivers  --->  [*] USB support  --->  <*>   USB Modem (CDC ACM) support 
Device Drivers  --->  [*] USB support  --->  <*>   USB Serial Converter support  --->   [*]   USB Serial Console device support  
Device Drivers  --->  [*] USB support  --->  <*>   USB Serial Converter support  --->   [*]   USB Generic Serial Driver
Device Drivers  --->  [*] USB support  --->  <*>   USB Serial Converter support  --->   <*>   USB Serial Simple Driver   
其他根据设备种类选择                                                                                             

Start compiling

1
make -j`nproc` bindeb-pkg

The above command directly generates the deb file package in one step. Enter ls ../ to see it.

The software package corresponding to make-kpkg cannot be updated in Ubuntu 22.04. The official statement is that it has not been maintained for too long, so it was removed. Therefore, the following command cannot be run. It may be possible in some earlier versions.

1
2
make -j-j`nproc`
fakeroot make-kpkg  --initrd --cross-compile aarch64-linux-gnu- --arch arm64  kernel_image kernel_headers

After compilation is completed, 3 parts of the file will be generated

  1. Kernel deb package, including headers and image linux-headers-5.15.0-handsomekernel+_5.15.0-handsomekernel+-7_arm64.deb and linux-image-5.15.0-handsomekernel+_5.15.0-handsomekernel+-7_arm64.deb are one level above the compilation directory ../

  2. Image.gz linux/arch/arm64/boot/Image.gz

  3. Device tree dtb linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-xxxxxxxx, choose according to your device

1
2
3
4
-rw-rw-r-- 1 knightli knightli 49312  8月  5 18:32 linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-sp970.dtb
-rw-rw-r-- 1 knightli knightli 49480  8月  5 18:32 linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-ufi001b.dtb
-rw-rw-r-- 1 knightli knightli 49652  8月  5 18:32 linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-ufi001c.dtb
-rw-rw-r-- 1 knightli knightli 49312  8月  5 18:32 linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-uz801.dtb

Process rootfs, root partition

The modification is completed based on the root partition package released by the original openstick project.

Download the original openstick debian base package

1
2
3
4
mkdir ~/rootfs
cd ~/rootfs
wget https://github.com/OpenStick/OpenStick/releases/download/v1/debian.zip && unzip debian.zip
mv ./debian/rootfs.img ~/rootfs

In the debian.zip package, rootfs.img is the img file of the root partition.

使用simg2img转化成可以mount的格式

1
simg2img rootfs.img root.img

chroot handles rootfs

mount root.img

1
2
3
4
5
sudo mount root.img /mnt
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

Install the latest compiled kernel

1
2
sudo cp xxx/linux-headers-5.15.0-handsomekernel+_5.15.0-handsomekernel+-7_arm64.deb /mnt
sudo cp xxx/linux-image-5.15.0-handsomekernel+_5.15.0-handsomekernel+-7_arm64.deb /mnt

Use chroot to enter the mounted system, remove the original linux-image package in the system, and install the just-generated deb software package. Delete the deb file after installation.

1
2
3
4
sudo chroot /mnt
dpkg -l | grep -E "linux-headers|linux-image" |awk '{print $2}'|xargs dpkg -P
dpkg -i *.deb
rm linux-*.deb 

Create rc.local boot script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat>>/etc/systemd/system/rc-local.service<<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#

exit 0
EOF

systemctl daemon-reload && systemctl enable rc-local

Reconfigure locale

The debian used by OpenStick may have been streamlined, so error messages may sometimes appear. warning: setlocale: LC_ALL: cannot change locale (en_US)

The following methods can solve this problem

1
2
$ sudo apt install locales
$ sudo dpkg-reconfigure locales

You can choose en_US.UTF-8

apt source modification

/etc/apt/sources.list.d/mobian.list is invalid and can be deleted directly

Extract files needed for subsequent boot partitions

File:/mnt/boot/initrd**.img Just copy it in advance

End chroot End umount

Directly enter exit to exit

1
2
3
4
5
sudo umount /mnt/proc 
sudo umount /mnt/dev/pts
sudo umount /mnt/dev
sudo umount /mnt/sys
sudo umount /mnt

Use simg2img to convert back

Convert boot.img to rootfs.img format for flashing

1
img2simg root.img rootfs.img

rootfs.img is the root partition image file after final processing.

Process the boot partition

The following takes the ufi001c device as an example, and other devices can be deduced by analogy.

Required file list

  1. Image.gz (linux/arch/arm64/boot/Image.gz) generated during compilation
  2. The device tree dtb (linux/arch/arm64/boot/dts/qcom/msm8916-handsome-openstick-ufi001c.dtb) generated during the compilation process is selected according to your device
  3. /boot/initrd**.img generated after modifying rootfs

Generate boot

1
2
3
cat Image.gz msm8916-handsome-openstick-ufi001c.dtb > ufi001c-kernel-dtb
mv initrd.img-* initrd.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 ufi001c-kernel-dtb -o ufi001c-boot.img

ufi001c-boot.img is the final generated boot partition image file

At this point, rootfs.img ufi001c-boot.img has been generated. Just replace rootfs.img boot.img in the debian.zip flash package and flash it.

记录并分享
Built with Hugo
Theme Stack designed by Jimmy