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