summaryrefslogtreecommitdiff
path: root/cmd/bootdev.c
blob: fa7285ba25e1c04663b76fd2e8cd85ee9c95e27b (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * 'bootdev' command
 *
 * Copyright 2021 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <bootdev.h>
#include <bootflow.h>
#include <bootstd.h>
#include <command.h>
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>

static int bootdev_check_state(struct bootstd_priv **stdp)
{
	struct bootstd_priv *std;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return ret;
	if (!std->cur_bootdev) {
		printf("Please use 'bootdev select' first\n");
		return -ENOENT;
	}
	*stdp = std;

	return 0;
}

static int do_bootdev_list(struct cmd_tbl *cmdtp, int flag, int argc,
			   char *const argv[])
{
	bool probe;

	probe = argc >= 2 && !strcmp(argv[1], "-p");
	bootdev_list(probe);

	return 0;
}

static int do_bootdev_select(struct cmd_tbl *cmdtp, int flag, int argc,
			     char *const argv[])
{
	struct bootstd_priv *std;
	struct udevice *dev;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return CMD_RET_FAILURE;
	if (argc < 2) {
		std->cur_bootdev = NULL;
		return 0;
	}
	if (bootdev_find_by_any(argv[1], &dev, NULL))
		return CMD_RET_FAILURE;

	std->cur_bootdev = dev;

	return 0;
}

static int do_bootdev_info(struct cmd_tbl *cmdtp, int flag, int argc,
			   char *const argv[])
{
	struct bootstd_priv *priv;
	struct bootflow *bflow;
	int ret, i, num_valid;
	struct udevice *dev;
	bool probe;

	probe = argc >= 2 && !strcmp(argv[1], "-p");

	ret = bootdev_check_state(&priv);
	if (ret)
		return CMD_RET_FAILURE;

	dev = priv->cur_bootdev;

	/* Count the number of bootflows, including how many are valid*/
	num_valid = 0;
	for (ret = bootdev_first_bootflow(dev, &bflow), i = 0;
	     !ret;
	     ret = bootdev_next_bootflow(&bflow), i++)
		num_valid += bflow->state == BOOTFLOWST_READY;

	/*
	 * Prove the device, if requested, otherwise assume that there is no
	 * error
	 */
	ret = 0;
	if (probe)
		ret = device_probe(dev);

	printf("Name:      %s\n", dev->name);
	printf("Sequence:  %d\n", dev_seq(dev));
	printf("Status:    %s\n", ret ? simple_itoa(-ret) : device_active(dev) ?
		"Probed" : "OK");
	printf("Uclass:    %s\n", dev_get_uclass_name(dev_get_parent(dev)));
	printf("Bootflows: %d (%d valid)\n", i, num_valid);

	return 0;
}

static int do_bootdev_hunt(struct cmd_tbl *cmdtp, int flag, int argc,
			   char *const argv[])
{
	struct bootstd_priv *priv;
	const char *spec = NULL;
	bool list = false;
	int ret = 0;

	if (argc >= 2) {
		if (!strcmp(argv[1], "-l"))
			list = true;
		else
			spec = argv[1];
	}

	ret = bootstd_get_priv(&priv);
	if (ret)
		return ret;
	if (list) {
		bootdev_list_hunters(priv);
	} else {
		ret = bootdev_hunt(spec, true);
		if (ret) {
			printf("Failed (err=%dE)\n", ret);

			return CMD_RET_FAILURE;
		}
	}

	return 0;
}

U_BOOT_LONGHELP(bootdev,
	"list [-p]         - list all available bootdevs (-p to probe)\n"
	"bootdev hunt [-l|<spec>]  - use hunt drivers to find bootdevs\n"
	"bootdev select <bd>       - select a bootdev by name | label | seq\n"
	"bootdev info [-p]         - show information about a bootdev (-p to probe)");

U_BOOT_CMD_WITH_SUBCMDS(bootdev, "Boot devices", bootdev_help_text,
	U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootdev_list),
	U_BOOT_SUBCMD_MKENT(hunt, 2, 1, do_bootdev_hunt),
	U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootdev_select),
	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootdev_info));