作者 | 将狼才鲸 |
---|---|
创建日期 | 2022-11-26 |
前提:编译U-Boot和Linux kernel源码时,源码版本、PC主机Linux系统版本、交叉编译器版本都有影响,最好按照网上教程中相同的版本来尝试,要不然经常会遇到编译时遇到了问题,但又搜不到解决方法的尴尬局面。
必须在Linux系统中编译(如Ubuntu),不能在MSYS2中编译。
网上能搜到的QEMU + U-Boot已有的示例都是使用的ARM官方的开发板配置:如vexpress_ca9x4_defconfig、qemu_arm_vexpress_defconfig、versatile_defconfig,第一阶段我也使用相同的配置,后续我会在树莓派2b的硬件上尝试。
编译好的u-boot文件放在本仓库根目录/linux/文件夹下,可以直接运行查看效果
当前硬件为ARM Versatile™ Express开发板系列的CoreTile Express主板。
我使用最新版本的U-Boot官方源码,系统使用的是VMware Player Ubuntu18.04。当前下载的U-Boot版本是v2023.01-rc2,地址:Gitee 极速下载 / u-boot Prepare v2023.01-rc2,文件有33M,要查看所有版本的话则进入https://gitee.com/mirrors/u-boot/tags
参考网址
如果使用虚拟机,则下载安装VMware Player,过程略。
下载并安装64位Ubuntu 18.04,因为我的笔记本电脑是2012年买的,配置不行,所以我在虚拟机中用老系统能跑的比较快;Ubuntu 18.04系统会持续支持到2028年,不用担心里面的软件过时。
参考网址:
Ubuntu 国内镜像下载地址大全
转载:ubuntu各个版本的发行时间和停止支持的时间,更新到最新版和代号。
从ARM官网下载最新的,在64位PC上使用的32位ARM交叉编译工具,下载速度比较慢。
解压交叉编译器,然后在Linux系统中输出芯片架构和交叉编译工具名称的环境变量和交叉编译工具的路径
# 因为我当前不编译Linux PC下的程序,所以我直接将整个环境都配成交叉编译的
export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabihf-
# 增加交叉编译器的路径,修改成你自己解压的路径
export PATH=$PATH:/home/jim/Desktop/tools/arm-gnu-toolchain-11.3/bin
jim@DESKTOP-SVP3BEM MSYS /d
$ qemu-system-arm -M vexpress-a9 -m 256 -kernel u-boot --nographicU-Boot 2023.01-rc2 (Nov 25 2022 - 17:27:26 +0800)DRAM: 256 MiB
WARNING: Caches not enabled
Core: 18 devices, 10 uclasses, devicetree: embed
Flash: 64 MiB
MMC: mmci@5000: 0
Loading Environment from Flash... *** Warning - bad CRC, us
ing default environment
......
如果要编译用于BCM2836或ARM CoreTile Express开发板的Linux kernel,则必须要在Linux环境如Ubuntu中编译。一是因为编译过程中有各种依赖库;二是因为kernel源码中有三个以aux命名的文件,这个文件名在Windows环境中不允许存在,在Windows中解压或者拷贝kernel源码时都会报错;如果一定要在Windows下的MinGW中编译,网上找不到任何教程,你可以在kernel源码中强行修改aux文件名和对应的Kconfig、Makefile后,并安装各种依赖库,只能自行尝试;在MSYS2中安装各种依赖库软件也是需要自行摸索的,应该很少有教程。
Ubuntu主机和交叉编译工具可以直接用上面U-Boot同样的环境,下面介绍的是32位Ubuntu16.04下使用的交叉编译工具;Linux kernel在此环境下也能编译通过。
下面是在64位Ubuntu 18.04中编译Linux kernel的步骤
编译好的zImage和vexpress-v2p-ca9.dtb文件放在本仓库根目录/linux/文件夹下,可以直接运行查看效果
交叉编译环境保持和上面编译U-Boot时相同
下载最新的Linux kernel源码:v6.1-rc4
编译:
安装过程中遇到了报错,需要安装依赖库:
运行:
运行效果:
jim@DESKTOP-SVP3BEM MSYS /d/1_git/cj-security-camera/linux
$ qemu-system-arm -M vexpress-a9 -m 256M -nographic -kernel zImage -dtb vexpress-v2p-ca9.dtb
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.120220712, 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 rangesnode 0: [mem 0x0000000060000000-0x000000006fffffff]
Initmem setup node 0 [mem 0x0000000060000000-0x000000006fffffff]
CPU: All CPU(s) started in SVC mode.
......
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---
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
export PATH=$PATH:/home/jim/Desktop/tools/gcc-arm-10.2/bin ,并重新注销或者重启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 rangesnode 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 directoryPlease press Enter to activate this console.
/ # ls
bin dev linuxrc lost+found sbin usr
/ #