Project

General

Profile

KernelBuild » History » Revision 37

Revision 36 (Wolfgang Wiedmeyer, 05/08/2017 09:06 AM) → Revision 37/41 (Wolfgang Wiedmeyer, 05/08/2017 09:15 AM)

h1. Kernel Build 

 h2. Use case 

 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. 

 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. 

 h2. Dependencies  

 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. 

 The Trisquel ARM version of gcc seem to work well. To install it run: 
 <pre> 
 $ apt-get install gcc-arm-none-eabi 
 </pre> 

 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. 

 You can install Trisquel in a container to work around this. 
 This way, it will have very few CPU and memory overhead compared to a virtual machine. 
 It will also save disk space since you can just store the Trisquel rootfs in any directory. 

 h2. Example with crespo under Trisquel 

 h3. Getting the right parameters 

 First download the following example image and its signature: 
 * https://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip.asc 
 * https://ftp-osl.osuosl.org/pub/replicant/images/replicant-4.2/0004/images/crespo/replicant-4.2-crespo.zip 

 As usual, verify the signature after [[ReplicantReleaseKey#Replicant-42-and-below|importing the release key]]: 
 <pre> 
 $ gpg --armor --verify path/to/replicant-4.2-crespo.zip.asc path/to/replicant-4.2-crespo.zip 
 </pre> 

 Make sure the check succeeds! 

 Then unpack the zip file: 
 <pre> 
 $ mkdir replicant-4.2-crespo && cd replicant-4.2-crespo && unzip ../replicant-4.2-crespo.zip 
 </pre> 

 That should have extracted a boot.img. We then should not forget to look at what format the boot.img is in: 
 <pre> 
 $ file boot.img 
 boot.img: Android bootimg, kernel (0x30008000), ramdisk (0x31000000), page size: 4096, cmdline (console=ttyFIQ0 no_console_suspend) 
 </pre> 
 Here it says it's an "Android bootimg", so we need the following tools: 
 * @mkbootimg@ to pack an image 
 * @unpackbootimg@ to unpack an image (older versions are called @unbootimg@ and behave differently as described below) 

 Ways to get such tools: 
 * [[ReplicantImages|Pre-built by Replicant]] 
 * Some GNU/Linux distributions also have packages for some of the tools. 
 * Build Replicant and use its tools. This would defeat the purpose of this tutorial since we want to avoid a full build of Replicant. 

 Some android tools were converted to build on GNU/Linux without requiring the Android build system: 
 * In https://github.com/freesmartphone/utilities, you have the source code for @mkbootimg@ and @unbootimg@ in @android/image-utils@. 

 Extract the ramdisk, kernel image and parameters from the original boot.img: 

 <pre> 
 $ unpackbootimg -i boot.img -o boot  
 Android magic found at: 0 
 BOARD_KERNEL_CMDLINE console=ttyFIQ0 no_console_suspend 
 BOARD_KERNEL_BASE 30000000 
 BOARD_RAMDISK_OFFSET 01000000 
 BOARD_SECOND_OFFSET 00f00000 
 BOARD_TAGS_OFFSET 00000100 
 BOARD_PAGE_SIZE 4096 
 BOARD_SECOND_SIZE 0 
 BOARD_DT_SIZE 0 
 </pre> 

 This will unpack the boot.img in the directory @boot@. 

 We also check the kernel image format, since we will build that: 
 <pre> 
 $ file boot/boot.img-zImage 
 boot/boot.img-zImage: Linux kernel ARM boot executable zImage (little-endian) 
 </pre> 

 h3. Building 

 If you want to be able to run "make menuconfig", install libncurses5-dev: 
 <pre> 
 # apt-get install libncurses5-dev 
 </pre> 

 Download the sources: 
 <pre> 
 $ git clone https://git.replicant.us/replicant/kernel_samsung_crespo.git 
 </pre> 

 Then in each console you build from, run: 
 <pre> 
 $ export ARCH=arm 
 $ export CROSS_COMPILE=arm-none-eabi- 
 </pre> 

 Configure it for crespo: 
 <pre> 
 $ make crespo_defconfig 
 </pre> 

 If you want to configure it further: 
 <pre> 
 $ make menuconfig 
 </pre> 

 Then build a zImage: 
 <pre> 
 $ make -j4 zImage 
 </pre> 

 If the compilation succeeded, the image is at: 
 <pre> 
 arch/arm/boot/zImage 
 </pre> 

 h3. Build Failures 

 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. 

 As a result, variations in the default kernel configuration for your device can result in build errors. 

 Compilation failures can also happen when you use another gcc version, like we do in this guide. 
 This happens frequently if you use a gcc that is more recent than your kernel. 
 The "not very clean" code also increases the probability of it. 

 If you manage to fix build failures for a kernel, feel free to [[DeveloperGuide#Code-hosting-and-submitting-patches|submit patches]]. 

 h3. Repacking 

 We now create a new boot.img from the parameters and ramdisk ramdisk.img we extracted from the default boot.img 
 <pre> 
 $ mkbootimg --kernel path/to/arch/arm/boot/zImage /path/to/arch/arm/boot/zImage --ramdisk boot/boot.img-ramdisk.gz ramdisk.img --cmdline "console=ttyFIQ0 no_console_suspend" --base 0x30000000 --pagesize 4096 -o new-boot.img 
 </pre> 

 Then we verify that it matches the default boot.img parameters: 
 <pre> 
 $ unpackbootimg -i new-boot.img  
 Android magic found at: 0 
 BOARD_KERNEL_CMDLINE console=ttyFIQ0 no_console_suspend 
 BOARD_KERNEL_BASE 30000000 
 BOARD_RAMDISK_OFFSET 01000000 
 BOARD_SECOND_OFFSET 00f00000 
 BOARD_TAGS_OFFSET 00000100 
 BOARD_PAGE_SIZE 4096 
 BOARD_SECOND_SIZE 0 
 BOARD_DT_SIZE 0 
 </pre> 

 Here we check if the following parameters match: 
 * Kernel base addr (@BOARD_KERNEL_BASE@) 
 * Ramdisk offset (@BOARD_RAMDISK_OFFSET@)  
 * Kernel tag offset (@BOARD_TAGS_OFFSET@)  
 * Page size (@BOARD_PAGE_SIZE@) 
 * cmdline if you don't plan to change it (@BOARD_KERNEL_CMDLINE@) 

 h3. Testing 

 Reboot the device to the bootloader, and run: 
 <pre> 
 $ fastboot boot new-boot.img 
 < waiting for device > 
 downloading 'boot.img'... 
 OKAY [    0.435s] 
 booting... 
 OKAY [    0.288s] 
 finished. total time: 0.723s 
 </pre>