[Android Software Development] Dark Wraith:
How to relate it to Android and use it to modify kernels/Roms/apks etc​

​

Android is basically an operating system for phones based on a linux kernel and it is an open source operating system.

>What does open source mean?

-It means that the files that were required for building or compiling android are available for you to view and modify accordingly. So the main advantage is that you can build your own android OS and kernel from scratch,adding modifications that are sometimes not available in stock android OS.

-Now I wont go through the tutorial to build kernel or Os from source(There are many tutorials here regarding that)

-If you download the source code for your phone or pure Android code by google(also known as vanilla code), You will see that it is built on C language. You will see tons of C files and .H files(header files).

What happens with those C files and why is source code so big?

-Well at the beginning of the tutorial i had told you that compiling a program file gives you an output in computer language that is flashed on your device's ROM(read only memory).

-So you must have heard during kernel compilation about compiling zImage.zImage is nothing but the output after compiling your kernel source code.zImage is then packed with ramdisk and you get your desired boot.img for flashing.

>What is a cross compiler toolchain?

-Toolchain is nothing but an application or a tool that is used to compile your source code. It is known as cross compiler because you are compiling a piece of code present on your computer not meant for your computer but some other device( Your mobile). Hence the term Cross compiler.

Note: When you specify arch=arm while compiling using a toolchain, you specify that the output generated is for your arm device( Your mobile).

>So now what can we do with the source files?

I will take kernel compilation as an example..

Overclocking:

In your source files you will find a file named..acpuclock.c but it differs from mobile to mobile and depends on your mobile's chipset(It is mostly under arch/arm/mach-msm folder...Example for xperia s....(msm 8680 chipset) , it is kernel source/arch/arm/mach-msm/acpuclock-8x60.c

Now you fill find a strucure in C in that file with the name;

Source: Xda university adding features to your kernel

Note: I take this example as it differs from device to device, so the best way would be to take xda's example, you can study other dev's github to learn more about your device

static struct pll pll2_tbl[] = {
{ 42, 0, 1, 0 }, /* 806 MHz */
{ 53, 1, 3, 0 }, /* 1024 MHz */
{ 125, 0, 1, 1 }, /* 1200 MHz */
{ 73, 0, 1, 0 }, /* 1401 MHz */
};

and

static struct clkctl_acpu_speed acpu_freq_tbl[] = {
{ 0, 24576, LPXO, 0, 0, 30720000, 900, VDD_RAW(900) },
{ 0, 61440, PLL_3, 5, 11, 61440000, 900, VDD_RAW(900) },
{ 1, 122880, PLL_3, 5, 5, 61440000, 900, VDD_RAW(900) },
{ 0, 184320, PLL_3, 5, 4, 61440000, 900, VDD_RAW(900) },
{ 0, MAX_AXI_KHZ, AXI, 1, 0, 61440000, 900, VDD_RAW(900) },
{ 1, 245760, PLL_3, 5, 2, 61440000, 900, VDD_RAW(900) },
{ 1, 368640, PLL_3, 5, 1, 122800000, 900, VDD_RAW(900) },
/* AXI has MSMC1 implications. See above. */
{ 1, 768000, PLL_1, 2, 0, 153600000, 1050, VDD_RAW(1050) },
/*
* AXI has MSMC1 implications. See above.
*/
{ 1, 806400, PLL_2, 3, 0, UINT_MAX, 1100, VDD_RAW(1100), &pll2_tbl[0]},
{ 1, 1024000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[1]},
{ 1, 1200000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[2]},
{ 1, 1401600, PLL_2, 3, 0, UINT_MAX, 1250, VDD_RAW(1250), &pll2_tbl[3]},
{ 0 }
};

Now what if you add the following lines to the first structure

{78, 1, 3, 0 }, /* 1516 MHz */
{83, 1, 3, 0 }, /* 1612 MHz */
{88, 1, 3, 0 }, /* 1708 MHz */

and

