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

« back to all changes in this revision

Viewing changes to grub-core/commands/search.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
/* search.c - search devices based on a file or a filesystem label */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005,2007,2008,2009  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/types.h>
 
21
#include <grub/misc.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/err.h>
 
24
#include <grub/dl.h>
 
25
#include <grub/device.h>
 
26
#include <grub/file.h>
 
27
#include <grub/env.h>
 
28
#include <grub/command.h>
 
29
#include <grub/search.h>
 
30
#include <grub/i18n.h>
 
31
#include <grub/disk.h>
 
32
#include <grub/partition.h>
 
33
 
 
34
void
 
35
FUNC_NAME (const char *key, const char *var, int no_floppy,
 
36
           char **hints, unsigned nhints)
 
37
{
 
38
  int count = 0;
 
39
  grub_fs_autoload_hook_t saved_autoload;
 
40
 
 
41
  auto int iterate_device (const char *name);
 
42
  int iterate_device (const char *name)
 
43
  {
 
44
    int found = 0;
 
45
 
 
46
    /* Skip floppy drives when requested.  */
 
47
    if (no_floppy &&
 
48
        name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
 
49
      return 0;
 
50
 
 
51
#ifdef DO_SEARCH_FILE
 
52
      {
 
53
        char *buf;
 
54
        grub_file_t file;
 
55
 
 
56
        buf = grub_xasprintf ("(%s)%s", name, key);
 
57
        if (! buf)
 
58
          return 1;
 
59
 
 
60
        grub_file_filter_disable_compression ();
 
61
        file = grub_file_open (buf);
 
62
        if (file)
 
63
          {
 
64
            found = 1;
 
65
            grub_file_close (file);
 
66
          }
 
67
        grub_free (buf);
 
68
      }
 
69
#else
 
70
      {
 
71
        /* SEARCH_FS_UUID or SEARCH_LABEL */
 
72
        grub_device_t dev;
 
73
        grub_fs_t fs;
 
74
        char *quid;
 
75
 
 
76
        dev = grub_device_open (name);
 
77
        if (dev)
 
78
          {
 
79
            fs = grub_fs_probe (dev);
 
80
 
 
81
#ifdef DO_SEARCH_FS_UUID
 
82
#define compare_fn grub_strcasecmp
 
83
#define read_fn uuid
 
84
#else
 
85
#define compare_fn grub_strcmp
 
86
#define read_fn label
 
87
#endif
 
88
 
 
89
            if (fs && fs->read_fn)
 
90
              {
 
91
                fs->read_fn (dev, &quid);
 
92
 
 
93
                if (grub_errno == GRUB_ERR_NONE && quid)
 
94
                  {
 
95
                    if (compare_fn (quid, key) == 0)
 
96
                      found = 1;
 
97
 
 
98
                    grub_free (quid);
 
99
                  }
 
100
              }
 
101
 
 
102
            grub_device_close (dev);
 
103
          }
 
104
      }
 
105
#endif
 
106
 
 
107
    if (found)
 
108
      {
 
109
        count++;
 
110
        if (var)
 
111
          grub_env_set (var, name);
 
112
        else
 
113
          grub_printf (" %s", name);
 
114
      }
 
115
 
 
116
    grub_errno = GRUB_ERR_NONE;
 
117
    return (found && var);
 
118
  }
 
119
 
 
120
  auto int part_hook (grub_disk_t disk, const grub_partition_t partition);
 
121
  int part_hook (grub_disk_t disk, const grub_partition_t partition)
 
122
  {
 
123
    char *partition_name, *devname;
 
124
    int ret;
 
125
 
 
126
    partition_name = grub_partition_get_name (partition);
 
127
    if (! partition_name)
 
128
      return 1;
 
129
 
 
130
    devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
 
131
    grub_free (partition_name);
 
132
    if (!devname)
 
133
      return 1;
 
134
    ret = iterate_device (devname);
 
135
    grub_free (devname);    
 
136
 
 
137
    return ret;
 
138
  }
 
139
 
 
140
  auto void try (void);
 
141
  void try (void)    
 
142
  {
 
143
    unsigned i;
 
144
    for (i = 0; i < nhints; i++)
 
145
      {
 
146
        char *end;
 
147
        if (!hints[i][0])
 
148
          continue;
 
149
        end = hints[i] + grub_strlen (hints[i]) - 1;
 
150
        if (*end == ',')
 
151
          *end = 0;
 
152
        if (iterate_device (hints[i]))
 
153
          {
 
154
            if (!*end)
 
155
              *end = ',';
 
156
            return;
 
157
          }
 
158
        if (!*end)
 
159
          {
 
160
            grub_device_t dev;
 
161
            int ret;
 
162
            dev = grub_device_open (hints[i]);
 
163
            if (!dev)
 
164
              {
 
165
                *end = ',';
 
166
                continue;
 
167
              }
 
168
            if (!dev->disk)
 
169
              {
 
170
                grub_device_close (dev);
 
171
                *end = ',';
 
172
                continue;
 
173
              }
 
174
            ret = grub_partition_iterate (dev->disk, part_hook);
 
175
            *end = ',';
 
176
            grub_device_close (dev);
 
177
            if (ret)
 
178
              return;
 
179
          }
 
180
      }
 
181
    grub_device_iterate (iterate_device);
 
182
  }
 
183
 
 
184
  /* First try without autoloading if we're setting variable. */
 
185
  if (var)
 
186
    {
 
187
      saved_autoload = grub_fs_autoload_hook;
 
188
      grub_fs_autoload_hook = 0;
 
189
      try ();
 
190
 
 
191
      /* Restore autoload hook.  */
 
192
      grub_fs_autoload_hook = saved_autoload;
 
193
 
 
194
      /* Retry with autoload if nothing found.  */
 
195
      if (grub_errno == GRUB_ERR_NONE && count == 0)
 
196
        try ();
 
197
    }
 
198
  else
 
199
    try ();
 
200
 
 
201
  if (grub_errno == GRUB_ERR_NONE && count == 0)
 
202
    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
 
203
}
 
204
 
 
205
static grub_err_t
 
206
grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
 
207
                    char **args)
 
208
{
 
209
  if (argc == 0)
 
210
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
 
211
 
 
212
  FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
 
213
             argc > 2 ? argc - 2 : 0);
 
214
 
 
215
  return grub_errno;
 
216
}
 
217
 
 
218
static grub_command_t cmd;
 
219
 
 
220
#ifdef DO_SEARCH_FILE
 
221
GRUB_MOD_INIT(search_fs_file)
 
222
#elif defined (DO_SEARCH_FS_UUID)
 
223
GRUB_MOD_INIT(search_fs_uuid)
 
224
#else
 
225
GRUB_MOD_INIT(search_label)
 
226
#endif
 
227
{
 
228
  cmd =
 
229
    grub_register_command (COMMAND_NAME, grub_cmd_do_search,
 
230
                           N_("NAME [VARIABLE] [HINTS]"),
 
231
                           HELP_MESSAGE);
 
232
}
 
233
 
 
234
#ifdef DO_SEARCH_FILE
 
235
GRUB_MOD_FINI(search_fs_file)
 
236
#elif defined (DO_SEARCH_FS_UUID)
 
237
GRUB_MOD_FINI(search_fs_uuid)
 
238
#else
 
239
GRUB_MOD_FINI(search_label)
 
240
#endif
 
241
{
 
242
  grub_unregister_command (cmd);
 
243
}