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