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