Communication is hard, especially with all the different personalities, languages, and
cultures involved in an international community like this.

Faith Ekstrand

It is rules and not our individual smarts that keep us from making mistakes.

Faith Ekstrand

Read more »

Scent of a woman (1992)

No mistakes in the Tango, not like life. It’s simple, that’s what makes the Tango so great. If you make a mistake, get all tangled up, just Tango on.

Rudy (1993)

Having dreams is what makes life tolerable

The Shawshank Redemption (1994)

Get busy living or get busy dying

Cache

音同 cash, 不管是在硬件还是软件都是影响性能的一个重要因素之一。Cache 无论是 CPU cache 还是 GPU cache 一般都分级, L1,L2, 在 Multi-processor CPU/GPU 架构中, L1 一般是分开的,每个 Processor 有一个自己的 L1 Cache, 而 L2 Cache 是全局的,所有 processors 共享的。

Read more »

环境准备

WSL2 作为上位机

识别 Windows 11 Host 的 USB Serial Device

分别在 Windows 11 上安装 usbipd-win, 在 WSL2 上安装 user space tools for USB/IP

1
2
3
4
5
6
7
Microsoft Windows [版本 10.0.22621.1702]
(c) Microsoft Corporation。保留所有权利。

C:\Windows\System32>usbipd wsl list
BUSID VID:PID DEVICE STATE
1-9 046d:c534 USB 输入设备 Not attached
1-13 04e2:1410 USB 串行设备 (COM4) Not attached

WSL2 Ubuntu-20.04 创建 /dev/ttyUSB0

1
2
3
4
5
6
7
8
9
10
11
[Thu Jun 15 19:19:44 2023] vhci_hcd vhci_hcd.0: pdev(0) rhport(0) sockfd(3)
[Thu Jun 15 19:19:44 2023] vhci_hcd vhci_hcd.0: devid(65546) speed(2) speed_str(full-speed)
[Thu Jun 15 19:19:44 2023] vhci_hcd vhci_hcd.0: Device attached
[Thu Jun 15 19:19:45 2023] vhci_hcd: vhci_device speed not set
[Thu Jun 15 19:19:45 2023] usb 1-1: new full-speed USB device number 2 using vhci_hcd
[Thu Jun 15 19:19:45 2023] vhci_hcd: vhci_device speed not set
[Thu Jun 15 19:19:45 2023] usb 1-1: SetAddress Request (2) to port 0
[Thu Jun 15 19:19:45 2023] usb 1-1: New USB device found, idVendor=04e2, idProduct=1410, bcdDevice= 0.03
[Thu Jun 15 19:19:45 2023] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[Thu Jun 15 19:19:45 2023] xr_serial 1-1:1.0: xr_serial converter detected
[Thu Jun 15 19:19:45 2023] usb 1-1: xr_serial converter now attached to ttyUSB0

Recovery 模式应该连接哪个 Type-C (USB SERIAL) 接口

HiKey970 有两个 Type-C 接口,而且当板子被设置为 Recovery 模式时,两个接口均会被识别为“串口”。在左手边的 (J3101) 是用来访问 Debug UART 的,而在 HDMI 和 USB 中间的那个(J1801)是在 Recovery 模式下使用的。而且这两个接口是两个不同厂家提供的芯片,使用完全不同的内核驱动模块

USB-to-Serial on Hikey970

  • 前者(J3101):
    Bus 001 Device 003: ID 04e2:1410 Exar Corp. XR21V1410 USB-UART IC

Select CONFIG_USB_SERIAL_XR on WSL2 Kernal config

  • 后者(J1801):
    Bus 001 Device 002: ID 12d1:3609 Huawei Technologies Co., Ltd. USB SER

Select CONFIG_USB_SERIAL_OPTION on WSL2 Kernel config

Build rootfs.img and boot.img

boot.img

boot.img 主要提供 bootloader, 所以它可以只包含 grub.efi, Hikey970 使用的 boot.img 是 64M 大小

rootfs.img

