]> git.proxmox.com Git - grub2.git/blame - commands/loadenv.c
2008-07-02 Bean <bean123ch@gmail.com>
[grub2.git] / commands / loadenv.c
CommitLineData
2270f77b 1/* loadenv.c - command to load/save environment variable. */
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/normal.h>
21#include <grub/dl.h>
22#include <grub/mm.h>
23#include <grub/arg.h>
24#include <grub/file.h>
25#include <grub/disk.h>
26#include <grub/misc.h>
27#include <grub/env.h>
28#include <grub/envblk.h>
29#include <grub/partition.h>
30
31static const struct grub_arg_option options[] =
32 {
33 {"file", 'f', 0, "specify filename", 0, ARG_TYPE_PATHNAME},
34 {0, 0, 0, 0, 0, 0}
35 };
36
37char buffer[GRUB_ENVBLK_MAXLEN];
38grub_envblk_t envblk;
39
40static grub_file_t
41read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length))
42{
43 char *buf = 0;
44 grub_file_t file;
45
46 if (! filename)
47 {
48 char *prefix;
49
50 prefix = grub_env_get ("prefix");
51 if (prefix)
52 {
53 int len;
54
55 len = grub_strlen (prefix);
56 buf = grub_malloc (len + 1 + sizeof (GRUB_ENVBLK_DEFCFG));
57 grub_strcpy (buf, prefix);
58 buf[len] = '/';
59 grub_strcpy (buf + len + 1, GRUB_ENVBLK_DEFCFG);
60 filename = buf;
61 }
62 else
63 {
64 grub_error (GRUB_ERR_FILE_NOT_FOUND, "prefix is not found");
65 return 0;
66 }
67 }
68
69 file = grub_file_open (filename);
70 grub_free (buf);
71 if (! file)
72 return 0;
73
74 if (read_hook)
75 {
76 if (! file->device->disk)
77 {
78 grub_file_close (file);
79 grub_error (GRUB_ERR_BAD_DEVICE,
80 "this command is available only for disk devices.");
81 return 0;
82 }
83 file->read_hook = read_hook;
84 }
85
86 if (grub_file_read (file, buffer, GRUB_ENVBLK_MAXLEN) != GRUB_ENVBLK_MAXLEN)
87 {
88 grub_file_close (file);
89 grub_error (GRUB_ERR_BAD_FILE_TYPE, "file too short");
90 return 0;
91 }
92
93 envblk = grub_envblk_find (buffer);
94 if (! envblk)
95 {
96 grub_file_close (file);
97 grub_error (GRUB_ERR_BAD_FILE_TYPE, "environment block not found");
98 return 0;
99 }
100
101 return file;
102}
103
104static grub_err_t
105grub_cmd_load_env (struct grub_arg_list *state,
106 int argc __attribute__ ((unused)), char **args __attribute__ ((unused)))
107
108{
109 grub_file_t file;
110
111 auto int hook (char *name, char *value);
112 int hook (char *name, char *value)
113 {
114 grub_env_set (name, value);
115
116 return 0;
117 }
118
119 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
120 if (! file)
121 return grub_errno;
122
123 grub_file_close (file);
124
125 grub_envblk_iterate (envblk, hook);
126
127 return grub_errno;
128}
129
130static grub_err_t
131grub_cmd_list_env (struct grub_arg_list *state,
132 int argc __attribute__ ((unused)), char **args __attribute__ ((unused)))
133{
134 grub_file_t file;
135
136 auto int hook (char *name, char *value);
137 int hook (char *name, char *value)
138 {
139 grub_printf ("%s=%s\n", name, value);
140
141 return 0;
142 }
143
144 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
145 if (! file)
146 return grub_errno;
147
148 grub_file_close (file);
149
150 grub_envblk_iterate (envblk, hook);
151
152 return grub_errno;
153}
154
155static grub_err_t
156grub_cmd_save_env (struct grub_arg_list *state, int argc, char **args)
157{
158 grub_file_t file;
159 grub_disk_t disk;
160 grub_disk_addr_t addr[GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS];
161 char buf[GRUB_DISK_SECTOR_SIZE];
162 grub_disk_addr_t part_start = 0;
163 int num = 0;
164
165 auto void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, unsigned offset,
166 unsigned length);
167
168 void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector,
169 unsigned offset, unsigned length)
170 {
171 if ((offset != 0) || (length != GRUB_DISK_SECTOR_SIZE))
172 return;
173
174 if (num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS))
175 addr[num++] = sector;
176 }
177
178 if (! argc)
179 return grub_error (GRUB_ERR_BAD_ARGUMENT, "No variable is specified");
180
181 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, hook);
182 if (! file)
183 return grub_errno;
184
185 file->read_hook = 0;
186
187 if (num != GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS)
188 {
189 grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
190 goto quit;
191 }
192
193 disk = file->device->disk;
194 if (disk->partition)
195 part_start = grub_partition_get_start (disk->partition);
196
197 for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
198 {
199 if (grub_disk_read (disk, addr[num] - part_start, 0,
200 GRUB_DISK_SECTOR_SIZE, buf))
201 goto quit;
202
203 if (grub_memcmp (&buffer[num << GRUB_DISK_SECTOR_BITS], buf,
204 GRUB_DISK_SECTOR_SIZE))
205 {
206 grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
207 goto quit;
208 }
209 }
210
211 while (argc)
212 {
213 char *value;
214
215 value = grub_env_get (args[0]);
216 if (value)
217 {
218 if (grub_envblk_insert (envblk, args[0], value))
219 {
220 grub_error (GRUB_ERR_BAD_ARGUMENT, "environment block too small");
221 goto quit;
222 }
223 }
224
225 argc--;
226 args++;
227 }
228
229 for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
230 if (grub_disk_write (disk, addr[num] - part_start, 0,
231 GRUB_DISK_SECTOR_SIZE,
232 &buffer[num << GRUB_DISK_SECTOR_BITS]))
233 goto quit;
234
235quit:
236 grub_file_close (file);
237
238 return grub_errno;
239}
240
241GRUB_MOD_INIT(loadenv)
242{
243 (void) mod;
244 grub_register_command ("load_env", grub_cmd_load_env, GRUB_COMMAND_FLAG_BOTH,
245 "load_env [-f FILE]", "Load variables from environment block file.", options);
246 grub_register_command ("list_env", grub_cmd_list_env, GRUB_COMMAND_FLAG_BOTH,
247 "list_env [-f FILE]", "List variables from environment block file.", options);
248 grub_register_command ("save_env", grub_cmd_save_env, GRUB_COMMAND_FLAG_BOTH,
249 "save_env [-f FILE] variable_name [...]", "Save variables to environment block file.", options);
250}
251
252GRUB_MOD_FINI(loadenv)
253{
254 grub_unregister_command ("load_env");
255 grub_unregister_command ("list_env");
256 grub_unregister_command ("save_env");
257}