system/extras
Revision | b2dccf1e0de430806f6cd55a809143435b7e857b (tree) |
---|---|
Time | 2020-07-15 05:35:50 |
Author | Håkan Kvist <hakan.kvist@sony...> |
Commiter | Automerger Merge Worker |
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
@@ -31,7 +31,12 @@ using android::hardware::boot::V1_0::Slot; | ||
31 | 31 | using android::hardware::boot::V1_1::IBootControl; |
32 | 32 | using android::hardware::boot::V1_1::MergeStatus; |
33 | 33 | |
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[]) | |
35 | 40 | { |
36 | 41 | fprintf(where, |
37 | 42 | "%s - command-line wrapper for the boot HAL.\n" |
@@ -48,18 +53,22 @@ static void usage(FILE* where, int /* argc */, char* argv[]) | ||
48 | 53 | " set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n" |
49 | 54 | " is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n" |
50 | 55 | " 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", | |
59 | 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"); | |
60 | 69 | } |
61 | 70 | |
62 | -static int do_hal_info(const sp<IBootControl> module) { | |
71 | +static int do_hal_info(const sp<V1_0::IBootControl> module) { | |
63 | 72 | module->interfaceDescriptor([&](const auto& descriptor) { |
64 | 73 | fprintf(stdout, |
65 | 74 | "HAL Version: %s\n", |
@@ -68,14 +77,14 @@ static int do_hal_info(const sp<IBootControl> module) { | ||
68 | 77 | return EX_OK; |
69 | 78 | } |
70 | 79 | |
71 | -static int do_get_number_slots(sp<IBootControl> module) | |
80 | +static int do_get_number_slots(sp<V1_0::IBootControl> module) | |
72 | 81 | { |
73 | 82 | uint32_t numSlots = module->getNumberSlots(); |
74 | 83 | fprintf(stdout, "%u\n", numSlots); |
75 | 84 | return EX_OK; |
76 | 85 | } |
77 | 86 | |
78 | -static int do_get_current_slot(sp<IBootControl> module) | |
87 | +static int do_get_current_slot(sp<V1_0::IBootControl> module) | |
79 | 88 | { |
80 | 89 | Slot curSlot = module->getCurrentSlot(); |
81 | 90 | fprintf(stdout, "%u\n", curSlot); |
@@ -99,14 +108,14 @@ static int handle_return(const Return<void> &ret, CommandResult cr, const char* | ||
99 | 108 | return EX_OK; |
100 | 109 | } |
101 | 110 | |
102 | -static int do_mark_boot_successful(sp<IBootControl> module) | |
111 | +static int do_mark_boot_successful(sp<V1_0::IBootControl> module) | |
103 | 112 | { |
104 | 113 | CommandResult cr; |
105 | 114 | Return<void> ret = module->markBootSuccessful(generate_callback(&cr)); |
106 | 115 | return handle_return(ret, cr, "Error marking as having booted successfully: %s\n"); |
107 | 116 | } |
108 | 117 | |
109 | -static int do_set_active_boot_slot(sp<IBootControl> module, | |
118 | +static int do_set_active_boot_slot(sp<V1_0::IBootControl> module, | |
110 | 119 | Slot slot_number) |
111 | 120 | { |
112 | 121 | CommandResult cr; |
@@ -114,7 +123,7 @@ static int do_set_active_boot_slot(sp<IBootControl> module, | ||
114 | 123 | return handle_return(ret, cr, "Error setting active boot slot: %s\n"); |
115 | 124 | } |
116 | 125 | |
117 | -static int do_set_slot_as_unbootable(sp<IBootControl> module, | |
126 | +static int do_set_slot_as_unbootable(sp<V1_0::IBootControl> module, | |
118 | 127 | Slot slot_number) |
119 | 128 | { |
120 | 129 | CommandResult cr; |
@@ -135,13 +144,13 @@ static int handle_return(const Return<BoolResult> &ret, const char* errStr) { | ||
135 | 144 | return EX_SOFTWARE; |
136 | 145 | } |
137 | 146 | |
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) | |
139 | 148 | { |
140 | 149 | Return<BoolResult> ret = module->isSlotBootable(slot_number); |
141 | 150 | return handle_return(ret, "Error calling isSlotBootable(): %s\n"); |
142 | 151 | } |
143 | 152 | |
144 | -static int do_is_slot_marked_successful(sp<IBootControl> module, | |
153 | +static int do_is_slot_marked_successful(sp<V1_0::IBootControl> module, | |
145 | 154 | Slot slot_number) |
146 | 155 | { |
147 | 156 | Return<BoolResult> ret = module->isSlotMarkedSuccessful(slot_number); |
@@ -157,16 +166,17 @@ std::optional<MergeStatus> stringToMergeStatus(const std::string &status) { | ||
157 | 166 | return {}; |
158 | 167 | } |
159 | 168 | |
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[]) { | |
161 | 171 | if (argc != 3) { |
162 | - usage(stderr, argc, argv); | |
172 | + usage(stderr, bootVersion, argc, argv); | |
163 | 173 | exit(EX_USAGE); |
164 | 174 | return -1; |
165 | 175 | } |
166 | 176 | |
167 | 177 | auto status = stringToMergeStatus(argv[2]); |
168 | 178 | if (!status.has_value()) { |
169 | - usage(stderr, argc, argv); | |
179 | + usage(stderr, bootVersion, argc, argv); | |
170 | 180 | exit(EX_USAGE); |
171 | 181 | return -1; |
172 | 182 | } |
@@ -194,7 +204,7 @@ std::ostream& operator<<(std::ostream& os, MergeStatus state) { | ||
194 | 204 | } |
195 | 205 | } |
196 | 206 | |
197 | -static int do_get_snapshot_merge_status(sp<IBootControl> module) { | |
207 | +static int do_get_snapshot_merge_status(sp<V1_1::IBootControl> module) { | |
198 | 208 | MergeStatus ret = module->getSnapshotMergeStatus(); |
199 | 209 | std::stringstream ss; |
200 | 210 | ss << ret; |
@@ -202,7 +212,7 @@ static int do_get_snapshot_merge_status(sp<IBootControl> module) { | ||
202 | 212 | return EX_OK; |
203 | 213 | } |
204 | 214 | |
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) { | |
206 | 216 | std::function<void(hidl_string)> cb = [](hidl_string suffix){ |
207 | 217 | fprintf(stdout, "%s\n", suffix.c_str()); |
208 | 218 | }; |
@@ -215,17 +225,17 @@ static int do_get_suffix(sp<IBootControl> module, Slot slot_number) { | ||
215 | 225 | return EX_OK; |
216 | 226 | } |
217 | 227 | |
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[]) | |
219 | 229 | { |
220 | 230 | if (pos > argc - 1) { |
221 | - usage(stderr, argc, argv); | |
231 | + usage(stderr, bootVersion, argc, argv); | |
222 | 232 | exit(EX_USAGE); |
223 | 233 | return -1; |
224 | 234 | } |
225 | 235 | errno = 0; |
226 | 236 | uint64_t ret = strtoul(argv[pos], NULL, 10); |
227 | 237 | if (errno != 0 || ret > UINT_MAX) { |
228 | - usage(stderr, argc, argv); | |
238 | + usage(stderr, bootVersion, argc, argv); | |
229 | 239 | exit(EX_USAGE); |
230 | 240 | return -1; |
231 | 241 | } |
@@ -234,45 +244,62 @@ static uint32_t parse_slot(int pos, int argc, char *argv[]) | ||
234 | 244 | |
235 | 245 | int main(int argc, char *argv[]) |
236 | 246 | { |
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; | |
238 | 250 | |
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; | |
242 | 259 | } |
243 | 260 | |
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; | |
248 | 264 | } |
249 | 265 | |
266 | + // Functions present from version 1.0 | |
250 | 267 | if (strcmp(argv[1], "hal-info") == 0) { |
251 | - return do_hal_info(module); | |
268 | + return do_hal_info(v1_0_module); | |
252 | 269 | } 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); | |
254 | 271 | } 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); | |
256 | 273 | } 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); | |
258 | 275 | } 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)); | |
260 | 277 | } 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)); | |
262 | 279 | } 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)); | |
266 | 281 | } 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 | + } | |
275 | 300 | } |
276 | 301 | |
277 | - return 0; | |
302 | + // Parameter not matched, print usage | |
303 | + usage(stderr, bootVersion, argc, argv); | |
304 | + return EX_USAGE; | |
278 | 305 | } |