7

Here I got an image of Raspberry Pi (re-designed). Let's call it pi2-16gb.img

Emulate ARM Raspberry Pi 2 model B v1.1 on a Ubuntu PC

Now I want to emulate it before inserting it to a Pi 2 physically.

I created a startup script start_arm_pi.sh as this:

#!/bin/bash
# Starts raspberry pi image in configuration mode

qemu-system-arm -kernel ./boot_for_pi2/kernel.img \
    -cpu arm1176 -m 1024 \
    -M versatilepb -no-reboot -serial stdio \
    -append "root=/dev/mapper/crypt_sdcard cryptdevice=/dev/mmcblk0p2:crypt_sdcard panic=1 rootfstype=ext4 rw init=/bin/bash" \
    -hda /path/to/pi2-16gb.img 

But with error:

pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument

About the image

Actually there's two partitions of the image file pi2-16gb.img.

/boot/ from /dev/mmcblk0p1 and / from /dev/mmcblk0p2 . But the /dev/mmcblk0p2 is encrypted by LUKS encryption.

the kernel.img is derived from the un-encrypted partition /dev/mmcblk0p1

All the files in the partition /dev/mmcblk0p1 (will be mounted as /boot/ are in this picture.

enter image description here

And I think the file cmdline.txt and the file config.txt may be helpful.

Content of cmdline.txt

dwc_otg.fiq_fix_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mapper/crypt_sdcard cryptdevice=/dev/mmcblk0p2:crypt_sdcard rootfstype=ext4 rootwait

Content of config.txt

initramfs initramfs.gz 0x00f00000 hdmi_ignore_hotplug=1

Do you have some ideas that emulate Pi on a my Ubuntu PC?

Thank you a million!!


updated

I had a physical Pi 2 running this image.

And from the TTL serial port (Tx, Rx), I received messages as the following)

pastebin(dot)com/Y7Xg5WRg

Found a info line about kernel command:

[ 0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa21041 bcm2709.serial=0x2925606f smsc95xx.macaddr=B8:27:EB:25:60:6F bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000 dwc_otg.fiq_fix_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mapper/crypt_sdcard cryptdevice=/dev/mmcblk0p2:crypt_sdcard rootfstype=ext4 rootwait

Will this be helpful?


updated

I had found that qemu has support Raspberry Pi 2 from version 2.6.0 according to its doc page: wiki.qemu(dot)org/ChangeLog/2.6

So I compiled qemu version 2.6.0 on my Ubuntu 14.04 x86_64 PC. And I went to boot it using this command.

/usr/local/src/qemu-2.6.0/bin/debug/native/arm-softmmu/qemu-system-arm \
    -kernel /path/to/boot_for_pi2/kernel7.img \
    -cpu arm1176 -m 256 \
    -M raspi2 \
    -no-reboot \
    -serial stdio \
    -sd /path/to/pi2-16gb.img \
    -append "dwc_otg.fiq_fix_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mapper/crypt_sdcard cryptdevice=/dev/mmcblk0p2:crypt_sdcard rootfstype=ext4"

And here's the output now.

WARNING: Image format was not specified for '/path/to/pi2-16gb.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.
Uncompressing Linux... done, booting the kernel.

But it stopped here and the qemu window is black with nothing...

Does this mean something's wrong with the kernel?

File kernel7.img is derived from its /boot/ partition actually.


updated

I started the emulator successfully but with an error...

/usr/local/src/qemu-2.6.0/bin/debug/native/arm-softmmu/qemu-system-arm \
-kernel /path/to/kernel-qemu-4.4.11-jessie \
-m 256 \
-cpu arm1176 \
-M versatilepb \
-no-reboot \
-serial stdio \
-hda /media/alex/F049-1FB6/2016-05-27-raspbian-jessie.img \
-append "dwc_otg.lpm_enable=0 root=/dev/sda2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait init=/bin/bash" \

The driver can be mounted. Here I tested it with a latest rasbian image 2016-05-27-raspbian-jessie.img

The qemu kernel I used is from :

github(dot)com/dhruvvyas90/qemu-rpi-kernel

But I had a kernel panic...

Here's the error in detail.

debug error kernel panic

  • It doesn't look like there is a space in between the #! and the /bin/bash I believe the space is required for your script to work. – sir_ian Jun 29 '16 at 19:30
  • It's alright. Not this thing. – dotslash Jun 29 '16 at 23:40
  • You shouldn't use a "Versatile" or "Qemu" kernel with the newest QEMU version. Use the real Raspberry Kernel (extract it from the image ?). – flakeshake Jul 2 '16 at 14:05
  • I tried the Kernel derived from the image but it did not even load. So what CPU model should I set to use with the qemu 2.6? – dotslash Jul 4 '16 at 19:00
  • 2
    Possible duplicate of Emulation on a Linux PC – Ciro Santilli 新疆改造中心996ICU六四事件 Aug 22 '17 at 9:43
2

I succeeded in launching Raspbian Jessie Lite on Kubuntu 14.04 after I compiled QEMU from git. There's a bit of a problem though: I haven't made the keyboard input work (have to use virtual serial terminal instead).

Anyway, here's what I did:

1) Compile QEMU from git (adjust configure options for your needs, but these did work well enough for me):

