Compiling the raspberry pie kernel

Source: Internet
Author: User
Tags gz file git clone

Author: huaqing visionary lecturer

In the raspberry system, there are a lot of ready-made drivers and resources, although this is far from suitable for most of the design use, so in the use of certain devices, you need to write or modify some of the drivers, to apply to the device you want to control.

1. Development environment and preparation

(1) My development environment is Ubuntu 14.04.

(2) Link to Raspberry Pie query system version

Command: Uname-a

Description: A lot of ways, you can only connect to the Raspberry Pi desktop system in the terminal input This command query, you can also use SSH remote login to the Raspberry query in Windows environment, can also be remote landing under the Linux environment Raspberry pie to query. I chose the last one, because my Linux environment is a true machine installation.

Install the Putty under Linux:

Command: sudo apt-get install putty

Once the installation is complete, it can be used directly.

Execute command: sudo putty

The following figure:


As shown in the image above, the interface is exactly the same as the Windows environment, so you can log in, and finally query the version of the raspberry system you are using, as follows:


As shown above, my raspber pi system is a Linux raspberrypi 4.1.12-v7+ version. This is important and will determine the version of the kernel that needs to be compiled.

2. Get kernel source, cross-compile tool chain and firmware

(1) Obtain Raspber PI system kernel source code

The raspberry faction has put most of the resources related to the raspberry pie on the GitHub, so just github the corresponding plate to find it, as shown in the following figure:

The link address is: Https://github.com/raspberrypi

So directly in the Linux column can find the relevant kernel source code.

Link Address: https://github.com/raspberrypi/linux/tree/rpi-4.1.y

The following figure:

As above, that is the 4.1 version of the kernel source code, if the other version can be Drop-down box to choose their own. to clone it:

Command: Git clone--depth=1 https://github.com/raspberrypi/linux/

If the network is unstable or slow, directly select Download Zip download package can be downloaded successfully after the decompression. Because Git does not support breakpoint downloads.

But do not know what the problem, I directly downloaded the zip compressed file has problems, can not compile through, so can only use the cloning method.

(2) Get the cross-compilation tool chain

The same Raspberrypi plate in GitHub also provides tools tool resources, as shown in the following figure:

Link Address: https://github.com/raspberrypi/tools/tree/master/arm-bcm2708

As shown above, the path of the tools on the plate can be seen find the corresponding cross-compilation tool chain under the path, and know that the Raspberry faction provides 3 kinds of cross compilation tool chain (the bottom is the same tool chain, only for the 32bit system and 64bit system to make a distinction), first cloned it.

Command: Git clone https://github.com/raspberrypi/tools

(3) Get firmware

The same is the GitHub Raspberripi plate obtained, as shown in the following figure:

As shown above, that is, the corresponding firmware, directly cloned down.

Command: Git clone https://github.com/raspberrypi/firmware

3. Get the configuration file for the current raspberry system

It is important to note that before the 3.x version of the Raspberrypi system, the Raspberry system defaults to the/proc/config.gz file, but after updating to the 4.x version,/proc/ The config.gz file does not exist by default, so we need to get this file first.

Execute command: sudo modprobe configs

After the above command is executed, the config.gz file is generated in the/proc/directory. Then copy this file from the raspberry system to our PC platform (there are many ways, such as U disk, NFS system file sharing, Tcp,samba, etc.).

Then execute the command in the top-level directory of the kernel that needs to be compiled:

Command: zcat config.gz > Config

The purpose is to generate a. config profile

4. Pre-compilation preparation

(1) Determine the compiler version within the Raspberry pie system.

Use command cat/proc/version on Putty to query the version number of the raspberry system compiler. The following figure:

As you know, the GCC compiler has a version number of version 4.8.3, and the update time is March 3, 2014.

(2) Select the version of the compilation tool chain, because there are three versions of the cross compilation tool chain to download, you should select one, but given the version mismatch (if the compiler version is different from the original system version, there may be some inexplicable problems, especially when developing the drive). The versions of each compiler are viewed below.

Arm-bcm2708hardfp-linux-gnueabi compiler: Switch to the tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/directory,

Use command:./arm-bcm2708hardfp-linux-gnueabi-gcc–v

As shown in the figure above, this compiler is version 4.7.1 and can also see the time of the update. However, the version is lower than the version of the existing system.

Arm-bcm2708-linux-gnueabi compiler: Switch to the tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/directory,

Use command:./arm-bcm2708-linux-gnueabi-gcc–v

As the figure above shows, this compiler is also version 4.7.1, except that the update time is different.

Gcc-linaro-arm-linux-gnueabihf-raspbian compiler: Switch to tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/ Table of Contents,

Use command:./arm-linux-gnueabihf-gcc–v

As shown in the figure above, this compiles it to version 4.8.3, and the update time is 20140106. and applies to the 32bitPC platform.

Then look at the gcc-linaro-arm-linux-gnueabihf-raspbian-x64 compiler: Switch to tools/arm-bcm2708/ gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/directory, using the command:./arm-linux-gnueabihf-gcc–v

As pictured above, this version of the compiler and the version of the compiler forcing the raspberry system is the same version, so it should be used for compiling. But this is the 64bit PC platform compiler, if it is a 32-platform computer, use the Gcc-linaro-arm-linux-gnueabihf-raspbian compiler. My current environment is 32bit environment, and then log the text before, I have used 64bit PC platform compiled! No problem.

(3) Install the compilation tool chain

I have not installed the Gcc-linaro-arm-linux-gnueabihf-raspbian compilation tool chain in my environment here, because I have installed other versions of the tools used to develop other products. In order not to cause conflict, I do not do this set of tool chain installation, but directly select a simple violence, the tool chain to add to the makefile.

