RPi Kernel Compilation

Source: Internet
Author: User
Tags arch linux

Overview

This page explains how to rebuild the kernel image for the RPi. There are two possible routes available:

Both of these routes are covered below, however, you are stronugly recommended to follow the cross compilation route. The low processing power of the RPi means that a local compile will take too hours.

Example Checklist/Roadmap

This section serves to hold a new user's hand just a bit more than some of the other more generic information below in the document. to get more information on the steps in the roadmap, search this page for additional details. it assumes you can navigate filesystems, move files into ss systems, and have a general understanding of compiling linux kernels, filesystems, partitions, and block devices.

This series of steps yielded a successful custom/updated hardfp kernel to a stock Raspbian installation, cross compiled from an amd64 Debian system without regression on any kernel configuration options or requiring modififiboot parameters. be aware that in the worst case, you may need to overlay a stock set of kernel/modules/firmware on the Raspberry Pi if something fails. if you do not know how to do this, then a reimage of the SD card may be necessary. assuming this is not an issue for your configuration, continue onward:

The Raspberry Pi shocould now boot with the newly configured/recompiled kernel.

Get the kernel source

The kernel source shoshould be downloaded from the RPI linux section on GitHub. although you coshould just compile the vanilla kernel from Kernel.org, it will not have the necessary drivers and modules for the Broadcom SoC on the RPi. you can however apply patches from the vanilla kernel to the RPi one-be prepared for potential compiler grumbles though!

Jan '14 the current is rpi-3.10.y, you can check this and other available versions by browsing RPI linux section on GitHub

You can download the source directly using git. For the 3.10 branch:

git initgit clone --depth 1 git://github.com/raspberrypi/linux.git

And for the other Stable Code branch change the numbers in the following to suit:

git initgit fetch git://github.com/raspberrypi/linux.git rpi-3.6.y:refs/remotes/origin/rpi-3.6.ygit checkout rpi-3.6.y

Or you can download a tarball from the same website:

Get a compiler

Next, you will need to get a version of GCC in order to build the kernel.

1. On the RPiRaspbian and PiBang
apt-get updateapt-get -y dist-upgradeapt-get -y install gcc make bc
Arch Linux
pacman -Syupacman -S gcc make
OpenSuSE Linux

Detailed OpenSuSE RPI 12.3 Image 20130407 + 3.8.8 kernel hack tutorial witten (22042013 updated) see: http://www.raspberrypi.org/phpBB3/viewtopic.php? F = 87 & t = 40664 & p = 331953 # p331953 Kernel Compile takes ~ 22 H on RPI Model B due massive module compiles, Include all IP_VS, ARPD, Fuse-zfs, Zram and more :-)

This works as well for Debian, Fedora remix and others (just package Install command differ)

zypper install u-boot-tools sudo gcc automake autoconf bison gettext flex libncurses5 ncurses-devel
cd /usr/srcmkdir GIT; cd GIT; git init; D=`date +"%m-%d-%Y"`git fetch git://github.com/raspberrypi/linux.git rpi-3.8.y:refs/remotes/origin/rpi-3.8.ygit checkout rpi-3.8.ytar cpf rpi-3.8.y.$D.tar   rpi-3.8.ycd /usr/srctar xpf  GIT/rpi-3.8.y.$D.tarrm linuxln -s linux-rpi-3.8.y linuxcd /usr/src/linuxkversion=$(make -s kernelrelease)cp linux/.config .config_$kversioncd /usr/src/# get config-3.8.7.ipvs+krb5+arpd.tar.bz2 from the Tutorial:wget http://www.raspberrypi.org/phpBB3/download/file.php?id=3174# copy the .config to /usr/src/linux: tar xpfj config-3.8.7.ipvs+krb5+arpd.tar.bz2#make the Kernel and go sleep :-)cd linuxmake oldconfignohup make zImage dep modules &#Next day .. Install It.cd /usr/src/linuxkversion=$(make -s kernelrelease)echo $kversionmkdir -p  /boot/$kversionmake ARCH=arm INSTALL_PATH=/boot/ installcp System.map /boot/System.map-$kversioncp System.map-$kversion /boot/System.mapmake ARCH=arm modules_install INSTALL_MOD_PATH=/make ARCH=arm INSTALL_PATH=/boot/ zinstallcp .config /boot/config-$kversioncp ./Module.symvers  /boot/symvers-$kversioncp arch/arm/boot/Image /boot/kernel.img
2. Cross compiling from Linux

