If you have watched "Top Gear", you would have heard Jeremy Clarkson said "How hard could it be?". Why is it so hard to build a rom for our tab?
When I first play with rom building, it was quite straightforward. I recuilt my retired PC (Celeron 1.2G with 3G RAM, 40G HD) and follow the guild for building PA. Installing Ubuntu 12.04 and then the necessary packages. Everything is ready in a few hours. It was really challenging your patience to repo sync the source. After a few retries, I finally completed the download in two days! After two hours, my harddisk is full! Then I collected another retired 80G HD from a friend. Re-install everything and build again. Building requried 7 hours on my antique PC. With only a few minor errors to correct, my first rom pa2.55 was out. So how hard could it be? It was our predecessors ( @ohanar, @locerra, etc.) who had already done lots of hardwork on device trees and kernel! If Samsung has really released the hardware related sources, there will not be so much troubles for us. At the time Android 4.2 launch, we have a new problem. Our tab's CSR chip doesn't support Bluedroid. See the Nexus family, everything is ready. Rom building will never be so pain in the ass as us.
1.1 What's wrong with our hardware
GT-7.7 and Plus use the exynos4 smdk4210 board with Mali-400 GPU. Quite a number of devices have similar hardware. The main problem we have would be:
1.1.1 CSR bluetooth chip.
CSR chip doesn't support bluedroid which become standard after Android 4.2. Bluez.org has provide a solution for 4.2 early this year but porting to 4.3 and 4.4 may take some time.
1.1.2 Athero wifi chip.
Samusng's wifi driver has issues on 5G support and SOD. I'm using the modified kernel driver from Samsung's 4.1.2 source. Athero provide drivers for 3.1 to 3.10 linux kernels but they have no wakelock support. I also wrote a drive base on Athero's source and add wakelock support but have no time to polish it now.
1.1.3 modem chip (xm6260).
RIL stand for (radio interface library). Most of the phone manufacturer would provide their own vendor lib. The only working blob now is Samsung's ICS propietry libsec-ril.so which is obselete now. The one from stock 4.1.2 not working but no time to trace. It also don't seems to have much difference. This library handle the AT commands issuing to the (baseband to) modem chip. xm6260 has a complete set of command manual. It should not be too hard to rewrite if we can hack the ports. However it is not worth to splend too much time on this. Those has experience in AT commands are welcome to help.
RIL details: http://www.kandroid.org/online-pdk/guide/telephony.html
1.2 Where to get the source?
1.2.1 Samsung's source for our tab
The best source would be from Samsung http://opensource.samsung.com/. Sadly, the source from Samsung only cover the kernel. Our tab only have source up to 4.1.2 and kernel source 3.0.31 from Samsung.
1.2.2 Samsung's source for other devices
Luckly, we have similar architecture to some newer Samsung models. Their source is our major source now. eg. We can use the Mali driver from Note8 and/or i9300.
1.2.3 Manufacturere's web site.
The other source would be from the chip manufacturer. eg. Athero has source for linux.
1.3 What is going on at boot?
1.3.1 Power ON
a) When power button is long pressed (should be released after the logo shows otherwise it will reboot again every 6 seconds). The manufacturer's bootloader would show the vendor's logo. At that moment, bootloader would sense if there are key being pressed. Standard Android will go to boot menu when pwr + vol- is pressed. Samsung would go to download mode (something like fastboot) and accept pwr + vol+ to start recovery mode. If no other key is pressed, it would be a normal boot.
b) When USB is plug in (PC usb port or charger) at power off, the tab will be turn on silently. It is in the LPM (low power mode). According to lpm.rc (executed by Init, will be explained in 1.3.3), CPU will be set to POWERSAVING and the program charger is running. It should only handle simple tasks like Detecting the pwr button to show the charging images. If the pwr is long press when showing the charging image, it will reboot as in a).
3) Lollipop LPM: charger is link to the program /sbin/healthd. The process is similar to b) but lpm.rc is merged to init.rc.
1.3.2 Boot image
For our tab, there are two parts for a boot.img or a recovery.img. kernel binary and initramfs. As the name imply, Init-Ram-FS is the initial file system loaded into ram when the devices boot. Kernel binary provide the hardware drivers libraies which communicate with the hardware and baseband. At normal boot or recovery mode, the image (in /boot or /recovery partition) will be decompressed and loaded to memory. Initramfs will be loaded in root(/). It would consume a few hundreds MB of RAM and that's why our free ram is only 7xxM.
In LPM, it would depends on the last boot state. If it was powered off from recovery, recovery.img will be loaded and handle the LPM. If it was powered off from platform, boot.img will be loaded and handle LPM. That's why It might show different charging images if the charging images for reovery.img and boot.img are different.
Stock cook roms can unpack and modified the initramfs from the existing zImage. It is created in system/core if we build it from source. Here is a tool which can unpack/repack our boot.img or recovery.img.
There is the Init program in every initramfs. This is the first program to run according to the sequence in the init.rc (or lpm.rc when in LPM). Partitions will be mounted and symlinked, default values and permissions are assigned, services will be started, etc. The source of Init is in system/core. The last thing Init do would be the continuous eventloop to process events.
2. Getting Start
To start building, we need the building environment. Many tutorials have alreay covered the details so I just list my suggestions. The guide from Google is a bit outdated but really helpful. http://source.android.com/source/building.html
Basic knowledge of linux is required. All commands are run in a terminal.
A decent PC with as much ram as possible. Thanks for the donations, I can upgrade to a better i5 notebook with 4G ram and a 500G partition only for building. It would take 3+hrs. for a clean build. RAM and Harddisk speed are essential. A desktop PC with same configuration will be faster since notebook harddisk is slower. I plan to add more RAM when the price drop (seems to be a long wait )
RAM: 4G or above is recommended.
Harddisk space: 30G+ for system, 60G+ per rom, size of ccache (50G-100G), Roughly, a partition with at least 120G for one rom. Bigger is better.
A decent internet connection is essential. It will take days to sync the source if you only have bandwidth of 2M or less .
64bit Ubuntu Desktop 12.04 or above. Recommend 13.04 or above. I'm using the latest 14.04.
2.3 Install the required packages
These packages are required during building and packing. (eg. gzip is required to compress the kernel binary)
type command: (have a beer or coffee. It will take a while)
For Ubuntu 12.x and 13.x: sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 \ libc6-dev lib32ncurses5-dev ia32-libs x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 lib32z-dev \ libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc \ readline-common libreadline6-dev libreadline6 bzip2 libbz2-dev libbz2-1.0 libncurses5-dev lib32readline5 \ lib32readline-gplv2-dev lib32readline6 libreadline-dev libreadline6-dev:i386 libreadline6:i386 pngcrush \ libghc-bzlib-dev lib32bz2-dev libsdl1.2-dev libesd0-dev squashfs-tools schedtool libwxgtk2.8-dev python sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so For Ubuntu 14.x and 15.x: sudo apt-get install git-core gnupg flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.8-dev squashfs-tools \ build-essential zip curl libncurses5-dev zlib1g-dev pngcrush schedtool libxml2 libxml2-utils xsltproc lzop \ libc6-dev schedtool g++-multilib lib32z1-dev lib32ncurses5-dev lib32readline-gplv2-dev gcc-multilib
Download (for Linux 64-bit of course): http://developer.android.com/sdk/index.html
Extract the file to a directory (eg. ~/android-sdk)
Add the path to ~/.bashrc
sudo gedit ~/.bashrc
Verify the path and update the SDK
If you have installed eclipse, openJAVA is ready. Some say it is not suitable for android development.
I've tried both and I found no appearing difference.
To install SUN JAVA (Ubuntu 13.04 or above)
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java6-installer
sudo apt-get install openjdk-7-jdk
mkdir -p ~/bin curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo
Which are required for adb and fastboot, eg. 51-android.zip
sudo cp 51-android.rules /etc/udev/rules.d sudo chmod 0644 /etc/udev/rules.d/51-android.rules
sudo gedit ~/.bashrc
export USE_CCACHE=1 export CCACHE_DIR=~/android/.ccache
ccache will cache the object files of gcc for next build, it would increase the speed of subsequent builds alot.
After source is sync in 2.11 and before build, we can set the size of ccache first.
Make a new working driectory for your rom
type command: (eg. ~/android/rom)
mkdir -p ~/android/rom
It is usually in the project android (manifest for PA). The readme of this project would provide the command to init.
eg. CM11.0: https://github.com/CyanogenMod/android/tree/cm-11.0
repo init -u git://github.com/CyanogenMod/android.git -b cm-11.0
2.10 preparing local_manifest.xml
It is not a good pratice to change the manifest.xml directly. If the ROM has change its manifest, you would require to init again. The right way is putting everything your device needed in a seperate file .repo/local_manifests/local_manifest.xml. The device trees and kernel source for GalaxyTab 7.7 and Plus : https://github.com/danielhk?tab=repositories
eg. cm11.0 for p6800
<?xml version="1.0" encoding="UTF-8"?> <manifest> <project name="danielhk/android_hardware_atheros_wlan" path="hardware/atheros/wlan" /> <project name="danielhk/android_device_samsung_p6800" path="device/samsung/p6800" /> <project name="danielhk/android_device_samsung_smdk4210-tab" path="device/samsung/smdk4210-tab" /> <project name="danielhk/android_kernel_samsung_smdk4210" path="kernel/samsung/smdk4210" /> <project name="danielhk/proprietary_vendor_samsung_smdk4210" path="vendor/samsung" /> <project name="CyanogenMod/android_hardware_samsung" path="hardware/samsung" /> </manifest>
2.11 sync and build
To fetch the source (specified in manifest.xml and local_manifest.xml) to working directory (~/android/rom)
repo sync -j32
When it is done. You will find that ~/android/rom is not empty now.
You may set the ccache size now:
type command: (eg. 50G)
To build a rom, change to the rom directory. Usual commands: (eg. for CM roms)
cd ~/android/rom . build/envsetup.sh lunch cm_p6800-userdebug mka bacon
Copy it to the rom directory, type command:
cd ~/android/rom ./rom-build.sh p6800
3. What can we do?
Since Samsung provide the complete kernel source, working on kernel would be comparatively speaking much easier.
When we say kernel, we mean the boot.img or zImage which already include the initramfs. This image will be flash to the /kernel (/dev/block/mmcblk0p5)
Hardware and flags are defined in defconfig inside <kernel dir>/arch/arm/config. It is the Linux standard.
3.1.1 difference between stock kernel and the kernel in custom rom
The main difference between the two kernels is the location of kernel library modules. Libray modules are mainly for network and storage drivers. By building them as modules, they can be stoped and restarted in case of critical errors.
Stock kernel put it in /lib which is part of the initramfs. We need to do extra steps to manually copy them to the initramfs after the first build and rebuild again. If you want to build the stock kernel, there is a very good tutorial.
Custom rom put the kernel library modules in /system/lib. Thus boot.img need to match those modules, otherwise they will not work properly.
For custom roms, which defconfig to be used is defined in device tree.
eg. For p6800, in the file device/samsung/p6800/BoardConfig.mk. There is the line:
TARGET_KERNEL_CONFIG := cyanogenmod_p6800_defconfig
I wrote a build-k.zip scirpt to simplify the building of kernel alone. Copy it to the rom working directory (~/android/rom)
eg. For p6800, type command:
3.1.2 Add/modify O/C, U/V, GOVERNORS, hardware drivers, etc.
As mentioned in 1.2, we can copy/modified the source from i9100 and n7000 kernels. For example, Dorimanx and Slyahkernel provide many useful code for us. The source of i9300 from Samsung may also provide some drivers update (eg. Mali)
Ref: Exynos4210 cpu menu., Mirror download
3.1.3 Default Orientation
Why the incall screen always landscape?
Many apks assume the default orientation is portrait but our tab's default orientation is landscape. It is a kernel behaviour so that recoveries (including the stock 3e recovery) are all in landscape.
It is already in my do list for a long time but trying to keep up with the android plaftform already occupy all my spare time...
I already have some idea how to do. Anyone interested to help on this can PM me.
3.1.4 Update the kernel source
The existing kernel is base on Linux 3.0.x. It is outdated but I don't think Samsung would bother to update it. I had tried starting with the 3.4 source from Google but don't have enough time to carry on. I have filled some drivers already. Will take another shot later if I have time...
Anyone have experience in Linux kernel are welcome to help. No more guides are there, this would be a great challenge but fun!
3.2 device tree
Every device requires a device tree which:
a) Specify the hardware
b) define some overrided library. (eg. sensor, camera)
c) provide the extra files to be put in the initramfs. (init.<device>.rc, fstab.<device>, etc.)
If there are a few models variants, common parts would be put in a seperate common device tree (eg. smdk4210-tab for our tab). In this way, changing can be made once for all variants.
Most of the work on porting for a specific ROM would be on the device tree. Same as the kernel, we can get some update by modifying from other devices. CM's device tree for i9100, n7000 and i9300, etc. With a good device tree, we can port to any ROM.
3.3 Bluetooth support after 4.2.
In 4.2.2, I changed the source of framework, system_core, phone apk and device tree to work with bluez. It was a huge job! Hundreds of lines had been changed. Many files are replaced. It was quite clumsy but was the only way then. This way may not work for 4.3 onward.
Here are the details of both stack:
Bluedriod : http://source.android.com/devices/bluetooth.html
Bluez : http://www.kandroid.org/online-pdk/guide/bluetooth.html
3.3.1 We may modify the bluedroid to support our chip.
3.3.2 Working on the bluez support.
Bluez5 is OK now. See my thread: How to port Bluez to Android 4.4
With the device tree and kernel, we can build the recovery from source. CWM would be a side product of CM roms and TWRP has a tutorial.
The same build-k.zip script can be used to build recovery alone. In the rom working direcotry, to build recovery only
./build-k p6800 recovery