[PATCH] Kexec-hardboot patch

Search This thread

Tasssadar

Inactive Recognized Developer
Dec 31, 2010
818
6,128
Brno
tasssadar.github.com
In this post, I would like to explain what kexec-hardboot patch is.

@kernel developers: I would like to ask you to merge this patch to your kernels, because it is essential part of MultiROM - it allows me to boot any kernel without changing the boot partition. I realize that it is no small request, but the patch is not big, touches relatively stable parts of kernel and should not cause any problems. Thank you.

What is kexec?
It is syscall of Linux kernel, which allows you to boot another Linux kernel without restarting the device - "Linux boots itself". The functionality is equivalent to fastboot -c *cmdline* boot zImage initrd.img, but without PC and fastboot. It is fairly known thing, so more info at wikipedia and man kexec.
Standard kexec call unfortunatelly does not work on Nexus 4. It freezes somewhere, and it is very difficult to find out where - probably some of the drivers are not shut down/re-initialized properly, it is a commong thing among Android devices, which is why kexec-hardboot was made.

What is the difference between normal and hardboot exec?
Kexec-hardboot patch adds a real device restart to that process, so that all the drivers can be properly reinitialized. It stores new kernel to RAM, reboots the device as usual, and kernel from boot partition immediately jumps to the one which was stored to RAM before reboot.
Unlike grouper's kexec-hardboot patch, this one only requires the host kernel to be patched. This is one of the improvements I made, and I think it is pretty significant.
To sumarize the process:
  1. kexec --load-hardboot.... is called and kernel it loaded into RAM.
  2. kexec -e is called. Special info is written to memory (to area which is not overwritten on reboot) and the device is rebooted.
  3. After reboot, very early in the boot process, kernel checks if that special info is present in RAM and if so, it loads new kernel from RAM and jumps to it.
  4. Kexecd' kernel starts and boots.
For more info, read the original thread.

Patches:
Kernel patch: https://gist.github.com/Tasssadar/7833796, 4.4 AOSP kernel repo
This is the kernel patch. Only the host kernel needs to be patched.
Related CONFIG options:
  • CONFIG_KEXEC=y
  • CONFIG_KEXEC_HARDBOOT=y
  • CONFIG_PROC_DEVICETREE=y
  • CONFIG_ATAGS_PROC=n # This one is turned on automatically, but it is not needed, so you can disable it.
All these options must be enabled.

Userspace kexec binary: https://github.com/Tasssadar/kexec-tools
I had to change some things in kexec userspace binary because of some kernel bugs, complete description is in that repository. You can get statically built binary at https://github.com/Tasssadar/multirom/blob/master/install_zip/prebuilt-installer/multirom/kexec

Usage:
Once you have the kernel patches and kexec userspace binary in place, just run following command to boot into new kernel:
Code:
kexec --load-hardboot zImage --initrd=initrd.img --mem-min=0x20000000 --command-line="$(cat /proc/cmdline)" --dtb
kexec -e
Note the command line parameter - cmdline from bootloader is not added automatically, you have to put it there by yourself.

Authors:
This patch was made by Mike Kasick for Samsung Epic 4G. Since that, it was ported to several devices, one of them is Asus Transformer TF201 - I used patch from TF201 and modified it a bit (basically just changed few SoC specific constants). People at #ubuntu-arm helped me out with that, thanks.
For hammerhead, I've improved the patch a bit - only the host needs to be patched now and I've added support for DTB.
 
Last edited:

Lostboy2909

Senior Member
Jan 19, 2012
64
1
Mumbai
Help with applying the patch

Okay so I have my aosp kernel made. I have my working directory too. Can you just guide me on how do I apply this partch to it?
 

wulsic

Senior Member
Aug 21, 2013
1,880
2,316
24
Nijmegen
OnePlus 8 Pro
@Anoopnk since the patch is for the kernel(zImage) its not possible to patch it like that. Or you have to compile the kernel or you need to ask your kernel dev friendly or he can include it in his kernel.
 

l8nit3

Member
Dec 20, 2013
13
4
fort erie
Hello, first I'd like to apologize for dredging up an old thread. Second, I am attempting to build a CM13+kali+kexec compatible kernel, with extra wifi drivers as the base kernel doesnt support my wifi card. Based on that goal, I have a multi-part question for the OP, or anyone else with the know-how to assist me in my quest.

1)Is there a source available for your kernel_kexec_hammerhead_cm13-01-2c39db662.zip listed on the multirom page for nexus 5? I'm attempting to build a new kernel, for kali+CM13+multirom, and your android_kernel_google_msm git only has branches going up to CM12. It would be far easier for me to start with a 'clean' CM13+kexec source then trying to patch myself as per question #2

2) I've attempted to patch a plain old hammerhead_defconfig from CM13 git, however I believe I am doing so wrong. I have managed to compile kexec-tools and aquired the hammerhead 3.4.x patch. Now am I supposed to patch my produced zImage-dtb AFTER build, running the patch from the kexec-tools folder and against the zImage-dtb? Is it something like 'KEXEC.patch -p1 < zimage-dtb'

Any clarification on the use of the kexec-tools patch, or simply a link to github source of a 'clean' CM13 kernel with the kexec patch already applied, would be incredibly helpfull on my journey down the rabbit hole :)

~EDIT~
Ok so I've managed to figure out the patch and feel like a dumbass. For anyone else looking to get into kernel dev, and wanting to use the kexec patch, heres the basics:
1- run patch within your kernel source directory
2-ensure that the config settings in OP are set (I had to add them to my source by hand as they werent there)
3-when zipping up your kernel, add the sbin and lib/kexec-tools folders from the kexec-tools source build, and be sure your updater-script / updater-binary is set to move these folders into /sbin and /lib respectively.
4-Flash and enjoy Kexec! (check with multirom manager)
 
