summaryrefslogtreecommitdiffstats
path: root/commands/boot.c
blob: 33d1177861a13d83ec8f4cf63589036f0e3c463a (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <environment.h>
#include <globalvar.h>
#include <magicvar.h>
#include <command.h>
#include <common.h>
#include <getopt.h>
#include <libgen.h>
#include <malloc.h>
#include <boot.h>
#include <fs.h>

#include <linux/stat.h>

static int verbose;
static int dryrun;

static void bootsources_list(void)
{
	DIR *dir;
	struct dirent *d;
	const char *path = "/env/boot";

	dir = opendir(path);
	if (!dir) {
		printf("cannot open %s: %s\n", path, strerror(-errno));
		return;
	}

	printf("Bootsources: ");

	while ((d = readdir(dir))) {
		if (*d->d_name == '.')
			continue;

		printf("%s ", d->d_name);
	}

	printf("\n");

	closedir(dir);
}

static const char *getenv_or_null(const char *var)
{
	const char *val = getenv(var);

	if (val && *val)
		return val;
	return NULL;
}

/*
 * Start a single boot script. 'path' is a full path to a boot script.
 */
static int boot_script(char *path)
{
	struct bootm_data data = {};
	int ret;

	printf("booting %s...\n", basename(path));

	globalvar_set_match("linux.bootargs.dyn.", "");
	globalvar_set_match("bootm.", "");

	ret = run_command(path, 0);
	if (ret) {
		printf("Running %s failed\n", path);
		goto out;
	}

	data.initrd_address = UIMAGE_INVALID_ADDRESS;
	data.os_address = UIMAGE_SOME_ADDRESS;
	data.oftree_file = getenv_or_null("global.bootm.oftree");
	data.os_file = getenv_or_null("global.bootm.image");
	getenv_ul("global.bootm.image.loadaddr", &data.os_address);
	getenv_ul("global.bootm.initrd.loadaddr", &data.initrd_address);
	data.initrd_file = getenv_or_null("global.bootm.initrd");
	data.verbose = verbose;
	data.dryrun = dryrun;

	ret = bootm_boot(&data);
	if (ret)
		pr_err("Booting %s failed: %s\n", basename(path), strerror(-ret));
out:
	return ret;
}

/*
 * boot a script. 'name' can either be a filename under /env/boot/,
 * a full path to a boot script or a path to a directory. This function
 * returns a negative error on failure, or 0 on a successful dryrun boot.
 */
static int boot(const char *name)
{
	char *path;
	DIR *dir;
	struct dirent *d;
	struct stat s;
	int ret;

	if (*name == '/')
		path = xstrdup(name);
	else
		path = asprintf("/env/boot/%s", name);

	ret = stat(path, &s);
	if (ret) {
		pr_err("%s: %s\n", path, strerror(-ret));
		goto out;
	}

	if (S_ISREG(s.st_mode)) {
		ret = boot_script(path);
		goto out;
	}

	dir = opendir(path);
	if (!dir) {
		ret = -errno;
		printf("cannot open %s: %s\n", path, strerror(-errno));
		goto out;
	}

	while ((d = readdir(dir))) {
		char *file;
		struct stat s;

		if (*d->d_name == '.')
			continue;

		file = asprintf("%s/%s", path, d->d_name);

		ret = stat(file, &s);
		if (ret) {
			free(file);
			continue;
		}

		if (!S_ISREG(s.st_mode)) {
			free(file);
			continue;
		}

		ret = boot_script(file);

                free(file);

		if (!ret)
			break;
	}

	closedir(dir);
out:
	free(path);

	return ret;
}

static int do_boot(int argc, char *argv[])
{
	const char *sources = NULL;
	char *source, *freep;
	int opt, ret = 0, do_list = 0;

	verbose = 0;
	dryrun = 0;

	while ((opt = getopt(argc, argv, "vld")) > 0) {
		switch (opt) {
		case 'v':
			verbose++;
			break;
		case 'l':
			do_list = 1;
			break;
		case 'd':
			dryrun = 1;
			break;
		}
	}

	if (do_list) {
		bootsources_list();
		return 0;
	}

	if (optind < argc) {
		while (optind < argc) {
			source = argv[optind];
			optind++;
			ret = boot(source);
			if (!ret)
				break;
		}
		return ret;
	}

	sources = getenv("global.boot.default");
	if (!sources)
		return 0;

	freep = source = xstrdup(sources);

	while (1) {
		char *sep = strchr(source, ' ');
		if (sep)
			*sep = 0;
		ret = boot(source);
		if (!ret)
			break;

		if (sep)
			source = sep + 1;
		else
			break;
	}

	free(freep);

	return ret;
}

BAREBOX_CMD_HELP_START(boot)
BAREBOX_CMD_HELP_USAGE("boot [OPTIONS] [BOOTSRC...]\n")
BAREBOX_CMD_HELP_SHORT("Boot an operating system.\n")
BAREBOX_CMD_HELP_SHORT("[BOOTSRC...] can be:\n")
BAREBOX_CMD_HELP_SHORT("- a filename from /env/boot/\n")
BAREBOX_CMD_HELP_SHORT("- a full path to a file\n")
BAREBOX_CMD_HELP_SHORT("- a path to a directory. All files in this directory are treated\n")
BAREBOX_CMD_HELP_SHORT("  as boot scripts.\n")
BAREBOX_CMD_HELP_SHORT("Multiple bootsources may be given which are probed in order until\n")
BAREBOX_CMD_HELP_SHORT("one succeeds.\n")
BAREBOX_CMD_HELP_SHORT("\nOptions:\n")
BAREBOX_CMD_HELP_OPT  ("-v","Increase verbosity\n")
BAREBOX_CMD_HELP_OPT  ("-d","Dryrun. See what happens but do no actually boot\n")
BAREBOX_CMD_HELP_OPT  ("-l","List available boot sources\n")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(boot)
	.cmd	= do_boot,
	.usage		= "boot the machine",
	BAREBOX_CMD_HELP(cmd_boot_help)
BAREBOX_CMD_END

BAREBOX_MAGICVAR_NAMED(global_boot_default, global.boot.default, "default boot order");