1 /* file.c - file I/O functions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2006,2007,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/misc.h>
22 #include <grub/file.h>
26 #include <grub/device.h>
27 #include <grub/i18n.h>
29 void (*EXPORT_VAR (grub_grubnet_fini
)) (void);
31 grub_file_filter_t grub_file_filters
[GRUB_FILE_FILTER_MAX
];
33 /* Get the device part of the filename NAME. It is enclosed by parentheses. */
35 grub_file_get_device_name (const char *name
)
39 char *p
= grub_strchr (name
, ')');
44 grub_error (GRUB_ERR_BAD_FILENAME
, N_("missing `%c' symbol"), ')');
48 ret
= (char *) grub_malloc (p
- name
);
52 grub_memcpy (ret
, name
+ 1, p
- name
- 1);
53 ret
[p
- name
- 1] = '\0';
60 /* Support mem:xxx:size:xxx format in chainloader */
61 grub_file_t
grub_memfile_open(const char *name
);
62 #define GRUB_MEMFILE_MEM "mem:"
63 #define GRUB_MEMFILE_SIZE "size:"
65 grub_file_t
grub_memfile_open(const char *name
)
70 file
= (grub_file_t
)grub_zalloc(sizeof(*file
));
76 file
->name
= grub_strdup(name
);
77 file
->data
= (void *)grub_strtoul(name
+ grub_strlen(GRUB_MEMFILE_MEM
), NULL
, 0);
79 size
= grub_strstr(name
, GRUB_MEMFILE_SIZE
);
80 file
->size
= (grub_off_t
)grub_strtoul(size
+ grub_strlen(GRUB_MEMFILE_SIZE
), NULL
, 0);
82 grub_errno
= GRUB_ERR_NONE
;
87 grub_file_open (const char *name
, enum grub_file_type type
)
89 grub_device_t device
= 0;
90 grub_file_t file
= 0, last_file
= 0;
92 const char *file_name
;
93 grub_file_filter_id_t filter
;
95 /* <DESC> : mem:xxx:size:xxx format in chainloader */
96 if (grub_strncmp(name
, GRUB_MEMFILE_MEM
, grub_strlen(GRUB_MEMFILE_MEM
)) == 0) {
97 return grub_memfile_open(name
);
100 device_name
= grub_file_get_device_name (name
);
104 /* Get the file part of NAME. */
105 file_name
= (name
[0] == '(') ? grub_strchr (name
, ')') : NULL
;
111 device
= grub_device_open (device_name
);
112 grub_free (device_name
);
116 file
= (grub_file_t
) grub_zalloc (sizeof (*file
));
120 file
->device
= device
;
122 /* In case of relative pathnames and non-Unix systems (like Windows)
123 * name of host files may not start with `/'. Blocklists for host files
124 * are meaningless as well (for a start, host disk does not allow any direct
125 * access - it is just a marker). So skip host disk in this case.
127 if (device
->disk
&& file_name
[0] != '/'
128 #if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
129 && grub_strcmp (device
->disk
->name
, "host")
132 /* This is a block list. */
133 file
->fs
= &grub_fs_blocklist
;
136 file
->fs
= grub_fs_probe (device
);
141 if ((file
->fs
->fs_open
) (file
, file_name
) != GRUB_ERR_NONE
)
144 file
->name
= grub_strdup (name
);
145 grub_errno
= GRUB_ERR_NONE
;
147 for (filter
= 0; file
&& filter
< ARRAY_SIZE (grub_file_filters
);
149 if (grub_file_filters
[filter
])
152 file
= grub_file_filters
[filter
] (file
, type
);
153 if (file
&& file
!= last_file
)
155 file
->name
= grub_strdup (name
);
156 grub_errno
= GRUB_ERR_NONE
;
160 grub_file_close (last_file
);
166 grub_device_close (device
);
168 /* if (net) grub_net_close (net); */
175 grub_disk_read_hook_t grub_file_progress_hook
;
178 grub_file_read (grub_file_t file
, void *buf
, grub_size_t len
)
181 grub_disk_read_hook_t read_hook
;
182 void *read_hook_data
;
184 if (file
->offset
> file
->size
)
186 grub_error (GRUB_ERR_OUT_OF_RANGE
,
187 N_("attempt to read past the end of file"));
194 if (len
> file
->size
- file
->offset
)
195 len
= file
->size
- file
->offset
;
197 /* Prevent an overflow. */
198 if ((grub_ssize_t
) len
< 0)
204 if (grub_strncmp(file
->name
, GRUB_MEMFILE_MEM
, grub_strlen(GRUB_MEMFILE_MEM
)) == 0) {
205 grub_memcpy(buf
, (grub_uint8_t
*)(file
->data
) + file
->offset
, len
);
210 read_hook
= file
->read_hook
;
211 read_hook_data
= file
->read_hook_data
;
212 if (!file
->read_hook
)
214 file
->read_hook
= grub_file_progress_hook
;
215 file
->read_hook_data
= file
;
216 file
->progress_offset
= file
->offset
;
218 res
= (file
->fs
->fs_read
) (file
, buf
, len
);
219 file
->read_hook
= read_hook
;
220 file
->read_hook_data
= read_hook_data
;
228 grub_file_close (grub_file_t file
)
230 if (file
->fs
&& file
->fs
->fs_close
)
231 (file
->fs
->fs_close
) (file
);
234 grub_device_close (file
->device
);
235 grub_free (file
->name
);
241 grub_file_seek (grub_file_t file
, grub_off_t offset
)
245 if (offset
> file
->size
)
247 grub_error (GRUB_ERR_OUT_OF_RANGE
,
248 N_("attempt to seek outside of the file"));
253 file
->offset
= offset
;