1# Copyright (C) 2016 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
15class Bootctl(object):
16    def __init__(self, device):
17        self.device = device
18        self.base = ["bootctl"]
19
20    def _exec(self, cmd):
21        return self.device.shell_nocheck(self.base + [cmd])
22
23    def get_number_slots(self):
24        """returns number of slots"""
25
26        return int(self._exec("get-number-slots")[1])
27
28    def get_current_slot(self):
29        """returns current slot number"""
30
31        return int(self._exec("get-current-slot")[1])
32
33    def mark_boot_successful(self):
34        """returns true on success, false on failure"""
35
36        return self._exec("mark-boot-successful")[0] == 0
37
38    def set_active_boot_slot(self, slot):
39        """returns true on success, false on failure"""
40
41        return self._exec("set-active-boot-slot " + str(slot))[0] == 0
42
43    def set_slot_as_unbootable_slot(self, slot):
44        """returns true on success, false on failure"""
45
46        return self._exec("set-slot-as-unbootable " + str(slot))[0] == 0
47
48    def is_slot_bootable(self, slot):
49        """Returns true if slot is bootable"""
50
51        return self._exec("is-slot-bootable " + str(slot))[0] == 0
52
53    def is_slot_marked_successful(self, slot):
54        """returns true on success, false on failure"""
55
56        return self._exec("is-slot-marked-successful " + str(slot))[0] == 0
57
58    def get_suffix(self, slot):
59        """returns suffix string for specified slot number"""
60
61        return self._exec("get-suffix " + str(slot))[1].strip()
62