summaryrefslogtreecommitdiff
path: root/avr/env.c
blob: 28437a433ccba7286eac672f9c2e8737b0e20085 (plain)
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
#include "common.h"
#include <string.h>
#include <stdlib.h>

#include <avr/eeprom.h>

#include "config.h"
#include "debug.h"
#include "xmalloc.h"
#include "crc.h"
#include "command.h"

#include "env.h"


#define ENV_SIZE	(CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
#define ACTIVE_FLAG	1
#define OBSOLETE_FLAG	0
#define ENV_GET_VAL	(1<<0)


/*
 * Default Environment
 */

#define DELIM 		"\0"

const FLASH char default_env[] = {
	"bootdelay="	"3"		DELIM
	"bootcmd="	"reset; loadf; go $(startaddr)"	DELIM
	"baudrate="	"115200"	DELIM
	"startaddr="	"0"		DELIM
	DELIM
};

/* EEPROM storage */
typedef struct environment_s {
	uint16_t	crc;		/* CRC16 over data bytes	*/
	uint8_t		flags;		/* active/obsolete flags	*/
	char		data[ENV_SIZE]; /* Environment data		*/
} env_t;


/* */
typedef struct env_item_s {
#define EF_N_EEP	(1<<7)		/* Variable name is in EEPROM */
#define EF_V_EEP	(1<<6)		/* Variable value is in EEPROM */
#define EF_DIRTY	(1<<0)		/* Variable is new or value changed */
	uint8_t flags;
	union {
		uint16_t eep;
		char	 *ram;
	} name;
	union {
		uint16_t eep;
		char	 *ram;
	} val;
} env_item_t;


static uint8_t env_valid;
static env_item_t env_list[CONFIG_ENVVAR_MAX];
static int entrycount;


static
char env_get_char(uint16_t index)
{
	unsigned int off = CONFIG_ENV_OFFSET;

	if (env_valid == 2)
		off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;

	return (char) eeprom_read_byte((const uint8_t *)off + index + 
				offsetof(env_t, data));
}


static
uint16_t varname_get(char *buf, env_item_t *ep)
{
	int i = 0;
	char c;
	
	if (ep->flags & EF_N_EEP) {

		while ((c = env_get_char(ep->name.eep + i)) != '=' &&
				c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) {

			buf[i] = c;
			i++;
		}
		if (i > 0 && c != '=') {
			debug("** ee_name: '%s' not '=' terminated!\n", buf);
		}
	} else {
		strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN);
		i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN);
	}
	buf[i] = '\0';
	return i;
}


static
uint16_t varval_getlen(uint16_t index)
{
	int i = 0;
	char c;

	while ((c = env_get_char (index + i)) != '\0') {
		i++;
	};

	return i;
}

static
uint16_t varval_get(char *buf, uint16_t index, int len)
{
	int i = 0;
	char c;
	
	while ((c = env_get_char (index + i)) != '\0' && i < len) {
		buf[i] = c;
		i++;
	};
		
	buf[i] = '\0';
	
	/* TODO: len check */
	
	return i;
}

static
int comp_env_key_item(const void *key, const void *item)
{
	char buf[CONFIG_SYS_ENV_NAMELEN+1];
	env_item_t *ep = (env_item_t *) item;
	
	varname_get(buf, ep);
	
	return strcmp((char *) key, buf);
}

static
int comp_env_items(const void *m1, const void *m2)
{
	char b1[CONFIG_SYS_ENV_NAMELEN+1];
	char b2[CONFIG_SYS_ENV_NAMELEN+1];
	
	env_item_t *ep1 = (env_item_t *) m1;
	env_item_t *ep2 = (env_item_t *) m2;
	
	varname_get(b1, ep1);
	varname_get(b2, ep2);
	
	return strcmp(b1, b2);
}


