2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
28 #include <hardware/hardware.h>
29 #include <hardware/boot_control.h>
31 static void usage(FILE* where, int argc, char* argv[])
34 "%s - command-line wrapper for the boot_control HAL.\n"
40 " %s hal-info - Show info about boot_control HAL used.\n"
41 " %s get-number-slots - Prints number of slots.\n"
42 " %s get-current-slot - Prints currently running SLOT.\n"
43 " %s mark-boot-successful - Mark current slot as GOOD.\n"
44 " %s set-active-boot-slot SLOT - On next boot, load and execute SLOT.\n"
45 " %s set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
46 " %s is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.\n"
47 " %s get-suffix SLOT - Prints suffix for SLOT.\n"
49 "SLOT parameter is the zero-based slot-number.\n",
50 argv[0], argv[0], argv[0], argv[0], argv[0],
51 argv[0], argv[0], argv[0], argv[0], argv[0]);
54 static int do_hal_info(const hw_module_t *hw_module)
59 "HAL module version: %d.%d\n",
62 hw_module->module_api_version>>8,
63 hw_module->module_api_version&0xff);
67 static int do_get_number_slots(boot_control_module_t *module)
69 int num_slots = module->getNumberSlots(module);
70 fprintf(stdout, "%d\n", num_slots);
74 static int do_get_current_slot(boot_control_module_t *module)
76 int cur_slot = module->getCurrentSlot(module);
77 fprintf(stdout, "%d\n", cur_slot);
81 static int do_mark_boot_successful(boot_control_module_t *module)
83 int ret = module->markBootSuccessful(module);
86 fprintf(stderr, "Error marking as having booted successfully: %s\n",
91 static int do_set_active_boot_slot(boot_control_module_t *module,
94 int ret = module->setActiveBootSlot(module, slot_number);
97 fprintf(stderr, "Error setting active boot slot: %s\n", strerror(-ret));
101 static int do_set_slot_as_unbootable(boot_control_module_t *module,
104 int ret = module->setSlotAsUnbootable(module, slot_number);
107 fprintf(stderr, "Error setting slot as unbootable: %s\n", strerror(-ret));
112 static int do_is_slot_bootable(boot_control_module_t *module, int slot_number)
114 int ret = module->isSlotBootable(module, slot_number);
121 static int do_get_suffix(boot_control_module_t *module, int slot_number)
123 const char* suffix = module->getSuffix(module, slot_number);
124 fprintf(stdout, "%s\n", suffix);
128 static int parse_slot(int pos, int argc, char *argv[])
130 if (pos > argc - 1) {
131 usage(stderr, argc, argv);
135 int ret = strtol(argv[pos], NULL, 10);
136 if (ret == LONG_MIN || ret == LONG_MAX) {
137 usage(stderr, argc, argv);
144 int main(int argc, char *argv[])
146 const hw_module_t *hw_module;
147 boot_control_module_t *module;
150 ret = hw_get_module("bootctrl", &hw_module);
152 fprintf(stderr, "Error getting bootctrl module.\n");
155 module = (boot_control_module_t*) hw_module;
156 module->init(module);
159 usage(stderr, argc, argv);
163 if (strcmp(argv[1], "hal-info") == 0) {
164 return do_hal_info(hw_module);
165 } else if (strcmp(argv[1], "get-number-slots") == 0) {
166 return do_get_number_slots(module);
167 } else if (strcmp(argv[1], "get-current-slot") == 0) {
168 return do_get_current_slot(module);
169 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
170 return do_mark_boot_successful(module);
171 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
172 return do_set_active_boot_slot(module, parse_slot(2, argc, argv));
173 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
174 return do_set_slot_as_unbootable(module, parse_slot(2, argc, argv));
175 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
176 return do_is_slot_bootable(module, parse_slot(2, argc, argv));
177 } else if (strcmp(argv[1], "get-suffix") == 0) {
178 return do_get_suffix(module, parse_slot(2, argc, argv));
180 usage(stderr, argc, argv);