[TUTS/DEVELOPMENT] Kernel Development

Please Vote Accordingly


  • Total voters
    154

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Kernel Development Tutorials

Important Links:
Ubuntu - http://www.ubuntu.com/download/ubuntu/download
Sourcery G++ Lite 2011.03-42 for ARM EABI - http://www.codesourcery.com/sgpp/lite/arm/portal/release1802
Stock Samsung Source - https://github.com/dharamg3/kernel_stock_g3 OR https://opensource.samsung.com/
Github for the Tutorials - https://github.com/dharamg3/g3_tuts

Source of Other Kernels to Compare:
G3MOD - Galaxy 3 - https://github.com/dharamg3/G3MOD
FuguMod - Galaxy 3 - https://github.com/sdadier/froyo

  • I will explain everything Step by Step.
  • If you got time to learn/develop join in.
  • If you get some step working and a fellow developer cant get it, please help each other by answering everyone's doubts.I cant reply to each and every query always.
  • Most of the code is to be run in the terminal. So I wont be mentioning to Open terminal every time.
  • I would like to thank marcellusbe who was the first one to make a proper custom Kernel for G3, who taught me how to build a kernel, and also lot of other help.

Note:
  • Please do not post useless messages.
  • If you dont know coding, please learn that first.
  • Do not flood my inbox with PM's. I will not respond to PM's regarding Kernel Development. Please post in the thread.
    [*]I AM NOT RESPONSIBLE IF YOU HARDBRICK YOUR PHONE, DOING ANY OF THE FOLLOWING STEPS
 
Last edited:

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Install Ubuntu(x86/x64)

1. Install a virtual machine software.(VMware/Virtualbox)
2. Create a new Virtual Machine.
3. Use Ubuntu ISO to install. Download from here - http://www.ubuntu.com/download/ubuntu/download - I use Ubuntu 10.10 x86
4. Allocate enough space for the Virtual machine as the source is itself around 500mb for Kernel. And if you are like me, then you will want to keep backups at each stage.
5. Recommended RAM is 1GB.
6. Let Ubuntu be installed (VMware and Virtualbox have Easy Install which installs Ubuntu itself)

Logging In As Root

1. When Ubuntu first boots, login with the username and password you mentioned while installing.
2. Open Terminal
3. Use following command.(Always the code after $/# is the one to be used. The one before that just specifies [email protected]:~)
## Anything after ## is a comment by me.
Code:
[email protected]:~$ sudo passwd
[sudo] password for dharammaniar: ## Enter password which you used to login.
Enter new UNIX password: ## Enter Password for root(it can be same as your prev password)
Retype new UNIX password: ## Re-Enter Password for root
passwd: password updated successfully
[email protected]:~$ su
Password: ## Enter the root password
[email protected]:/home/dharammaniar#
4. Once you get the last line as above means you now have root access.
5. Now logout from this user and login with username as root
6. On starting Terminal now, you should get
Setting Up Build Environment

1. You should always be running as root.
2. Go To System->Administration->Synaptic Package Manager
3. Search for ncurses-devel
4. Install libncurses5-dev.
5. Go to http://www.codesourcery.com/sgpp/lite/arm/portal/release1802
6. Download IA32 GNU/Linux Installer. Keep the arm-2011.03-42-arm-none-eabi.bin file on the desktop.(I like to keep everything on the desktop for faster access)
7. Run code and select NO
Code:
dpkg-reconfigure -plow dash
8. Run Installer using code
Code:
apt-get install ia32-libs ## FOR THOSE WHO HAVE x64 Ubuntu installed
/bin/sh /root/Desktop/arm-2011.03-42-arm-none-eabi.bin  ## THANKS TO cdesai for the tip :)
9. Do the usual Next Next Next Next.Dont change any paths.Install as typical.
10. Wait till it is installing
11. Wait more :D
12. Still waiting :eek:
13. Again Next -> Done.
14. You now have the development environment ready. :)

