summaryrefslogtreecommitdiffstats
path: root/defaultenv/defaultenv.c
blob: 3b4efcfd36e35c64621916ff928f33f644a89c33 (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
/*
 * Copyright (C) 2014 Sascha Hauer, 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 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 <common.h>
#include <envfs.h>
#include <filetype.h>
#include <uncompress.h>
#include <malloc.h>
#include <init.h>
#include <asm/unaligned.h>
#include "barebox_default_env.h"

static LIST_HEAD(defaultenv_list);

struct defaultenv {
	struct list_head list;
	const char *name;
	void *buf;
	size_t size;
};

static void defaultenv_add_base(void)
{
	static int base_added;

	if (base_added)
		return;

	base_added = 1;

	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW))
		defaultenv_append_directory(defaultenv_2_base);
	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_MENU))
		defaultenv_append_directory(defaultenv_2_menu);
	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_DFU))
		defaultenv_append_directory(defaultenv_2_dfu);
	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC))
		defaultenv_append_directory(defaultenv_1);
}

static void defaultenv_add_external(void)
{
	static int external_added;

	if (external_added)
		return;

	external_added = 1;

	/*
	 * The traditional or external environment given with
	 * CONFIG_DEFAULT_ENVIRONMENT_PATH.
	 */
	defaultenv_append((void *)default_environment,
			default_environment_size, "defaultenv");
}

/*
 * defaultenv_append - append a envfs buffer to the default environment
 * @buf:	The buffer containing the binary environment. If it is
 *              not a plain buffer it is assumed to be a compressed buffer.
 * @size:	The length of the buffer
 *
 * This adds an overlay to the default environment. New files will be created,
 * existing files will be overwritten with the overlay.
 */
void defaultenv_append(void *buf, unsigned int size, const char *name)
{
	struct defaultenv *df;

	defaultenv_add_base();

	df = xzalloc(sizeof(*df));
	df->buf = buf;
	df->size = size;
	df->name = name;

	list_add_tail(&df->list, &defaultenv_list);
}

static int defaultenv_load_one(struct defaultenv *df, const char *dir,
		unsigned flags)
{
	void *freep = NULL;
	void *buf;
	enum filetype ft = file_detect_type(df->buf, df->size);
	uint32_t size;
	int ret;

	pr_debug("loading %s\n", df->name);

	if (!IS_ENABLED(CONFIG_DEFAULT_COMPRESSION_NONE) &&
			ft != filetype_barebox_env) {
		size = get_unaligned_le32(df->buf + df->size - 4);
		freep = malloc(size);
		if (!freep)
			return -ENOMEM;

		ret = uncompress(df->buf, df->size,
				NULL, NULL,
				freep, NULL, uncompress_err_stdout);
		if (ret) {
			free(freep);
			pr_err("Failed to uncompress: %s\n", strerror(-ret));
			return ret;
		}

		buf = freep;
	} else {
		buf = df->buf;
		size = df->size;
	}

	ret = envfs_load_from_buf(buf, size, dir, flags);

	free(freep);

	if (ret)
		pr_err("Failed to load defaultenv: %s\n", strerror(-ret));

	return ret;
}

/*
 * defaultenv_load - load the default environment
 * @dir:	The directory the default environment should be loaded to.
 *
 * This loads all environment snippets previously registered with
 * defaultenv_append to the directory given with @dir.
 *
 * Return: 0 for success, negative error code otherwise.
 */
int defaultenv_load(const char *dir, unsigned flags)
{
	struct defaultenv *df;
	int ret;

	defaultenv_add_base();

	defaultenv_add_external();

	list_for_each_entry(df, &defaultenv_list, list) {
		ret = defaultenv_load_one(df, dir, flags);
		if (ret)
			return ret;
	}

	return 0;
}