make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / kern / file.c
blobadf55daf38c69eea9d2d8dbb6434fa866f159a97
1 /* file.c - file I/O functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2006,2007 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>
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/fs.h>
25 #include <grub/device.h>
27 /* Get the device part of the filename NAME. It is enclosed by parentheses. */
28 char *
29 grub_file_get_device_name (const char *name)
31 if (name[0] == '(')
33 char *p = grub_strchr (name, ')');
34 char *ret;
36 if (! p)
38 grub_error (GRUB_ERR_BAD_FILENAME, "missing `)'");
39 return 0;
42 ret = (char *) grub_malloc (p - name);
43 if (! ret)
44 return 0;
46 grub_memcpy (ret, name + 1, p - name - 1);
47 ret[p - name - 1] = '\0';
48 return ret;
51 return 0;
54 grub_file_t
55 grub_file_open (const char *name)
57 grub_device_t device;
58 grub_file_t file = 0;
59 char *device_name;
60 char *file_name;
62 device_name = grub_file_get_device_name (name);
63 if (grub_errno)
64 return 0;
66 /* Get the file part of NAME. */
67 file_name = grub_strchr (name, ')');
68 if (file_name)
69 file_name++;
70 else
71 file_name = (char *) name;
73 device = grub_device_open (device_name);
74 grub_free (device_name);
75 if (! device)
76 goto fail;
78 file = (grub_file_t) grub_malloc (sizeof (*file));
79 if (! file)
80 goto fail;
82 file->device = device;
83 file->offset = 0;
84 file->data = 0;
85 file->read_hook = 0;
87 if (device->disk && file_name[0] != '/')
88 /* This is a block list. */
89 file->fs = &grub_fs_blocklist;
90 else
92 file->fs = grub_fs_probe (device);
93 if (! file->fs)
94 goto fail;
97 if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE)
98 goto fail;
100 return file;
102 fail:
103 if (device)
104 grub_device_close (device);
106 /* if (net) grub_net_close (net); */
108 grub_free (file);
110 return 0;
113 grub_ssize_t
114 grub_file_read (grub_file_t file, char *buf, grub_size_t len)
116 grub_ssize_t res;
118 if (len == 0 || len > file->size - file->offset)
119 len = file->size - file->offset;
121 /* Prevent an overflow. */
122 if ((grub_ssize_t) len < 0)
123 len >>= 1;
125 if (len == 0)
126 return 0;
128 res = (file->fs->read) (file, buf, len);
129 if (res > 0)
130 file->offset += res;
132 return res;
135 grub_err_t
136 grub_file_close (grub_file_t file)
138 if (file->fs->close)
139 (file->fs->close) (file);
141 if (file->device)
142 grub_device_close (file->device);
143 grub_free (file);
144 return grub_errno;
147 grub_off_t
148 grub_file_seek (grub_file_t file, grub_off_t offset)
150 grub_off_t old;
152 if (offset > file->size)
154 grub_error (GRUB_ERR_OUT_OF_RANGE,
155 "attempt to seek outside of the file");
156 return -1;
159 old = file->offset;
160 file->offset = offset;
161 return old;