[G1 Android Development] Firerat:
Kernel Switch script generator

Kernel Switch script generator

I've made a script to generate kernel switching scripts,

probably a little over the top :) but I had fun..

The basic configuration is

on the sdcard a directory called 'kernels'

within that are subdirectories for each kernel

e.g.

/sdcard/kernels/

/sdcard/kernels/RamHack/

/sdcard/kernels/NoRamHack/

Each kernel directory has at its root the boot.img

and the full 'tree' for the kernel modules

e.g. if your running kernels 'name' is 2.6.29.6-RamHack it would look like this

[COLOR="Blue"]/sdcard/kernels/RamHack/[/COLOR][COLOR="Green"][B]boot.img[/B][/COLOR]
[COLOR="Blue"]/sdcard/kernels/RamHack/system[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/lib[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/lib/modules[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/lib/modules/[/COLOR][COLOR="Green"][B]wlan.ko[/B][/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/modules[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/modules/lib[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/modules/lib/modules[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/modules/lib/modules/2.6.29.6-RamHack[/COLOR]
[COLOR="blue"]/sdcard/kernels/RamHack/system/modules/lib/modules/2.6.29.6-RamHack/[/COLOR][COLOR="Green"][B]ramzswap.ko[/B][/COLOR]

simply creating a new directory and 'unzipping' a 'kernel only' update zip into it should suffice.

The scripts

yeap, Plural I have two scripts, which are essentially the same.

one is for Gscript, and the other is for ASE

Since the only real difference is that with ASE I have had to explicitly specify 'su' for the commands that require it, I shall post this in a code block

the gscript version is the same, but since it has 'native' support for running su

the su -c "command" was not required

Both scripts are included in the attachment.

use

Gscript

If you haven't already got gscript it can be downloaded from the market

unzip the attachment ( base of this post ) to your sdcard

Launch gscript

menu(key) --> Add Script --> load file -->KSGen-0-2-gscript.sh-->untick needs su?-->Save

Tapping KSGen-0-2-gscript.sh will run the script

now repeat the Add Script process for each of the kernels

Kernel Switch scripts will have a ks- prefix, followed by the directory the boot.img is in

NB the ks- scripts require root, so keep the 'needs su?' ticked

Tapping the ks-script will prompt for SU, the files will be checked and copied if required, the boot.img flashed and your phone rebooted.

ASE

ASE is a little simpler, although I'm not overly keen on the interface

but it does have great potential.

direct download here

unzip the attachment ( base of this post ) to your sdcard ( if you haven't already done so )

Launch ASE

After launching ASE you will immediately see the script is already set up ready to run. simply tap the KSGen-0-2-ase.sh script.

Once the script has completed , press back

you will now see the generated script

tapping one will start the kernel switch, and you will be pestered a few times for SU, if you set always allow it will not pester you again.

edit:

ok, nice feature of ase, which makes up for the su bit (I'll see if I can get that a little less of a pain )

home screen

longpress --> shortcut --> ase --> select ks-???????

select background or terminal, its up to you

Regenerate and updating

Both of the scripts 'skip' ks- generation if one already exists ( e.g. ks-RamHack.sh and /sdcard/kernels/RamHack are a pair )

if you install a new boot.img and modules in an old directory the previous scripts will not work ( since they check the md5sums of the files )

ASE , is straight forward, simply 'long press' the ks- and select delete, the KSGen script will now make a new ks-

Gscript is a little more awkward, firstly you have to delete in the same way as ASE, via a 'long press', but gscript does not delete script on the sdcard, this has to be done externally, via command line, file manager etc. Damn pain I know.

What these scripts do not do

These scripts do not check compatibility, they will blindly flash an incompatible kernel

They do not backup the running kernel ( but it is on my list )

Other things I haven't thought about

Todo

Better out of space handling

I have built in some very basic logic on available space, if the scripts don't think there is enough room for all the kernel modules it will only update the wlan.ko ( since this is always over written it takes up no extra space)​

Backup running kernel

Basically I need to ensure a dd will give me a boot.img that I can flash​

abort reboot if attempting to flash the same kernel

flash_image doesn't flash boot.img if its the same as the running one, but frustratingly it still returns an exit code of 0, however logcat does advertise it, so I'll read that​

Confirmation dialogue for ASE

more/better checks on command success/fail

tidy scripts up

i.e. section up into functions​

well, here is the code block, as I said might be a little ott

#!/system/bin/sh
# KSGen-0-2-1ase.sh
# Kernel Switch script generator - Firerat 2010-04-08
# http://xdaforums.com/member.php?u=1595778
###############################################################
# scan /sdcard/kernels and generate gscripts for easy switching
# [THINK TANK] Toggle between RHack and NonRamHack
# http://xdaforums.com/showthread.php?t=658177
version="Version 0.2.1 - Alpha ASE Edition"
# 0.2.1 fixed mount for broken ROMS
# fixed date on broken ROMS

# probably won't change
ScriptDir="/sdcard/ase/scripts"
# this is where kernels are to be kept
KernelSwitchBaseDir="/sdcard/kernels"

touch ${ScriptDir}/ks-thisisatempfile.sh
# to lazy to count,
ScriptsDone=`find $ScriptDir -name "ks-*"|sed -e :a -e '$!N; s/\n/|/; ta' -e s/ks-//g -e s/.sh/\$/g -e s~$ScriptDir~~g`
rm ${ScriptDir}/ks-thisisatempfile.sh
if [ ! -e "$KernelSwitchBaseDir/flash_image" ] && [ -e `which flash_image` ];
then
echo "Grabbing a copy of flash_image.. just in case"
cp `which flash_image` $KernelSwitchBaseDir/
# TODO - check for cp fail
else
EndMsg="Couldn't find flash_image. I've generated the scripts anyway
but before you can use them we need to get
hold of a flash_image binary, recovery has one :)
run me in recovery and I steal a copy when Amon_RA isn't looking"
fi

for KernelDir in `find $KernelSwitchBaseDir -maxdepth 1 -type d|grep -v $KernelSwitchBaseDir\$|egrep -v "$ScriptsDone"`; do
# remove META-INF if it exists - Firerat 2010-04-08
if [ -e ${KernelDir}/META-INF ];
then
rm -rf ${KernelDir}/META-INF
fi
echo ${KernelDir}
KernelName=`basename $KernelDir`
# for now assuming kernel boot image is boot.img, and has correct magic - Firerat 2010-04-08
KernelImage=$KernelDir/boot.img
KernelImageMd5sum=`md5sum $KernelImage|cut -d " " -f1`
# create gscript shell script - Firerat 2010-04-08
echo "#!/system/bin/sh" > $ScriptDir/ks-$KernelName.sh
echo "######################################################################" >> $ScriptDir/ks-$KernelName.sh
echo "## Switch Kernel $version " >> $ScriptDir/ks-$KernelName.sh
echo "## Firerat 2010-04-08" >> $ScriptDir/ks-$KernelName.sh
echo "######################################################################" >> $ScriptDir/ks-$KernelName.sh
echo "# Script Gernerated `date`" >> $ScriptDir/ks-$KernelName.sh
echo "######################################################################" >> $ScriptDir/ks-$KernelName.sh
echo "ScriptDir_=\"$ScriptDir\"" >> $ScriptDir/ks-$KernelName.sh
echo "KernelSwitchBaseDir_=\"$KernelSwitchBaseDir\"" >> $ScriptDir/ks-$KernelName.sh
echo "KernelDir_=\"$KernelDir\"" >> $ScriptDir/ks-$KernelName.sh
echo "KernelName_=\"$KernelName\"" >> $ScriptDir/ks-$KernelName.sh
echo "KernelImage_=\"$KernelImage\"" >> $ScriptDir/ks-$KernelName.sh
echo "KernelImageMd5sum_=\"$KernelImageMd5sum\"" >> $ScriptDir/ks-$KernelName.sh
# Find Kernel Modules - Firerat 2010-04-08
# Make sure Modules list is clean - Firerat 2010-04-08
Modules=""
for Module in `find $KernelDir -name "*.ko"`; do
echo "`basename $Module .ko`_=\"$Module\"" >> $ScriptDir/ks-$KernelName.sh
echo "`basename $Module .ko`Md5sum_=\"`md5sum $Module|cut -d " " -f1`\"" >> $ScriptDir/ks-$KernelName.sh
echo "`basename $Module .ko`InstallPath_=\"`echo $Module|sed s~$KernelDir~~`\"" >> $ScriptDir/ks-$KernelName.sh
Modules="$Modules `basename $Module .ko`"
done
echo "Modules_=\"$Modules\"" >> $ScriptDir/ks-$KernelName.sh

#######################################################################

cat >> $ScriptDir/ks-$KernelName.sh << "EOF"
# check that nothing got corrupted - Firerat 2010-04-08

echo "Checking $KernelImage_"
if [ -e $KernelImage_ ];
then
if [ "`md5sum $KernelImage_|cut -d " " -f1`" != "$KernelImageMd5sum_" ];
then
echo "$KernelImage_
isn't the version I was expecting"
echo "I'm not going to flash it, as it may have become corupt"
exit 1
else
echo "md5sum matches .. $KernelImageMd5sum_"
if [ "$Modules_" != "" ];
then
for Module in $Modules_; do

eval Kmodule=\$${Module}_
eval Md5sum=\$${Module}Md5sum_
echo "Checking $Kmodule .."
if [ "`md5sum $Kmodule|cut -d " " -f1`" != "$Md5sum" ];
then
echo "$Kmodule
isn't the version I was expecting"
# TODO - better msg
echo "I'm not going to continue, as it may have become corupt"
exit 1
else
echo "md5sum matches .. $Md5sum"
fi
done
echo "All files appear to be in good order.. proceding.."
fi
# mount rw - Firerat 2010-04-08
su -c "busybox mount -o rw,remount /system"
# check if a CacheHack ROM, libs might be on mtdblock4
# should set the mnt point as a variable
if [ -d /system/csys ];
then
su -c "busybox mount -o rw,remount /system/csys"
fi
# check available space
SystemFree_K=`toolbox df|grep \/system\:|cut -d " " -f6|sed s/K//`
if [ -d /system/csys ];
then
CsysFree_K=`toolbox df|grep \/csys\:|cut -d " " -f6|sed s/K//`
else
CsysFree_K=$SystemFree_K
fi
ModDirs=`find $KernelDir_ -maxdepth 1 -type d|sed -e :a -e '$!N; s/\n/ /; ta'|sed s~$KernelDir_\ ~~`
KernelModSize=`busybox du -s $ModDirs|cut -f1`
if [ "$KernelModSize" -lt "$SystemFree_K" ] || [ "$KernelModSize" -lt "$CsysFree_K" ];
then
# phew, I thought I was going to have to figure out which partition things had to go to
for Module in $Modules_; do

eval InstallPath=\$${Module}InstallPath_
eval Md5sum=\$${Module}Md5sum_
eval Module_=\$${Module}_

if [ -e $InstallPath ];
then
if [ "`md5sum $InstallPath|cut -d " " -f1`" != "$Md5sum" ];
then
echo "Installing $Module to $InstallPath"
su -c "install -o 0 -g 0 -m 644 -D $Module_ $InstallPath"
if [ "$?" != "0" ];
then
# TODO - do something if it fails
echo "hmm, something went wrong"
echo "if this keeps cropping up, I'll add a log"
echo "- Firerat"
fi
else
echo "$Module is already installed"
fi
else
# meh, TODO Put everything in functions so code isn't dupicated - Firerat 2010-04-10
echo "Installing $Module to $InstallPath"
su -c "install -o 0 -g 0 -m 644 -D $Module_ $InstallPath"
if [ "$?" != "0" ];
then
# TODO - do something if it fails
echo "hmm, something went wrong"
echo "if this keeps cropping up, I'll add a log"
echo "- Firerat"
fi
fi
done
else
echo "TODO - do something when no space"
echo "Space appears to be limited where your kernel modules are kept"
echo "I'll write wlan.ko, so you at least have wifi"
echo "you may not be able to use things like compcache"
echo "I'll try and come up with a better solution,
like clear old modules - Firerat 2010-04-10 Version 0.2 beta"

for Module in wlan; do
eval InstallPath=\$${Module}InstallPath_
eval Md5sum=\$${Module}Md5sum_
eval Module_=\$${Module}_
if [ "`md5sum $InstallPath|cut -d " " -f1`" != "$Md5sum" ];
then
echo "Installing $Module to $InstallPath"
su -c "install -o 0 -g 0 -m 644 -D $Module_ $InstallPath"
if [ "$?" != "0" ];
then
# TODO - do something if it fails
echo "hmm, something went wrong"
echo "if this keeps cropping up, I'll add a log"
echo "- Firerat"
exit 1
fi
else
echo "$Module is already installed"
fi
done
fi
# Flash boot.img
if [ ! -e `which flash_image` ];
then
su -c "install -o 0 -g 0 -m 700 -D $flash_image /system/bin/flash_image"
fi
echo $KernelImage_
su -c "flash_image boot $KernelImage_"
echo "$KernelName_ Kernel Flashed..."
echo "Rebooting now"
su -c "sync"
su -c "reboot"
fi
else
echo "Sorry, $KernelImage_"
echo "seems to be missing....."
exit 1
fi
EOF
echo "$ScriptDir/ks-$KernelName.sh generated"
done

NB these scripts should be considered as Alpha nearing beta

I have tried to account for and test all eventualities, but you know its always the things that you missed that got missed.

Version 0.2.1 - fixed busybox mount, and error from date

( my bad, I keep forgetting no-one else has figured out busybox is better than toolbox ;) )