Downloading Sources

1. Download Samsung Source from https://opensource.samsung.com/ or https://github.com/dharamg3/kernel_stock_g3
2. If you download from https://opensource.samsung.com/ then you will not have the initramfs.
3. To get stock initramfs, you need to extract it from one of the stock kernel(I will explain it later).
 
Last edited:

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Extracting initramfs from zImage

Download Files from the attachments.

1. Extract files from initramfs-utils.zip to /bin/
2. Make a folder on the Desktop named unpack.
3. Extract the repack-zImage.sh file from repack-zImage.v6.zip in the unpack folder on Desktop.
4. Add your zImage from which you want to extract initramfs in the unpack folder.
5. Run Command
Code:
cd /root/Desktop/unpack
sh repack-zImage.sh -u
6. You will get the unpacked zImage in the same folder which will contain the initramfs folder also.

Note: To make things simple, i use a script to unpack.
Download from attachment.
Add this unpack.sh file to your unpack folder and run it by double clicking :)

Building Kernel

1. Download Source as mentioned in the above tutorial.
2. Create a folder on the Desktop. I have named it g3.
3. Add initramfs folder and Kernel folder in that folder.
4. Run Code to make initramfs.cpio
Code:
cd initramfs
find . -print0 | cpio --null -ov --format=newc > ../initramfs.cpio
5. You can also use the script named initramfs.sh from attachments. Copy it to the g3 folder and run it by double clicking.
6. By doing step 4 or 5 you will get initramfs.cpio in the g3 folder.
7. Next you need to do is modify the makefile in Kernel folder.

Original is
Code:
ARCH		:= arm
CROSS_COMPILE   := /opt/toolchains/arm-2009q3/bin/arm-none-linux-gnueabi-
Change to
Code:
ARCH		:= arm
CROSS_COMPILE   := /root/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-
8. Now you need the configuration file.
9. Copy the apollo_rev_02_android_defconfig file from /Kernel/arch/arm/configs to /Kernel/
10. Rename it to .config (Note: It should be only .config and not apollo_rev_02_android_defconfig.config)
11. Now in Terminal run the following code.
Code:
cd /root/Desktop/g3/Kernel
make menuconfig
12. You will get a GUI menu.

13. Go to General Setup
14. Next go to Initramfs source file(s)

15. Press Enter and give path as
Code:
../initramfs.cpio

16. Exit from menuconfig
17. Enter Code
Code:
make
OR

Code:
make -j4 ## THIS ONE ONLY IF YOU GOT A GOOD CPU THAT SUPPORTS MULTI-THREADING :)
18. Building will start. There will be many warnings. Ignore them.
19. After building is finished, you will get zImage at /Kernel/arch/arm/boot/
20. Add this zImage to a test.tar(attachment) using Total Commander.
21. Flash as One Package in Odin.
22. Enjoy :)

Note: This is the stock kernel and supports only rfs filesystem. So before flashing it, please change filesystem to rfs and then flash.
 

Attachments

Last edited:

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Kernel Basics​

The Kernel is basically divided into 2 parts.
The Kernel and the initramfs.Those are the two folders in your g3folder.

initramfs
initramfs is a root filesystem which is embedded into the kernel and loaded at an early stage of the boot process. It is the successor of initrd. It provides early userspace which lets you do things that the kernel can't easily do by itself during the boot process.
 
Last edited:

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Kernel Modding - Part 1

PLEASE DONT ASK HOW OR WHY ANY PART IS DONE. I WILL EXPLAIN ALL THAT I KNOW. BUT SOME PART OF THIS HAS BEEN DONE BY TRIAL AND ERROR METHOD, WHEREAS SOME PART MAYBE OUT OF MY SCOPE OF KNOWLEDGE.

Removing The Sound Click Bug During Calls