Please note that when cross-compiling, your compiler may not target the correct ARM processor by default. this will at best reduce performance, or worse, compile for a much newer processor resulting in illegal instructions in your code. the pre-built compiler or a custom-built compiler are recommended because of this. (For example, the latest GCC Linaro binary targets armv7-a by default, whereas the RPi requires armv6kz ). it is possible to add extra compiler options toHOSTCFLAGSLine inMakefile. The correct flags are shown on thesoftware page-note that you may also need to add-MarmIf your compiler produces Thumb code by default.

Use the provided compiler

Download the pre-built bmc2708 compiler from the RPI tools section on GitHub.

git clone git://github.com/raspberrypi/tools.git

Or you can download a tarball from the website using this link.

Custom-built Linaro GCC

See Linaro GCC Compilation.

Ubuntu
apt-get install gcc-arm-linux-gnueabi make ncurses-dev
Gentoo Linux
crossdev -S -v -t arm-unknown-linux-gnueabi

Crossdev shocould create a cross-toolchain using the latest stable versions of the required packages. if it fails, you can specify exact versions by removing the "-S" flag and adding the "-- B", "-- g ", "-- k" and "-- l" flags. on,Cross-S-v-A gnueabi armWorks just fine.

Arch Linux
yaourt -S arm-linux-gnueabi-gcc
Cross compiling from OSXMacports

The Kernel source requires a case-sensitive filesystem. if you do not have a HFS + Case-sensitive partition that can be used, create a disk image with the appropriate format. ensure latest Xcode and command line tools are installed from Apple Developer Connection Install macports

port install arm-none-eabi-gccport install arm-none-eabi-binutils

If you get an error message that elf. h is missing

sudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelf

From opensource.apple.com, download and copy elf. h and elftypes. h to/usr/include

Edit elf. h and add

#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

If you get a "SEGMENT_SIZE is undeclared" errorOpen the Makefile and change the line:

NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

To

NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) -Dlinux

Complete scriptRequires raspberrypi. config to be in the same folder that you execute from

sudo port install arm-none-eabi-gccsudo port install arm-none-eabi-binutilssudo port install libelf && sudo ln -s /opt/local/include/libelf /usr/include/libelfsudo curl http://opensource.apple.com/source/dtrace/dtrace-48/sys/elftypes.h?txt -o  /usr/include/elftypes.hsudo curl http://opensource.apple.com/source/dtrace/dtrace-48/sys/elf.h?txt -o /usr/include/elf.h#code to append to elf.hecho "#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" > elf-append.hsudo -s 'cat elf-append.h >> /usr/include/elf.h'#Make a case sensitive 3gb disk image, raspberrypi-kernel, and attach ithdiutil create -size 10g -type SPARSEBUNDLE -nospotlight -volname raspberrypi-kernel -fs "Case-sensitive Journaled HFS+" -attach ./raspberrypi-kernel.dmgcp raspberrypi.config /Volumes/raspberrypi-kernel/mkdir /Volumes/raspberrypi-kernel/srccd /Volumes/raspberrypi-kernel/src#get source, either 1. from zip (faster), or 2. from git#1. from zipcurl https://codeload.github.com/raspberrypi/linux/zip/rpi-3.6.y -o ./rpi-3.6.y.zipunzip rpi-3.6.y.zip#2. from git (disabled)#git init#git fetch git://github.com/raspberrypi/linux.git rpi-3.6.y:refs/remotes/origin/rpi-3.6.y#git checkout rpi-3.6.ycpu=$(sysctl hw.ncpu | awk '{print $2}')cpup1=$((cpu+1))cd /Volumes/raspberrypi-kernel/src/linux-rpi-3.6.y/export CCPREFIX=/opt/local/bin/arm-none-eabi-make mrpropercp /Volumes/raspberrypi-kernel/raspberrypi.config .config#answer yes to all config options#yes "" | make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfigmake ARCH=arm CROSS_COMPILE=${CCPREFIX} -j$cpup1#make ARCH=arm CROSS_COMPILE=${CCPREFIX} modules -j$cpup1
Yagarto

Download and install from here.

Perform the compilation

Firstly, ensure your build directory is clean:

make mrproper

Next, in all cases, you will want to get a working kernel configuration to start from. You can get the one running on the RPi by typing the following (on the RPi ):

zcat /proc/config.gz > .config

