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