Raspberry pi linux kernel building

G-UK / Raspberry Pi 4 Arm64 Kernel Cross-Compile.md

The Raspberry Pi foundation have now released a beta version of an official 64-bit Kernel which can be installed using the rpi-update script. The rpi-update script can be found at https://github.com/Hexxeh/rpi-update/blob/master/rpi-update or through the Raspbian repositories

The objective of these instructions is to build a stock 64bit Linux Kernel for use on the Raspberry Pi 4B on a Debian x64 machine (Windows Subsystem for Linux in my case), and deploy on the Raspberry Pi.

  • Transfer to Pi is using my NAS in this example, replace with shared drive/memory stick etc. as required.
    • (N: drive on Windows and /mnt/NAS on Linux in this example).
  • For a specific Kernel version replace the 4.19 with the wanted version in the git clone command.
    • Greater than 3GB RAM only currently available on Kernel 4.19
      • Workaround: Add «total_mem=3072» to config.txt when using other kernel versions with the 4GB Pi4
    • FKMS 3D driver fixes only currently available on Kernel 4.19.
      • 3D Acceleration does not work, falls back to software.

On your Debian build machine

Create a build directory.

Clone the official Raspberry Pi repository and pre-built binaries (Ensuring to remove the pre-built 32bit Kernel)

Load the stock Raspbeery Pi 4 configuration.

Update Config using nconfig if required (Skip for stock Pi4 config)

Build the Kernel with optimisations for the Pi4 Arm Cortex A72 CPU.

Now to copy our new Kernel off the build machine and tidy up your build folder.

Источник

Raspberry Pi/Kernel Compilation

The Raspberry Pi cannot run a vanilla Linux kernel. A patched version of the kernel is maintained by the Raspberry Pi Foundation and is available from their GitHub page.

Contents

Prerequisites

To compile a kernel, you require dev-vcs/git to download the source code and also (optional) genkernel to manage the build process.

Get the kernel source

Compile and install the kernel with genkernel

Using genkernel can build a Linux kernel with support for many different features. Follow one of the examples below that has the features that you require.

Default kernel

In this example the configuration options from the running kernel are used to compile the new kernel.

After the kernel has compiled it will be installed into the /boot folder.

Kernel with initramfs

This example will run menuconfig before compiling the kernel, allowing you to enable any extra modules you may need. Using a kernel with an initramfs allows you to load modules, decrypt partitions and other more complex task that maybe require early in the boot process.

To support initramfs the following options need to be enabled in menuconfig.

After the kernel has compiled it and the initramfs be installed into the /boot folder, you need to add it to bootloader (skip to Adding New Kernel to Bootloader)

Compile and install the kernel without genkernel

The first time configuring the kernel sources, create a default .config file (for Raspberry Pi2 use bcm2709_defconfig):

After that, modify this default configuration (a good idea is to add .config support):

And then try to compile/install it:

For now, to make work WIFI, you need also to download firmware:

Adding New Kernel to Bootloader

By default the Raspberry Pi looks for a kernel in /boot/kernel.img . This is changed in the configuration file /boot/config.txt to load the new kernel.

If using an initramfs you also need to add that to the config.txt .

Now the Raspberry Pi can be rebooted and should make use of the new kernel. If for some reason the new kernel does not load or gives errors, the kernel entry in the /boot/config.txt can be removed. Then on the next reboot the default kernel.img will be loaded.

Detailed step-by-step guide

If you encounter problems building or deploying the kernel, try following the detailed kernel building guide for clues on resolving the problems. Additionally The Raspberry Pi foundation provides these build guides to assist in Kernel compilation.

Источник

How to compile a kernel module for Raspberry pi?

I’m having trouble compiling a kernel module for a raspberry pi. I want to compile a «hello world» kernel module using the raspberry pi itself.

I am using raspbian wheezy 3.6.11+.

Here is the Makefile I am using:

Here is the source code for hello-1.c:

Here’s what I get when I try to make the project:

I tried creating the build directory at /lib/modules/3.6.11+

I have GNU Make 3.81 and gcc (Debian 4.6.3-14+rpi1) 4.6.3 installed. I also installed the linux source using

sudo apt-get install linux-source

Any ideas on what I might do to get this to compile?

Читайте также:  Приложение windows 10 upgrade

6 Answers 6

When compiling a module the -C parameter should point to the source tree where the kernel was built (don’t clean it up!). If you built it on the pi its likely in a directory under your home directory.

The build directory under /lib/modules/ is a Debian-ism, where a cut-down version of the source tree is provided with just enough context to build modules against. The kernels from the Raspberry Pi Foundation kernels don’t ship with a build directory.

They may be a bit out of date, but raspbian provides a kernel as a Debian-style package, which should include the build directory you could use to build kernel modules against.

Here are the steps I used to build the Hello World kernel module on Raspbian.

Perform sudo rpi-update

