summaryrefslogtreecommitdiff
path: root/bootctl/bootctl.c
blob: 58fdbb6967521ef5274557495f3f7fb53b1287d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sysexits.h>

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hardware/hardware.h>
#include <hardware/boot_control.h>

static void usage(FILE* where, int argc, char* argv[])
{
    fprintf(where,
            "%s - command-line wrapper for the boot_control HAL.\n"
            "\n"
            "Usage:\n"
            "  %s COMMAND\n"
            "\n"
            "Commands:\n"
            "  %s hal-info                    - Show info about boot_control HAL used.\n"
            "  %s get-number-slots            - Prints number of slots.\n"
            "  %s get-current-slot            - Prints currently running SLOT.\n"
            "  %s mark-boot-successful        - Mark current slot as GOOD.\n"
            "  %s set-active-boot-slot SLOT   - On next boot, load and execute SLOT.\n"
            "  %s set-slot-as-unbootable SLOT - Mark SLOT as invalid.\n"
            "  %s is-slot-bootable SLOT       - Returns 0 only if SLOT is bootable.\n"
            "  %s get-suffix SLOT             - Prints suffix for SLOT.\n"
            "\n"
            "SLOT parameter is the zero-based slot-number.\n",
            argv[0], argv[0], argv[0], argv[0], argv[0],
            argv[0], argv[0], argv[0], argv[0], argv[0]);
}

static int do_hal_info(const hw_module_t *hw_module)
{
    fprintf(stdout,
            "HAL name:            %s\n"
            "HAL author:          %s\n"
            "HAL module version:  %d.%d\n",
            hw_module->name,
            hw_module->author,
            hw_module->module_api_version>>8,
            hw_module->module_api_version&0xff);
    return EX_OK;
}

static int do_get_number_slots(boot_control_module_t *module)
{
    int num_slots = module->getNumberSlots(module);
    fprintf(stdout, "%d\n", num_slots);
    return EX_OK;
}

static int do_get_current_slot(boot_control_module_t *module)
{
    int cur_slot = module->getCurrentSlot(module);
    fprintf(stdout, "%d\n", cur_slot);
    return EX_OK;
}

static int do_mark_boot_successful(boot_control_module_t *module)
{
    int ret = module->markBootSuccessful(module);
    if (ret == 0)
        return EX_OK;
    fprintf(stderr, "Error marking as having booted successfully: %s\n",
            strerror(-ret));
    return EX_SOFTWARE;
}

static int do_set_active_boot_slot(boot_control_module_t *module,
                                   int slot_number)
{
    int ret = module->setActiveBootSlot(module, slot_number);
    if (ret == 0)
        return EX_OK;
    fprintf(stderr, "Error setting active boot slot: %s\n", strerror(-ret));
    return EX_SOFTWARE;
}

static int do_set_slot_as_unbootable(boot_control_module_t *module,
                                     int slot_number)
{
    int ret = module->setSlotAsUnbootable(module, slot_number);
    if (ret == 0)
        return EX_OK;
    fprintf(stderr, "Error setting slot as unbootable: %s\n", strerror(-ret));
    return EX_SOFTWARE;
}


static int do_is_slot_bootable(boot_control_module_t *module, int slot_number)
{
    int ret = module->isSlotBootable(module, slot_number);
    if (ret == 0)
        return EX_SOFTWARE;
    return EX_OK;
}


static int do_get_suffix(boot_control_module_t *module, int slot_number)
{
    const char* suffix = module->getSuffix(module, slot_number);
    fprintf(stdout, "%s\n", suffix);
    return EX_OK;
}

static int parse_slot(int pos, int argc, char *argv[])
{
    if (pos > argc - 1) {
        usage(stderr, argc, argv);
        exit(EX_USAGE);
        return -1;
    }
    errno = 0;
    long int ret = strtol(argv[pos], NULL, 10);
    if (errno != 0 || ret > INT_MAX || ret < 0) {
        usage(stderr, argc, argv);
        exit(EX_USAGE);
        return -1;
    }
    return (int)ret;
}

int main(int argc, char *argv[])
{
    const hw_module_t *hw_module;
    boot_control_module_t *module;
    int ret;

    if (argc < 2) {
        usage(stderr, argc, argv);
        return EX_USAGE;
    }

    ret = hw_get_module("bootctrl", &hw_module);
    if (ret != 0) {
        fprintf(stderr, "Error getting bootctrl module.\n");
        return EX_SOFTWARE;
    }
    module = (boot_control_module_t*) hw_module;
    module->init(module);

    if (strcmp(argv[1], "hal-info") == 0) {
        return do_hal_info(hw_module);
    } else if (strcmp(argv[1], "get-number-slots") == 0) {
        return do_get_number_slots(module);
    } else if (strcmp(argv[1], "get-current-slot") == 0) {
        return do_get_current_slot(module);
    } else if (strcmp(argv[1], "mark-boot-successful") == 0) {
        return do_mark_boot_successful(module);
    } else if (strcmp(argv[1], "set-active-boot-slot") == 0) {
        return do_set_active_boot_slot(module, parse_slot(2, argc, argv));
    } else if (strcmp(argv[1], "set-slot-as-unbootable") == 0) {
        return do_set_slot_as_unbootable(module, parse_slot(2, argc, argv));
    } else if (strcmp(argv[1], "is-slot-bootable") == 0) {
        return do_is_slot_bootable(module, parse_slot(2, argc, argv));
    } else if (strcmp(argv[1], "get-suffix") == 0) {
        return do_get_suffix(module, parse_slot(2, argc, argv));
    } else {
        usage(stderr, argc, argv);
        return EX_USAGE;
    }

    return 0;
}