]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_nvedit.c
* Code cleanup:
[people/ms/u-boot.git] / common / cmd_nvedit.c
CommitLineData
a68d3ed0
WD
1/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27/**************************************************************************
28 *
29 * Support for persistent environment data
30 *
31 * The "environment" is stored as a list of '\0' terminated
32 * "name=value" strings. The end of the list is marked by a double
33 * '\0'. New entries are always added at the end. Deleting an entry
34 * shifts the remaining entries to the front. Replacing an entry is a
35 * combination of deleting the old value and adding the new one.
36 *
37 * The environment is preceeded by a 32 bit CRC over the data part.
38 *
39 **************************************************************************
40 */
41
42#include <common.h>
43#include <command.h>
44#include <environment.h>
2a3cb020 45#include <watchdog.h>
a68d3ed0
WD
46#include <linux/stddef.h>
47#include <asm/byteorder.h>
48#if (CONFIG_COMMANDS & CFG_CMD_NET)
49#include <net.h>
50#endif
51
52#if !defined(CFG_ENV_IS_IN_NVRAM) && !defined(CFG_ENV_IS_IN_EEPROM) && !defined(CFG_ENV_IS_IN_FLASH) && !defined(CFG_ENV_IS_NOWHERE)
53# error Define one of CFG_ENV_IS_IN_NVRAM, CFG_ENV_IS_IN_EEPROM, CFG_ENV_IS_IN_FLASH, CFG_ENV_IS_NOWHERE
54#endif
55
56#define XMK_STR(x) #x
57#define MK_STR(x) XMK_STR(x)
58
59/************************************************************************
60************************************************************************/
61
62/* Function that returns a character from the environment */
63extern uchar (*env_get_char)(int);
64
65/* Function that returns a pointer to a value from the environment */
66/* (Only memory version supported / needed). */
67extern uchar *env_get_addr(int);
68
69/* Function that updates CRC of the enironment */
70extern void env_crc_update (void);
71
72/************************************************************************
73************************************************************************/
74
75static int envmatch (uchar *, int);
76
77/*
78 * Table with supported baudrates (defined in config_xyz.h)
79 */
80static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
81#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
82
83
84/************************************************************************
85 * Command interface: print one or all environment variables
86 */
87
88int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
89{
90 int i, j, k, nxt;
91 int rcode = 0;
92
93 if (argc == 1) { /* Print all env variables */
94 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
95 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
96 ;
97 for (k=i; k<nxt; ++k)
98 putc(env_get_char(k));
99 putc ('\n');
100
101 if (ctrlc()) {
102 puts ("\n ** Abort\n");
103 return 1;
104 }
105 }
106
107 printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
108
109 return 0;
110 }
111
112 for (i=1; i<argc; ++i) { /* print single env variables */
113 char *name = argv[i];
114
115 k = -1;
116
117 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
118
119 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
120 ;
121 k = envmatch(name, j);
122 if (k < 0) {
123 continue;
124 }
125 puts (name);
126 putc ('=');
127 while (k < nxt)
128 putc(env_get_char(k++));
129 putc ('\n');
130 break;
131 }
132 if (k < 0) {
133 printf ("## Error: \"%s\" not defined\n", name);
134 rcode ++;
135 }
136 }
137 return rcode;
138}
139
140/************************************************************************
141 * Set a new environment variable,
142 * or replace or delete an existing one.
143 *
144 * This function will ONLY work with a in-RAM copy of the environment
145 */
146
147int _do_setenv (int flag, int argc, char *argv[])
148{
149 DECLARE_GLOBAL_DATA_PTR;
150
151 int i, len, oldval;
152 int console = -1;
153 uchar *env, *nxt = NULL;
154 uchar *name;
155 bd_t *bd = gd->bd;
156
157 uchar *env_data = env_get_addr(0);
158
159 if (!env_data) /* need copy in RAM */
160 return 1;
161
162 name = argv[1];
163
164 /*
165 * search if variable with this name already exists
166 */
167 oldval = -1;
168 for (env=env_data; *env; env=nxt+1) {
169 for (nxt=env; *nxt; ++nxt)
170 ;
171 if ((oldval = envmatch(name, env-env_data)) >= 0)
172 break;
173 }
174
175 /*
176 * Delete any existing definition
177 */
178 if (oldval >= 0) {
179#ifndef CONFIG_ENV_OVERWRITE
180
181 /*
0587597c
SR
182 * Ethernet Address and serial# can be set only once,
183 * ver is readonly.
a68d3ed0
WD
184 */
185 if ( (strcmp (name, "serial#") == 0) ||
0587597c
SR
186#if defined(CONFIG_VERSION_VARIABLE)
187 (strcmp (name, "ver") == 0) ||
188#endif /* CONFIG_VERSION_VARIABLE */
a68d3ed0
WD
189 ((strcmp (name, "ethaddr") == 0)
190#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
191 && (strcmp (env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
192#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
193 ) ) {
194 printf ("Can't overwrite \"%s\"\n", name);
195 return 1;
196 }
197#endif
198
199 /* Check for console redirection */
200 if (strcmp(name,"stdin") == 0) {
201 console = stdin;
202 } else if (strcmp(name,"stdout") == 0) {
203 console = stdout;
204 } else if (strcmp(name,"stderr") == 0) {
205 console = stderr;
206 }
207
208 if (console != -1) {
209 if (argc < 3) { /* Cannot delete it! */
210 printf("Can't delete \"%s\"\n", name);
211 return 1;
212 }
213
214 /* Try assigning specified device */
215 if (console_assign (console, argv[2]) < 0)
216 return 1;
217 }
218
219 /*
220 * Switch to new baudrate if new baudrate is supported
221 */
222 if (strcmp(argv[1],"baudrate") == 0) {
223 int baudrate = simple_strtoul(argv[2], NULL, 10);
224 int i;
225 for (i=0; i<N_BAUDRATES; ++i) {
226 if (baudrate == baudrate_table[i])
227 break;
228 }
229 if (i == N_BAUDRATES) {
230 printf ("## Baudrate %d bps not supported\n",
231 baudrate);
232 return 1;
233 }
234 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
235 baudrate);
236 udelay(50000);
237 gd->baudrate = baudrate;
d0fb80c3
WD
238#ifdef CONFIG_PPC
239 gd->bd->bi_baudrate = baudrate;
240#endif
241
a68d3ed0
WD
242 serial_setbrg ();
243 udelay(50000);
244 for (;;) {
245 if (getc() == '\r')
246 break;
247 }
248 }
249
250 if (*++nxt == '\0') {
251 if (env > env_data) {
252 env--;
253 } else {
254 *env = '\0';
255 }
256 } else {
257 for (;;) {
258 *env = *nxt++;
259 if ((*env == '\0') && (*nxt == '\0'))
260 break;
261 ++env;
262 }
263 }
264 *++env = '\0';
265 }
266
267#ifdef CONFIG_NET_MULTI
268 if (strncmp(name, "eth", 3) == 0) {
269 char *end;
270 int num = simple_strtoul(name+3, &end, 10);
271
272 if (strcmp(end, "addr") == 0) {
273 eth_set_enetaddr(num, argv[2]);
274 }
275 }
276#endif
277
278
279 /* Delete only ? */
280 if ((argc < 3) || argv[2] == NULL) {
281 env_crc_update ();
282 return 0;
283 }
284
285 /*
286 * Append new definition at the end
287 */
288 for (env=env_data; *env || *(env+1); ++env)
289 ;
290 if (env > env_data)
291 ++env;
292 /*
293 * Overflow when:
294 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
295 */
296 len = strlen(name) + 2;
297 /* add '=' for first arg, ' ' for all others */
298 for (i=2; i<argc; ++i) {
299 len += strlen(argv[i]) + 1;
300 }
301 if (len > (&env_data[ENV_SIZE]-env)) {
302 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
303 return 1;
304 }
305 while ((*env = *name++) != '\0')
306 env++;
307 for (i=2; i<argc; ++i) {
308 char *val = argv[i];
309
310 *env = (i==2) ? '=' : ' ';
311 while ((*++env = *val++) != '\0')
312 ;
313 }
314
315 /* end is marked with double '\0' */
316 *++env = '\0';
317
318 /* Update CRC */
319 env_crc_update ();
320
321 /*
322 * Some variables should be updated when the corresponding
323 * entry in the enviornment is changed
324 */
325
326 if (strcmp(argv[1],"ethaddr") == 0) {
327 char *s = argv[2]; /* always use only one arg */
328 char *e;
329 for (i=0; i<6; ++i) {
330 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
331 if (s) s = (*e) ? e+1 : e;
332 }
333#ifdef CONFIG_NET_MULTI
334 eth_set_enetaddr(0, argv[2]);
335#endif
336 return 0;
337 }
338
339 if (strcmp(argv[1],"ipaddr") == 0) {
340 char *s = argv[2]; /* always use only one arg */
341 char *e;
342 unsigned long addr;
343 bd->bi_ip_addr = 0;
344 for (addr=0, i=0; i<4; ++i) {
345 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
346 addr <<= 8;
347 addr |= (val & 0xFF);
348 if (s) s = (*e) ? e+1 : e;
349 }
350 bd->bi_ip_addr = htonl(addr);
351 return 0;
352 }
353 if (strcmp(argv[1],"loadaddr") == 0) {
354 load_addr = simple_strtoul(argv[2], NULL, 16);
355 return 0;
356 }
357#if (CONFIG_COMMANDS & CFG_CMD_NET)
358 if (strcmp(argv[1],"bootfile") == 0) {
359 copy_filename (BootFile, argv[2], sizeof(BootFile));
360 return 0;
361 }
362#endif /* CFG_CMD_NET */
c7de829c 363
0587597c 364#ifdef CONFIG_AMIGAONEG3SE
c7de829c
WD
365 if (strcmp(argv[1], "vga_fg_color") == 0 ||
366 strcmp(argv[1], "vga_bg_color") == 0 ) {
367 extern void video_set_color(unsigned char attr);
368 extern unsigned char video_get_attr(void);
369
370 video_set_color(video_get_attr());
371 return 0;
372 }
373#endif /* CONFIG_AMIGAONEG3SE */
374
a68d3ed0
WD
375 return 0;
376}
377
378void setenv (char *varname, char *varvalue)
379{
380 char *argv[4] = { "setenv", varname, varvalue, NULL };
381 _do_setenv (0, 3, argv);
382}
383
384int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
385{
386 if (argc < 2) {
387 printf ("Usage:\n%s\n", cmdtp->usage);
388 return 1;
389 }
390
391 return _do_setenv (flag, argc, argv);
392}
393
394/************************************************************************
395 * Prompt for environment variable
396 */
397
398#if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
399int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
400{
401 extern char console_buffer[CFG_CBSIZE];
402 char message[CFG_CBSIZE];
403 int size = CFG_CBSIZE - 1;
404 int len;
405 char *local_args[4];
406
407 local_args[0] = argv[0];
408 local_args[1] = argv[1];
409 local_args[2] = NULL;
410 local_args[3] = NULL;
411
412 if (argc < 2) {
413 printf ("Usage:\n%s\n", cmdtp->usage);
414 return 1;
415 }
416 /* Check the syntax */
417 switch (argc) {
418 case 1:
419 printf ("Usage:\n%s\n", cmdtp->usage);
420 return 1;
421
422 case 2: /* askenv envname */
423 sprintf (message, "Please enter '%s':", argv[1]);
424 break;
425
426 case 3: /* askenv envname size */
427 sprintf (message, "Please enter '%s':", argv[1]);
428 size = simple_strtoul (argv[2], NULL, 10);
429 break;
430
431 default: /* askenv envname message1 ... messagen size */
432 {
433 int i;
434 int pos = 0;
435
436 for (i = 2; i < argc - 1; i++) {
437 if (pos) {
438 message[pos++] = ' ';
439 }
440 strcpy (message+pos, argv[i]);
441 pos += strlen(argv[i]);
442 }
443 message[pos] = '\0';
444 size = simple_strtoul (argv[argc - 1], NULL, 10);
445 }
446 break;
447 }
448
449 if (size >= CFG_CBSIZE)
450 size = CFG_CBSIZE - 1;
451
452 if (size <= 0)
453 return 1;
454
455 /* prompt for input */
456 len = readline (message);
457
458 if (size < len)
459 console_buffer[size] = '\0';
460
461 len = 2;
462 if (console_buffer[0] != '\0') {
463 local_args[2] = console_buffer;
464 len = 3;
465 }
466
467 /* Continue calling setenv code */
468 return _do_setenv (flag, len, local_args);
469}
470#endif /* CFG_CMD_ASKENV */
471
472/************************************************************************
473 * Look up variable from environment,
474 * return address of storage for that variable,
475 * or NULL if not found
476 */
477
478char *getenv (uchar *name)
479{
480 int i, nxt;
481
2a3cb020
WD
482 WATCHDOG_RESET();
483
a68d3ed0
WD
484 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
485 int val;
486
487 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
488 if (nxt >= CFG_ENV_SIZE) {
489 return (NULL);
490 }
491 }
492 if ((val=envmatch(name, i)) < 0)
493 continue;
494 return (env_get_addr(val));
495 }
496
497 return (NULL);
498}
499
500int getenv_r (uchar *name, uchar *buf, unsigned len)
501{
502 int i, nxt;
503
504 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
505 int val, n;
506
507 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
508 if (nxt >= CFG_ENV_SIZE) {
509 return (-1);
510 }
511 }
512 if ((val=envmatch(name, i)) < 0)
513 continue;
514 /* found; copy out */
515 n = 0;
516 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
517 ;
518 if (len == n)
519 *buf = '\0';
520 return (n);
521 }
522 return (-1);
523}
524
525#if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
526 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
527 (CFG_CMD_ENV|CFG_CMD_FLASH))
528int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
529{
530 extern char * env_name_spec;
531
532 printf ("Saving Environment to %s...\n", env_name_spec);
533
534 return (saveenv() ? 1 : 0);
535}
8bde7f77
WD
536
537
a68d3ed0
WD
538#endif
539
540
541/************************************************************************
542 * Match a name / name=value pair
543 *
544 * s1 is either a simple 'name', or a 'name=value' pair.
545 * i2 is the environment index for a 'name2=value2' pair.
546 * If the names match, return the index for the value2, else NULL.
547 */
548
549static int
550envmatch (uchar *s1, int i2)
551{
552
553 while (*s1 == env_get_char(i2++))
554 if (*s1++ == '=')
555 return(i2);
556 if (*s1 == '\0' && env_get_char(i2-1) == '=')
557 return(i2);
558 return(-1);
559}
8bde7f77
WD
560
561
562/**************************************************/
563
564cmd_tbl_t U_BOOT_CMD(PRINTENV) = MK_CMD_ENTRY(
565 "printenv", CFG_MAXARGS, 1, do_printenv,
566 "printenv- print environment variables\n",
567 "\n - print values of all environment variables\n"
568 "printenv name ...\n"
569 " - print value of environment variable 'name'\n"
570);
571
572cmd_tbl_t U_BOOT_CMD(SETENV) = MK_CMD_ENTRY(
573 "setenv", CFG_MAXARGS, 0, do_setenv,
574 "setenv - set environment variables\n",
575 "name value ...\n"
576 " - set environment variable 'name' to 'value ...'\n"
577 "setenv name\n"
578 " - delete environment variable 'name'\n"
579);
580
581#if ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
582
583cmd_tbl_t U_BOOT_CMD(SAVEENV) = MK_CMD_ENTRY(
584 "saveenv", 1, 0, do_saveenv,
585 "saveenv - save environment variables to persistent storage\n",
586 NULL
587);
588
589#endif /* CFG_CMD_ENV */
590
591#if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
592
593cmd_tbl_t U_BOOT_CMD(ASKENV) = MK_CMD_ENTRY(
594 "askenv", CFG_MAXARGS, 1, do_askenv,
595 "askenv - get environment variables from stdin\n",
596 "name [message] [size]\n"
597 " - get environment variable 'name' from stdin (max 'size' chars)\n"
598 "askenv name\n"
599 " - get environment variable 'name' from stdin\n"
600 "askenv name size\n"
601 " - get environment variable 'name' from stdin (max 'size' chars)\n"
602 "askenv name [message] size\n"
603 " - display 'message' string and get environment variable 'name'"
604 "from stdin (max 'size' chars)\n"
605);
606#endif /* CFG_CMD_ASKENV */
607
608#if (CONFIG_COMMANDS & CFG_CMD_RUN)
609int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
610cmd_tbl_t U_BOOT_CMD(RUN) = MK_CMD_ENTRY(
611 "run", CFG_MAXARGS, 1, do_run,
612 "run - run commands in an environment variable\n",
613 "var [...]\n"
614 " - run the commands in the environment variable(s) 'var'\n"
615);
616#endif /* CFG_CMD_RUN */