2 efivars_procfs.[ch] - Manipulates EFI variables as exported in /proc/efi/vars
4 Copyright (C) 2001,2003 Dell Computer Corporation <Matt_Domsch@dell.com>
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.
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.
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
21 #define _FILE_OFFSET_BITS 64
31 #include <sys/types.h>
36 #include "efibootmgr.h"
37 #include "efivars_procfs.h"
40 procfs_read_variable(const char *name, efi_variable_t *var)
42 char filename[PATH_MAX];
45 if (!name || !var) return EFI_INVALID_PARAMETER;
47 snprintf(filename, PATH_MAX-1, "%s/%s", PROCFS_DIR_EFI_VARS,name);
48 fd = open(filename, O_RDONLY);
52 readsize = read(fd, var, sizeof(*var));
53 if (readsize != sizeof(*var)) {
55 return EFI_INVALID_PARAMETER;
62 * select_variable_names()
63 * @d - dirent to compare against
65 * This ignores "." and ".." entries, and selects all others.
69 select_variable_names(const struct dirent *d)
71 if (!strcmp(d->d_name, ".") ||
72 !strcmp(d->d_name, ".."))
79 * @var - variable to be written
80 * @file - name of file to open for writing @var is returned.
82 * This ignores "." and ".." entries, and selects all others.
85 find_write_victim(efi_variable_t *var, char file[PATH_MAX])
87 struct dirent **namelist = NULL;
89 char testname[PATH_MAX], *p;
91 memset(testname, 0, sizeof(testname));
92 n = scandir(PROCFS_DIR_EFI_VARS, &namelist,
93 select_variable_names, alphasort);
98 efichar_to_char(p, var->VariableName, PATH_MAX);
100 p += sprintf(p, "-");
101 efi_guid_unparse(&var->VendorGuid, p);
103 for (i=0; i<n; i++) {
105 strncmp(testname, namelist[i]->d_name, sizeof(testname))) {
107 sprintf(file, "%s/%s", PROCFS_DIR_EFI_VARS,
108 namelist[i]->d_name);
121 if (!found) return NULL;
127 procfs_write_variable(efi_variable_t *var)
131 char buffer[PATH_MAX], name[PATH_MAX], *p = NULL;
133 if (!var) return EFI_INVALID_PARAMETER;
134 memset(buffer, 0, sizeof(buffer));
135 memset(name, 0, sizeof(name));
137 p = find_write_victim(var, name);
138 if (!p) return EFI_INVALID_PARAMETER;
140 fd = open(name, O_WRONLY);
142 sprintf(buffer, "write_variable():open(%s)", name);
144 return EFI_INVALID_PARAMETER;
146 writesize = write(fd, var, sizeof(*var));
147 if (writesize != sizeof(*var)) {
149 return EFI_INVALID_PARAMETER;
157 procfs_delete_variable(efi_variable_t *var)
159 if (!var) return EFI_INVALID_PARAMETER;
162 return procfs_write_variable(var);
167 procfs_edit_variable(const char *unused, efi_variable_t *var)
169 if (!var) return EFI_INVALID_PARAMETER;
170 return procfs_write_variable(var);
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,