Add function for printing command usage
[oweals/u-boot_mod.git] / u-boot / 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  * Support for persistent environment data
29  *
30  * The "environment" is stored as a list of '\0' terminated
31  * "name=value" strings. The end of the list is marked by a double
32  * '\0'. New entries are always added at the end. Deleting an entry
33  * shifts the remaining entries to the front. Replacing an entry is a
34  * combination of deleting the old value and adding the new one.
35  *
36  * The environment is preceeded by a 32 bit CRC over the data part.
37  *
38  **************************************************************************
39  */
40 #include <common.h>
41 #include <command.h>
42 #include <environment.h>
43 #include <serial.h>
44 #include <linux/stddef.h>
45 #include <asm/byteorder.h>
46 #if (CONFIG_COMMANDS & CFG_CMD_NET)
47 #include <net.h>
48 #endif
49
50 DECLARE_GLOBAL_DATA_PTR;
51
52 #if !defined(CFG_ENV_IS_IN_NVRAM)       && \
53     !defined(CFG_ENV_IS_IN_EEPROM)      && \
54     !defined(CFG_ENV_IS_IN_FLASH)       && \
55     !defined(CFG_ENV_IS_IN_DATAFLASH)   && \
56     !defined(CFG_ENV_IS_IN_NAND)        && \
57     !defined(CFG_ENV_IS_NOWHERE)
58         #error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
59 #endif
60
61 #define XMK_STR(x)      #x
62 #define MK_STR(x)       XMK_STR(x)
63
64 /************************************************************************
65  ************************************************************************/
66
67 /* Function that returns a character from the environment */
68 extern uchar (*env_get_char)(int);
69
70 /* Function that returns a pointer to a value from the environment */
71 /* (Only memory version supported / needed). */
72 extern uchar *env_get_addr(int);
73
74 /* Function that updates CRC of the enironment */
75 extern void env_crc_update(void);
76
77 /************************************************************************
78  ************************************************************************/
79
80 static int envmatch(uchar *, int);
81
82 /*
83  * Table with supported baudrates (defined in config_xyz.h)
84  */
85 static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
86 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
87
88 /************************************************************************
89  * Command interface: print one or all environment variables
90  */
91 int do_printenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
92         int i, j, k, nxt;
93         int rcode = 0;
94
95         if(argc == 1){ /* Print all env variables       */
96                 for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
97
98                         for(nxt = i; env_get_char(nxt) != '\0'; ++nxt);
99
100                         for(k = i; k < nxt; ++k){
101                                 putc(env_get_char(k));
102                         }
103
104                         putc('\n');
105
106                         if(ctrlc()){
107                                 puts("\nAbort\n");
108                                 return(1);
109                         }
110                 }
111
112 #if defined(CFG_ENV_IS_IN_NVRAM) || \
113         defined(CFG_ENV_IS_IN_EEPROM) || \
114         ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
115     ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
116                 printf("\nEnvironment size: %d/%d bytes\n\n", i, ENV_SIZE);
117 #else
118                 printf("\nEnvironment size: %d bytes\n\n", i);
119 #endif
120                 return(0);
121         }
122
123         for(i = 1; i < argc; ++i){ /* print single env variables        */
124                 char *name = argv[i];
125
126                 k = -1;
127
128                 for(j = 0; env_get_char(j) != '\0'; j = nxt + 1){
129
130                         for(nxt = j; env_get_char(nxt) != '\0'; ++nxt);
131
132                         k = envmatch((uchar *)name, j);
133
134                         if(k < 0){
135                                 continue;
136                         }
137
138                         puts(name);
139                         putc('=');
140
141                         while(k < nxt){
142                                 putc(env_get_char(k++));
143                         }
144
145                         putc('\n');
146                         break;
147                 }
148                 if(k < 0){
149                         printf("## Error: \"%s\" not defined\n", name);
150                         rcode++;
151                 }
152         }
153         return(rcode);
154 }
155
156 /************************************************************************
157  * Set a new environment variable,
158  * or replace or delete an existing one.
159  *
160  * This function will ONLY work with a in-RAM copy of the environment
161  */
162 int _do_setenv(int flag, int argc, char *argv[]){
163         int i, len, oldval;
164         int console = -1;
165         uchar *env, *nxt = NULL;
166         char *name;
167         bd_t *bd = gd->bd;
168
169         uchar *env_data = env_get_addr(0);
170
171         if(!env_data){ /* need copy in RAM */
172                 return(1);
173         }
174
175         name = argv[1];
176
177         /*
178          * search if variable with this name already exists
179          */
180         oldval = -1;
181
182         for(env = env_data; *env; env = nxt + 1){
183                 for(nxt = env; *nxt; ++nxt);
184
185                 if((oldval = envmatch((uchar *)name, env - env_data)) >= 0){
186                         break;
187                 }
188         }
189
190         /*
191          * Delete any existing definition
192          */
193         if(oldval >= 0){
194                 /* Check for console redirection */
195                 if(strcmp(name, "stdin") == 0){
196                         console = stdin;
197                 } else if(strcmp(name, "stdout") == 0){
198                         console = stdout;
199                 } else if(strcmp(name, "stderr") == 0){
200                         console = stderr;
201                 }
202
203                 if(console != -1){
204                         if(argc < 3){ /* Cannot delete it! */
205                                 printf("## Error: can't delete \"%s\"\n", name);
206                                 return(1);
207                         }
208
209                         /* Try assigning specified device */
210                         if(console_assign(console, argv[2]) < 0){
211                                 return(1);
212                         }
213
214 #ifdef CONFIG_SERIAL_MULTI
215                         if(serial_assign(argv[2]) < 0){
216                                 return(1);
217                         }
218 #endif
219                 }
220
221                 /*
222                  * Switch to new baudrate if new baudrate is supported
223                  */
224                 if(strcmp(argv[1], "baudrate") == 0){
225                         int baudrate = simple_strtoul(argv[2], NULL, 10);
226                         int i;
227
228                         for(i = 0; i < N_BAUDRATES; ++i){
229                                 if(baudrate == baudrate_table[i]){
230                                         break;
231                                 }
232                         }
233
234                         if(i == N_BAUDRATES){
235                                 printf("## Error: baudrate %d bps is not supported,\n", baudrate);
236                                 printf("          choose one from the list:\n\n");
237                                 for(i = 0; i < N_BAUDRATES; ++i){
238                                         printf("- %7d bps%s\n", baudrate_table[i], baudrate_table[i] == gd->baudrate ? " [current]" : "");
239                                 }
240                                 printf("\n");
241                                 return(1);
242                         }
243
244                         printf("Switch baudrate to %d bps and press ENTER...\n", baudrate);
245                         udelay(50000);
246
247                         gd->baudrate = baudrate;
248
249                         serial_setbrg();
250
251                         udelay(50000);
252
253                         for(;;){
254                                 if(getc() == '\r'){
255                                         break;
256                                 }
257                         }
258                 }
259
260                 if(*++nxt == '\0'){
261                         if(env > env_data){
262                                 env--;
263                         } else {
264                                 *env = '\0';
265                         }
266                 } else {
267                         for(;;){
268                                 *env = *nxt++;
269
270                                 if((*env == '\0') && (*nxt == '\0')){
271                                         break;
272                                 }
273
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         /* Delete only ? */
292         if((argc < 3) || argv[2] == NULL){
293                 env_crc_update();
294                 return(0);
295         }
296
297         /*
298          * Append new definition at the end
299          */
300         for(env = env_data; *env || *(env + 1); ++env);
301
302         if(env > env_data){
303                 ++env;
304         }
305         /*
306          * Overflow when:
307          * "name" + "=" + "val" +"\0\0"  > ENV_SIZE - (env-env_data)
308          */
309         len = strlen(name) + 2;
310
311         /* add '=' for first arg, ' ' for all others */
312         for(i = 2; i < argc; ++i){
313                 len += strlen(argv[i]) + 1;
314         }
315
316         if(len > (&env_data[ENV_SIZE] - env)){
317                 printf("## Error: environment overflow, \"%s\" deleted\n", name);
318                 return(1);
319         }
320
321         while((*env = *name++) != '\0'){
322                 env++;
323         }
324
325         for(i = 2; i < argc; ++i){
326                 char *val = argv[i];
327
328                 *env = (i == 2) ? '=' : ' ';
329                 while((*++env = *val++) != '\0');
330         }
331
332         /* end is marked with double '\0' */
333         *++env = '\0';
334
335         /* Update CRC */
336         env_crc_update();
337
338         /*
339          * Some variables should be updated when the corresponding
340          * entry in the enviornment is changed
341          */
342         if(strcmp(argv[1], "ethaddr") == 0){
343                 char *s = argv[2]; /* always use only one arg */
344                 char *e;
345
346                 for(i = 0; i < 6; ++i){
347                         bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
348                         if(s){
349                                 s = (*e) ? e + 1 : e;
350                         }
351                 }
352 #ifdef CONFIG_NET_MULTI
353                 eth_set_enetaddr(0, argv[2]);
354 #endif
355                 return(0);
356         }
357
358         if(strcmp(argv[1], "ipaddr") == 0){
359                 char *s = argv[2]; /* always use only one arg */
360                 char *e;
361                 unsigned long addr;
362
363                 bd->bi_ip_addr = 0;
364
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){
370                                 s = (*e) ? e + 1 : e;
371                         }
372                 }
373
374                 bd->bi_ip_addr = htonl(addr);
375                 return(0);
376         }
377
378         if(strcmp(argv[1], "loadaddr") == 0){
379                 load_addr = simple_strtoul(argv[2], NULL, 16);
380                 return(0);
381         }
382
383 #if (CONFIG_COMMANDS & CFG_CMD_NET)
384         if(strcmp(argv[1], "bootfile") == 0){
385                 copy_filename(BootFile, argv[2], sizeof(BootFile));
386                 return(0);
387         }
388 #endif  /* CFG_CMD_NET */
389
390         return(0);
391 }
392
393 void setenv(char *varname, char *varvalue){
394         char *argv[4] = { "setenv", varname, varvalue, NULL };
395         _do_setenv(0, 3, argv);
396 }
397
398 int do_setenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
399         if(argc < 2){
400                 print_cmd_help(cmdtp);
401                 return(1);
402         }
403
404         return(_do_setenv(flag, argc, argv));
405 }
406
407 /************************************************************************
408  * Prompt for environment variable
409  */
410
411 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
412 int do_askenv( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
413         extern char console_buffer[CFG_CBSIZE];
414         char message[CFG_CBSIZE];
415         int size = CFG_CBSIZE - 1;
416         int len;
417         char *local_args[4];
418
419         local_args[0] = argv[0];
420         local_args[1] = argv[1];
421         local_args[2] = NULL;
422         local_args[3] = NULL;
423
424         if(argc < 2){
425                 print_cmd_help(cmdtp);
426                 return(1);
427         }
428         /* Check the syntax */
429         switch(argc){
430                 case 1:
431                 print_cmd_help(cmdtp);
432                 return(1);
433
434                 case 2: /* askenv envname */
435                 sprintf(message, "Please enter '%s':", argv[1]);
436                 break;
437
438                 case 3: /* askenv envname size */
439                 sprintf(message, "Please enter '%s':", argv[1]);
440                 size = simple_strtoul(argv[2], NULL, 10);
441                 break;
442
443                 default: /* askenv envname message1 ... messagen size */
444                 {
445                         int i;
446                         int pos = 0;
447
448                         for(i = 2; i < argc - 1; i++){
449                                 if(pos){
450                                         message[pos++] = ' ';
451                                 }
452                                 strcpy(message+pos, argv[i]);
453                                 pos += strlen(argv[i]);
454                         }
455                         message[pos] = '\0';
456                         size = simple_strtoul(argv[argc - 1], NULL, 10);
457                 }
458                 break;
459         }
460
461         if(size >= CFG_CBSIZE){
462                 size = CFG_CBSIZE - 1;
463         }
464
465         if(size <= 0){
466                 return(1);
467         }
468
469         /* prompt for input */
470         len = readline(message);
471
472         if(size < len){
473                 console_buffer[size] = '\0';
474         }
475
476         len = 2;
477         if(console_buffer[0] != '\0'){
478                 local_args[2] = console_buffer;
479                 len = 3;
480         }
481
482         /* Continue calling setenv code */
483         return(_do_setenv(flag, len, local_args));
484 }
485 #endif  /* CFG_CMD_ASKENV */
486
487 /************************************************************************
488  * Look up variable from environment,
489  * return address of storage for that variable,
490  * or NULL if not found
491  */
492 char *getenv(char *name){
493         int i, nxt;
494
495         for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
496                 int val;
497
498                 for(nxt = i; env_get_char(nxt) != '\0'; ++nxt){
499                         if(nxt >= CFG_ENV_SIZE){
500                                 return(NULL);
501                         }
502                 }
503                 if((val = envmatch((uchar *)name, i)) < 0){
504                         continue;
505                 }
506                 return((char *)env_get_addr(val));
507         }
508
509         return(NULL);
510 }
511
512 int getenv_r(char *name, char *buf, unsigned len){
513         int i, nxt;
514
515         for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
516                 int val, n;
517
518                 for(nxt = i; env_get_char(nxt) != '\0'; ++nxt){
519                         if(nxt >= CFG_ENV_SIZE){
520                                 return(-1);
521                         }
522                 }
523                 if((val = envmatch((uchar *)name, i)) < 0){
524                         continue;
525                 }
526                 /* found; copy out */
527                 n = 0;
528                 while((len > n++) && (*buf++ = env_get_char(val++)) != '\0');
529
530                 if(len == n){
531                         *buf = '\0';
532                 }
533
534                 return(n);
535         }
536
537         return(-1);
538 }
539
540 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
541     ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
542       (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
543     ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
544       (CFG_CMD_ENV|CFG_CMD_NAND))
545 int do_saveenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
546         extern char * env_name_spec;
547
548         printf("Saving environment to %s...\n\n", env_name_spec);
549
550         return(saveenv() ? 1 : 0);
551 }
552
553 #endif
554
555 /************************************************************************
556  * Match a name / name=value pair
557  *
558  * s1 is either a simple 'name', or a 'name=value' pair.
559  * i2 is the environment index for a 'name2=value2' pair.
560  * If the names match, return the index for the value2, else NULL.
561  */
562 static int envmatch(uchar *s1, int i2){
563
564         while(*s1 == env_get_char(i2++)){
565                 if(*s1++ == '='){
566                         return(i2);
567                 }
568         }
569
570         if(*s1 == '\0' && env_get_char(i2 - 1) == '='){
571                 return(i2);
572         }
573
574         return(-1);
575 }
576
577 /**************************************************/
578 U_BOOT_CMD(printenv, CFG_MAXARGS, 1, do_printenv, "print environment variables\n", "[name]\n\t- print values of all environment variables or of one with name 'name'\n");
579
580 U_BOOT_CMD(setenv, CFG_MAXARGS, 0, do_setenv, "set environment variables\n",
581         "name value ...\n"
582         "\t- set environment variable 'name' to 'value ...'\n"
583         "setenv name\n"
584         "\t- delete environment variable 'name'\n");
585
586 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
587     ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
588       (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
589     ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
590       (CFG_CMD_ENV|CFG_CMD_NAND))
591 U_BOOT_CMD(saveenv, 1, 0, do_saveenv, "save environment variables to FLASH\n", NULL);
592
593 #endif  /* CFG_CMD_ENV */
594
595 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
596
597 U_BOOT_CMD(
598                 askenv, CFG_MAXARGS, 1, do_askenv,
599                 "get environment variables from stdin\n",
600                 "name [message] [size]\n"
601                 "    - get environment variable 'name' from stdin (max 'size' chars)\n"
602                 "askenv name\n"
603                 "    - get environment variable 'name' from stdin\n"
604                 "askenv name size\n"
605                 "    - get environment variable 'name' from stdin (max 'size' chars)\n"
606                 "askenv name [message] size\n"
607                 "    - display 'message' string and get environment variable 'name'"
608                 "from stdin (max 'size' chars)\n"
609 );
610 #endif  /* CFG_CMD_ASKENV */
611
612 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
613 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
614 U_BOOT_CMD(run, CFG_MAXARGS, 1, do_run, "run commands in an environment variable\n", "var [...]\n\t- run the commands in the environment variable(s) 'var'\n");
615 #endif  /* CFG_CMD_RUN */