blob: 8ead010d65d1cefe9db53ed27597ac7338612d5f [file] [log] [blame]
Connor O'Briena4fbed72016-10-14 15:03:12 -07001/*
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
Alessio Balsinifcba1342019-12-19 13:43:36 +000017#include <optional>
18#include <sstream>
19
Tianjiecc5edd32020-11-25 15:08:38 -080020#include <android/hardware/boot/1.2/IBootControl.h>
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020021#include <sysexits.h>
Connor O'Briena4fbed72016-10-14 15:03:12 -070022
23using android::sp;
24
25using android::hardware::hidl_string;
26using android::hardware::Return;
27
28using android::hardware::boot::V1_0::BoolResult;
Connor O'Briena4fbed72016-10-14 15:03:12 -070029using android::hardware::boot::V1_0::CommandResult;
30using android::hardware::boot::V1_0::Slot;
Alessio Balsinifcba1342019-12-19 13:43:36 +000031using android::hardware::boot::V1_1::IBootControl;
32using android::hardware::boot::V1_1::MergeStatus;
Connor O'Briena4fbed72016-10-14 15:03:12 -070033
Håkan Kvist079aaac2020-07-02 17:27:06 +020034namespace V1_0 = android::hardware::boot::V1_0;
35namespace V1_1 = android::hardware::boot::V1_1;
Tianjiecc5edd32020-11-25 15:08:38 -080036namespace V1_2 = android::hardware::boot::V1_2;
Håkan Kvist079aaac2020-07-02 17:27:06 +020037
Tianjiecc5edd32020-11-25 15:08:38 -080038enum BootCtlVersion { BOOTCTL_V1_0, BOOTCTL_V1_1, BOOTCTL_V1_2 };
Håkan Kvist079aaac2020-07-02 17:27:06 +020039
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020040static void usage(FILE* where, BootCtlVersion bootVersion, int /* argc */, char* argv[]) {
Connor O'Briena4fbed72016-10-14 15:03:12 -070041 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"
Alessio Balsinifcba1342019-12-19 13:43:36 +000048 " 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"
Tianjiecc5edd32020-11-25 15:08:38 -080052 " get-active-boot-slot - Prints the SLOT to load on next boot.\n"
Alessio Balsinifcba1342019-12-19 13:43:36 +000053 " set-active-boot-slot SLOT - On next boot, load and execute SLOT.\n"
54 " set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
55 " is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n"
56 " is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.\n"
Håkan Kvist079aaac2020-07-02 17:27:06 +020057 " get-suffix SLOT - Prints suffix for SLOT.\n",
Alessio Balsinifcba1342019-12-19 13:43:36 +000058 argv[0], argv[0]);
Håkan Kvist079aaac2020-07-02 17:27:06 +020059 if (bootVersion >= BOOTCTL_V1_1) {
60 fprintf(where,
61 " set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic\n"
62 " partition is in progress. Valid STAT values\n"
63 " are: none, unknown, snapshotted, merging,\n"
64 " or cancelled.\n"
65 " get-snapshot-merge-status - Prints the current snapshot-merge status.\n");
66 }
67 fprintf(where,
68 "\n"
69 "SLOT parameter is the zero-based slot-number.\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -070070}
71
Håkan Kvist079aaac2020-07-02 17:27:06 +020072static int do_hal_info(const sp<V1_0::IBootControl> module) {
Yifan Hong2e3024f2017-02-22 18:11:18 -080073 module->interfaceDescriptor([&](const auto& descriptor) {
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020074 fprintf(stdout, "HAL Version: %s\n", descriptor.c_str());
Steven Moreland6b8d0332017-01-04 13:00:07 -080075 });
Connor O'Briena4fbed72016-10-14 15:03:12 -070076 return EX_OK;
77}
78
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020079static int do_get_number_slots(sp<V1_0::IBootControl> module) {
Connor O'Briena4fbed72016-10-14 15:03:12 -070080 uint32_t numSlots = module->getNumberSlots();
81 fprintf(stdout, "%u\n", numSlots);
82 return EX_OK;
83}
84
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020085static int do_get_current_slot(sp<V1_0::IBootControl> module) {
Connor O'Briena4fbed72016-10-14 15:03:12 -070086 Slot curSlot = module->getCurrentSlot();
87 fprintf(stdout, "%u\n", curSlot);
88 return EX_OK;
89}
90
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020091static std::function<void(CommandResult)> generate_callback(CommandResult* crp) {
92 return [=](CommandResult cr) { *crp = cr; };
Connor O'Briena4fbed72016-10-14 15:03:12 -070093}
94
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020095static int handle_return(const Return<void>& ret, CommandResult cr, const char* errStr) {
Yifan Hong87d71f32016-12-20 17:34:36 -080096 if (!ret.isOk()) {
97 fprintf(stderr, errStr, ret.description().c_str());
Connor O'Briena4fbed72016-10-14 15:03:12 -070098 return EX_SOFTWARE;
99 } else if (!cr.success) {
100 fprintf(stderr, errStr, cr.errMsg.c_str());
101 return EX_SOFTWARE;
102 }
103 return EX_OK;
104}
105
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200106static int do_mark_boot_successful(sp<V1_0::IBootControl> module) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700107 CommandResult cr;
108 Return<void> ret = module->markBootSuccessful(generate_callback(&cr));
109 return handle_return(ret, cr, "Error marking as having booted successfully: %s\n");
110}
111
Tianjiecc5edd32020-11-25 15:08:38 -0800112static int do_get_active_boot_slot(sp<V1_2::IBootControl> module) {
113 uint32_t slot = module->getActiveBootSlot();
114 fprintf(stdout, "%u\n", slot);
115 return EX_OK;
116}
117
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200118static int do_set_active_boot_slot(sp<V1_0::IBootControl> module, Slot slot_number) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700119 CommandResult cr;
120 Return<void> ret = module->setActiveBootSlot(slot_number, generate_callback(&cr));
121 return handle_return(ret, cr, "Error setting active boot slot: %s\n");
122}
123
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200124static int do_set_slot_as_unbootable(sp<V1_0::IBootControl> module, Slot slot_number) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700125 CommandResult cr;
126 Return<void> ret = module->setSlotAsUnbootable(slot_number, generate_callback(&cr));
127 return handle_return(ret, cr, "Error setting slot as unbootable: %s\n");
128}
129
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200130static int handle_return(const Return<BoolResult>& ret, const char* errStr) {
Yifan Hong87d71f32016-12-20 17:34:36 -0800131 if (!ret.isOk()) {
132 fprintf(stderr, errStr, ret.description().c_str());
Connor O'Briena4fbed72016-10-14 15:03:12 -0700133 return EX_SOFTWARE;
134 } else if (ret == BoolResult::INVALID_SLOT) {
135 fprintf(stderr, errStr, "Invalid slot");
136 return EX_SOFTWARE;
137 } else if (ret == BoolResult::TRUE) {
138 return EX_OK;
139 }
140 return EX_SOFTWARE;
141}
142
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200143static int do_is_slot_bootable(sp<V1_0::IBootControl> module, Slot slot_number) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700144 Return<BoolResult> ret = module->isSlotBootable(slot_number);
145 return handle_return(ret, "Error calling isSlotBootable(): %s\n");
146}
147
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200148static int do_is_slot_marked_successful(sp<V1_0::IBootControl> module, Slot slot_number) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700149 Return<BoolResult> ret = module->isSlotMarkedSuccessful(slot_number);
150 return handle_return(ret, "Error calling isSlotMarkedSuccessful(): %s\n");
151}
152
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200153std::optional<MergeStatus> stringToMergeStatus(const std::string& status) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000154 if (status == "cancelled") return MergeStatus::CANCELLED;
155 if (status == "merging") return MergeStatus::MERGING;
156 if (status == "none") return MergeStatus::NONE;
157 if (status == "snapshotted") return MergeStatus::SNAPSHOTTED;
158 if (status == "unknown") return MergeStatus::UNKNOWN;
159 return {};
160}
161
Håkan Kvist079aaac2020-07-02 17:27:06 +0200162static int do_set_snapshot_merge_status(sp<V1_1::IBootControl> module, BootCtlVersion bootVersion,
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200163 int argc, char* argv[]) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000164 if (argc != 3) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200165 usage(stderr, bootVersion, argc, argv);
Alessio Balsinifcba1342019-12-19 13:43:36 +0000166 exit(EX_USAGE);
167 return -1;
168 }
169
170 auto status = stringToMergeStatus(argv[2]);
171 if (!status.has_value()) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200172 usage(stderr, bootVersion, argc, argv);
Alessio Balsinifcba1342019-12-19 13:43:36 +0000173 exit(EX_USAGE);
174 return -1;
175 }
176
177 if (!module->setSnapshotMergeStatus(status.value())) {
178 return EX_SOFTWARE;
179 }
180 return EX_OK;
181}
182
183std::ostream& operator<<(std::ostream& os, MergeStatus state) {
184 switch (state) {
185 case MergeStatus::CANCELLED:
186 return os << "cancelled";
187 case MergeStatus::MERGING:
188 return os << "merging";
189 case MergeStatus::NONE:
190 return os << "none";
191 case MergeStatus::SNAPSHOTTED:
192 return os << "snapshotted";
193 case MergeStatus::UNKNOWN:
194 return os << "unknown";
195 default:
196 return os;
197 }
198}
199
Håkan Kvist079aaac2020-07-02 17:27:06 +0200200static int do_get_snapshot_merge_status(sp<V1_1::IBootControl> module) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000201 MergeStatus ret = module->getSnapshotMergeStatus();
202 std::stringstream ss;
203 ss << ret;
204 fprintf(stdout, "%s\n", ss.str().c_str());
205 return EX_OK;
206}
Connor O'Briena4fbed72016-10-14 15:03:12 -0700207
Håkan Kvist079aaac2020-07-02 17:27:06 +0200208static int do_get_suffix(sp<V1_0::IBootControl> module, Slot slot_number) {
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200209 std::function<void(hidl_string)> cb = [](hidl_string suffix) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700210 fprintf(stdout, "%s\n", suffix.c_str());
211 };
212 Return<void> ret = module->getSuffix(slot_number, cb);
Yifan Hong87d71f32016-12-20 17:34:36 -0800213 if (!ret.isOk()) {
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200214 fprintf(stderr, "Error calling getSuffix(): %s\n", ret.description().c_str());
Connor O'Briena4fbed72016-10-14 15:03:12 -0700215 return EX_SOFTWARE;
216 }
217 return EX_OK;
218}
219
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200220static uint32_t parse_slot(BootCtlVersion bootVersion, int pos, int argc, char* argv[]) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700221 if (pos > argc - 1) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200222 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700223 exit(EX_USAGE);
224 return -1;
225 }
226 errno = 0;
227 uint64_t ret = strtoul(argv[pos], NULL, 10);
228 if (errno != 0 || ret > UINT_MAX) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200229 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700230 exit(EX_USAGE);
231 return -1;
232 }
233 return (uint32_t)ret;
234}
235
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200236int main(int argc, char* argv[]) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200237 sp<V1_0::IBootControl> v1_0_module;
238 sp<V1_1::IBootControl> v1_1_module;
Tianjiecc5edd32020-11-25 15:08:38 -0800239 sp<V1_2::IBootControl> v1_2_module;
Håkan Kvist079aaac2020-07-02 17:27:06 +0200240 BootCtlVersion bootVersion = BOOTCTL_V1_0;
Connor O'Briena4fbed72016-10-14 15:03:12 -0700241
Håkan Kvist079aaac2020-07-02 17:27:06 +0200242 v1_0_module = V1_0::IBootControl::getService();
243 if (v1_0_module == nullptr) {
244 fprintf(stderr, "Error getting bootctrl v1.0 module.\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700245 return EX_SOFTWARE;
246 }
Håkan Kvist079aaac2020-07-02 17:27:06 +0200247 v1_1_module = V1_1::IBootControl::castFrom(v1_0_module);
248 if (v1_1_module != nullptr) {
249 bootVersion = BOOTCTL_V1_1;
250 }
Connor O'Briena4fbed72016-10-14 15:03:12 -0700251
Tianjiecc5edd32020-11-25 15:08:38 -0800252 v1_2_module = V1_2::IBootControl::castFrom(v1_0_module);
253 if (v1_2_module != nullptr) {
254 bootVersion = BOOTCTL_V1_2;
255 }
256
Håkan Kvist079aaac2020-07-02 17:27:06 +0200257 if (argc < 2) {
258 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700259 return EX_USAGE;
260 }
261
Håkan Kvist079aaac2020-07-02 17:27:06 +0200262 // Functions present from version 1.0
263 if (strcmp(argv[1], "hal-info") == 0) {
264 return do_hal_info(v1_0_module);
265 } else if (strcmp(argv[1], "get-number-slots") == 0) {
266 return do_get_number_slots(v1_0_module);
267 } else if (strcmp(argv[1], "get-current-slot") == 0) {
268 return do_get_current_slot(v1_0_module);
269 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
270 return do_mark_boot_successful(v1_0_module);
271 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
272 return do_set_active_boot_slot(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
273 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
274 return do_set_slot_as_unbootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
275 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
276 return do_is_slot_bootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
277 } else if (strcmp(argv[1], "is-slot-marked-successful") == 0) {
278 return do_is_slot_marked_successful(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
279 } else if (strcmp(argv[1], "get-suffix") == 0) {
280 return do_get_suffix(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
281 }
282
283 // Functions present from version 1.1
284 if (strcmp(argv[1], "set-snapshot-merge-status") == 0 ||
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200285 strcmp(argv[1], "get-snapshot-merge-status") == 0) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200286 if (v1_1_module == nullptr) {
287 fprintf(stderr, "Error getting bootctrl v1.1 module.\n");
288 return EX_SOFTWARE;
289 }
290 if (strcmp(argv[1], "set-snapshot-merge-status") == 0) {
291 return do_set_snapshot_merge_status(v1_1_module, bootVersion, argc, argv);
292 } else if (strcmp(argv[1], "get-snapshot-merge-status") == 0) {
293 return do_get_snapshot_merge_status(v1_1_module);
294 }
295 }
296
Tianjiecc5edd32020-11-25 15:08:38 -0800297 if (strcmp(argv[1], "get-active-boot-slot") == 0) {
298 if (v1_2_module == nullptr) {
299 fprintf(stderr, "Error getting bootctrl v1.2 module.\n");
300 return EX_SOFTWARE;
301 }
302
303 return do_get_active_boot_slot(v1_2_module);
304 }
305
Håkan Kvist079aaac2020-07-02 17:27:06 +0200306 // Parameter not matched, print usage
307 usage(stderr, bootVersion, argc, argv);
308 return EX_USAGE;
Connor O'Briena4fbed72016-10-14 15:03:12 -0700309}