Building Samsung Open-Source Kernel and disable DEFEX

Search This thread

cocacola2015

Member
Nov 2, 2015
24
15
I'm going to show you how to build a custom kernel, and a custom boot.img.

Requirements

Hypothetical workspace directory on the filesystem: /workspace, now prepare it like this:
/workspace/kernel - this is where the kernel source code will be, this is what we will build. Extract the downloaded Kernel.tar.gz here
/workspace/build - this is the kernel compilation result, populated by the build
/workspace/toolchain - this is the required cross-compilation toolchain you download or check-out from the google link
/workspace/kitchen - Extract Android Image Kitchen here

Go to http://opensource.samsung.com/reception.do and search for SM-J415, download one of the results, extract Kernel.tar.gz to /workspace/kernel. I believe SWA stands for South West Asia, and MAE - Middle-east Africa, it doesn't matter which you pick, it is related to radio regulations.

Now overwrite the file /workspace/kernel/build_kernel.sh with:
Code:
#!/bin/bash

# The cross compilation toolchain path
export TOOLCHAIN=$(pwd)/../toolchain/arm-linux-androideabi-4.8
# This is the directory for the compiled kernel
export OUTDIR="O=$(pwd)/../build"

export PATH=$TOOLCHAIN/bin:$PATH
export ARCH=arm
export CROSS_COMPILE=arm-linux-androideabi-
export THREADS=$(nproc --all)
export COMMON_ARGS="-j$THREADS $OUTDIR arch=arm CFLAGS_MODULE=-fno-pic arch=arm"
if [ "$1" == "build" ]; then
        make $COMMON_ARGS j4primelte_sea_open_defconfig
        make $COMMON_ARGS
elif [ "$1" == "rebuild" ]; then
        make $COMMON_ARGS
elif [ "$1" == "clean" ]; then
        make $COMMON_ARGS distclean
        make $COMMON_ARGS clean
else
        echo "./build_kernel.sh build|rebuild|clean"
fi

Building kernel source code
Run the script:
$ cd /workspace/kernel/
edit: /workspace/kernel/arch/arm/configs/j4primelte_sea_open_defconfig
change CONFIG_SECURITY_DEFEX=y to CONFIG_SECURITY_DEFEX=n
$ bash build_kernel.sh build

It should build normally, if it fails there's something wrong with your OS setup. After a long time, you should see the compiled and compressed kernel with the DTP appended at:
/workspace/target/arch/arm/boot/zImage-dtb
The kernel configuration it created from the defconfig files in the kernel source tree is at
/workspace/target/.config

Build a new boot.img
$ cd /workspace/kitchen
$ bash unpackimg.sh /path/to/a/boot/or/recovery.img
Now you will have the unpacked kernel in: /workspace/kitchen/split_img/boot.img-zImage
Delete it
$ rm split_img/boot.img-zImage
Link the built custom kernel there instead
$ ln -s /workspace/target/arch/arm/boot/zImage-dtb /workspace/kitchen/split_img/boot.img-zImage
Now each time you create the boot.img, it will include your custom kernel instead.
Tweak the files and ramdisk as much as you want, and repackage the boot.img
$ bash repackimg.sh
Now you have a boot.img at /workspace/kitchen/image-new.img that is ready to flash to the device. You can unpack custom recoveries the same way as you unpacked boot.img to make them use your custom kernel.

Kernel configurations tried
CONFIG_SECURITY=n - boot loop
CONFIG_SECURITY_SELINUX=n - boot loop
CONFIG_SECURITY_DEFEX=n - works
CONFIG_DM_VERITY=n - works, does not prevent initramfs from using DM-VERITY, you still need some sort of ramdisk hack to disable verification of the next boot phase after initrd.

Often when editing the defconfig files, the same variables are declared in many different files so you might be better off using "sed' to change the variables, example:
$ grep -lr "CONFIG_SECURITY=y" | while read line; do sed -i 's/CONFIG_SECURITY=y/CONFIG_SECURITY=n/g' $line; done
When running "build_kernel.sh build", it will print "configuration written to .config" so verify that the variable was actually changed in the final config /workspace/build/.config

