summaryrefslogtreecommitdiffstats
path: root/commands/bootchooser.c
blob: 46b063e027db30073ada28487bc0434026cc081a (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: © 2012 Jan Luebbe <j.luebbe@pengutronix.de>
// SPDX-FileCopyrightText: © 2015 Marc Kleine-Budde <mkl@pengutronix.de>

#include <bootchooser.h>
#include <globalvar.h>
#include <command.h>
#include <common.h>
#include <getopt.h>
#include <malloc.h>
#include <stdio.h>

#define DONTTOUCH	-2
#define DEFAULT		-1

static void target_reset(struct bootchooser_target *target, int priority, int attempts)
{
	printf("Resetting target %s to ", bootchooser_target_name(target));

	if (priority >= 0)
		printf("priority %d", priority);
	else if (priority == DEFAULT)
		printf("default priority");

	if (priority > DONTTOUCH && attempts > DONTTOUCH)
		printf(", ");

	if (attempts >= 0)
		printf("%d attempts", attempts);
	else if (attempts == DEFAULT)
		printf("default attempts");

	printf("\n");

	if (priority > DONTTOUCH)
		bootchooser_target_set_priority(target, priority);
	if (attempts > DONTTOUCH)
		bootchooser_target_set_attempts(target, attempts);
}

static int do_bootchooser(int argc, char *argv[])
{
	int opt, ret = 0, i;
	struct bootchooser *bootchooser;
	struct bootchooser_target *target;
	int attempts = DONTTOUCH;
	int priority = DONTTOUCH;
	int info = 0;
	bool done_something = false;
	bool last_boot_successful = false;

	while ((opt = getopt(argc, argv, "a:p:is")) > 0) {
		switch (opt) {
		case 'a':
			if (!strcmp(optarg, "default"))
				attempts = DEFAULT;
			else
				attempts = simple_strtoul(optarg, NULL, 0);
			break;
		case 'p':
			if (!strcmp(optarg, "default"))
				priority = DEFAULT;
			else
				priority = simple_strtoul(optarg, NULL, 0);
			break;
		case 'i':
			info = 1;
			break;
		case 's':
			last_boot_successful = true;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	bootchooser = bootchooser_get();
	if (IS_ERR(bootchooser)) {
		printf("No bootchooser found\n");
		return COMMAND_ERROR;
	}

	if (last_boot_successful) {
		bootchooser_last_boot_successful();
		done_something = true;
	}

	if (attempts != DONTTOUCH || priority != DONTTOUCH) {
		if (optind < argc) {
			for (i = optind; i < argc; i++) {
				target = bootchooser_target_by_name(bootchooser, argv[i]);
				if (!target) {
					printf("No such target: %s\n", argv[i]);
					ret = COMMAND_ERROR;
					goto out;
				}

				target_reset(target, priority, attempts);
			}
		} else {
			bootchooser_for_each_target(bootchooser, target)
				target_reset(target, priority, attempts);
		}
		done_something = true;
	}

	if (info) {
		bootchooser_info(bootchooser);
		done_something = true;
	}

	if (!done_something) {
		printf("Nothing to do\n");
		ret = COMMAND_ERROR_USAGE;
	}
out:
	bootchooser_put(bootchooser);

	return ret;
}

BAREBOX_CMD_HELP_START(bootchooser)
BAREBOX_CMD_HELP_TEXT("Control misc behaviour of the bootchooser")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-a <n|default> [TARGETS]",  "set remaining attempts of given targets to 'n' or the default attempts")
BAREBOX_CMD_HELP_OPT ("-p <n|default> [TARGETS]",  "set priority of given targets to 'n' or the default priority")
BAREBOX_CMD_HELP_OPT ("-i",  "Show information about the bootchooser")
BAREBOX_CMD_HELP_OPT ("-s",  "Mark the last boot successful")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(bootchooser)
	.cmd = do_bootchooser,
	BAREBOX_CMD_DESC("bootchooser control")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_HELP(cmd_bootchooser_help)
BAREBOX_CMD_END