时间轴

2025-12-26

init


环境

OS

使用wsl的archlinux
环境

compiler

ARM目前总共发布了8种架构:ARMv1、ARMv2、ARMv3、ARMv4、ARMv5、ARMv6、ARMv7、ARMv8。

针对于Cortex-A5x处理器可以使用aarch64-linux-gnu-gcc -march=armv8-a命令编译代码,ARM GNU编译器可通过下面的链接下载

使用archlinux上通过pacman下载的aarch64-linux-gnu-gcc版本是15.1.0

aarch64-linux-gnu-gcc

source code

源码是topeet提供的:

编译内核

编译

使用aarch64-linux-gnu-gcc 15.1.0版本编译器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo pacman -Sy bc

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- rockchip_linux_defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_prepare

# -Wno-dangling-pointer 禁用 GCC 的 “悬空指针” 警告。
# -Wno-enum-int-mismatch 禁用 “枚举类型与整数类型不匹配” 的警告。
export KCFLAGS="-Wno-dangling-pointer -Wno-enum-int-mismatch"

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image dtbs -j$(nproc)

unset KCFLAGS # 可选,用完清除

clangd 查看内核源码

生成compile_command.json

1
./scripts/clang-tools/gen_compile_commands.py

使用clangd作为语言服务器要添加ComplieFlags,因为clangd对GCC的某些编译选项不识别:

1
2
3
CompileFlags:
Add: -Wno-unknown-warning-option
Remove: [-m*, -f*]

sp报错

clangd报错:

1
clang [asm_invalid_global_var_reg]: Register 'sp' unsuitable for global register variables on this target

将变量 current_stack_pointer 绑定到 sp 寄存器(即当前栈指针),以便直接读取栈顶地址。这种写法在 GCC 编译 Linux 内核时是合法的(尤其在 ARM64 架构下),但 Clang 对此有更严格的限制。因此我们修改arch/arm64/include/asm/stack_pointer.h源码如下,对于__clang__使用内联汇编函数返回sp的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_STACK_POINTER_H
#define __ASM_STACK_POINTER_H

/*
* how to get the current stack pointer from C
*/
#ifdef __clang__
// For clang: fake it
static inline unsigned long get_sp(void)
{
unsigned long sp;
__asm__("" : "=r"(sp) : "r"(0));
return sp;
}
#define current_stack_ptr get_sp()

#else
register unsigned long current_stack_pointer asm("sp");

#endif

#endif /* __ASM_STACK_POINTER_H */

编译uboot

1
2
3
4
make rk3568_defconfig
export KCFLAGS="-Wno-enum-int-mismatch -Wno-maybe-uninitialized"
make CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc) all
unset KCFLAGS # 可选,用完清除

生成:

  • u-boot(ELF)
  • u-boot.bin(原始二进制)
  • spl/u-boot-spl.bin(如果启用 SPL)
  • tpl/u-boot-tpl.bin(如果启用 TPL)
  • u-boot.dtb(内嵌或外部设备树)
1
2
3
tools/mkimage -n rk3568 -T rksd -d tpl/u-boot-tpl.bin idbloader.img

cat spl/u-boot-spl.bin >> idbloader.img

这样生成的loader可能有点问题还待研究TODO

编译根文件系统

1
2
3
4
5
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- rockchip_rk3568_defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
sudo pacman -Sy cpio rsync


编译recover

TODO

烧写

TODO