Imported Debian patch 0.4.9-0.sarge.2
[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 #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_sysfs.h"
38
39 static efi_status_t
40 sysfs_read_variable(const char *name, efi_variable_t *var)
41 {
42         char filename[PATH_MAX];
43         int fd;
44         size_t readsize;
45         char buffer[PATH_MAX+40];
46         if (!name || !var) return EFI_INVALID_PARAMETER;
47         memset(buffer, 0, sizeof(buffer));
48
49         snprintf(filename, PATH_MAX-1, "%s/%s/raw_var", SYSFS_DIR_EFI_VARS,name);
50         fd = open(filename, O_RDONLY);
51         if (fd == -1) {
52                 return EFI_NOT_FOUND;
53         }
54         readsize = read(fd, var, sizeof(*var));
55         if (readsize != sizeof(*var)) {
56                 close(fd);
57                 return EFI_INVALID_PARAMETER;
58         }
59         close(fd);
60         return var->Status;
61 }
62
63 static efi_status_t
64 sysfs_write_variable(const char *filename, efi_variable_t *var)
65 {
66         int fd;
67         size_t writesize;
68         char buffer[PATH_MAX+40];
69
70         if (!filename || !var) return EFI_INVALID_PARAMETER;
71         memset(buffer, 0, sizeof(buffer));
72
73         fd = open(filename, O_WRONLY);
74         if (fd == -1) {
75                 return EFI_INVALID_PARAMETER;
76         }
77         writesize = write(fd, var, sizeof(*var));
78         if (writesize != sizeof(*var)) {
79                 close(fd);
80                 return EFI_INVALID_PARAMETER;
81         }
82         close(fd);
83         return EFI_SUCCESS;
84 }
85
86
87 static efi_status_t
88 sysfs_edit_variable(const char *name, efi_variable_t *var)
89 {
90         char filename[PATH_MAX];
91         if (!var) return EFI_INVALID_PARAMETER;
92         snprintf(filename, PATH_MAX-1, "%s/%s/raw_var", SYSFS_DIR_EFI_VARS,name);
93         return sysfs_write_variable(filename, var);
94 }
95
96 static efi_status_t
97 sysfs_create_variable(efi_variable_t *var)
98 {
99         char filename[PATH_MAX];
100         if (!var) return EFI_INVALID_PARAMETER;
101         snprintf(filename, PATH_MAX-1, "%s/%s", SYSFS_DIR_EFI_VARS,"new_var");
102         return sysfs_write_variable(filename, var);
103 }
104
105 static efi_status_t
106 sysfs_delete_variable(efi_variable_t *var)
107 {
108         char filename[PATH_MAX];
109         if (!var) return EFI_INVALID_PARAMETER;
110         snprintf(filename, PATH_MAX-1, "%s/%s", SYSFS_DIR_EFI_VARS,"del_var");
111         return sysfs_write_variable(filename, var);
112 }
113
114 struct efivar_kernel_calls sysfs_kernel_calls = {
115         .read = sysfs_read_variable,
116         .edit = sysfs_edit_variable,
117         .create = sysfs_create_variable,
118         .delete = sysfs_delete_variable,
119         .path = SYSFS_DIR_EFI_VARS,
120 };