Monday, July 20, 2015

Creating a custom Linux Image for the Raspberry Pi











Creating a custom Linux distribution for the Raspberry Pi is very similar to creating a custom Linux distribution for the RIoTboard.  A new meta-layer is created and customized. The Yocto project already has support for the Raspberry Pi.  There is a hardware specific BSP overlay layer in the Yocto project called meta-raspberrypi.  The layer contains machine specific tunings, compile time parameters, kernel configuration options, and boot loader parameters that are specific to the Raspberry Pi hardware.  Other items such as the GPU/CPU memory split, ARM and GPU processor frequencies, and SDRAM frequency are also set in the pre-existing bsp layer.

This guide provides simple directions for creating an operating system image (U-Boot, GNU/Linux kernel, and root file system) for the Raspberry Pi.  The operating system image will contain native ARM cross compilers, a full development environment, and kernel source code.  These directions will work for the Raspberry Pi 2, B+, B, and A+.

The operating system image will be called specialpi.

Note: The Yocto Project will be releasing version 1.9 in October at which time, several changes will be incorporated which fix the current kernel defconfig issue with the meta-raspberrypi layer. In the mean time, the best option for customizing the raspberry pi kernel is building it in the Linux tree, or out of the Yocto tree.  By default, the meta-raspberrypi layer pulls the broadcom defconfig from the applicable kernel branch and uses that for the configuration.

A 64-bit Fedora 21 GNU/Linux host will be used to compile all of the source components.  Here is what uname shows on my development host:
Fedora release 21 for x86_64 GNU/Linux kernel 4.0.7-200.fc21.x86_64 #1 SMP

This guide consists of the following steps.

1. Install required Packages for a 64-bit Fedora 21 Host development system
2. Pull the Raspberry Pi layer from the Yocto project
3. Create the specialpi layer and image.
4. Make the specialpi image useful.
5. Create layer.conf in the specialpi layer.
6. Make the specialpi layer visible.
7. Set the type of MACHINE.
8. Set up the local configuration.
9. Execute the build.
10.Burn the build to a bootable media device.
11. Validate the build.

Hardware and software prerequisites

Host Operating System


Fedora release 21 for x86_64
GNU/Linux kernel 4.0.7-200.fc21.x86_64 #1 SMP

Host Hardware


Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
Physical Memory 12 GB
My /home partition is 414 GB and resides on a solid state hard drive.

The Yocto build requires a large amount of space so I recommend keeping a scratch area on your /home partition of at least 100 GB.  You can get away with less space but if you decide to make changes to the source and binaries within the Yocto tree later on down the line, then it is a good idea to have extra space.


1. Install required Packages for a 64-bit Fedora 21 Host development system


from the Yocto Project Mega Manual Revision 1.8

Execute the following commands on the development host.

 host]$ sudo yum install gawk make wget tar bzip2 gzip python unzip perl patch \
        diffutils diffstat git cpp gcc gcc-c++ glibc-devel texinfo chrpath \
        ccache perl-Data-Dumper perl-Text-ParseWords perl-Thread-Queue socat \
        findutils which
 host]$ sudo yum install SDL-devel xterm perl-Thread-Queue
 host]$ sudo yum install make docbook-style-dsssl docbook-style-xsl \
        docbook-dtds docbook-utils fop libxslt dblatex xmlto xsltproc
 host]$ sudo yum install autoconf automake libtool glib2-devel

2. Pull the Raspberry Pi layer from the Yocto project



Execute the following commands on the development host.

host]$ mkdir $HOME/bin
host]$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
host]$ chmod a+x $HOME/bin/repo
host]$ echo "PATH=$PATH:$HOME/bin" >> $HOME/.bashrc
host]$ source .bashrc
host]$ mkdir -p $HOME/src/rpi
host]$ cd $HOME/src/rpi
host]$ git clone git://git.yoctoproject.org/poky -b fido
host]$ cd poky
host]$ git clone git://git.yoctoproject.org/meta-raspberrypi -b fido
host]$ git clone git://git.openembedded.org/meta-openembedded -b fido
host]$ source oe-init-build-env build
host]$ export RPIHOME=$HOME/src/rpi/poky

3. Create the specialpi layer and image


Execute the following commands on the development host.

host]$ cd $RPIHOME
host]$ mkdir -p specialpi/recipes-specialpi/images
host]$ mkdir specialpi/conf
host]$ cd specialpi/recipes-specialpi/images
host]$ cp ../../../meta-raspberrypi/recipes-core/images/rpi-hwup-image.bb specialpi.bb