$ sudo apt-get build-dep qemu
$ git clone https://github.com/qemu/qemu
$ cd qemu
$ ./configure --prefix=$HOME/opt/qemu --with-sdlabi=1.2 \
  --disable-gtk --enable-sdl
$ make
$ make install

2) Determine Raspbian image partition layout:

$ fdisk -l 2017-07-05-raspbian-jessie-lite.img

Disk 2017-07-05-raspbian-jessie-lite.img: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5e88b289

                              Device Boot      Start         End      Blocks   Id  System
2017-07-05-raspbian-jessie-lite.img1            8192       93596       42702+   c  W95 FAT32 (LBA)
2017-07-05-raspbian-jessie-lite.img2           94208     4194303     2050048   83  Linux

3) Mount Raspbian image's first partition taking into account the figures for start of partitions obtained above (sector 8192 for partition 1):

$ cd ~/raspbian
$ sudo mkdir -pv /mnt/tmp
$ sudo mount ./2017-07-05-raspbian-jessie-lite.img /mnt/tmp/ -oloop,offset=$((8192*512))

4) Take initial cmdline.txt from there, as well as the correct kernel and .dtb file:

$ cp -v /mnt/tmp/{kernel7.img,bcm2709-rpi-2-b.dtb,cmdline.txt} .

5) Unmount the image:

$ sudo umount /mnt/tmp

6) To prevent a the kernel complaining that the root partition extends beyond SD card size, append a bit of zeros to the image (here I choose 2 GiB — this is a bit of an overkill, but won't hurt, adjust for your needs):

$ fallocate -l 2G ./2017-07-05-raspbian-jessie-lite.img

7) We can finally do a first launch, where the initial script will resize the filesystem to fit the whole device:

$ ~/opt/qemu/bin/qemu-system-arm -M raspi2 -kernel ./kernel7.img \
   -dtb ./bcm2709-rpi-2-b.dtb -sd ./2017-07-05-raspbian-jessie-lite.img \
   -append "$(<cmdline.txt)"

The kernel will fail to reset, so when it prints sysrq: SysRq: Resetting, just kill QEMU via Ctrl+C or closing its window.

8) Again mount the boot partition and copy the updated cmdline.txt:

$ sudo mount ./2017-07-05-raspbian-jessie-lite.img /mnt/tmp/ -oloop,offset=$((8192*512))
$ cp -v /mnt/tmp/cmdline.txt .
$ sudo umount /mnt/tmp

9) Finally, launch QEMU as you did in step 7), and Raspbian start normally, so after a minute or two you'll be at the login prompt.

Now, as the keyboard doesn't work (seems to be QEMU limitation for RPi), you'll likely want to setup virtual serial console. To do this, make sure the final cmdline.txt has console=ttyAMA0,115200 (and no other mentionings of ttyAMA0), and QEMU command line option -serial stdio is present. This will give you login prompt in the terminal used by QEMU, where you can then type and use your system.

To avoid QEMU being killed when you type Ctrl+C in the serial console, see this Unix.SE post.

  • Thanks for sharing! I also made it with some settings here – dotslash Feb 2 '18 at 9:43
0

You'll only be able to emulate the devices specifically supported (or that you've added), e.g., http://qemu.weilnetz.de/qemu-doc.html#ARM-System-emulator.

AFAICT some chunks of RPi hardware aren't supported without customization.

  • But I see that qemu version 2.6 has support for raspberry pi 2 already. wiki.qemu.org/ChangeLog/2.6 What commands should I use to boot this image now? I have compiled a newer qemu 2.6 already for test. – dotslash Jun 29 '16 at 14:13
  • From the release notes you are quoting: "New partial Raspberry Pi 2 emulation with "raspi2" machine type. For now, it can boot older releases of Windows and Raspbian, but lacks a number of devices including USB. " So likely you can only use Wheezy (not Jessie) images, and USB doesn't work (which is quite trivial). – Phil B. Jun 29 '16 at 14:19
  • @PhilB. "... lacks a number of devices" is pretty vague, no? Meaning integrated hardware like audio may not work. – Dave Newton Jun 29 '16 at 15:54
  • @PhilB. I have updated this question. And the qemu did not boot the kernel correctly. @Dave Newton, I am not going to attach anything other device to it now. It should be possible to emulate it, just the img file itself. – dotslash Jun 29 '16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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