summaryrefslogtreecommitdiffstats
path: root/discover/grub2/env.c
blob: 3598927e0f295c5656e2fa93ee3192d32a18b92e (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
259
260
261
262
263
264
265
266
267
268
269
270
271

#include <stdio.h>
#include <string.h>

#include <log/log.h>
#include <file/file.h>
#include <types/types.h>
#include <talloc/talloc.h>
#include <util/util.h>

#include <discover/parser.h>

#include "grub2.h"

static const char *default_envfile = "grubenv";
static const char *signature = "# GRUB Environment Block\n";

static int parse_buf_to_env(struct grub2_script *script, void *buf, int len)
{
	char *tmp = NULL, *line, *sep;
	int siglen;

	siglen = strlen(signature);

	if (len < siglen) {
		pb_log("grub environment block too small\n");
		return -1;
	}

	if (memcmp(buf, signature, siglen)) {
		pb_log("grub environment block has invalid signature\n");
		return -1;
	}

	buf += siglen;

	for (line = strtok_r(buf, "\n", &tmp); line;
				line = strtok_r(NULL, "\n", &tmp)) {

		if (*line == '#')
			continue;

		sep = strchr(line, '=');
		if (!sep)
			continue;
		if (sep == line)
			continue;

		*sep = '\0';
		script_env_set(script, line, sep + 1);
	}

	return 0;
}

int builtin_load_env(struct grub2_script *script,
		void *data __attribute__((unused)),
		int argc, char *argv[]);

int builtin_load_env(struct grub2_script *script,
		void *data __attribute__((unused)),
		int argc, char *argv[])
{
	struct discover_device *dev = script->ctx->device;
	const char *envfile;
	char *buf, *envpath;
	int rc, len;

	/* we only support local filesystems */
	if (!dev->mounted) {
		pb_log("load_env: can't load from a non-mounted device (%s)\n",
				dev->device->id);
		return -1;
	}

	if (argc == 3 && !strcmp(argv[1], "-f"))
		envfile = argv[2];
	else
		envfile = default_envfile;

	envpath = talloc_asprintf(script, "%s/%s",
				script_env_get(script, "prefix") ? : "",
				envfile);

	rc = parser_request_file(script->ctx, dev, envpath, &buf, &len);

	if (!rc) {
		rc = parse_buf_to_env(script, buf, len);
		talloc_free(buf);
	}

	return 0;
}

static int update_env(char *buf, int buflen, const char *name,
		const char *value)
{
	/* head and tail define the pointers within the existing data that we
	 * can insert our env entry, end is the last byte of valid environment
	 * data. if tail - head != entrylen, when we'll memmove to create (or
	 * remove) space */
	int i, j, linelen, namelen, valuelen, entrylen, delta, space;
	char *head, *tail, *end;
	bool replace;

	namelen = strlen(name);
	valuelen = strlen(value);
	entrylen = namelen + strlen("=") + valuelen + strlen("\n");

	head = tail = end = NULL;
	replace = false;

	/* For each line (where linelen includes the trailing \n). Find
	 * head, tail and end.
	 */
	for (i = 0; i < buflen; i += linelen) {
		char *eol = NULL, *sep = NULL, *line = buf + i;

		/* find eol and sep */
		for (j = 0; !eol && i + j < buflen; j++) {
			switch (line[j]) {
			case '\n':
				eol = line + j;
				break;
			case '=':
				if (!sep)
					sep = line + j;
				break;
			}
		}

		/* no eol, put the new value at the start of this line,
		 * no need to shift data. This will also match the
		 * padding '#' chars at the end of the entries. */
		if (!eol) {
			if (!head) {
				head = line;
				tail = head + entrylen;
			}
			end = line;
			break;
		}

		linelen = (eol - line) + 1;
		end = line + linelen;

		/* invalid line? may as well overwrite it with valid data */
		if (!sep && !replace) {
			head = line;
			tail = line + linelen;
		}

		/* an existing entry for this var? We set replace, as we prefer
		 * to overwrite an existing entry (the first one, in
		 * particular) rather than use an invalid line.
		 */
		if (sep && !replace && sep - line == namelen &&
				!strncmp(name, line, namelen)) {
			head = line;
			tail = line + linelen;
			replace = true;
		}
	}

	if (!head || !tail || !end) {
		pb_log("grub save_env: can't parse buffer space\n");
		return -1;
	}

	/* how much extra space do we need? */
	delta = entrylen - (tail - head);
	/* how much space do we have? */
	space = (buf + buflen) - end;

	/* check we have enough space. the tail > buf-end check is required
	 * for the case where there was no eol, and we set
	 * tail = head + entrylen
	 */
	if (delta > space || tail > buf + buflen) {
		pb_log("grub save_env: not enough buffer space\n");
		return -1;
	}

	/* create space between head & tail */
	if (delta) {
		/* for positive delta, we need to reduce the copied data size */
		int shiftlen = delta > 0 ? delta : 0;
		memmove(tail + delta, tail, (buf + buflen) - tail - shiftlen);
	}

	/* if we've shifted data down towards head, we'll need to append
	 * padding */
	if (delta < 0)
		memset(buf + buflen + delta, '#', 0 - delta);

	/* set the entry data */
	memcpy(head, name, namelen);
	memcpy(head + namelen, "=", 1);
	memcpy(head + namelen + 1, value, valuelen);
	memcpy(head + namelen + 1 + valuelen, "\n", 1);

	return 0;
}

int builtin_save_env(struct grub2_script *script,
		void *data __attribute__((unused)),
		int argc, char *argv[]);

int builtin_save_env(struct grub2_script *script,
		void *data __attribute__((unused)),
		int argc, char *argv[])
{
	struct discover_device *dev = script->ctx->device;
	int i, rc, len, siglen;
	char *buf, *envpath;
	const char *envfile;
	bool using_dash_f = false;

	/* we only support local filesystems */
	if (!dev->mounted) {
		pb_log("save_env: can't save to a non-mounted device (%s)\n",
				dev->device->id);
		return -1;
	}

	if (argc >= 2 && !strcmp(argv[1], "-f")) {
		if (argc < 3) {
			pb_log("save_env: for -f, need argument\n");
			return -1;
		}

		envfile = argv[2];
		using_dash_f = true;
	} else
		envfile = default_envfile;

	envpath = talloc_asprintf(script, "%s/%s",
				script_env_get(script, "prefix") ? : "",
				envfile);
	buf = NULL;

	rc = parser_request_file(script->ctx, dev, envpath, &buf, &len);

	siglen = strlen(signature);

	/* we require the environment to be pre-allocated, so abort if
	 * the file isn't present and valid */
	if (rc || len < siglen || memcmp(buf, signature, siglen))
		goto err;

	/* For "-f", skip the "-f <file>" arguments in picking the
	 * variables to save. */
	i = (using_dash_f ? 3 : 1);

	for (; i < argc; i++) {
		const char *name, *value;

		name = argv[i];
		value = script_env_get(script, name);

		update_env(buf + siglen, len - siglen, name, value);
	}

	rc = parser_replace_file(script->ctx, dev, envpath, buf, len);

err:
	talloc_free(buf);
	talloc_free(envpath);
	return rc;
}

OpenPOWER on IntegriCloud