1 /* loadenv.c - command to load/save environment variable. */
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>
24 #include <grub/file.h>
25 #include <grub/disk.h>
26 #include <grub/misc.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
},
37 char buffer
[GRUB_ENVBLK_MAXLEN
];
41 read_envblk_file (char *filename
, void NESTED_FUNC_ATTR
read_hook (grub_disk_addr_t sector
, unsigned offset
, unsigned length
))
50 prefix
= grub_env_get ("prefix");
55 len
= grub_strlen (prefix
);
56 buf
= grub_malloc (len
+ 1 + sizeof (GRUB_ENVBLK_DEFCFG
));
57 grub_strcpy (buf
, prefix
);
59 grub_strcpy (buf
+ len
+ 1, GRUB_ENVBLK_DEFCFG
);
64 grub_error (GRUB_ERR_FILE_NOT_FOUND
, "prefix is not found");
69 file
= grub_file_open (filename
);
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.");
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");
93 envblk
= grub_envblk_find (buffer
);
96 grub_file_close (file
);
97 grub_error (GRUB_ERR_BAD_FILE_TYPE
, "environment block not found");
105 grub_cmd_load_env (struct grub_arg_list
*state
,
106 int argc
__attribute__ ((unused
)), char **args
__attribute__ ((unused
)))
111 auto int hook (char *name
, char *value
);
112 int hook (char *name
, char *value
)
114 grub_env_set (name
, value
);
119 file
= read_envblk_file ((state
[0].set
) ? state
[0].arg
: 0, 0);
123 grub_file_close (file
);
125 grub_envblk_iterate (envblk
, hook
);
131 grub_cmd_list_env (struct grub_arg_list
*state
,
132 int argc
__attribute__ ((unused
)), char **args
__attribute__ ((unused
)))
136 auto int hook (char *name
, char *value
);
137 int hook (char *name
, char *value
)
139 grub_printf ("%s=%s\n", name
, value
);
144 file
= read_envblk_file ((state
[0].set
) ? state
[0].arg
: 0, 0);
148 grub_file_close (file
);
150 grub_envblk_iterate (envblk
, hook
);
156 grub_cmd_save_env (struct grub_arg_list
*state
, int argc
, char **args
)
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;
165 auto void NESTED_FUNC_ATTR
hook (grub_disk_addr_t sector
, unsigned offset
,
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
))
174 if (num
< (GRUB_ENVBLK_MAXLEN
>> GRUB_DISK_SECTOR_BITS
))
175 addr
[num
++] = sector
;
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
);
187 if (num
!= GRUB_ENVBLK_MAXLEN
>> GRUB_DISK_SECTOR_BITS
)
189 grub_error (GRUB_ERR_BAD_DEVICE
, "invalid blocklist");
193 disk
= file
->device
->disk
;
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
))
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");
215 value
= grub_env_get (args
[0]);
218 if (grub_envblk_insert (envblk
, args
[0], value
))
220 grub_error (GRUB_ERR_BAD_ARGUMENT
, "environment block too small");
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
]))
236 grub_file_close (file
);
241 GRUB_MOD_INIT(loadenv
)
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");