8

I am trying to emulate a Raspberry Pi zero W with Qemu based on an image I used on a real Raspberry Pi zero W.

The command I am using is:

sudo qemu-system-arm \                                                                                                                                                       
-kernel ./qemu-rpi-kernel/kernel-qemu-4.9.59-stretch \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-hda pi_zero_kinetic_raspbian.qcow \
-cpu arm1176 -m 512 \
-M versatilepb \
-no-reboot \
-serial stdio \
-net nic -net user \
-net tap,ifname=vnet0,script=no,downscript=no

But Qemu complain that Error: unrecognized/unsupported machine ID (r1 = 0x00000183)

So added this option:

-dtb linux/arch/arm/boot/dts/bcm2835-rpi-zero-w.dts

But In this case:

qemu-system-arm: Unable to copy device tree in memory
Couldn't open dtb file qemu-rpi-kernel/tools/linux/arch/arm/boot/dts/bcm2835-rpi-zero-w.dts

So I tried to compile the dts in order to get the dtb with:

dtc -O dtb -o bcm2835-rpi-zero-w.dtb bcm2835-rpi-zero-w.dts

But the compilation fail and I get:

Error: bcm2835-rpi-zero-w.dts:13.1-9 syntax error
FATAL ERROR: Unable to parse input tree

I couldn't find any tutorial about Pi zero and all the tutorial about the first Rapsberry Pi seems to be outdated. I am not sure that compiling the dtb on my own is the way to go. Any input would be appreciated, thanks!

4 Answers 4

5

This works for me on MacOS Mojave,

Create a file called qemu_script.sh and copy-paste the code below

nano qemu_script.sh

give execution perms and execute,

$ chmod +x qemu_script.sh
$ ./qemu_script.sh

Code:

#!/bin/sh

brew install qemu

export QEMU=$(which qemu-system-arm)
export TMP_DIR=~/tmp/qemu-rpi
export RPI_KERNEL=${TMP_DIR}/kernel-qemu-4.14.79-stretch
export RPI_FS=${TMP_DIR}/2018-11-13-raspbian-stretch-lite.img
export PTB_FILE=${TMP_DIR}/versatile-pb.dtb
export IMAGE_FILE=2018-11-13-raspbian-stretch-lite.zip
export IMAGE=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/${IMAGE_FILE}

mkdir -p $TMP_DIR; cd $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.14.79-stretch?raw=true \
        -O ${RPI_KERNEL}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb \
        -O ${PTB_FILE}

wget $IMAGE
unzip $IMAGE_FILE


$QEMU -kernel ${RPI_KERNEL} \
    -cpu arm1176 -m 256 -M versatilepb \
    -dtb ${PTB_FILE} -no-reboot \
    -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
    -drive "file=${RPI_FS},index=0,media=disk,format=raw" \
    -net user,hostfwd=tcp::5022-:22 -net nic \

You should get: enter image description here

5
  • What's that? Can you explain please?
    – Ingo
    Feb 11, 2019 at 20:39
  • Ok, added steps and resulting image in MacOS Feb 11, 2019 at 23:17
  • You don't need export in your script. Oct 31, 2019 at 20:40
  • @PierreThibault feel free to modify the post if you tested it and works for you Nov 1, 2019 at 2:21
  • @user9869932 I made a Python script so it can also work on Windows. I cannot share it. Nov 1, 2019 at 21:02
3
  1. You are still using "versatilepb". If you want to emulate a Raspberry Pi, use "raspi"
  2. The precompiled DTB files can be downloaded from the foundation firmware github. Direct link for the Pi Zero DTB here.
3
  • Thanks for the answer. If I use the "raspi" machine I got unsupported machine type. And raspi is not listed as supported machine. Is there a way to add it?
    – GuillaumeB
    Sep 19, 2018 at 10:13
  • I even tried with the latest version of Qemu (from source 3.0.50) and there is only raspi2 no raspi
    – GuillaumeB
    Sep 19, 2018 at 12:51
  • 2
    what happened when you tried raspi2?
    – jsotola
    Sep 20, 2018 at 23:44
1

I share my updated version of the script for Raspbian-buster-lite guest on Ubuntu host.

#!/bin/sh

QEMU=$(command -v qemu-system-arm)
TMP_DIR=qemu-rpi
RPI_KERNEL=kernel-qemu-4.19.50-buster
RPI_KERNEL_FILE=$TMP_DIR/$RPI_KERNEL
PTB=versatile-pb.dtb
PTB_FILE=$TMP_DIR/$PTB
IMAGE_BASE=2019-09-26-raspbian-buster-lite
IMAGE=$IMAGE_BASE.zip
IMAGE_FILE=$TMP_DIR/$IMAGE
RPI_FS=$TMP_DIR/$IMAGE_BASE.img

mkdir -p $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/${RPI_KERNEL}?raw=true \
        -O ${RPI_KERNEL_FILE}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/$PTB \
        -O ${PTB_FILE}

wget http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/$IMAGE \
        -O ${IMAGE_FILE}
unzip $IMAGE_FILE -d $TMP_DIR


$QEMU -kernel ${RPI_KERNEL_FILE} \
    -cpu arm1176 -m 256 -M versatilepb \
    -dtb ${PTB_FILE} -no-reboot \
    -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
    -drive "file=${RPI_FS},index=0,media=disk,format=raw" \
    -net user,hostfwd=tcp::5022-:22 -net nic
0

What I did was modify the QEMU source to add support for RPi Zero W recompile. https://github.com/igwtech/qemu

1
  • 4
    Please be more specific than "I did some modifications"!
    – MatsK
    Apr 10, 2020 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.