Project

General

Profile

KernelBuild » History » Version 41

Denis 'GNUtoo' Carikli, 11/28/2019 10:59 PM
Follow the developers guidelines for links

1 28 Wolfgang Wiedmeyer
h1. Kernel Build
2 1 Denis 'GNUtoo' Carikli
3 2 Denis 'GNUtoo' Carikli
h2. Use case
4
5 29 Wolfgang Wiedmeyer
Building a kernel aside Replicant is faster to set up and faster to build since you do not need to fetch and use the huge Android build system.
6 2 Denis 'GNUtoo' Carikli
7 30 Wolfgang Wiedmeyer
Users wanting to add a driver to their kernel, or developers that want to work on kernel related areas can do that to speed up the development process. If the changes are integrated back into Replicant, they will automatically be built by the Android build system when building images.
8 2 Denis 'GNUtoo' Carikli
9 4 Denis 'GNUtoo' Carikli
h2. Dependencies 
10 1 Denis 'GNUtoo' Carikli
11 30 Wolfgang Wiedmeyer
Since you are not compiling any user space applications, you don't need the Android build system. The Linux kernel and bootloaders such as U-Boot can be built without the Android build system.
12 4 Denis 'GNUtoo' Carikli
13 12 Denis 'GNUtoo' Carikli
The Trisquel ARM version of gcc seem to work well. To install it run:
14
<pre>
15
$ apt-get install gcc-arm-none-eabi
16
</pre>
17 4 Denis 'GNUtoo' Carikli
18 30 Wolfgang Wiedmeyer
If you use distributions such as Parabola, this will probably not work because the arm-none-eabi-gcc is too recent for many device kernels. But there are efforts to make the kernel sources compatible with more recent compiler versions.
19 1 Denis 'GNUtoo' Carikli
20 30 Wolfgang Wiedmeyer
You can install Trisquel in a container to work around this.
21
This way, it will have very few CPU and memory overhead compared to a virtual machine.
22 4 Denis 'GNUtoo' Carikli
It will also save disk space since you can just store the Trisquel rootfs in any directory.
23
24 10 Denis 'GNUtoo' Carikli
h2. Example with crespo under Trisquel
25 1 Denis 'GNUtoo' Carikli
26 10 Denis 'GNUtoo' Carikli
h3. Getting the right parameters
27
28 31 Wolfgang Wiedmeyer
First download the following example image and its signature:
29 27 Wolfgang Wiedmeyer
* https://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip.asc
30 1 Denis 'GNUtoo' Carikli
* https://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip
31
32 31 Wolfgang Wiedmeyer
As usual, verify the signature after [[ReplicantReleaseKey#Replicant-42-and-below|importing the release key]]:
33 6 Denis 'GNUtoo' Carikli
<pre>
34 31 Wolfgang Wiedmeyer
$ gpg --armor --verify path/to/replicant-4.2-crespo.zip.asc path/to/replicant-4.2-crespo.zip
35 6 Denis 'GNUtoo' Carikli
</pre>
36 31 Wolfgang Wiedmeyer
37
Make sure the check succeeds!
38 5 Denis 'GNUtoo' Carikli
39 7 Denis 'GNUtoo' Carikli
Then unpack the zip file:
40
<pre>
41 8 Denis 'GNUtoo' Carikli
$ mkdir replicant-4.2-crespo && cd replicant-4.2-crespo && unzip ../replicant-4.2-crespo.zip
42 7 Denis 'GNUtoo' Carikli
</pre>
43
44 9 Denis 'GNUtoo' Carikli
That should have extracted a boot.img. We then should not forget to look at what format the boot.img is in:
45
<pre>
46
$ file boot.img
47 1 Denis 'GNUtoo' Carikli
boot.img: Android bootimg, kernel (0x30008000), ramdisk (0x31000000), page size: 4096, cmdline (console=ttyFIQ0 no_console_suspend)
48
</pre>
49
Here it says it's an "Android bootimg", so we need the following tools:
50 33 Wolfgang Wiedmeyer
* @mkbootimg@ to pack an image
51 38 Wolfgang Wiedmeyer
* @unpackbootimg@ to unpack an image
52 32 Wolfgang Wiedmeyer
53 33 Wolfgang Wiedmeyer
54 41 Denis 'GNUtoo' Carikli
First, install mkbootimg and unpackbootimg. The [[ToolsInstallation]] page has some installation instructions for them.
55 38 Wolfgang Wiedmeyer
Then extract the ramdisk, kernel image and parameters from the original @boot.img@:
56 19 Denis 'GNUtoo' Carikli
57 1 Denis 'GNUtoo' Carikli
<pre>
58 34 Wolfgang Wiedmeyer
$ unpackbootimg -i boot.img -o boot 
59
Android magic found at: 0
60
BOARD_KERNEL_CMDLINE console=ttyFIQ0 no_console_suspend
61
BOARD_KERNEL_BASE 30000000
62
BOARD_RAMDISK_OFFSET 01000000
63
BOARD_SECOND_OFFSET 00f00000
64
BOARD_TAGS_OFFSET 00000100
65
BOARD_PAGE_SIZE 4096
66
BOARD_SECOND_SIZE 0
67
BOARD_DT_SIZE 0
68 11 Denis 'GNUtoo' Carikli
</pre>
69 1 Denis 'GNUtoo' Carikli
70 34 Wolfgang Wiedmeyer
This will unpack the boot.img in the directory @boot@.
71
72 22 Denis 'GNUtoo' Carikli
We also check the kernel image format, since we will build that:
73
<pre>
74 34 Wolfgang Wiedmeyer
$ file boot/boot.img-zImage
75
boot/boot.img-zImage: Linux kernel ARM boot executable zImage (little-endian)
76 22 Denis 'GNUtoo' Carikli
</pre>
77 11 Denis 'GNUtoo' Carikli
78
h3. Building
79
80 14 Denis 'GNUtoo' Carikli
If you want to be able to run "make menuconfig", install libncurses5-dev:
81
<pre>
82
# apt-get install libncurses5-dev
83
</pre>
84 1 Denis 'GNUtoo' Carikli
85 13 Denis 'GNUtoo' Carikli
Download the sources:
86 11 Denis 'GNUtoo' Carikli
<pre>
87 4 Denis 'GNUtoo' Carikli
$ git clone https://git.replicant.us/replicant/kernel_samsung_crespo.git
88 1 Denis 'GNUtoo' Carikli
</pre>
89 13 Denis 'GNUtoo' Carikli
90 34 Wolfgang Wiedmeyer
Then in each console you build from, run:
91 13 Denis 'GNUtoo' Carikli
<pre>
92 35 Wolfgang Wiedmeyer
$ export ARCH=arm
93
$ export CROSS_COMPILE=arm-none-eabi-
94 13 Denis 'GNUtoo' Carikli
</pre>
95
96
Configure it for crespo:
97 1 Denis 'GNUtoo' Carikli
<pre>
98
$ make crespo_defconfig
99 13 Denis 'GNUtoo' Carikli
</pre>
100
101 34 Wolfgang Wiedmeyer
If you want to configure it further:
102 14 Denis 'GNUtoo' Carikli
<pre>
103 1 Denis 'GNUtoo' Carikli
$ make menuconfig
104 14 Denis 'GNUtoo' Carikli
</pre>
105
106 13 Denis 'GNUtoo' Carikli
Then build a zImage:
107
<pre>
108
$ make -j4 zImage
109
</pre>
110 11 Denis 'GNUtoo' Carikli
111 34 Wolfgang Wiedmeyer
If the compilation succeeded, the image is at:
112 15 Denis 'GNUtoo' Carikli
<pre>
113
arch/arm/boot/zImage
114
</pre>
115
116 34 Wolfgang Wiedmeyer
h3. Build Failures
117 15 Denis 'GNUtoo' Carikli
118 34 Wolfgang Wiedmeyer
Many device-specific kernels often contains not very clean code. This is very common with high volume devices due to time to market constraints. Upstream Linux has a lot higher code quality standards, but having your patches merged there requires more time.
119 15 Denis 'GNUtoo' Carikli
120
As a result, variations in the default kernel configuration for your device can result in build errors.
121
122 1 Denis 'GNUtoo' Carikli
Compilation failures can also happen when you use another gcc version, like we do in this guide.
123
This happens frequently if you use a gcc that is more recent than your kernel.
124 15 Denis 'GNUtoo' Carikli
The "not very clean" code also increases the probability of it.
125 35 Wolfgang Wiedmeyer
126
If you manage to fix build failures for a kernel, feel free to [[DeveloperGuide#Code-hosting-and-submitting-patches|submit patches]].
127 15 Denis 'GNUtoo' Carikli
128 20 Denis 'GNUtoo' Carikli
h3. Repacking
129 21 Denis 'GNUtoo' Carikli
130 37 Wolfgang Wiedmeyer
We now create a new boot.img from the parameters and ramdisk we extracted from the default boot.img
131 23 Denis 'GNUtoo' Carikli
<pre>
132 37 Wolfgang Wiedmeyer
$ mkbootimg --kernel path/to/arch/arm/boot/zImage --ramdisk boot/boot.img-ramdisk.gz --cmdline "console=ttyFIQ0 no_console_suspend" --base 0x30000000 --pagesize 4096 -o new-boot.img
133 23 Denis 'GNUtoo' Carikli
</pre>
134
135
Then we verify that it matches the default boot.img parameters:
136
<pre>
137 36 Wolfgang Wiedmeyer
$ unpackbootimg -i new-boot.img 
138
Android magic found at: 0
139
BOARD_KERNEL_CMDLINE console=ttyFIQ0 no_console_suspend
140
BOARD_KERNEL_BASE 30000000
141
BOARD_RAMDISK_OFFSET 01000000
142
BOARD_SECOND_OFFSET 00f00000
143
BOARD_TAGS_OFFSET 00000100
144
BOARD_PAGE_SIZE 4096
145
BOARD_SECOND_SIZE 0
146
BOARD_DT_SIZE 0
147 23 Denis 'GNUtoo' Carikli
</pre>
148
149
Here we check if the following parameters match:
150 36 Wolfgang Wiedmeyer
* Kernel base addr (@BOARD_KERNEL_BASE@)
151
* Ramdisk offset (@BOARD_RAMDISK_OFFSET@) 
152
* Kernel tag offset (@BOARD_TAGS_OFFSET@) 
153
* Page size (@BOARD_PAGE_SIZE@)
154
* cmdline if you don't plan to change it (@BOARD_KERNEL_CMDLINE@)
155 23 Denis 'GNUtoo' Carikli
156 1 Denis 'GNUtoo' Carikli
h3. Testing
157 24 Denis 'GNUtoo' Carikli
158
Reboot the device to the bootloader, and run:
159
<pre>
160
$ fastboot boot new-boot.img
161 25 Denis 'GNUtoo' Carikli
< waiting for device >
162
downloading 'boot.img'...
163
OKAY [  0.435s]
164
booting...
165
OKAY [  0.288s]
166
finished. total time: 0.723s
167 24 Denis 'GNUtoo' Carikli
</pre>