summaryrefslogtreecommitdiffstats
path: root/drivers/of/barebox.c
blob: 64f483326d611f7687c7a7b9b47e8dd87ac1efed (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
/*
 * barebox.c
 *
 * Copyright (c) 2013 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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 <common.h>
#include <init.h>
#include <io.h>
#include <of.h>
#include <malloc.h>
#include <partition.h>
#include <envfs.h>
#include <fs.h>

#define ENV_MNT_DIR "/boot"	/* If env on filesystem, where to mount */

/* If dev describes a file on a fs, mount the fs and change devpath to
 * point to the file's path.  Otherwise leave devpath alone.  Does
 * nothing in env in a file support isn't enabled.  */
static int environment_check_mount(struct device_d *dev, char **devpath)
{
	const char *filepath;
	int ret;

	if (!IS_ENABLED(CONFIG_OF_BAREBOX_ENV_IN_FS))
		return 0;

	ret = of_property_read_string(dev->device_node, "file-path", &filepath);
	if (ret == -EINVAL) {
		/* No file-path so just use device-path */
		return 0;
	} else if (ret) {
		/* file-path property exists, but has error */
		dev_err(dev, "Problem with file-path property\n");
		return ret;
	}

	/* Get device env is on and mount it */
	mkdir(ENV_MNT_DIR, 0777);
	ret = mount(*devpath, "fat", ENV_MNT_DIR, NULL);
	if (ret) {
		dev_err(dev, "Failed to load environment: mount %s failed (%d)\n",
			*devpath, ret);
		return ret;
	}

	/* Set env to be in a file on the now mounted device */
	dev_dbg(dev, "Loading default env from %s on device %s\n",
		filepath, *devpath);
	*devpath = basprintf("%s/%s", ENV_MNT_DIR, filepath);
	return 0;
}

static int environment_probe(struct device_d *dev)
{
	char *path;
	int ret;

	ret = of_find_path(dev->device_node, "device-path", &path, OF_FIND_PATH_FLAGS_BB);
	if (ret)
		return ret;

	/* Do we need to mount a fs and find env there? */
	ret = environment_check_mount(dev, &path);
	if (ret)
		return ret;

	dev_dbg(dev, "Setting default environment path to %s\n", path);
	default_environment_path_set(path);

	return 0;
}

static struct of_device_id environment_dt_ids[] = {
	{
		.compatible = "barebox,environment",
	}, {
		/* sentinel */
	}
};

static struct driver_d environment_driver = {
	.name		= "barebox-environment",
	.probe		= environment_probe,
	.of_compatible	= environment_dt_ids,
};

static int barebox_of_driver_init(void)
{
	struct device_node *node;

	node = of_get_root_node();
	if (!node)
		return 0;

	node = of_find_node_by_path("/chosen");
	if (!node)
		return 0;

	of_platform_populate(node, of_default_bus_match_table, NULL);

	platform_driver_register(&environment_driver);

	return 0;
}
late_initcall(barebox_of_driver_init);