blob: 1bf91815a2b7a130360ae579f4617c0023d0fdb5 [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
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070020#include <BootControlClient.h>
Tianjiecc5edd32020-11-25 15:08:38 -080021#include <android/hardware/boot/1.2/IBootControl.h>
Thiébaud Weksteendf537e12020-10-23 17:01:57 +020022#include <sysexits.h>
Connor O'Briena4fbed72016-10-14 15:03:12 -070023
24using android::sp;
25
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070026using aidl::android::hardware::boot::MergeStatus;
Connor O'Briena4fbed72016-10-14 15:03:12 -070027
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070028using android::hal::BootControlClient;
29using android::hal::BootControlVersion;
30using android::hal::CommandResult;
Connor O'Briena4fbed72016-10-14 15:03:12 -070031
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070032static void usage(FILE* where, BootControlVersion bootVersion, int /* argc */, char* argv[]) {
Connor O'Briena4fbed72016-10-14 15:03:12 -070033 fprintf(where,
34 "%s - command-line wrapper for the boot HAL.\n"
35 "\n"
36 "Usage:\n"
37 " %s COMMAND\n"
38 "\n"
39 "Commands:\n"
Alessio Balsinifcba1342019-12-19 13:43:36 +000040 " hal-info - Show info about boot_control HAL used.\n"
41 " get-number-slots - Prints number of slots.\n"
42 " get-current-slot - Prints currently running SLOT.\n"
43 " mark-boot-successful - Mark current slot as GOOD.\n"
Tianjiecc5edd32020-11-25 15:08:38 -080044 " get-active-boot-slot - Prints the SLOT to load on next boot.\n"
Alessio Balsinifcba1342019-12-19 13:43:36 +000045 " set-active-boot-slot SLOT - On next boot, load and execute SLOT.\n"
46 " set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
47 " is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n"
48 " is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.\n"
Håkan Kvist079aaac2020-07-02 17:27:06 +020049 " get-suffix SLOT - Prints suffix for SLOT.\n",
Alessio Balsinifcba1342019-12-19 13:43:36 +000050 argv[0], argv[0]);
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070051 if (bootVersion >= BootControlVersion::BOOTCTL_V1_1) {
Håkan Kvist079aaac2020-07-02 17:27:06 +020052 fprintf(where,
53 " set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic\n"
54 " partition is in progress. Valid STAT values\n"
55 " are: none, unknown, snapshotted, merging,\n"
56 " or cancelled.\n"
57 " get-snapshot-merge-status - Prints the current snapshot-merge status.\n");
58 }
59 fprintf(where,
60 "\n"
61 "SLOT parameter is the zero-based slot-number.\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -070062}
63
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070064static constexpr auto ToString(BootControlVersion ver) {
65 switch (ver) {
66 case BootControlVersion::BOOTCTL_V1_0:
67 return "android.hardware.boot@1.0::IBootControl";
68 case BootControlVersion::BOOTCTL_V1_1:
69 return "android.hardware.boot@1.1::IBootControl";
70 case BootControlVersion::BOOTCTL_V1_2:
71 return "android.hardware.boot@1.2::IBootControl";
72 case BootControlVersion::BOOTCTL_AIDL:
73 return "android.hardware.boot@aidl::IBootControl";
74 }
75}
76
77static int do_hal_info(const BootControlClient* module) {
78 fprintf(stdout, "HAL Version: %s\n", ToString(module->GetVersion()));
Connor O'Briena4fbed72016-10-14 15:03:12 -070079 return EX_OK;
80}
81
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070082static int do_get_number_slots(BootControlClient* module) {
83 auto numSlots = module->GetNumSlots();
Connor O'Briena4fbed72016-10-14 15:03:12 -070084 fprintf(stdout, "%u\n", numSlots);
85 return EX_OK;
86}
87
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070088static int do_get_current_slot(BootControlClient* module) {
89 auto curSlot = module->GetCurrentSlot();
Connor O'Briena4fbed72016-10-14 15:03:12 -070090 fprintf(stdout, "%u\n", curSlot);
91 return EX_OK;
92}
93
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -070094static int handle_return(CommandResult cr, const char* errStr) {
95 if (!cr.IsOk()) {
96 fprintf(stderr, errStr, cr.errMsg.c_str());
Connor O'Briena4fbed72016-10-14 15:03:12 -070097 return EX_SOFTWARE;
98 } else if (!cr.success) {
99 fprintf(stderr, errStr, cr.errMsg.c_str());
100 return EX_SOFTWARE;
101 }
102 return EX_OK;
103}
104
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700105static int do_mark_boot_successful(BootControlClient* module) {
106 auto ret = module->MarkBootSuccessful();
107 return handle_return(ret, "Error marking as having booted successfully: %s\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700108}
109
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700110static int do_get_active_boot_slot(BootControlClient* module) {
111 uint32_t slot = module->GetActiveBootSlot();
Tianjiecc5edd32020-11-25 15:08:38 -0800112 fprintf(stdout, "%u\n", slot);
113 return EX_OK;
114}
115
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700116static int do_set_active_boot_slot(BootControlClient* module, int32_t slot_number) {
117 const auto cr = module->SetActiveBootSlot(slot_number);
118 return handle_return(cr, "Error setting active boot slot: %s\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700119}
120
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700121static int do_set_slot_as_unbootable(BootControlClient* module, int32_t slot_number) {
122 const auto cr = module->MarkSlotUnbootable(slot_number);
123 return handle_return(cr, "Error setting slot as unbootable: %s\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700124}
125
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700126static int handle_return(const std::optional<bool>& ret, const char* errStr) {
127 if (!ret.has_value()) {
128 fprintf(stderr, errStr, "");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700129 return EX_SOFTWARE;
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700130 }
131 if (ret.value()) {
132 printf("%d\n", ret.value());
Connor O'Briena4fbed72016-10-14 15:03:12 -0700133 return EX_OK;
134 }
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700135 printf("%d\n", ret.value());
Connor O'Briena4fbed72016-10-14 15:03:12 -0700136 return EX_SOFTWARE;
137}
138
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700139static int do_is_slot_bootable(BootControlClient* module, int32_t slot_number) {
140 const auto ret = module->IsSlotBootable(slot_number);
141 return handle_return(ret, "Error calling isSlotBootable()\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700142}
143
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700144static int do_is_slot_marked_successful(BootControlClient* module, int32_t slot_number) {
145 const auto ret = module->IsSlotMarkedSuccessful(slot_number);
146 return handle_return(ret, "Error calling isSlotMarkedSuccessful()\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700147}
148
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200149std::optional<MergeStatus> stringToMergeStatus(const std::string& status) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000150 if (status == "cancelled") return MergeStatus::CANCELLED;
151 if (status == "merging") return MergeStatus::MERGING;
152 if (status == "none") return MergeStatus::NONE;
153 if (status == "snapshotted") return MergeStatus::SNAPSHOTTED;
154 if (status == "unknown") return MergeStatus::UNKNOWN;
155 return {};
156}
157
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700158static int do_set_snapshot_merge_status(BootControlClient* module, BootControlVersion bootVersion,
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200159 int argc, char* argv[]) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000160 if (argc != 3) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200161 usage(stderr, bootVersion, argc, argv);
Alessio Balsinifcba1342019-12-19 13:43:36 +0000162 exit(EX_USAGE);
163 return -1;
164 }
165
166 auto status = stringToMergeStatus(argv[2]);
167 if (!status.has_value()) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200168 usage(stderr, bootVersion, argc, argv);
Alessio Balsinifcba1342019-12-19 13:43:36 +0000169 exit(EX_USAGE);
170 return -1;
171 }
172
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700173 const auto ret = module->SetSnapshotMergeStatus(status.value());
174 return handle_return(ret, "Failed to set snapshot merge status: %s\n");
Alessio Balsinifcba1342019-12-19 13:43:36 +0000175}
176
177std::ostream& operator<<(std::ostream& os, MergeStatus state) {
178 switch (state) {
179 case MergeStatus::CANCELLED:
180 return os << "cancelled";
181 case MergeStatus::MERGING:
182 return os << "merging";
183 case MergeStatus::NONE:
184 return os << "none";
185 case MergeStatus::SNAPSHOTTED:
186 return os << "snapshotted";
187 case MergeStatus::UNKNOWN:
188 return os << "unknown";
189 default:
190 return os;
191 }
192}
193
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700194static int do_get_snapshot_merge_status(BootControlClient* module) {
Alessio Balsinifcba1342019-12-19 13:43:36 +0000195 MergeStatus ret = module->getSnapshotMergeStatus();
196 std::stringstream ss;
197 ss << ret;
198 fprintf(stdout, "%s\n", ss.str().c_str());
199 return EX_OK;
200}
Connor O'Briena4fbed72016-10-14 15:03:12 -0700201
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700202static int do_get_suffix(BootControlClient* module, int32_t slot_number) {
203 const auto ret = module->GetSuffix(slot_number);
204 if (ret.empty()) {
205 fprintf(stderr, "Error calling getSuffix()\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700206 return EX_SOFTWARE;
207 }
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700208 printf("%s\n", ret.c_str());
Connor O'Briena4fbed72016-10-14 15:03:12 -0700209 return EX_OK;
210}
211
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700212static uint32_t parse_slot(BootControlVersion bootVersion, int pos, int argc, char* argv[]) {
Connor O'Briena4fbed72016-10-14 15:03:12 -0700213 if (pos > argc - 1) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200214 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700215 exit(EX_USAGE);
216 return -1;
217 }
218 errno = 0;
219 uint64_t ret = strtoul(argv[pos], NULL, 10);
220 if (errno != 0 || ret > UINT_MAX) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200221 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700222 exit(EX_USAGE);
223 return -1;
224 }
225 return (uint32_t)ret;
226}
227
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200228int main(int argc, char* argv[]) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700229 const auto client = android::hal::BootControlClient::WaitForService();
230 if (client == nullptr) {
231 fprintf(stderr, "Failed to get bootctl module.\n");
Connor O'Briena4fbed72016-10-14 15:03:12 -0700232 return EX_SOFTWARE;
233 }
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700234 const auto bootVersion = client->GetVersion();
Tianjiecc5edd32020-11-25 15:08:38 -0800235
Håkan Kvist079aaac2020-07-02 17:27:06 +0200236 if (argc < 2) {
237 usage(stderr, bootVersion, argc, argv);
Connor O'Briena4fbed72016-10-14 15:03:12 -0700238 return EX_USAGE;
239 }
240
Håkan Kvist079aaac2020-07-02 17:27:06 +0200241 // Functions present from version 1.0
242 if (strcmp(argv[1], "hal-info") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700243 return do_hal_info(client.get());
Håkan Kvist079aaac2020-07-02 17:27:06 +0200244 } else if (strcmp(argv[1], "get-number-slots") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700245 return do_get_number_slots(client.get());
Håkan Kvist079aaac2020-07-02 17:27:06 +0200246 } else if (strcmp(argv[1], "get-current-slot") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700247 return do_get_current_slot(client.get());
Håkan Kvist079aaac2020-07-02 17:27:06 +0200248 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700249 return do_mark_boot_successful(client.get());
Håkan Kvist079aaac2020-07-02 17:27:06 +0200250 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700251 return do_set_active_boot_slot(client.get(), parse_slot(bootVersion, 2, argc, argv));
Håkan Kvist079aaac2020-07-02 17:27:06 +0200252 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700253 return do_set_slot_as_unbootable(client.get(), parse_slot(bootVersion, 2, argc, argv));
Håkan Kvist079aaac2020-07-02 17:27:06 +0200254 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700255 return do_is_slot_bootable(client.get(), parse_slot(bootVersion, 2, argc, argv));
Håkan Kvist079aaac2020-07-02 17:27:06 +0200256 } else if (strcmp(argv[1], "is-slot-marked-successful") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700257 return do_is_slot_marked_successful(client.get(), parse_slot(bootVersion, 2, argc, argv));
Håkan Kvist079aaac2020-07-02 17:27:06 +0200258 } else if (strcmp(argv[1], "get-suffix") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700259 return do_get_suffix(client.get(), parse_slot(bootVersion, 2, argc, argv));
Håkan Kvist079aaac2020-07-02 17:27:06 +0200260 }
261
262 // Functions present from version 1.1
263 if (strcmp(argv[1], "set-snapshot-merge-status") == 0 ||
Thiébaud Weksteendf537e12020-10-23 17:01:57 +0200264 strcmp(argv[1], "get-snapshot-merge-status") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700265 if (bootVersion < BootControlVersion::BOOTCTL_V1_1) {
Håkan Kvist079aaac2020-07-02 17:27:06 +0200266 fprintf(stderr, "Error getting bootctrl v1.1 module.\n");
267 return EX_SOFTWARE;
268 }
269 if (strcmp(argv[1], "set-snapshot-merge-status") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700270 return do_set_snapshot_merge_status(client.get(), bootVersion, argc, argv);
Håkan Kvist079aaac2020-07-02 17:27:06 +0200271 } else if (strcmp(argv[1], "get-snapshot-merge-status") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700272 return do_get_snapshot_merge_status(client.get());
Håkan Kvist079aaac2020-07-02 17:27:06 +0200273 }
274 }
275
Tianjiecc5edd32020-11-25 15:08:38 -0800276 if (strcmp(argv[1], "get-active-boot-slot") == 0) {
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700277 if (bootVersion < BootControlVersion::BOOTCTL_V1_2) {
Tianjiecc5edd32020-11-25 15:08:38 -0800278 fprintf(stderr, "Error getting bootctrl v1.2 module.\n");
279 return EX_SOFTWARE;
280 }
281
Kelvin Zhang7aaa4e62022-06-22 15:11:13 -0700282 return do_get_active_boot_slot(client.get());
Tianjiecc5edd32020-11-25 15:08:38 -0800283 }
284
Håkan Kvist079aaac2020-07-02 17:27:06 +0200285 // Parameter not matched, print usage
286 usage(stderr, bootVersion, argc, argv);
287 return EX_USAGE;
Connor O'Briena4fbed72016-10-14 15:03:12 -0700288}