Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,810,319 Members 52,541 Now Online
XDA Developers Android and Mobile Development Forum

[guide] Hacking the startup sequence to run your own code

Tip us?
 
Nick Hill
Old
#1  
Junior Member - OP
Thanks Meter 7
Posts: 17
Join Date: Dec 2013
Default [guide] Hacking the startup sequence to run your own code

I have been trying to customise my MTK phones with a pre-defined user experience, with sepcific settings etc.

Perhaps one way to achieve this is to check if the device has been facory reset, then use a script to insert specific settings once. For this I need to launch a script early in the boot sequence.

This could be achieved by dismantling the boot.img, altering the init.rc, then re-building, which requires significant work and risks bricking the device. So I looked for a way to run a script without significant re-flashing. here goes:

xlog changes the system log level, and is called just a few times at boot-up. Xlog is run early, and as root, with the parameter boot.

This script renames xlog as xlogboot, introduces a script in place of xlog, which runs another script, startup.sh when xlog is called with parameter boot. All the original parameters are passed to the renamed xlog binary, so the system functionality should not be changed.

I pushed the script to the device, then run it as root. I can now edit /system/bin/startup.sh to run whatever initialisation commands I wish. I have so far tested on a ZTE v965 .

Code:
#!/system/bin/sh

#This script creates a hook into the bootup system of Android
#This hook operates when /system/bin/xlog is called with
#the first parameter 'boot'

#Use this script entirely at your own risk. I highly recommend 
#you ensure YOU have a backup of YOUR device before going further

#Public Domain Nick Hill 2014

if [ -e /system/bin/xlogboot ] || [ -e /system/bin/startup.sh ]; then
echo "This script may have already been run, exiting.."
exit 1
fi

if [ ! -e /system/bin/xlog ] 
then
echo "This script is not running in an expected environment. Exiting.."
exit 2
fi

if [ $USER != "root" ] 
then
echo "This script must be run as root. try running su. Exiting.."
exit 3
fi


touch /system/bin/atestfile
if [ ! -e /system/bin/atestfile ] 
then
echo "It appears system folder is not mounted read-write. Remounting..."
mount -o remount,rw /system
else
rm /system/bin/atestfile
fi

echo -e '#!/system/bin/sh\n' > /system/bin/startup.sh
chown root:root  /system/bin/startup.sh
chmod 755  /system/bin/startup.sh

mv /system/bin/xlog /system/bin/xlogboot

echo -e '#!/system/bin/sh\n/system/bin/xlogboot $*\nif [ "$1" == "boot" ] \nthen\n/system/bin/startup.sh\nfi\n' >/system/bin/xlog
chown root:shell /system/bin/xlog
chmod 755 /system/bin/xlog
Enjoy!
 
Nick Hill
Old
#2  
Junior Member - OP
Thanks Meter 7
Posts: 17
Join Date: Dec 2013
Default Make your own custom rom

Problem: You have an android device which you want to give/sell in USA or England. The device has come with an array of auto-starting chinese pop-ups, horrible wallpaper, and a Chinese clock applet. You uninstall all those system apps, replace your widgets with nice English ones. The phone when factory reset, starts in Chinese. Returns the user to tacky wallpaper, and you have lost the nice theming and widgets you have painstakingly installed and set up in the system folder.

Solution: This backup script. I have tested it on a v965, it also appears the file system would be OK on an A766 and many other android devices.

Instructions:
Use my previous script on this thread to create a hacked startup sequence. You must already have the /system/bin/startup.sh in place.
Unpack the tarball
Check the script lists the files you intend to back up. You will need to edit the script for that. I have set it to preserve settings for language, launcher, the 3D clock app factory.widgets.ThreeDDigitalWeatherClock-1.apk , which I think is really cool, wallpaper and the ADB mode.
Copy the install.sh and busybox to a writable directory on your android device.
su (go to root)
In a shell, cd to the directory where you uploaded the files.
sh install.sh

This will create a directory /system/perm-config

When you have the device as you would like it,
cd /system/perm-config
./make-backup.sh
This will ceate a directory tree in /system/perm-config/data folder, containing the backups of specific app data.
If all goes well, next time the phone is factory reset, the defaults are reloaded.

You could then re-flash the system partition of devices, perform a factory reset to take the device to your own defaults. You can use the last script (to hack the startup sequence), and this script to make your own custom ROMs. The script should do it's job even after un-rooting a device.

As usual, use at your own risk. I take no responsibility if your phone bricks, explodes, turns to antimatter, suddenly drops into a parallel dimension or starts receiving calls from the future.
Attached Files
File Type: gz permanent-config.tar.gz - [Click for QR Code] (1.05 MB, 4 views)
Tags
boot up commands, init sequence
Thread Tools
Display Modes