Auto-starting Samsung Android phone when charger is plugged in.

MrSmokeTooMuch
3 min readJun 19, 2018

If you used iPhone then you are used to having your phone automatically turn on when phone is plugged in to charger. That doesn’t happen on Android phones. Well I needed that particular feature on XCover 3 phone. So I’ll explain in this post how to make your Samsung phone start when its plugged in.

Of course this requires at least temporary root on your samsung phone. So unlock bootloader, root phone and make sure you can remount /system.

Also — other android manufacturers have implemented this feature differently and for example on Nexus 7 you can do it simply by running:

fastboot oem off-mode-charge 0

I have started by finding this thread on XDA Developers:

https://forum.xda-developers.com/showthread.php?t=1187631

As you can see there is “lpm” binary in /system/bin/ on Samsung phones. It handles showing charging screen when your phone is turned off. So simple fix is to just replace old lpm or lpmplay(depending on device) binary with shell script.

#!/system/bin/sh
/system/bin/reboot

When system detects its being charged it boots parts of android system and starts this binary, which is now changed with shell script that simply reboots the phone.

Lets take a look at a bit more advanced example of this script and how would you go and change this binary.

For this example prerequisites are that you have rooted phone with busybox installed. You can get busybox installer at:

https://play.google.com/store/apps/details?id=stericson.busybox

Lets begin.

First you need shell access, which you can get either through adb or some terminal emulator like termux.

user@pc ~ $ adb shell

As you are now logged in to your phones shell. Request superuser access with

$ su

and remount /system

# mount -o remount,rw /system

Now — we go to /system/bin and make a copy of original lpm binary, delete original and write a script with vi or nano to start the old binary and when everything is started reboot the phone.

# cd /system/bin# cp lpm lpm.orig# vi lpm#!/system/bin/sh
/system/bin/lpm.orig &
while [ true ]; do
sleep 1
ps | grep lpm.orig && sleep 3 && /system/bin/reboot
done

Then change permissions for binary replacement.

# chmod 0755 /system/bin/lpm# chmod 0755 /system/bin/lpm.orig# chown root.shell /system/bin/lpm# chown root.shell /system/bin/lpm.orig

Well that’s all fine and dandy and it works as it is. But we have two issues:

  • we need either apk or we need to add account to download busybox
  • when phone reaches 0% and is then started, booting right away just empties battery enough to shut it right down again and keep it in boot loop.

Get battery percentage:

well it should be simple just run

adb shell dumpsys battery

but dumpsys command won’t work because all the functionality is not loaded in that state. So we need to run

cat /sys/class/power_supply/battery/capacity

I have also seen stack overflow thread where this was suggested but it didn’t work:

cat /sys/class/power_supply/battery/batt_attr_text

As we now have battery percentage level we can modify our script:

#!/system/bin/sh
/system/bin/lpm.orig &
while [ true ]; do

bat_proc=`cat /sys/class/power_supply/battery/capacity`

if [ $bat_proc -gt 10 ]; then
sleep 3 && /system/bin/reboot
fi

sleep 5
done

Now to the other issue. Having to install busybox just to be able to use text editor to paste the script in doesn’t make sense if you have multiple phones, that are all new. So I wrote a script that writes a script that writes a script.

*insert inception sound*

#!/bin/bash
script="mount -o remount,rw /system
cd /system/bin
cp lpm lpm.orig
rm lpm
echo \"#!/system/bin/sh
/system/bin/lpm.orig &
while [ true ]; do

bat_proc=\\\`cat /sys/class/power_supply/battery/capacity\\\`

if [ \\\$bat_proc -gt 10 ]; then
sleep 3 && /system/bin/reboot
fi

sleep 5
done\" > lpm

chmod 0755 /system/bin/lpm
chmod 0755 /system/bin/lpm.orig
chown root.shell /system/bin/lpm
chown root.shell /system/bin/lpm.orig"
echo "su
cd /data/local
echo '$script' > replaceLPM.sh
chmod +x replaceLPM.sh
./replaceLPM.sh
exit
exit" > tmpSH.txt
adb shell < tmpSH.txt

And now we have a script that turns normal lpm binary to script without installing busybox. Well if you want to update your script and you run similar script again you are screwed, so I just wrote another script that returns everything to normal state and replaces script with lpm binary.

#!/bin/bash
script="mount -o remount,rw /system
cd /system/bin
cp lpm.orig lpm
"
echo "su
cd /data/local
echo '$script' > resetLPM.sh
chmod +x resetLPM.sh
./resetLPM.sh
exit
exit" > resetLPMScript.txt
adb shell < resetLPMScript.txt

Cheers!

--

--