static
int envlist_import(void)
{
	char name[CONFIG_SYS_ENV_NAMELEN+1];
	uint16_t idx = 0;
	int nlen;
	env_item_t e;
	
	e.flags = EF_N_EEP | EF_V_EEP;
	e.name.eep = idx;
	while ((nlen = varname_get(name, &e)) != 0 && idx < ENV_SIZE) {
	
		if (entrycount <= CONFIG_ENVVAR_MAX) {
			e.val.eep = idx + nlen + 1;
	
			env_list[entrycount++] = e;

			idx += nlen + 1;
			while (env_get_char(idx++) != 0 && idx < ENV_SIZE)
				;
			e.name.eep = idx;
		} else {
			debug("** Too many environment variables!\n");
			break;
		}
	}
	qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items);

	return 0;
}


static
int set_default_env(void)
{
	char buf[56];
	uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data);
	unsigned int len = ENV_SIZE;
	unsigned int i, src = 0;
	char c = 0xff, c0 = c;

	if (env_valid == 1) {
		eep = CONFIG_ENV_OFFSET + offsetof(env_t, data) + CONFIG_ENV_SIZE;
	}

	while (len)  {
		memcpy_P(buf, default_env+src, sizeof(buf));
		for (i=0; i < (len < sizeof(buf) ? len : sizeof(buf)) &&
				!(c == 0 && c0 == 0);
				i++) {
			c0 = c; c = buf[i];
		}
		eeprom_update_block(buf, (char *) eep, i);
		if (c == 0 && c0 == 0)
			len = 0;
		if (len > sizeof(buf))
			len -= sizeof(buf);
		src += sizeof(buf);
		eep += sizeof(buf);
	}

	return 0;
}


static
uint16_t env_crc(uint16_t data_offset)
{
	uint16_t crc; 
	uint16_t i;
	char c, c0;

	crc = 0xffff;
	c = 0xff;
	for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++)
	{
		c0 = c; 
		c = eeprom_read_byte((const uint8_t *) data_offset + i);
		crc = crc16(crc, c);
	}
	return crc ;
}


/**
 * return valid env
 */
static
int env_check_valid(void)
{
	const uint16_t offset[2] = {CONFIG_ENV_OFFSET,
				    CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE};
	uint_fast8_t flags[2], crc_ok[2];
	int rc;

	/* read FLAGS */
	flags[0] = eeprom_read_byte ((uint8_t *) offset[0] +
				offsetof(env_t, flags));
	flags[1] = eeprom_read_byte ((uint8_t *) offset[1] +
				offsetof(env_t, flags));

	/* check CRC */
	crc_ok[0] = (
		eeprom_read_word((uint16_t *) offset[0] +
				offsetof(env_t, crc))
		== env_crc(offset[0] + offsetof(env_t, data))
	);
	crc_ok[1] = (
		eeprom_read_word((uint16_t *) offset[1] +
				offsetof(env_t, crc))
		== env_crc(offset[1] + offsetof(env_t, data))
	);

	if (!crc_ok[0] && !crc_ok[1]) {
		rc = 0;

	} else if (crc_ok[0] && !crc_ok[1]) {
		rc = 1;
	} else if (!crc_ok[0] && crc_ok[1]) {
		rc = 2;
	} else {
		/* both ok - check serial */
#if 1
		if      (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG)
			rc = 2;
		else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF)
			rc = 2;
		else
			rc = 1;
#else
		if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
			rc = 1;
		else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
			rc = 2;
		else if (flags[0] == 0xFF && flags[1] == 0)
			rc = 2;
		else if (flags[1] == 0xFF && flags[0] == 0)
			rc = 1;
		else /* flags are equal - almost impossible */
			rc = 1;
#endif
	}
	
	return rc;
}


int env_init(void)
{
	env_valid = env_check_valid();
	if (env_valid == 0) {
		printf_P(PSTR("*** Warning - bad CRC, "
				"using default environment\n\n"));
		set_default_env();
	}
	entrycount = 0;
	envlist_import();
	return 0; 
}	


static
env_item_t *envlist_search(const char *name)
{
	return bsearch(name, env_list, entrycount, 
				sizeof(env_item_t), comp_env_key_item);
}
			

