作者 | 将狼才鲸 |
---|---|
创建日期 | 2022-11-26 |
前提:编译U-Boot和Linux kernel源码时,源码版本、PC主机Linux系统版本、交叉编译器版本都有影响,最好按照网上教程中相同的版本来尝试,要不然经常会遇到编译时遇到了问题,但又搜不到解决方法的尴尬局面。
必须在Linux系统中编译(如Ubuntu),不能在MSYS2中编译。
MSYS2中的QEMU不能引导文件系统(未尝试去查找解决该问题),在Linux下运行QEMU能正常引导。
从官网下载最新的BusyBox发布版本:busybox-1.35.0.tar.bz2
因为BusyBox源码更新较慢,ARM最新的编译器11.3编译BusyBox报错无法解决,所以我改用老版本的编译器,能编译通过
10.2版本编译器ARM官方下载地址:gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
交叉编译环境的配置方法和上面编译U-Boot时相同
# 因为我当前不编译Linux PC下的程序,所以我直接将整个环境都配成交叉编译的
export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabihf-
# 增加交叉编译器的路径,修改成你自己解压的路径
export PATH=$PATH:/home/jim/Desktop/tools/gcc-arm-10.2/bin
# 如果之前以及输出了别的版本gcc的路径,则需并重新注销或者重启Ubuntu后再执行上面的步骤
static
)
以下内容无效。以下为尝试解决最新的ARM GCC编译器编译时的问题,但最终失败,但保留重新编译安装coreutils工具集的步骤:
遇到报错无法解决:
coreutils/dd.c: 在函数‘dd_output_status’中:
coreutils/dd.c:123:21: 编译器内部错误: 非法指令
123 | #define G (*(struct globals*)bb_common_bufsiz1)
| ^~~~~~~
coreutils/dd.c:192:29: 附注: in expansion of macro ‘G’
192 | seconds = (now_us - G.begin_time_us) / 1000000.0;
| ^
free(): invalid next size (fast)
editors/awk.c: 在函数‘next_token’中:
editors/awk.c:1121:17: 编译器内部错误: 已放弃
1121 | debug_printf_parse("%s: using concat-inserted token\n", __func__);
| ^~~~~~~~~~~~~~~~~~
export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabihf-
export PATH=$PATH:/home/jim/Desktop/tools/arm-gnu-toolchain-11.3/bin
已在BusyBox源码文件夹下生成了_install目录,里面有板子上能用到的各种系统命令
制作根文件系统:
mkdir my_mnt
dd if=/dev/zero of=rootfs.img bs=1024 count=16384 # 创建 16MB 虚拟磁盘
mkfs.ext2 rootfs.img # 格式化成 ext2 格式文件系统
sudo mount -o loop rootfs.img my_mnt # 将镜像文件和块设备关联并挂载设备到my_mnt
sudo cp -r _install/* my_mnt # 将 BUsybox 所有生成的程序拷贝到根目录
# 创建4个tty设备(c代表字符设备,4是主设备号,1~4分别是次设备号)
sudo mkdir -p my_mnt/dev
sudo mknod my_mnt/dev/tty1 c 4 1
sudo mknod my_mnt/dev/tty2 c 4 2
sudo mknod my_mnt/dev/tty3 c 4 3
sudo mknod my_mnt/dev/tty4 c 4 4
# 创建终端
sudo mknod -m 666 my_mnt/console c 5 1
sudo umount my_mnt
jim@jim:~/Desktop/usr$ qemu-system-arm -M vexpress-a9 -m 256M -nographic -kernel zImage -dtb vexpress-v2p-ca9.dtb -sd rootfs.img -append "root=/dev/mmcblk0 rw console=ttyAMA0"
WARNING: Image format was not specified for 'rootfs.img' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
Booting Linux on physical CPU 0x0
Linux version 6.1.0-rc6 (jim@jim) (arm-none-linux-gnueabihf-gcc (Arm GNU Toolchain 11.3.Rel1) 11.3.1 20220712, GNU ld (Arm GNU Toolchain 11.3.Rel1) 2.38.20220708) #1 SMP Fri Nov 25 23:17:29 CST 2022
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
OF: fdt: Machine model: V2P-CA9
Memory policy: Data cache writeback
Reserved memory: created DMA memory pool at 0x4c000000, size 8 MiB
OF: reserved mem: initialized node vram@4c000000, compatible id shared-dma-pool
cma: Reserved 16 MiB at 0x6f000000
Zone ranges:
Normal [mem 0x0000000060000000-0x000000006fffffff]
Movable zone start for each node
Early memory node ranges
node 0: [mem 0x0000000060000000-0x000000006fffffff]
Initmem setup node 0 [mem 0x0000000060000000-0x000000006fffffff]
CPU: All CPU(s) started in SVC mode.
percpu: Embedded 15 pages/cpu s30612 r8192 d22636 u61440
Built 1 zonelists, mobility grouping on. Total pages: 65024
Kernel command line: root=/dev/mmcblk0 rw console=ttyAMA0
printk: log_buf_len individual max cpu contribution: 4096 bytes
printk: log_buf_len total cpu_extra contributions: 12288 bytes
printk: log_buf_len min size: 16384 bytes
printk: log_buf_len: 32768 bytes
printk: early log buf free: 14944(91%)
......
can't run '/etc/init.d/rcS': No such file or directory
Please press Enter to activate this console.
/ # ls
bin dev linuxrc lost+found sbin usr
/ #