Imported Debian patch 3.6-3
[debian/elilo] / alternate.c
1 /*
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *
5  * This file is part of the ELILO, the EFI Linux boot loader.
6  *
7  *  ELILO is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  ELILO is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with ELILO; see the file COPYING.  If not, write to the Free
19  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  *  02111-1307, USA.
21  *
22  * Please check out the elilo.txt for complete documentation on how
23  * to use this program.
24  */
25
26 #include <efi.h>
27 #include <efilib.h>
28
29 #include "elilo.h"
30
31 #define ELILO_ALTK_VAR  L"EliloAlt"
32
33 /*
34  * we use a NULL GUID for now because it is more convenient
35  */
36 static EFI_GUID altk_guid={0,};
37
38 /*
39  * Check for the existence of an EFI variable named EliloAlt.
40  * It contains the name of an alternate kernel (and possible options) 
41  * to boot ONLY once. 
42  * The variable is then deleted. This ensures that next reboot won't
43  * pick it up. 
44  *
45  * The content of the variable is assumed to be Unicode string. It is
46  * preferrably NULL terminated but the code can deal with this as well.
47  *
48  * The size of the buffer MUST be expressed in bytes (not CHAR16).
49  *
50  * Return:
51  *      - 0 if successful
52  *      - -1 in case nothing happened (no variable found)
53  *        Please note that no fatal error is reported by this function
54  */
55 INTN
56 alternate_kernel(CHAR16 *buffer, INTN size)
57 {
58         EFI_STATUS status;
59         INTN ret = -1;
60
61         /*
62          * size of the buffer is expressed in bytes
63          *
64          * we reserve one Unicode for CHAR_NULL (forced), so
65          * the buffer must be at least 2 more bytes to accomodate one Unicode
66          */
67         if (size < 4) return -1;
68
69         /*
70          * reserve for CHAR_NULL
71          */
72         size-=2;
73
74         /*
75          * Look if the variable exists. 
76          * We may fail because:
77          *      - the variable does not exist
78          *      - our buffer size is too small.
79          */
80         status = RT->GetVariable(ELILO_ALTK_VAR, &altk_guid, NULL, &size, buffer);
81         if (EFI_ERROR(status)) {
82                 DBG_PRT((L"cannot access variable %s: %r", ELILO_ALTK_VAR, status));
83
84                 /* if buffer is too small, erase variable because it's unuseable */
85                 if (status == EFI_BUFFER_TOO_SMALL) goto delete_var;
86
87                 return -1;
88         }
89
90         /*
91          * sanity check
92          *
93          * verify that size (expressed in bytes) is multiple of 2. If 
94          * not then it can't possibly be representing a UNICODE string
95          * (2bytes/character).
96          *
97          * We also consider empty as useless (2nd test).
98          */
99         if (size & 0x1) {
100                 Print(L"invalid content for %s variable, erasing variable\n", ELILO_ALTK_VAR);
101                 goto delete_var;
102         }
103
104         /* We also consider empty as useless (2nd test) */
105         if (size == 2) goto delete_var;
106
107         buffer[size] = CHAR_NULL;
108
109         VERB_PRT(2, Print(L"found alternate variable %s : %s\n", ELILO_ALTK_VAR, buffer));
110
111         ret = 0;
112 delete_var:
113         status = RT->SetVariable(ELILO_ALTK_VAR, &altk_guid, 0, 0, NULL);
114         if (EFI_ERROR(status)) {
115                 ERR_PRT((L"cannot erase variable %s", ELILO_ALTK_VAR));
116         }
117         return ret;
118 }