Wednesday, August 17, 2016

Recap on setting up Raspberry Pi emulation on MacOSX

The purpose is not completely guide how to setup Raspberry Pi emulation on MacOSX but rather to note my practice and with specific problem solving on my own environment

This is really interesting topic, it recalled my old days with Linux programming, configure and build Linux kernel, but I have to give up my time, just want to get the Raspberry emulation up and run, that's it!

I had referenced many sources (see below) in order to setup and solve problems and when I finished my setup, I got the conclusion:
  1. Install brew (http://brew.sh/) on your MacOSX first
  2. Use brew to install common tools and libraries
  3. Download and build ARM build tools chain, this will be used to compile Raspberry Kernel
  4. Download, configure and build Raspberry Kernel using tools chain above
  5. Install QEMU and run Raspberry emulation
I would also make a copy of sources in put it here, it's just to make sure the guide still here and convenient for you to follow, or in the worst case if the source goes down (that I faced sometime when reading guide that has referenced sources)


  • In order to install brew, it's quite simple so I would like to skip this step and move on from the step #2 install common tools and libraries, #3 download and build ARM tools chain

brew install mpfr gmp libmpc libelf texinfo
mkdir ~/rpi
mkdir ~/rpi/arm-cs-tools
git clone https://github.com/jsnyder/arm-eabi-toolchain.git
cd arm-eabi-toolchain
PREFIX=$HOME/rpi/arm-cs-tools make install-cross
make clean
echo "export PATH=$HOME/rpi/arm-cs-tools/bin:$PATH" >> ~/.zshrc

*Please be noticed here: If you use the other shell rather than zsh you have to change to output of export accordingly, in my case, it's bash shell - .bash_profile. It also depends on your purpose if you want to setup tools chain for your own usage only or for global system you might want to output export command to appropriate shell's resource or profile file

You also have to update XCode in your MacOSX to latest version, the reason is the build tools, and libraries, dependencies might depend on XCode's libraries in order to build your 'build tools' or Kernel

  • Clone Raspberry Kernel:

mkdir ~/rpi/kernel
cd ~/rpi/kernel
git clone https://github.com/raspberrypi/linux.git
cd linux

  • Make a copy of predefined Kernel Configuration, and modify it as your wishes

cp arch/arm/configs/bcmrpi_cutdown_defconfig .config
make ARCH=arm CROSS_COMPILE=~/rpi/arm-cs-tools/bin/arm-none-eabi- menuconfig

*The .config file is where you can modify your Kernel configuration, and the build process will follow up defined configuration in there

In my case, there is no bcmrpi_cutdown_defconfig in the folder, so I have to google, download and put it in configs folder

At this point, I leave all the default (by pressing ENTER) to the questions from command in order to build .config file

If you don't really know what changes (configuration files, aka: .config file) written to your Kernel configuration you can issue command below to see what files had changed last 10 minutes

find ~/rpi/kernel/arch/arm/configs -mmin -10

  • Make sure SYMBOLS are defined

During compilation process it will appear several warnings that you can ignore but not for the errors. There are many copies of the orginial tutorial on this topic (not sure which one is the original) but none of them correct this error of missing SYMBOLS, except comment on this link: http://www.mluis.com/post/27090213894/rpi-osx. Thanks to Alan for his discover https://disqus.com/by/alanjjenkins/ atleast it's applicable for my environment where it missed #define R_X86_64_64 0 and #define R_X86_64_NONE 0.

Also, the libelf.h is in libelf folder, in my case, so I have to modify a bit the include to #include

sudo touch /usr/local/include/elf.h

#include
#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
#define R_ARM_NONE 0
#define R_ARM_PC24 1
#define R_ARM_ABS32 2
#define R_MIPS_NONE 0
#define R_MIPS_16 1
#define R_MIPS_32 2
#define R_MIPS_REL32 3
#define R_MIPS_26 4
#define R_MIPS_HI16 5
#define R_MIPS_LO16 6
#define R_X86_64_64 0
#define R_X86_64_NONE 0

  • Build your Kernel

make ARCH=arm CROSS_COMPILE=~/rpi/arm-cs-tools/bin/arm-none-eabi- -k

During Kernel compilation process, you might see errors alike below and the process will be stopped (thanks to http://blog.icanit.ru/2014/05/dev-c-kernel-rpi-os-x.html)
UNROLL lib/raid6/int1.c
 awk: can't open file -vN=1
  source line number 1 source file -vN=1
  context is
       >>> <<<  
  CC [M] lib/raid6/int1.o
 arm-none-eabi-gcc: ошибка: lib/raid6/int1.c: No such file or directory

It happened the same in my environment so I just follow the steps guided in the link to fix it. It is not really exactly the line number mentioned in the link. But you still can compare the text below with relevant text in your file

Fix editing lib / raid6 / Makefile file line 14.15 (Adds problems after -v, -N and change the order in the OS X awk sensitive):
    cmd_unroll = $(AWK) -v N=$(UNROLL) -f $(srctree)/$(src)/unroll.awk \  
           < $< > $@ || ( rm -f $@ && exit 1 )  

The same:
Building modules, stage 2.  
 xargs: illegal option -- r  

To fix it, we need to get rid of the use of the xargs -r flag, which yne supported by BSD version of this tool 
should be corrected in line 63 scripts / Makefile.modpost file at:

 MODLISTCMD := find $(MODVERDIR) -name '*.mod' | grep . | xargs grep -h '\.ko$$' | sort -u  



*Download and build Raspberry Kernel, this is the most painful part for me or anyone who has no idea or forgot the way to build Linux Kernel. It involves not only download and build but also difficult steps to configure your Kernel before building, and also configure, download dependencies, libraries that the build process relies on.

This step is also really important since how your Raspberry works later on depends on this step, if your Raspberry supports or not features, eg: kinda of filesystem, network, etc.. In this step, it even says successful but it's not guaranteed that you can use your Kernel to boot up your Raspberry. The boot up process will actually check configure, hardware, etc.. So if you mis-configure any parts of it aka: drivers, processor, etc. It will be fail during boot up, and you have to analyse the errors, go back and reconfigure your Kernel

It's also recommended to turn on DEBUG mode in your Kernel configuration file so the boot up process later will show you more details of the errors. Painful & Good luck!

  • Download & Install QEMU

It has very clear explanation why you need to install apple-gcc42: "Due to a bug of a white screen hanging QEMU if compiled with llvm one must install the package apple-gcc42 from the homebrew’s dupes repository."

brew install homebrew/dupes/apple-gcc42

Then compile QEMU

brew install qemu —use-gcc

At this point, you reach mostly the end of the procedure, one more step before running QEMU is to make sure your brew got upgraded, updated to latest

$ brew update
$ brew upgrade

In my case, I got errors complaining
dyld: Library not loaded: /usr/local/lib/libpng15.15.dylib
Referenced from: /usr/local/bin/php
Reason: image not found

  • Launch QEMU with Kernel Image, and Harddisk Image

Bravo - you have done the most important steps, you now can enjoy your hard work already

qemu-system-arm -M versatilepb -cpu arm1176 -kernel kernel-qemu-3.10.25-wheezy -hda 2012-07-15-wheezy-raspian-minimal.img -append "root=/dev/sda2"

*Please be noticed, if you copy command above and paste it into your console you might have a problem with this character " and it will failed your command

The parameter "root=/dev/sda2" pointing root filesystem to your image (harddisk), in this case it's sda2 (the partition #2 on your sda). For some instances, it might fail to load the root filesystem since the Kernel could not recognize the filesystem and you have to point root to other partition, eg: sda1, sda3, it depends on your disk image

Not syncing: No working init found. Try passing init= option to kernel. See Linux Documentatio/init.txt fot guidance.

You might want to check out this page: https://github.com/torvalds/linux/blob/master/Documentation/init.txt to understand more how init process works in Linux

There is another reason is your disk image is corrupted so that the boot up process couldn't recognize it, you have to use other computer to mount that disk image and fix it. This link will give you the detail explanation http://raspberrypi.stackexchange.com/questions/41965/error-on-boot-no-working-init-found. In sort the command to check and fix your disk image, if it's ext2 filesystem

e2fsck


  • Get your Raspberry up to date


*Before doing update/upgrade your Raspberry emulation, please make sure your QEMU running on MacOSX with internet access. For some cases, your disk image isn't configure for internet access, you might want to see below for how-to

sudo apt-get update
sudo apt-get upgrade





References:



Digital Inspiration Technology Guide

Change the world with your passion