Project

General

Profile

DeprecatedReplicant40DevicePortingGuide » History » Version 6

Paul Kocialkowski, 06/23/2013 01:18 PM

1 1 Paul Kocialkowski
h1. Replicant 4.0 Porting Guide
2
3
{{>toc}}
4
5
This guide is a step-by-step explanation of the process of porting a new device to Replicant 4.0.
6
7
h2. Overview
8
9
Porting a new device to Replicant is a long task, so make sure you're ready to go through all the steps mentioned below. While it's not technically hard (unless you have to write free software replacements yourself), the process itself takes time as many steps are involved:
10
* Checking that your devices meets [[Replicant40PortingGuide#Prerequisites|the required prerequisites]]
11
* Discovering the phone's hardware and the amount of non-free blobs involved
12
* Getting Replicant sources, reading all the relevant material about developing on Replicant
13
* Adding the device-specific repositories and files
14
* Setting up the build environment for the new device
15 4 Paul Kocialkowski
* Building a recovery image
16 1 Paul Kocialkowski
17
h2. Prerequisites
18
19
Before porting your device to Replicant, you must make sure it complies with the following requirements:
20
* The phone is already supported by CyanogenMod (CM) 9.x (or, worst-case scenario, by a non-official CyanogenMod 9.x port)
21
* The phone has a GSM modem: Replicant doesn't support CDMA phones (but you can add support for it if you're skilled)
22
* There is a way of installing another operating system, either through the bootloader or via recovery (this is likely if there is a CM port)
23
* The kernel is not signed: this means that the bootloader doesn't check the kernel's signature to match with the vendor's key to allow it to run
24
25
If your device fails to comply with one of these requirements, it won't be possible to port Replicant to it.
26
If you don't know about whether your device complies or not, you'll probably learn it along the way.
27
28
h2. Discovering the phone's hardware and associated blobs
29
30 2 Paul Kocialkowski
h3. Finding the device's codenames
31 1 Paul Kocialkowski
32 2 Paul Kocialkowski
First of all, you'll have to find out the device's codename that was given by its manufacturer. "Wikipedia":http://www.wikipedia.org/ usually has that information on the device's article. For instance, the codename for the European version of the Nexus S given by Samsung is i9023. This codename will help in the process of getting informations about the device.
33 1 Paul Kocialkowski
34 2 Paul Kocialkowski
Then, a second codename (that can turn out the be the same as the previous one) is given to the device at Android-level. If your device is supported by CyanogenMod, you can find it out from the "CyanogenMod Wiki":http://wiki.cyanogenmod.org/ or on "CyanogenMod download page":http://get.cm/. For instance, the "Nexus S":http://wiki.cyanogenmod.org/w/Crespo_Info codename is: @crespo@.
35 1 Paul Kocialkowski
36 2 Paul Kocialkowski
h3. Investigating the hardware
37
38
It is useful to have a general idea of what kind of hardware is present in the phone. From the Wikipedia and CyanogenMod pages about the device, it's already possible to know what System on a Chip (SoC) it uses and a couple other details.
39
40
To learn more details, you can consider looking for a teardown of the device (for instance on "iFixit":http://www.ifixit.com/), that will reveal what chips are used on the device. Looking at the kernel defconfig for the device will also help a lot, you can also try to find the service manual for the device.
41
42
You can then compare that to the devices that are already supported in Replicant to get an idea of what will possibly work.
43
44
h3. Finding out if the device checks the kernel's signature
45
46 1 Paul Kocialkowski
One very important step is to find out if the device is Tivoized: that means that even though the manufacturer releases the kernel source code for the device, the bootloader checks the kernel signature and will refuse to start it if it's not properly signed by the manufacturer. In other words, if you build the kernel yourself, the device will refuse to run it since it's not signed by the manufacturer. Since the Linux kernel is released under the GPLv2, there are no specific dispositions to counter Tivoization, and so porting the device to Replicant is pointless as it will require a prebuilt and signed kernel from the manufacturer.
47
48 2 Paul Kocialkowski
This is not an easy information to find out, but the developers involved in the CyanogenMod port will probably know that information. It's a good idea to just ask them.
49
50
h3. Discovering the way of flashing the device
51
52
To install the future Replicant image on the device, you have to find out how the device can be flashed with a new operating system. The "CyanogenMod Wiki":http://wiki.cyanogenmod.org/ has install guides for the supported devices and you'll probably find install guides for non-official CM ports as well. It is very important to understand the flashing procedure as it will have to be documented on the Replicant wiki.
53
54
There are basically two ways of flashing a new operating system:
55
# Through the bootloader: a program has to send the images to the phone in bootloader mode. Make sure that program is free if your device supports flashing via bootloader.
56
# With recovery: a recovery image has to be installed instead of the current kernel so that at next reboot, recovery permits the installation of another operating system. Make sure this doesn't involve rooting the phone using non-free software.
57
58 3 Paul Kocialkowski
h3. The non-free blobs
59 2 Paul Kocialkowski
60
The key information to get before starting the port is the list of the non-free components that are required by CyanogenMod.
61
The easiest way to do this is to spot the device repository in "CyanogenMod repos":https://github.com/CyanogenMod/ and look for the @extract-files.sh@ or @proprietary-blobs.txt@ file on the @ics@ branch.
62
There is usually a link to the device repository from the "CyanogenMod Wiki":http://wiki.cyanogenmod.org/
63
64 1 Paul Kocialkowski
For instance, the list of non-free components for the "Nexus S":https://github.com/CyanogenMod/android_device_samsung_crespo is "extract-files.sh":https://github.com/CyanogenMod/android_device_samsung_crespo/blob/ics/extract-files.sh
65
66
From that list, spot what is related to what hardware component (audio, camera, sensors, gps, modem, etc): that gives an idea of the amount of work required to add support for the phone.
67 3 Paul Kocialkowski
68
h2. Getting started with Replicant development
69
70
In order to prepare everything for the Replicant port:
71
* Install CyanogenMod on the phone
72
* Install the [[BuildDependencies]]
73
* Get the sources: [[GettingReplicantSources]]
74
* Read the Replicant developer guide: [[DeveloperGuide]]
75
* Learn how to do debug: [[GettingLogs]] [[GDBDebugging]]
76
77
h2. Cloning the device files
78
79
Once your Replicant tree is ready, you can start adding the necessary repos for your device.
80
That means cloning the necessary repos in the right place. These repos are:
81
* A *device-specific repo*. On CyanogenMod, it is usually called: @android_device_vendor_device@.
82
* Sometimes one or more *common repo(s)*, usually called: @android_device_vendor_devices-common@.
83
Some devices don't need any common repo, but some do.
84
* A *kernel repo*. On CyanogenMod, it is usually called: @android_kernel_samsung_devices@.
85
The kernel repo can be shared across a family of devices (for instance, on kernel repo for Samsung Exynos, one for Samsung OMAP, etc).
86
87
You can find the device-specific repo from the device's page on the "CyanogenMod Wiki":http://wiki.cyanogenmod.org/. 
88
Make sure you check out the branches that match the CM 9.0 version (the branch may be called @ics@).
89
90
Once you have cloned the device-specific repo for your device and checked out the correct branch, refer to the @cm.dependencies@ file to find what repos are left to clone.
91
Clone these repos in the correct locations and remove the prefix (e.g. @android_device_samsung_crespo@ must be cloned in @device/samsung/@ and renamed to @crespo@).
92
93
If your cloned the kernel source for your device, it is likely that the kernel build is already integrated, so you can skip the next sections below.
94
95
h3. In case of a missing kernel repository
96
97
If the kernel repo is nowhere to be found (make sure you've asked the CyanogenMod team), you'll need to get the kernel source directly from the vendor, especially if your device is supported by a 3rd party CyanogenMod fork.
98
Keep in mind that the Linux kernel is GPLv2, so vendors have the legal obligation to release the modified kernel sources as soon as they sell you the device.
99
That means the kernel sources will be available online. Here are some websites where such releases are done:
100
* "Samsung Open Source Release Center":http://opensource.samsung.com/
101
For Samsung kernels. Search the device codename (e.g. I9000) and download the package called "Opensource Update" (e.g. GT-I9000_Opensource_GB_Update2.zip).
102
This will hold a kernel archive with all the sources and instructions on how to build it and which defconfig to use.
103
* "CodeAurora":https://www.codeaurora.org/contribute/projects/qaep
104
For MSM/QSD devices, and particularly HTC ones.
105
* "HTCdev":http://www.htcdev.com/devcenter/downloads
106
* "Motorola Sourceforge":http://sourceforge.net/motorola/
107
* "Sony Mobile Developer":http://developer.sonymobile.com/downloads/opensource/
108
* "LG OpenSource":http://www.lg.com/global/support/opensource/opensource.jsp
109
110
Once you have the kernel sources, read the instructions to find out which defconfig to use.
111
112
Since manufacturers usually don't release the git history along with the files, you'll need to recreate a git repo:
113
* Clone the mainline kernel in the same version as the Makefile from the sources you just obtained
114
* Remove the cloned files *except the .git directory*
115
* Move the manufacturer kernel tree at the place of the files you just removed
116
* Add all the files in git (@git add -A@) and commit (@git commit@) with a message explaining what you just imported (e.g. "GT-I9000 GB Opensource Update 2")
117
118
Now that you have a git repo, you can move it to the Replicant code tree, under the name: @kernel/vendor/devices@ (e.g. @kernel/samsung/aries@).
119
Make sure to make the @devices@ name match the @devices@ in @android_device_vendor_devices-common@ if the kernel is shared across these devices or to match the @device@ in @android_device_vendor_device@.
120
121
h3. In case of a prebuilt kernel
122
123
Some devices are still using a prebuilt kernel. Even though the CyanogenMod team is trying to avoid that, it remains in many repos.
124
For such devices, you will need to remove the prebuilt binaries and the instructions to copy the prebuilt kernel and its modules.
125
126
In the device repository (@device/vendor/device@) and common repository for your device (if any), remove the prebuilt kernel and modules (usually called @kernel@ and @module.ko@ (replace module with the name of a module) or a @modules@ directory).
127
Remove the instructions to copy these prebuilts on the makefiles. Remove instructions such as:
128
<pre>
129
PRODUCT_COPY_FILES += \
130
    $(LOCAL_KERNEL):kernel
131
132
LOCAL_KERNEL := $(LOCAL_PATH)/kernel
133
</pre>
134
and anything regarding @TARGET_PREBUILT_KERNEL@ as well as the instructions to copy the prebuilt modules.
135
136
The @BoardConfig.mk@ (or @BoardConfigCommon.mk@ in the common directory for your device) will most likely hold a line like:
137
<pre>
138
TARGET_PREBUILT_KERNEL := device/samsung/p5/kernel
139
</pre>
140
you must remove this line.
141
142
Now that the device repository has no prebuilt instructions, you can add the instructions to build the kernel. In the @BoardConfig.mk@ file, add the following lines:
143
<pre>
144
TARGET_KERNEL_SOURCE := kernel/samsung/p3
145
TARGET_KERNEL_CONFIG := samsung_p5_defconfig
146
</pre>
147
and make sure to replace the location and defconfig by the correct values for your devices (being the location of the device kernel tree and the appropriate defconfig).
148
149
h3. Building the correct kernel image format
150
151
There are different types of kernel images:
152
* Android image: that is a zImage, without a separate initramfs
153
* zImage: that is a zImage, with a built-in initramfs
154
* uImage: that is an image for the u-boot bootloader, with a built-in initramfs
155
156
You need to find out which type of kernel image your device uses. Asking people who know about that is the best idea.
157
158
h4. Android image
159
160
This is the easiest case to handle: just make sure the @CONFIG_INITRAMFS_SOURCE@ option in the kernel defonfig is left blank or undefined:
161
<pre>
162
CONFIG_INITRAMFS_SOURCE=""
163
</pre>
164
165
h4. zImage with built-in initramfs
166
167
Building a zImage with a built-in initramfs requires the following steps:
168
In the kernel defconfig, define the @CONFIG_INITRAMFS_SOURCE@ option that way:
169
<pre>
170
CONFIG_INITRAMFS_SOURCE="../../root"
171
</pre>
172
173
Once this is done, duplicate the defconfig and add the @_recovery@ prefix before the @_defconfig@ ending (e.g. @herring_recovery_defconfig@), edit that file and replace @CONFIG_INITRAMFS_SOURCE@ with:
174
<pre>
175
CONFIG_INITRAMFS_SOURCE="../../recovery/root"
176
</pre>
177
178
Back to the device repository, edit the @BoardConfig.mk@ file and add the following line:
179
<pre>
180
TARGET_KERNEL_RECOVERY_CONFIG := samsung_p5_recovery_defconfig
181
</pre>
182
and make sure to replace the defconfig by the appropriate defconfig you just cloned (the one with the @_recovery_defconfig@ ending).
183
184
Still in the device repository, create a @bootimg.mk@ file containing the following:
185
<pre>
186
LOCAL_PATH := $(call my-dir)
187
188
$(INSTALLED_BOOTIMAGE_TARGET): $(INSTALLED_KERNEL_TARGET)
189
	$(ACP) $(INSTALLED_KERNEL_TARGET) $@
190
191
$(INSTALLED_RECOVERYIMAGE_TARGET): $(INSTALLED_RECOVERY_KERNEL_TARGET)
192
	$(ACP) $(INSTALLED_RECOVERY_KERNEL_TARGET) $@
193
</pre>
194
195
Edit the @BoardConfig.mk@ file and add the following line:
196
<pre>
197
BOARD_CUSTOM_BOOTIMG_MK := device/vendor/device/bootimg.mk
198
</pre>
199
and make sure to replace @device/vendor/device/@ to the correct path to your device's repository.
200
201
h4. uImage with built-in initramfs
202
203
Follow the previous instructions (*zImage with built-in initramfs*) and set the @BOARD_USES_UBOOT@ variable in the @BoardConfig.mk@ file:
204
<pre>
205
BOARD_USES_UBOOT := true
206
</pre>
207 5 Paul Kocialkowski
208
209
h2. Setting up the build environment
210
211
Now that the repos are cloned, you need to modify some makefiles to cope with Replicant paths.
212
In the device repository (@device/vendor/device@), modify the file called @cm.mk@ and replace the @vendor/cm/@ occurrences by @vendor/replicant/@. Other makefiles may need that as well (in any case, build will fail very early if you missed one). In that same @cm.mk@ file, change the PRODUCT_NAME variable by replacing the @cm@ prefix with @replicant@ (e.g. change PRODUCT_NAME := cm_crespo  to PRODUCT_NAME := replicant_crespo).
213
214
Now that your device files are ready, you can declare a new build target: these are held in @vendor/replicant/jenkins-build-targets@.
215
Modify that file and add a line (at the end) with the PRODUCT_NAME you set and the @-eng@ suffix (e.g. @replicant_crespo-eng@).
216
217
From now on, everything should be ready to start a build. To check for errors or missed occurrences, start a terminal in the Replicant tree root and lunch:
218
<pre>
219
source build/envsetup.sh
220
lunch replicant_device-eng
221
</pre>
222
223
Adapt replicant_device-eng from what you added to the @jenkins-build-target@ (e.g. @replicant_crespo-eng@).
224
If an error occurs, it will explicitly report it and you'll need to fix it before doing anything.
225
If everything works correctly, you should see something like:
226
<pre>
227
============================================
228
PLATFORM_VERSION_CODENAME=REL
229
PLATFORM_VERSION=4.0.4
230
TARGET_PRODUCT=replicant_crespo
231
TARGET_BUILD_VARIANT=eng
232
TARGET_BUILD_TYPE=release
233
TARGET_BUILD_APPS=
234
TARGET_ARCH=arm
235
TARGET_ARCH_VARIANT=armv7-a
236
HOST_ARCH=x86
237
HOST_OS=linux
238
HOST_BUILD_TYPE=release
239
BUILD_ID=IMM76L
240
============================================
241
</pre>
242
243
*You must repeat these steps everytime before building anything on a freshly-opened terminal.*
244
Remember:
245
<pre>
246
source build/envsetup.sh
247
lunch replicant_device-eng
248
</pre>
249
(make sure to replace device by your device's product name).
250
251
h2. Building a recovery image
252
253
Now that everything is set-up, you can build the first image to test on your device: the recovery image.
254
255 6 Paul Kocialkowski
The build target is *recoveryimage*, so all you have to do is:
256 5 Paul Kocialkowski
<pre>
257
make -j9 recoveryimage
258
</pre>
259
260 6 Paul Kocialkowski
This should trigger the kernel build and the recovery initramfs build and in the end, produce the @out/target/product/device/recovery.img@ file.
261 5 Paul Kocialkowski
Once your image is built (it takes some time), flash it to the recovery partition of your device (if any). It's a good idea to look at the CyanogenMod installation guide to find out how to install that recovery image.
262 1 Paul Kocialkowski
263
There is usually also a key combination to hold to boot directly to recovery: hopefully, your recovery image will start.
264 6 Paul Kocialkowski
265
h2. Building the system
266
267
It is time to build a complete set of Replicant images. This includes at least the system and kernel images. Depending on the installation method, an userdata image might be needed too.
268
269
h3. Building the kernel
270
271
Let's start by building the boot image, that is both the kernel and the Android initramfs. The build target is *bootimage*:
272
<pre>
273
make -j9 bootimage
274
</pre>
275
276
In the end, the @out/target/product/device/boot.img@ file will be produced.
277
278
h3. Setting up the system image format
279
280
It is time for you to take a good look at the installation process. Mainly, about whether the images will be flashed using the bootloader or recovery.
281
Since CyanogenMod uses the zip installation method, that we do not want to use, you're on your own here.
282
283
h4. Finding the appropriate filesystem
284
285
It will be easy to find out the filesystem for the different partitions if the device already runs CyanogenMod:
286
<pre>
287
$ adb shell mount
288
rootfs / rootfs ro,relatime 0 0
289
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
290
devpts /dev/pts devpts rw,relatime,mode=600 0 0
291
proc /proc proc rw,relatime 0 0
292
sysfs /sys sysfs rw,relatime 0 0
293
none /acct cgroup rw,relatime,cpuacct 0 0
294
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
295
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
296
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
297
/dev/block/mtdblock2 /system yaffs2 ro,relatime 0 0
298
/dev/block/mtdblock3 /cache yaffs2 rw,nosuid,nodev,relatime 0 0
299
/dev/block/mtdblock5 /radio yaffs2 rw,relatime 0 0
300
/dev/block/mmcblk0p2 /data ext4 rw,nosuid,nodev,noatime,nodiratime,barrier=1,data=ordered,noauto_da_alloc 0 0
301
/dev/block/mtdblock6 /datadata yaffs2 rw,relatime 0 0
302
/dev/block/mtdblock4 /efs yaffs2 rw,relatime 0 0
303
/sys/kernel/debug /sys/kernel/debug debugfs rw,relatime 0 0
304
/dev/block/vold/179:1 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
305
/dev/block/vold/179:1 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
306
tmpfs /mnt/sdcard/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0
307
</pre>
308
309
So we can deduce that system is yaffs2 and data is ext4. Don't bother about the other partitions and mount-points, only */system* and */data* matter for now.
310
311
h4. Changing the images format for bootloader installation
312
313
You have to modify the @BoardConfig.mk@ file on the main device repository (it might be delegated to @BoardConfigCommon.mk@ on the common repos).
314
315
To build ext4 system and userdata images, make sure you have:
316
<pre>
317
TARGET_USERIMAGES_USE_EXT4 := true
318
</pre>
319
To build yaffs2 system and userdata images, make sure you have:
320
<pre>
321
TARGET_USERIMAGES_USE_EXT4 := false
322
</pre>
323
324
h4. Changing the images format for recovery installation
325
326
MORE TO COME
327
328
h3. Building the system image
329
330
Building the system is the longest task. The build target is *systemimage*:
331
<pre>
332
make -j9 systemimage
333
</pre>
334
335
You might encounter build errors due to the lack of non-free libs. You'll need to find clean workarounds for that. Removing options from @BoardConfig.mk@ can help solve the situation.
336
For instance, the following error:
337
<pre>
338
make: *** No rule to make target `out/target/product/i9300/obj/lib/libTVOut.so', needed by `out/target/product/i9300/obj/EXECUTABLES/mediaserver_intermediates/LINKED/mediaserver'.  Stop.
339
</pre>
340
Was solved by turning @BOARD_USE_SECTVOUT@ to false:
341
<pre>
342
BOARD_USE_SECTVOUT := false
343
</pre>