rootfs.img 就是整个系统了(根文件系统),内核可执行文件(Image)和设备树二进制文件(.dtb) 都包含在它的 boot 目录里,Hikey970 使用的 rootfs.img 原始大小是 4.0GB, 但经过 android-tools 工具包里的 img2simg 处理后只有 716M

image 与 Raw image 大小对比
1
2
-rw-r--r-- 1 luc luc 4.0G 11月26日 06:48 rootfs.img
-rw-r--r-- 1 luc luc 716M 11月18日 21:47 rootfs.sparse.img

debootstrap

  • /usr/sbin/qemu-debootstrap
  • /usr/sbin/debootstrap

是两个 Shell 脚本, 主要就是通过下载相应平台的 binaries,通过 chroot 来制作根文件系统

fastboot

fastboot 是用来从 Host 向开发板烧写固件和镜像的常用工具之一,在 Arch Linux 上它可以通过以下命令安装

fastboot
1
yay -S android-sdk-platform-tools

fastboot 常用命令

bootloader 获取各种信息,如 version, partition
1
fastboot getvar all
ID, 可以用来判断设备是否已与 Host 正常连接就绪
1
fastboot devices
1
2
3
4
5
fastboot flash ptable 64gtoendprm_ptable.img
fastboot flash xloader sec_xloader.img
fastboot flash fastboot l-loader.bin
fastboot flash fip fip.bin
fastboot flash boot boot2grub.uefi.img
Sparse 镜像,指定传输块大小,每 8M 传输
1
fastboot -S 8M flash system rootfs.sparse.img

启动

Bootloader

mount -o loop boot2grub.uefi.imglink
1
2
3
4
➜  /mnt ls -lh /mnt/EFI/BOOT
总计 881K
-rwxr-xr-x 1 root root 29K 2018年 2月14日 fastboot.efi
-rwxr-xr-x 1 root root 852K 2018年 2月14日 grubaa64.efi
/boot 目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hikey970% ls -lhR /boot
/boot:
total 23M
drwxr-xr-x 3 root root 4.0K Jun 12 2018 EFI
-rw-r--r-- 1 root root 23M Jun 12 2018 Image
drwxr-xr-x 2 root root 4.0K Jun 12 2018 grub
-rw-r--r-- 1 root root 68K Jun 12 2018 kirin970-hikey970.dtb

/boot/EFI:
total 4.0K
drwxr-xr-x 2 root root 4.0K Jun 12 2018 BOOT

/boot/EFI/BOOT:
total 32K
-rw-r--r-- 1 root root 29K Jun 12 2018 fastboot.efi

/boot/grub:
total 4.0K
-rw-r--r-- 1 root root 462 Jun 12 2018 grub.cfg

Hikey970 boot ubuntu bionic

NOTE:
HiKey970 的输入电压要求在 8V ~ 18V 之间,但最好使用 12V 以上接近 18V的输入电压,否则可能出现 fastboot flash 时出现板子自己重启的怪现象
Hikey970 Input Power Voltage

连接 WiFi

Hikey970 wifi

Hikey970 WiFi LED indicator

xfce4 桌面

吃灰5,6年的板子又再一次亮了

Hikey970 xfce4

制作 rootfs, initramfs

环境是 qemu-system-aarch64 Debian 13 Trixie, 折腾了一圈,最后还是发现 Ubuntu, Debian, Arch Linux 这3个,还是 Debian 对 Aarch64 支持最好,Ubuntu 甚至还一个桌面版的 Arm 安装镜像都没有(Arm架构的安装镜像似乎都是服务器版的)。

Debian vs. Ubuntu

NetworkManager vs wpasupplicant

点亮吹灰 Hikey970 用的是 hikey970-ubuntu-image, 它的 rootfs 里安装的是 NetworkManager, 当我试着将 hikey970-ubuntu-image 转换成 hikey970-debian-image 时,发现 debootstrap 会因为奇怪的包依赖问题,无法安装 NetworkManager, 而且了解到 wpasupplicant 可以完成同样的事情(连接 WiFi,让板子联网), 而且体量更小,更适合这种开发板。

