Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 1 | #!/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 Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 19 | # 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 Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 22 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 23 | TARGET_SLOT="$1" |
Andreas Gampe | 6c05a73 | 2016-06-10 15:08:53 -0700 | [diff] [blame] | 24 | STATUS_FD="$2" |
| 25 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 26 | # Maximum number of packages/steps. |
| 27 | MAXIMUM_PACKAGES=1000 |
| 28 | |
Andreas Gampe | 90ba9eb | 2016-08-16 17:43:33 -0700 | [diff] [blame] | 29 | # 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 |
| 32 | BOOT_PROPERTY_NAME="dev.bootcomplete" |
| 33 | |
| 34 | BOOT_COMPLETE=$(getprop $BOOT_PROPERTY_NAME) |
| 35 | if [ "$BOOT_COMPLETE" != "1" ] ; then |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 36 | echo "$0: Error: boot-complete not detected." |
Andreas Gampe | 90ba9eb | 2016-08-16 17:43:33 -0700 | [diff] [blame] | 37 | # We must return 0 to not block sideload. |
| 38 | exit 0 |
| 39 | fi |
| 40 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 41 | # 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. |
| 44 | if [ "$TARGET_SLOT" = "0" ] ; then |
| 45 | TARGET_SLOT_SUFFIX="_a" |
| 46 | elif [ "$TARGET_SLOT" = "1" ] ; then |
| 47 | TARGET_SLOT_SUFFIX="_b" |
| 48 | else |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 49 | echo "$0: Unknown target slot $TARGET_SLOT" |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 50 | exit 1 |
| 51 | fi |
| 52 | |
Jiakai Zhang | 02989be | 2024-08-28 15:23:11 +0000 | [diff] [blame] | 53 | # A source that infinitely emits arbitrary lines. |
| 54 | # When connected to STDIN of another process, this source keeps STDIN open until |
| 55 | # the consumer process closes STDIN or this script dies. |
| 56 | function infinite_source { |
| 57 | while echo .; do |
| 58 | sleep 1 |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | PR_DEXOPT_JOB_VERSION="$(pm art pr-dexopt-job --version)" |
| 63 | if (( $? == 0 )) && (( $PR_DEXOPT_JOB_VERSION >= 3 )); then |
| 64 | # Delegate to Pre-reboot Dexopt, a feature of ART Service. |
| 65 | # ART Service decides what to do with this request: |
| 66 | # - If Pre-reboot Dexopt is disabled or unsupported, the command returns |
| 67 | # non-zero. This is always the case if the current system is Android 14 or |
| 68 | # earlier. |
| 69 | # - If Pre-reboot Dexopt is enabled in synchronous mode, the command blocks |
| 70 | # until Pre-reboot Dexopt finishes, and returns zero no matter it succeeds or |
| 71 | # not. This is the default behavior if the current system is Android 15. |
| 72 | # - If Pre-reboot Dexopt is enabled in asynchronous mode, the command schedules |
| 73 | # an asynchronous job and returns 0 immediately. The job will then run by the |
| 74 | # job scheduler when the device is idle and charging. |
| 75 | if infinite_source | pm art on-ota-staged --slot "$TARGET_SLOT_SUFFIX"; then |
| 76 | # Handled by Pre-reboot Dexopt. |
| 77 | exit 0 |
| 78 | fi |
| 79 | echo "Pre-reboot Dexopt not enabled. Fall back to otapreopt." |
| 80 | else |
| 81 | echo "Pre-reboot Dexopt is too old. Fall back to otapreopt." |
| 82 | fi |
| 83 | |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 84 | if [ "$(/system/bin/otapreopt_chroot --version)" != 2 ]; then |
| 85 | # We require an updated chroot wrapper that reads dexopt commands from stdin. |
| 86 | # Even if we kept compat with the old binary, the OTA preopt wouldn't work due |
| 87 | # to missing sepolicy rules, so there's no use spending time trying to dexopt |
| 88 | # (b/291974157). |
| 89 | echo "$0: Current system image is too old to work with OTA preopt - skipping." |
| 90 | exit 0 |
| 91 | fi |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 92 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 93 | PREPARE=$(cmd otadexopt prepare) |
Andreas Gampe | 6c05a73 | 2016-06-10 15:08:53 -0700 | [diff] [blame] | 94 | # Note: Ignore preparation failures. Step and done will fail and exit this. |
| 95 | # This is necessary to support suspends - the OTA service will keep |
| 96 | # the state around for us. |
| 97 | |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 98 | # Create an array with all dexopt commands in advance, to know how many there are. |
| 99 | otadexopt_cmds=() |
| 100 | while (( ${#otadexopt_cmds[@]} < MAXIMUM_PACKAGES )) ; do |
Martin Stjernholm | 5c6bf39 | 2023-03-01 18:18:15 +0000 | [diff] [blame] | 101 | DONE=$(cmd otadexopt done) |
| 102 | if [ "$DONE" = "OTA complete." ] ; then |
| 103 | break |
| 104 | fi |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 105 | otadexopt_cmds+=("$(cmd otadexopt next)") |
Nikita Ioffe | 32e49e2 | 2021-07-06 17:05:16 +0000 | [diff] [blame] | 106 | done |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 107 | |
| 108 | DONE=$(cmd otadexopt done) |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 109 | cmd otadexopt cleanup |
| 110 | |
| 111 | echo "$0: Using streaming otapreopt_chroot on ${#otadexopt_cmds[@]} packages" |
| 112 | |
| 113 | function print_otadexopt_cmds { |
| 114 | for cmd in "${otadexopt_cmds[@]}" ; do |
| 115 | print "$cmd" |
| 116 | done |
| 117 | } |
| 118 | |
| 119 | function report_progress { |
| 120 | while read count ; do |
| 121 | # mksh can't do floating point arithmetic, so emulate a fixed point calculation. |
| 122 | (( permilles = 1000 * count / ${#otadexopt_cmds[@]} )) |
| 123 | printf 'global_progress %d.%03d\n' $((permilles / 1000)) $((permilles % 1000)) >&${STATUS_FD} |
| 124 | done |
| 125 | } |
| 126 | |
| 127 | print_otadexopt_cmds | \ |
| 128 | /system/bin/otapreopt_chroot $STATUS_FD $TARGET_SLOT_SUFFIX | \ |
| 129 | report_progress |
| 130 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 131 | if [ "$DONE" = "OTA incomplete." ] ; then |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 132 | echo "$0: Incomplete." |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 133 | else |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame] | 134 | echo "$0: Complete or error." |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 135 | fi |
| 136 | |
Andreas Gampe | 6c05a73 | 2016-06-10 15:08:53 -0700 | [diff] [blame] | 137 | print -u${STATUS_FD} "global_progress 1.0" |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 138 | |
| 139 | exit 0 |