Lazy loaded image
QEMU (i MX6ULL) + 交叉编译工具链搭建完整指南
Words 1798Read Time 5 min
2026-3-5

QEMU (i.MX6UL) + 交叉编译工具链搭建完整指南

说明:本指南使用 QEMU 的 mcimx6ul-evk 机型(i.MX6UL/Cortex-A7),与 i.MX6ULL 同为 Cortex-A7 架构,适合驱动开发和内核调试。

1. 安装交叉编译工具链

针对 i.MX6UL 系列(ARM Cortex-A7,32位),推荐使用 arm-linux-gnueabihf 工具链。

方案A:直接 apt 安装(最快)

验证:

方案B:使用 Linaro 预编译工具链(推荐,版本可控)


2. 安装 QEMU(支持 ARM virt/sabrelite)

验证:
注意:QEMU 对 i.MX6ULL 的支持机型为 sabrelite(imx6ul-evk),官方支持在 QEMU 5.0+ 之后逐步完善。

3. 准备内核和 DTB

下载并交叉编译内核

编译产物位于:
  • 内核:arch/arm/boot/zImage
  • DTB(mcimx6ul-evk 机型):arch/arm/boot/dts/imx6ul-14x14-evk.dtb

4. 制作最小 rootfs(用 BusyBox)

打包为 initramfs:

5. 启动 QEMU 模拟 i.MX6UL

机型说明mcimx6ul-evk 对应 Freescale i.MX6UL EVK(Cortex-A7),与 i.MX6ULL 同架构,是最接近的 QEMU 机型。
video=off 说明:必须添加此参数。QEMU 未完整模拟 i.MX6UL 的 LCDIF 显示控制器,内核 mxsfb DRM 驱动会因等待 vblank 中断反复超时(每次 10 秒,共约 60 秒),且 fbcon 会将控制台从串口切换到帧缓冲(Console: switching to colour frame buffer device),导致串口无输出。video=off 禁用帧缓冲设备,避免此问题。
退出 QEMU-serial mon:stdio 模式下,按 Ctrl+A 然后按 X 即可终止 QEMU。按 Ctrl+A 再按 C 可进入 QEMU monitor(输入 quit 退出)。

6. 启动延迟分析与优化

imx_v6_v7_defconfig 默认启用了大量 i.MX6 外设驱动,但 QEMU mcimx6ul-evk 未完整模拟这些硬件,导致驱动探测超时。未优化时启动到 init 需要 67s,优化后仅需 5s

6.1 延迟根因:mxsfb DRM 驱动 vblank 超时

QEMU 不模拟 LCDIF vblank 中断,mxsfb DRM 驱动每次 atomic commit 等待 10s 超时,启动过程触发 6 次:
调用链:mxsfb_probe → drm_fbdev_generic_setup → register_framebuffer → fbcon_fb_registered → do_fbcon_takeover → drm_atomic_helper_wait_for_vblanks → 10s timeout × 6 ≈ 60s
注意video=off 内核参数只影响 video mode 解析,不阻止 DRM 驱动通过设备树匹配探测。必须在内核配置中禁用 CONFIG_DRM_MXSFB

6.2 其他 QEMU 不支持的硬件超时

驱动
日志
配置项
耗时
MXS DMA (APBH)
stmp_reset_block: module reset timeout
CONFIG_MXS_DMA
~1s
SNVS RTC
snvs_rtc: probe failed -110
CONFIG_RTC_DRV_SNVS
<0.1s
SPI NOR
spi-nor: probe failed -110
设备树自动探测
~1s
mag3110 / wm8960
I2C 设备探测失败
设备树自动探测
<0.2s

6.3 优化方案(已集成到第3节内核配置步骤)

优化效果:启动到 init 67s → 5s

7. 一键验证脚本


8. 完整工具链验证

目标:编写一个测试程序,交叉编译后打入 rootfs,在 QEMU 中执行并确认输出,形成 编写 → 编译 → 打包 → 运行 的完整闭环。

8.1 编写测试程序

8.2 交叉编译并检查产物

8.3 将程序打入 rootfs 并重新打包

8.4 启动 QEMU 并运行

8.5 预期输出与验证清单

file hello 显示 ELF 32-bit LSB executable, ARM
QEMU 启动后能进入 BusyBox shell(/ # 提示符)
执行 /hello 输出 Hello from cross-compiled binary! 及内核信息
uname -m 在 QEMU 内返回 armv7l

8.6 常见问题排查

现象
可能原因
解决方案
/hello: not found
未将 hello 打入 rootfs 或路径错误
确认 hello 在 _install/ 根目录,重新 cpio 打包
Exec format error
使用了宿主机 gcc 而非交叉编译器
file hello 确认为 ARM ELF,重新用 arm-linux-gnueabihf-gcc 编译
No such file or directory(动态链接)
编译时未加 -static,rootfs 缺少 ld-linux
-static 重新编译,或将工具链 sysroot 下的 .so 拷入 rootfs
Permission denied
hello 缺少可执行权限
打包前执行 chmod +x hello
启动极慢(60s+),串口输出中断,日志出现 vblank wait timed outflip_done timed outConsole: switching to colour frame buffer device
QEMU 未完整模拟 LCDIF 显示控制器,mxsfb DRM 驱动 vblank 超时,fbcon 抢占串口控制台
内核启动参数加 video=off

总结路径

遇到具体报错(内核 panic、DTB 不匹配、串口无输出等),直接把错误信息发给我,我帮你定位。
上一篇
Data Structure and Algorithm
下一篇
用面试拷问嵌入式技术栈

Comments
Loading...