Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / commands / loadenv.c
blob4dbf1d93c326f954990d6ba49bc3f50947349f28
1 /* loadenv.c - command to load/save environment variable. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
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.
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.
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/>.
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/partition.h>
29 #include <grub/lib/envblk.h>
31 static const struct grub_arg_option options[] =
33 {"file", 'f', 0, "specify filename", 0, ARG_TYPE_PATHNAME},
34 {0, 0, 0, 0, 0, 0}
37 char buffer[GRUB_ENVBLK_MAXLEN];
38 grub_envblk_t envblk;
40 static grub_file_t
41 read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length))
43 char *buf = 0;
44 grub_file_t file;
46 if (! filename)
48 char *prefix;
50 prefix = grub_env_get ("prefix");
51 if (prefix)
53 int len;
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;
62 else
64 grub_error (GRUB_ERR_FILE_NOT_FOUND, "prefix is not found");
65 return 0;
69 file = grub_file_open (filename);
70 grub_free (buf);
71 if (! file)
72 return 0;
74 if (read_hook)
76 if (! file->device->disk)
78 grub_file_close (file);
79 grub_error (GRUB_ERR_BAD_DEVICE,
80 "this command is available only for disk devices.");
81 return 0;
83 file->read_hook = read_hook;
86 if (grub_file_read (file, buffer, GRUB_ENVBLK_MAXLEN) != GRUB_ENVBLK_MAXLEN)
88 grub_file_close (file);
89 grub_error (GRUB_ERR_BAD_FILE_TYPE, "file too short");
90 return 0;
93 envblk = grub_envblk_find (buffer);
94 if (! envblk)
96 grub_file_close (file);
97 grub_error (GRUB_ERR_BAD_FILE_TYPE, "environment block not found");
98 return 0;
101 return file;
104 static grub_err_t
105 grub_cmd_load_env (struct grub_arg_list *state,
106 int argc __attribute__ ((unused)), char **args __attribute__ ((unused)))
109 grub_file_t file;
111 auto int hook (char *name, char *value);
112 int hook (char *name, char *value)
114 grub_env_set (name, value);
116 return 0;
119 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
120 if (! file)
121 return grub_errno;
123 grub_file_close (file);
125 grub_envblk_iterate (envblk, hook);
127 return grub_errno;
130 static grub_err_t
131 grub_cmd_list_env (struct grub_arg_list *state,
132 int argc __attribute__ ((unused)), char **args __attribute__ ((unused)))
134 grub_file_t file;
136 auto int hook (char *name, char *value);
137 int hook (char *name, char *value)
139 grub_printf ("%s=%s\n", name, value);
141 return 0;
144 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, 0);
145 if (! file)
146 return grub_errno;
148 grub_file_close (file);
150 grub_envblk_iterate (envblk, hook);
152 return grub_errno;
155 static grub_err_t
156 grub_cmd_save_env (struct grub_arg_list *state, int argc, char **args)
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;
165 auto void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, unsigned offset,
166 unsigned length);
168 void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector,
169 unsigned offset, unsigned length)
171 if ((offset != 0) || (length != GRUB_DISK_SECTOR_SIZE))
172 return;
174 if (num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS))
175 addr[num++] = sector;
178 if (! argc)
179 return grub_error (GRUB_ERR_BAD_ARGUMENT, "No variable is specified");
181 file = read_envblk_file ((state[0].set) ? state[0].arg : 0, hook);
182 if (! file)
183 return grub_errno;
185 file->read_hook = 0;
187 if (num != GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS)
189 grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
190 goto quit;
193 disk = file->device->disk;
194 if (disk->partition)
195 part_start = grub_partition_get_start (disk->partition);
197 for (num = 0; num < (GRUB_ENVBLK_MAXLEN >> GRUB_DISK_SECTOR_BITS); num++)
199 if (grub_disk_read (disk, addr[num] - part_start, 0,
200 GRUB_DISK_SECTOR_SIZE, buf))
201 goto quit;
203 if (grub_memcmp (&buffer[num << GRUB_DISK_SECTOR_BITS], buf,
204 GRUB_DISK_SECTOR_SIZE))
206 grub_error (GRUB_ERR_BAD_DEVICE, "invalid blocklist");
207 goto quit;
211 while (argc)
213 char *value;
215 value = grub_env_get (args[0]);
216 if (value)
218 if (grub_envblk_insert (envblk, args[0], value))
220 grub_error (GRUB_ERR_BAD_ARGUMENT, "environment block too small");
221 goto quit;
225 argc--;
226 args++;
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;
235 quit:
236 grub_file_close (file);
238 return grub_errno;
241 GRUB_MOD_INIT(loadenv)
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);
252 GRUB_MOD_FINI(loadenv)
254 grub_unregister_command ("load_env");
255 grub_unregister_command ("list_env");
256 grub_unregister_command ("save_env");