File: env_api.c

package info (click to toggle)
efibootguard 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 796 kB
  • sloc: ansic: 7,805; makefile: 405; python: 326; sh: 84
file content (332 lines) | stat: -rw-r--r-- 6,705 bytes parent folder | download
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
/*
 * EFI Boot Guard
 *
 * Copyright (c) Siemens AG, 2017-2023
 *
 * Authors:
 *  Andreas Reichel <andreas.reichel.ext@siemens.com>
 *  Felix Moessbauer <felix.moessbauer@siemens.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 * SPDX-License-Identifier:	GPL-2.0-only
 */

#include "env_api.h"
#include "ebgenv.h"
#include "uservars.h"

/* global EBG options */
ebgenv_opts_t ebgenv_opts;

/* UEFI uses 16-bit wide unicode strings.
 * However, wchar_t support functions are fixed to 32-bit wide
 * characters in glibc. This code is compiled with
 *  -fshort-wchar
 * which enables 16-bit wide wchar_t support. However,
 * glibc functions do not work with 16-bit wchar_t input, except
 * it was specifically compiled for that, which is unusual.
 * Thus, the needed conversion by truncation function is
 * reimplemented here.
 */
char *str16to8(char *buffer, const char16_t *src)
{
	if (!src || !buffer) {
		return NULL;
	}
	char *tmp = buffer;
	while (*src) {
		*buffer = (char)*src;
		src++;
		buffer++;
	}
	*buffer = 0;
	return tmp;
}

char16_t *str8to16(char16_t *buffer, const char *src)
{
	if (!src || !buffer) {
		return NULL;
	}
	char16_t *tmp = buffer;
	while (*src) {
		*buffer = (char16_t)*src;
		src++;
		buffer++;
	}
	*buffer = 0;
	return tmp;
}

int ebg_set_opt_bool(ebg_opt_t opt, bool value)
{
	switch (opt) {
	case EBG_OPT_PROBE_ALL_DEVICES:
		ebgenv_opts.search_all_devices = value;
		break;
	case EBG_OPT_VERBOSE:
		ebgenv_opts.verbose = value;
		bgenv_be_verbose(value);
		break;
	default:
		return EINVAL;
	}
	return 0;
}

int ebg_get_opt_bool(ebg_opt_t opt, bool *value)
{
	switch (opt) {
	case EBG_OPT_PROBE_ALL_DEVICES:
		*value = ebgenv_opts.search_all_devices;
		break;
	case EBG_OPT_VERBOSE:
		*value = ebgenv_opts.verbose;
		break;
	default:
		return EINVAL;
	}
	return 0;
}

void ebg_beverbose(ebgenv_t __attribute__((unused)) * e, bool v)
{
	ebg_set_opt_bool(EBG_OPT_VERBOSE, v);
}

int ebg_env_create_new(ebgenv_t *e)
{
	if (!bgenv_init()) {
		return EIO;
	}

	BGENV *latest_env = bgenv_open_latest();
	if (!latest_env) {
		return EIO;
	}

	const BG_ENVDATA *latest_data = ((BGENV *)latest_env)->data;

	if (latest_data->in_progress != 1) {
		e->bgenv = (void *)bgenv_create_new();
		if (!e->bgenv) {
			bgenv_close(latest_env);
			return errno;
		}
		BG_ENVDATA *new_data = ((BGENV *)e->bgenv)->data;
		uint32_t new_rev = new_data->revision;
		uint8_t new_in_progress = new_data->in_progress;
		memcpy(new_data, latest_env->data, sizeof(BG_ENVDATA));
		new_data->revision = new_rev;
		new_data->in_progress = new_in_progress;
		bgenv_close(latest_env);
	} else {
		e->bgenv = latest_env;
	}

	return 0;
}

int ebg_env_open_current(ebgenv_t *e)
{
	if (!bgenv_init()) {
		return EIO;
	}

	e->bgenv = (void *)bgenv_open_latest();

	return e->bgenv == NULL ? EIO : 0;
}

int ebg_env_get(ebgenv_t *e, const char *key, char *buffer)
{
	return bgenv_get((BGENV *)e->bgenv, key, NULL, buffer,
			 ENV_STRING_LENGTH);
}

int ebg_env_get_ex(ebgenv_t *e, const char *key, uint64_t *usertype,
		   uint8_t *buffer, uint32_t maxlen)
{
	return bgenv_get((BGENV *)e->bgenv, key, usertype, buffer, maxlen);
}

int ebg_env_set(ebgenv_t *e, const char *key, const char *value)
{
	return bgenv_set((BGENV *)e->bgenv, key, USERVAR_TYPE_DEFAULT |
			 USERVAR_TYPE_STRING_ASCII, value,
			 strlen(value) + 1);
}

