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