File: alternate.c

package info (click to toggle)
elilo 3.14-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,292 kB
  • sloc: ansic: 13,963; sh: 719; asm: 533; makefile: 263; perl: 44
file content (121 lines) | stat: -rw-r--r-- 3,555 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright (C) 2001-2003 Hewlett-Packard Co.
 *	Contributed by Stephane Eranian <eranian@hpl.hp.com>
 *	Contributed by Fenghua Yu <fenghua.yu@intel.com>
 *	Contributed by Bibo Mao <bibo.mao@intel.com>
 *	Contributed by Chandramouli Narayanan<mouli@linux.intel.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.
 *
 * Please check out the elilo.txt for complete documentation on how
 * to use this program.
 */

#include <efi.h>
#include <efilib.h>

#include "elilo.h"

#define ELILO_ALTK_VAR	L"EliloAlt"

/*
 * we use a NULL GUID for now because it is more convenient
 */
static EFI_GUID altk_guid={0,};

/*
 * Check for the existence of an EFI variable named EliloAlt.
 * It contains the name of an alternate kernel (and possible options) 
 * to boot ONLY once. 
 * The variable is then deleted. This ensures that next reboot won't
 * pick it up. 
 *
 * The content of the variable is assumed to be Unicode string. It is
 * preferrably NULL terminated but the code can deal with this as well.
 *
 * The size of the buffer MUST be expressed in bytes (not CHAR16).
 *
 * Return:
 *	- 0 if successful
 *	- -1 in case nothing happened (no variable found)
 *	  Please note that no fatal error is reported by this function
 */
INTN
alternate_kernel(CHAR16 *buffer, UINTN size)
{
	EFI_STATUS status;
	INTN ret = -1;

	/*
	 * size of the buffer is expressed in bytes
	 *
	 * we reserve one Unicode for CHAR_NULL (forced), so
	 * the buffer must be at least 2 more bytes to accomodate one Unicode
	 */
	if (size < 4) return -1;

	/*
	 * reserve for CHAR_NULL
	 */
	size-=2;

	/*
	 * Look if the variable exists. 
	 * We may fail because:
	 * 	- the variable does not exist
	 * 	- our buffer size is too small.
	 */
	status = uefi_call_wrapper(RT->GetVariable, 5, ELILO_ALTK_VAR, &altk_guid, NULL, &size, buffer);
	if (EFI_ERROR(status)) {
		DBG_PRT((L"cannot access variable %s: %r", ELILO_ALTK_VAR, status));

		/* if buffer is too small, erase variable because it's unuseable */
		if (status == EFI_BUFFER_TOO_SMALL) goto delete_var;

		return -1;
	}

	/*
	 * sanity check
	 *
	 * verify that size (expressed in bytes) is multiple of 2. If 
	 * not then it can't possibly be representing a UNICODE string
	 * (2bytes/character).
	 *
	 * We also consider empty as useless (2nd test).
	 */
	if (size & 0x1) {
		Print(L"invalid content for %s variable, erasing variable\n", ELILO_ALTK_VAR);
		goto delete_var;
	}

	/* We also consider empty as useless (2nd test) */
	if (size == 2) goto delete_var;

	buffer[size] = CHAR_NULL;

	VERB_PRT(2, Print(L"found alternate variable %s : %s\n", ELILO_ALTK_VAR, buffer));

	ret = 0;
delete_var:
	status = uefi_call_wrapper(RT->SetVariable, 5, ELILO_ALTK_VAR, &altk_guid, 0, 0, NULL);
	if (EFI_ERROR(status)) {
		ERR_PRT((L"cannot erase variable %s", ELILO_ALTK_VAR));
	}
	return ret;
}