1 /*
2 * Copyright (C) 2015 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 <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <sysexits.h>
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <hardware/hardware.h>
29 #include <hardware/boot_control.h>
30
usage(FILE * where,int argc,char * argv[])31 static void usage(FILE* where, int argc, char* argv[])
32 {
33 fprintf(where,
34 "%s - command-line wrapper for the boot_control HAL.\n"
35 "\n"
36 "Usage:\n"
37 " %s COMMAND\n"
38 "\n"
39 "Commands:\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 is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.\n"
48 " %s get-suffix SLOT - Prints suffix for SLOT.\n"
49 "\n"
50 "SLOT parameter is the zero-based slot-number.\n",
51 argv[0], argv[0], argv[0], argv[0], argv[0], argv[0],
52 argv[0], argv[0], argv[0], argv[0], argv[0]);
53 }
54
do_hal_info(const hw_module_t * hw_module)55 static int do_hal_info(const hw_module_t *hw_module)
56 {
57 fprintf(stdout,
58 "HAL name: %s\n"
59 "HAL author: %s\n"
60 "HAL module version: %d.%d\n",
61 hw_module->name,
62 hw_module->author,
63 hw_module->module_api_version>>8,
64 hw_module->module_api_version&0xff);
65 return EX_OK;
66 }
67
do_get_number_slots(boot_control_module_t * module)68 static int do_get_number_slots(boot_control_module_t *module)
69 {
70 int num_slots = module->getNumberSlots(module);
71 fprintf(stdout, "%d\n", num_slots);
72 return EX_OK;
73 }
74
do_get_current_slot(boot_control_module_t * module)75 static int do_get_current_slot(boot_control_module_t *module)
76 {
77 int cur_slot = module->getCurrentSlot(module);
78 fprintf(stdout, "%d\n", cur_slot);
79 return EX_OK;
80 }
81
do_mark_boot_successful(boot_control_module_t * module)82 static int do_mark_boot_successful(boot_control_module_t *module)
83 {
84 int ret = module->markBootSuccessful(module);
85 if (ret == 0)
86 return EX_OK;
87 fprintf(stderr, "Error marking as having booted successfully: %s\n",
88 strerror(-ret));
89 return EX_SOFTWARE;
90 }
91
do_set_active_boot_slot(boot_control_module_t * module,int slot_number)92 static int do_set_active_boot_slot(boot_control_module_t *module,
93 int slot_number)
94 {
95 int ret = module->setActiveBootSlot(module, slot_number);
96 if (ret == 0)
97 return EX_OK;
98 fprintf(stderr, "Error setting active boot slot: %s\n", strerror(-ret));
99 return EX_SOFTWARE;
100 }
101
do_set_slot_as_unbootable(boot_control_module_t * module,int slot_number)102 static int do_set_slot_as_unbootable(boot_control_module_t *module,
103 int slot_number)
104 {
105 int ret = module->setSlotAsUnbootable(module, slot_number);
106 if (ret == 0)
107 return EX_OK;
108 fprintf(stderr, "Error setting slot as unbootable: %s\n", strerror(-ret));
109 return EX_SOFTWARE;
110 }
111
112
do_is_slot_bootable(boot_control_module_t * module,int slot_number)113 static int do_is_slot_bootable(boot_control_module_t *module, int slot_number)
114 {
115 int ret = module->isSlotBootable(module, slot_number);
116 if (ret == 0) {
117 return EX_SOFTWARE;
118 } else if (ret < 0) {
119 fprintf(stderr, "Error calling isSlotBootable(): %s\n",
120 strerror(-ret));
121 return EX_SOFTWARE;
122 }
123 return EX_OK;
124 }
125
126
do_get_suffix(boot_control_module_t * module,int slot_number)127 static int do_get_suffix(boot_control_module_t *module, int slot_number)
128 {
129 const char* suffix = module->getSuffix(module, slot_number);
130 fprintf(stdout, "%s\n", suffix);
131 return EX_OK;
132 }
133
do_is_slot_marked_successful(boot_control_module_t * module,int slot_number)134 static int do_is_slot_marked_successful(boot_control_module_t *module,
135 int slot_number)
136 {
137 if (module->isSlotMarkedSuccessful == NULL) {
138 fprintf(stderr, "isSlotMarkedSuccessful() is not implemented by HAL.\n");
139 return EX_UNAVAILABLE;
140 }
141 int ret = module->isSlotMarkedSuccessful(module, slot_number);
142 if (ret == 0) {
143 return EX_SOFTWARE;
144 } else if (ret < 0) {
145 fprintf(stderr, "Error calling isSlotMarkedSuccessful(): %s\n",
146 strerror(-ret));
147 return EX_SOFTWARE;
148 }
149 return EX_OK;
150 }
151
parse_slot(int pos,int argc,char * argv[])152 static int parse_slot(int pos, int argc, char *argv[])
153 {
154 if (pos > argc - 1) {
155 usage(stderr, argc, argv);
156 exit(EX_USAGE);
157 return -1;
158 }
159 errno = 0;
160 long int ret = strtol(argv[pos], NULL, 10);
161 if (errno != 0 || ret > INT_MAX || ret < 0) {
162 usage(stderr, argc, argv);
163 exit(EX_USAGE);
164 return -1;
165 }
166 return (int)ret;
167 }
168
main(int argc,char * argv[])169 int main(int argc, char *argv[])
170 {
171 const hw_module_t *hw_module;
172 boot_control_module_t *module;
173 int ret;
174
175 if (argc < 2) {
176 usage(stderr, argc, argv);
177 return EX_USAGE;
178 }
179
180 ret = hw_get_module("bootctrl", &hw_module);
181 if (ret != 0) {
182 fprintf(stderr, "Error getting bootctrl module.\n");
183 return EX_SOFTWARE;
184 }
185 module = (boot_control_module_t*) hw_module;
186 module->init(module);
187
188 if (strcmp(argv[1], "hal-info") == 0) {
189 return do_hal_info(hw_module);
190 } else if (strcmp(argv[1], "get-number-slots") == 0) {
191 return do_get_number_slots(module);
192 } else if (strcmp(argv[1], "get-current-slot") == 0) {
193 return do_get_current_slot(module);
194 } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
195 return do_mark_boot_successful(module);
196 } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
197 return do_set_active_boot_slot(module, parse_slot(2, argc, argv));
198 } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
199 return do_set_slot_as_unbootable(module, parse_slot(2, argc, argv));
200 } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
201 return do_is_slot_bootable(module, parse_slot(2, argc, argv));
202 } else if (strcmp(argv[1], "get-suffix") == 0) {
203 return do_get_suffix(module, parse_slot(2, argc, argv));
204 } else if (strcmp(argv[1], "is-slot-marked-successful") == 0) {
205 return do_is_slot_marked_successful(module, parse_slot(2, argc, argv));
206 } else {
207 usage(stderr, argc, argv);
208 return EX_USAGE;
209 }
210
211 return 0;
212 }
213