+ All Categories
Home > Documents > Building Linux Kernel for Raspberry Pi - wiki.aalto.fi

Building Linux Kernel for Raspberry Pi - wiki.aalto.fi

Date post: 20-Mar-2022
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
20
Building Linux Kernel for Raspberry Pi Hannu Flinck 12.12.2013
Transcript

Building Linux Kernel for Raspberry Pi

Hannu Flinck

12.12.2013

Why to change kernel?

• When to change to new kernel? • Need to have new

• memory management, • scheduling, • interrupt handling, • protocol stacks, • GUI • updated System on Chip (SoC) features.

• Full blown porting is a quite large task: – starting from boot loader, all HW depended code (drivers) – compiling the kernel, preferably in an efficient machine (i.e. not RPi) =>

leads to cross compilation – needs proper tool chain and test environment: e.g QEMU, Yocot in this

case

• Focus on building the Linux kernel for Raspberry Pi – ARM1176JFZ-S processor with BCM2835 System on Chip (SoC)

Linux internals Linux kernel can be divided into

3 levels:

• system call interface (SCI) – located in /linux/kernel and /linux/arch

• architecture-independent level – PM, MM, VFS, NET

• architecture-dependent – In /linux/drivers and /linux/arch (Board

Support Package)

Linux Kernel Makefile structure

• Organized in a hierarchy of five levels: • Makefile the top Makefile in /linux.

• .config the kernel configuration file.

• arch/$(ARCH)/Makefile architecture specific

Makeflies

• scripts/Makefile.* common rules etc. for all kbuild

Makefiles to autogenerate files.

• kbuild Makefiles there are about 500 of these.

• The top Makefile takes .config as an input • builds the kernel recursively

• A successful kernel building process will result into two major products: the resident kernel images (vmlinux, and architecture specific targets) and modules.

Kernel configuration

• Once you have kernel source tree, you need to configure it

• Used ”menuconfig” out of many alternatives

• Creates ./config file

ARM Specific files

• Linux kernel 3.6.11 contains generic ARM code in the following subdirectories: – boot, common, configs, kernel, lib, mm, net, nwfpe, vfp, oprofile and tools

– After compilation both compressed and uncompressed images in boot

• In arch/arm: – machine specific directories (mach-*)

– mach-bcm2708 contains SoC specifics

• For arm architecture a ”machine type” number is defined for each board in arch/arm/tools/machine-types file: – BCM2708 machine number is “3138

– Used in compile time to generate header file, include/asm-arm/mach-types.h.

– boot process to look for the right “machine_desc structure”

Machine structure macro • In /arch/arm/mach-bcmbcm2708/bcm2708.c • Bootloader passes the machine type number in register r1 to the kernel start up code,

arch/arm/kernel/head.S, that is kernel start up code for all 32-bit CPUs. • This code calls __lookup_machine_type in arch/arm/kernel/headcommon to look for the

machine_desc structures. If the requested machine number is found the machine descriptions are known and the boot process continues

MACHINE_START(BCM2708, "BCM2708") /* Maintainer: Broadcom Europe Ltd. */ .map_io = bcm2708_map_io, .init_irq = bcm2708_init_irq, .timer =&bcm2708_timer, .init_machine = bcm2708_init, .init_early = bcm2708_init_early, .reserve = board_reserve, .restart = bcm2708_restart, MACHINE_END

Notes on Kernel Porting

• Kernel.org maintains vanilla Linux kernel

– No drivers nor modules for the Broadcom SoC

• Linux kernel maintained for Raspberry in https://github.com/raspberrypi/linux

• Diff –r /vanilla linux-3.10.20 kernel rpi-3.6.y.tar.gz/linux/kernel:

– 1688 files that are only in linux-3.10.20/arch, but not in the raspberry/linux/arch

– 1600 files only in raspberry/linux/arch and

– 4289 files under /arch that have changes

• problematic parts of the Raspberry Pi code that haven't been upstreamed: the dwc_otg USB driver, and the VCHIQ driver used for talking to the GPU

QEMU

• (Basic compilation skipped, see the article)

• QEMU: a generic open source machine emulator and virtualizer

• It allows to run unmodified operating system in a virtual machine but still uses the host devices.

• It is based on a dynamic translation of the target instruction set to the host instruction set.

• QEMU runs on Linux, Windows and Mac OS X.

QEMU (cont.)

• CPU emulator (x86, x86-64, PowerPC, ARM and Sparc) • Emulated devices (e.g. VGA display, 16450 serial port, PS/2

mouse and keyboard, IDE hard disk, NE2000 network card, ...)

• Generic devices (e.g. block devices, character devices, network devices) used to connect emulated devices to the host devices

• Machine descriptions (e.g. PC, PowerMac, Sun4m) instantiating the emulated devices

• Debugger • User interface • QEMU can be run in two modes either in user space

virtualizer mode or full system emulation mode.

Compiling for QEMU

