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

« back to all changes in this revision

Viewing changes to kern/fs.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
 
/* fs.c - filesystem manager */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2002,2005,2007  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/disk.h>
21
 
#include <grub/net.h>
22
 
#include <grub/fs.h>
23
 
#include <grub/file.h>
24
 
#include <grub/err.h>
25
 
#include <grub/misc.h>
26
 
#include <grub/types.h>
27
 
#include <grub/mm.h>
28
 
#include <grub/term.h>
29
 
 
30
 
static grub_fs_t grub_fs_list;
31
 
 
32
 
grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
33
 
 
34
 
void
35
 
grub_fs_register (grub_fs_t fs)
36
 
{
37
 
  fs->next = grub_fs_list;
38
 
  grub_fs_list = fs;
39
 
}
40
 
 
41
 
void
42
 
grub_fs_unregister (grub_fs_t fs)
43
 
{
44
 
  grub_fs_t *p, q;
45
 
 
46
 
  for (p = &grub_fs_list, q = *p; q; p = &(q->next), q = q->next)
47
 
    if (q == fs)
48
 
      {
49
 
        *p = q->next;
50
 
        break;
51
 
      }
52
 
}
53
 
 
54
 
void
55
 
grub_fs_iterate (int (*hook) (const grub_fs_t fs))
56
 
{
57
 
  grub_fs_t p;
58
 
 
59
 
  for (p = grub_fs_list; p; p = p->next)
60
 
    if (hook (p))
61
 
      break;
62
 
}
63
 
 
64
 
grub_fs_t
65
 
grub_fs_probe (grub_device_t device)
66
 
{
67
 
  grub_fs_t p;
68
 
  auto int dummy_func (const char *filename,
69
 
                       const struct grub_dirhook_info *info);
70
 
 
71
 
  int dummy_func (const char *filename __attribute__ ((unused)),
72
 
                  const struct grub_dirhook_info *info  __attribute__ ((unused)))
73
 
    {
74
 
      return 1;
75
 
    }
76
 
 
77
 
  if (device->disk)
78
 
    {
79
 
      /* Make it sure not to have an infinite recursive calls.  */
80
 
      static int count = 0;
81
 
 
82
 
      for (p = grub_fs_list; p; p = p->next)
83
 
        {
84
 
          grub_dprintf ("fs", "Detecting %s...\n", p->name);
85
 
          (p->dir) (device, "/", dummy_func);
86
 
          if (grub_errno == GRUB_ERR_NONE)
87
 
            return p;
88
 
 
89
 
          grub_error_push ();
90
 
          grub_dprintf ("fs", "%s detection failed.\n", p->name);
91
 
          grub_error_pop ();
92
 
 
93
 
          if (grub_errno != GRUB_ERR_BAD_FS)
94
 
            return 0;
95
 
 
96
 
          grub_errno = GRUB_ERR_NONE;
97
 
        }
98
 
 
99
 
      /* Let's load modules automatically.  */
100
 
      if (grub_fs_autoload_hook && count == 0)
101
 
        {
102
 
          count++;
103
 
 
104
 
          while (grub_fs_autoload_hook ())
105
 
            {
106
 
              p = grub_fs_list;
107
 
 
108
 
              (p->dir) (device, "/", dummy_func);
109
 
              if (grub_errno == GRUB_ERR_NONE)
110
 
                {
111
 
                  count--;
112
 
                  return p;
113
 
                }
114
 
 
115
 
              if (grub_errno != GRUB_ERR_BAD_FS)
116
 
                {
117
 
                  count--;
118
 
                  return 0;
119
 
                }
120
 
 
121
 
              grub_errno = GRUB_ERR_NONE;
122
 
            }
123
 
 
124
 
          count--;
125
 
        }
126
 
    }
127
 
  else if (device->net->fs)
128
 
    return device->net->fs;
129
 
 
130
 
  grub_error (GRUB_ERR_UNKNOWN_FS, "unknown filesystem");
131
 
  return 0;
132
 
}
133
 
 
134
 
 
135
 
 
136
 
