1 /*
2 * Copyright (C) 2016 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 <optional>
18 #include <sstream>
19
20 #include <sysexits.h>
21 #include <android/hardware/boot/1.1/IBootControl.h>
22
23 using android::sp;
24
25 using android::hardware::hidl_string;
26 using android::hardware::Return;
27
28 using android::hardware::boot::V1_0::BoolResult;
29 using android::hardware::boot::V1_0::CommandResult;
30 using android::hardware::boot::V1_0::Slot;
31 using android::hardware::boot::V1_1::IBootControl;
32 using android::hardware::boot::V1_1::MergeStatus;
33
34 namespace V1_0 = android::hardware::boot::V1_0;
35 namespace V1_1 = android::hardware::boot::V1_1;
36
37 enum BootCtlVersion { BOOTCTL_V1_0, BOOTCTL_V1_1 };
38
usage(FILE * where,BootCtlVersion bootVersion,int,char * argv[])39 static void usage(FILE* where, BootCtlVersion bootVersion, int /* argc */, char* argv[])
40 {
41 fprintf(where,
42 "%s - command-line wrapper for the boot HAL.\n"
43 "\n"
44 "Usage:\n"
45 " %s COMMAND\n"
46 "\n"
47 "Commands:\n"
48 " hal-info - Show info about boot_control HAL used.\n"
49 " get-number-slots - Prints number of slots.\n"
50 " get-current-slot - Prints currently running SLOT.\n"
51 " mark-boot-successful - Mark current slot as GOOD.\n"
52 " set-active-boot-slot SLOT - On next boot, load and execute SLOT.\n"
53 " set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
54 " is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n"
55 " is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.\n"
56 " get-suffix SLOT - Prints suffix for SLOT.\n",
57 argv[0], argv[0]);
58 if (bootVersion >= BOOTCTL_V1_1) {
59 fprintf(where,
60 " set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic\n"
61 " partition is in progress. Valid STAT values\n"
62 " are: none, unknown, snapshotted, merging,\n"
63 " or cancelled.\n"
64 " get-snapshot-merge-status - Prints the current snapshot-merge status.\n");
65 }
66 fprintf(where,
67 "\n"
68 "SLOT parameter is the zero-based slot-number.\n");
69 }
70
do_hal_info(const sp<V1_0::IBootControl> module)71 static int do_hal_info(const sp<V1_0::IBootControl> module) {
72 module->interfaceDescriptor([&](const auto& descriptor) {
73 fprintf(stdout,
74 "HAL Version: %s\n",
75 descriptor.c_str());
76 });
77 return EX_OK;
78 }
79
do_get_number_slots(sp<V1_0::IBootControl> module)80 static int do_get_number_slots(sp<V1_0::IBootControl> module)
81 {
82 uint32_t numSlots = module->getNumberSlots();
83 fprintf(stdout, "%u\n", numSlots);
84 return EX_OK;
85 }
86
do_get_current_slot(sp<V1_0::IBootControl> module)87 static int do_get_current_slot(sp<V1_0::IBootControl> module)
88 {
89 Slot curSlot = module->getCurrentSlot();
90 fprintf(stdout, "%u\n", curSlot);
91 return EX_OK;
92 }
93
generate_callback(CommandResult * crp)94 static std::function<void(CommandResult)> generate_callback(CommandResult *crp) {
95 return [=](CommandResult cr){
96 *crp = cr;
97 };
98 }
99
handle_return(const Return<void> & ret,CommandResult cr,const char * errStr)100 static int handle_return(const Return<void> &ret, CommandResult cr, const char* errStr) {
101 if (!ret.isOk()) {
102 fprintf(stderr, errStr, ret.description().c_str());
103 return EX_SOFTWARE;
104 } else if (!cr.success) {
105 fprintf(stderr, errStr, cr.errMsg.c_str());
106 return EX_SOFTWARE;
107 }
108 return EX_OK;
109 }
110
do_mark_boot_successful(sp<V1_0::IBootControl> module)111 static int do_mark_boot_successful(sp<V1_0::IBootControl> module)
112 {
113 CommandResult cr;
114 Return<void> ret = module->markBootSuccessful(generate_callback(&cr));
115 return handle_return(ret, cr, "Error marking as having booted successfully: %s\n");
116 }
117
do_set_active_boot_slot(sp<V1_0::IBootControl> module,Slot slot_number)118 static int do_set_active_boot_slot(sp<V1_0::IBootControl> module,
119 Slot slot_number)
120 {
121 CommandResult cr;
122 Return<void> ret = module->setActiveBootSlot(slot_number, generate_callback(&cr));
123 return handle_return(ret, cr, "Error setting active boot slot: %s\n");
124 }
125
do_set_slot_as_unbootable(sp<V1_0::IBootControl> module,Slot slot_number)126 static int do_set_slot_as_unbootable(sp<V1_0::IBootControl> module,
127 Slot slot_number)
128 {
129 CommandResult cr;
130 Return<void> ret = module->setSlotAsUnbootable(slot_number, generate_callback(&cr));
131 return handle_return(ret, cr, "Error setting slot as unbootable: %s\n");
132 }
133
handle_return(const Return<BoolResult> & ret,const char * errStr)134 static int handle_return(const Return<BoolResult> &ret, const char* errStr) {
135 if (!ret.isOk()) {
136 fprintf(stderr, errStr, ret.description().c_str());
137 return EX_SOFTWARE;
138 } else if (ret == BoolResult::INVALID_SLOT) {
139 fprintf(stderr, errStr, "Invalid slot");
140 return EX_SOFTWARE;
141 } else if (ret == BoolResult::TRUE) {
142 return EX_OK;
143 }
144 return EX_SOFTWARE;
145 }
146
do_is_slot_bootable(sp<V1_0::IBootControl> module,Slot slot_number)147 static int do_is_slot_bootable(sp<V1_0::IBootControl> module, Slot slot_number)
148 {
149 Return<BoolResult> ret = module->isSlotBootable(slot_number);
150 return handle_return(ret, "Error calling isSlotBootable(): %s\n");
151 }
152
do_is_slot_marked_successful(sp<V1_0::IBootControl> module,Slot slot_number)153 static int do_is_slot_marked_successful(sp<V1_0::IBootControl> module,
154 Slot slot_number)
155 {
156 Return<BoolResult> ret = module->isSlotMarkedSuccessful(slot_number);
157 return handle_return(ret, "Error calling isSlotMarkedSuccessful(): %s\n");
158 }
159
stringToMergeStatus(const std::string & status)160 std::optional<MergeStatus> stringToMergeStatus(const std::string &status) {
161 if (status == "cancelled") return MergeStatus::CANCELLED;
162 if (status == "merging") return MergeStatus::MERGING;
163 if (status == "none") return MergeStatus::NONE;
164 if (status == "snapshotted") return MergeStatus::SNAPSHOTTED;
165 if (status == "unknown") return MergeStatus::UNKNOWN;
166 return {};
167 }
168
do_set_snapshot_merge_status(sp<V1_1::IBootControl> module,BootCtlVersion bootVersion,int argc,char * argv[])169 static int do_set_snapshot_merge_status(sp<V1_1::IBootControl> module, BootCtlVersion bootVersion,
170 int argc, char *argv[]) {
171 if (argc != 3) {
172 usage(stderr, bootVersion, argc, argv);
173 exit(EX_USAGE);
174 return -1;
175 }
176
177 auto status = stringToMergeStatus(argv[2]);
178 if (!status.has_value()) {
179 usage(stderr, bootVersion, argc, argv);
180 exit(EX_USAGE);
181 return -1;
182 }
183
184 if (!module->setSnapshotMergeStatus(status.value())) {
185 return EX_SOFTWARE;
186 }
187 return EX_OK;
188 }
189
operator <<(std::ostream & os,MergeStatus state)190 std::ostream& operator<<(std::ostream& os, MergeStatus state) {
191 switch (state) {
192 case MergeStatus::CANCELLED:
193 return os << "cancelled";
194 case MergeStatus::MERGING:
195 return os << "merging";
196 case MergeStatus::NONE:
197 return os << "none";
198 case MergeStatus::SNAPSHOTTED:
199 return os << "snapshotted";
200 case MergeStatus::UNKNOWN:
201 return os << "unknown";
202 default:
203 return os;
204 }
205 }
206
do_get_snapshot_merge_status(sp<V1_1::IBootControl> module)207 static int do_get_snapshot_merge_status(sp<V1_1::IBootControl> module) {
208 MergeStatus ret = module->getSnapshotMergeStatus();
209 std::stringstream ss;
210 ss << ret;
211 fprintf(stdout, "%s\n", ss.str().c_str());
212 return EX_OK;
213 }
214
do_get_suffix(sp<V1_0::IBootControl> module,Slot slot_number)215 static int do_get_suffix(sp<V1_0::IBootControl> module, Slot slot_number) {
216 std::function<void(hidl_string)> cb = [](hidl_string suffix){
217 fprintf(stdout, "%s\n", suffix.c_str());
218 };
219 Return<void> ret = module->getSuffix(slot_number, cb);
220 if (!ret.isOk()) {
221 fprintf(stderr, "Error calling getSuffix(): %s\n",
222 ret.description().c_str());
223 return EX_SOFTWARE;
224 }
225 return EX_OK;
226 }
227
parse_slot(BootCtlVersion bootVersion,int pos,int argc,char * argv[])228 static uint32_t parse_slot(BootCtlVersion bootVersion, int pos, int argc, char *argv[])
229 {
230 if (pos > argc - 1) {
231 usage(stderr, bootVersion, argc, argv);
232 exit(EX_USAGE);
233 return -1;
234 }
235 errno = 0;
236 uint64_t ret = strtoul(argv[pos], NULL, 10);
237 if (errno != 0 || ret > UINT_MAX) {
238 usage(stderr, bootVersion, argc, argv);
239 exit(EX_USAGE);
240 return -1;
241 }
242 return (uint32_t)ret;
243 }
244
main(int argc,char * argv[])245 int main(int argc, char *argv[])
246 {
247 sp<V1_0::IBootControl> v1_0_module;
248 sp<V1_1::IBootControl> v1_1_module;
249 BootCtlVersion bootVersion = BOOTCTL_V1_0;
250
251 v1_0_module = V1_0::IBootControl::getService();
252 if (v1_0_module == nullptr) {
253 fprintf(stderr, "Error getting bootctrl v1.0 module.\n");
254 return EX_SOFTWARE;
255 }
256 v1_1_module = V1_1::IBootControl::castFrom(v1_0_module);
257 if (v1_1_module != nullptr) {
258 bootVersion = BOOTCTL_V1_1;
259 }
260
261 if (argc < 2) {
262 usage(stderr, bootVersion, argc, argv);
263 return EX_USAGE;
264 }
265
266 // Functions present from version 1.0
267 if (strcmp(argv[1], "hal-info") == 0) {
268 return do_hal_info(v1_0_module);
269 } else if (strcmp(argv[1], "get-number-slots") == 0) {
270 return do_get_number_slots(v1_0_module);
271 } else if (strcmp(argv[1], "get-current-slot") == 0) {
272 return do_get_current_slot(v1_0_module);
273 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
274 return do_mark_boot_successful(v1_0_module);
275 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
276 return do_set_active_boot_slot(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
277 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
278 return do_set_slot_as_unbootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
279 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
280 return do_is_slot_bootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
281 } else if (strcmp(argv[1], "is-slot-marked-successful") == 0) {
282 return do_is_slot_marked_successful(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
283 } else if (strcmp(argv[1], "get-suffix") == 0) {
284 return do_get_suffix(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
285 }
286
287 // Functions present from version 1.1
288 if (strcmp(argv[1], "set-snapshot-merge-status") == 0 ||
289 strcmp(argv[1], "get-snapshot-merge-status") == 0 ) {
290
291 if (v1_1_module == nullptr) {
292 fprintf(stderr, "Error getting bootctrl v1.1 module.\n");
293 return EX_SOFTWARE;
294 }
295 if (strcmp(argv[1], "set-snapshot-merge-status") == 0) {
296 return do_set_snapshot_merge_status(v1_1_module, bootVersion, argc, argv);
297 } else if (strcmp(argv[1], "get-snapshot-merge-status") == 0) {
298 return do_get_snapshot_merge_status(v1_1_module);
299 }
300 }
301
302 // Parameter not matched, print usage
303 usage(stderr, bootVersion, argc, argv);
304 return EX_USAGE;
305 }
306