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