/* Block list support routines.  */
137
 
 
138
 
struct grub_fs_block
139
 
{
140
 
  grub_disk_addr_t offset;
141
 
  unsigned long length;
142
 
};
143
 
 
144
 
static grub_err_t
145
 
grub_fs_blocklist_open (grub_file_t file, const char *name)
146
 
{
147
 
  char *p = (char *) name;
148
 
  unsigned num = 0;
149
 
  unsigned i;
150
 
  grub_disk_t disk = file->device->disk;
151
 
  struct grub_fs_block *blocks;
152
 
 
153
 
  /* First, count the number of blocks.  */
154
 
  do
155
 
    {
156
 
      num++;
157
 
      p = grub_strchr (p, ',');
158
 
      if (p)
159
 
        p++;
160
 
    }
161
 
  while (p);
162
 
 
163
 
  /* Allocate a block list.  */
164
 
  blocks = grub_zalloc (sizeof (struct grub_fs_block) * (num + 1));
165
 
  if (! blocks)
166
 
    return 0;
167
 
 
168
 
  file->size = 0;
169
 
  p = (char *) name;
170
 
  for (i = 0; i < num; i++)
171
 
    {
172
 
      if (*p != '+')
173
 
        {
174
 
          blocks[i].offset = grub_strtoull (p, &p, 0);
175
 
          if (grub_errno != GRUB_ERR_NONE || *p != '+')
176
 
            {
177
 
              grub_error (GRUB_ERR_BAD_FILENAME,
178
 
                          "invalid file name `%s'", name);
179
 
              goto fail;
180
 
            }
181
 
        }
182
 
 
183
 
      p++;
184
 
      blocks[i].length = grub_strtoul (p, &p, 0);
185
 
      if (grub_errno != GRUB_ERR_NONE
186
 
          || blocks[i].length == 0
187
 
          || (*p && *p != ',' && ! grub_isspace (*p)))
188
 
        {
189
 
          grub_error (GRUB_ERR_BAD_FILENAME,
190
 
                      "invalid file name `%s'", name);
191
 
          goto fail;
192
 
        }
193
 
 
194
 
      if (disk->total_sectors < blocks[i].offset + blocks[i].length)
195
 
        {
196
 
          grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
197
 
          goto fail;
198
 
        }
199
 
 
200
 
      file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
201
 
      p++;
202
 
    }
203
 
 
204
 
  file->data = blocks;
205
 
 
206
 
  return GRUB_ERR_NONE;
207
 
 
208
 
 fail:
209
 
  grub_free (blocks);
210
 
  return grub_errno;
211
 
}
212
 
 
213
 
static grub_ssize_t
214
 
grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
215
 
{
216
 
  struct grub_fs_block *p;
217
 
  grub_disk_addr_t sector;
218
 
  grub_off_t offset;
219
 
  grub_ssize_t ret = 0;
220
 
 
221
 
  if (len > file->size - file->offset)
222
 
    len = file->size - file->offset;
223
 
 
224
 
  sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
225
 
  offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
226
 
  for (p = file->data; p->length && len > 0; p++)
227
 
    {
228
 
      if (sector < p->length)
229
 
        {
230
 
          grub_size_t size;
231
 
 
232
 
          size = len;
233
 
          if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
234
 
               >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
235
 
            size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
236
 
 
237
 
          if (grub_disk_read (file->device->disk, p->offset + sector, offset,
238
 
                              size, buf) != GRUB_ERR_NONE)
239
 
            return -1;
240
 
 
241
 
          ret += size;
242
 
          len -= size;
243
 
          sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
244
 
          offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
245
 
        }
246
 
      else
247
 
        sector -= p->length;
248
 
    }
249
 
 
250
 
  return ret;
251
 
}
252
 
 
253
 
struct grub_fs grub_fs_blocklist =
254
 
  {
255
 
    .name = "blocklist",
256
 
    .dir = 0,
257
 
    .open = grub_fs_blocklist_open,
258
 
    .read = grub_fs_blocklist_read,
259
 
    .close = 0,
260
 
    .next = 0
261
 
  };