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