~ubuntu-branches/debian/sid/grub2/sid-200907171840

« back to all changes in this revision

Viewing changes to kern/corecmd.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2009-07-02 13:23:51 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702132351-tanpn0ryyijp93gu
Tags: 1.96+20090702-1
* New SVN snapshot.
* rules: Remove duplicated files in sparc64-ieee1275 port.
* rules: Comment out -DGRUB_ASSUME_LINUX_HAS_FB_SUPPORT=1 setting.  We'll
  re-evaluate using it when it's more mature.  (Closes: #535026).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* corecmd.c - critical commands which are registered in kernel */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 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/mm.h>
 
21
#include <grub/dl.h>
 
22
#include <grub/err.h>
 
23
#include <grub/env.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/term.h>
 
26
#include <grub/file.h>
 
27
#include <grub/device.h>
 
28
#include <grub/command.h>
 
29
 
 
30
/* set ENVVAR=VALUE */
 
31
static grub_err_t
 
32
grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
 
33
                   int argc, char *argv[])
 
34
{
 
35
  char *var;
 
36
  char *val;
 
37
 
 
38
  auto int print_env (struct grub_env_var *env);
 
39
 
 
40
  int print_env (struct grub_env_var *env)
 
41
    {
 
42
      grub_printf ("%s=%s\n", env->name, env->value);
 
43
      return 0;
 
44
    }
 
45
 
 
46
  if (argc < 1)
 
47
    {
 
48
      grub_env_iterate (print_env);
 
49
      return 0;
 
50
    }
 
51
 
 
52
  var = argv[0];
 
53
  val = grub_strchr (var, '=');
 
54
  if (! val)
 
55
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
 
56
 
 
57
  val[0] = 0;
 
58
  grub_env_set (var, val + 1);
 
59
  val[0] = '=';
 
60
 
 
61
  return 0;
 
62
}
 
63
 
 
64
static grub_err_t
 
65
grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
 
66
                     int argc, char *argv[])
 
67
{
 
68
  if (argc < 1)
 
69
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
 
70
                       "no environment variable specified");
 
71
 
 
72
  grub_env_unset (argv[0]);
 
73
  return 0;
 
74
}
 
75
 
 
76
static grub_err_t
 
77
grub_core_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
 
78
                      int argc, char **args)
 
79
{
 
80
  if (argc < 1)
 
81
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
 
82
                       "no environment variable specified");
 
83
 
 
84
  grub_env_export (args[0]);
 
85
  return 0;
 
86
}
 
87
 
 
88
/* insmod MODULE */
 
89
static grub_err_t
 
90
grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
 
91
                      int argc, char *argv[])
 
92
{
 
93
  char *p;
 
94
  grub_dl_t mod;
 
95
 
 
96
  if (argc == 0)
 
97
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
 
98
 
 
99
  p = grub_strchr (argv[0], '/');
 
100
  if (! p)
 
101
    mod = grub_dl_load (argv[0]);
 
102
  else
 
103
    mod = grub_dl_load_file (argv[0]);
 
104
 
 
105
  if (mod)
 
106
    grub_dl_ref (mod);
 
107
 
 
108
  return 0;
 
109
}
 
110
 
 
111
static int
 
112
grub_mini_print_devices (const char *name)
 
113
{
 
114
  grub_printf ("(%s) ", name);
 
115
 
 
116
  return 0;
 
117
}
 
118
 
 
119
static int
 
120
grub_mini_print_files (const char *filename,
 
121
                       const struct grub_dirhook_info *info)
 
122
{
 
123
  grub_printf ("%s%s ", filename, info->dir ? "/" : "");
 
124
 
 
125
  return 0;
 
126
}
 
127
 
 
128
/* ls [ARG] */
 
129
static grub_err_t
 
130
grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
 
131
                  int argc, char *argv[])
 
132
{
 
133
  if (argc < 1)
 
134
    {
 
135
      grub_device_iterate (grub_mini_print_devices);
 
136
      grub_putchar ('\n');
 
137
      grub_refresh ();
 
138
    }
 
139
  else
 
140
    {
 
141
      char *device_name;
 
142
      grub_device_t dev;
 
143
      grub_fs_t fs;
 
144
      char *path;
 
145
 
 
146
      device_name = grub_file_get_device_name (argv[0]);
 
147
      dev = grub_device_open (device_name);
 
148
      if (! dev)
 
149
        goto fail;
 
150
 
 
151
      fs = grub_fs_probe (dev);
 
152
      path = grub_strchr (argv[0], ')');
 
153
      if (! path)
 
154
        path = argv[0];
 
155
      else
 
156
        path++;
 
157
 
 
158
      if (! path && ! device_name)
 
159
        {
 
160
          grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
 
161
          goto fail;
 
162
        }
 
163
 
 
164
      if (! path)
 
165
        {
 
166
          if (grub_errno == GRUB_ERR_UNKNOWN_FS)
 
167
            grub_errno = GRUB_ERR_NONE;
 
168
 
 
169
          grub_printf ("(%s): Filesystem is %s.\n",
 
170
                       device_name, fs ? fs->name : "unknown");
 
171
        }
 
172
      else if (fs)
 
173
        {
 
174
          (fs->dir) (dev, path, grub_mini_print_files);
 
175
          grub_putchar ('\n');
 
176
          grub_refresh ();
 
177
        }
 
178
 
 
179
    fail:
 
180
      if (dev)
 
181
        grub_device_close (dev);
 
182
 
 
183
      grub_free (device_name);
 
184
    }
 
185
 
 
186
  return grub_errno;
 
187
}
 
188
 
 
189
void
 
190
grub_register_core_commands (void)
 
191
{
 
192
  grub_register_command ("set", grub_core_cmd_set,
 
193
                         "set [ENVVAR=VALUE]", "set an environment variable");
 
194
  grub_register_command ("unset", grub_core_cmd_unset,
 
195
                         "unset ENVVAR", "remove an environment variable");
 
196
  grub_register_command ("export", grub_core_cmd_export,
 
197
                         "export ENVVAR", "Export a variable.");
 
198
  grub_register_command ("ls", grub_core_cmd_ls,
 
199
                         "ls [ARG]", "list devices or files");
 
200
  grub_register_command ("insmod", grub_core_cmd_insmod,
 
201
                         "insmod MODULE", "insert a module");
 
202
}