[Sony Xperia Miro, Tipo (Dual), J, E] purplemaze:
Xperia J: fast+snappy stock kernel JB (11.2.A.0.21)

The following guide shows how to build the stock sources for you

stock xperia j phone with JB.

When Foxcon adopted the drivers for the xperia j they left in

a huge amount of debug which slows down your phone.

When we finally build our own kernel (Chapter 7)

we optimize it by:

- optimizing kernel for speed (not size)

- remove a huge amount of debug from the drivers

- remove kernel core debug

- remove debug_fs

- build without module support

In my opinion the kernel feels much more snappier afterwards.

The system reacts much more fluent on user inputs and sound

has less hangs than before.

Let me hear how it feels for you and if you like it or not :)

Maybe you have further modifications. So please post it here.

The last section describes how you can build your own kernel patch files.

0. Prerequisites

1. Extracting the current boot image

2. Splitting the image into kernel, ramdisk and cmdline

3. Unpack the ramdisk

4. Build the sony kernel with the original kernel configuration (.config)

5. Build a new boot image

6. Flash the new boot image to the phone

7. Now for the FUN part: TUNE the sony kernel with the attached patch-file

A: Howto build a patch file by comparing a

fresh extracted kernel sources with your edited sources:

0. Prerequisites

==================

- Device needs to be rooted and bootloader unlocked !!!

- Device needs to be up to date with latest Jelly Bean release 11.2.A.0.21 !!!

- A linux machine as working environment

- free ARM compiler, lite version, EABI, URL:

sourcery.mentor.com/sgpp/lite/arm/portal/release2322

- latest Xperia Jlo sources, URL:

developer.sonymobile.com/downloads/xperia-open-source-archives/open-source-archive-for-build-11-2-a-0-21/

- phyton script 'mkelf.py' to re-/build parition image, URL:

dl-developer.sonymobile.com/tools/image_generation_script_for_Xperia_smartphones.zip

- another basic guide, URL:

developer.sonymobile.com/2011/05/06/how-to-build-a-linux-kernel

1. Extracting the current boot image

======================================

- Install Andrdoid SDK.

- Then add a path to your .bashrc file of your linux host:

linux-w49x:~/my_kernel # echo "export PATH=/root/adt-bundle-linux-x86-20130219/sdk/platform-tools:$PATH" >> ~/.bashrc

linux-w49x:~/my_kernel # . ~/.bashrc

- Enable "USB-Debugging" in the phone seetings

- Start the phone and connect via USB to your linux machine

- At first we copy the sony boot image to the sdcard of the device:

linux-w49x:~ # adb shell

shell@android:/ $ su

shell@android:/ # dd if=/dev/block/mmcblk0p3 of=/sdcard/sony_boot.img

40960+0 records in

40960+0 records out

20971520 bytes transferred in 2.078 secs (10092165 bytes/sec)

- Read kernel config of your current kernel and store it on sdcard, too:

shell@android:/ $ su

shell@android:/ # cat /proc/config.gz > /sdcard/sony_config.gz

130|shell@android:/ $ exit

130|shell@android:/ $ exit

- Transfer both to your linux PC:

linux-w49x:~ # adb pull /sdcard/sony_boot.img

linux-w49x:~ # adb pull /sdcard/sony_config.gz

2. Splitting the image into kernel, ramdisk and cmdline

========================================================

- Basically the image consists of:

* 4k singed sin header with a ?x509? certificate

* kernel

* ramdisk

* cmdline parameters for the kernel

* a lot of empty space (~15 MB)

- Hexdump the image to make it human readable:

linux-w49x:~ # hexdump -C sony_boot.img > dump

linux-w49x:~ # head dump

00000000 7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00 |.ELF...a........|

