1#!/system/bin/sh
2
3#
4# Copyright (C) 2017 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# This script will extract ASAN libraries from /system/asan.tar.gz to /data and then reboot.
20
21# TODO:
22#   * Timestamp or something to know when to run this again. Right now take the existence of
23#     /data/lib as we're already done.
24#   * Need to distinguish pre- from post-decryption for FDE.
25
26SRC=/system/asan.tar.bz2
27MD5_FILE=/data/asan.md5sum
28ASAN_DIR=/data/asan
29# Minimum /data size in blocks. Arbitrarily 512M.
30MIN_DATA_SIZE=131072
31
32# Checks for FDE pre-decrypt state.
33
34VOLD_STATUS=$(getprop vold.decrypt)
35if [ "$VOLD_STATUS" = "trigger_restart_min_framework" ] ; then
36  log -p i -t asan_install "Pre-decrypt FDE detected (by vold property)!"
37  exit 1
38fi
39
40STATFS_BLOCKS=$(stat -f -c '%b' /data)
41if [ "$STATFS_BLOCKS" -le "$MIN_DATA_SIZE" ] ; then
42  log -p i -t asan_install "Pre-decrypt FDE detected (by /data size)!"
43  exit 1
44fi
45
46# Check for ASAN source.
47
48if ! test -f $SRC ; then
49  log -p i -t asan_install "Did not find $SRC!"
50  exit 1
51fi
52
53log -p i -t asan_install "Found $SRC, checking whether we need to apply it."
54
55# Checksum check.
56
57ASAN_TAR_MD5=$(md5sum $SRC)
58if test -f $MD5_FILE ; then
59  INSTALLED_MD5=$(cat $MD5_FILE)
60  if [ "x$ASAN_TAR_MD5" = "x$INSTALLED_MD5" ] ; then
61    log -p i -t asan_install "Checksums match, nothing to be done here."
62    exit 0
63  fi
64fi
65
66# Actually apply the source.
67
68# Just clean up, helps with restorecon.
69rm -rf $ASAN_DIR
70
71log -p i -t asan_install "Untarring $SRC..."
72
73# Unzip from /system/asan.tar.gz into data. Need to pipe as gunzip is not on device.
74bzip2 -c -d $SRC | tar -x -f - --no-same-owner -C / || exit 1
75
76# Cannot log here, log would run with system_data_file.
77
78# Set correct permission bits.
79chmod -R 744 $ASAN_DIR
80cd $ASAN_DIR ; find . -type d -exec chmod 755 {} \;
81
82restorecon -R -F $ASAN_DIR/*/lib*
83
84log -p i -t asan_install "Fixed selinux labels..."
85
86
87# Now write down our checksum to mark the extraction complete.
88echo "$ASAN_TAR_MD5" > $MD5_FILE
89
90# We want to reboot now. It seems it is not possible to run "reboot" here, the device will
91# just be stuck.
92
93log -p i -t asan_install "Signaling init to reboot..."
94
95setprop sys.powerctl reboot
96