添加了牛皮藓,主要方便不知情的人联系我。有洁癖者自己去除代码,在
[ddnasgpl.git] / common / cmd_nvedit.c
blobc8c1ced177b4006e5fc4ff38d8b12c3cd13db369
1 /*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
9 * project.
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.
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.
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
27 /**************************************************************************
29 * Support for persistent environment data
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.
37 * The environment is preceeded by a 32 bit CRC over the data part.
39 **************************************************************************
42 #include <common.h>
43 #include <command.h>
44 #include <environment.h>
45 #include <watchdog.h>
46 #include <serial.h>
47 #include <linux/stddef.h>
48 #include <asm/byteorder.h>
49 #if (CONFIG_COMMANDS & CFG_CMD_NET)
50 #include <net.h>
51 #endif
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) && \
57 !defined(CFG_ENV_IS_IN_NAND) && \
58 !defined(CFG_ENV_IS_NOWHERE)
59 # error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
60 #endif
62 #define XMK_STR(x) #x
63 #define MK_STR(x) XMK_STR(x)
65 /************************************************************************
66 ************************************************************************/
68 /* Function that returns a character from the environment */
69 extern uchar (*env_get_char)(int);
71 /* Function that returns a pointer to a value from the environment */
72 /* (Only memory version supported / needed). */
73 extern uchar *env_get_addr(int);
75 /* Function that updates CRC of the enironment */
76 extern void env_crc_update (void);
78 /************************************************************************
79 ************************************************************************/
81 static int envmatch (uchar *, int);
84 * Table with supported baudrates (defined in config_xyz.h)
86 static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
87 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
90 /************************************************************************
91 * Command interface: print one or all environment variables
94 int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
96 int i, j, k, nxt;
97 int rcode = 0;
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)
103 for (k=i; k<nxt; ++k)
104 putc(env_get_char(k));
105 putc ('\n');
107 if (ctrlc()) {
108 puts ("\n ** Abort\n");
109 return 1;
113 printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
115 return 0;
118 for (i=1; i<argc; ++i) { /* print single env variables */
119 char *name = argv[i];
121 k = -1;
123 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
125 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
127 k = envmatch((uchar *)name, j);
128 if (k < 0) {
129 continue;
131 puts (name);
132 putc ('=');
133 while (k < nxt)
134 putc(env_get_char(k++));
135 putc ('\n');
136 break;
138 if (k < 0) {
139 printf ("## Error: \"%s\" not defined\n", name);
140 rcode ++;
143 return rcode;
146 /************************************************************************
147 * Set a new environment variable,
148 * or replace or delete an existing one.
150 * This function will ONLY work with a in-RAM copy of the environment
153 int _do_setenv (int flag, int argc, char *argv[])
155 DECLARE_GLOBAL_DATA_PTR;
157 int i, len, oldval;
158 int console = -1;
159 uchar *env, *nxt = NULL;
160 char *name;
161 bd_t *bd = gd->bd;
163 uchar *env_data = env_get_addr(0);
165 if (!env_data) /* need copy in RAM */
166 return 1;
168 name = argv[1];
171 * search if variable with this name already exists
173 oldval = -1;
174 for (env=env_data; *env; env=nxt+1) {
175 for (nxt=env; *nxt; ++nxt)
177 if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
178 break;
182 * Delete any existing definition
184 if (oldval >= 0) {
185 #ifndef CONFIG_ENV_OVERWRITE
188 * Ethernet Address and serial# can be set only once,
189 * ver is readonly.
191 if ( (strcmp (name, "serial#") == 0) ||
192 ((strcmp (name, "ethaddr") == 0)
193 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
194 && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
195 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
196 ) ) {
197 printf ("Can't overwrite \"%s\"\n", name);
198 return 1;
200 #endif
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;
211 if (console != -1) {
212 if (argc < 3) { /* Cannot delete it! */
213 printf("Can't delete \"%s\"\n", name);
214 return 1;
217 /* Try assigning specified device */
218 if (console_assign (console, argv[2]) < 0)
219 return 1;
221 #ifdef CONFIG_SERIAL_MULTI
222 if (serial_assign (argv[2]) < 0)
223 return 1;
224 #endif
228 * Switch to new baudrate if new baudrate is supported
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;
237 if (i == N_BAUDRATES) {
238 printf ("## Baudrate %d bps not supported\n",
239 baudrate);
240 return 1;
242 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
243 baudrate);
244 udelay(50000);
245 gd->baudrate = baudrate;
246 #ifdef CONFIG_PPC
247 gd->bd->bi_baudrate = baudrate;
248 #endif
250 serial_setbrg ();
251 udelay(50000);
252 for (;;) {
253 if (getc() == '\r')
254 break;
258 if (*++nxt == '\0') {
259 if (env > env_data) {
260 env--;
261 } else {
262 *env = '\0';
264 } else {
265 for (;;) {
266 *env = *nxt++;
267 if ((*env == '\0') && (*nxt == '\0'))
268 break;
269 ++env;
272 *++env = '\0';
275 #ifdef CONFIG_NET_MULTI
276 if (strncmp(name, "eth", 3) == 0) {
277 char *end;
278 int num = simple_strtoul(name+3, &end, 10);
280 if (strcmp(end, "addr") == 0) {
281 eth_set_enetaddr(num, argv[2]);
284 #endif
287 /* Delete only ? */
288 if ((argc < 3) || argv[2] == NULL) {
289 env_crc_update ();
290 return 0;
293 * Append new definition at the end
295 for (env=env_data; *env || *(env+1); ++env)
297 if (env > env_data)
298 ++env;
300 * Overflow when:
301 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
303 len = strlen(name) + 2;
304 /* add '=' for first arg, ' ' for all others */
305 for (i=2; i<argc; ++i) {
306 len += strlen(argv[i]) + 1;
308 if (len > (&env_data[ENV_SIZE]-env)) {
309 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
310 return 1;
312 while ((*env = *name++) != '\0')
313 env++;
314 for (i=2; i<argc; ++i) {
315 char *val = argv[i];
317 *env = (i==2) ? '=' : ' ';
318 while ((*++env = *val++) != '\0')
322 /* end is marked with double '\0' */
323 *++env = '\0';
325 /* Update CRC */
326 env_crc_update ();
329 * Some variables should be updated when the corresponding
330 * entry in the enviornment is changed
333 if (strcmp(argv[1],"ethaddr") == 0) {
334 char *s = argv[2]; /* always use only one arg */
335 char *e;
336 for (i=0; i<6; ++i) {
337 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
338 if (s) s = (*e) ? e+1 : e;
340 #ifdef CONFIG_NET_MULTI
341 eth_set_enetaddr(0, argv[2]);
342 #endif
343 return 0;
346 if (strcmp(argv[1],"ipaddr") == 0) {
347 char *s = argv[2]; /* always use only one arg */
348 char *e;
349 unsigned long addr;
350 bd->bi_ip_addr = 0;
351 for (addr=0, i=0; i<4; ++i) {
352 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
353 addr <<= 8;
354 addr |= (val & 0xFF);
355 if (s) s = (*e) ? e+1 : e;
357 bd->bi_ip_addr = htonl(addr);
358 return 0;
360 if (strcmp(argv[1],"loadaddr") == 0) {
361 load_addr = simple_strtoul(argv[2], NULL, 16);
362 return 0;
364 #if (CONFIG_COMMANDS & CFG_CMD_NET)
365 if (strcmp(argv[1],"bootfile") == 0) {
366 copy_filename (BootFile, argv[2], sizeof(BootFile));
367 return 0;
369 #endif /* CFG_CMD_NET */
371 #ifdef CONFIG_AMIGAONEG3SE
372 if (strcmp(argv[1], "vga_fg_color") == 0 ||
373 strcmp(argv[1], "vga_bg_color") == 0 ) {
374 extern void video_set_color(unsigned char attr);
375 extern unsigned char video_get_attr(void);
377 video_set_color(video_get_attr());
378 return 0;
380 #endif /* CONFIG_AMIGAONEG3SE */
382 return 0;
385 void setenv (char *varname, char *varvalue)
387 char *argv[4] = { "setenv", varname, varvalue, NULL };
388 _do_setenv (0, 3, argv);
391 int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
393 if (argc < 2) {
394 printf ("Usage:\n%s\n", cmdtp->usage);
395 return 1;
398 return _do_setenv (flag, argc, argv);
401 /************************************************************************
402 * Prompt for environment variable
405 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
406 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
408 extern char console_buffer[CFG_CBSIZE];
409 char message[CFG_CBSIZE];
410 int size = CFG_CBSIZE - 1;
411 int len;
412 char *local_args[4];
414 local_args[0] = argv[0];
415 local_args[1] = argv[1];
416 local_args[2] = NULL;
417 local_args[3] = NULL;
419 if (argc < 2) {
420 printf ("Usage:\n%s\n", cmdtp->usage);
421 return 1;
423 /* Check the syntax */
424 switch (argc) {
425 case 1:
426 printf ("Usage:\n%s\n", cmdtp->usage);
427 return 1;
429 case 2: /* askenv envname */
430 sprintf (message, "Please enter '%s':", argv[1]);
431 break;
433 case 3: /* askenv envname size */
434 sprintf (message, "Please enter '%s':", argv[1]);
435 size = simple_strtoul (argv[2], NULL, 10);
436 break;
438 default: /* askenv envname message1 ... messagen size */
440 int i;
441 int pos = 0;
443 for (i = 2; i < argc - 1; i++) {
444 if (pos) {
445 message[pos++] = ' ';
447 strcpy (message+pos, argv[i]);
448 pos += strlen(argv[i]);
450 message[pos] = '\0';
451 size = simple_strtoul (argv[argc - 1], NULL, 10);
453 break;
456 if (size >= CFG_CBSIZE)
457 size = CFG_CBSIZE - 1;
459 if (size <= 0)
460 return 1;
462 /* prompt for input */
463 len = readline (message);
465 if (size < len)
466 console_buffer[size] = '\0';
468 len = 2;
469 if (console_buffer[0] != '\0') {
470 local_args[2] = console_buffer;
471 len = 3;
474 /* Continue calling setenv code */
475 return _do_setenv (flag, len, local_args);
477 #endif /* CFG_CMD_ASKENV */
479 /************************************************************************
480 * Look up variable from environment,
481 * return address of storage for that variable,
482 * or NULL if not found
485 char *getenv (char *name)
487 int i, nxt;
489 WATCHDOG_RESET();
491 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
492 int val;
494 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
495 if (nxt >= CFG_ENV_SIZE) {
496 return (NULL);
499 if ((val=envmatch((uchar *)name, i)) < 0)
500 continue;
501 return ((char *)env_get_addr(val));
504 return (NULL);
507 int getenv_r (char *name, char *buf, unsigned len)
509 int i, nxt;
511 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
512 int val, n;
514 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
515 if (nxt >= CFG_ENV_SIZE) {
516 return (-1);
519 if ((val=envmatch((uchar *)name, i)) < 0)
520 continue;
521 /* found; copy out */
522 n = 0;
523 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
525 if (len == n)
526 *buf = '\0';
527 return (n);
529 return (-1);
532 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || defined(CFG_ENV_IS_IN_NAND) || \
533 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
534 (CFG_CMD_ENV|CFG_CMD_FLASH))
535 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
537 extern char * env_name_spec;
539 printf ("Saving Environment to %s...\n", env_name_spec);
541 return (saveenv() ? 1 : 0);
545 #endif
548 /************************************************************************
549 * Match a name / name=value pair
551 * s1 is either a simple 'name', or a 'name=value' pair.
552 * i2 is the environment index for a 'name2=value2' pair.
553 * If the names match, return the index for the value2, else NULL.
556 static int
557 envmatch (uchar *s1, int i2)
560 while (*s1 == env_get_char(i2++))
561 if (*s1++ == '=')
562 return(i2);
563 if (*s1 == '\0' && env_get_char(i2-1) == '=')
564 return(i2);
565 return(-1);
569 /**************************************************/
571 U_BOOT_CMD(
572 printenv, CFG_MAXARGS, 1, do_printenv,
573 "printenv- print environment variables\n",
574 "\n - print values of all environment variables\n"
575 "printenv name ...\n"
576 " - print value of environment variable 'name'\n"
579 U_BOOT_CMD(
580 setenv, CFG_MAXARGS, 0, do_setenv,
581 "setenv - set environment variables\n",
582 "name value ...\n"
583 " - set environment variable 'name' to 'value ...'\n"
584 "setenv name\n"
585 " - delete environment variable 'name'\n"
588 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || defined(CFG_ENV_IS_IN_NAND) || \
589 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
590 (CFG_CMD_ENV|CFG_CMD_FLASH))
591 U_BOOT_CMD(
592 saveenv, 1, 0, do_saveenv,
593 "saveenv - save environment variables to persistent storage\n",
594 NULL
597 #endif /* CFG_CMD_ENV */
599 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
601 U_BOOT_CMD(
602 askenv, CFG_MAXARGS, 1, do_askenv,
603 "askenv - get environment variables from stdin\n",
604 "name [message] [size]\n"
605 " - get environment variable 'name' from stdin (max 'size' chars)\n"
606 "askenv name\n"
607 " - get environment variable 'name' from stdin\n"
608 "askenv name size\n"
609 " - get environment variable 'name' from stdin (max 'size' chars)\n"
610 "askenv name [message] size\n"
611 " - display 'message' string and get environment variable 'name'"
612 "from stdin (max 'size' chars)\n"
614 #endif /* CFG_CMD_ASKENV */
616 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
617 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
618 U_BOOT_CMD(
619 run, CFG_MAXARGS, 1, do_run,
620 "run - run commands in an environment variable\n",
621 "var [...]\n"
622 " - run the commands in the environment variable(s) 'var'\n"
624 #endif /* CFG_CMD_RUN */