Вы находитесь на странице: 1из 3

Raspberry Pi/2/3 Kernel Build From Sources From Scratch.

Please open terminal on


Ubuntu PC. Before starting the procedure power on your Ubuntu desktop
PC and Pi board.

Warning: You must be work on one terminal in ubuntu if you switch your terminal
you will not be able to compile below commands.
Note: I had done all the given procedure at your Ubuntu PC so for learning purpose
you can delte your sources and tools to test the whole
process from scratch.

Now Open terminal in Ubuntu PC and please be stick with this terminal don't close
it at any stage other wise you will face issues.

1. cd
2. mkdir raspberryPi
3. cd raspberryPi
4. sudo apt-get install libncurses5-dev
5. git clone --depth=1 https://github.com/raspberrypi/linux
6. git clone https://github.com/raspberrypi/tools

Put the following line#8 in your .bashrc file located at ~/.bashrc location but I
already done it on your Ubuntu PC so you have to replace your old line with given
line#7

To open .bashrc file you must give this command.

7. gedit ~/.bashrc
8. export PATH=$PATH:~/raspberryPi/tools/arm-bcm2708/gcc-linaro-arm-linux-
gnueabihf-raspbian-x64/bin

9. source ~/.bashrc
10. cd ~/raspberryPi/linux/
11. KERNEL=kernel7 // For Raspberry Pi1 you must use this "KERNEL=kernel"
12. export CCPREFIX=~/raspberryPi/tools/arm-bcm2708/gcc-linaro-arm-linux-
gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
13. ${CCPREFIX}gcc -v // To check your GCC compiler version

Now we need .config file from running raspberry Pi to get all well for new kernel
build, so we will login on Pi open terminal
and then give this command on raspberry pi terminal.

14. sudo modprobe configs

After this command our config.gz will be created at /proc/config.gz loactaion, now
we will again come on Ubuntu terminal where we left.

SSH host changing


15. scp pi@raspberrypi-ip-address:/proc/config.gz .

If prompted for password then please put your login password for raspberry most
probably "raspberry"

16. make mrproper


17. gunzip -c config.gz > .config
18. make ARCH=arm CROSS_COMPILE=${CCPREFIX} oldconfig

If above command ask you some questions about new kernel modules then please enter
without any answer until you get a command prompt.
19. make ARCH=arm CROSS_COMPILE=${CCPREFIX} zImage modules dtbs -j4

After successfully kernel build process you will get new kernel image in
arch/arm/boot/zImage

Now we will load this new kernel image into raspberry pi, so please shutdown your
raspberry pi and safely remove its SD card and
put it in your Ubuntu system. After this given below command on Ubuntu terminal
where you left.

20. lsblk

If it is a NOOBS SD card you should see something like this:

sdb
sdb1
sdb2
sdb5
sdb6
sdb7

sdb1, sdb2, sdb3 etc will be your SD card partitions With sdb6 being the FAT (boot)
partition, and sdb7 being the ext4 filesystem (root) partition but please make sure
your's may be of different names for FAT (boot) and filesystem (root).
so please use names accordingly.

Now we will mount your SD card on standard location.

21. mkdir /mnt/fat32


22. mkdir /mnt/ext4
23. sudo mount /dev/sdb6 /mnt/fat32 // Again please make sure
/dev/sdb6 if it's boot partition
24. sudo mount /dev/sdb7 /mnt/ext4 // Again please make sure
/dev/sdb7 if it's root partition

Now we will Install modules

25. sudo make ARCH=arm CROSS_COMPILE=${CCPREFIX} INSTALL_MOD_PATH=/mnt/ext4


modules_install

Finally, copy the kernel and Device Tree blobs onto the SD card, making sure to
back up your old kernel:

26. sudo cp /mnt/fat32/$KERNEL.img /mnt/fat32/$KERNEL-backup.img


27. sudo scripts/mkknlimg arch/arm/boot/zImage /mnt/fat32/$KERNEL.img
28. sudo cp arch/arm/boot/dts/*.dtb /mnt/fat32/
29. sudo cp arch/arm/boot/dts/overlays/*.dtb* /mnt/fat32/overlays/
30. sudo cp arch/arm/boot/dts/overlays/README /mnt/fat32/overlays/
31. sudo umount /mnt/fat32
32. sudo umount /mnt/ext4

After successfully execution of above commands you can safely remove your SD card
and put it in your Pi board and boot it and you will surely
get a new compiled kernel. If you successfully boot your Pi with newly build kernel
then you will surely be able to write your own kernel modules and able to load them
directly in your Pi board without any kernel version issues. So now I tell you the
format of Makefile of kernel modules for cross compiling your kernel moudules here
is the format of kernel module Makefile:
obj-m += piencoder.o

all:
make -C ~/raspberryPi/linux ARCH=arm CROSS_COMPILE=~/raspberryPi/tools/arm-
bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
SUBDIRS=$(PWD) modules

clean:
make -C ~/raspberryPi/linux ARCH=arm CROSS_COMPILE=~/raspberryPi/tools/arm-
bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
SUBDIRS=$(PWD) clean

So the above format is for writing Makefile for kernel module say any, copy the
above Makefile format in Makefile file where your kernel module
is placed.

33. make

After make command you will get a piencoder.ko file in your kernel module folder so
to insert this module file you must trasfer it in your Pi board,
to transfer this piencoder.ko file use this command on your Ubuntu PC terminal.

34. scp piencoder.ko pi@raspberrypi-ip-address:~/

Now you can see this piencoder.ko file in your Raspberry Pi home location, to load
this kernel module you can give following commands.

35. cd
36. sudo insmod piencoder.ko

After above command your kernel module will be loaded and you can confirm it by
giving this command.

37. dmesg

Above command will show you the printk message of your Kernel module which makes
sense that your kernel module loaded successfully.

References:

https://www.raspberrypi.org/documentation/linux/kernel/building.md
http://sysprogs.com/VisualKernel/tutorials/raspberry/buildkernel/
http://elinux.org/Raspberry_Pi_Kernel_Compilation#2._Cross_compiling_from_Linux
http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html
https://pihw.wordpress.com/guides/direct-network-connection/

Вам также может понравиться