Elliott Hughes | fbfa0e8 | 2018-04-03 13:42:18 -0700 | [diff] [blame] | 1 | # /* vim: set ai ts=4 ft=sh: */ |
| 2 | # |
| 3 | # Copyright 2017, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | _fastboot() { |
| 19 | if ! type -t "$1" >/dev/null; then |
| 20 | return |
| 21 | fi |
| 22 | |
| 23 | if type -t _init_completion >/dev/null; then |
| 24 | _init_completion || return |
| 25 | fi |
| 26 | |
| 27 | local where i cur serial |
| 28 | COMPREPLY=() |
| 29 | |
| 30 | serial="${ANDROID_SERIAL:-none}" |
| 31 | where=OPTIONS |
| 32 | for ((i=1; i <= COMP_CWORD; i++)); do |
| 33 | cur="${COMP_WORDS[i]}" |
| 34 | case "${cur}" in |
| 35 | -s) |
| 36 | where=OPT_SERIAL |
| 37 | ;; |
| 38 | --slot) |
| 39 | where=OPT_SLOT |
| 40 | ;; |
| 41 | -*) |
| 42 | where=OPTIONS |
| 43 | ;; |
| 44 | *) |
| 45 | if [[ $where == OPT_SERIAL ]]; then |
| 46 | where=OPT_SERIAL_ARG |
| 47 | serial=${cur} |
| 48 | elif [[ $where == OPT_SLOT ]]; then |
| 49 | where=OPT_SLOT_ARG |
| 50 | else |
| 51 | where=COMMAND |
| 52 | break |
| 53 | fi |
| 54 | ;; |
| 55 | esac |
| 56 | done |
| 57 | |
| 58 | if [[ $where == COMMAND && $i -ge $COMP_CWORD ]]; then |
| 59 | where=OPTIONS |
| 60 | fi |
| 61 | |
Elliott Hughes | b2b9015 | 2018-04-04 13:43:56 -0700 | [diff] [blame] | 62 | OPTIONS="-a -c --disable-verification --disable-verity -h --help -s --set-active --skip-secondary --skip-reboot --slot -u --version -w" |
Elliott Hughes | fbfa0e8 | 2018-04-03 13:42:18 -0700 | [diff] [blame] | 63 | COMMAND="continue devices erase flash flashall flashing format getvar get_staged help oem reboot stage update" |
| 64 | |
| 65 | case $where in |
| 66 | OPTIONS|OPT_SERIAL) |
| 67 | COMPREPLY=( $(compgen -W "$OPTIONS $COMMAND" -- "$cur") ) |
| 68 | ;; |
| 69 | OPT_SERIAL_ARG) |
| 70 | local devices=$(command fastboot devices 2> /dev/null | awk '{ print $1 }') |
| 71 | COMPREPLY=( $(compgen -W "${devices}" -- ${cur}) ) |
| 72 | ;; |
| 73 | OPT_SLOT_ARG) |
| 74 | local slots="a all b other" |
| 75 | COMPREPLY=( $(compgen -W "${slots}" -- ${cur}) ) |
| 76 | ;; |
| 77 | COMMAND) |
| 78 | if [[ $i -eq $COMP_CWORD ]]; then |
| 79 | COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") ) |
| 80 | else |
| 81 | i=$((i+1)) |
| 82 | case "${cur}" in |
| 83 | flash) |
| 84 | _fastboot_cmd_flash "$serial" $i |
| 85 | ;; |
| 86 | reboot) |
| 87 | if [[ $COMP_CWORD == $i ]]; then |
Elliott Hughes | bd1e2dd | 2018-04-04 13:51:54 -0700 | [diff] [blame] | 88 | args="bootloader" |
Elliott Hughes | fbfa0e8 | 2018-04-03 13:42:18 -0700 | [diff] [blame] | 89 | COMPREPLY=( $(compgen -W "${args}" -- "${COMP_WORDS[i]}") ) |
| 90 | fi |
| 91 | ;; |
| 92 | update) |
| 93 | _fastboot_cmd_update "$serial" $i |
| 94 | ;; |
| 95 | esac |
| 96 | fi |
| 97 | ;; |
| 98 | esac |
| 99 | |
| 100 | return 0 |
| 101 | } |
| 102 | |
| 103 | _fastboot_cmd_flash() { |
| 104 | local serial i cur |
| 105 | local partitions |
| 106 | |
| 107 | serial=$1 |
| 108 | i=$2 |
| 109 | |
| 110 | cur="${COMP_WORDS[COMP_CWORD]}" |
| 111 | if [[ $i -eq $COMP_CWORD ]]; then |
Elliott Hughes | f0b9c7c | 2018-04-04 13:51:24 -0700 | [diff] [blame] | 112 | partitions="boot bootloader dtbo modem odm oem product radio recovery system vbmeta vendor" |
Elliott Hughes | fbfa0e8 | 2018-04-03 13:42:18 -0700 | [diff] [blame] | 113 | COMPREPLY=( $(compgen -W "$partitions" -- $cur) ) |
| 114 | else |
| 115 | _fastboot_util_complete_local_file "${cur}" '!*.img' |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | _fastboot_cmd_update() { |
| 120 | local serial i cur |
| 121 | |
| 122 | serial=$1 |
| 123 | i=$2 |
| 124 | |
| 125 | cur="${COMP_WORDS[COMP_CWORD]}" |
| 126 | |
| 127 | _fastboot_util_complete_local_file "${cur}" '!*.zip' |
| 128 | } |
| 129 | |
| 130 | _fastboot_util_complete_local_file() { |
| 131 | local file xspec i j IFS=$'\n' |
| 132 | local -a dirs files |
| 133 | |
| 134 | file=$1 |
| 135 | xspec=$2 |
| 136 | |
| 137 | # Since we're probably doing file completion here, don't add a space after. |
| 138 | if [[ $(type -t compopt) = "builtin" ]]; then |
| 139 | compopt -o plusdirs |
| 140 | if [[ "${xspec}" == "" ]]; then |
| 141 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") ) |
| 142 | else |
| 143 | compopt +o filenames |
| 144 | COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") ) |
| 145 | fi |
| 146 | else |
| 147 | # Work-around for shells with no compopt |
| 148 | |
| 149 | dirs=( $(compgen -d -- "${cur}" ) ) |
| 150 | |
| 151 | if [[ "${xspec}" == "" ]]; then |
| 152 | files=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") ) |
| 153 | else |
| 154 | files=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") ) |
| 155 | fi |
| 156 | |
| 157 | COMPREPLY=( $( |
| 158 | for i in "${files[@]}"; do |
| 159 | local skip= |
| 160 | for j in "${dirs[@]}"; do |
| 161 | if [[ $i == $j ]]; then |
| 162 | skip=1 |
| 163 | break |
| 164 | fi |
| 165 | done |
| 166 | [[ -n $skip ]] || printf "%s\n" "$i" |
| 167 | done |
| 168 | )) |
| 169 | |
| 170 | COMPREPLY=( ${COMPREPLY[@]:-} $( |
| 171 | for i in "${dirs[@]}"; do |
| 172 | printf "%s/\n" "$i" |
| 173 | done |
| 174 | )) |
| 175 | fi |
| 176 | } |
| 177 | |
| 178 | if [[ $(type -t compopt) = "builtin" ]]; then |
| 179 | complete -F _fastboot fastboot |
| 180 | else |
| 181 | complete -o nospace -F _fastboot fastboot |
| 182 | fi |