Kexec-hardboot patch

Tasssadar

Inactive Recognized Developer
Dec 31, 2010
818
6,126
0
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/6766757, 4.3 AOSP kernel repo, cm 10.2 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_ATAGS_PROC=y
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=0x85000000 --command-line="$(cat /proc/cmdline)"
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 flo, I've improved the patch a bit - only the host needs to be patched now and I also fixed a bug with /proc/atags not reading all tags.
 

asim0

Senior Member
Aug 22, 2010
323
189
0
what would be the benefit for end user? Or is it just for devs to try kernels faster?

Sent from teh void
 

dopy25

Senior Member
Feb 21, 2009
1,103
416
0
Fresno
@Tasssadar

I don't know if you're around this thread much anymore, but would it be possible to implement this patch alone in another model of phone's kernels?

I have an Optimus G which is almost identical to the N4 and am trying to read as much as I can on this patch but am not finding if it is device specific, and if it is, how much needs to be changed to be used on a different device (even if it is almost the same phone).

I know there are kernels that have this patch enabled but I don't know where it came from if it's not yours. I have been told it is not possible with a stock based kernel for the O.G. but I see you have some stock kernels for the N4.

I know almost is not exact, but I think so much of them are the same that it doesn't normally take much. Thank you for your time and work put into this.
 

opssemnik

Senior Member
Jun 10, 2013
1,549
1,034
113
Porto Alegre,Rio Grande Do Sul,Brasil
@Tasssadar

I don't know if you're around this thread much anymore, but would it be possible to implement this patch alone in another model of phone's kernels?

I have an Optimus G which is almost identical to the N4 and am trying to read as much as I can on this patch but am not finding if it is device specific, and if it is, how much needs to be changed to be used on a different device (even if it is almost the same phone).

I know there are kernels that have this patch enabled but I don't know where it came from if it's not yours. I have been told it is not possible with a stock based kernel for the O.G. but I see you have some stock kernels for the N4.

I know almost is not exact, but I think so much of them are the same that it doesn't normally take much. Thank you for your time and work put into this.
the kernel part itself you probably wont need to modify the patch, however you probably will need to change the offssets in multirom itself. if i recall, there is a guide on how to port multirom for other devices
 

dopy25

Senior Member
Feb 21, 2009
1,103
416
0
Fresno
the kernel part itself you probably wont need to modify the patch, however you probably will need to change the offssets in multirom itself. if i recall, there is a guide on how to port multirom for other devices
Thank you but I am already using multirom on my phone (Lg Optimus G on Sprint). It works like a champ, but I am trying to see if it can be applied to a stock rom. I am having issues with stock roms not using already patched kernels and would like to know if it is possible to use the patch mentioned here on another phone. The N4 and Optimus G are basically the same phone so I thought it might work.