See https://github.com/Hexxeh/rpi-update for details on rpi-update . You have to be on the latest firmware and associated kernel to be able to perform the next step.

Install and run rpi-source to install the source code that built the latest kernel that you are running. This will create the correct entry in /lib/modules for the kernel that you are running. Note: you don’t need to be root to run this, however the script will perform certain tasks using sudo and the root password will be requested during the script execution.

Instructions to install rpi-source can be found at https://github.com/notro/rpi-source/wiki

Once those steps are performed you should be able to make the Hello World kernel module.

Источник

Raspberry Pi Kernel Compilation

Contents

Distributions — operating systems and development environments for the Raspberry Pi.

Performance — measures of the Raspberry Pi’s performance.

Programming — programming languages that might be used on the Raspberry Pi.

Important note

The instructions provided below appears to be outdated, are inconsistent and fail for the current version of the reffered repositories. Consider http://www.raspberrypi.org/documentation/linux/kernel/building.md, for which the cross-compilation instructions is confirmed to work. (19 March 2015, tools: 783eb21c, linux: c4ba28133).

The «Re4son kernel-builder» provides a working environment to build the latest kernel packages and can be used as a reference for studying how I cross compile the Re4son-Kernel.

Overview

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

  1. Compile on the Raspberry Pi itself
  2. Cross compile on another Linux system

Both of these routes are covered below, however, you are strongly recommended to follow the cross-compilation route. The low processing power of the Raspberry Pi means that a local compile will take many hours. A compilation of the latest kernel and modules took about 752 minutes (12h30m)!