static
env_item_t *envlist_enter(env_item_t *e)
{
	char *key = e->name.ram;
	const size_t size = sizeof(env_item_t);
	env_item_t *ep;
	
	ep = bsearch(key, env_list, entrycount, 
			size, comp_env_key_item);
					
	if (ep == NULL) {
		if (entrycount < CONFIG_ENVVAR_MAX) {
			
			env_list[entrycount++] = *e;
			qsort(env_list, entrycount, size, comp_env_items);
			ep = bsearch(key, env_list, entrycount, 
						size, comp_env_key_item);
		}
	} else { 
		if ((ep->flags & EF_V_EEP) == 0) {
			free(ep->val.ram);
		}
		ep->val.ram = e->val.ram;
	}		

	if (ep != NULL) {
		ep->flags |= EF_DIRTY;
		ep->flags &= ~EF_V_EEP;
		
		if ((ep->flags & EF_N_EEP) == 0) {
			int nlen = strnlen(key, CONFIG_SYS_ENV_NAMELEN);
			char *name = xmalloc(nlen + 1);
			if (name == NULL) {
				printf_P(PSTR("## Can't malloc %d bytes\n"), 
					nlen + 1);
				return NULL;
			}
			strcpy(name, key);
			name[nlen] = '\0';
			ep->name.ram = name;
		}
	}

	return ep;		
}	


static
int env_item_delete(env_item_t *ep)
{
	if (entrycount == 0)
		return -1;
	if ((ep->flags & EF_V_EEP) == 0) 
		free(ep->val.ram);
	if ((ep->flags & EF_N_EEP) == 0) 
		free(ep->name.ram); 

	entrycount--;
	size_t size = sizeof(env_item_t);
	memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);

	return 0;		
}

static
int envlist_delete(const char *name)
{
	env_item_t *ep = bsearch(name, env_list, entrycount, 
				sizeof(env_item_t), comp_env_key_item);

	if (ep != NULL)
		return env_item_delete(ep);

	return 1;
}

	
static
env_item_t *envlist_get(const char *name, uint_fast8_t flag)
{
	env_item_t *ep;
	
	ep = envlist_search(name);

	if (ep != NULL && (flag & ENV_GET_VAL)) {
		if (ep->flags & EF_V_EEP) {
			char *vp;
			int len;
			len = varval_getlen(ep->val.eep);
			if (len > CONFIG_SYS_CBSIZE)
				len = CONFIG_SYS_CBSIZE;
			vp = xmalloc(len + 1);
			if (vp) {
				varval_get(vp, ep->val.eep, len + 1);
				ep->val.ram = vp;
				ep->flags &= ~EF_V_EEP;
			} else
				printf_P(PSTR("Out of memory!\n"));
		}
	}			
	
	return ep;
}


char *getenv(const char *name)
{
	env_item_t *ep;
	char *ret = NULL;

	ep = envlist_get(name, ENV_GET_VAL);
	if (ep != NULL)
		ret = ep->val.ram;
	return ret;
}

static
int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
{
	char buf[CONFIG_SYS_ENV_NAMELEN+1];
	int len;
	env_item_t e = *ep;

	len = varname_get(buf, ep);
	if (len == 0)
		return 0;
	buf[len++] = '=';
	space_left -= len;

	if (space_left <= 0)
		return 0;
	
	eeprom_update_block(buf, (uint8_t *) offset, len);
	offset += len;
	
	if (e.val.ram != NULL) {
		char c;
		do {
			if (e.flags & EF_V_EEP) 
				c = env_get_char(e.val.eep++);
			else
				c = *e.val.ram++;
			
			eeprom_update_byte((uint8_t *) offset, c);
			offset++;
			space_left--;
			len++;
		} while ((c != '\0') && space_left );
	}
	return len;
}	


