[Miscellaneous Android Development] alecxs:
Factory Reset / Google Account Lock (FRP) common Warning!

UPDATE JAN/2018: For Lollipop 5.1+ and higher, disable Factory Reset Protection (FRP) first! i didn't know this when i wrote this tutorial (it was written for Jellybean). Please always remove google account from settings and/or enable OEM Unlock in Developer Options before Factory Reset! it is a new security feature which will get you in trouble, please google for it!

How to install and run the shell script

- root the phone

- install SuperSU, busybox and terminal emulator (or similar apps)

- install SELinux busybox, SELinuxModeChanger and NextApp SDFix (Android 4.3+ higher only)

- install sqlite3 binary to /system/xbin (Samsung only)

- download file to external sdcard: bckp2arm.zip

(if not installed, download gnu-tar and p7zip, too)

- unpack the zip file: type "unzip bckp2arm.zip" in Terminal Emulator

(no need to unpack gnu-tar or p7zip)

- type "su" in Terminal Emulator, grant access in SuperSU app

- type "sh /sdcard/bckp2arm.sh"

- wait for the script is finished (up to 1 hour)

1. How to migrate data with broken display

- on source phone, boot in Android system recovery <3e> (Vol down + Power)

- select "backup user data" (Vol down), confirm (Vol up)

(if display is broken and you have black screen, it may work anyway. do it on both broken and new phone at the same time, count and reproduce the key input sequence)

- copy the userdata_20160823_100259.backup file to empty sdcard

- insert sdcard in target phone

- on target phone, do factory reset

(make sure you have a copy of all your pictures, even from internal emulated sdcard)

- create a text file "bckp2arm.ini" with path "/sdcard/userdata_20160823_100259.backup"

- copy the "bckp2arm.ini" file into same folder where "bckp2arm.sh" is

- run the shell script "bckp2arm.sh" (see above)

2. How to reset screenlock

(NOTE: this may work with CWM backups, too)

- boot in Android system recovery <3e> (Vol down + Power) (or custom recovery)

- select "backup user data" (Vol down), confirm (Vol up) (or "Backup" (Vol), confirm (Power))

- wait for a new backup is created

- do factory reset. you have access now.

- create a text file "bckp2arm.ini" with path to backup file (or data.ext4.tar file)

- add a new line in "bckp2arm.ini": PROTECT_SCREEN=0

- run the shell script "bckp2arm.sh" (see above)

- after restoring, you have full access

3. How to calculate new checksum

(if you wanna go back completely stock again)

- create a text file "bckp2arm.ini" with path to backup file

- add a new line in "bckp2arm.ini": PROTECT_SDINT=0

- run the shell script "bckp2arm.sh" (see above)

- after restoring, in stock recovery, select "Backup User data" instead of "reboot system now"

thats it, you now creating a new copy of your backup file with (new phone specific) valid checksum

How to create a text file "bckp2arm.ini"

busybox have text editor "vi" but not recommend. just type: echo "/sdcard/userdata_20160823_100259.backup" > bckp2arm.ini

(use > to overwrite the file, >> to append a new line).

to show the file: cat bckp2arm.ini

You may install a Text Editor app as well, for example DroidEdit.

(klick for full description of parameters)

bckp2arm.ini

use the following parameters to control the unpacking behavior

(the following information is for advanced restoring only, skip reading here)

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

PROTECT_SDINT / Internal Virtual SD Card

since Honeycomb, there is so called fuse tool which emulates /data/media folder inside userdata partition as a separate storage device in /storage/emulated/0. usually you will find all your pictures and music files there in /storage/sdcard1, unless you have changed the default write disk (in Settings -> Storage) from Phone storage to SD Card. as a result the emulated storage is included in backup. with this param PROTECT_SDINT you can control the unpacking method.

Values:

<undefined>: merge (default)

Keep current files. create a new backup. unpack files from old backup. mix up all files and folders. existing files will be overwritten.

0: delete

Format emulated SD Card. Exclude emulated storage from new backup. unpack files from old backup only.

1: preserve