If you want to compile an upstream kernel, rather than the Raspberry Pi Foundation’s downstream kernel, please see RPi_Upstream_Kernel_Compilation for a few tips.

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 across 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 AMD 64-bit Debian system without regression on any kernel configuration options or requiring modified boot 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:

  1. Get the latest Raspberry Pi kernel source (https://github.com/raspberrypi/linux)
  2. Set an environment variable KERNEL_SRC to point to the location of the source (for example, KERNEL_SRC=/home/me/linux/ )
  3. Get the latest Raspberry Pi compiler (git clone https://github.com/raspberrypi/tools)
  4. Set an environment variable CCPREFIX to point to the location of tools (for example, CCPREFIX=/home/me/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- )
  5. From the kernel clone location, clean the kernel source with «make mrproper»
  6. Pull the /proc/config.gz from the running Raspbian installation
  7. Prime the kernel with the old configuration by running «ARCH=arm CROSS_COMPILE=$ make oldconfig»
  8. Modify the kernel configuration by either modifying the .config file or using «ARCH=arm CROSS_COMPILE=$ make menuconfig»
  9. Build the new kernel by using «ARCH=arm CROSS_COMPILE=$ make»
  10. Set an environment variable, MODULES_TEMP, to point to the location of the source (for example, MODULES_TEMP=/home/me/modules/ )
  11. Set aside the new kernel modules by using «ARCH=arm CROSS_COMPILE=$ INSTALL_MOD_PATH=$ make modules_install»
  12. From the tools clone location, in the mkimage directory, run «./imagetool-uncompressed.py $/arch/arm/boot/zImage»
  13. Move the resulting kernel.img to the Raspberry Pi’s /boot/ directory
  14. Package up the modules into an archive such that at the top level, the structure looks like this:
    • ./firmware
    • ./firmware/brcm
    • ./firmware/edgeport
    • ./firmware/emi26
    • .
    • ./modules
    • ./modules/3.6.11+
    • ./modules/3.6.11+/kernel
    • ./modules/3.6.11+/kernel/lib
    • ./modules/3.6.11+/kernel/fs
    • .
  15. Move the modules archive to the Raspberry Pi and extract them such that the aforementioned firmware and modules directories overwrite /lib/firmware and /lib/modules
  16. Get the latest Raspberry Pi firmware (git://github.com/raspberrypi/firmware.git)
  17. Transfer the following files from the firmware/boot directory to the Raspberry Pi /boot directory:
    • bootcode.bin
    • fixup.dat
    • start.elf
  18. Transfer the firmware/hardfp/opt directory to the Raspberry Pi /opt directory
  19. Reboot the Raspberry Pi

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

Get the kernel source

The kernel source should be downloaded from the Raspberry Pi Linux section on GitHub. Although you could just compile the vanilla kernel from Kernel.org, it will not have the necessary drivers and modules for the Broadcom SoC on the Raspberry Pi. You can however apply patches from the vanilla kernel to the Raspberry Pi one — be prepared for potential compiler grumbles though!

On Jan 2014, the current is rpi-3.10.y. You can check this and other available versions by browsing Raspberry Pi Linux section on GitHub

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

And for the other stable code branch, change the numbers in the following to suit:

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 Raspberry Pi

Arch Linux

Funtoo or Gentoo Linux

openSUSE Linux

Detailed openSUSE Raspberry Pi 12.3 Image 20130407 + 3.8.8 kernel hack tutorial witten (2013-04-22 updated) see: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=87&t=40664&p=331953#p331953 The kernel compile takes about 22 hours on Raspberry Pi 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 the package install command differs):

Raspbian and PiBang

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 Raspberry Pi requires armv6kz). It is possible to add extra compiler options to the HOSTCFLAGS line in Makefile. The correct flags are shown on the software page — note that you may also need to add -marm if your compiler produces Thumb code by default.

Use the provided compiler

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

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

Custom-built Linaro GCC

Ubuntu

Funtoo or Gentoo Linux

Crossdev should 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 2012-05-06, cross -S -v -A gnueabi arm works just fine.

Arch Linux

Cross compiling from OS X

Macports

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 the latest versin of Xcode and command line tools are installed from Apple Developer Connection Install macports

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

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

Edit elf.h and add

If you get a «SEGMENT_SIZE is undeclared» error open the Makefile and change the line:

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

Yagarto

Download and install from here.

Perform the compilation

Firstly, ensure your build directory is clean:

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

Then copy .config into your build directory.

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

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

If you are building on the Raspberry Pi, remove ARCH=arm CROSS_COMPILE=$ from each command.

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

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:

Now you are ready to build:
(On the Raspberry Pi, type 'screen' to open a virtual screen. If you use it you can disconnect from the Raspberry Pi and compile overnight. )

If you are on a multi-core system, you can make the build faster by appending -j where is the number of cores on your system plus one (that is, -j3 for two cores).

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

The modules will be build with the following command.

Transfer the build

The fully built kernel will be arch/arm/boot/Image. Copy your new kernel file into the Raspberry Pi boot partition, though preferably as a new file (such as kernel_new.img) just in case it doesn't work. If you're building on the Raspberry Pi, just copy the file to /boot. If you use a different filename, edit config.txt change the kernel line:

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

In the build directory, run the following command:

The contents of this directory, a single

directory, should then be copied into the Raspberry Pi root directory, merging or overwriting

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 should be done offline by mounting the SD card on another system.

NOTE: The lib directory will have symlinks back to the kernel sources (lib/modules/ /source and lib/modules/ /build). If you have limited space on the SD card and don't intend to compile modules on the Raspberry Pi itself, you will probably want to remove those links before you transfer the lib directory. The size difference can be many hundreds of MB.

Your Raspberry Pi should 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 should 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 (that is, 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:

And for the next branch:

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 Raspberry Pi boot directory with those you've downloaded. These are:

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:

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 the /opt/vc directory from the Raspberry Pi root, then:

  • For hard float, copy vc from the hardfp/opt directory into /opt in the Raspberry Pi root directory
  • Otherwise copy vc from the top-level opt directory into /opt in the Raspberry Pi root directory.

Note: The hard float vs soft float here refers only to the kernel itself, not the functionality it provides. Your applications will still be able to use hard floats. The kernel doesn't use floats anyway, so it is not something to worry about as long as you select the correct vc directory to copy.

Test your build

Power cycle your Raspberry Pi and check the following:

  • If you have the serial port on the GPIO expander wired up, you should 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 running vcgencmd measure_temp. If it prints "VCHI initialization failed", you have the a mismatch between the firmware, the VC libraries, and the kernel driver.
  • Run uname -a 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 see missed completion of cmd 18 regarding DMA transfers to the SD card, you can safely ignore it.

Build modules for the running kernel

You need the kernel sources for the currently running kernel to successfully build kernel modules for the Raspberry Pi. More specifically, only parts of the complete source, the so called kernel headers are needed. There are two ways to arrive at a state from which you can build kernel modules on the Raspberry Pi.
1. is most suitable if you have been using Kernel sources from raspberrypi.org
2. is using only mainline kernel stuff

1. Using rpi-source

rpi-source downloads a matching source for the running kernel. It supports rpi-update kernels and Raspian kernels.

2. Manually

If you have used or want to stick with a vanilla (mainline) kernel, there is a different way

You have been following the RPi_Upstream_Kernel_Compilation guide, correct? Great, let's move on: That guide is not using modules, so enable modules in make menuconfig first. Then execute

The second line will create the .ko files and it will create the necessary folder structure (/usr/src/linux/lib/modules/`uname -r`/[build|kernel|source]. build and source are symlinks to the kernel sources. kernel contains the module files (under some subdirectories).

Now you can continue with RPi_Upstream_Kernel_Compilation compiling the bootloader and so on. Finally also copy the used kernel sources onto the SD card, e.g. under /home/pi/linux

Boot the Raspberry Pi, then execute

to change back ownership to you (this is necessary unless your username on your build machine is also "pi". ). Now copy /home/pi/linux/lib/modules/* to the correct

Источник

Читайте также:  Гаджеты windows 10 где они
Оцените статью