int ebg_env_set_ex(ebgenv_t *e, const char *key, uint64_t usertype,
		   const uint8_t *value, uint32_t datalen)
{
	return bgenv_set((BGENV *)e->bgenv, key, usertype, value, datalen);
}

uint32_t ebg_env_user_free(ebgenv_t *e)
{
	if (!e->bgenv) {
		return 0;
	}
	if (!((BGENV *)e->bgenv)->data) {
		return 0;
	}
	return bgenv_user_free(((BGENV *)e->bgenv)->data->userdata);
}

uint16_t ebg_env_getglobalstate(void __attribute__((unused)) *reserved)
{
	BGENV *env;
	int res = USTATE_UNKNOWN;

	if (!bgenv_init()) {
		errno = EIO;
		return res;
	}

	/* Test for rolled-back condition. */
	for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) {
		env = bgenv_open_by_index(i);

		if (!env) {
			continue;
		}
		/* update was unsuccessful if there is a config,
		 * with revision == REVISION_FAILED and
		 * with ustate == USTATE_FAILED */
		if (env->data->revision == REVISION_FAILED &&
		    env->data->ustate == USTATE_FAILED) {
			res = USTATE_FAILED;
		}
		bgenv_close(env);
		if (res == USTATE_FAILED) {
			return res;
		}
	}

	env = bgenv_open_latest();
	if (!env) {
		errno = EIO;
		return res;
	}

	res = env->data->ustate;
	bgenv_close(env);

	return res;
}

int ebg_env_setglobalstate(ebgenv_t *e, uint16_t ustate)
{
	char buffer[2];
	int res;

	if (ustate > USTATE_FAILED) {
		return -EINVAL;
	}
	(void)snprintf(buffer, sizeof(buffer), "%d", ustate);
	res = bgenv_set((BGENV *)e->bgenv, "ustate", 0, buffer,
			strlen(buffer) + 1);

	if (ustate != USTATE_OK) {
		return res;
	}

	for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) {
		BGENV *env = bgenv_open_by_index(i);

		if (!env) {
			continue;
		}
		if (env->data->ustate != ustate) {
			env->data->ustate = ustate;
			env->data->crc32 = bgenv_crc32(0, env->data,
				sizeof(BG_ENVDATA) - sizeof(env->data->crc32));
			if (!bgenv_write(env)) {
				bgenv_close(env);
				return -EIO;
			}
		}
		bgenv_close(env);
	}
	return 0;
}

int ebg_env_close(ebgenv_t *e)
{
	int res = 0;

	/* if no environment is open, just return EIO */
	if (!e->bgenv) {
		return EIO;
	}

	BGENV *env_current;
	env_current = (BGENV *)e->bgenv;

	/* recalculate checksum */
	env_current->data->crc32 =
	    bgenv_crc32(0, env_current->data,
			sizeof(BG_ENVDATA) - sizeof(env_current->data->crc32));
	/* save */
	if (!bgenv_write(env_current)) {
		res = EIO;
	}
	bgenv_close(env_current);
	e->bgenv = NULL;
	bgenv_finalize();
	return res;
}

int ebg_env_register_gc_var(ebgenv_t *e, char *key)
{
	GC_ITEM **pgci;
	pgci = (GC_ITEM **)&e->gc_registry;

	if (!key) {
		return EINVAL;
	}
	while (*pgci) {
		pgci = &((*pgci)->next);
	}
	*pgci = (GC_ITEM *)calloc(1, sizeof(GC_ITEM));
	if (!*pgci) {
		return ENOMEM;
	}
	if (asprintf(&((*pgci)->key), "%s", key) == -1) {
		free(*pgci);
		*pgci = NULL;
		return ENOMEM;
	}
	return 0;
}

int ebg_env_finalize_update(ebgenv_t *e)
{
	if (!e->bgenv || !((BGENV *)e->bgenv)->data) {
		return EIO;
	}

	GC_ITEM *pgci, *tmp;
	uint8_t *udata;

	pgci = (GC_ITEM *)e->gc_registry;
	udata = ((BGENV *)e->bgenv)->data->userdata;
	while (pgci) {
		uint8_t *var;
		var = bgenv_find_uservar(udata, pgci->key);
		if (var) {
			bgenv_del_uservar(udata, var);
		}
		free(pgci->key);
		tmp = pgci->next;
		free(pgci);
		pgci = tmp;
	}

	((BGENV *)e->bgenv)->data->in_progress = 0;
	((BGENV *)e->bgenv)->data->ustate = USTATE_INSTALLED;
	return 0;
}