Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*-
 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * Loading modules, booting the system
 */

#include <stand.h>
#include <sys/reboot.h>
#include <sys/boot.h>
#include <string.h>

#include "bootstrap.h"

static int	autoboot(int timeout, char *prompt);
static char	*getbootfile(int try);
static int	loadakernel(int try, int argc, char* argv[]);

/* List of kernel names to try (may be overwritten by boot.config) XXX should move from here? */
static const char *default_bootfiles = "kernel";

static int autoboot_tried;

/*
 * The user wants us to boot.
 */
COMMAND_SET(boot, "boot", "boot a file or loaded kernel", command_boot);

static int
command_boot(int argc, char *argv[])
{
	struct preloaded_file	*fp;

	/*
	 * See if the user has specified an explicit kernel to boot.
	 */
	if ((argc > 1) && (argv[1][0] != '-')) {

		/* XXX maybe we should discard everything and start again? */
		if (file_findfile(NULL, NULL) != NULL) {
			snprintf(command_errbuf, sizeof(command_errbuf),
			    "can't boot '%s', kernel module already loaded", argv[1]);
			return(CMD_ERROR);
		}

		/* find/load the kernel module */
		if (mod_loadkld(argv[1], argc - 2, argv + 2) != 0)
			return(CMD_ERROR);
		/* we have consumed all arguments */
		argc = 1;
	}

	/*
	 * See if there is a kernel module already loaded
	 */
	if (file_findfile(NULL, NULL) == NULL)
		if (loadakernel(0, argc - 1, argv + 1))
			/* we have consumed all arguments */
			argc = 1;

	/*
	 * Loaded anything yet?
	 */
	if ((fp = file_findfile(NULL, NULL)) == NULL) {
		command_errmsg = "no bootable kernel";
		return(CMD_ERROR);
	}

	/*
	 * If we were given arguments, discard any previous.
	 * XXX should we merge arguments?  Hard to DWIM.
	 */
	if (argc > 1) {
		if (fp->f_args != NULL)
			free(fp->f_args);
		fp->f_args = unargv(argc - 1, argv + 1);
	}

	/* Hook for platform-specific autoloading of modules */
	if (archsw.arch_autoload() != 0)
		return(CMD_ERROR);

#ifdef LOADER_VERIEXEC
	verify_pcr_export();		/* for measured boot */
#ifdef LOADER_VERIEXEC_PASS_MANIFEST
	pass_manifest_export_envs();
#endif
#endif

	/* Call the exec handler from the loader matching the kernel */
	file_formats[fp->f_loader]->l_exec(fp);
	return(CMD_ERROR);
}


/*
 * Autoboot after a delay
 */

COMMAND_SET(autoboot, "autoboot", "boot automatically after a delay", command_autoboot);

static int
command_autoboot(int argc, char *argv[])
{
	int		howlong;
	char	*cp, *prompt;

	prompt = NULL;
	howlong = -1;
	switch(argc) {
	case 3:
		prompt = argv[2];
		/* FALLTHROUGH */
	case 2:
		howlong = strtol(argv[1], &cp, 0);
		if (*cp != 0) {
			snprintf(command_errbuf, sizeof(command_errbuf),
			    "bad delay '%s'", argv[1]);
			return(CMD_ERROR);
		}
		/* FALLTHROUGH */
	case 1:
		return(autoboot(howlong, prompt));
	}

	command_errmsg = "too many arguments";
	return(CMD_ERROR);
}

/*
 * Called before we go interactive.  If we think we can autoboot, and
 * we haven't tried already, try now.
 */
void
autoboot_maybe()
{
	char	*cp;

	cp = getenv("autoboot_delay");
	if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
		autoboot(-1, NULL);		/* try to boot automatically */
}

static int
autoboot(int timeout, char *prompt)
{
	time_t	when, otime, ntime;
	int		c, yes;
	char	*argv[2], *cp, *ep;
	char	*kernelname;
#ifdef BOOT_PROMPT_123
	const char	*seq = "123", *p = seq;
#endif

	autoboot_tried = 1;

	if (timeout == -1) {
		timeout = 10;
		/* try to get a delay from the environment */
		if ((cp = getenv("autoboot_delay"))) {
			timeout = strtol(cp, &ep, 0);
			if (cp == ep)
				timeout = 10;		/* Unparseable? Set default! */
		}
	}

	kernelname = getenv("kernelname");
	if (kernelname == NULL) {
		argv[0] = NULL;
		loadakernel(0, 0, argv);
		kernelname = getenv("kernelname");
		if (kernelname == NULL) {
			command_errmsg = "no valid kernel found";
			return(CMD_ERROR);
		}
	}

	if (timeout >= 0) {
		otime = time(NULL);
		when = otime + timeout;	/* when to boot */

		yes = 0;

#ifdef BOOT_PROMPT_123
		printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or "
		    "1 2 3 sequence for command prompt." : prompt);
#else
		printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
#endif

		for (;;) {
			if (ischar()) {
				c = getchar();
#ifdef BOOT_PROMPT_123
				if ((c == '\r') || (c == '\n')) {
					yes = 1;
					break;
				} else if (c != *p++)
					p = seq;
				if (*p == 0)
					break;
#else
				if ((c == '\r') || (c == '\n'))
					yes = 1;
				break;
#endif
			}
			ntime = time(NULL);
			if (ntime >= when) {
				yes = 1;
				break;
			}

			if (ntime != otime) {
				printf("\rBooting [%s] in %d second%s... ",
				    kernelname, (int)(when - ntime),
				    (when-ntime)==1?"":"s");
				otime = ntime;
			}
		}
	} else {
		yes = 1;
	}

	if (yes)
		printf("\rBooting [%s]...               ", kernelname);
	putchar('\n');
	if (yes) {
		argv[0] = "boot";
		argv[1] = NULL;
		return(command_boot(1, argv));
	}
	return(CMD_OK);
}

