Project

General

Profile

KernelBuild » History » Version 26

Loic Dachary, 10/23/2016 10:55 AM
fix wording (you need => y ou do not need)

1 1 Denis 'GNUtoo' Carikli
h1. KernelBuild
2
3 2 Denis 'GNUtoo' Carikli
h2. Use case
4
5 26 Loic Dachary
Building a kernel aside Replicant is faster to setup and faster to build since you do not need to fetch and use the huge Android build system.
6 2 Denis 'GNUtoo' Carikli
7
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, however if the changes are integrated back into Replicant, they will automatically be built by the Android build system, when building images.
8
9 4 Denis 'GNUtoo' Carikli
h2. Dependencies 
10 1 Denis 'GNUtoo' Carikli
11 4 Denis 'GNUtoo' Carikli
Since you are not compiling any userspace applications, you don't need the Android build system. The Linux Kenrel, and Bootloaders such as uboot can be built without the Android build system.
12
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 1 Denis 'GNUtoo' Carikli
If you use distributions such as Parabola, this will probably not work because the arm-none-eabi-gcc is too recent for many devices kernels.
19
20 4 Denis 'GNUtoo' Carikli
To workaround that you can install Trisquel in a container.
21
This way it will have very few CPU and memory overhead compared to a virtual machine.
22
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 6 Denis 'GNUtoo' Carikli
First download the following example image and its signatures:
29
* http://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/security/4A80EB23.asc
30 5 Denis 'GNUtoo' Carikli
* http://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip.asc
31
* http://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip
32 1 Denis 'GNUtoo' Carikli
33
As usual, verify the signature:
34 6 Denis 'GNUtoo' Carikli
<pre>
35 8 Denis 'GNUtoo' Carikli
$ gpg --import 4A80EB23.asc
36
$ gpg --verify replicant-4.2-crespo.zip.asc
37 6 Denis 'GNUtoo' Carikli
</pre>
38
It should then say something like:
39
<pre>
40
$ gpg --verify replicant-4.2-crespo.zip.asc 
41
gpg: assuming signed data in 'replicant-4.2-crespo.zip'
42
gpg: Signature made Tue 01 Sep 2015 01:31:47 PM CEST using RSA key ID 4A80EB23
43
gpg: Good signature from "Replicant project release key <contact@replicant.us>" [unknown]
44
gpg: WARNING: This key is not certified with a trusted signature!
45
gpg:          There is no indication that the signature belongs to the owner.
46
Primary key fingerprint: E776 092B 052A DC91 FDD1  FD80 16D1 FEEE 4A80 EB23
47
</pre>
48 5 Denis 'GNUtoo' Carikli
49 7 Denis 'GNUtoo' Carikli
Then unpack the zip file:
50
<pre>
51 8 Denis 'GNUtoo' Carikli
$ mkdir replicant-4.2-crespo && cd replicant-4.2-crespo && unzip ../replicant-4.2-crespo.zip
52 7 Denis 'GNUtoo' Carikli
</pre>
53
54 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:
55
<pre>
56
$ file boot.img
57 1 Denis 'GNUtoo' Carikli
boot.img: Android bootimg, kernel (0x30008000), ramdisk (0x31000000), page size: 4096, cmdline (console=ttyFIQ0 no_console_suspend)
58
</pre>
59
Here it says it's an "Android bootimg", so we need the following tools:
60 10 Denis 'GNUtoo' Carikli
* mkbootimg to pack an image 
61
* unbootimg to unpack an image
62
63
Ways to get such tools:
64
* Pre-built by Replicant at http://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/tools/
65
* Build Replicant and use its tools. That defeat the purpose here since we want to avoid building Replicant fully.
66
67
Some android tools were converted to build on GNU/Linux without requiring the Android build system.
68
* In git://git.freesmartphone.org/utilities.git you have adb in android/adb, mkbootimg and unbootimg in android/image-utils
69
70
Some GNU/Linux distributions also have packages for some of the tools.
71 9 Denis 'GNUtoo' Carikli
72 19 Denis 'GNUtoo' Carikli
Extract the ramdisk and the kernel image and parameters from the original boot.img:
73
74 1 Denis 'GNUtoo' Carikli
<pre>
75 19 Denis 'GNUtoo' Carikli
$ unbootimg --kernel kernel.img --ramdisk ramdisk.img -i boot.img 
76 11 Denis 'GNUtoo' Carikli
total image size:   3100672
77
kernel size:        2903532
78
kernel load addr:   0x30008000
79
ramdisk size:       189142
80
ramdisk load addr:  0x31000000
81
2nd boot size:      0
82
2nd boot load addr: 0x30f00000
83
kernel tags addr:   0x30000100
84
page size:          4096
85
board:              `'
86
cmdline:            `console=ttyFIQ0 no_console_suspend'
87 1 Denis 'GNUtoo' Carikli
id:                 bd59d387bf083b0946e25a8f17f1aaef4bcc7412000
88 11 Denis 'GNUtoo' Carikli
</pre>
89 19 Denis 'GNUtoo' Carikli
90 22 Denis 'GNUtoo' Carikli
We also check the kernel image format, since we will build that:
91
<pre>
92
$ file kernel.img 
93
kernel.img: Linux kernel ARM boot executable zImage (little-endian)
94
</pre>
95 11 Denis 'GNUtoo' Carikli
96
h3. Building
97
98 14 Denis 'GNUtoo' Carikli
If you want to be able to run "make menuconfig", install libncurses5-dev:
99
<pre>
100
# apt-get install libncurses5-dev
101
</pre>
102
103 13 Denis 'GNUtoo' Carikli
Download the sources:
104 11 Denis 'GNUtoo' Carikli
<pre>
105 4 Denis 'GNUtoo' Carikli
$ git clone https://git.replicant.us/replicant/kernel_samsung_crespo.git
106 1 Denis 'GNUtoo' Carikli
</pre>
107 13 Denis 'GNUtoo' Carikli
108
Then in each console you build from, do:
109
<pre>
110
export ARCH=arm
111
export CROSS_COMPILE=arm-none-eabi-
112
</pre>
113
114
Configure it for crespo:
115
<pre>
116
$ make crespo_defconfig
117
</pre>
118
119 14 Denis 'GNUtoo' Carikli
If you want to configure it furthurer:
120
<pre>
121
$ make menuconfig
122
</pre>
123
124 13 Denis 'GNUtoo' Carikli
Then build a zImage:
125
<pre>
126
$ make -j4 zImage
127
</pre>
128 11 Denis 'GNUtoo' Carikli
129 15 Denis 'GNUtoo' Carikli
If the compilation succedded, the image is at:
130
<pre>
131
arch/arm/boot/zImage
132
</pre>
133
134
h3. Building Failures
135
136
Many devices 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 way higher code quality standards, but having your patches merged there requires more time.
137
138
As a result, variations in the default kernel configuration for your device can result in build errors.
139
140
Compilation failures can also happen when you use another gcc version, like we do in this guide.
141
This happens frequently if you use a gcc that is more recent than your kernel.
142
The "not very clean code" also increase the probability of it.
143
144 20 Denis 'GNUtoo' Carikli
h3. Repacking
145 21 Denis 'GNUtoo' Carikli
146 23 Denis 'GNUtoo' Carikli
We now create a new boot.img from the parameters and ramdisk.img we extracted from the default boot.img
147
<pre>
148
$ mkbootimg --kernel /path/to/arch/arm/boot/zImage --ramdisk ramdisk.img --cmdline "console=ttyFIQ0 no_console_suspend" --base 0x30000000 --pagesize 4096 -o new-boot.img
149
</pre>
150
151
Then we verify that it matches the default boot.img parameters:
152
<pre>
153
$ unbootimg -i new-boot.img 
154
total image size:   3096576
155
kernel size:        2899584
156
kernel load addr:   0x30008000
157
ramdisk size:       189142
158
ramdisk load addr:  0x31000000
159
2nd boot size:      0
160
2nd boot load addr: 0x30f00000
161
kernel tags addr:   0x30000100
162
page size:          4096
163
board:              `'
164
cmdline:            `console=ttyFIQ0 no_console_suspend'
165
id:                 f33faefb7b1eca7d1d1d6dc7603aed2bd82d65c000
166
</pre>
167
168
Here we check if the following parameters match:
169
* kernel load addr
170
* ramdisk load addr
171
* Kernel tag addr
172
* page size
173
* cmdline if you don't plan to change it.
174
175 1 Denis 'GNUtoo' Carikli
h3. Testing
176 24 Denis 'GNUtoo' Carikli
177
Reboot the device to the bootloader, and run:
178
<pre>
179
$ fastboot boot new-boot.img
180 25 Denis 'GNUtoo' Carikli
< waiting for device >
181
downloading 'boot.img'...
182
OKAY [  0.435s]
183
booting...
184
OKAY [  0.288s]
185
finished. total time: 0.723s
186 24 Denis 'GNUtoo' Carikli
</pre>