blob: 28bd7932a2f88e9c2f2757efb08195da9be25e72 [file] [log] [blame]
Andreas Gampe01ad5982016-03-09 16:27:29 -08001#!/system/bin/sh
2
3#
4# Copyright (C) 2016 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
Martin Stjernholm47a9d992023-07-28 21:43:36 +010019# This script runs as a postinstall step to drive otapreopt. It comes with the
20# OTA package, but runs /system/bin/otapreopt_chroot in the (old) active system
21# image. See system/extras/postinst/postinst.sh for some docs.
Andreas Gampe01ad5982016-03-09 16:27:29 -080022
Andreas Gampe0354bd02016-06-27 14:25:30 -070023TARGET_SLOT="$1"
Andreas Gampe6c05a732016-06-10 15:08:53 -070024STATUS_FD="$2"
25
Andreas Gampe01ad5982016-03-09 16:27:29 -080026# Maximum number of packages/steps.
27MAXIMUM_PACKAGES=1000
28
Andreas Gampe90ba9eb2016-08-16 17:43:33 -070029# First ensure the system is booted. This is to work around issues when cmd would
30# infinitely loop trying to get a service manager (which will never come up in that
31# mode). b/30797145
32BOOT_PROPERTY_NAME="dev.bootcomplete"
33
34BOOT_COMPLETE=$(getprop $BOOT_PROPERTY_NAME)
35if [ "$BOOT_COMPLETE" != "1" ] ; then
Martin Stjernholm47a9d992023-07-28 21:43:36 +010036 echo "$0: Error: boot-complete not detected."
Andreas Gampe90ba9eb2016-08-16 17:43:33 -070037 # We must return 0 to not block sideload.
38 exit 0
39fi
40
Andreas Gampe0354bd02016-06-27 14:25:30 -070041# Compute target slot suffix.
42# TODO: Once bootctl is not restricted, we should query from there. Or get this from
43# update_engine as a parameter.
44if [ "$TARGET_SLOT" = "0" ] ; then
45 TARGET_SLOT_SUFFIX="_a"
46elif [ "$TARGET_SLOT" = "1" ] ; then
47 TARGET_SLOT_SUFFIX="_b"
48else
Martin Stjernholm47a9d992023-07-28 21:43:36 +010049 echo "$0: Unknown target slot $TARGET_SLOT"
Andreas Gampe0354bd02016-06-27 14:25:30 -070050 exit 1
51fi
52
Martin Stjernholm47a9d992023-07-28 21:43:36 +010053if [ "$(/system/bin/otapreopt_chroot --version)" != 2 ]; then
54 # We require an updated chroot wrapper that reads dexopt commands from stdin.
55 # Even if we kept compat with the old binary, the OTA preopt wouldn't work due
56 # to missing sepolicy rules, so there's no use spending time trying to dexopt
57 # (b/291974157).
58 echo "$0: Current system image is too old to work with OTA preopt - skipping."
59 exit 0
60fi
Andreas Gampe0354bd02016-06-27 14:25:30 -070061
Andreas Gampe01ad5982016-03-09 16:27:29 -080062PREPARE=$(cmd otadexopt prepare)
Andreas Gampe6c05a732016-06-10 15:08:53 -070063# Note: Ignore preparation failures. Step and done will fail and exit this.
64# This is necessary to support suspends - the OTA service will keep
65# the state around for us.
66
Martin Stjernholm47a9d992023-07-28 21:43:36 +010067# Create an array with all dexopt commands in advance, to know how many there are.
68otadexopt_cmds=()
69while (( ${#otadexopt_cmds[@]} < MAXIMUM_PACKAGES )) ; do
Martin Stjernholm5c6bf392023-03-01 18:18:15 +000070 DONE=$(cmd otadexopt done)
71 if [ "$DONE" = "OTA complete." ] ; then
72 break
73 fi
Martin Stjernholm47a9d992023-07-28 21:43:36 +010074 otadexopt_cmds+=("$(cmd otadexopt next)")
Nikita Ioffe32e49e22021-07-06 17:05:16 +000075done
Andreas Gampe01ad5982016-03-09 16:27:29 -080076
77DONE=$(cmd otadexopt done)
Martin Stjernholm47a9d992023-07-28 21:43:36 +010078cmd otadexopt cleanup
79
80echo "$0: Using streaming otapreopt_chroot on ${#otadexopt_cmds[@]} packages"
81
82function print_otadexopt_cmds {
83 for cmd in "${otadexopt_cmds[@]}" ; do
84 print "$cmd"
85 done
86}
87
88function report_progress {
89 while read count ; do
90 # mksh can't do floating point arithmetic, so emulate a fixed point calculation.
91 (( permilles = 1000 * count / ${#otadexopt_cmds[@]} ))
92 printf 'global_progress %d.%03d\n' $((permilles / 1000)) $((permilles % 1000)) >&${STATUS_FD}
93 done
94}
95
96print_otadexopt_cmds | \
97 /system/bin/otapreopt_chroot $STATUS_FD $TARGET_SLOT_SUFFIX | \
98 report_progress
99
Andreas Gampe01ad5982016-03-09 16:27:29 -0800100if [ "$DONE" = "OTA incomplete." ] ; then
Martin Stjernholm47a9d992023-07-28 21:43:36 +0100101 echo "$0: Incomplete."
Andreas Gampe01ad5982016-03-09 16:27:29 -0800102else
Martin Stjernholm47a9d992023-07-28 21:43:36 +0100103 echo "$0: Complete or error."
Andreas Gampe01ad5982016-03-09 16:27:29 -0800104fi
105
Andreas Gampe6c05a732016-06-10 15:08:53 -0700106print -u${STATUS_FD} "global_progress 1.0"
Andreas Gampe01ad5982016-03-09 16:27:29 -0800107
108exit 0