]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/file.c
verifiers: File type for fine-grained signature-verification controlling
[grub2.git] / grub-core / kern / file.c
1 /* file.c - file I/O functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2006,2007,2009 Free Software Foundation, Inc.
5 *
6 * GRUB 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/misc.h>
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/net.h>
24 #include <grub/mm.h>
25 #include <grub/fs.h>
26 #include <grub/device.h>
27 #include <grub/i18n.h>
28
29 void (*EXPORT_VAR (grub_grubnet_fini)) (void);
30
31 grub_file_filter_t grub_file_filters[GRUB_FILE_FILTER_MAX];
32
33 /* Get the device part of the filename NAME. It is enclosed by parentheses. */
34 char *
35 grub_file_get_device_name (const char *name)
36 {
37 if (name[0] == '(')
38 {
39 char *p = grub_strchr (name, ')');
40 char *ret;
41
42 if (! p)
43 {
44 grub_error (GRUB_ERR_BAD_FILENAME, N_("missing `%c' symbol"), ')');
45 return 0;
46 }
47
48 ret = (char *) grub_malloc (p - name);
49 if (! ret)
50 return 0;
51
52 grub_memcpy (ret, name + 1, p - name - 1);
53 ret[p - name - 1] = '\0';
54 return ret;
55 }
56
57 return 0;
58 }
59
60 grub_file_t
61 grub_file_open (const char *name, enum grub_file_type type)
62 {
63 grub_device_t device = 0;
64 grub_file_t file = 0, last_file = 0;
65 char *device_name;
66 const char *file_name;
67 grub_file_filter_id_t filter;
68
69 device_name = grub_file_get_device_name (name);
70 if (grub_errno)
71 goto fail;
72
73 /* Get the file part of NAME. */
74 file_name = (name[0] == '(') ? grub_strchr (name, ')') : NULL;
75 if (file_name)
76 file_name++;
77 else
78 file_name = name;
79
80 device = grub_device_open (device_name);
81 grub_free (device_name);
82 if (! device)
83 goto fail;
84
85 file = (grub_file_t) grub_zalloc (sizeof (*file));
86 if (! file)
87 goto fail;
88
89 file->device = device;
90
91 /* In case of relative pathnames and non-Unix systems (like Windows)
92 * name of host files may not start with `/'. Blocklists for host files
93 * are meaningless as well (for a start, host disk does not allow any direct
94 * access - it is just a marker). So skip host disk in this case.
95 */
96 if (device->disk && file_name[0] != '/'
97 #if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
98 && grub_strcmp (device->disk->name, "host")
99 #endif
100 )
101 /* This is a block list. */
102 file->fs = &grub_fs_blocklist;
103 else
104 {
105 file->fs = grub_fs_probe (device);
106 if (! file->fs)
107 goto fail;
108 }
109
110 if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE)
111 goto fail;
112
113 file->name = grub_strdup (name);
114 grub_errno = GRUB_ERR_NONE;
115
116 for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters);
117 filter++)
118 if (grub_file_filters[filter])
119 {
120 last_file = file;
121 file = grub_file_filters[filter] (file, type);
122 if (file && file != last_file)
123 {
124 file->name = grub_strdup (name);
125 grub_errno = GRUB_ERR_NONE;
126 }
127 }
128 if (!file)
129 grub_file_close (last_file);
130
131 return file;
132
133 fail:
134 if (device)
135 grub_device_close (device);
136
137 /* if (net) grub_net_close (net); */
138
139 grub_free (file);
140
141 return 0;
142 }
143
144 grub_disk_read_hook_t grub_file_progress_hook;
145
146 grub_ssize_t
147 grub_file_read (grub_file_t file, void *buf, grub_size_t len)
148 {
149 grub_ssize_t res;
150 grub_disk_read_hook_t read_hook;
151 void *read_hook_data;
152
153 if (file->offset > file->size)
154 {
155 grub_error (GRUB_ERR_OUT_OF_RANGE,
156 N_("attempt to read past the end of file"));
157 return -1;
158 }
159
160 if (len == 0)
161 return 0;
162
163 if (len > file->size - file->offset)
164 len = file->size - file->offset;
165
166 /* Prevent an overflow. */
167 if ((grub_ssize_t) len < 0)
168 len >>= 1;
169
170 if (len == 0)
171 return 0;
172 read_hook = file->read_hook;
173 read_hook_data = file->read_hook_data;
174 if (!file->read_hook)
175 {
176 file->read_hook = grub_file_progress_hook;
177 file->read_hook_data = file;
178 file->progress_offset = file->offset;
179 }
180 res = (file->fs->read) (file, buf, len);
181 file->read_hook = read_hook;
182 file->read_hook_data = read_hook_data;
183 if (res > 0)
184 file->offset += res;
185
186 return res;
187 }
188
189 grub_err_t
190 grub_file_close (grub_file_t file)
191 {
192 if (file->fs->close)
193 (file->fs->close) (file);
194
195 if (file->device)
196 grub_device_close (file->device);
197 grub_free (file->name);
198 grub_free (file);
199 return grub_errno;
200 }
201
202 grub_off_t
203 grub_file_seek (grub_file_t file, grub_off_t offset)
204 {
205 grub_off_t old;
206
207 if (offset > file->size)
208 {
209 grub_error (GRUB_ERR_OUT_OF_RANGE,
210 N_("attempt to seek outside of the file"));
211 return -1;
212 }
213
214 old = file->offset;
215 file->offset = offset;
216
217 return old;
218 }