Last edited:
  • Like
Reactions: paully520 and 9h0s7

Top Liked Posts

  • There are no posts matching your filters.
  • 73
    In this post, I would like to explain what kexec-hardboot patch is.

    @kernel developers: I would like to ask you to merge this patch to your kernels, because it is essential part of MultiROM - it allows me to boot any kernel without changing the boot partition. I realize that it is no small request, but the patch is not big, touches relatively stable parts of kernel and should not cause any problems. Thank you.

    What is kexec?
    It is syscall of Linux kernel, which allows you to boot another Linux kernel without restarting the device - "Linux boots itself". The functionality is equivalent to fastboot -c *cmdline* boot zImage initrd.img, but without PC and fastboot. It is fairly known thing, so more info at wikipedia and man kexec.
    Standard kexec call unfortunatelly does not work on Nexus 4. It freezes somewhere, and it is very difficult to find out where - probably some of the drivers are not shut down/re-initialized properly, it is a commong thing among Android devices, which is why kexec-hardboot was made.

    What is the difference between normal and hardboot exec?
    Kexec-hardboot patch adds a real device restart to that process, so that all the drivers can be properly reinitialized. It stores new kernel to RAM, reboots the device as usual, and kernel from boot partition immediately jumps to the one which was stored to RAM before reboot.
    Unlike grouper's kexec-hardboot patch, this one only requires the host kernel to be patched. This is one of the improvements I made, and I think it is pretty significant.
    To sumarize the process:
    1. kexec --load-hardboot.... is called and kernel it loaded into RAM.
    2. kexec -e is called. Special info is written to memory (to area which is not overwritten on reboot) and the device is rebooted.
    3. After reboot, very early in the boot process, kernel checks if that special info is present in RAM and if so, it loads new kernel from RAM and jumps to it.
    4. Kexecd' kernel starts and boots.
    For more info, read the original thread.

    Patches:
    Kernel patch: https://gist.github.com/Tasssadar/7833796, 4.4 AOSP kernel repo
    This is the kernel patch. Only the host kernel needs to be patched.
    Related CONFIG options:
    • CONFIG_KEXEC=y
    • CONFIG_KEXEC_HARDBOOT=y
    • CONFIG_PROC_DEVICETREE=y
    • CONFIG_ATAGS_PROC=n # This one is turned on automatically, but it is not needed, so you can disable it.
    All these options must be enabled.

    Userspace kexec binary: https://github.com/Tasssadar/kexec-tools
    I had to change some things in kexec userspace binary because of some kernel bugs, complete description is in that repository. You can get statically built binary at https://github.com/Tasssadar/multirom/blob/master/install_zip/prebuilt-installer/multirom/kexec

    Usage:
    Once you have the kernel patches and kexec userspace binary in place, just run following command to boot into new kernel:
    Code:
    kexec --load-hardboot zImage --initrd=initrd.img --mem-min=0x20000000 --command-line="$(cat /proc/cmdline)" --dtb
    kexec -e
    Note the command line parameter - cmdline from bootloader is not added automatically, you have to put it there by yourself.

    Authors:
    This patch was made by Mike Kasick for Samsung Epic 4G. Since that, it was ported to several devices, one of them is Asus Transformer TF201 - I used patch from TF201 and modified it a bit (basically just changed few SoC specific constants). People at #ubuntu-arm helped me out with that, thanks.
    For hammerhead, I've improved the patch a bit - only the host needs to be patched now and I've added support for DTB.
    43
    Merged, thanks again for the awesome work @Tasssadar!
    2
    @neobuddy89 did you see this?

    Sent from Nexus 5 on Slimkat

    Yeah, I saw this. :p
    2
    Hello, first I'd like to apologize for dredging up an old thread. Second, I am attempting to build a CM13+kali+kexec compatible kernel, with extra wifi drivers as the base kernel doesnt support my wifi card. Based on that goal, I have a multi-part question for the OP, or anyone else with the know-how to assist me in my quest.

    1)Is there a source available for your kernel_kexec_hammerhead_cm13-01-2c39db662.zip listed on the multirom page for nexus 5? I'm attempting to build a new kernel, for kali+CM13+multirom, and your android_kernel_google_msm git only has branches going up to CM12. It would be far easier for me to start with a 'clean' CM13+kexec source then trying to patch myself as per question #2

    2) I've attempted to patch a plain old hammerhead_defconfig from CM13 git, however I believe I am doing so wrong. I have managed to compile kexec-tools and aquired the hammerhead 3.4.x patch. Now am I supposed to patch my produced zImage-dtb AFTER build, running the patch from the kexec-tools folder and against the zImage-dtb? Is it something like 'KEXEC.patch -p1 < zimage-dtb'

    Any clarification on the use of the kexec-tools patch, or simply a link to github source of a 'clean' CM13 kernel with the kexec patch already applied, would be incredibly helpfull on my journey down the rabbit hole :)

    ~EDIT~
    Ok so I've managed to figure out the patch and feel like a dumbass. For anyone else looking to get into kernel dev, and wanting to use the kexec patch, heres the basics:
    1- run patch within your kernel source directory
    2-ensure that the config settings in OP are set (I had to add them to my source by hand as they werent there)
    3-when zipping up your kernel, add the sbin and lib/kexec-tools folders from the kexec-tools source build, and be sure your updater-script / updater-binary is set to move these folders into /sbin and /lib respectively.
    4-Flash and enjoy Kexec! (check with multirom manager)