Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / util / grub-editenv.c
blob68caf42635a30a50bbabe6d4bcde884ffaa51a75
1 /* grub-editenv.c - tool to edit environment block. */
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 <config.h>
21 #include <grub/types.h>
22 #include <grub/util/misc.h>
23 #include <grub/lib/envblk.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <getopt.h>
31 void
32 grub_putchar (int c)
34 putchar (c);
37 void
38 grub_refresh (void)
40 fflush (stdout);
43 void *
44 grub_term_get_current_input (void)
46 return 0;
49 void *
50 grub_term_get_current_output (void)
52 return 0;
55 int
56 grub_getkey (void)
58 return 0;
61 char *
62 grub_env_get (const char *name __attribute__ ((unused)))
64 return NULL;
67 static struct option options[] = {
68 {"help", no_argument, 0, 'h'},
69 {"version", no_argument, 0, 'V'},
70 {"verbose", no_argument, 0, 'v'},
71 {0, 0, 0, 0}
74 char buffer[GRUB_ENVBLK_MAXLEN];
75 grub_envblk_t envblk;
77 static void
78 usage (int status)
80 if (status)
81 fprintf (stderr, "Try ``grub-editenv --help'' for more information.\n");
82 else
83 printf ("\
84 Usage: grub-editenv [OPTIONS] FILENAME COMMAND\n\
85 \n\
86 Tool to edit environment block.\n\
87 \nCommands:\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\
93 \nOptions:\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\
97 \n\
98 Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
100 exit (status);
104 create_envblk_file (char *name)
106 FILE *f;
107 grub_envblk_t p;
109 f = fopen (name, "wb");
110 if (! f)
111 return 1;
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);
123 fclose (f);
124 return 0;
127 FILE *
128 open_envblk_file (char *name)
130 FILE *f;
132 f = fopen (name, "r+b");
133 if (! f)
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);
140 if (! envblk)
141 grub_util_error ("Can\'t find environment block");
143 return f;
146 static void
147 cmd_info (void)
149 printf ("Envblk offset: %ld\n", (long) (envblk->data - buffer));
150 printf ("Envblk length: %d\n", envblk->length);
153 static void
154 cmd_list (void)
156 auto int hook (char *name, char *value);
157 int hook (char *name, char *value)
159 printf ("%s=%s\n", name, value);
160 return 0;
163 grub_envblk_iterate (envblk, hook);
166 static void
167 cmd_set (int argc, char *argv[])
169 while (argc)
171 char *p;
173 p = strchr (argv[0], '=');
174 if (! p)
175 grub_util_error ("Invalid parameter");
177 *(p++) = 0;
179 if (*p)
181 if (grub_envblk_insert (envblk, argv[0], p))
182 grub_util_error ("Environment block too small");
184 else
185 grub_envblk_delete (envblk, argv[0]);
187 argc--;
188 argv++;
192 static void
193 cmd_clear (void)
195 envblk->data[0] = envblk->data[1] = 0;
199 main (int argc, char *argv[])
201 FILE *f;
203 progname = "grub-editenv";
205 /* Check for options. */
206 while (1)
208 int c = getopt_long (argc, argv, "hVv", options, 0);
210 if (c == -1)
211 break;
212 else
213 switch (c)
215 case 'h':
216 usage (0);
217 break;
219 case 'V':
220 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
221 return 0;
223 case 'v':
224 verbosity++;
225 break;
227 default:
228 usage (1);
229 break;
233 /* Obtain PATH. */
234 if (optind >= argc)
236 fprintf (stderr, "Filename not specified.\n");
237 usage (1);
240 if (optind + 1 >= argc)
242 fprintf (stderr, "Command not specified.\n");
243 usage (1);
246 if (! strcmp (argv[optind + 1], "create"))
247 return create_envblk_file (argv[optind]);
249 f = open_envblk_file (argv[optind]);
251 optind++;
252 if (! strcmp (argv[optind], "info"))
253 cmd_info ();
254 else if (! strcmp (argv[optind], "list"))
255 cmd_list ();
256 else
258 if (! strcmp (argv[optind], "set"))
259 cmd_set (argc - optind - 1, argv + optind + 1);
260 else if (! strcmp (argv[optind], "clear"))
261 cmd_clear ();
263 fseek (f, 0, SEEK_SET);
264 fwrite (buffer, sizeof (buffer), 1, f);
266 fclose (f);
268 return 0;