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