We all know that this is the most basic bug found out in the Kernel of Samsung. The clicking sound we get during calls. Many of you may complain that this bug is solved in the newer ROM's, but remember we are building from the original Samsung Source, which is modified in the newer kernel's. We dont have those modifications so here it goes, the Sound Click Bug.

We to find out this bug you need to check the dmesg when you are in a call.
It shows you that there is some sound routing done during the call. So you know that this is something related to the sound driver.

Now the sound driver for Galaxy 3 is the wm8994 driver, which is present at Kernel/sound/soc/codecs/

So the changes to be made are in the file wm8994.c
In the function static int wm8994_resume(struct platform_device *pdev)
Originally the code was
Code:
if (wm8994_power == 0) {
		audio_power(1); /* Board Specific function */
		wm8994_power = 1;
	}
wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
wm8994_enable_path(codec, wm8994_curr_path);
After editing the code is

Code:
if (wm8994_power == 0) {
		audio_power(1); /* Board Specific function */
		wm8994_power = 1;
                wm8994_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
                wm8994_enable_path(codec, wm8994_curr_path);
}
1% Battery MOD

The battery driver for galaxy 3 is found at Kernel/drivers/power/
The file to be added is s5p6442_battery.c
The Samsung battery driver originally divides battery in the following Steps:
100,90,80,70,60,50,40,30,20,15,5,3,1
If you look at the source code, you should be able to find that out easily.
We need to make it return each value in between also so that we get the 1% MOD that is required.

