blob: 4e3c21b8a413818e7ebfbfb67329b8543df8f1d1 [file] [log] [blame]
Kelvin Zhang121bec52022-05-31 14:43:59 -07001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "BootControl.h"
18#include <cstdint>
19
20#include <android-base/logging.h>
21
22using HIDLMergeStatus = ::android::bootable::BootControl::MergeStatus;
23using ndk::ScopedAStatus;
24
25namespace aidl::android::hardware::boot {
26
27BootControl::BootControl() {
28 CHECK(impl_.Init());
29}
30
31ScopedAStatus BootControl::getActiveBootSlot(int32_t* _aidl_return) {
32 *_aidl_return = impl_.GetActiveBootSlot();
33 return ScopedAStatus::ok();
34}
35
36ScopedAStatus BootControl::getCurrentSlot(int32_t* _aidl_return) {
37 *_aidl_return = impl_.GetCurrentSlot();
38 return ScopedAStatus::ok();
39}
40
41ScopedAStatus BootControl::getNumberSlots(int32_t* _aidl_return) {
42 *_aidl_return = impl_.GetNumberSlots();
43 return ScopedAStatus::ok();
44}
45
46namespace {
47
48static constexpr MergeStatus ToAIDLMergeStatus(HIDLMergeStatus status) {
49 switch (status) {
50 case HIDLMergeStatus::NONE:
51 return MergeStatus::NONE;
52 case HIDLMergeStatus::UNKNOWN:
53 return MergeStatus::UNKNOWN;
54 case HIDLMergeStatus::SNAPSHOTTED:
55 return MergeStatus::SNAPSHOTTED;
56 case HIDLMergeStatus::MERGING:
57 return MergeStatus::MERGING;
58 case HIDLMergeStatus::CANCELLED:
59 return MergeStatus::CANCELLED;
60 }
61}
62
63static constexpr HIDLMergeStatus ToHIDLMergeStatus(MergeStatus status) {
64 switch (status) {
65 case MergeStatus::NONE:
66 return HIDLMergeStatus::NONE;
67 case MergeStatus::UNKNOWN:
68 return HIDLMergeStatus::UNKNOWN;
69 case MergeStatus::SNAPSHOTTED:
70 return HIDLMergeStatus::SNAPSHOTTED;
71 case MergeStatus::MERGING:
72 return HIDLMergeStatus::MERGING;
73 case MergeStatus::CANCELLED:
74 return HIDLMergeStatus::CANCELLED;
75 }
76}
77
78}
79
80ScopedAStatus BootControl::getSnapshotMergeStatus(MergeStatus* _aidl_return) {
81 *_aidl_return = ToAIDLMergeStatus(impl_.GetSnapshotMergeStatus());
82 return ScopedAStatus::ok();
83}
84
85ScopedAStatus BootControl::getSuffix(int32_t in_slot, std::string* _aidl_return) {
86 if (!impl_.IsValidSlot(in_slot)) {
87 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
88 INVALID_SLOT, (std::string("Invalid slot ") + std::to_string(in_slot)).c_str());
89 }
90 *_aidl_return = impl_.GetSuffix(in_slot);
91 return ScopedAStatus::ok();
92}
93
94ScopedAStatus BootControl::isSlotBootable(int32_t in_slot, bool* _aidl_return) {
95 if (!impl_.IsValidSlot(in_slot)) {
96 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
97 INVALID_SLOT, (std::string("Invalid slot ") + std::to_string(in_slot)).c_str());
98 }
99 *_aidl_return = impl_.IsSlotBootable(in_slot);
100 return ScopedAStatus::ok();
101}
102
103ScopedAStatus BootControl::isSlotMarkedSuccessful(int32_t in_slot, bool* _aidl_return) {
104 if (!impl_.IsValidSlot(in_slot)) {
105 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
106 INVALID_SLOT, (std::string("Invalid slot ") + std::to_string(in_slot)).c_str());
107 }
108 *_aidl_return = impl_.IsSlotMarkedSuccessful(in_slot);
109 return ScopedAStatus::ok();
110}
111
112ScopedAStatus BootControl::markBootSuccessful() {
113 if (!impl_.MarkBootSuccessful()) {
114 return ScopedAStatus::fromServiceSpecificErrorWithMessage(COMMAND_FAILED,
115 "Operation failed");
116 }
117 return ScopedAStatus::ok();
118}
119
120ScopedAStatus BootControl::setActiveBootSlot(int32_t in_slot) {
121 if (!impl_.IsValidSlot(in_slot)) {
122 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
123 INVALID_SLOT, (std::string("Invalid slot ") + std::to_string(in_slot)).c_str());
124 }
125 if (!impl_.SetActiveBootSlot(in_slot)) {
126 return ScopedAStatus::fromServiceSpecificErrorWithMessage(COMMAND_FAILED,
127 "Operation failed");
128 }
129 return ScopedAStatus::ok();
130}
131
132ScopedAStatus BootControl::setSlotAsUnbootable(int32_t in_slot) {
133 if (!impl_.IsValidSlot(in_slot)) {
134 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
135 INVALID_SLOT, (std::string("Invalid slot ") + std::to_string(in_slot)).c_str());
136 }
137 if (!impl_.SetSlotAsUnbootable(in_slot)) {
138 return ScopedAStatus::fromServiceSpecificErrorWithMessage(COMMAND_FAILED,
139 "Operation failed");
140 }
141 return ScopedAStatus::ok();
142}
143
144ScopedAStatus BootControl::setSnapshotMergeStatus(MergeStatus in_status) {
145 if (!impl_.SetSnapshotMergeStatus(ToHIDLMergeStatus(in_status))) {
146 return ScopedAStatus::fromServiceSpecificErrorWithMessage(COMMAND_FAILED,
147 "Operation failed");
148 }
149 return ScopedAStatus::ok();
150}
151
152} // namespace aidl::android::hardware::boot