Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

system-extras: Commit

system/extras


Commit MetaInfo

Revisionb2dccf1e0de430806f6cd55a809143435b7e857b (tree)
Time2020-07-15 05:35:50
AuthorHåkan Kvist <hakan.kvist@sony...>
CommiterAutomerger Merge Worker

Log Message

Make bootctl work on devices that does not have v1.1 API am: 6cbcea87a6 am: 07e09a6ba3

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/extras/+/12128008

Change-Id: I81a7887d7274c2fc93904d119f7d5b48cc6714ce

Change Summary

Incremental Difference

--- a/bootctl/bootctl.cpp
+++ b/bootctl/bootctl.cpp
@@ -31,7 +31,12 @@ using android::hardware::boot::V1_0::Slot;
3131 using android::hardware::boot::V1_1::IBootControl;
3232 using android::hardware::boot::V1_1::MergeStatus;
3333
34-static void usage(FILE* where, int /* argc */, char* argv[])
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+
39+static void usage(FILE* where, BootCtlVersion bootVersion, int /* argc */, char* argv[])
3540 {
3641 fprintf(where,
3742 "%s - command-line wrapper for the boot HAL.\n"
@@ -48,18 +53,22 @@ static void usage(FILE* where, int /* argc */, char* argv[])
4853 " set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
4954 " is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n"
5055 " is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.\n"
51- " get-suffix SLOT - Prints suffix for SLOT.\n"
52- " set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic\n"
53- " partition is in progress. Valid STAT values\n"
54- " are: none, unknown, snapshotted, merging,\n"
55- " or cancelled.\n"
56- " get-snapshot-merge-status - Prints the current snapshot-merge status.\n"
57- "\n"
58- "SLOT parameter is the zero-based slot-number.\n",
56+ " get-suffix SLOT - Prints suffix for SLOT.\n",
5957 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");
6069 }
6170
62-static int do_hal_info(const sp<IBootControl> module) {
71+static int do_hal_info(const sp<V1_0::IBootControl> module) {
6372 module->interfaceDescriptor([&](const auto& descriptor) {
6473 fprintf(stdout,
6574 "HAL Version: %s\n",
@@ -68,14 +77,14 @@ static int do_hal_info(const sp<IBootControl> module) {
6877 return EX_OK;
6978 }
7079
71-static int do_get_number_slots(sp<IBootControl> module)
80+static int do_get_number_slots(sp<V1_0::IBootControl> module)
7281 {
7382 uint32_t numSlots = module->getNumberSlots();
7483 fprintf(stdout, "%u\n", numSlots);
7584 return EX_OK;
7685 }
7786
78-static int do_get_current_slot(sp<IBootControl> module)
87+static int do_get_current_slot(sp<V1_0::IBootControl> module)
7988 {
8089 Slot curSlot = module->getCurrentSlot();
8190 fprintf(stdout, "%u\n", curSlot);
@@ -99,14 +108,14 @@ static int handle_return(const Return<void> &ret, CommandResult cr, const char*
99108 return EX_OK;
100109 }
101110
102-static int do_mark_boot_successful(sp<IBootControl> module)
111+static int do_mark_boot_successful(sp<V1_0::IBootControl> module)
103112 {
104113 CommandResult cr;
105114 Return<void> ret = module->markBootSuccessful(generate_callback(&cr));
106115 return handle_return(ret, cr, "Error marking as having booted successfully: %s\n");
107116 }
108117
109-static int do_set_active_boot_slot(sp<IBootControl> module,
118+static int do_set_active_boot_slot(sp<V1_0::IBootControl> module,
110119 Slot slot_number)
111120 {
112121 CommandResult cr;
@@ -114,7 +123,7 @@ static int do_set_active_boot_slot(sp<IBootControl> module,
114123 return handle_return(ret, cr, "Error setting active boot slot: %s\n");
115124 }
116125
117-static int do_set_slot_as_unbootable(sp<IBootControl> module,
126+static int do_set_slot_as_unbootable(sp<V1_0::IBootControl> module,
118127 Slot slot_number)
119128 {
120129 CommandResult cr;
@@ -135,13 +144,13 @@ static int handle_return(const Return<BoolResult> &ret, const char* errStr) {
135144 return EX_SOFTWARE;
136145 }
137146
138-static int do_is_slot_bootable(sp<IBootControl> module, Slot slot_number)
147+static int do_is_slot_bootable(sp<V1_0::IBootControl> module, Slot slot_number)
139148 {
140149 Return<BoolResult> ret = module->isSlotBootable(slot_number);
141150 return handle_return(ret, "Error calling isSlotBootable(): %s\n");
142151 }
143152
144-static int do_is_slot_marked_successful(sp<IBootControl> module,
153+static int do_is_slot_marked_successful(sp<V1_0::IBootControl> module,
145154 Slot slot_number)
146155 {
147156 Return<BoolResult> ret = module->isSlotMarkedSuccessful(slot_number);
@@ -157,16 +166,17 @@ std::optional<MergeStatus> stringToMergeStatus(const std::string &status) {
157166 return {};
158167 }
159168
160-static int do_set_snapshot_merge_status(sp<IBootControl> module, int argc, char *argv[]) {
169+static int do_set_snapshot_merge_status(sp<V1_1::IBootControl> module, BootCtlVersion bootVersion,
170+ int argc, char *argv[]) {
161171 if (argc != 3) {
162- usage(stderr, argc, argv);
172+ usage(stderr, bootVersion, argc, argv);
163173 exit(EX_USAGE);
164174 return -1;
165175 }
166176
167177 auto status = stringToMergeStatus(argv[2]);
168178 if (!status.has_value()) {
169- usage(stderr, argc, argv);
179+ usage(stderr, bootVersion, argc, argv);
170180 exit(EX_USAGE);
171181 return -1;
172182 }
@@ -194,7 +204,7 @@ std::ostream& operator<<(std::ostream& os, MergeStatus state) {
194204 }
195205 }
196206
197-static int do_get_snapshot_merge_status(sp<IBootControl> module) {
207+static int do_get_snapshot_merge_status(sp<V1_1::IBootControl> module) {
198208 MergeStatus ret = module->getSnapshotMergeStatus();
199209 std::stringstream ss;
200210 ss << ret;
@@ -202,7 +212,7 @@ static int do_get_snapshot_merge_status(sp<IBootControl> module) {
202212 return EX_OK;
203213 }
204214
205-static int do_get_suffix(sp<IBootControl> module, Slot slot_number) {
215+static int do_get_suffix(sp<V1_0::IBootControl> module, Slot slot_number) {
206216 std::function<void(hidl_string)> cb = [](hidl_string suffix){
207217 fprintf(stdout, "%s\n", suffix.c_str());
208218 };
@@ -215,17 +225,17 @@ static int do_get_suffix(sp<IBootControl> module, Slot slot_number) {
215225 return EX_OK;
216226 }
217227
218-static uint32_t parse_slot(int pos, int argc, char *argv[])
228+static uint32_t parse_slot(BootCtlVersion bootVersion, int pos, int argc, char *argv[])
219229 {
220230 if (pos > argc - 1) {
221- usage(stderr, argc, argv);
231+ usage(stderr, bootVersion, argc, argv);
222232 exit(EX_USAGE);
223233 return -1;
224234 }
225235 errno = 0;
226236 uint64_t ret = strtoul(argv[pos], NULL, 10);
227237 if (errno != 0 || ret > UINT_MAX) {
228- usage(stderr, argc, argv);
238+ usage(stderr, bootVersion, argc, argv);
229239 exit(EX_USAGE);
230240 return -1;
231241 }
@@ -234,45 +244,62 @@ static uint32_t parse_slot(int pos, int argc, char *argv[])
234244
235245 int main(int argc, char *argv[])
236246 {
237- sp<IBootControl> module;
247+ sp<V1_0::IBootControl> v1_0_module;
248+ sp<V1_1::IBootControl> v1_1_module;
249+ BootCtlVersion bootVersion = BOOTCTL_V1_0;
238250
239- if (argc < 2) {
240- usage(stderr, argc, argv);
241- return EX_USAGE;
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;
242259 }
243260
244- module = IBootControl::getService();
245- if (module == NULL) {
246- fprintf(stderr, "Error getting bootctrl module.\n");
247- return EX_SOFTWARE;
261+ if (argc < 2) {
262+ usage(stderr, bootVersion, argc, argv);
263+ return EX_USAGE;
248264 }
249265
266+ // Functions present from version 1.0
250267 if (strcmp(argv[1], "hal-info") == 0) {
251- return do_hal_info(module);
268+ return do_hal_info(v1_0_module);
252269 } else if (strcmp(argv[1], "get-number-slots") == 0) {
253- return do_get_number_slots(module);
270+ return do_get_number_slots(v1_0_module);
254271 } else if (strcmp(argv[1], "get-current-slot") == 0) {
255- return do_get_current_slot(module);
272+ return do_get_current_slot(v1_0_module);
256273 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
257- return do_mark_boot_successful(module);
274+ return do_mark_boot_successful(v1_0_module);
258275 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
259- return do_set_active_boot_slot(module, parse_slot(2, argc, argv));
276+ return do_set_active_boot_slot(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
260277 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
261- return do_set_slot_as_unbootable(module, parse_slot(2, argc, argv));
278+ return do_set_slot_as_unbootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
262279 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
263- return do_is_slot_bootable(module, parse_slot(2, argc, argv));
264- } else if (strcmp(argv[1], "get-suffix") == 0) {
265- return do_get_suffix(module, parse_slot(2, argc, argv));
280+ return do_is_slot_bootable(v1_0_module, parse_slot(bootVersion, 2, argc, argv));
266281 } else if (strcmp(argv[1], "is-slot-marked-successful") == 0) {
267- return do_is_slot_marked_successful(module, parse_slot(2, argc, argv));
268- } else if (strcmp(argv[1], "set-snapshot-merge-status") == 0) {
269- return do_set_snapshot_merge_status(module, argc, argv);
270- } else if (strcmp(argv[1], "get-snapshot-merge-status") == 0) {
271- return do_get_snapshot_merge_status(module);
272- } else {
273- usage(stderr, argc, argv);
274- return EX_USAGE;
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+ }
275300 }
276301
277- return 0;
302+ // Parameter not matched, print usage
303+ usage(stderr, bootVersion, argc, argv);
304+ return EX_USAGE;
278305 }
Show on old repository browser