]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_nvedit.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.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 #ifdef CONFIG_CONSOLE_MUX
217                         i = iomux_doenv(console, argv[2]);
218                         if (i)
219                                 return i;
220 #else
221                         /* Try assigning specified device */
222                         if (console_assign (console, argv[2]) < 0)
223                                 return 1;
224
225 #ifdef CONFIG_SERIAL_MULTI
226                         if (serial_assign (argv[2]) < 0)
227                                 return 1;
228 #endif
229 #endif /* CONFIG_CONSOLE_MUX */
230                 }
231
232                 /*
233                  * Switch to new baudrate if new baudrate is supported
234                  */
235                 if (strcmp(argv[1],"baudrate") == 0) {
236                         int baudrate = simple_strtoul(argv[2], NULL, 10);
237                         int i;
238                         for (i=0; i<N_BAUDRATES; ++i) {
239                                 if (baudrate == baudrate_table[i])
240                                         break;
241                         }
242                         if (i == N_BAUDRATES) {
243                                 printf ("## Baudrate %d bps not supported\n",
244                                         baudrate);
245                                 return 1;
246                         }
247                         printf ("## Switch baudrate to %d bps and press ENTER ...\n",
248                                 baudrate);
249                         udelay(50000);
250                         gd->baudrate = baudrate;
251 #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
252                         gd->bd->bi_baudrate = baudrate;
253 #endif
254
255                         serial_setbrg ();
256                         udelay(50000);
257                         for (;;) {
258                                 if (getc() == '\r')
259                                       break;
260                         }
261                 }
262
263                 if (*++nxt == '\0') {
264                         if (env > env_data) {
265                                 env--;
266                         } else {
267                                 *env = '\0';
268                         }
269                 } else {
270                         for (;;) {
271                                 *env = *nxt++;
272                                 if ((*env == '\0') && (*nxt == '\0'))
273                                         break;
274                                 ++env;
275                         }
276                 }
277                 *++env = '\0';
278         }
279
280 #ifdef CONFIG_NET_MULTI
281         if (strncmp(name, "eth", 3) == 0) {
282                 char *end;
283                 int   num = simple_strtoul(name+3, &end, 10);
284
285                 if (strcmp(end, "addr") == 0) {
286                         eth_set_enetaddr(num, argv[2]);
287                 }
288         }
289 #endif
290
291
292         /* Delete only ? */
293         if ((argc < 3) || argv[2] == NULL) {
294                 env_crc_update ();
295                 return 0;
296         }
297
298         /*
299          * Append new definition at the end
300          */
301         for (env=env_data; *env || *(env+1); ++env)
302                 ;
303         if (env > env_data)
304                 ++env;
305         /*
306          * Overflow when:
307          * "name" + "=" + "val" +"\0\0"  > ENV_SIZE - (env-env_data)
308          */
309         len = strlen(name) + 2;
310         /* add '=' for first arg, ' ' for all others */
311         for (i=2; i<argc; ++i) {
312                 len += strlen(argv[i]) + 1;
313         }
314         if (len > (&env_data[ENV_SIZE]-env)) {
315                 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
316                 return 1;
317         }
318         while ((*env = *name++) != '\0')
319                 env++;
320         for (i=2; i<argc; ++i) {
321                 char *val = argv[i];
322
323                 *env = (i==2) ? '=' : ' ';
324                 while ((*++env = *val++) != '\0')
325                         ;
326         }
327
328         /* end is marked with double '\0' */
329         *++env = '\0';
330
331         /* Update CRC */
332         env_crc_update ();
333
334         /*
335          * Some variables should be updated when the corresponding
336          * entry in the enviornment is changed
337          */
338
339         if (strcmp(argv[1],"ethaddr") == 0) {
340                 char *s = argv[2];      /* always use only one arg */
341                 char *e;
342                 for (i=0; i<6; ++i) {
343                         bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
344                         if (s) s = (*e) ? e+1 : e;
345                 }
346 #ifdef CONFIG_NET_MULTI
347                 eth_set_enetaddr(0, argv[2]);
348 #endif
349                 return 0;
350         }
351
352         if (strcmp(argv[1],"ipaddr") == 0) {
353                 char *s = argv[2];      /* always use only one arg */
354                 char *e;
355                 unsigned long addr;
356                 bd->bi_ip_addr = 0;
357                 for (addr=0, i=0; i<4; ++i) {
358                         ulong val = s ? simple_strtoul(s, &e, 10) : 0;
359                         addr <<= 8;
360                         addr  |= (val & 0xFF);
361                         if (s) s = (*e) ? e+1 : e;
362                 }
363                 bd->bi_ip_addr = htonl(addr);
364                 return 0;
365         }
366         if (strcmp(argv[1],"loadaddr") == 0) {
367                 load_addr = simple_strtoul(argv[2], NULL, 16);
368                 return 0;
369         }
370 #if defined(CONFIG_CMD_NET)
371         if (strcmp(argv[1],"bootfile") == 0) {
372                 copy_filename (BootFile, argv[2], sizeof(BootFile));
373                 return 0;
374         }
375 #endif
376
377 #ifdef CONFIG_AMIGAONEG3SE
378         if (strcmp(argv[1], "vga_fg_color") == 0 ||
379             strcmp(argv[1], "vga_bg_color") == 0 ) {
380                 extern void video_set_color(unsigned char attr);
381                 extern unsigned char video_get_attr(void);
382
383                 video_set_color(video_get_attr());
384                 return 0;
385         }
386 #endif  /* CONFIG_AMIGAONEG3SE */
387
388         return 0;
389 }
390
391 int setenv (char *varname, char *varvalue)
392 {
393         char *argv[4] = { "setenv", varname, varvalue, NULL };
394         if (varvalue == NULL)
395                 return _do_setenv (0, 2, argv);
396         else
397                 return _do_setenv (0, 3, argv);
398 }
399
400 #ifdef CONFIG_HAS_UID
401 void forceenv (char *varname, char *varvalue)
402 {
403         char *argv[4] = { "forceenv", varname, varvalue, NULL };
404         _do_setenv (0xdeaf4add, 3, argv);
405 }
406 #endif
407
408 int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
409 {
410         if (argc < 2) {
411                 cmd_usage(cmdtp);
412                 return 1;
413         }
414
415         return _do_setenv (flag, argc, argv);
416 }
417
418 /************************************************************************
419  * Prompt for environment variable
420  */
421
422 #if defined(CONFIG_CMD_ASKENV)
423 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
424 {
425         extern char console_buffer[CONFIG_SYS_CBSIZE];
426         char message[CONFIG_SYS_CBSIZE];
427         int size = CONFIG_SYS_CBSIZE - 1;
428         int len;
429         char *local_args[4];
430
431         local_args[0] = argv[0];
432         local_args[1] = argv[1];
433         local_args[2] = NULL;
434         local_args[3] = NULL;
435
436         if (argc < 2) {
437                 cmd_usage(cmdtp);
438                 return 1;
439         }
440         /* Check the syntax */
441         switch (argc) {
442         case 1:
443                 cmd_usage(cmdtp);
444                 return 1;
445
446         case 2:         /* askenv envname */
447                 sprintf (message, "Please enter '%s':", argv[1]);
448                 break;
449
450         case 3:         /* askenv envname size */
451                 sprintf (message, "Please enter '%s':", argv[1]);
452                 size = simple_strtoul (argv[2], NULL, 10);
453                 break;
454
455         default:        /* askenv envname message1 ... messagen size */
456             {
457                 int i;
458                 int pos = 0;
459
460                 for (i = 2; i < argc - 1; i++) {
461                         if (pos) {
462                                 message[pos++] = ' ';
463                         }
464                         strcpy (message+pos, argv[i]);
465                         pos += strlen(argv[i]);
466                 }
467                 message[pos] = '\0';
468                 size = simple_strtoul (argv[argc - 1], NULL, 10);
469             }
470                 break;
471         }
472
473         if (size >= CONFIG_SYS_CBSIZE)
474                 size = CONFIG_SYS_CBSIZE - 1;
475
476         if (size <= 0)
477                 return 1;
478
479         /* prompt for input */
480         len = readline (message);
481
482         if (size < len)
483                 console_buffer[size] = '\0';
484
485         len = 2;
486         if (console_buffer[0] != '\0') {
487                 local_args[2] = console_buffer;
488                 len = 3;
489         }
490
491         /* Continue calling setenv code */
492         return _do_setenv (flag, len, local_args);
493 }
494 #endif
495
496 /************************************************************************
497  * Look up variable from environment,
498  * return address of storage for that variable,
499  * or NULL if not found
500  */
501
502 char *getenv (char *name)
503 {
504         int i, nxt;
505
506         WATCHDOG_RESET();
507
508         for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
509                 int val;
510
511                 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
512                         if (nxt >= CONFIG_ENV_SIZE) {
513                                 return (NULL);
514                         }
515                 }
516                 if ((val=envmatch((uchar *)name, i)) < 0)
517                         continue;
518                 return ((char *)env_get_addr(val));
519         }
520
521         return (NULL);
522 }
523
524 int getenv_r (char *name, char *buf, unsigned len)
525 {
526         int i, nxt;
527
528         for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
529                 int val, n;
530
531                 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
532                         if (nxt >= CONFIG_ENV_SIZE) {
533                                 return (-1);
534                         }
535                 }
536                 if ((val=envmatch((uchar *)name, i)) < 0)
537                         continue;
538                 /* found; copy out */
539                 n = 0;
540                 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
541                         ;
542                 if (len == n)
543                         *buf = '\0';
544                 return (n);
545         }
546         return (-1);
547 }
548
549 #if defined(CONFIG_CMD_ENV) && !defined(CONFIG_ENV_IS_NOWHERE)
550
551 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
552 {
553         extern char * env_name_spec;
554
555         printf ("Saving Environment to %s...\n", env_name_spec);
556
557         return (saveenv() ? 1 : 0);
558 }
559
560 U_BOOT_CMD(
561         saveenv, 1, 0,  do_saveenv,
562         "save environment variables to persistent storage",
563         NULL
564 );
565
566 #endif
567
568
569 /************************************************************************
570  * Match a name / name=value pair
571  *
572  * s1 is either a simple 'name', or a 'name=value' pair.
573  * i2 is the environment index for a 'name2=value2' pair.
574  * If the names match, return the index for the value2, else NULL.
575  */
576
577 int envmatch (uchar *s1, int i2)
578 {
579
580         while (*s1 == env_get_char(i2++))
581                 if (*s1++ == '=')
582                         return(i2);
583         if (*s1 == '\0' && env_get_char(i2-1) == '=')
584                 return(i2);
585         return(-1);
586 }
587
588
589 /**************************************************/
590
591 U_BOOT_CMD(
592         printenv, CONFIG_SYS_MAXARGS, 1,        do_printenv,
593         "print environment variables",
594         "\n    - print values of all environment variables\n"
595         "printenv name ...\n"
596         "    - print value of environment variable 'name'\n"
597 );
598
599 U_BOOT_CMD(
600         setenv, CONFIG_SYS_MAXARGS, 0,  do_setenv,
601         "set environment variables",
602         "name value ...\n"
603         "    - set environment variable 'name' to 'value ...'\n"
604         "setenv name\n"
605         "    - delete environment variable 'name'\n"
606 );
607
608 #if defined(CONFIG_CMD_ASKENV)
609
610 U_BOOT_CMD(
611         askenv, CONFIG_SYS_MAXARGS,     1,      do_askenv,
612         "get environment variables from stdin",
613         "name [message] [size]\n"
614         "    - get environment variable 'name' from stdin (max 'size' chars)\n"
615         "askenv name\n"
616         "    - get environment variable 'name' from stdin\n"
617         "askenv name size\n"
618         "    - get environment variable 'name' from stdin (max 'size' chars)\n"
619         "askenv name [message] size\n"
620         "    - display 'message' string and get environment variable 'name'"
621         "from stdin (max 'size' chars)\n"
622 );
623 #endif
624
625 #if defined(CONFIG_CMD_RUN)
626 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
627 U_BOOT_CMD(
628         run,    CONFIG_SYS_MAXARGS,     1,      do_run,
629         "run commands in an environment variable",
630         "var [...]\n"
631         "    - run the commands in the environment variable(s) 'var'\n"
632 );
633 #endif