So now, the original code was
Code:
if (bat_vol > batt_full)
	{
		int temp = (batt_max - batt_full);
		if (bat_vol > (batt_full + temp) || 
				s3c_bat_info.bat_info.batt_is_full)
			bat_level = 100;
		else
			bat_level = 90;

#ifdef __CHECK_CHG_CURRENT__
		if (s3c_bat_info.bat_info.charging_enabled) {
			check_chg_current(bat_ps);
			if (!s3c_bat_info.bat_info.batt_is_full)
				bat_level = 90;
		}
#endif /* __CHECK_CHG_CURRENT__ */
		dev_dbg(dev, "%s: (full)level = %d\n", __func__, bat_level );
	}
	else if (batt_full >= bat_vol && bat_vol > batt_almost)
	{
		int temp = (batt_full - batt_almost) / 2;
		if (bat_vol > (batt_almost + 86))
			bat_level = 80;
		else
			bat_level = 70;

		dev_dbg(dev, "%s: (almost)level = %d\n", __func__, bat_level);
	}
	else if (batt_almost >= bat_vol && bat_vol > batt_high)
	{
		int temp = (batt_almost - batt_high) / 2;
		if (bat_vol > (batt_high + 62))
			bat_level = 60;
		else
			bat_level = 50;
		dev_dbg(dev, "%s: (high)level = %d\n", __func__, bat_level );
	}
	else if (batt_high >= bat_vol && bat_vol > batt_medium)
	{
		int temp = (batt_high - batt_medium) / 2;
		if (bat_vol > (batt_medium + 26))
			bat_level = 40;
		else
			bat_level = 30;
		dev_dbg(dev, "%s: (med)level = %d\n", __func__, bat_level);
	}
	else if (batt_medium >= bat_vol && bat_vol > batt_low)
	{
		int temp = (batt_medium - batt_low) / 2;
		if (bat_vol > (batt_low + 50))
			bat_level = 20;
		else
			bat_level = 15;		
		dev_dbg(dev, "%s: (low)level = %d\n", __func__, bat_level);
	}
	else if (batt_low >= bat_vol && bat_vol > batt_critical)
	{
		bat_level = 5;
		dev_dbg(dev, "%s: (cri)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_critical >= bat_vol && bat_vol > batt_min)
	{
		bat_level = 3;
		dev_info(dev, "%s: (min)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_min >= bat_vol && bat_vol > batt_off)
	{
		bat_level = 1;
		dev_info(dev, "%s: (off)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_off >= bat_vol)
	{
		bat_level = 0;
		dev_info(dev, "%s: (off)level = %d, vol = %d", __func__,
				bat_level, bat_vol);
	}
We need to edit it to make

Code:
if (bat_vol > batt_full)
	{
		int temp = (batt_max - batt_full);
		if (bat_vol > (batt_full + temp) ||
				s3c_bat_info.bat_info.batt_is_full)
			bat_level = 100;
		else
		{
		        bat_level = 90 + (((bat_vol - batt_full) *10) / temp);
		}

#ifdef __CHECK_CHG_CURRENT__
		if (s3c_bat_info.bat_info.charging_enabled) {
			check_chg_current(bat_ps);
			if (!s3c_bat_info.bat_info.batt_is_full)
				bat_level = 90;
		}
#endif /* __CHECK_CHG_CURRENT__ */
		dev_dbg(dev, "%s: (full)level = %d\n", __func__, bat_level );
	}
	else if (batt_full >= bat_vol && bat_vol > batt_almost)
	{
		int temp = (batt_full - batt_almost) / 2;
		bat_level = 70 + (((bat_vol - batt_almost) *10) / temp);

		dev_dbg(dev, "%s: (almost)level = %d\n", __func__, bat_level);
	}
	else if (batt_almost >= bat_vol && bat_vol > batt_high)
	{
		int temp = (batt_almost - batt_high) / 2;
		bat_level = 50 + (((bat_vol - batt_high) *10) / temp);
		dev_dbg(dev, "%s: (high)level = %d\n", __func__, bat_level );
	}
	else if (batt_high >= bat_vol && bat_vol > batt_medium)
	{
		int temp = (batt_high - batt_medium) / 2;
		bat_level = 30 + (((bat_vol - batt_medium) *10) / temp);
		dev_dbg(dev, "%s: (med)level = %d\n", __func__, bat_level);
	}
	else if (batt_medium >= bat_vol && bat_vol > batt_low)
	{
		int temp = (batt_medium - batt_low) / 2;
		bat_level = 15 + ((( bat_vol - batt_low) *5) / temp);
		dev_dbg(dev, "%s: (low)level = %d\n", __func__, bat_level);
	}
	else if (batt_low >= bat_vol && bat_vol > batt_critical)
	{
		bat_level = 5;
		dev_dbg(dev, "%s: (cri)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_critical >= bat_vol && bat_vol > batt_min)
	{
		bat_level = 3;
		dev_info(dev, "%s: (min)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_min >= bat_vol && bat_vol > batt_off)
	{
		bat_level = 1;
		dev_info(dev, "%s: (off)level = %d, vol = %d\n", __func__,
				bat_level, bat_vol);
	}
	else if (batt_off >= bat_vol)
	{
		bat_level = 0;
		dev_info(dev, "%s: (off)level = %d, vol = %d", __func__,
				bat_level, bat_vol);
	}
and also we need to change the Total ADC count

Originally

Code:
#define ADC_TOTAL_COUNT    20
Change it to

Code:
#define ADC_TOTAL_COUNT    100
Voodoo Sound Driver
The Sound Driver for Galaxy 3 is the wm8994 Wolfson Microelectronics Codec.
The WM8994 is a highly integrated ultra-low power hi-fi CODEC designed for smartphones and other portable devices rich in multimedia features.

Features
  • Hi-Fi 24-bit 4-channel DAC and 2-channel ADC
  • 100dB SNR during stereo DAC playback (‘A’ weighted)
  • Smart MIC interface
    - Power, clocking and data input for up to four digital MICs
    - High performance analogue MIC interface
    - MIC activity monitor & interrupt allows processor to sleep
  • 1W stereo / 2W mono class D/AB speaker driver
  • Capless class W headphone drivers
    - Integrated charge pump
    - 6mW total power for DAC playback to headphones
  • 4 line outputs (single-ended or differential)
  • BTL earpiece driver
  • Digital audio interfaces for multi-processor architecture
    - Asynchronous stereo duplex sample rate conversion
    - Powerful mixing and digital loopback functions
  • ReTune™ Mobile 5-band, 6-channel parametric EQ
  • Programmable dynamic range controller
  • Dual FLL provides all necessary clocks
    - Self-clocking modes allows processor to sleep
    - All standard sample rates from 8kHz to 96kHz
  • Active noise reduction circuits
    - DC offset correction removes pops and clicks
    - Ground loop noise cancellation
  • Integrated LDO regulators
  • 72-ball W-CSP package (4.511mm x 4.023mm x 0.7mm)

The Voodoo Driver is developed by supercurio.
The changes to be made for the voodoo driver are at sound/soc/codec/
The Changes are as the following Commit. (Please ignore the /Kernel/scripts part....I forgot to do a make clean -i before commiting :eek:)

RootADB and BusyBox

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:

  • A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
  • A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
  • A daemon, which runs as a background process on each emulator or device instance.
You can find the adb tool in <sdk>/platform-tools/.
RootADB gives you root access while performing adb functions.

BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc. The utilities in BusyBox generally have fewer options than their full-featured GNU cousins; however, the options that are included provide the expected functionality and behave very much like their GNU counterparts. BusyBox provides a fairly complete environment for any small or embedded system.

BusyBox has been written with size-optimization and limited resources in mind. It is also extremely modular so you can easily include or exclude commands (or features) at compile time. This makes it easy to customize your embedded systems. To create a working system, just add some device nodes in /dev, a few configuration files in /etc, and a Linux kernel.

The Changes to be made for these are in the initramfs.
These can be found in this Commit.
 
Last edited:

Dharam_Maniar

Retired Recognized Developer
Nov 24, 2010
2,555
3,009
0
30
Kernel Modding - Part 2

PLEASE DONT ASK HOW OR WHY ANY PART IS DONE. I WILL EXPLAIN ALL THAT I KNOW. BUT SOME PART OF THIS HAS BEEN DONE BY TRIAL AND ERROR METHOD, WHEREAS SOME PART MAYBE OUT OF MY SCOPE OF KNOWLEDGE.

File System Support: rfs/ext2/ext3/ext4

RFS
SAMSUNG RFS (Robust File System) is an embedded flash file system that uses SAMSUNG OneNAND flash memory as storage on any consumer electronic devices.
SAMSUNG RFS runs in the Linux kernel and is fully compatible with FAT file system standards (FAT16/32).

EXT2
http://en.wikipedia.org/wiki/Ext2

EXT3
http://en.wikipedia.org/wiki/Ext3

EXT4
http://en.wikipedia.org/wiki/Ext4

[DEV] Debate: ext2 vs ext3 vs ext4 : http://forum.xda-developers.com/showthread.php?t=800353

[REF] Information About Filesystems : http://forum.xda-developers.com/showthread.php?t=1058095

The changes to be done are as in the commit.
 
Last edited:

rudolf895

Inactive Recognized Developer
Oct 11, 2010
2,503
631
0
IND
Too many reserved post as usual :D(all you need is one post to explain all :p)
Anyways I'll make videos on this soon..
all the users need to know is to install Ubuntu by themself.. everything else will be covered in the video.
 
  • Like
Reactions: [email protected]

FadeFx

Senior Member
Mar 22, 2010
8,838
3,034
0
Vienna
Too many reserved post as usual :D(all you need is one post to explain all :p)
Anyways I'll make videos on this soon..
all the users need to know is to install Ubuntu by themself.. everything else will be covered in the video.
the big advantage of having multiple posts reserved is that you can have som kind of directory on the first post linking to the sections in the other posts, this is of big use in a massive theme like kernel development. also i do not see why nobody does this here as of now, always they reserve multiple posts and nobody links them in first post, what makes the use of them obsolete...