Project

General

Profile

KernelBuild » History » Version 34

Wolfgang Wiedmeyer, 05/08/2017 08:38 AM
adapt for new unpackbootimg and more wording/typos

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
* @unpackbootimg@ to unpack an image (older versions are called @unbootimg@)
52 10 Denis 'GNUtoo' Carikli
53 1 Denis 'GNUtoo' Carikli
Ways to get such tools:
54 32 Wolfgang Wiedmeyer
* [[ReplicantImages|Pre-built by Replicant]]
55
* Some GNU/Linux distributions also have packages for some of the tools.
56
* Build Replicant and use its tools. This would defeat the purpose of this tutorial since we want to avoid a full build of Replicant.
57 10 Denis 'GNUtoo' Carikli
58 32 Wolfgang Wiedmeyer
Some android tools were converted to build on GNU/Linux without requiring the Android build system:
59 33 Wolfgang Wiedmeyer
* In https://github.com/freesmartphone/utilities, you have the source code for @mkbootimg@ and @unbootimg@ in @android/image-utils@.
60 9 Denis 'GNUtoo' Carikli
61 34 Wolfgang Wiedmeyer
Extract the ramdisk, kernel image and parameters from the original boot.img:
62 19 Denis 'GNUtoo' Carikli
63 1 Denis 'GNUtoo' Carikli
<pre>
64 34 Wolfgang Wiedmeyer
$ unpackbootimg -i boot.img -o boot 
65
Android magic found at: 0
66
BOARD_KERNEL_CMDLINE console=ttyFIQ0 no_console_suspend
67
BOARD_KERNEL_BASE 30000000
68
BOARD_RAMDISK_OFFSET 01000000
69
BOARD_SECOND_OFFSET 00f00000
70
BOARD_TAGS_OFFSET 00000100
71
BOARD_PAGE_SIZE 4096
72
BOARD_SECOND_SIZE 0
73
BOARD_DT_SIZE 0
74 11 Denis 'GNUtoo' Carikli
</pre>
75 1 Denis 'GNUtoo' Carikli
76 34 Wolfgang Wiedmeyer
This will unpack the boot.img in the directory @boot@.
77
78 22 Denis 'GNUtoo' Carikli
We also check the kernel image format, since we will build that:
79
<pre>
80 34 Wolfgang Wiedmeyer
$ file boot/boot.img-zImage
81
boot/boot.img-zImage: Linux kernel ARM boot executable zImage (little-endian)
82 22 Denis 'GNUtoo' Carikli
</pre>
83 11 Denis 'GNUtoo' Carikli
84
h3. Building
85
86 14 Denis 'GNUtoo' Carikli
If you want to be able to run "make menuconfig", install libncurses5-dev:
87
<pre>
88
# apt-get install libncurses5-dev
89
</pre>
90 1 Denis 'GNUtoo' Carikli
91 13 Denis 'GNUtoo' Carikli
Download the sources:
92 11 Denis 'GNUtoo' Carikli
<pre>
93 4 Denis 'GNUtoo' Carikli
$ git clone https://git.replicant.us/replicant/kernel_samsung_crespo.git
94 1 Denis 'GNUtoo' Carikli
</pre>
95 13 Denis 'GNUtoo' Carikli
96 34 Wolfgang Wiedmeyer
Then in each console you build from, run:
97 13 Denis 'GNUtoo' Carikli
<pre>
98
export ARCH=arm
99 1 Denis 'GNUtoo' Carikli
export CROSS_COMPILE=arm-none-eabi-
100 13 Denis 'GNUtoo' Carikli
</pre>
101
102
Configure it for crespo:
103 1 Denis 'GNUtoo' Carikli
<pre>
104
$ make crespo_defconfig
105 13 Denis 'GNUtoo' Carikli
</pre>
106
107 34 Wolfgang Wiedmeyer
If you want to configure it further:
108 14 Denis 'GNUtoo' Carikli
<pre>
109 1 Denis 'GNUtoo' Carikli
$ make menuconfig
110 14 Denis 'GNUtoo' Carikli
</pre>
111
112 13 Denis 'GNUtoo' Carikli
Then build a zImage:
113
<pre>
114
$ make -j4 zImage
115
</pre>
116 11 Denis 'GNUtoo' Carikli
117 34 Wolfgang Wiedmeyer
If the compilation succeeded, the image is at:
118 15 Denis 'GNUtoo' Carikli
<pre>
119
arch/arm/boot/zImage
120
</pre>
121
122 34 Wolfgang Wiedmeyer
h3. Build Failures
123 15 Denis 'GNUtoo' Carikli
124 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.
125 15 Denis 'GNUtoo' Carikli
126
As a result, variations in the default kernel configuration for your device can result in build errors.
127
128
Compilation failures can also happen when you use another gcc version, like we do in this guide.
129
This happens frequently if you use a gcc that is more recent than your kernel.
130 34 Wolfgang Wiedmeyer
The "not very clean" code also increases the probability of it.
131 15 Denis 'GNUtoo' Carikli
132 20 Denis 'GNUtoo' Carikli
h3. Repacking
133 21 Denis 'GNUtoo' Carikli
134 23 Denis 'GNUtoo' Carikli
We now create a new boot.img from the parameters and ramdisk.img we extracted from the default boot.img
135
<pre>
136
$ 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
137
</pre>
138
139
Then we verify that it matches the default boot.img parameters:
140
<pre>
141
$ unbootimg -i new-boot.img 
142
total image size:   3096576
143
kernel size:        2899584
144
kernel load addr:   0x30008000
145
ramdisk size:       189142
146
ramdisk load addr:  0x31000000
147
2nd boot size:      0
148
2nd boot load addr: 0x30f00000
149
kernel tags addr:   0x30000100
150
page size:          4096
151
board:              `'
152
cmdline:            `console=ttyFIQ0 no_console_suspend'
153
id:                 f33faefb7b1eca7d1d1d6dc7603aed2bd82d65c000
154
</pre>
155
156
Here we check if the following parameters match:
157
* kernel load addr
158
* ramdisk load addr
159
* Kernel tag addr
160
* page size
161
* cmdline if you don't plan to change it.
162
163 1 Denis 'GNUtoo' Carikli
h3. Testing
164 24 Denis 'GNUtoo' Carikli
165
Reboot the device to the bootloader, and run:
166
<pre>
167
$ fastboot boot new-boot.img
168 25 Denis 'GNUtoo' Carikli
< waiting for device >
169
downloading 'boot.img'...
170
OKAY [  0.435s]
171
booting...
172
OKAY [  0.288s]
173
finished. total time: 0.723s
174 24 Denis 'GNUtoo' Carikli
</pre>