xref: /aosp_15_r20/system/extras/tests/bootloader/bootctl.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2016 The Android Open Source Project
2*288bf522SAndroid Build Coastguard Worker#
3*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*288bf522SAndroid Build Coastguard Worker#
7*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*288bf522SAndroid Build Coastguard Worker#
9*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*288bf522SAndroid Build Coastguard Worker# limitations under the License.
14*288bf522SAndroid Build Coastguard Worker
15*288bf522SAndroid Build Coastguard Workerclass Bootctl(object):
16*288bf522SAndroid Build Coastguard Worker    def __init__(self, device):
17*288bf522SAndroid Build Coastguard Worker        self.device = device
18*288bf522SAndroid Build Coastguard Worker        self.base = ["bootctl"]
19*288bf522SAndroid Build Coastguard Worker
20*288bf522SAndroid Build Coastguard Worker    def _exec(self, cmd):
21*288bf522SAndroid Build Coastguard Worker        return self.device.shell_nocheck(self.base + [cmd])
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker    def get_number_slots(self):
24*288bf522SAndroid Build Coastguard Worker        """returns number of slots"""
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Worker        return int(self._exec("get-number-slots")[1])
27*288bf522SAndroid Build Coastguard Worker
28*288bf522SAndroid Build Coastguard Worker    def get_current_slot(self):
29*288bf522SAndroid Build Coastguard Worker        """returns current slot number"""
30*288bf522SAndroid Build Coastguard Worker
31*288bf522SAndroid Build Coastguard Worker        return int(self._exec("get-current-slot")[1])
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Worker    def mark_boot_successful(self):
34*288bf522SAndroid Build Coastguard Worker        """returns true on success, false on failure"""
35*288bf522SAndroid Build Coastguard Worker
36*288bf522SAndroid Build Coastguard Worker        return self._exec("mark-boot-successful")[0] == 0
37*288bf522SAndroid Build Coastguard Worker
38*288bf522SAndroid Build Coastguard Worker    def set_active_boot_slot(self, slot):
39*288bf522SAndroid Build Coastguard Worker        """returns true on success, false on failure"""
40*288bf522SAndroid Build Coastguard Worker
41*288bf522SAndroid Build Coastguard Worker        return self._exec("set-active-boot-slot " + str(slot))[0] == 0
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker    def set_slot_as_unbootable_slot(self, slot):
44*288bf522SAndroid Build Coastguard Worker        """returns true on success, false on failure"""
45*288bf522SAndroid Build Coastguard Worker
46*288bf522SAndroid Build Coastguard Worker        return self._exec("set-slot-as-unbootable " + str(slot))[0] == 0
47*288bf522SAndroid Build Coastguard Worker
48*288bf522SAndroid Build Coastguard Worker    def is_slot_bootable(self, slot):
49*288bf522SAndroid Build Coastguard Worker        """Returns true if slot is bootable"""
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Worker        return self._exec("is-slot-bootable " + str(slot))[0] == 0
52*288bf522SAndroid Build Coastguard Worker
53*288bf522SAndroid Build Coastguard Worker    def is_slot_marked_successful(self, slot):
54*288bf522SAndroid Build Coastguard Worker        """returns true on success, false on failure"""
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker        return self._exec("is-slot-marked-successful " + str(slot))[0] == 0
57*288bf522SAndroid Build Coastguard Worker
58*288bf522SAndroid Build Coastguard Worker    def get_suffix(self, slot):
59*288bf522SAndroid Build Coastguard Worker        """returns suffix string for specified slot number"""
60*288bf522SAndroid Build Coastguard Worker
61*288bf522SAndroid Build Coastguard Worker        return self._exec("get-suffix " + str(slot))[1].strip()
62