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 | |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 53 | if [ "$(/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 |
| 60 | fi |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 61 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 62 | PREPARE=$(cmd otadexopt prepare) |
Andreas Gampe | 6c05a73 | 2016-06-10 15:08:53 -0700 | [diff] [blame] | 63 | # 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 Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 67 | # Create an array with all dexopt commands in advance, to know how many there are. |
| 68 | otadexopt_cmds=() |
| 69 | while (( ${#otadexopt_cmds[@]} < MAXIMUM_PACKAGES )) ; do |
Martin Stjernholm | 5c6bf39 | 2023-03-01 18:18:15 +0000 | [diff] [blame] | 70 | DONE=$(cmd otadexopt done) |
| 71 | if [ "$DONE" = "OTA complete." ] ; then |
| 72 | break |
| 73 | fi |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 74 | otadexopt_cmds+=("$(cmd otadexopt next)") |
Nikita Ioffe | 32e49e2 | 2021-07-06 17:05:16 +0000 | [diff] [blame] | 75 | done |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 76 | |
| 77 | DONE=$(cmd otadexopt done) |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 78 | cmd otadexopt cleanup |
| 79 | |
| 80 | echo "$0: Using streaming otapreopt_chroot on ${#otadexopt_cmds[@]} packages" |
| 81 | |
| 82 | function print_otadexopt_cmds { |
| 83 | for cmd in "${otadexopt_cmds[@]}" ; do |
| 84 | print "$cmd" |
| 85 | done |
| 86 | } |
| 87 | |
| 88 | function 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 | |
| 96 | print_otadexopt_cmds | \ |
| 97 | /system/bin/otapreopt_chroot $STATUS_FD $TARGET_SLOT_SUFFIX | \ |
| 98 | report_progress |
| 99 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 100 | if [ "$DONE" = "OTA incomplete." ] ; then |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 101 | echo "$0: Incomplete." |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 102 | else |
Martin Stjernholm | 47a9d99 | 2023-07-28 21:43:36 +0100 | [diff] [blame^] | 103 | echo "$0: Complete or error." |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 104 | fi |
| 105 | |
Andreas Gampe | 6c05a73 | 2016-06-10 15:08:53 -0700 | [diff] [blame] | 106 | print -u${STATUS_FD} "global_progress 1.0" |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 107 | |
| 108 | exit 0 |