• RaspberryPi CPU is ARM1176JZF-S (ARMv6k) which is not yet supported in most QEMU distros => need to build QEMU for Rpi

• Cannot emulate Raspberry Pi's General Purpose Input Output (GPIO) facilities and GPU

• Add two compression libraries and simple DirectMedia Layer development library – $sudo apt-get install git zlib1g-dev libsdl1.2-dev

• Then we need to add pixel-manipulation library for X and cairo: – $sudo apt-get install libpixman-1-dev

• clone QEMU – $git clone http://git.qemu.org/git/qemu.git

Compiling for QEMU (cont.)

• Configure: – $./configure --target-list="arm-softmmu arm-linux-user" --enable-sdl

--prefix=/usr

• Add: git submodule update –init dtc – $ make

– $ sudo make install

• Check that arm1176 support is in place: – $ qemu-system-arm -cpu

• Get Raspberry disk image from http://www.raspberrypi.org/downloads • The image needs to be edited to disable dynamic endianness switching • Mount the image with two partitions:

– sudo mount -t vfat -o loop,offset=4194304 ~/qemu_vms/2013-09-25-wheezy-raspbian.img /mnt/img1

– sudo mount -t vfat -o loop,offset=62914560 ~/qemu_vms/2013-09-25-wheezy-raspbian.img /mnt/img2

• Edit: sudo nano /mnt/img2/etc/ld.so.preload • Comment out line “/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so”.

Compiling for QEMU (cont.)

• Get Linux kernel with git clone https://github.com/raspberrypi/linux.git

• Get patch for ARM11(ARMv6) Versatile board: – $ wget http://xecdesign.com/downloads/linux-

qemu/linux-arm.patch

– $ patch -p1 -d linux/ < linux-arm.patch

• We need to add manually the PCI host bridge and LSI53C895A PCI SCSI Host Bus Adapter with hard disk and CD-ROM devices to the emulated Versatile baseboard – $ cd linux

– $ make ARCH=arm versatile_defconfig

– $ make ARCH=arm menuconfig

Compiling for QEMU (cont.)

• Use menuconfig to define Compiler:

• “arm-linux-gnueabihf-”

• Processor, floating point support

• File system, etc..

• Finally to compile go to linux-directory:

– $ make ARCH=arm

– $ make ARCH=arm INSTALL_MOD_PATH=../modules

modules_install

Compiling for QEMU (cont.)

• Start QEMU: $ sudo qemu-system-arm -kernel zImage

-cpu arm1176 -m 256 –M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1" -hda ~/qemu_vms/2013-09-25-wheezy-raspbian.img -redir tcp:5022::22

• To get kernel output dumped to

a file outside the QEMU virtual system for later analysis, add e.g. "-serial file:/tmp/qemu-output.log" to the QEMU command line

Yocto Project

• The Yocto Project* is an open source collaboration project – Provides templates, tools and methods to help you create custom

Linux-based systems for embedded products regardless of hardware architecture.

• Focused resources for system application developers who need to customize a Linux distribution for a device

• Supports multiple architectures (IA, ARM, PowerPC, MIPS, etc) • Enables transition from Proof of Concept (POC) to supported • Proprietary code can be included in build structure within a

separate • layer, which can be kept private. (security) • Project hosted by the Linux Foundation *) www.yoctoproject.org

Source: http://elinux.org/images/9/9a/The_Yocto_Project_Overview_and_Update.pdf

Main Yocto elements

• Poky – distribution of Yocto

• OpenEmbedded – framework to create Linux distributions for embedded

devices

• BitBake – Make like package management build tool for embedded

distributions (used by OpenEmbedded)

• Metalayer – information on how to build packages, e.g. raspberry-meta

• QEMU part of the developer tool set

Installing and Configuring for Raspberry

• $ git clone -b dora git://git.yoctoproject.org/poky.git • $ cd poky • $ git clone -b dylan git://git.yoctoproject.org/meta-raspberrypi • Initialize environment variables and the build directory, use: • $ . oe-init-build-env build script • edit conf/local.conf tom match the compilation environment:

BB_NUMBER_THREADS = "2" PARALLEL_MAKE = "-j 2" MACHINE ?= "raspberrypi" GPU_MEM = "16"

• edit poky/build/conf so that the paths match your locale set up

BBLAYERS ?= " \ /home/hannu/yocto/poky/meta \ /home/hannu/yocto/poky/meta-yocto \ /home/hannu/yocto/poky/meta-yocto-bsp \ /home/hannu/yocto/poky/meta-raspberrypi \

• Build the image: bitbake rpi-basic-image • Can be copied (dd) to SD as such

Summary

• To port a new kernel to Raspberry Pi, one needs to get the SoC drivers of the BCM2708/BCM2835

• Get the cross compilation environment

• powerful testing and development tools ensure positive outcome of the porting

• QEMU

• Even better Yocto Project tools


Recommended