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

« back to all changes in this revision

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