Using QEMU to emulate Linux on Raspberry Pi 3 - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

QEMU is an emulator for emulating machines and the processor-specific instruction set architecture used on the machine.

As a software developer when faced with a lack of physical hardware, QEMU helps in emulating the hardware to continue development.

One such commonly used board is the Raspberry Pi which has a Broadcom SoC with an ARM 64-bit core.

Here I will describe the steps to boot into a Linux environment on the Raspberry Pi 3 using QEMU. The Machine used to run QEMU

Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

Getting QEMU

sudo apt-get install qemu-system-arm

Building Raspberry Pi images

Even though the core Linux OS version may be the same on different embedded platforms, they differ in the kernel modules, drivers written specifically for the peripherals on that platform.

Buildroot

We will use Buildroot to manage the build process. It is better to add buildroot as a submodule to your repo. This helps in syncing easily with the Buildroot project without reorganizing your repo contents.

Buildroot contains board configuration files for many boards. Under the buildroot/configs directory image

Run below commands from outside your buildroot directory which is added as a submodule to begin complete build for Raspberry Pi 3 64-bit

make -C buildroot defconfig BR2_DEFCONFIG=configs/raspberrypi3_64_defconfig
make -C buildroot

The defconfig file I used is here. raspberrypi3_64_defconfig

Once build completes, the kernel, root filesystem and dtb will be available under output/images in the buildroot directory image

The rootfs.ext2 is an image file containing the root filesystem used by the kernel to bring up userspace. Alternatively the image file provided by the Raspbian OS distribution can also be used.

Booting

The below command kicks off the RPi boot on QEMU. Modifying the arguments could cause the boot to hang, so use this command as reference if making any changes.

qemu-system-aarch64 \
	-M raspi3 \
	-kernel $KERNEL \
	-dtb $DTB \
	-m 1024 -nographic \
	-append "rw console=ttyAMA0,115200 root=/dev/mmcblk0 fsck.repair=yes rootwait" \
	-device sd-card,drive=mycard -drive if=none,file=$ROOTFS,format=raw,id=mycard

The kernel,dtb and rootfs paths are local to my system. Modify them to point to the correct path on your development machine

image

image

I have added the above command as shell script here. runqemu-rpi.sh