Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cross Compiling for Pi

cross compile

go lang for pi 0

Using rust in Raspberry pi

QEMU for library dependencies

building qemu from raspberri pi zero in wsl2 ubuntu

It is best if you make a directory somewhere in windows for the sources. Using powershell to keep wsl2 VHD files small,

cd c:\Users\<user_name>\Documents
mkdir qemu-build

Start ubuntu wsl2 instance. Now using shell,

cd ~/<some_dir>
mkdir qemu-build
sudo mount --bind "/mnt/c/Users/<user_name>/Documents/qemu-build" qemu-build
cd qemu-build

This is where we will build qemu for raspberry pi zero.

Get qemu sources and dependencies,

git clone https://github.com/igwtech/qemu
# do followings only if you need to modify submodule sources
# git submodule init
# git submodule update --recursive

We are using a forked qemu source above because the official qemu repo doesn't provide support for raspberry pi zero. Feel free to diff the code with tags from original source, which will provide valuable insight to adding another arm processor support.

Activate source repositories by un-commenting the deb-src lines in /etc/apt/sources.list.

Get qemu dependencies,

sudo apt-get update
sudo apt-get build-dep qemu

Create a build directory

mkdir build
cd build

Configure qemu to build all qemu binaries,

../qemu/configure

Otherwise if you already have installed all the binaries, or only interested in qemu-arm and qemu-system-arm, this configures to build just those,

../qemu/configure --target-list=arm-softmmu,arm-linux-user

To find all the configuration options, run configure --help.

Build and install qemu,

make
sudo make install

Now we can remove the mount,

cd ../..
sudo umount qemu-build

You can remove the build directory qemu-build\build if you like, or keep it for later development.

Run qemu for raspi0,

qemu-system-arm -machine raspi0 -serial stdio  -dtb bcm2708-rpi-zero-w.dtb -kernel kernel.img -append 'printk.time=0 earlycon=pl011,0x20201000 console=ttyAMA0'

qemu-kvm has problems in wsl2, currently it does not work properly.

Raspbian apt sources

/etc/apt/sources.list,

deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi

/etc/apt/sources.list.d/raspi.list,

deb http://archive.raspberrypi.org/debian/ buster main
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://archive.raspberrypi.org/debian/ buster main

install the cross compiler

check https://github.com/Pro/raspi-toolchain to use their prebuilt toolchain in wsl2

# Download the toolchain:
wget https://github.com/Pro/raspi-toolchain/releases/latest/download/raspi-toolchain.tar.gz
# The toolchain has to be in /opt/cross-pi-gcc since it's not location independent.
sudo tar xfz raspi-toolchain.tar.gz --strip-components=1 -C /opt

raspbian filesystem

/etc/fstab,

proc            /proc           proc    defaults          0       0
PARTUUID=288695f5-01  /boot           vfat    defaults          0       2
PARTUUID=288695f5-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

pi-sd-2/etc/ld.so.preload,

/usr/lib/arm-linux-gnueabihf/libarmmem-${PLATFORM}.so

Installing library dependencies in a image

If there is a dependency on additional libraries, we should install those in the raspberry pi SD. Then we can save an image of the SD using Win32DiskImage in a .img file. Now we can mount the image and copy the necessary libraries to the toolchain sysroot we installed earlier.

$ fdisk -lu /mnt/d/pi_images/pi-sd.img
Disk /mnt/d/pi_images/pi-sd.img: 28.97 GiB, 31086084096 bytes, 60715008 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
Disklabel type: dos
Disk identifier: 0x288695f5

Device                      Boot  Start      End  Sectors  Size Id Type
/mnt/d/pi_images/pi-sd.img1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/mnt/d/pi_images/pi-sd.img2      532480 60715007 60182528 28.7G 83 Linux

The fat32 partition is the first one. The offset is 8192*512=4194304 and size is 524288*512=268435456 in bytes.

The ext4 partition is the second one. The offset is 512*532480=272629760 and size is 512*60182528=30813454336 in bytes.

Now you can mount them,

mkdir /home/pi/pi-sd-1
mkdir /home/pi/pi-sd-2

mount -o loop,offset=4194304,sizelimit=268435456 /mnt/d/pi-images/pi-sd.img /home/pi/pi-sd-1
mount -o loop,offset=272629760 /mnt/d/pi_images/pi-sd.img /home/pi/pi-sd-2

ls -la /home/pi/pi-sd-1
ls -la /home/pi/pi-sd-2

There is no need to specify size for the last partition. At this point we can edit the image to get it ready for emulation.

to cross compile copy all libraries

rsync -vR --progress -rl --delete-after --safe-links /home/pi/pi-sd-2/{lib,usr,etc/ld.so.conf.d,opt/vc/lib} $HOME/rpi/rootfs

(TODO) Now you can copy appropriate libraries to /opt/rpi_tools/arm-bcm2708/arm-linux-gnueabihf/arm-linux-gnueabihf/sysroot.

qemu rpi kernel (TODO)

disk images

utilities