aboutsummaryrefslogtreecommitdiff
path: root/devices/flash_device.sh
blob: 3df38336afe469045ec5ce4fc1b8144343de4f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#
# Copyright (c) 2016-2017, Linaro Ltd.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Used for updating the Nexus system images.

# local_path is defined in the script sourcing this file
# shellcheck disable=SC2154
source "${local_path}/../utils/utils.sh"
source "${local_path}/../utils/utils_android.sh"
source "${local_path}/../utils/utils_android_root.sh"

temp_dir=
booted_to_fastboot="false"

usage() {
  log I "$0"
  log I "Script used to update the device image."
  log I "Assumes you have exported in your environment the following variables:"
  log I " SNAPSHOTS_URL := link to the Jenkins .img artifacts;"
  log I " BUILD_NO := the build that you would like to try;"
  log I " BOARD := the device that you will be flashing;"
  log I " -h - help"
  log I " -v - verbose"
  log I " -f - skip the adb checks, the device is booted to fastboot"
}

argument_parser() {
  while getopts ":vhf" opt; do
    case ${opt} in
      f)
        booted_to_fastboot="true"
        ;;
      v)
        enable_verbose
        ;;
      h)
        usage
        exit 0
        ;;
      \?)
        log E "Invalid option: ${OPTARG}"
        abort
        ;;
    esac
  done
  readonly booted_to_fastboot
}

download_image() {
  log I "Downloading $1 to ${temp_dir} ... "
  safe wget "${SNAPSHOTS_URL}/${BUILD_TARGET}/${BUILD_NO}/$1" -O "${temp_dir}/$1"
}


clean_up() {
  log I "Removing temporary directory: ${temp_dir} ."
  safe rm -fr "${temp_dir}"
}

download_images() {
  # Create a temporary folder.
  temp_dir=$(mktemp -d)
  readonly temp_dir

  # Don't leave a mess behind and make sure we clean-up before exiting.
  trap clean_up EXIT

  if [[ -v BOOTLOADER_IMAGE ]]; then
    download_image "${BOOTLOADER_IMAGE}"
  fi
  if [[ -v RADIO_IMAGE ]]; then
    download_image "${RADIO_IMAGE}"
  fi
  for image in "${USERSPACE_IMAGES[@]}"; do
    download_image "${image}"
  done
}

fastboot_flash_image() {
  log I "Flashing $2 to partition $1 ..."
  safe fastboot flash "$1" "${temp_dir}/$2"
}

fastboot_flash_images() {
  if [[ -v BOOTLOADER_IMAGE ]]; then
    fastboot_flash_image bootloader "${BOOTLOADER_IMAGE}"
    safe fastboot reboot bootloader
    sleep 5
  fi

  if [[ -v RADIO_IMAGE ]]; then
    fastboot_flash_image radio "${RADIO_IMAGE}"
    safe fastboot reboot bootloader
    sleep 5
  fi

  for partition in "${!USERSPACE_IMAGES[@]}"; do
    fastboot_flash_image "${partition}" "${USERSPACE_IMAGES[$partition]}"
  done

  if [[ -v SLOT ]]; then
    safe fastboot --set-active="${SLOT}"
  fi

  if ! fastboot format userdata; then
    safe fastboot erase userdata
  fi
  safe fastboot erase metadata
}

adb_check_device() {
  # Check that there is a device connected.
  if ! adb get-state>/dev/null 2>&1; then
    log E "There is no adb device available. Is your device booted to fastboot?"
    log E "If yes re-run the script with the \"-f\" option."
    exit 1
  fi

  # Check that the device is what we expect.
  local adb_device=$(retrieve_target_product_name)
  if [[ ! ${adb_device} =~ ${BOARD} ]]; then
    log E "Unsupported device ${adb_device}."
    log E "This script works for ${BOARD}."
    exit 1
  fi
}

build_fastboot() {
  log I "Building fastboot..."
  # We want to make sure that fastboot is built from source and it is not part
  # of an old SDK.

  # Note: Older fastboot versions are know to fail at flashing new system images.
  # Recent version of fastboot that have a "--version" command line option should work.

  source_android_environment_default
  # Note: We do not care about the lunch target, but we need to run `lunch` to
  # bring the built `adb` and `fastboot` binaries into the PATH.
  setup_android_target aosp_arm64-userdebug
  safe make adb fastboot
}

check_environemnt() {
  if ! [[ -v SNAPSHOTS_URL && -v BUILD_NO && -v BOARD ]]; then
    log E "Your environemnt does not have the following one of the following variables:"
    log E " SNAPSHOTS_URL := link to the Jenkins .img artifacts;"
    log E " BUILD_NO := the build that you would like to try;"
    log E " BOARD := the device that you will be flashing;"
    exit 1
  fi
}

main() {
  argument_parser "$@"

  check_environemnt
  build_fastboot

  if ! ${booted_to_fastboot}; then
    adb_check_device
    safe adb reboot-bootloader
  fi
  download_images
  fastboot_flash_images
  safe fastboot reboot
}

main "$@"