:eek:
 
Last edited:
  • Like
Reactions: speed_rapide

Geckyn

Senior Member
Jun 6, 2018
140
121



The kernel source code is on the Samsung opensource website.... there are two versions one that is MEA ( for Middle East and Africa roms) and the other one for SWA. It works if compiled properly
 

Attachments

  • 20190219_215043.jpg
    20190219_215043.jpg
    121.7 KB · Views: 443

cocacola2015

Member
Nov 2, 2015
24
15
ok
i have questions loop device depend on kernel and if it is .
How to add support?

Type "make xconfig" in the kernel directory, and a window will open for configuring the .config file in that same directory.

Search for "Loopback device support" and add a checkmark (not a dot, so that the module is built into the kernel.)


Can you please provide a log or something? It sounds like you are missing dependencies in your operating system for building kernels.
 

ashyx

Inactive Recognized Contributor
Oct 14, 2012
15,055
9,947
This, I solved it yesterday, Thanks .
But I have 2 problems :
1- Device is arm and at bulid_kernel.sh tell me to use toolchain arch64 ,
Which I should Use arm or arm64 ,
I confused as cpu is arm64 .

https://www.qualcomm.com/products/snapdragon/processors/425


Or

Ndk

https://developer.android.com/ndk/downloads/index.html

2- Which command I should write after menuconfig

./build_kernel.sh
Or
make -jX .
Just use whichever is in the build script.

You will need to add menuconfig to build_kernel.sh before make or your changes will be lost.
Then run build_kernel.sh
 

ashyx

Inactive Recognized Contributor
Oct 14, 2012
15,055
9,947
@ashyx ,all is ok .
The error from clang and there is 2 config files .

Fixed and I will test kernel but I have problem when compiling I choose lz4 type ,do U see I should choose another .
Also where is zimage now ,i compiled manually not with build_kernel.sh .
You don't need the export arguments which are contradictory anyway, as you have already defined your toolchain and architecture before hand.

Also the boot image does not need to be lz4. The compiler will tell you where the finished zImage is when completed. You should find it in the boot directory of the arm64 directory if you are not using OUT_DIR statements.
 
Last edited:

ashyx

Inactive Recognized Contributor
Oct 14, 2012
15,055
9,947
Sorry ashyx this is last thing ,
-You told me later that device is arm not arm64 .
In Your twrp thread .
-Also defconfig of device in /arch/arm .
-Arch=arm in build_kernel.sh .
-Gsi system armaonly only work on the device .
-All apps told that device is arm .

I confused ,
Please tell that it is right to use arm64 tool chain .
Or How did U build it ?

By arm64 toolchain or arm toolchain ?
Very Thank U .
I was just going by the screen shot you posted. Like I said your commands are contradictory.
You have both arm and arm64 toolchains defined in the same script.
You also have an export statement for arm64 directly under a statement for an arm toolchain.
Not sure why you added both?