static
int saveenv(void)
{
	unsigned int off     = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
	unsigned int off_red = CONFIG_ENV_OFFSET;
	unsigned int pos;
	int len, left;
	uint16_t crc;
	int rc = 0;

	if (env_valid == 2) {
		off	= CONFIG_ENV_OFFSET;
		off_red	= CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
	}

	eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); 

	pos = off + offsetof(env_t, data);
	left = ENV_SIZE - 1;
	for (int i = 0 ; i < entrycount; i++) {
		len = env_item_save(&env_list[i], pos, left);
		if (len == 0) {
			return 1;
		}
		pos += len;
		left -= len;
	}
	/* terminate list */
	eeprom_update_byte((uint8_t *) pos, 0);
	crc = env_crc(off + offsetof(env_t, data));
	eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc);
	eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 
					ACTIVE_FLAG);

	if (rc == 0) {
		while (entrycount != 0) {
			env_item_delete(&env_list[entrycount-1]);
		}
		eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), 
					OBSOLETE_FLAG);
		env_valid = (env_valid == 2) ? 1 : 2;

		envlist_import();
	}
	
	return rc;
}


static
int env_item_print(env_item_t *ep)
{
	char buf[CONFIG_SYS_ENV_NAMELEN+1];
	int len;
	env_item_t e = *ep;
	
	varname_get(buf, ep);
	len = printf_P(PSTR("%s="), buf);
	
	if (e.val.ram != NULL) {
		while (1) {
			char c;
			if (e.flags & EF_V_EEP) 
				c = env_get_char(e.val.eep++);
			else
				c = *e.val.ram++;
			
			if (c != '\0') {
				putchar(c);
				len++;
			} else
				break;
		}
	}
	putchar('\n');
	len ++;

	return len;
}	


/*
 * Command interface: print one or all environment variables
 *
 * Returns -1 in case of error, or length of printed string
 */
static 
int env_print(char *name)
{
	int len = -1;

	if (name) {		/* print a single name */
	
		env_item_t *ep = envlist_search(name);
		if (ep != NULL)
			len = env_item_print(ep);

	} else {		/* print whole list */
		len = 0;
		for (int i = 0 ; i < entrycount; i++) {
			len += env_item_print(&env_list[i]);
		}
	}
	return len;
}

static
int env_print_ramsize(void)
{
	int size = 0;
	uint8_t name_cnt = 0;
	uint8_t val_cnt = 0;
	
	for (int i = 0 ; i < entrycount; i++) {
		if ((env_list[i].flags & EF_N_EEP) == 0 &&
				(env_list[i].name.ram != NULL)) {
			name_cnt++;
			size += strlen(env_list[i].name.ram) + 3;
		}
		if ((env_list[i].flags & EF_V_EEP) == 0 &&
				(env_list[i].val.ram != NULL)) {
			val_cnt++;
			size += strlen(env_list[i].val.ram) + 3;
		}
	}
	printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
			size, name_cnt, val_cnt);
	return size;
}


command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	command_ret_t rc = CMD_RET_SUCCESS;

	(void) cmdtp; (void) flag;

	if (argc == 1) {
		/* print all env vars */
		int size = env_print(NULL);
		if (size < 0)
			return CMD_RET_FAILURE;
		printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
			size, ENV_SIZE);
		env_print_ramsize();
		return CMD_RET_SUCCESS;
	}

	/* print selected env vars */
	for (int i = 1; i < argc; ++i) {
		int rc = env_print(argv[i]);
		if (rc < 0) {
			printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
			rc = CMD_RET_FAILURE;
		}
	}

	return rc;
}


/**
 * Set or delete environment variable
 *
 * Set a new environment variable,
 * or replace or delete an existing one.
 *
 * @param flag		(not used)
 * @param argc
 * @param argv[1]	Environment variable to set or delete
 *			if no more args
 * @param argv[2] ...	Value to set it to
 *
 * @return 0 if ok, 1 on error
 */