Keep current files only. create a new backup. skip unpacking from old backup. (use this only if you don't care about your pictures etc.)

2: preserve & unpack

Keep current files. create a new backup. unpack files from old backup to external microSD Card instead. existing files will be overwritten. (make sure you have a copy of your external microSD Card)

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

PROTECT_SDEXT / microSD Card 2. partition

Not all phones have enough internal memory to save all apps to internal memory. on rooted devices apps like Link2SD / A2SD uses a secondary partition on your SD card and uses it as another segment of internal storage. it moves apk, dalvik-cache (.dex), lib and internal data files into this partition (which the OS mounts on boot) and creates a symbolic link in the original location. This retains the file structure that the system expects but with the bulk of the data moved to the SD card.

This script may - or may not - work with Link2SD / A2SD. Most 2. partitions are never included in backup. Even if partition unmounted, it is remounted on next boot. Later then, after a successfull restoring - if Apps uid's not matching - Android deletes data from it!

Anyway, you may use this partition for temporary unpacking. change the settings in bckp2arm.ini to skip this check.

Values:

<undefined>: exit (default)

stop the script if 2. partition is mounted on /data/sdext2 or /system/sd. be aware this detection won't work after factory reset! if 2. partition is NOT mounted on /data/sdext2 or /system/sd, same as 1: mount.

0: restore

keep partition mounted on /data/sdext2. restore partition from backup. existing files will be overwritten. if extended file system is needed for unpacking, search for other ext2 partitions on microSD Card.

1: mount

if extended file system is needed for unpacking, unmount the 2. partition and remount it to /cache. existing files will be overwritten.

2: preserve

keep partition mounted. don't use the partition. if extended file system is needed for unpacking, search for other ext2 partitions on microSD Card.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

PROTECT_IMEI / Clone Phones IMEI and WiFi MAC-Address

Some phones have copy of NVRAM partition inside /data/nvram folder. as a result the nvram is included in backup. if restored, the Phones IMEI and WiFi MAC-Address is used from backup (cloned), instead from Phone.

BEWARE OF USING FAKE IMEI MAY ILLEGAL IN YOUR COUNTRY!

Values:

1: preserve (default)

Keep your Phones IMEI and WiFi MAC-Address. delete /data/nvram folder (will generated from android).

0: clone

Overwrite your Phones IMEI and WiFi MAC-Address and using IMEI from old phone backup instead (not recommended). NVRAM partition is not affected. (for undo this: make a factory reset to restore the original IMEI and WiFi MAC-Address)

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

PROTECT_SCREEN / screenlock settings

Values:

1: preserve (default)

Restore screenlock settings from backup file.

0: unlock

unlock your Phones screenlock pattern.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

WIPE_DALVIK / dalvik-cache

Android Dalvik Virtual Machine (DVM) is a google fork of Java Virtual Machine (JVM) and a Just In Time Compiler (JITC) for Android - a main part of Android OS - which is replaced by Android Runtime (ART) since Lollipop. Both uses Dalvik Executable format (.dex) files, which are zipped into a single Android Package (.apk). When Dalvik Cache is deleted, it will generated from android on next boot. Wipe Dalvik Cache if phone is stuck on boot screen and you think any app causes this because of possible version mismatches between android package (.apk) and dalvik executable (.dex). Wiping Dalvik Cache will not affect to apps stored data.

Values:

0: unpack (default)

restore Dalvik Executable (.dex) files from backup.

1: wipe

delete /data/dalvik-cache folder (will generated from android).

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

WIPE_CACHE / clear cache

The App Cache is a temporary storage which speeds up device operating and reduces data processing. websites load faster on a second visit. The increase in speed after the initial launch of an app is thanks to this cached data. However, if an app starts to misbehave or stop working, or you've run out of storage space on device, free it up. Clearing cache will not affect to apps stored data.

Values:

0: unpack (default)

unpack Cache files from backup file.

1: clear

clear cache for every app. same as "Clear cache" button in Settings -> Apps.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

ADB_MODE / shell selection

Android is moved from data partition to dummy file during runtime. this leaves ~ 500 MB unallocated space as long as system server is running. Terminal emulator is an app that runs on top of android and requires system server. a soft reboot will free up the unallocated disc space, but this kills Terminal emulator, too. if you run the script from computer with adb shell, set this parameter for soft reboot. adb shell is not affected, because script runs from computer, and will continue after reboot.

Values:

0: terminal emulator mode (default)

keep system server alive. no reboot. this consumes additional temporary space.

1: adb shell mode

kill the system server, this causes a soft reboot. run it from adb shell only!

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

USB_MODE / Phone Charging Mode

The Phone Charging Mode is used as confirmation prompt. script will not run unless charger is connected. This avoid running out of battery. It allow to set Display always on, as long as USB cable is connected, and this does not throttle CPU. However, BE AWARE this is a security risk (lock your screen when leaving the room). For Phones without microSD Card slot the USB connector is needed for USB Stick. when using ADB Mode together with USB On-The-Go (OTG) you need to set up ADB over WiFi.

Values:

0: unplugged mode

battery mode without connected charger. use this in case the usb connector is broken. Turns display off. Slows down CPU in sleep mode

1: charging / Client Mode (default)

charging mode with USB cable connected. enables the 'keep awake while plugged in' setting (Screen will never sleep while charging). CPU stays awake

2: USB On-The-Go (OTG) / Host Mode

Host mode for USB Stick. enables the 'keep awake while plugged in' setting. Don't turn Wi-Fi off (for adb over WiFi)

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

GNUTAR / GNU tar for android

The script use "tar" for unpacking - which usually comes with busybox. However, busybox tar is not unpacking backup files, therefore GNU tar is needed. If you need other binary than ARM architecture or if newer version is already installed, disable GNU tar installation.

Values:

1: enabled (default)

search for GNU tar on sdcard. unpack the zip file. install tar binary to phone cache

0: disabled

don't search for GNU tar on sdcard. use phones default tar, if installed. use busybox tar, if symlinked.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

P7ZIP / p7zip for android

The script use p7zip for unpacking unknown file content, if all other unpacking methods fail. If you need other binary than ARM architecture or if newer version is already installed, disable p7zip installation. Make sure the p7zip version is supporting large files > 2 GB. You can simply check it by typing 7z | grep p7zip it must show HugesFiles=on.

Values:

1: enabled (default)

search for p7zip on sdcard. unpack the zip file. install 7z binary to phone cache

0: disabled

don't search for p7zip on sdcard. use phones default 7z (if installed)

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

This script is tested with following phone models:

- Archos 50b Oxygen 4 GB, Mediatek MT6582, Android 4.2.2 "Jelly Bean"

- Wiko Rainbow 4G 8 GB, Mediatek MT6582, Android 4.2.2 "Jelly Bean"

- Samsung Galaxy S II 16 GB, Exynos 4 Dual (4210), Android 7.1.2 "Lineage OS 14.1"

- Cubot King Kong 16 GB, Mediatek MT6580, Android 7.0 "Nougat"

(i will update this list, please always send me private message with your successfully tested phone)