00000010 02 00 28 00 01 00 00 00 00 80 20 00 34 00 00 00 |..(....... .4...|

00000020 00 00 00 00 00 00 00 00 34 00 20 00 03 00 00 00 |........4. .....|

00000030 00 00 00 00 01 00 00 00 00 10 00 00 00 80 20 00 |.............. .| <== Byte 9/10/11: is kernel start (after 4k sin header)

00000040 00 80 20 00 68 70 3c 00 68 70 3c 00 00 00 00 00 |.. .hp<.hp<.....| <== Byte 9/10/11: is kernel length

00000050 00 00 00 00 01 00 00 00 68 80 3c 00 00 00 40 01 |........h.<...@.| <== Byte 9/10/11: is ramdisk start

00000060 00 00 40 01 c6 1b 15 00 c6 1b 15 00 00 00 00 80 |..@.............| <== Byte 9/10/11: is ramdisk length

00000070 00 00 00 00 04 00 00 00 2e 9c 51 00 00 00 00 00 |..........Q.....| <== Byte 9/10/11: is cmdline start

00000080 00 00 00 00 00 02 00 00 00 02 00 00 00 00 00 20 |............... | <== Byte 9/10/11: is cmdline length (512 characters)

00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

- Now read the addresses from behind:

00 10 00 => is 0x001000

68 70 3c => is 0x3c7068

68 80 3c => is 0x3c8068

c6 1b 15 => is 0x151bc6

2e 9c 51 => is 0x519c2e

00 02 00 => is 0x000200

- NOTE: THE NUMBERS WILL BE DIFFERENT FOR YOUR st26i DEVICE !!!

- Use 'dd' to split sony_boot.img into its single parts.

- BUT use the correct numbers from your kernel here:

linux-w49x:~ # dd skip=$((0x001000)) count=$((0x3c7068)) bs=1 if=sony_boot.img of=sony_kernel.img

3960936+0 records out

3960936 bytes (4.0 MB) copied, 66.2326 s, 59.8 kB/s

linux-w49x:~ # dd skip=$((0x3c8068)) count=$((0x151bc6)) bs=1 if=sony_boot.img of=sony_ramdisk.img.gz

1383366+0 records in

1383366+0 records out

1383366 bytes (1.4 MB) copied, 23.1965 s, 59.6 kB/s

linux-w49x:~ # dd skip=$((0x519c2e)) count=$((0x000200)) bs=1 if=sony_boot.img of=sony_cmdline.img

512+0 records in

512+0 records out

512 bytes (512 B) copied, 0.00931558 s, 55.0 kB/s

3. Unpack the ramdisk

=======================

- First unzip ramdisk (should start with 0x1f 0x8b )

linux-w49x:~ # hexdump -C sony_ramdisk.img.gz | head

00000000 1f 8b 08 00 a0 a8 50 51 00 03 ec 3d 69 73 db b8 |......PQ...=is..|

00000010 92 f9 fa f4 2b b0 72 ea cd d8 ab 83 92 cf 64 ca |....+.r.......d.|

00000020 5b 2b 5b b4 ad 7a b6 e4 91 e4 1c b5 f5 4a a1 48 |[+[..z.......J.H|

00000030 48 42 99 57 78 58 f1 ce e4 bf 6f 37 c0 03 a4 a8 |HB.WxX....o7....|

00000040 83 49 66 6a 76 ca aa 38 a6 c0 ee 46 a3 bb d1 68 |.Ifjv..8...F...h|

00000050 a0 01 58 39 55 4e 95 96 a2 28 ea 61 eb 4c 85 df |..X9UN...(.a.L..|

00000060 ca 51 4b ed 2a c5 9f 37 c7 ad a3 ab f6 71 f7 38 |.QK.*..7.....q.8|

00000070 57 7e 16 fd 6e af c1 4b ca 1b af 14 b9 be 2b 8e |W~..n..K......+.|

00000080 dc ea 1c ad c1 6b 65 bf 25 5f b7 d4 d7 4a e8 85 |.....ke.%_...J..|

00000090 f4 89 da 81 d1 98 3b a6 31 63 fe a2 e1 e9 af 5e |......;.1c.....^|

linux-w49x:~ # gunzip sony_ramdisk.img.gz

- Now again, the resulting sony_ramdisk.img should start with 0x30 0x37 0x30.

- Now extract cpio archive to a directory:

linux-w49x:~ # mkdir ramdisk

linux-w49x:~ # cd ramdisk/

linux-w49x:~/ramdisk # cpio -i < ../sony_ramdisk.img

4444 blocks

linux-w49x:~/ramdisk # ls

charger init init.qcom.ril.path.sh init.s1.rc logo.rle sys

data init.Sony.rc init.qcom.sh init.target.rc logo.rle.org system

default.prop init.goldfish.rc init.qcom.usb.rc init.trace.rc proc ueventd.Sony.rc

dev init.qcom.class_core.sh init.qcom.usb.sh init.usb.rc res ueventd.goldfish.rc

fstab.msm7627a init.qcom.class_main.sh init.rc init.usbmode.sh sbin ueventd.rc

- OPTIONAL: You can now modify the ramdisk to your needs... we will

repack it later from here.

4. Build the sony kernel with the original kernel configuration (.config)

===========================================================================

In this section we will just rebuild the sony kernel with its default config.

This step is optional. You might want to SKIP THIS STEP and continue dircetly

with building the optimized kernel (Chapter 7)

- Install the mentioned ARM compiler from Mentor (for URL, see top of page)

- Extract the kernel config we fetched from the device:

linux-w49x:~ # gunzip sony_config.gz

- Extract the sony kernel:

linux-w49x:~ # tar -xf 11.2.A.0.21.tar.bz2

- Add the config to the kernel base directory:

linux-w49x:~ # cp sony_config ./kernel/.config

linux-w49x:~ # cd kernel

linux-w49x:~/kernel # ARCH=arm CROSS_COMPILE=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/arm-none-eabi- make oldconfig

- OPTIONAL: reconfigure the kernel OR skip this step:

linux-w49x:~/kernel # ARCH=arm CROSS_COMPILE=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/arm-none-eabi- make menuconfig

- Because the ARM compiler is pretty strict, edit the kernel Makefile in "kernel/Makefile":

- Change this part....

ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE

KBUILD_CFLAGS += -Os

else

KBUILD_CFLAGS += -O2

endif

- .. by appending this to the flags:

ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE

KBUILD_CFLAGS += -Os $(call cc-disable-warning,maybe-uninitialized,) $(call cc-disable-warning,implicit-function-declaration,) $(call cc-disable-warning,strict-prototypes,) $(call cc-disable-warning,unused-function,) $(call cc-disable-warning,unused-variable,)

else

KBUILD_CFLAGS += -O2 $(call cc-disable-warning,maybe-uninitialized,) $(call cc-disable-warning,implicit-function-declaration,) $(call cc-disable-warning,strict-prototypes,) $(call cc-disable-warning,unused-function,) $(call cc-disable-warning,unused-variable,)

endif

- Finally we build the kernel:

linux-w49x:~/kernel # ARCH=arm CROSS_COMPILE=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/arm-none-eabi- make

- Time to grab a BIG 0xCOFFEE :)

5. Build a new boot image

===========================

- Collect the new kernel:

linux-w49x:~ # cp ~/kernel/arch/arm/boot/zImage my_kernel.img

- Pack a new ramdisk (or just take exsiting one)

linux-w49x:~ # cd ramdisk

linux-w49x:~/ramdisk # find . | cpio --quiet -H newc -o | gzip > ../my_ramdisk.img.gz

linux-w49x:~/ramdisk # cd ..

- Pack everything together using mkelf.py from sony (URL, see above):

linux-w49x:~ # python mkelf.py -o my_boot.img my_kernel.img@0x00208000 my_ramdisk.img.gz@0x01400000,ramdisk sony_cmdline.img@0x0,cmdline

6. Flash the new boot image to the phone

==========================================

- power off device

- vol up + attach usb = fastboot

linux-w49x:~ # fastboot flash boot ./my_boot.img

linux-w49x:~ # fastboot reboot

- If anything goes wrong you can always flash the extraced image using:

linux-w49x:~ # fastboot flash boot ./sony_image.img

linux-w49x:~ # fastboot reboot

7. Now for the FUN part: TUNE the sony kernel with the attached patch-file

=============================================================================

- Basically we disable "module support" as we have none

- Disable A LOT OF DEBUG: debugfs and various debug statments in MSM drivers

- Tune vibration period to be more gentle

- Optimize kernel size for speed and not for size

- Remove kernel and user space process debug infos

- Make sure you unpack the stock sony kernel sources. The sources need to be

fresh and clean!!!

linux-w49x:~ # tar -xf 11.2.A.0.21.tar.bz2

linux-w49x:~ # cd kernel/

- patch the performance tweaks to it. The perf_tweak.patch is appended to this post:

linux-w49x:~/kernel # patch -p3 < ../perf_tweak.patch

patching file ./kernel/power/earlysuspend.c

patching file ./Makefile

patching file ./arch/arm/mach-msm/smd_pkt.c

patching file ./arch/arm/mach-msm/sdio_cmux.c

patching file ./arch/arm/mach-msm/reset_modem.c

patching file ./arch/arm/mach-msm/qdsp5v2/mi2s.c

patching file ./arch/arm/mach-msm/qdsp5v2/audio_out.c

patching file ./arch/arm/mach-msm/modem_notifier.c

patching file ./arch/arm/mach-msm/msm_cpr-debug.c

patching file ./arch/arm/mach-msm/smd_rpcrouter.c

patching file ./arch/arm/mach-msm/ipc_router.c

patching file ./arch/arm/mach-msm/bam_dmux.c

patching file ./arch/arm/mach-msm/qdsp6/msm_q6vdec.c

patching file ./arch/arm/mach-msm/rmt_storage_client.c

patching file ./arch/arm/mach-msm/include/mach/debug_mm.h

patching file ./arch/arm/mach-msm/clock.c

patching file ./arch/arm/mach-msm/sdio_dmux.c

patching file ./arch/arm/mach-msm/msm_cpr.h

patching file ./arch/arm/mach-msm/qdsp5/audio_mp3.c

patching file ./arch/arm/mach-msm/qdsp5/audmgr.c

patching file ./arch/arm/mach-msm/qdsp5/audio_acdb.c

patching file ./arch/arm/mach-msm/qdsp5/audio_lpa.c

patching file ./arch/arm/mach-msm/pm2.c

patching file ./arch/arm/mach-msm/sdio_ctl.c

patching file ./arch/arm/mach-msm/clock-debug.c

patching file ./arch/arm/mach-msm/board-msm7627a-display.c

patching file ./arch/arm/mach-msm/vreg.c

patching file ./arch/arm/mach-msm/board-tamsui-jlo.c

patching file ./arch/arm/mach-msm/clock.h

patching file ./net/netfilter/xt_socket.c

patching file ./include/linux/vibrator_class.h

patching file ./include/linux/bma250.h

patching file ./drivers/media/common/tuners/xc4000.c

patching file ./drivers/tty/serial/msm_serial_hs.c

patching file ./drivers/vibrators/fih_vibrator.c

patching file ./drivers/vibrators/vibrator_class.c

patching file ./drivers/video/msm/msm_fb.c

patching file ./drivers/video/msm/mipi_orise.c

patching file ./drivers/bluetooth/bluesleep.c

patching file ./drivers/usb/otg/msm_otg.c

patching file ./drivers/usb/otg/msm72k_otg.c

patching file ./drivers/usb/gadget/f_diag.c

patching file ./drivers/usb/gadget/u_ctrl_hsuart.c

patching file ./drivers/usb/gadget/f_rmnet_smd_sdio.c

patching file ./drivers/usb/gadget/u_serial.c

patching file ./drivers/usb/gadget/u_bam.c

patching file ./drivers/usb/gadget/f_rmnet_smd.c

patching file ./drivers/input/keyboard/fih_gpio_keys.c

patching file ./drivers/input/keyboard/fih_power_key.c

patching file ./drivers/input/touchscreen/cyttsp_core.c

patching file ./drivers/input/sensor/qpdss702.c

patching file ./drivers/leds/fih_led.c

patching file ./drivers/net/wireless/bcmdhd/wl_linux_mon.c

patching file ./drivers/net/wireless/bcmdhd/Makefile

patching file ./drivers/net/wireless/bcmdhd/dhd_custom_gpio.c

patching file ./drivers/power/fih_bq27520_fuelgauger.c

patching file ./drivers/power/fih_msm_battery.c

patching file ./drivers/gpu/msm/adreno_postmortem.c

patching file ./drivers/gpu/msm/adreno.c

patching file ./drivers/rtc/rtc-msm.c

patching file ./.config

linux-w49x:~/kernel # ARCH=arm CROSS_COMPILE=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/arm-none-eabi- make

===> repeat steps 5) and 6) but use our new zImage.

A: Howto build a patch file by comparing a

fresh extracted kernel sources with your edited sources:

=========================================================

- Compare two kernel directories and create a patch from it:

linux-w49x:~ # export BASEDIR=$PWD

linux-w49x:~ # cd kernel/

linux-w49x:~/kernel # rm $BASEDIR/perf_tweak.patch

linux-w49x:~/kernel # find -name '*.c' -o -name '*.h' -o -name 'Makefile' -o -name '.config' | xargs -I@ diff -upN $BASEDIR/kernel/@ $BASEDIR/my_kernel/@ >> $BASEDIR/perf_tweak.patch