所以这个 Debian 12 rootfs 网络这块使用了 wpasupplicant, iw, iproute2, dhcpcd5 四剑客

  • wpasupplicant
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=WPA supplicant daemon (interface %i)
After=sys-subsystem-net-devices-%i.device
Wants=sys-subsystem-net-devices-%i.device

[Service]
Type=simple
ExecStart=/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i %i
Restart=on-failure

[Install]
WantedBy=multi-user.target

Note:

  1. systemctl enable wpa_supplicant@wlan0.service 时 systemd 会自动创建一个符号链接文件到 /etc/systemd/system/wpa_supplicant@.service, 而文件里的 %i 是无线网络设备接口名,即 wlan0.
  2. 手动创建 /etc/wpa_supplicant/wpa_supplicant.conf
    1
    wpa_passphrase <SSID> <PASSWORD> >/etc/wpa_supplicant/wpa_supplicant.conf
  • iw
1
2
iw dev wlan0 scan | grep 'SSID:'
iw dev wlan0 connect <SSID>
  • iproute2
1
2
ip link show wlan0
ip addr wlan0
  • dhcpcd5
1
2
systemctl enable dhcpcd.service
systemctl start dhcpcd.service
  • ntpdate
1
/sbin/ntpdate ntp.aliyun.com

显示

flowchart LR
    DPE["Display Engine<br>(display controller)"]
    DSI["Display Serial Interface"]
    HDMI["external HDMI converter"]

    subgraph in-chip
      DPE -- RGB timing --> DSI
    end

    DSI --> HDMI

Panfrost on HiKey970

Pathor(Written in C) 和 Tyr(Written in Rust) 都只用于 Valhall 以上的 Mali GPUs. HiKey 970 (HI3670 SoC) 搭载的是 Mali G72 MP12 (Bifrost),所以只能使用 Panfrost 驱动。上面可以启动的内核是 v4.19, 当时的 GPU 驱动还是 lima.

HiKey 970 开发板对应的 devicetree 源文件 hi3670-hikey970.dts, 在 v4.19 时也合入了主线。

commit 5510ee99c0deb0c0235acee6498a6745c8317df1
Refs: v4.19-rc1-6-g5510ee99c0de
Author: Manivannan Sadhasivam mani@kernel.org
AuthorDate: Fri Aug 10 23:23:39 2018 +0530
Commit: Wei Xu xuwei5@hisilicon.com
CommitDate: Wed Sep 19 16:15:25 2018 +0100

arm64: dts: Add devicetree support for HiKey970 board

Add devicetree support for HiKey970 development board which
based on Hi3670 SoC and is also one of the 96Boards Consumer
Edition and AI platform.

Only UART6 is enabled which is the default console required
by the 96Boards Consumer Edition Specification.

This patch has been tested on HiKey970 Board.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Wei Xu <xuwei5@hisilicon.com>

arch/arm64/boot/dts/hisilicon/hi3670-hikey970.dts | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)

v6.19

到目前为止,使用 @mengzhuo/hikey970-ubuntu-image 可以正常启动 HiKey970,而且安装了 Xfce,所以我 fork 了这个仓库,将其更名为 hikey970-debian-image,将基于 Ubuntu bionic (18.04 LTS) 的 rootfs.img 移植到基于 Debian bookworm (12) 的 rootfs.img,并成功启动。之后准备将这块 2018 年 3 月发布的板子作为学习和测试内核最新驱动的平台,所以现在就看看主线编译的设备树 (arch/arm64/boot/dts/hisilicon/hi3670-hikey970.dtb) 和内核 (arch/arm64/boot/Image.gz) 是否能正常启动。

  • kernel + dtb
    Try latest kernel on hikey970
  • kernel only
    Try latest kernel on hikey970

random: crng init done took 70 minutes

