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 <android/hardware/boot/1.2/IBootControl.h>
21 #include <sysexits.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 namespace V1_2 = android::hardware::boot::V1_2;
37 
38 enum BootCtlVersion { BOOTCTL_V1_0, BOOTCTL_V1_1, BOOTCTL_V1_2 };
39 
usage(FILE * where,BootCtlVersion bootVersion,int,char * argv[])40 static void usage(FILE* where, BootCtlVersion bootVersion, int /* argc */, char* argv[]) {
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             "  get-active-boot-slot           - Prints the SLOT to load on next boot.\n"
53             "  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"
57             "  get-suffix SLOT                - Prints suffix for SLOT.\n",
58             argv[0], argv[0]);
59     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");
70 }
71 
do_hal_info(const sp<V1_0::IBootControl> module)72 static int do_hal_info(const sp<V1_0::IBootControl> module) {
73     module->interfaceDescriptor([&](const auto& descriptor) {
74         fprintf(stdout, "HAL Version: %s\n", descriptor.c_str());
75     });
76     return EX_OK;
77 }
78 
do_get_number_slots(sp<V1_0::IBootControl> module)79 static int do_get_number_slots(sp<V1_0::IBootControl> module) {
80     uint32_t numSlots = module->getNumberSlots();
81     fprintf(stdout, "%u\n", numSlots);
82     return EX_OK;
83 }
84 
do_get_current_slot(sp<V1_0::IBootControl> module)85 static int do_get_current_slot(sp<V1_0::IBootControl> module) {
86     Slot curSlot = module->getCurrentSlot();
87     fprintf(stdout, "%u\n", curSlot);
88     return EX_OK;
89 }
90 
generate_callback(CommandResult * crp)91 static std::function<void(CommandResult)> generate_callback(CommandResult* crp) {
92     return [=](CommandResult cr) { *crp = cr; };
93 }
94 
handle_return(const Return<void> & ret,CommandResult cr,const char * errStr)95 static int handle_return(const Return<void>& ret, CommandResult cr, const char* errStr) {
96     if (!ret.isOk()) {
97         fprintf(stderr, errStr, ret.description().c_str());
98         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 
do_mark_boot_successful(sp<V1_0::IBootControl> module)106 static int do_mark_boot_successful(sp<V1_0::IBootControl> module) {
107     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 
do_get_active_boot_slot(sp<V1_2::IBootControl> module)112 static 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 
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, Slot slot_number) {
119     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 
do_set_slot_as_unbootable(sp<V1_0::IBootControl> module,Slot slot_number)124 static int do_set_slot_as_unbootable(sp<V1_0::IBootControl> module, Slot slot_number) {
125     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 
handle_return(const Return<BoolResult> & ret,const char * errStr)130 static int handle_return(const Return<BoolResult>& ret, const char* errStr) {
131     if (!ret.isOk()) {
132         fprintf(stderr, errStr, ret.description().c_str());
133         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 
do_is_slot_bootable(sp<V1_0::IBootControl> module,Slot slot_number)143 static int do_is_slot_bootable(sp<V1_0::IBootControl> module, Slot slot_number) {
144     Return<BoolResult> ret = module->isSlotBootable(slot_number);
145     return handle_return(ret, "Error calling isSlotBootable(): %s\n");
146 }
147 
do_is_slot_marked_successful(sp<V1_0::IBootControl> module,Slot slot_number)148 static int do_is_slot_marked_successful(sp<V1_0::IBootControl> module, Slot slot_number) {
149     Return<BoolResult> ret = module->isSlotMarkedSuccessful(slot_number);
150     return handle_return(ret, "Error calling isSlotMarkedSuccessful(): %s\n");
151 }
152 
stringToMergeStatus(const std::string & status)153 std::optional<MergeStatus> stringToMergeStatus(const std::string& status) {
154     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 
do_set_snapshot_merge_status(sp<V1_1::IBootControl> module,BootCtlVersion bootVersion,int argc,char * argv[])162 static int do_set_snapshot_merge_status(sp<V1_1::IBootControl> module, BootCtlVersion bootVersion,
163                                         int argc, char* argv[]) {
164     if (argc != 3) {
165         usage(stderr, bootVersion, argc, argv);
166         exit(EX_USAGE);
167         return -1;
168     }
169 
170     auto status = stringToMergeStatus(argv[2]);
171     if (!status.has_value()) {
172         usage(stderr, bootVersion, argc, argv);
173         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 
operator <<(std::ostream & os,MergeStatus state)183 std::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 
do_get_snapshot_merge_status(sp<V1_1::IBootControl> module)200 static int do_get_snapshot_merge_status(sp<V1_1::IBootControl> module) {
201     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 }
207 
do_get_suffix(sp<V1_0::IBootControl> module,Slot slot_number)208 static int do_get_suffix(sp<V1_0::IBootControl> module, Slot slot_number) {
209     std::function<void(hidl_string)> cb = [](hidl_string suffix) {
210         fprintf(stdout, "%s\n", suffix.c_str());
211     };
212     Return<void> ret = module->getSuffix(slot_number, cb);
213     if (!ret.isOk()) {
214         fprintf(stderr, "Error calling getSuffix(): %s\n", ret.description().c_str());
215         return EX_SOFTWARE;
216     }
217     return EX_OK;
218 }
219 
parse_slot(BootCtlVersion bootVersion,int pos,int argc,char * argv[])220 static uint32_t parse_slot(BootCtlVersion bootVersion, int pos, int argc, char* argv[]) {
221     if (pos > argc - 1) {
222         usage(stderr, bootVersion, argc, argv);
223         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) {
229         usage(stderr, bootVersion, argc, argv);
230         exit(EX_USAGE);
231         return -1;
232     }
233     return (uint32_t)ret;
234 }
235 
main(int argc,char * argv[])236 int main(int argc, char* argv[]) {
237     sp<V1_0::IBootControl> v1_0_module;
238     sp<V1_1::IBootControl> v1_1_module;
239     sp<V1_2::IBootControl> v1_2_module;
240     BootCtlVersion bootVersion = BOOTCTL_V1_0;
241 
242     v1_0_module = V1_0::IBootControl::getService();
243     if (v1_0_module == nullptr) {
244         fprintf(stderr, "Error getting bootctrl v1.0 module.\n");
245         return EX_SOFTWARE;
246     }
247     v1_1_module = V1_1::IBootControl::castFrom(v1_0_module);
248     if (v1_1_module != nullptr) {
249         bootVersion = BOOTCTL_V1_1;
250     }
251 
252     v1_2_module = V1_2::IBootControl::castFrom(v1_0_module);
253     if (v1_2_module != nullptr) {
254         bootVersion = BOOTCTL_V1_2;
255     }
256 
257     if (argc < 2) {
258         usage(stderr, bootVersion, argc, argv);
259         return EX_USAGE;
260     }
261 
262     // 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 ||
285         strcmp(argv[1], "get-snapshot-merge-status") == 0) {
286         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 
297     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 
306     // Parameter not matched, print usage
307     usage(stderr, bootVersion, argc, argv);
308     return EX_USAGE;
309 }
310