{ 1, 1516800, PLL_2, 3, 0, UINT_MAX, 1250, VDD_RAW(1250), &pll2_tbl[6]},
{ 1, 1612800, PLL_2, 3, 0, UINT_MAX, 1275, VDD_RAW(1275), &pll2_tbl[7]},
{ 1, 1708800, PLL_2, 3, 0, UINT_MAX, 1300, VDD_RAW(1300), &pll2_tbl[8]},
{ 1, 1804800, PLL_2, 3, 0, UINT_MAX, 1325, VDD_RAW(1325), &pll2_tbl[9]},
{ 0 }
};

So this would add few additional clock speeds to your device like 1516 Mhz,1612 Mhz, 1708 Mhz, 1804 Mhz

But this really differs from device to device and you might wanna study the repository of some dev of your device to know the exact changes....

Same goes for undervolting

Adding Governors

What is a governor: A governor is actually nothing more then a behavior profile for your CPU, the governor will tell the CPU exactly what to do in what situation. The term ‘governor’ has nothing to do with it’s function, imagine it was called CPU Presidents! sounds strange but it’s as normal as using the word Governor.(Source : XDA university) (Couldn't improve on this so posted it here)

How to add a governor??

Simple, get a hold of your governor file( another C file, by the name cpufreq_govname.c)

Now for governors you need to edit your Kconfig file. What is Kconfig file. It is your configuration file that tells you compiler how to compile the source file...

You have to add the lines

config CPU_FREQ_GOV_GOVNAMEHERE
tristate "'gov_name_lowercase' cpufreq governor"
depends on CPU_FREQ
help
governor' - a custom governor!

you can head over to http://xda-university.com/as-a-developer/adding-features-to-your-kernel for further details, I will just tell you why you do it...

After that you have to open makefile and add the line for your governor

obj-$(CONFIG_CPU_FREQ_GOV_Governorname) += cpufreq_governorname.o

#btw check out the fact that we are adding .o file not .c, I will explain this later

Now head over to kernel_source/includes/linux and open cpufreq.h(The header file).. Now you have to add

#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_Governorname)
extern struct cpufreq_governor cpufreq_gov_Governorname;
#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_Governorname)

# is a comment and compiler ignores those...main thing is extern struct line...It just defines the structure name for your governor...

How to add these lines...Well you have to scroll through the file until you find similar lines and see the pattern...You have to add these lines according to the exact pattern or else this will throw and error while compiling

Adding I/O schedulers....

Just like governors, you have to find your I/O scheduler C file and add it to kernel source/block folder

and similarly open Kconfig.iosched(not Kconfig)

and add the lines

Note: Taking SIO(Simple input output scheduler) as an example

config IOSCHED_SIO
tristate "Simple I/O scheduler"
default y
---help---
The Simple I/O scheduler is an extremely simple scheduler,
based on noop and deadline, that relies on deadlines to
ensure fairness. The algorithm does not do any sorting but
basic merging, trying to keep a minimum overhead. It is aimed
mainly for aleatory access devices (eg: flash devices).

Now open makefile and add

obj-$(CONFIG_IOSCHED_SIO) += sio-iosched.o

Have you guys wondered what is this o file instead of C file we are using..?????

Well guys, Thats an object file..It is generated by your toolchain while compiling your C files. What the compiler does is that for each C file, it creates a corresponding object(.o file) and links them together at the end of compilation to make the final executable(In our case zImage)...

so in makefile you dont give the kernel source file(.C), you give the corresponding object file(.O)

>APKs

for Apks you need the android SDK(Standard Development Kit) and some basic knowledge about Java and xml coding. I am not that familiar with developing apks so wont give you wrong info. Maybe someone can add in the thread later and i will add it in the post later....:)

So that's it for now. I hope you find it useful. Please do give your feedbacks(good or bad) so that i can add to this in future

Credits

-XDA Ofcourse...

-Xda university tutorial on Adding features to kernel.Link here

-Thanks to [MENTION=4354408]broodplank1337[/MENTION].. [MENTION=4663391]abcdjdj[/MENTION]. [MENTION=4113493]N00B_IN_N33D[/MENTION] and [MENTION=5260857]karan5chaos[/MENTION] for finding the errors and telling me about the changes that had to be made

Cheers..................;)