OSDN Git Service

am e8d9e9a3: am d36de04a: Merge "bootctl: Command-line wrapper for boot_control HAL."
[android-x86/system-extras.git] / bootctl / bootctl.c
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
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 get-suffix SLOT             - Prints suffix for SLOT.\n"
48             "\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]);
52 }
53
54 static int do_hal_info(const hw_module_t *hw_module)
55 {
56     fprintf(stdout,
57             "HAL name:            %s\n"
58             "HAL author:          %s\n"
59             "HAL module version:  %d.%d\n",
60             hw_module->name,
61             hw_module->author,
62             hw_module->module_api_version>>8,
63             hw_module->module_api_version&0xff);
64     return EX_OK;
65 }
66
67 static int do_get_number_slots(boot_control_module_t *module)
68 {
69     int num_slots = module->getNumberSlots(module);
70     fprintf(stdout, "%d\n", num_slots);
71     return EX_OK;
72 }
73
74 static int do_get_current_slot(boot_control_module_t *module)
75 {
76     int cur_slot = module->getCurrentSlot(module);
77     fprintf(stdout, "%d\n", cur_slot);
78     return EX_OK;
79 }
80
81 static int do_mark_boot_successful(boot_control_module_t *module)
82 {
83     int ret = module->markBootSuccessful(module);
84     if (ret == 0)
85         return EX_OK;
86     fprintf(stderr, "Error marking as having booted successfully: %s\n",
87             strerror(-ret));
88     return EX_SOFTWARE;
89 }
90
91 static int do_set_active_boot_slot(boot_control_module_t *module,
92                                    int slot_number)
93 {
94     int ret = module->setActiveBootSlot(module, slot_number);
95     if (ret == 0)
96         return EX_OK;
97     fprintf(stderr, "Error setting active boot slot: %s\n", strerror(-ret));
98     return EX_SOFTWARE;
99 }
100
101 static int do_set_slot_as_unbootable(boot_control_module_t *module,
102                                      int slot_number)
103 {
104     int ret = module->setSlotAsUnbootable(module, slot_number);
105     if (ret == 0)
106         return EX_OK;
107     fprintf(stderr, "Error setting slot as unbootable: %s\n", strerror(-ret));
108     return EX_SOFTWARE;
109 }
110
111
112 static int do_is_slot_bootable(boot_control_module_t *module, int slot_number)
113 {
114     int ret = module->isSlotBootable(module, slot_number);
115     if (ret == 0)
116         return EX_SOFTWARE;
117     return EX_OK;
118 }
119
120
121 static int do_get_suffix(boot_control_module_t *module, int slot_number)
122 {
123     const char* suffix = module->getSuffix(module, slot_number);
124     fprintf(stdout, "%s\n", suffix);
125     return EX_OK;
126 }
127
128 static int parse_slot(int pos, int argc, char *argv[])
129 {
130     if (pos > argc - 1) {
131         usage(stderr, argc, argv);
132         exit(EX_USAGE);
133         return -1;
134     }
135     int ret = strtol(argv[pos], NULL, 10);
136     if (ret == LONG_MIN || ret == LONG_MAX) {
137         usage(stderr, argc, argv);
138         exit(EX_USAGE);
139         return -1;
140     }
141     return ret;
142 }
143
144 int main(int argc, char *argv[])
145 {
146     const hw_module_t *hw_module;
147     boot_control_module_t *module;
148     int ret;
149
150     ret = hw_get_module("bootctrl", &hw_module);
151     if (ret != 0) {
152         fprintf(stderr, "Error getting bootctrl module.\n");
153         return EX_SOFTWARE;
154     }
155     module = (boot_control_module_t*) hw_module;
156     module->init(module);
157
158     if (argc < 2) {
159         usage(stderr, argc, argv);
160         return EX_USAGE;
161     }
162
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));
179     } else {
180         usage(stderr, argc, argv);
181         return EX_USAGE;
182     }
183
184     return 0;
185 }