~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/io/bufio.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* bufio.c - buffered io access */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2008  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/err.h>
 
21
#include <grub/types.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/fs.h>
 
25
#include <grub/bufio.h>
 
26
 
 
27
#define GRUB_BUFIO_DEF_SIZE     8192
 
28
#define GRUB_BUFIO_MAX_SIZE     1048576
 
29
 
 
30
struct grub_bufio
 
31
{
 
32
  grub_file_t file;
 
33
  grub_size_t block_size;
 
34
  grub_size_t buffer_len;
 
35
  char buffer[0];
 
36
};
 
37
typedef struct grub_bufio *grub_bufio_t;
 
38
 
 
39
static struct grub_fs grub_bufio_fs;
 
40
 
 
41
grub_file_t
 
42
grub_bufio_open (grub_file_t io, int size)
 
43
{
 
44
  grub_file_t file;
 
45
  grub_bufio_t bufio = 0;
 
46
 
 
47
  file = (grub_file_t) grub_malloc (sizeof (*file));
 
48
  if (! file)
 
49
    return 0;
 
50
 
 
51
  if (size == 0)
 
52
    size = GRUB_BUFIO_DEF_SIZE;
 
53
  else if (size > GRUB_BUFIO_MAX_SIZE)
 
54
    size = GRUB_BUFIO_MAX_SIZE;
 
55
 
 
56
  if ((size < 0) || ((unsigned) size > io->size))
 
57
    size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
 
58
            io->size);
 
59
 
 
60
  bufio = grub_malloc (sizeof (struct grub_bufio) + size);
 
61
  if (! bufio)
 
62
    {
 
63
      grub_free (file);
 
64
      return 0;
 
65
    }
 
66
 
 
67
  bufio->file = io;
 
68
  bufio->block_size = size;
 
69
  bufio->buffer_len = 0;
 
70
 
 
71
  file->device = io->device;
 
72
  file->offset = 0;
 
73
  file->size = io->size;
 
74
  file->data = bufio;
 
75
  file->read_hook = 0;
 
76
  file->fs = &grub_bufio_fs;
 
77
  file->not_easly_seekable = io->not_easly_seekable;
 
78
 
 
79
  return file;
 
80
}
 
81
 
 
82
grub_file_t
 
83
grub_buffile_open (const char *name, int size)
 
84
{
 
85
  grub_file_t io, file;
 
86
 
 
87
  io = grub_file_open (name);
 
88
  if (! io)
 
89
    return 0;
 
90
 
 
91
  file = grub_bufio_open (io, size);
 
92
  if (! file)
 
93
    {
 
94
      grub_file_close (io);
 
95
      return 0;
 
96
    }
 
97
 
 
98
  return file;
 
99
}
 
100
 
 
101
static grub_ssize_t
 
102
grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
 
103
{
 
104
  grub_size_t res = len;
 
105
  grub_bufio_t bufio = file->data;
 
106
  grub_uint32_t pos;
 
107
 
 
108
  if ((file->offset >= bufio->file->offset) &&
 
109
      (file->offset < bufio->file->offset + bufio->buffer_len))
 
110
    {
 
111
      grub_size_t n;
 
112
 
 
113
      pos = file->offset - bufio->file->offset;
 
114
      n = bufio->buffer_len - pos;
 
115
      if (n > len)
 
116
        n = len;
 
117
 
 
118
      grub_memcpy (buf, &bufio->buffer[pos], n);
 
119
      len -= n;
 
120
      if (! len)
 
121
        return res;
 
122
 
 
123
      buf += n;
 
124
      bufio->file->offset += bufio->buffer_len;
 
125
      pos = 0;
 
126
    }
 
127
  else
 
128
    {
 
129
      bufio->file->offset = grub_divmod64 (file->offset, bufio->block_size,
 
130
                                           &pos);
 
131
      bufio->file->offset *= bufio->block_size;
 
132
    }
 
133
 
 
134
  if (pos + len >= bufio->block_size)
 
135
    {
 
136
      if (pos)
 
137
        {
 
138
          grub_size_t n;
 
139
 
 
140
          bufio->file->fs->read (bufio->file, bufio->buffer,
 
141
                                 bufio->block_size);
 
142
          if (grub_errno)
 
143
            return -1;
 
144
 
 
145
          n = bufio->block_size - pos;
 
146
          grub_memcpy (buf, &bufio->buffer[pos], n);
 
147
          len -= n;
 
148
          buf += n;
 
149
          bufio->file->offset += bufio->block_size;
 
150
          pos = 0;
 
151
        }
 
152
 
 
153
      while (len >= bufio->block_size)
 
154
        {
 
155
          bufio->file->fs->read (bufio->file, buf, bufio->block_size);
 
156
          if (grub_errno)
 
157
            return -1;
 
158
 
 
159
          len -= bufio->block_size;
 
160
          buf += bufio->block_size;
 
161
          bufio->file->offset += bufio->block_size;
 
162
        }
 
163
 
 
164
      if (! len)
 
165
        {
 
166
          bufio->buffer_len = 0;
 
167
          return res;
 
168
        }
 
169
    }
 
170
 
 
171
  bufio->buffer_len = bufio->file->size - bufio->file->offset;
 
172
  if (bufio->buffer_len > bufio->block_size)
 
173
    bufio->buffer_len = bufio->block_size;
 
174
 
 
175
  bufio->file->fs->read (bufio->file, bufio->buffer, bufio->buffer_len);
 
176
  if (grub_errno)
 
177
    return -1;
 
178
 
 
179
  grub_memcpy (buf, &bufio->buffer[pos], len);
 
180
 
 
181
  return res;
 
182
}
 
183
 
 
184
static grub_err_t
 
185
grub_bufio_close (grub_file_t file)
 
186
{
 
187
  grub_bufio_t bufio = file->data;
 
188
 
 
189
  grub_file_close (bufio->file);
 
190
  grub_free (bufio);
 
191
 
 
192
  file->device = 0;
 
193
 
 
194
  return grub_errno;
 
195
}
 
196
 
 
197
static struct grub_fs grub_bufio_fs =
 
198
  {
 
199
    .name = "bufio",
 
200
    .dir = 0,
 
201
    .open = 0,
 
202
    .read = grub_bufio_read,
 
203
    .close = grub_bufio_close,
 
204
    .label = 0,
 
205
    .next = 0
 
206
  };