Imported Debian patch 0.4.9-0.sarge.2
[debian/efibootmgr] / src / lib / efivars_procfs.c
1 /*
2   efivars_procfs.[ch] - Manipulates EFI variables as exported in /proc/efi/vars
3
4   Copyright (C) 2001,2003 Dell Computer Corporation <Matt_Domsch@dell.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define _FILE_OFFSET_BITS 64
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33
34 #include "efi.h"
35 #include "efichar.h"
36 #include "efibootmgr.h"
37 #include "efivars_procfs.h"
38
39 static efi_status_t
40 procfs_read_variable(const char *name, efi_variable_t *var)
41 {
42         char filename[PATH_MAX];
43         int fd;
44         size_t readsize;
45         if (!name || !var) return EFI_INVALID_PARAMETER;
46
47         snprintf(filename, PATH_MAX-1, "%s/%s", PROCFS_DIR_EFI_VARS,name);
48         fd = open(filename, O_RDONLY);
49         if (fd == -1) {
50                 return EFI_NOT_FOUND;
51         }
52         readsize = read(fd, var, sizeof(*var));
53         if (readsize != sizeof(*var)) {
54                 close(fd);
55                 return EFI_INVALID_PARAMETER;
56         }
57         close(fd);
58         return var->Status;
59 }
60
61 /**
62  * select_variable_names()
63  * @d - dirent to compare against
64  *
65  * This ignores "." and ".." entries, and selects all others.
66  */
67
68 static int
69 select_variable_names(const struct dirent *d)
70 {
71         if (!strcmp(d->d_name, ".") ||
72             !strcmp(d->d_name, ".."))
73                 return 0;
74         return 1;
75 }
76
77 /**
78  * find_write_victim()
79  * @var - variable to be written
80  * @file - name of file to open for writing @var is returned.
81  *
82  * This ignores "." and ".." entries, and selects all others.
83  */
84 static char *
85 find_write_victim(efi_variable_t *var, char file[PATH_MAX])
86 {
87         struct dirent **namelist = NULL;
88         int i, n, found=0;
89         char testname[PATH_MAX], *p;
90
91         memset(testname, 0, sizeof(testname));
92         n = scandir(PROCFS_DIR_EFI_VARS, &namelist,
93                     select_variable_names, alphasort);
94         if (n < 0)
95                 return NULL;
96
97         p = testname;
98         efichar_to_char(p, var->VariableName, PATH_MAX);
99         p += strlen(p);
100         p += sprintf(p, "-");
101         efi_guid_unparse(&var->VendorGuid, p);
102
103         for (i=0; i<n; i++) {
104                 if (namelist[i] &&
105                     strncmp(testname, namelist[i]->d_name, sizeof(testname))) {
106                         found++;
107                         sprintf(file, "%s/%s", PROCFS_DIR_EFI_VARS,
108                                 namelist[i]->d_name);
109                         break;
110                 }
111         }
112
113         while (n--) {
114                 if (namelist[n]) {
115                         free(namelist[n]);
116                         namelist[n] = NULL;
117                 }
118         }
119         free(namelist);
120
121         if (!found) return NULL;
122         return file;
123 }
124
125
126 static efi_status_t
127 procfs_write_variable(efi_variable_t *var)
128 {
129         int fd;
130         size_t writesize;
131         char buffer[PATH_MAX], name[PATH_MAX], *p = NULL;
132
133         if (!var) return EFI_INVALID_PARAMETER;
134         memset(buffer, 0, sizeof(buffer));
135         memset(name, 0, sizeof(name));
136
137         p = find_write_victim(var, name);
138         if (!p) return EFI_INVALID_PARAMETER;
139
140         fd = open(name, O_WRONLY);
141         if (fd == -1) {
142                 sprintf(buffer, "write_variable():open(%s)", name);
143                 perror(buffer);
144                 return EFI_INVALID_PARAMETER;
145         }
146         writesize = write(fd, var, sizeof(*var));
147         if (writesize != sizeof(*var)) {
148                 close(fd);
149                 return EFI_INVALID_PARAMETER;
150
151         }
152         close(fd);
153         return EFI_SUCCESS;
154 }
155
156 static efi_status_t
157 procfs_delete_variable(efi_variable_t *var)
158 {
159         if (!var) return EFI_INVALID_PARAMETER;
160         var->DataSize = 0;
161         var->Attributes = 0;
162         return procfs_write_variable(var);
163
164 }
165
166 static efi_status_t
167 procfs_edit_variable(const char *unused, efi_variable_t *var)
168 {
169         if (!var) return EFI_INVALID_PARAMETER;
170         return procfs_write_variable(var);
171
172 }
173
174 struct efivar_kernel_calls procfs_kernel_calls = {
175         .read = procfs_read_variable,
176         .edit = procfs_edit_variable,
177         .create = procfs_write_variable,
178         .delete = procfs_delete_variable,
179         .path = PROCFS_DIR_EFI_VARS,
180 };