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