blob: 499a2ff0ed1a5931774eaf1761e1bbdea3f65a17 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Device management
### flash and, optionally, pave a connected device
## usage: fx flash [-s <serial>] [--build|--no-build] [--pave]
## --build | --no-build Build (or not) the required dependencies
## --pave Pave device after flashing (recommended)
## -s Serial of device you wish to flash to (only necessary
## if multiple devices in fastboot mode)
## --skip-verify Skip hardware verification. This is dangerous, please be
## sure the images you are flashing match the device. Only
## supported with ffx
## --no-bootloader-reboot Don't reboot bootloader after flashing firmware or
## recovery fastboot image.
##
## Defaults are defined by the "incremental" feature:
## 'fx --enable=incremental flash' defaults to "--build"
## 'fx --disable=incremental flash' defaults to "--no-build"
set -e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/fx-flash.sh || exit $?
fx-config-read
usage() {
fx-command-help
echo "Available devices:"
fx-command-run host-tool fastboot devices -l
exit 1
}
build=false
if is_feature_enabled "incremental"; then
# In incremental workflows, these defaults have changed.
# Keep old behavior if incremental is not enabled.
build=true
fi
pave=false
serial=
device=$(get-device-name)
skip_verify=false
no_bootloader_reboot=false
while [[ $# -ge 1 ]]; do
case "$1" in
-h|--help)
usage
;;
--no-build)
build=false
;;
--build)
build=true
;;
--pave)
pave=true
;;
--nopave|--no-pave)
pave=false
;;
-s)
shift
serial="$1"
;;
-device)
shift
device="$1"
;;
--skip-verify)
skip_verify=true
;;
--no-bootloader-reboot)
no_bootloader_reboot=true
;;
*)
fx-error "Unknown option $1."
usage
exit 1
esac
shift
done
get_flash_source_args=()
if [[ "${build}" == "true" ]]; then
get_flash_source_args+=(--build)
fi
flash_source="$(fx-command-run get-flash-source "${get_flash_source_args[@]}")"
case "${flash_source}" in
flash-manifest:*)
flash_manifest="${FUCHSIA_BUILD_DIR}"/"${flash_source#flash-manifest:}"
product_bundle=
artifacts=($(fx-command-run list-build-artifacts flash)) || exit $?
if $pave; then
artifacts+=($(fx-command-run list-build-artifacts pave)) || exit $?
fi
;;
product-bundle:*)
flash_manifest=
product_bundle="${FUCHSIA_BUILD_DIR}"/"${flash_source#product-bundle:}"
artifacts="${flash_source#product-bundle:}/product_bundle.json"
;;
*)
fx-error "Unsupported flash source format: ${flash_source}"
exit 1
esac
if $build; then
fx-info "Building/refreshing targets ${artifacts[@]}"
fx-command-run build "${artifacts[@]}"
fi
if is-remote-workflow-device; then
fx-error "It is not possible to flash a device over a remote tunnel, as flashing requires a direct USB connection."
fx-error "Use \`fx flash-remote\` from the local side of the link in these secarios."
fx-error "A lightweight build on the local side is: \`fx set core.x64 && fx self-build\`"
echo >&2
fx-error "If you are not using a remote workflow, run \`fx unset-device\` to remove the remote device configuration."
exit 1
fi
cd "${FUCHSIA_BUILD_DIR}"
fx-flash "${serial}" "${device}" "${flash_manifest}" "${product_bundle}" "${skip_verify}" "${no_bootloader_reboot}"
if $pave; then
# no need to build, since all dependencies were already built above.
fx-command-exec pave -1 --no-build
fi