diff options
author | MyoungJune Park <mj2004.park@samsung.com> | 2017-03-27 13:37:59 +0900 |
---|---|---|
committer | MyoungJune Park <mj2004.park@samsung.com> | 2017-06-12 16:15:18 +0900 |
commit | 98d02ddb1a4b4bf4b0879fb189707543d06bc6bf (patch) | |
tree | 05be6983bb828d98b320899e31b09f3794ec1c57 /script | |
parent | 41f717fb0857f466c34797859cc0c5525daa4e54 (diff) | |
download | factory-reset-98d02ddb1a4b4bf4b0879fb189707543d06bc6bf.tar.gz factory-reset-98d02ddb1a4b4bf4b0879fb189707543d06bc6bf.tar.bz2 factory-reset-98d02ddb1a4b4bf4b0879fb189707543d06bc6bf.zip |
init code
Change-Id: Ie6cb5cdbcabca325d23d3a230027d40f67c09644
Signed-off-by: MyoungJune Park <mj2004.park@samsung.com>
Diffstat (limited to 'script')
-rwxr-xr-x | script/build-backup-data.sh | 97 | ||||
-rw-r--r-- | script/dump-frlog.sh | 9 | ||||
-rwxr-xr-x | script/factoryreset-verify-result.sh | 36 | ||||
-rw-r--r-- | script/factoryreset-verify.sh | 101 | ||||
-rwxr-xr-x | script/run-factory-reset.sh | 299 |
5 files changed, 542 insertions, 0 deletions
diff --git a/script/build-backup-data.sh b/script/build-backup-data.sh new file mode 100755 index 0000000..bb8d58e --- /dev/null +++ b/script/build-backup-data.sh @@ -0,0 +1,97 @@ +#!/bin/bash +# +# Copyright (c) 20014- 2017 Samsung Electronics Co., Ltd. +# +# Contact: MyoungJune Park <mj2004.park@samsung.com> +# Created by Wonil Choi <wonil22.choi@samsung.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + + +## build backup data for factory reset + + +[ $INSTALL_ROOT ] || INSTALL_ROOT="/" +rdir=$INSTALL_ROOT/usr/system/RestoreDir +optdir=$INSTALL_ROOT/opt +CURDIR=`pwd` +BACKUPLIST=`ls -1 $optdir` +EXCLUDE_OPTION= +# For removable preload app +RESTORE_EXCEPTION_LIST= +if [ -e $INSTALL_ROOT/usr/packages/restore_exception.list ]; then + xlist=$INSTALL_ROOT/usr/packages/restore_exception.list + RESTORE_EXCEPTION_LIST=`sed -e "s#^\/##g" $xlist` +fi + +create_archive() { + echo "Create_Archive --------------------------------------------" + mkdir -p $rdir + + BACKUP_FULLPATH="" + for i in $BACKUPLIST; do + BACKUP_FULLPATH="$BACKUP_FULLPATH opt/$i" + done + + # create tar archives + echo "create archives... $BACKUP_FULLPATH" + cd $INSTALL_ROOT + bash -c "$BACKUPCMD $BACKUP_FULLPATH $EXCLUDE_OPTION" + + # checksum + echo "calculate checksum..." + xpath="! \( -path \"opt/var/tmp/*\" \)" + for i in $RESTORE_EXCEPTION_LIST; do + xpath="$xpath ! \( -path \"$i/*\" \)" + done + + for i in $BACKUP_FULLPATH; do + bash -c "find $i $xpath -type f -exec md5sum {} \; >> $rdir/checksum.md5" + bash -c "find $i $xpath -exec /usr/bin/chsmack {} \; >> $rdir/smack_label.txt" + done + + chmod go-rwx $rdir/* $rdir $rdir/.. +} + +case "$1" in +tar) +#SCM use opt/var/tmp for temporal files, the files are removed before the binary creation + EXCLUDE_OPTION="--exclude=opt/var/tmp/*" + for i in $RESTORE_EXCEPTION_LIST; do + EXCLUDE_OPTION="$EXCLUDE_OPTION --exclude=$i" + done + BACKUPCMD="tar czf $rdir/opt.tar.gz" + create_archive + ;; +""|zip) +#SCM use opt/var/tmp for temporal files, the files are removed before the binary creation + EXCLUDE_OPTION="-x "opt/var/tmp/*"" + for i in $RESTORE_EXCEPTION_LIST; do + EXCLUDE_OPTION="$EXCLUDE_OPTION \"$i/*\"" + done + BACKUPCMD="zip -yrX- $rdir/opt.zip" + create_archive + ;; +*) + echo "Usage: $0 {tar|zip}" + echo "zip is used by default" + cd $CURDIR + exit 2 + ;; +esac + +cd $CURDIR +## end - building backup data + diff --git a/script/dump-frlog.sh b/script/dump-frlog.sh new file mode 100644 index 0000000..b3e495d --- /dev/null +++ b/script/dump-frlog.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +#-------------------------------------- +# Factory-reset +#-------------------------------------- +FACTORY_RESET_DEBUG=$1/factory-reset +/bin/mkdir -p ${FACTORY_RESET_DEBUG} +/bin/tar -cvf ${FACTORY_RESET_DEBUG}/factory-reset-log.tar /opt/.factoryreset* + diff --git a/script/factoryreset-verify-result.sh b/script/factoryreset-verify-result.sh new file mode 100755 index 0000000..d482158 --- /dev/null +++ b/script/factoryreset-verify-result.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# +# Copyright (c) 20014- 2017 Samsung Electronics Co., Ltd. +# +# Contact: MyoungJune Park <mj2004.park@samsung.com> +# Created by Wonil Choi <wonil22.choi@samsung.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + + + +logfile="/opt/var/log/.verify.log" +frlog="/opt/.factoryreset.log" + +if [ -e $frlog ]; then + logfile="$frlog" +fi + +/bin/grep "NG," ${logfile} +if [ $? -ne 0 ];then + exit 0; +else + exit 1; +fi diff --git a/script/factoryreset-verify.sh b/script/factoryreset-verify.sh new file mode 100644 index 0000000..3302a21 --- /dev/null +++ b/script/factoryreset-verify.sh @@ -0,0 +1,101 @@ +#!/bin/sh +# +# Copyright (c) 20014- 2017 Samsung Electronics Co., Ltd. +# +# Contact: MyoungJune Park <mj2004.park@samsung.com> +# Created by Wonil Choi <wonil22.choi@samsung.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +logfile="/tmp/.verify_fail.log" +logchecksum="/tmp/verify_checksum.log" + +CURRENTPATH=`/bin/pwd` +ROOTDIR=/ + +OPT="opt/" +rdir=usr/system/RestoreDir/ + +cd ${ROOTDIR} + +check_verify () { +# check checksums with target data(restored) + cd $ROOTDIR; /usr/bin/md5sum -c ${ROOTDIR}${rdir}checksum.md5 > $logchecksum + + if [ $? -ne 0 ];then + echo "reset verify failed" + # RESERVED STRING for verification result + echo "FACTORYRESET VERIFICATION : FAIL" + /bin/grep FAIL $logchecksum + /bin/grep FAIL $logchecksum > $logfile + FACTORY_RESULT="NG," +# CALLS + CALLS_DB=`/bin/grep "phone-misc.db:" $logfile` +# CALLS_APP=`/bin/grep "org.tizen.phone" $logfile` +# contact db also has call logs + CONTACT_DB=`/bin/grep "contacts-svc.db:" $logfile` + if [[ "z$CALLS_DB" != "z" || "z$CALLS_APP" != "z" || + "z$CONTACT_DB" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}CALLS/" + fi +# SMEMO + SMEMO_DB=`/bin/grep "smemo.db:" $logfile` +# SMEMO_APP=`/bin/grep "org.tizen.smemo" $logfile` + if [[ "z$SMEMO_DB" != "z" || "z$SMEMO_APP" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}SMEMO/" + fi +# MEMO + MEMO_DB=`/bin/grep "\.memo.db:" $logfile` +# MEMO_APP=`/bin/grep "org.tizen.memo" $logfile` + if [[ "z$MEMO_DB" != "z" || "z$MEMO_APP" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}MEMO/" + fi +# SMS/MMS + MSG_DB=`/bin/grep "msg_service.db:" $logfile` +# MSG_APP=`/bin/grep "org.tizen.message" $logfile` +# MSG_SVC=`/bin/grep "msg-service" $logfile` + if [[ "z$MSG_DB" != "z" || "z$MSG_APP" != "z" || + "z$MSG_SVC" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}SMS/MMS/" + fi +# ALARM + ALARM_DB=`/bin/grep "alarm.db:" $logfile` +# ALARM_APP=`/bin/grep "org.tizen.clock" $logfile` + ALARMMGR_DB=`/bin/grep "alarmmgr.db:" $logfile` + if [[ "z$ALARM_DB" != "z" || "z$ALARM_APP" != "z" || + "z$ALARMMGR_DB" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}ALARM/" + fi +# CONTACT +# CONTACT_APP=`/bin/grep "org.tizen.contacts" $logfile` +# CONTACT_SVC=`/bin/grep "contacts-svc" $logfile` + if [[ "z$CONTACT_DB" != "z" || "z$CONTACT_APP" != "z" || + "z$CONTACT_SVC" != "z" ]]; then + FACTORY_RESULT="${FACTORY_RESULT}CONTACT/" + fi + +## Report final results + if [ "$FACTORY_RESULT" != "NG," ]; then + echo $FACTORY_RESULT + fi + else + echo "factory reset verify success" + echo "FACTORYRESET VERIFICATION : OK" + fi + + /bin/rm -f $logfile + /bin/rm -f $logchecksum +} + +check_verify diff --git a/script/run-factory-reset.sh b/script/run-factory-reset.sh new file mode 100755 index 0000000..c2d1f54 --- /dev/null +++ b/script/run-factory-reset.sh @@ -0,0 +1,299 @@ +#!/bin/sh +# +# Copyright (c) 20014- 2017 Samsung Electronics Co., Ltd. +# +# Contact: MyoungJune Park <mj2004.park@samsung.com> +# Created by Wonil Choi <wonil22.choi@samsung.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +logfile="/opt/.factoryreset.log" +logfile1="/opt/.factoryreset.log.1" +logchecksum="/tmp/factory_reset_checksum.log" + +CURRENTPATH=`/bin/pwd` +ROOTDIR=/ +USRDATADIR=/opt/usr + +OPT="opt/" +rdir=/usr/system/RestoreDir/ +FLAG_FTRRST_CP= +FLAG_WITHOUT_CP= + +for arg in $@ +do + case "$arg" in + "--ftrrstcp") + FLAG_FTRRST_CP="yes" + ;; + "--withoutcp") + FLAG_WITHOUT_CP="yes" + ;; + esac +done + +cd ${ROOTDIR} + +kill_normal_daemons () { + LIST=`/bin/ls /proc/` + for i in $LIST + do + if [[ `/usr/bin/expr match "$i" '[0-9]*'` != 0 && -f /proc/${i}/cmdline ]]; then + cmd=`/bin/cat /proc/${i}/cmdline | /usr/bin/tr '\000' ' ' | /usr/bin/awk '{print $1}'` + if [ `/usr/bin/expr match "$cmd" '[\/a-zA-Z\-]*'` != 0 ]; then + pname=`/bin/echo $cmd | /usr/bin/awk '{print $1}' | /usr/bin/awk -F '/' '{print $NF'}` + if [[ $pname != "factory-reset" && + $pname != "rc.shutdown" && + $pname != "run-factory-reset.sh" && + $pname != "enlightenment" && + $pname != "factory-reset-util" && + $pname != "dbus-daemon" && + $pname != "init" && + $pname != "-sh" && + $pname != "sh" && + $pname != "agetty" && + $pname != "Xorg" && + $pname != "initnormal" && + $pname != "system-recovery" && + $pname != "systemd-logind" && + $pname != "systemd-journald" && + $pname != "systemd-udevd" && + $pname != "mpdecision" && + $pname != "thermal-engine" && + $pname != "rmt_storage" && + $pname != "systemd" ]] + then + echo $pname: kill $1 it. + /usr/bin/killall $1 $pname + else + echo $pname: ignore killing. + fi + fi + fi + done +} + +kill_before_reset () { +## disable systemd-releaseagent + echo /bin/true > /sys/fs/cgroup/vip/release_agent + /bin/umount -lf /sys/fs/cgroup + +## kill processes + /usr/bin/killall lockscreen +# avoid crash popup + /usr/bin/killall -STOP deviced +# kill all apps first. + /usr/bin/killall launchpad_preloading_preinitializing_daemon + + kill_normal_daemons +# some apps revive scim. so kill again. + /usr/bin/killall -9 scim-launcher isf-panel-efl + echo "Check and kill again." + kill_normal_daemons -9 +} + +check_fail() { + cur_result=`/bin/grep " : FAIL" ${logfile}` + old_result=`/bin/grep " : FAIL" ${logfile1}` + if [[ "z$cur_result" != "z" && "z$old_result" != "z$cur_result" ]]; then + # reboot and retry + /bin/sync + /sbin/reboot + fi +} + +# mount and umount partitions +fs_ready() { +# data in microsd should be preserved. + /bin/umount -lf ${OPT}/storage/sdcard + #for encryption. If encrypted, it is mounted one more time as encryptfs. + /bin/umount -lf ${OPT}/storage/sdcard +# modem binary images should be preserved. + /bin/umount -lf ${OPT}/modem +# phone usr partition should be reset + /bin/mkdir -p $USRDATADIR + /bin/mount $USRDATADIR + mret=`/bin/grep "$USRDATADIR " /proc/mounts | /usr/bin/awk '{print $1}'` + device=`/bin/grep "$USRDATADIR" /etc/fstab | /usr/bin/awk '{print $1}'` + device=`/usr/bin/readlink -f $device` + if [[ "z$mret" != "z" && "$mret" != "$device" ]]; then + echo "$mret != $device" >> $logfile + echo "$USRDATADIR may be encrypted : FAIL" >> $logfile + check_fail + fi + if [[ "z$mret" = "z" && "z$device" != "z" ]]; then + # mount failed. format and remount + echo "$USRDATADIR mount failed. format and retry to mount again" >> $logfile + fstype=`/bin/grep "$USRDATADIR " /etc/fstab | /usr/bin/awk '{print $3}'` + /sbin/mkfs.$fstype $device -F + /bin/mount -t $fstype $device $USRDATADIR + fi + mret=`/bin/grep "$USRDATADIR " /proc/mounts | /usr/bin/awk '{print $2}'` + if [ "z$mret" = "z" ]; then + echo "$USRDATADIR MOUNT : FAIL" >> $logfile + # reboot and retry + /bin/sync + /sbin/reboot + fi + check_fail +} + +disable_keys() { + key_dir=`/bin/ls -1d /sys/devices/gpio_keys*` + if [ "z$key_dir" != "z" ]; then + key_code=`/bin/cat $key_dir/keys` + echo $key_code > $key_dir/disabled_keys + fi + # If cannot disabled keys by sysfs, kill apps about key operations + # systemctl stop starter + # /usr/bin/killall volume + # /usr/bin/killall cluster-home +} + +if [ -r ${rdir}opt.tar.gz ]; then + RSTCMD="/bin/tar xvzf ${rdir}opt.tar.gz -C /" +elif [ -r ${rdir}opt.zip ]; then + RSTCMD="/usr/bin/unzip -n -X ${rdir}opt.zip" +else + exit 1 +fi + +## display ui +cnt=0 +while [[ ! -e /tmp/.wm_ready && $cnt -le 10 ]] +do + echo "waiting window manager $cnt" >> $logfile + cnt=$(($cnt+1)) + /bin/sleep 1 +done + +if [ -e /tmp/.wm_ready ]; then + /usr/bin/factory-reset-util --displayui & + disable_keys +fi + +## start +echo "Factory Resetting..." +echo "*******************" >> $logfile +echo "Start Factory Reset" >> $logfile +/bin/date >> $logfile + +## stop lcd on-off and turn lcd on +ss_ready=`/bin/ps -ef | /bin/grep -e "deviced\|system_server" | /bin/grep -v grep` +if [ "z$ss_ready" != "z" ]; then + /usr/bin/dbus-send --system --type=method_call --print-reply --reply-timeout=10000 \ + --dest=org.tizen.system.deviced /Org/Tizen/System/DeviceD/Display \ + org.tizen.system.deviced.display.stop +fi + +/bin/sync +if [ "z$FLAG_WITHOUT_CP" == "zyes" ]; then + echo "whithout cp" >> $logfile +else +## shutdown modem + cp_ok=`/bin/ps -ef | /bin/grep telephony-daemon | /bin/grep -v grep` + if [[ "z$cp_ok" != "z" && "z$FLAG_FTRRST_CP" == "zyes" ]]; then + /usr/bin/factory-reset-util --ftrrstcp + echo "factory reset cp" >> $logfile + elif [ "z$cp_ok" != "z" ]; then + # cp service reset is default value. + /usr/bin/factory-reset-util --svcrstcp + echo "cp shutdown" >> $logfile + else + echo "There is no telephony-daemon" >> $logfile + fi +fi + +# reset audio - If not, some targets make noise when rebooting. +#/usr/bin/sound_server --soundreset + +##kill_before_reset + +fs_ready +echo "file system ready for factory reset" >> $logfile + +# For "Find my mobile" feature, it should work after factory reset. +# So backup its data. +#if [ -r /usr/share/oma-dm-cfg/factory_reset/dm_backup.sh ]; then +# echo "backup oma dm data" +# /usr/share/oma-dm-cfg/factory_reset/dm_backup.sh +#fi + +## Restore +echo "Delete & Restore System Files..." >> $logfile + +#remove all files +rmlist=`/bin/ls -1 ${OPT}` +for i in $rmlist +do + # except /opt/system. because there are csc configurations and they are + # updated after binary downloaded. And they should be preserved. + if [ "$i" != "system" ]; then + /bin/rm -rf ${OPT}$i + fi +done + +# For Tizen CC (Mobile Device Fundamentals Protection Profile Common Criteria) +if [ -r /usr/bin/resetCCMode ]; then + echo "MDFPP enabled" >> $logfile + /usr/bin/resetCCMode >> $logfile +fi + +/sbin/fstrim -v $USRDATADIR >> $logfile + +# Delete finished. Restore starts here. +cd / +$RSTCMD +echo "$RSTCMD return $?" >> $logfile + +mret=`/bin/grep "$USRDATADIR " /proc/mounts | /bin/grep rw | /usr/bin/awk '{print $2}'` +if [ "z$mret" = "z" ]; then + echo "$USRDATADIR is not RW MOUNTED, RESTORATION : FAIL" >> $logfile + /bin/rm -rf $USRDATADIR + check_fail +fi + +#check_verify +/usr/bin/factoryreset-verify.sh >> ${logfile} +check_fail + +/bin/mkdir -p /opt/var/log/ +/bin/touch /opt/var/log/Xorg.0.log + +/usr/bin/rstsmack ${rdir}smack_label.txt >> $logfile + +# Removable preload tpk apps will be installed by package manager +if [ -e $INSTALL_ROOT/usr/packages/restore_exception.list ]; then + pkgcmd -f +fi + +cd ${CURRENTPATH} +/bin/date >> $logfile + +## Factory Reset Count +cnt_file="/opt/system/factory-reset-cnt" +if [ -r $cnt_file ]; then + reset_cnt=`/bin/cat $cnt_file` + reset_cnt=$(($reset_cnt+1)) + echo $reset_cnt > $cnt_file +else + echo 1 > $cnt_file + /usr/bin/chsmack -a "*" $cnt_file + /bin/chmod 644 $cnt_file +fi + +/bin/sync + +echo "Finish FactoryReset..." + |