Imported Debian patch 0.5.3-1
[debian/efibootmgr] / src / lib / efivars_sysfs.c
1 /*
2   efivars_sysfs.[ch] - Manipulates EFI variables as exported in /sys/firmware/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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31
32 #include "efi.h"
33 #include "efichar.h"
34 #include "efibootmgr.h"
35 #include "efivars_sysfs.h"
36
37 static efi_status_t
38 sysfs_read_variable(const char *name, efi_variable_t *var)
39 {
40         char filename[PATH_MAX];
41         int fd;
42         size_t readsize;
43         char buffer[PATH_MAX+40];
44         if (!name || !var) return EFI_INVALID_PARAMETER;
45         memset(buffer, 0, sizeof(buffer));
46
47         snprintf(filename, PATH_MAX-1, "%s/%s/raw_var", SYSFS_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 static efi_status_t
62 sysfs_write_variable(const char *filename, efi_variable_t *var)
63 {
64         int fd;
65         size_t writesize;
66         char buffer[PATH_MAX+40];
67
68         if (!filename || !var) return EFI_INVALID_PARAMETER;
69         memset(buffer, 0, sizeof(buffer));
70
71         fd = open(filename, O_WRONLY);
72         if (fd == -1) {
73                 return EFI_INVALID_PARAMETER;
74         }
75         writesize = write(fd, var, sizeof(*var));
76         if (writesize != sizeof(*var)) {
77                 close(fd);
78                 return EFI_INVALID_PARAMETER;
79         }
80         close(fd);
81         return EFI_SUCCESS;
82 }
83
84
85 static efi_status_t
86 sysfs_edit_variable(const char *name, efi_variable_t *var)
87 {
88         char filename[PATH_MAX];
89         if (!var) return EFI_INVALID_PARAMETER;
90         snprintf(filename, PATH_MAX-1, "%s/%s/raw_var", SYSFS_DIR_EFI_VARS,name);
91         return sysfs_write_variable(filename, var);
92 }
93
94 static efi_status_t
95 sysfs_create_variable(efi_variable_t *var)
96 {
97         char filename[PATH_MAX];
98         if (!var) return EFI_INVALID_PARAMETER;
99         snprintf(filename, PATH_MAX-1, "%s/%s", SYSFS_DIR_EFI_VARS,"new_var");
100         return sysfs_write_variable(filename, var);
101 }
102
103 static efi_status_t
104 sysfs_delete_variable(efi_variable_t *var)
105 {
106         char filename[PATH_MAX];
107         if (!var) return EFI_INVALID_PARAMETER;
108         snprintf(filename, PATH_MAX-1, "%s/%s", SYSFS_DIR_EFI_VARS,"del_var");
109         return sysfs_write_variable(filename, var);
110 }
111
112 struct efivar_kernel_calls sysfs_kernel_calls = {
113         .read = sysfs_read_variable,
114         .edit = sysfs_edit_variable,
115         .create = sysfs_create_variable,
116         .delete = sysfs_delete_variable,
117         .path = SYSFS_DIR_EFI_VARS,
118 };