xref: /aosp_15_r20/external/angle/build/fuchsia/test/boot_device.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Copyright 2023 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Functionalities to reliably reboot the device."""
5
6import enum
7
8from typing import Optional
9
10class BootMode(enum.Enum):
11    """Specifies boot mode for device."""
12    REGULAR = enum.auto()
13    RECOVERY = enum.auto()
14    BOOTLOADER = enum.auto()
15
16
17class StateTransitionError(Exception):
18    """Raised when target does not transition to desired state."""
19
20
21def boot_device(target_id: Optional[str],
22                mode: BootMode,
23                serial_num: Optional[str] = None,
24                must_boot: bool = False) -> None:
25    """Boot device into desired mode.
26
27    Args:
28        target_id: Optional target_id of device.
29        mode: Desired boot mode.
30        must_boot: Forces device to boot, regardless of current state.
31    Raises:
32        StateTransitionError: When final state of device is not desired.
33    """
34    # Avoid cycle dependency.
35    # This file will be replaced with serial_boot_device quite soon, later one
36    # should be much more reliable comparing to ffx target list and ssh. So
37    # changing the file structure is not necessary in the current situation.
38    # pylint: disable=cyclic-import, import-outside-toplevel
39    # pylint: disable=wrong-import-position
40    import serial_boot_device
41    if not serial_boot_device.boot_device(target_id, serial_num, mode,
42                                          must_boot):
43        raise StateTransitionError(
44            f'Could not get device to desired state {mode}.')
45