summaryrefslogtreecommitdiffstats
path: root/common/bootargs.c
blob: c49136609f487638c606be4db0cb2c32ee311b21 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * bootargs.c - concatenate Linux bootargs
 *
 * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 */
#include <common.h>
#include <boot.h>
#include <malloc.h>
#include <magicvar.h>
#include <globalvar.h>
#include <environment.h>

static char *linux_bootargs;
static int linux_bootargs_overwritten;

/*
 * This returns the Linux bootargs
 *
 * There are two ways to handle bootargs. The old legacy way is to use the
 * 'bootargs' environment variable. The new and more flexible way is to use
 * global variables beginning with "global.linux.bootargs." and
 * "global.linux.mtdparts.". These variables will be concatenated together to
 * the resulting bootargs. If there are no "global.linux.bootargs." variables
 * we fall back to "bootargs"
 */
const char *linux_bootargs_get(void)
{
	char *bootargs, *parts;

	if (linux_bootargs_overwritten)
		return linux_bootargs;

	free(linux_bootargs);

	bootargs = globalvar_get_match("linux.bootargs.", " ");
	if (!strlen(bootargs))
		return getenv("bootargs");

	linux_bootargs = bootargs;

	parts = globalvar_get_match("linux.mtdparts.", ";");
	if (strlen(parts)) {
		bootargs = basprintf("%s mtdparts=%s", linux_bootargs,
				       parts);
		free(linux_bootargs);
		linux_bootargs = bootargs;
	}

	free(parts);

	parts = globalvar_get_match("linux.blkdevparts.", ";");
	if (strlen(parts)) {
		bootargs = basprintf("%s blkdevparts=%s", linux_bootargs,
				       parts);
		free(linux_bootargs);
		linux_bootargs = bootargs;
	}

	free(parts);

	return linux_bootargs;
}

int linux_bootargs_overwrite(const char *bootargs)
{
	if (bootargs) {
		free(linux_bootargs);
		linux_bootargs = xstrdup(bootargs);
		linux_bootargs_overwritten = 1;
	} else {
		linux_bootargs_overwritten = 0;
	}

	return 0;
}

BAREBOX_MAGICVAR(global.linux.bootargs.*, "Linux bootargs variables");
BAREBOX_MAGICVAR(global.linux.mtdparts.*, "Linux mtdparts variables");
BAREBOX_MAGICVAR(global.linux.blkdevparts.*, "Linux blkdevparts variables");