As far as I can see the architecture you're compiling for is arm, so you need an arm toolchain.
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    I'm going to show you how to build a custom kernel, and a custom boot.img.

    Requirements

    Hypothetical workspace directory on the filesystem: /workspace, now prepare it like this:
    /workspace/kernel - this is where the kernel source code will be, this is what we will build. Extract the downloaded Kernel.tar.gz here
    /workspace/build - this is the kernel compilation result, populated by the build
    /workspace/toolchain - this is the required cross-compilation toolchain you download or check-out from the google link
    /workspace/kitchen - Extract Android Image Kitchen here

    Go to http://opensource.samsung.com/reception.do and search for SM-J415, download one of the results, extract Kernel.tar.gz to /workspace/kernel. I believe SWA stands for South West Asia, and MAE - Middle-east Africa, it doesn't matter which you pick, it is related to radio regulations.

    Now overwrite the file /workspace/kernel/build_kernel.sh with:
    Code:
    #!/bin/bash
    
    # The cross compilation toolchain path
    export TOOLCHAIN=$(pwd)/../toolchain/arm-linux-androideabi-4.8
    # This is the directory for the compiled kernel
    export OUTDIR="O=$(pwd)/../build"
    
    export PATH=$TOOLCHAIN/bin:$PATH
    export ARCH=arm
    export CROSS_COMPILE=arm-linux-androideabi-
    export THREADS=$(nproc --all)
    export COMMON_ARGS="-j$THREADS $OUTDIR arch=arm CFLAGS_MODULE=-fno-pic arch=arm"
    if [ "$1" == "build" ]; then
            make $COMMON_ARGS j4primelte_sea_open_defconfig
            make $COMMON_ARGS
    elif [ "$1" == "rebuild" ]; then
            make $COMMON_ARGS
    elif [ "$1" == "clean" ]; then
            make $COMMON_ARGS distclean
            make $COMMON_ARGS clean
    else
            echo "./build_kernel.sh build|rebuild|clean"
    fi

    Building kernel source code
    Run the script:
    $ cd /workspace/kernel/
    edit: /workspace/kernel/arch/arm/configs/j4primelte_sea_open_defconfig
    change CONFIG_SECURITY_DEFEX=y to CONFIG_SECURITY_DEFEX=n
    $ bash build_kernel.sh build

    It should build normally, if it fails there's something wrong with your OS setup. After a long time, you should see the compiled and compressed kernel with the DTP appended at:
    /workspace/target/arch/arm/boot/zImage-dtb
    The kernel configuration it created from the defconfig files in the kernel source tree is at
    /workspace/target/.config

    Build a new boot.img
    $ cd /workspace/kitchen
    $ bash unpackimg.sh /path/to/a/boot/or/recovery.img
    Now you will have the unpacked kernel in: /workspace/kitchen/split_img/boot.img-zImage
    Delete it
    $ rm split_img/boot.img-zImage
    Link the built custom kernel there instead
    $ ln -s /workspace/target/arch/arm/boot/zImage-dtb /workspace/kitchen/split_img/boot.img-zImage
    Now each time you create the boot.img, it will include your custom kernel instead.
    Tweak the files and ramdisk as much as you want, and repackage the boot.img
    $ bash repackimg.sh
    Now you have a boot.img at /workspace/kitchen/image-new.img that is ready to flash to the device. You can unpack custom recoveries the same way as you unpacked boot.img to make them use your custom kernel.

    Kernel configurations tried
    CONFIG_SECURITY=n - boot loop
    CONFIG_SECURITY_SELINUX=n - boot loop
    CONFIG_SECURITY_DEFEX=n - works
    CONFIG_DM_VERITY=n - works, does not prevent initramfs from using DM-VERITY, you still need some sort of ramdisk hack to disable verification of the next boot phase after initrd.

    Often when editing the defconfig files, the same variables are declared in many different files so you might be better off using "sed' to change the variables, example:
    $ grep -lr "CONFIG_SECURITY=y" | while read line; do sed -i 's/CONFIG_SECURITY=y/CONFIG_SECURITY=n/g' $line; done
    When running "build_kernel.sh build", it will print "configuration written to .config" so verify that the variable was actually changed in the final config /workspace/build/.config

    :eek:
    1
    and I think ,They aren't kernel see

    No that is the latest kernel source code running on the latest firmware. You can use either of those 2 downloads from opensource.samsung.com
    1
    Thanks!! :D
    I ended up using https://xdaforums.com/showthread.php?t=2446269 which is pretty easy as well.

    I am now stuck on how to enable wifi after flashing a different kernal.
    Kernal = samsung opensource
    Rom = nouget 7.1.1 (different to opensource kernal)

    Any suggestions?
    Install the Magisk module LIBSECURE_STORAGE COMPANION