File: eliloalt.c

package info (click to toggle)
elilo 3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,356 kB
  • ctags: 2,507
  • sloc: ansic: 9,556; sh: 639; asm: 532; makefile: 196
file content (286 lines) | stat: -rw-r--r-- 6,455 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
/*
 * eliloalt.c
 *
 * Copyright (C) 2002 Hewlett-Packard Co
 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
 *
 * This file is part of the ELILO, the EFI Linux boot loader.
 *
 *  ELILO 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, or (at your option)
 *  any later version.
 *
 *  ELILO 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ELILO; see the file COPYING.  If not, write to the Free
 *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 */

/*
 * This program is used to set the EliloAlt EFI variable to influence
 * how elilo will behave at the next reboot. This variable is used
 * to boot a certain kernel/configuration only once (debug, for instance).
 *
 * This is is supposed to be installed in /usr/sbin/eliloalt and must only
 * be run by root.
 */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>


#define ELILOALT_VERSION	"0.01"

#define ELILO_ALT_NAME	"EliloAlt"
#define EFIVAR_DIR	"/proc/efi/vars"
#define ELILO_ALTVAR	EFIVAR_DIR"/"ELILO_ALT_NAME"-00000000-0000-0000-0000-000000000000"

/*
 * to write/create a variable we need to open for writing ANY existing variable.
 * So here we chose one that does seem to always exist
 */
#define ELILOALT_WRITE_VICTIM EFIVAR_DIR"/Efi-47c7b226-c42a-11d2-8e57-00a0c969723b";

#define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
#define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
#define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004

typedef unsigned long efi_status_t;
typedef uint8_t  efi_bool_t;
typedef uint16_t efi_char16_t;		/* UNICODE character */

/*
 * EFI GUID type definition
 */
typedef struct {
	uint32_t data1;
	uint16_t data2;
	uint16_t data3;
	uint8_t  data4[8];
} efi_guid_t;

/*
 * EFI variable structure 
 */
typedef struct _efi_variable_t {
        efi_char16_t	variablename[1024/sizeof(efi_char16_t)];
        efi_guid_t	vendorguid;
        uint64_t	datasize;
        uint8_t         data[1024];
	efi_status_t	status;
        uint32_t        attributes;
} __attribute__((packed)) efi_variable_t;

static char *elilo_alt_name = ELILO_ALT_NAME;

static struct option cmd_options[]={
	{ "version", 0, 0, 1},
	{ "help", 0, 0, 2},
	{ "delete", 0, 0, 3},
	{ "print", 0, 0, 4},
	{ "set", 1, 0, 5},
	{ 0, 0, 0, 0}
};

static void fatal_error(char *fmt,...) __attribute__((noreturn));

static void
fatal_error(char *fmt, ...) 
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	exit(1);
}


static void
usage(char **argv)
{
	printf("Usage: %s [OPTIONS] cmdline\n", argv[0]);

	printf(	"-h, --help\t\tdisplay this help and exit\n"
		"--version\t\toutput version information and exit\n"
		"-s, --set cmdline\tset elilo alternate variable to cmdline\n"
		"-p, --print\t\tprint elilo alternate variable\n"
		"-d, --delete\t\tprint elilo alternate variable\n"
	);
}

static void
check_proc_efi(void)
{
	int fd;

	if (getuid() != 0) {
		fatal_error("This program must be run as root\n");
	}
	fd = open(EFIVAR_DIR, O_RDONLY);
	if (fd == -1) {
		fatal_error("Cannot access %s\n", EFIVAR_DIR);
	}
}

static void
delete_var(void)
{
	efi_variable_t var;
	int fd, r, i;

	check_proc_efi();

	fd = open(ELILO_ALTVAR, O_WRONLY);
	if (fd == -1) {
		fatal_error("variable not defined\n");
	}

	memset(&var, 0, sizeof(var));

	for (i=0; i < sizeof(elilo_alt_name); i++) {
		var.variablename[i] = (efi_char16_t)elilo_alt_name[i];
	}

	/* 
	 * we use NULL GUID so no need to initialize it now memset() did it
	 * writing with a datasize=0 will effectively delete the variable.
	 */
	
	r = write(fd, &var, sizeof(var));
	if (r != sizeof(var)) {
		fatal_error("Variable %s defined but invalid content\n", ELILO_ALTVAR);
	}
	close(fd);
}


static void
print_var(void)
{
	efi_variable_t var;
	int fd, r, i;


	check_proc_efi();

	fd = open(ELILO_ALTVAR, O_RDONLY);
	if (fd == -1) {
		fatal_error("variable not defined\n");
	}

	memset(&var, 0, sizeof(var));

	r = read(fd, &var, sizeof(var));
	if (r != sizeof(var)) {
		fatal_error("Variable %s defined but invalid content\n", ELILO_ALTVAR);
	}
	printf("EliloAlt=\"");
	for(i=0; i < var.datasize; i+=1){
		printf("%c", var.data[i]);
	}
	printf("\"\n");
	close(fd);
}

static void
set_var(char *cmdline)
{
	efi_variable_t var;
	int fd, r, i, j, l;
	char *name = ELILOALT_WRITE_VICTIM;

	check_proc_efi();

	if (cmdline == NULL) {
		fatal_error("invalid cmdline argument\n");
	}

	l = strlen(cmdline);

	if (l >= 1024) {
		fatal_error("Variable content is too long, must be <= 512 characters\n");
	}

	fd = open(name, O_WRONLY);
	if (fd == -1) {
		fatal_error("can't open %s: %s\n", ELILO_ALTVAR, strerror(errno));
	}

	memset(&var, 0, sizeof(var));

	for (i=0; i < sizeof(elilo_alt_name); i++) {
		var.variablename[i] = (efi_char16_t)elilo_alt_name[i];
	}

	for (i=0, j=0; i < l; i++, j+=2) {
		var.data[j] = (efi_char16_t)cmdline[i];
	}
	/* +2 = include char16 for null termination */
	var.datasize   = j+2;

	var.attributes =  EFI_VARIABLE_NON_VOLATILE 
			| EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;

	/* 
	 * we use NULL GUID so no need to initialize it now memset() did it
	 * writing with a datasize=0 will effectively delete the variable.
	 */
	
	r = write(fd, &var, sizeof(var));
	if (r != sizeof(var)) {
		fatal_error("Variable %s defined but invalid content %d\n", ELILO_ALTVAR, r);
	}
	close(fd);

}

int
main(int argc, char **argv)
{
	int c;

	while ((c=getopt_long(argc, argv,"hdps:", cmd_options, 0)) != -1) {
		switch(c) {
			case   0: continue; /* fast path for options */
			case   1:
				printf("Version %s Date: %s\n", ELILOALT_VERSION, __DATE__);
				exit(0);
			case   2:
			case 'h':
				usage(argv);
				exit(0);
			case   3:
			case 'd':
				delete_var();
				exit(0);
			case   4:
			case 'p':
				print_var();
				exit(0);
			case   5:
			case 's':
				set_var(optarg);
				exit(0);
			default:
				fatal_error("Unknown option\n");
		}
	}
	print_var();
	return 0;
}