4. Make the specialpi image useful


Modify meta-specialpi/recipes-specialpi/images/special.bb so that it looks like the following

include recipes-core/images/core-image-minimal.bb

IMAGE_FEATURES += "tools-sdk tools-debug debug-tweaks ssh-server-openssh"

# Include modules in rootfs
IMAGE_INSTALL += " \
 packagegroup-core-buildessential \
 kernel-modules \
 kernel-devsrc \
 "

inherit core-image

IMAGE_ROOTFS_EXTRA_SPACE_append += "+ 3000000"

5. Create layer.conf in the specialpi layer.


Modify meta-specialpi/conf/layer.conf so that it looks like the following

BBPATH .= ":${LAYERDIR}"

# We have a recipes directory containing .bb and .bbappend files, add to BBFILES
BBFILES += "${LAYERDIR}/recipes*/*/*.bb \
            ${LAYERDIR}/recipes*/*/*.bbappend"

BBFILE_COLLECTIONS += "specialpi"
BBFILE_PATTERN_specialpi := "^${LAYERDIR}/"
BBFILE_PRIORITY_specialpi = "7"


6. Make the specialpi layer visible


Modify build/conf/bblayers.conf so that it looks like the following

# LAYER_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
LCONF_VERSION = "6"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
  /home/ns/src/rpi/poky/meta \
  /home/ns/src/rpi/poky/meta-yocto \
  /home/ns/src/rpi/poky/meta-yocto-bsp \
  /home/ns/src/rpi/poky/meta-raspberrypi \
  /home/ns/src/rpi/poky/meta-specialpi \
  "
BBLAYERS_NON_REMOVABLE ?= " \
  /home/ns/src/rpi/poky/meta \
  /home/ns/src/rpi/poky/meta-yocto \
  "


7. Set the type of MACHINE


For Raspberry Pi Model B,set MACHINE in build/conf/local.conf above the MACHINE ?? = "qemux86" line as follows
MACHINE ?= "raspberrypi"

For Raspberry Pi 2, B+, or A+,
set MACHINE in build/conf/local.conf above the MACHINE ?? = "qemux86" line as follows

MACHINE ?= "raspberrypi2"

8. Set up the local configuration

Add the following lines to the end of build/conf/local.conf file

CONF_VERSION = "1"
INHERIT += "archiver"
ARCHIVER_MODE[src] = "original"
COPY_LIC_MANIFEST = "1"
COPY_LIC_DIRS = "1"
SOURCE_MIRROR_URL ?= "file://${TOPDIR}/source-mirror/"
INHERIT += "own-mirrors"
BB_GENERATE_MIRROR_TARBALLS = "1"

9. Execute the build


For Raspberry Pi Model B, insert an SD card into the host.

Execute the following commands on the development host.

host]$ cd $RPIHOME/build
host]$ bitbake specialpi
host]$ cd tmp/deploy/images/raspberrypi
host]$ dd if=specialpi-image-raspberrypi.rpi-sdimg off=/dev/sd<X> bs=1M
host]$ sync

For Raspberry Pi 2, B+, or A+, insert a uSD card into the host.

Execute the following commands on the development host.

host]$ cd $RPIHOME/build
host]$ bitbake specialpi
host]$ cd tmp/deploy/images/raspberrypi
host]$ dd if=rpi-specialpi-image-specialpi.rpi-sdimg of=/dev/sdb bs=1M
host]$ sync

10.Burn the build to a bootable media device


For Raspberry Pi Model B, insert an SD card into the host.
Execute the following commands on the development host.

host]$ cd build/tmp/deploy/images/raspberrypi
host]$ dd if=specialpi-raspberrypi.rpi-sdimg off=/dev/sd<X> bs=1M
host]$ sync

For Raspberry Pi 2 or Model B+, insert a uSD card into the host.

Execute the following commands on the development host.

host]$ cd build/tmp/deploy/images/raspberrypi
host]$ dd if=specialpi-raspberrypi.rpi-sdimg of=/dev/sdb bs=1M
host]$ sync


11. Validate the build


Insert the SD card or uSD card in to the Raspberry Pi and validate that the Raspberry Pi properly boots the image on the SD or uSD card.

You should get a login prompt that says
Poky (Yocto Project Reference Distro) 1.8 raspberrypi /dev/ttyAMA0

The username is root. There is no password.
Kernel sources are in /usr/src/kernel
gcc is in /usr/bin

Increase IMAGE_ROOTFS_EXTRA_SPACE_append as needed in special.bb


No comments:

Post a Comment