static
command_ret_t _do_env_set(int flag, int argc, char * const argv[])
{
	int i, len;
	char *name, *value, *s;
	env_item_t e, *ep;

	(void) flag;
	debug("Initial value for argc=%d\n", argc);

	name = argv[1];
	value = argv[2];

	if (strchr(name, '=')) {
		printf_P(PSTR("## Error: illegal character '='"
		       "in variable name \"%s\"\n"), name);
		return CMD_RET_FAILURE;
	}
	if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) {
		printf_P(PSTR("## Error: Variable name \"%s\" too long. "
		       "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
		return CMD_RET_FAILURE;
	}
/*
	env_id++;
*/
	/* Delete only ? */
	if (argc < 3 || argv[2] == NULL) {
		int rc = envlist_delete(name);
		return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
	}

	/*
	 * Insert / replace new value
	 */
	for (i = 2, len = 0; i < argc; ++i)
		len += strlen(argv[i]) + 1;

	value = xmalloc(len);
	if (value == NULL) {
		printf_P(PSTR("## Can't malloc %d bytes\n"), len);
		return CMD_RET_FAILURE;
	}
	for (i = 2, s = value; i < argc; ++i) {
		char *v = argv[i];

		while ((*s++ = *v++) != '\0')
			;
		*(s - 1) = ' ';
	}
	if (s != value)
		*--s = '\0';

	e.flags = EF_DIRTY;
	e.name.ram = name;
	e.val.ram = value;
	ep = envlist_enter(&e);
	if (!ep) {
		printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
			name);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

/**
 * Set an environment variable
 *
 * @param varname	Environment variable to set
 * @param varvalue	Value to set it to
 * @return 0 if ok, 1 on error
 */
static
int setenv(const char *varname, const char *varvalue)
{
	const char * const argv[3] = { NULL, varname, varvalue };
	int argc = 3;

	if (varvalue == NULL || varvalue[0] == '\0')
		--argc;
		
	return (int) _do_env_set(0, argc, (char * const *)argv);
}

#if 0
/**
 * Set an environment variable to an integer value
 *
 * @param varname	Environment variable to set
 * @param value		Value to set it to
 * @return 0 if ok, 1 on error
 */
int setenv_ulong(const char *varname, unsigned long value)
{
	/* TODO: this should be unsigned */
	char *str = simple_itoa(value);

	return setenv(varname, str);
}
#endif


/**
 * Set an environment variable to an value in hex
 *
 * @param varname	Environment variable to set
 * @param value		Value to set it to
 * @return 0 if ok, 1 on error
 */
int setenv_hex(const char *varname, unsigned long value)
{
	char str[sizeof(unsigned long) *2 + 1];

	sprintf_P(str, PSTR("%lx"), value);
	return setenv(varname, str);
}

/**
 * Get an environment variable as a hex value
 *
 * @param varname	Environment variable to get
 * @param default_val	Return this, if variable does not exist
 * @return hex value of variable or default_val
 */
unsigned long getenv_hex(const char *varname, unsigned long default_val)
{
	const char *s;
	unsigned long value;
	char *endp;

	s = getenv(varname);
	if (s)
		value = strtoul(s, &endp, 16);
	if (!s || endp == s)
		return default_val;

	return value;
}

command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	(void) cmdtp;

	if (argc < 2)
		return CMD_RET_USAGE;

	return _do_env_set(flag, argc, argv);
}


command_ret_t do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	(void) cmdtp; (void) flag; (void) argc;	(void) argv;

	printf_P(PSTR("Saving Environment ...\n"));
	return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
}

#if defined(CONFIG_AUTO_COMPLETE) 
int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
{
	ENTRY *match;
	int found, idx;

	idx = 0;
	found = 0;
	cmdv[0] = NULL;

	while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
		int vallen = strlen(match->key) + 1;

		if (found >= maxv - 2 || bufsz < vallen)
			break;

		cmdv[found++] = buf;
		memcpy(buf, match->key, vallen);
		buf += vallen;
		bufsz -= vallen;
	}

	qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);

	if (idx)
		cmdv[found++] = "...";

	cmdv[found] = NULL;
	return found;
}
#endif