1# Copyright 2017 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Fastboot-related commands."""
16
17from gsi_util.utils.cmd_utils import run_command
18
19
20def flash(partition_name, image_name=None, allow_error=False):
21  """fastboot flash a partition with a given image."""
22
23  cmd = ['fastboot', 'flash', partition_name]
24
25  # image_name can be None, for `fastboot` to flash
26  # ${ANDROID_PRODUCT_OUT}/{partition_name}.img.
27  if image_name is not None:
28    cmd.append(image_name)
29
30  run_command(cmd, raise_on_error=not allow_error)
31
32
33def erase(partition_name=None, allow_error=False):
34  """fastboot erase a partition."""
35
36  if partition_name is None:
37    run_command(['fastboot', '-w'], raise_on_error=not allow_error)
38  else:
39    run_command(['fastboot', 'erase', partition_name],
40                raise_on_error=not allow_error)
41
42
43def reboot():
44  """fastboot reboot a device."""
45  run_command(['fastboot', 'reboot'], raise_on_error=False)
46