1 /* grub-editenv.c - tool to edit environment block. */
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/>.
21 #include <grub/types.h>
22 #include <grub/util/misc.h>
23 #include <grub/lib/envblk.h>
44 grub_term_get_current_input (void)
50 grub_term_get_current_output (void)
62 grub_env_get (const char *name
__attribute__ ((unused
)))
67 static struct option options
[] = {
68 {"help", no_argument
, 0, 'h'},
69 {"version", no_argument
, 0, 'V'},
70 {"verbose", no_argument
, 0, 'v'},
74 char buffer
[GRUB_ENVBLK_MAXLEN
];
81 fprintf (stderr
, "Try ``grub-editenv --help'' for more information.\n");
84 Usage: grub-editenv [OPTIONS] FILENAME COMMAND\n\
86 Tool to edit environment block.\n\
88 create create a blank environment block file\n\
89 info show information about the environment block\n\
90 list list the current variables\n\
91 set [name=value] ... change/delete variables\n\
92 clear delete all variables\n\
94 -h, --help display this message and exit\n\
95 -V, --version print version information and exit\n\
96 -v, --verbose print verbose messages\n\
98 Report bugs to <%s>.\n", PACKAGE_BUGREPORT
);
104 create_envblk_file (char *name
)
109 f
= fopen (name
, "wb");
113 /* Just in case OS don't save 0s. */
114 memset (buffer
, -1, sizeof (buffer
));
116 p
= (grub_envblk_t
) &buffer
[0];
117 p
->signature
= GRUB_ENVBLK_SIGNATURE
;
118 p
->length
= sizeof (buffer
) - sizeof (struct grub_envblk
);
119 p
->data
[0] = p
->data
[1] = 0;
121 fwrite (buffer
, sizeof (buffer
), 1, f
);
128 open_envblk_file (char *name
)
132 f
= fopen (name
, "r+b");
134 grub_util_error ("Can\'t open file %s", name
);
136 if (fread (buffer
, 1, sizeof (buffer
), f
) != sizeof (buffer
))
137 grub_util_error ("The envblk file is too short");
139 envblk
= grub_envblk_find (buffer
);
141 grub_util_error ("Can\'t find environment block");
149 printf ("Envblk offset: %ld\n", (long) (envblk
->data
- buffer
));
150 printf ("Envblk length: %d\n", envblk
->length
);
156 auto int hook (char *name
, char *value
);
157 int hook (char *name
, char *value
)
159 printf ("%s=%s\n", name
, value
);
163 grub_envblk_iterate (envblk
, hook
);
167 cmd_set (int argc
, char *argv
[])
173 p
= strchr (argv
[0], '=');
175 grub_util_error ("Invalid parameter");
181 if (grub_envblk_insert (envblk
, argv
[0], p
))
182 grub_util_error ("Environment block too small");
185 grub_envblk_delete (envblk
, argv
[0]);
195 envblk
->data
[0] = envblk
->data
[1] = 0;
199 main (int argc
, char *argv
[])
203 progname
= "grub-editenv";
205 /* Check for options. */
208 int c
= getopt_long (argc
, argv
, "hVv", options
, 0);
220 printf ("%s (%s) %s\n", progname
, PACKAGE_NAME
, PACKAGE_VERSION
);
236 fprintf (stderr
, "Filename not specified.\n");
240 if (optind
+ 1 >= argc
)
242 fprintf (stderr
, "Command not specified.\n");
246 if (! strcmp (argv
[optind
+ 1], "create"))
247 return create_envblk_file (argv
[optind
]);
249 f
= open_envblk_file (argv
[optind
]);
252 if (! strcmp (argv
[optind
], "info"))
254 else if (! strcmp (argv
[optind
], "list"))
258 if (! strcmp (argv
[optind
], "set"))
259 cmd_set (argc
- optind
- 1, argv
+ optind
+ 1);
260 else if (! strcmp (argv
[optind
], "clear"))
263 fseek (f
, 0, SEEK_SET
);
264 fwrite (buffer
, sizeof (buffer
), 1, f
);