Switch to the top-level directory of the compiled kernel and open the makefile file in the directory, and modify the following image:

Yes, I added the cross-compilation tool chain directly to the absolute path, but in order to be able to restore, so I backed up the original. This is equivalent to installing the compiler in the environment, but this is simpler and more brutal.

5. Start compiling

(1) Execute command: Make Bcm2835_defconfig

Here, for example, bcm2835_defconfig, mainly generates a. config profile. You can choose the appropriate, or copy, of the raspberry system, which also mentions the file that gets the configuration of the raspberry pie, which is omitted if you use the. config file from the raspberry pie.

(2) Executive order: KERNEL=KERNEL7

(3) Execute command: Make Zimage modules Dtbs

This sentence is really compiled, and so on! If the computer to meet the requirements, can be made after the addition of-j2,-j4,-j6,-j8 and so on, indicating how many threads to compile, you can improve the speed of compilation, waiting for success. After the compilation succeeds, the Zimage mirror file is generated under the path arch/arm/boot/.

Zimage is the compressed kernel image file, which is an executable mirror file, but for the construction of the raspberry pie system, this is not what we ultimately want, so we need to convert it to a kernel.img file.

(4) Install the compiled module driver

Execute command: mkdir modules

Execute command: sudo make install_mod_path=modules modules_install

Modules in the top-level directory of the source code will generate the associated drive library.

(5) Mirroring conversion

Kernel.img mirroring is a tool to convert zimage! and tools exist in the toolkit that is downloaded in the previous article. Tool name Mkimage, switch to tool directory (depending on the directory path you have stored, find the tool), the following figure:

Then execute the command:./imagetool-uncompressed.py. /.. /.. /kernel/linux/arch/arm/boot/zimage

Note: The execution of the command, depending on its own storage path, note the path can be.

After the command is successfully executed, the Kernel.img file is generated in the current directory, as shown in the following figure:

The kernel.img file as shown above is the desired image file for the desired raspberry kernel.

6. Backup firmware

(1) Create a backup directory

Command: mkdir Backup

(2) Backup

When the SD card is inserted, it is immediately recognized, the SD card device can be mounted to a directory for access, and can be accessed directly. The default is in the/media/wrw/directory (the Madia directory is a subdirectory under the root directory under the PC platform, WRW for the subdirectory of the currently logged in name = Word will see two directories, which is the two partitions of the SD card (the partitions of the FAT32 attribute and the EXT4 attribute), as shown in the following figure:

As the above figure shows, the boot directory is the directory of the FAT32 partition, and the other is the EXT4 partition directory. And we want to back up the boot directory, because the directory of the EXT4 partition is actually as a raspberry pie to run again yes with the file system.

Execute command: cd/media/wrw/boot/switch to directory,

Execute command: sudo cp *.elf *.bin ~/reapberry_pi/kernel/backup/

Execute command: LS ~/reapberry_pi/kernel/backup/query whether backup is successful,

Execute command: sudo rm *.elf *.bin

Because kernel can be set in the/boot/config.txt, you can not copy it out first.

7. Upgrade RPI's kernel, firmware and Lib

(1) Modify the name of the kernel.img file that was generated earlier.

Since I packaged the compiled zimage kernel image, I packaged it directly into the kernel.img name, and now rename it to distinguish it from the kernel name of the SD card, as shown in the following figure:

As pictured above, it is renamed Kernel_new.img.

(2) Copy the new kernel image to the FAT32 partition of the SD card, where it is copied to the partition named boot.

Execute command: sudo cp kernel_new.img/media/wrw/boot/

(3) Modify the configuration file of the raspberry pie system.

In the SD card FAT32 partition, there is a file named Config.txt.

Execute command in/media/wrw/boot directory: sudo vim config.txt

The following figure:

Add the line "kernel=kernel_new.img" to the front of the Config.txt file, and if so, modify it. This way the system will boot to the kernel_new.img mirror we just copied, or delete the kernel.img in the directory directly from the command RM and rename kernel_new.img to Kernel.img.

(4). Unzip the previously downloaded firmware firmware compression pack

Execute command: Unzip Firmware-master.zip

Execute command: CD firmware-master/

boot/

(5) Upgrade firmware firmware

Execute command: sudo cp bootcode.bin fixup.dat fixup_cd.dat start.elf/media/wrw/boot/

Execute command: CD. /hardfp/opt/

Now we're going to start with the second partition, the XT4 partition, and I'll call it "13d368bf-6dbf-4751-8ba1-88bed06bef77", but the name is not the most important one.

Execute command: sudo cp-r vc//media/wrw/13d368bf-6dbf-4751-8ba1-88bed06bef77/opt/

The meaning of the command is to copy the VC Library of the new firmware to the OPT subdirectory under the root file system of the raspberry system, and note that if you are using a Arm-bcm2708-linux-gnueabi compiler, a non hard floating point, then in the copy VC, Should copy is the firmware-master/opt/directory of VC.

(6) Upgrade Module drive Lib.

Switch to the previous module directory created in the Linux kernel source directory.

Execute command: CD. /.. /.. /kernel/linux/modules/This is my path and can be adjusted according to my own path.

Copy the Lib directory under the modules directory to the Raspberry root file system,

Execute command: sudo cp-r lib//media/wrw/13d368bf-6dbf-4751-8ba1-88bed06bef77/

8. Enjoy the new kernel

Plug the SD card back into the raspberry pie! You can see that it's starting up normally. Then on behalf of this raspberry pie raspberrypi operating system kernel source code successfully compiled. This way, the back is free to add a variety of drivers. Article from Huaqing Vision Embedded College: http://www.embedu.org/
>>> more excellent Technical blog daily updates

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.