1
2
3
4
5
6
7
8
9
10
11
12
[    5.201987] random: perl: uninitialized urandom read (4 bytes read)
[ 5.202263] random: perl: uninitialized urandom read (4 bytes read)
[FAILED] Failed to start systemd-ra…rvice - Load/Save Random Seed.
See 'systemctl status systemd-random-seed.service' for details.
Starting systemd-tmpfiles-…leanup of Temporary Directories...
[ OK ] Finished systemd-tmpfiles-… Cleanup of Temporary Directories.
Starting dpkg-db-backup.se…ly dpkg database backup service...
Starting logrotate.service - Rotate log files...
[ OK ] Finished dpkg-db-backup.se…aily dpkg database backup service.
[ OK ] Finished logrotate.service - Rotate log files.
[ 4396.712657] random: crng init done
[ 4396.712674] random: 4 urandom warning(s) missed due to ratelimiting

crng init 花这么长时间的原因是系统 entropy sources 不足,内核一直在填充 entropy pool, 这种情况只发生在刷写系统后第一次启动,之后 entropy pool 应该固化在 UFS 存储里了,启动时间就正常了。但因为需要频繁刷写系统,所以还是得解决这个问题。尝试了 rng-tools5 和 haveged 后,发现 haveged 可以解决这个问题。理论上 rng-tools5/rng-tools 也可以借助 /dev/hwrng 解决这个问题,但不知为何 rngd 服务始终不能正常工作,怀疑可能是 Hi3670 SoC 的 TRNG 驱动有问题。

参考

系统调用是 Linux 用户态程序与内核通信的接口。 每个特定的文件系统都会在自己的 file_operations 里提供各种文件操作接口,像 .open, .close, .ioctl。系统调用会通过 VFS 的接口调用这些具体的实现,而对于应用程序来说,一般不会直接使用系统调用,而是调用 C 库函数 (C routines).

ioctl 这个系统调用的声明大概是这样:

1
COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, compat_ulong_t, arg)

ioctl 的 C 库函数 (Aarch64 实现):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	.text
ENTRY(__ioctl)
/* move ioctl syscall number to x8 register */
mov x8, #__NR_ioctl
/* sign extend w0 to x0 */
sxtw x0, w0
/* issue software interrupt to invoke syscall */
svc #0x0
/* add 4095 to x0 and set flags according to result */
cmn x0, #4095
/* if carry bit is set (previous addition carrys out), branch to Lsyscall_error */
b.cs .Lsyscall_error
ret
PSEUDO_END (__ioctl)

/* symbol management, making __ioctl and ioctl effectively the same function */
libc_hidden_def (__ioctl)
weak_alias (__ioctl, ioctl)
Read more »

Distro Ubuntu Arch Linux
source git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy git@github.com:archlinux/linux.git
initrd sudo mkinitramfs -o /boot/initrd.img-KERNELVERSION KERNELVERSION sudo mkinitcpio --generate /boot/initramfs-KERNELVERSION.img --kernel KERNELVERSION
grub sudo update-grub sudo grub-mkconfig -o /boot/grub/grub.cfg
  • KERNELVERSIONmake kernelrelease 的输出,同时也是 sudo make modules_install 时在 /lib/modules 创建的目录名
Read more »

Gamma LUT property

Gamma LUT (Look-Up Table) is a property that is used to correct the color and brightness of an image or video. Gamma LUT is basically a table of values that is used to map input values to output values, for example, it may map low input values to higher output values to brighten the image.

Gamma LUT is used to adjust the gamma curve of an image or video, which means it can be used to correct the brightness and contrast of an image. This property is commonly used in image and video processing applications, such as photo editing software, video editing software, and color grading tools.

Gamma LUT can also be used to correct color shifts caused by different lighting conditions or camera settings. By adjusting the gamma curve of an image or video, it is possible to create a more natural and accurate representation of the original scene.

Read more »

Linux 内核的 patch 是以纯文本的邮件形式进行提交和代码走查的,而且 patch 是先到内核子系统 maintainer 维护的 git tree, 再到 Linus Torvalds 的 main tree。本文主要是以一个 patch 提交的实例来记录一下整个过程中的一些具体操作要点,至于 kernel patch 提交的规范和操作细节内核文档 和各种博客文章有很多,这里不再赘述。

Read more »
0%