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
29 #include <sys/types.h>
34 #include "efibootmgr.h"
35 #include "efivars_procfs.h"
38 procfs_read_variable(const char *name, efi_variable_t *var)
40 char filename[PATH_MAX];
43 if (!name || !var) return EFI_INVALID_PARAMETER;
45 snprintf(filename, PATH_MAX-1, "%s/%s", PROCFS_DIR_EFI_VARS,name);
46 fd = open(filename, O_RDONLY);
50 readsize = read(fd, var, sizeof(*var));
51 if (readsize != sizeof(*var)) {
53 return EFI_INVALID_PARAMETER;
60 * select_variable_names()
61 * @d - dirent to compare against
63 * This ignores "." and ".." entries, and selects all others.
67 select_variable_names(const struct dirent *d)
69 if (!strcmp(d->d_name, ".") ||
70 !strcmp(d->d_name, ".."))
77 * @var - variable to be written
78 * @file - name of file to open for writing @var is returned.
80 * This ignores "." and ".." entries, and selects all others.
83 find_write_victim(efi_variable_t *var, char file[PATH_MAX])
85 struct dirent **namelist = NULL;
87 char testname[PATH_MAX], *p;
89 memset(testname, 0, sizeof(testname));
90 n = scandir(PROCFS_DIR_EFI_VARS, &namelist,
91 select_variable_names, alphasort);
96 efichar_to_char(p, var->VariableName, PATH_MAX);
99 efi_guid_unparse(&var->VendorGuid, p);
101 for (i=0; i<n; i++) {
103 strncmp(testname, namelist[i]->d_name, sizeof(testname))) {
105 sprintf(file, "%s/%s", PROCFS_DIR_EFI_VARS,
106 namelist[i]->d_name);
119 if (!found) return NULL;
125 procfs_write_variable(efi_variable_t *var)
129 char buffer[PATH_MAX], name[PATH_MAX], *p = NULL;
131 if (!var) return EFI_INVALID_PARAMETER;
132 memset(buffer, 0, sizeof(buffer));
133 memset(name, 0, sizeof(name));
135 p = find_write_victim(var, name);
136 if (!p) return EFI_INVALID_PARAMETER;
138 fd = open(name, O_WRONLY);
140 sprintf(buffer, "write_variable():open(%s)", name);
142 return EFI_INVALID_PARAMETER;
144 writesize = write(fd, var, sizeof(*var));
145 if (writesize != sizeof(*var)) {
147 return EFI_INVALID_PARAMETER;
155 procfs_delete_variable(efi_variable_t *var)
157 if (!var) return EFI_INVALID_PARAMETER;
160 return procfs_write_variable(var);
165 procfs_edit_variable(const char *unused, efi_variable_t *var)
167 if (!var) return EFI_INVALID_PARAMETER;
168 return procfs_write_variable(var);
172 struct efivar_kernel_calls procfs_kernel_calls = {
173 .read = procfs_read_variable,
174 .edit = procfs_edit_variable,
175 .create = procfs_write_variable,
176 .delete = procfs_delete_variable,
177 .path = PROCFS_DIR_EFI_VARS,