Then copy. ConfigInto your build directory.

Alternatively, the default configuration is available in the downloaded kernel source inArch/arm/configs/bcmrpi_defconfig. Just copy this. ConfigIn the build directory.

From this point on, if you are cross-compiling, set an environment variableCCPREFIXThat points to the prefix of your compiler binary as each compiler will be named slightly differently.

export CCPREFIX=/path/to/your/compiler/binary/prefix-of-binary-

If you are building on the RPi, removeARCH = arm CROSS_COMPILE =$ {CCPREFIX}From each command.

Ensure that your configuration file is up-to-date:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig

If any configuration options have been added, you will be asked what set each option to. If you don't know the answer, just press enter to accept the default.

Optionally, if you want to make changes to the configuration, run this next:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} menuconfig

Now you are ready to build:

make ARCH=arm CROSS_COMPILE=${CCPREFIX}

If you are on a multi-core system, you can make the build faster by appending-J <N>Where<N>Is the number of cores on your system plus one (I. e.-j3 for 2 cores ).

Find something else to get on with while the compilation takes place. On an average PC with the default configuration, this shocould take about 15 minutes.

The modules will be built with the following command.

make ARCH=arm CROSS_COMPILE=${CCPREFIX} modules
Transfer the build

The fully built kernel will beArch/arm/boot/Image. Copy your new kernel file into the RPi boot partition, though preferably as a new file (suchKernel_new.img) Just in case it doesn't work. If you're building on the RPi, just copy the file/Boot. If you use a different filename, editConfig.txtChange the kernel line:

kernel=kernel_new.img#kernel=kernel.img

Now you need to transfer the modules. Set an environment variable that points to a temporary module path.

export MODULES_TEMP=~/modules

In the build directory, run the following command:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=${MODULES_TEMP} modules_install

The contents of this directory, a single

lib

Directory, shoshould then be copied into the RPi root directory, merging or overwriting

/lib

. NOTE: If you have rebuilt the new kernel with exactly the same version as the one that's running, you'll need to remove the old modules first. ideally this shoshould be done offline by mounting the SD card on another system.

Your RPi shocould now be ready to boot the new kernel. However, at this point it's recommended that you update your GPU firmware and libraries.This is required if you 've just moved from 3.2 to 3.6 as the firmware interface has changed.

Get the firmware

The firmware and boot files shocould be updated at the same time to ensure that your new kernel works properly. Again, two branches are available:

  • Master-This is the version of firmware currently used in Raspbian (I. e. it works with the 3.2 kernel ).
  • Next-This is a development branch which provides a newer GPU firmware to work with the updated drivers in the 3.6 kernel.

You can either download the source directly using git: You can download the firmware directly using git. For the master branch:

git clone git://github.com/raspberrypi/firmware.git

And for the next branch:

git fetch git://github.com/raspberrypi/firmware.git next:refs/remotes/origin/next

Or you can download a tarball from the website using these links: master next

Transfer the firmware

Firstly, update the required boot files in the RPiBootDirectory with those you 've downloaded. These are:

  • Bootcode. bin
  • Fixup. dat
  • Start. elf

Next, you need to copy the VC libraries over. There are two copies of this: one for hard float and one for soft float. To find the correct one, run the following command:

${CCPREFIX}gcc -v 2>&1 | grep hard

If something prints out, and you can see-- With-float = hard, You need the hard float ones. NOTE: The current version of Raspbian uses hard float.

Remove/Opt/vcDirectory from the RPi root, then:

  • For hard float, copyVcFromHardfp/optDirectory/OptIn the RPi root directory
  • Otherwise copyVcFrom the top-levelOptDirectory/OptIn the RPi root directory.
Test your build

Power cycle your RPi and check the following:

  • If you have the serial port on the GPIO expander wired up, you shoshould see the kernel booting.
  • The screen works-the kernel boots and you get a login prompt.
  • The VC interface is working-if the 'OK' LED flashes regularly eight or so times every few seconds once the OS has booted, it's not. You can also test this by runningVcgencmd measure_temp. If it prints "VCHI initialization failed", you have the mismatch between the firmware, the VC libraries, and the kernel driver.
  • RunUname-And check that your new kernel is the one that's running.
  • Make sure you don't have any odd error messages during boot that may indicate a module isn' t working properly. If you seeMissed completion of cmd 18Regarding DMA transfers to the SD card, you can safely ignore it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.