/*
 * Scrounge for the name of the (try)'th file we will try to boot.
 */
static char *
getbootfile(int try)
{
	static char *name = NULL;
	const char	*spec, *ep;
	size_t	len;

	/* we use dynamic storage */
	if (name != NULL) {
		free(name);
		name = NULL;
	}

	/*
	 * Try $bootfile, then try our builtin default
	 */
	if ((spec = getenv("bootfile")) == NULL)
		spec = default_bootfiles;

	while ((try > 0) && (spec != NULL)) {
		spec = strchr(spec, ';');
		if (spec)
			spec++;	/* skip over the leading ';' */
		try--;
	}
	if (spec != NULL) {
		if ((ep = strchr(spec, ';')) != NULL) {
			len = ep - spec;
		} else {
			len = strlen(spec);
		}
		name = malloc(len + 1);
		strncpy(name, spec, len);
		name[len] = 0;
	}
	if (name && name[0] == 0) {
		free(name);
		name = NULL;
	}
	return(name);
}

/*
 * Try to find the /etc/fstab file on the filesystem (rootdev),
 * which should be be the root filesystem, and parse it to find
 * out what the kernel ought to think the root filesystem is.
 *
 * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
 * so that the kernel can tell both which VFS and which node to use
 * to mount the device.  If this variable's already set, don't
 * overwrite it.
 */
int
getrootmount(char *rootdev)
{
	char	lbuf[128], *cp, *ep, *dev, *fstyp, *options;
	int		fd, error;

	if (getenv("vfs.root.mountfrom") != NULL)
		return(0);

	error = 1;
	sprintf(lbuf, "%s/etc/fstab", rootdev);
	if ((fd = open(lbuf, O_RDONLY)) < 0)
		goto notfound;

	/* loop reading lines from /etc/fstab    What was that about sscanf again? */
	fstyp = NULL;
	dev = NULL;
	while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
		if ((lbuf[0] == 0) || (lbuf[0] == '#'))
			continue;

		/* skip device name */
		for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
			;
		if (*cp == 0)		/* misformatted */
			continue;
		/* delimit and save */
		*cp++ = 0;
		free(dev);
		dev = strdup(lbuf);

		/* skip whitespace up to mountpoint */
		while ((*cp != 0) && isspace(*cp))
			cp++;
		/* must have /<space> to be root */
		if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
			continue;
		/* skip whitespace up to fstype */
		cp += 2;
		while ((*cp != 0) && isspace(*cp))
			cp++;
		if (*cp == 0)		/* misformatted */
			continue;
		/* skip text to end of fstype and delimit */
		ep = cp;
		while ((*cp != 0) && !isspace(*cp))
			cp++;
		*cp = 0;
		free(fstyp);
		fstyp = strdup(ep);

		/* skip whitespace up to mount options */
		cp += 1;
		while ((*cp != 0) && isspace(*cp))
			cp++;
		if (*cp == 0)           /* misformatted */
			continue;
		/* skip text to end of mount options and delimit */
		ep = cp;
		while ((*cp != 0) && !isspace(*cp))
			cp++;
		*cp = 0;
		options = strdup(ep);
		/* Build the <fstype>:<device> and save it in vfs.root.mountfrom */
		sprintf(lbuf, "%s:%s", fstyp, dev);
		setenv("vfs.root.mountfrom", lbuf, 0);

		/* Don't override vfs.root.mountfrom.options if it is already set */
		if (getenv("vfs.root.mountfrom.options") == NULL) {
			/* save mount options */
			setenv("vfs.root.mountfrom.options", options, 0);
		}
		free(options);
		error = 0;
		break;
	}
	close(fd);
	free(dev);
	free(fstyp);

notfound:
	if (error) {
		const char *currdev;

		currdev = getenv("currdev");
		if (currdev != NULL && strncmp("zfs:", currdev, 4) == 0) {
			cp = strdup(currdev);
			cp[strlen(cp) - 1] = '\0';
			setenv("vfs.root.mountfrom", cp, 0);
			error = 0;
			free(cp);
		}
	}

	return(error);
}

static int
loadakernel(int try, int argc, char* argv[])
{
	char *cp;

	for (try = 0; (cp = getbootfile(try)) != NULL; try++)
		if (mod_loadkld(cp, argc - 1, argv + 1) != 0)
			printf("can't load '%s'\n", cp);
		else
			return 1;
	return 0;
}