]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/env.c
Debugging code moved to debug.c
[z180-stamp.git] / avr / env.c
1 #include "common.h"
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include <avr/eeprom.h>
6
7 #include "config.h"
8 #include "debug.h"
9 #include "xmalloc.h"
10 #include "crc.h"
11 #include "command.h"
12
13 #include "env.h"
14
15
16 #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
17 #define ACTIVE_FLAG 1
18 #define OBSOLETE_FLAG 0
19
20
21 #define DELIM "\0"
22
23
24 #define ENV_GET_VAL (1<<0)
25
26
27 /*
28 * Default Environment
29 */
30 const FLASH char default_env[] = {
31 "bootdelay=" "3" DELIM
32 "bootcmd=" "reset; loadf; go $(startaddr)" DELIM
33 "baudrate=" "115200" DELIM
34 "startaddr=" "0" DELIM
35 DELIM
36 };
37
38
39
40
41 typedef struct environment_s {
42 uint16_t crc; /* CRC16 over data bytes */
43 uint8_t flags; /* active/obsolete flags */
44 char data[ENV_SIZE]; /* Environment data */
45 } env_t;
46
47
48
49 typedef struct env_item_s {
50 #define EF_N_EEP (1<<7) /* Variable name is in EEPROM */
51 #define EF_V_EEP (1<<6) /* Variable value is in EEPROM */
52 #define EF_DIRTY (1<<0) /* Variable is new or value changed */
53 uint8_t flags;
54 union {
55 uint16_t eep;
56 char *ram;
57 } name;
58 union {
59 uint16_t eep;
60 char *ram;
61 } val;
62 } env_item_t;
63
64 static uint8_t env_valid;
65 static env_item_t env_list[CONFIG_ENVVAR_MAX];
66 static int entrycount;
67
68
69 static char env_get_char(uint16_t index)
70 {
71 unsigned int off = CONFIG_ENV_OFFSET;
72
73 if (env_valid == 2)
74 off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
75
76 return (char) eeprom_read_byte((const uint8_t *)off + index +
77 offsetof(env_t, data));
78 }
79
80
81 uint16_t ee_name_get(char *buf, env_item_t *ep)
82 {
83 int i = 0;
84 char c;
85
86 if (ep->flags & EF_N_EEP) {
87
88 while ((c = env_get_char(ep->name.eep + i)) != '=' &&
89 c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) {
90
91 buf[i] = c;
92 i++;
93 }
94 if (i > 0 && c != '=') {
95 debug("** ee_name: '%s' not '=' terminated!\n", buf);
96 }
97 } else {
98 strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN);
99 i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN);
100 }
101 buf[i] = '\0';
102 return i;
103 }
104
105
106 uint16_t ee_val_get(char *buf, uint16_t index, int len)
107 {
108 int i = 0;
109 char c;
110
111 while ((c = env_get_char (index + i)) != '\0' && i < len) {
112 buf[i] = c;
113 i++;
114 };
115
116 buf[i] = '\0';
117
118 /* TODO: len check */
119
120 return i;
121 }
122
123 static
124 int comp_env_key_item(const void *key, const void *item)
125 {
126 char buf[CONFIG_SYS_ENV_NAMELEN+1];
127 env_item_t *ep = (env_item_t *) item;
128
129 ee_name_get(buf, ep);
130
131 return strcmp((char *) key, buf);
132 }
133
134 static
135 int comp_env_items(const void *m1, const void *m2)
136 {
137 char b1[CONFIG_SYS_ENV_NAMELEN+1];
138 char b2[CONFIG_SYS_ENV_NAMELEN+1];
139
140 env_item_t *ep1 = (env_item_t *) m1;
141 env_item_t *ep2 = (env_item_t *) m2;
142
143 ee_name_get(b1, ep1);
144 ee_name_get(b2, ep2);
145
146 return strcmp(b1, b2);
147 }
148
149 #if 0
150 env_item_t * ee_entry_get(const char *s, env_item_t *ep, uint_fast8_t flag)
151 {
152 char name[CONFIG_SYS_ENV_NAMELEN+1];
153 uint16_t idx = 0;
154 int nlen;
155
156 //printf_P(PSTR("*** ee_entry_get: >>>>>>>>> ENTER <<<<<<<<<\n"));
157
158
159 while ((nlen = ee_name_get(name, idx)) != 0 && idx < CONFIG_ENV_SIZE) {
160
161 //printf_P(PSTR("** idx: %d, name: '%s', s: '%s', nlen: %d, cpres:%d\n"),
162 // idx, name, s, nlen, strncmp(name, s, nlen));
163
164 if (strncmp(name, s, nlen) == 0)
165 break;
166
167 idx += nlen + 1;
168 while (env_get_char(idx++) != 0)
169 ;
170 }
171
172 if (nlen) {
173 char *vp;
174 ep->flags = EF_N_EEP;
175 ep->name.eep = idx;
176 if (flag & ENV_GET_VAL) {
177 vp = xmalloc(CONFIG_SYS_CBSIZE);
178 nlen = ee_val_get(vp, idx + nlen + 1, CONFIG_SYS_CBSIZE);
179 ep->val = xrealloc(vp, nlen + 1);
180 } else
181 ep->val = NULL;
182
183
184 //printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE 0x%.4x <<<<<\n"),
185 // (unsigned int) ep);
186 return ep;
187 }
188
189 //printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE <NULL> <<<<<\n"));
190 return NULL;
191 }
192 #endif
193
194 #if 0
195 static char p_env_name_buf[CONFIG_SYS_ENV_NAMELEN+1];
196
197 static char *dbg_p_env_name(env_item_t *p)
198 {
199 if (p->flags & EF_N_EEP) {
200 if (ee_name_get(p_env_name_buf, p) != 0)
201 return p_env_name_buf;
202 else
203 return "<NULL>";
204 }
205 return "<NO EEP_NAME>";
206 }
207 #endif
208
209 int env_item_print(env_item_t *ep)
210 {
211 char buf[CONFIG_SYS_ENV_NAMELEN+1];
212 int len;
213 env_item_t e = *ep;
214
215 ee_name_get(buf, ep);
216 len = printf_P(PSTR("%s="), buf);
217
218 if (e.val.ram != NULL) {
219 while (1) {
220 char c;
221 if (e.flags & EF_V_EEP)
222 c = env_get_char(e.val.eep++);
223 else
224 c = *e.val.ram++;
225
226 if (c != '\0') {
227 putchar(c);
228 len++;
229 } else
230 break;
231 }
232 }
233 putchar('\n');
234 len ++;
235
236 return len;
237 }
238
239
240 int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
241 {
242 char buf[CONFIG_SYS_ENV_NAMELEN+1];
243 int len;
244 env_item_t e = *ep;
245
246 debug("-- env_item_save(%04x, %04x, %d)\n",
247 ep, offset, space_left);
248
249 len = ee_name_get(buf, ep);
250 if (len == 0)
251 return 0;
252 buf[len++] = '=';
253 space_left -= len;
254
255 if ((unsigned) len < sizeof(buf))
256 buf[len] = '\0'; /* terminate for debugging */
257 debug(" len: %d, buf: '%s', space_left: %d\n", len, buf, space_left);
258
259 if (space_left <= 0)
260 return 0;
261
262 eeprom_update_block(buf, (uint8_t *) offset, len);
263 offset += len;
264
265 if (e.val.ram != NULL) {
266 char c;
267 do {
268 if (e.flags & EF_V_EEP)
269 c = env_get_char(e.val.eep++);
270 else
271 c = *e.val.ram++;
272
273 eeprom_update_byte((uint8_t *) offset, c);
274 offset++;
275 space_left--;
276 len++;
277 } while ((c != '\0') && space_left );
278 }
279 return len;
280 }
281
282
283 /*
284 * Update
285 *
286 */
287 int env_item_update(env_item_t *ep)
288 {
289 char buf[CONFIG_SYS_ENV_NAMELEN+1];
290 uint_fast8_t len;
291 char c;
292 unsigned pos = 0;
293
294 /* get name from old loc. (eeprom or ram */
295 len = ee_name_get(buf, ep);
296 buf[len++] = '=';
297
298 debug("-- env_item_update(%04x)\n", ep);
299 if (len < sizeof(buf))
300 buf[len] = '\0'; /* terminate for debugging */
301 debug(" len: %d, buf: '%s'\n", len, buf);
302
303 /* search this name in new eeprom env */
304 /* TODO: eliminate this ugly hack */
305 uint8_t save_env_valid = env_valid;
306 env_valid = (env_valid == 2) ? 1 : 2;
307
308 debug(" len: %d, buf: '%s', env_valid: %d\n", len, buf, env_valid);
309
310 while ((c = env_get_char(pos)) != '\0' && pos < (ENV_SIZE - len)) {
311 uint_fast8_t i = 0;
312
313 debug(" while: c: %02x, pos: %d\n ", c, pos);
314
315 while (c == buf[i] && i <= len) {
316
317 debug("%02x ", c);
318
319 ++i;
320 c = env_get_char(pos + i);
321 }
322
323 debug("\n c: %02x, i: %d, pos: %d\n", c, i, pos);
324
325 if (i == len) {
326 if ((ep->flags & EF_N_EEP) == 0)
327 free(ep->name.ram);
328 ep->name.eep = pos;
329 if ((ep->flags & EF_V_EEP) == 0)
330 free(ep->val.ram);
331 ep->val.eep = pos + i;
332 ep->flags &= ~EF_DIRTY;
333 ep->flags |= EF_N_EEP | EF_V_EEP;
334
335 /* TODO: */
336 env_valid = save_env_valid;
337 return 0;
338 }
339 pos += i + 1;
340 while (((c = env_get_char(pos++)) != '\0') && pos < (ENV_SIZE - len))
341 ;
342
343 debug("\n c: %02x, i: %d, pos: %d\n", c, i, pos);
344
345 }
346 /* TODO: */
347 env_valid = save_env_valid;
348
349 /* name not found */
350 return -1;
351 }
352
353
354 int envlist_import(void)
355 {
356 char name[CONFIG_SYS_ENV_NAMELEN+1];
357 uint16_t idx = 0;
358 int nlen;
359 env_item_t e;
360
361 e.flags = EF_N_EEP | EF_V_EEP;
362 e.name.eep = idx;
363 while ((nlen = ee_name_get(name, &e)) != 0 && idx < ENV_SIZE) {
364
365 if (entrycount <= CONFIG_ENVVAR_MAX) {
366 e.val.eep = idx + nlen + 1;
367
368 env_list[entrycount++] = e;
369
370 idx += nlen + 1;
371 while (env_get_char(idx++) != 0 && idx < ENV_SIZE)
372 ;
373 e.name.eep = idx;
374 } else {
375 debug("** Too many environment variables!\n");
376 break;
377 }
378 }
379 qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items);
380
381 return 0;
382 }
383
384 static env_item_t *envlist_search(const char *name)
385 {
386 return bsearch(name, env_list, entrycount,
387 sizeof(env_item_t), comp_env_key_item);
388 }
389
390 #if 0
391 env_item_t *envlist_insert(const char *key, env_item_t *e)
392 {
393 const size_t size = sizeof(env_item_t);
394
395 if (entrycount < CONFIG_ENVVAR_MAX) {
396 env_list[entrycount++] = *e;
397 qsort(env_list, entrycount, size, comp_env_items);
398
399 return bsearch(key, env_list, entrycount,
400 size, comp_env_key_item);
401
402 } else
403 return NULL;
404 }
405 #endif
406
407 env_item_t *envlist_enter(env_item_t *e)
408 {
409 char *key = e->name.ram;
410 const size_t size = sizeof(env_item_t);
411 env_item_t *ep;
412
413 ep = bsearch(key, env_list, entrycount,
414 size, comp_env_key_item);
415
416 if (ep == NULL) {
417 if (entrycount < CONFIG_ENVVAR_MAX) {
418
419 env_list[entrycount++] = *e;
420 qsort(env_list, entrycount, size, comp_env_items);
421 ep = bsearch(key, env_list, entrycount,
422 size, comp_env_key_item);
423 }
424 } else {
425 if ((ep->flags & EF_V_EEP) == 0) {
426 free(ep->val.ram);
427 }
428 ep->val.ram = e->val.ram;
429 }
430
431 if (ep != NULL) {
432 ep->flags |= EF_DIRTY;
433 ep->flags &= ~EF_V_EEP;
434
435 if ((ep->flags & EF_N_EEP) == 0) {
436 int nlen = strnlen(key, CONFIG_SYS_ENV_NAMELEN);
437 char *name = xmalloc(nlen + 1);
438 if (name == NULL) {
439 printf_P(PSTR("## Can't malloc %d bytes\n"),
440 nlen + 1);
441 return NULL;
442 }
443 strcpy(name, key);
444 name[nlen] = '\0';
445 ep->name.ram = name;
446 }
447 }
448
449 return ep;
450 }
451
452
453 static env_item_t *envlist_get(const char *name, uint_fast8_t flag)
454 {
455 env_item_t *ep;
456
457 ep = envlist_search(name);
458
459 if (ep != NULL && (flag & ENV_GET_VAL)) {
460 if (ep->flags & EF_V_EEP) {
461 char *vp;
462 uint_fast8_t len;
463 /* TODO: function that gets len of val,
464 to get rid of xrealloc */
465 vp = xmalloc(CONFIG_SYS_CBSIZE);
466 len = ee_val_get(vp, ep->val.eep, CONFIG_SYS_CBSIZE);
467 ep->val.ram = xrealloc(vp, len + 1);
468 ep->flags &= ~EF_V_EEP;
469 }
470 }
471
472 return ep;
473 }
474
475 int env_item_delete(env_item_t *ep)
476 {
477 if (entrycount == 0)
478 return -1;
479 if ((ep->flags & EF_V_EEP) == 0)
480 free(ep->val.ram);
481 if ((ep->flags & EF_N_EEP) == 0)
482 free(ep->name.ram);
483
484 entrycount--;
485 size_t size = sizeof(env_item_t);
486 memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
487
488 return 0;
489 }
490
491 static int envlist_delete(const char *name)
492 {
493 env_item_t *ep = bsearch(name, env_list, entrycount,
494 sizeof(env_item_t), comp_env_key_item);
495
496 if (ep != NULL)
497 return env_item_delete(ep);
498
499 #if 0
500 dump_ram((uint8_t *) &env_list[0], entrycount * sizeof(env_item_t),
501 "=== env_list:");
502 dump_heap();
503 debug("** entrycount: %d\n", entrycount);
504 for (int i=0; i<entrycount; i++) {
505 printf_P(PSTR("** env var [%d] "), i);
506 env_item_print(&env_list[i]);
507 }
508 #endif
509
510 return 1;
511 }
512
513 char *getenv(const char *name)
514 {
515 env_item_t *ep;
516 char *ret = NULL;
517
518 debug("** getenv: %s\n", name);
519
520 ep = envlist_get(name, ENV_GET_VAL);
521 if (ep != NULL)
522 ret = ep->val.ram;
523 #if 0
524 dump_ram((uint8_t *) &env_list[0], entrycount * sizeof(env_item_t),
525 "=== env_list:");
526 dump_heap();
527 debug("** entrycount: %d\n", entrycount);
528 for (int i=0; i<entrycount; i++) {
529 printf_P(PSTR("** env var [%d] "), i);
530 env_item_print(&env_list[i]);
531 }
532 #endif
533 return ret;
534 }
535
536 uint16_t env_crc(uint16_t data_offset)
537 {
538 uint16_t crc;
539 uint16_t i;
540 char c, c0;
541
542 crc = 0xffff;
543 c = 0xff;
544 for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++)
545 {
546 c0 = c;
547 c = eeprom_read_byte((const uint8_t *) data_offset + i);
548 crc = crc16(crc, c);
549 }
550 return crc ;
551 }
552
553 int saveenv(void)
554 {
555 unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
556 unsigned int off_red = CONFIG_ENV_OFFSET;
557 unsigned int pos;
558 int len, left;
559 uint16_t crc;
560 int rc = 0;
561
562 if (env_valid == 2) {
563 off = CONFIG_ENV_OFFSET;
564 off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
565 }
566
567 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
568
569 pos = off + offsetof(env_t, data);
570 left = ENV_SIZE - 1;
571 for (int i = 0 ; i < entrycount; i++) {
572 len = env_item_save(&env_list[i], pos, left);
573 if (len == 0) {
574 return 1;
575 }
576 pos += len;
577 left -= len;
578 }
579 /* terminate list */
580 eeprom_update_byte((uint8_t *) pos, 0);
581 crc = env_crc(off + offsetof(env_t, data));
582 eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc);
583 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags),
584 ACTIVE_FLAG);
585
586 if (rc == 0) {
587 while (entrycount != 0) {
588 env_item_delete(&env_list[entrycount-1]);
589 }
590 eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags),
591 OBSOLETE_FLAG);
592 env_valid = (env_valid == 2) ? 1 : 2;
593
594 envlist_import();
595 }
596
597 return rc;
598 }
599
600 /*-----------------------------------------------------------------------------*/
601
602 int set_default_env(void)
603 {
604 char buf[64];
605 uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data);
606 unsigned int len = ENV_SIZE;
607 unsigned int i, src = 0;
608 char c = 0xff, c0 = c;
609 #if 0
610 printf_P(PSTR("\n\n** set_default_env()\n"));
611 #endif
612 if (env_valid == 1) {
613 eep = CONFIG_ENV_OFFSET + offsetof(env_t, data) + CONFIG_ENV_SIZE;
614 }
615
616 while (len) {
617 memcpy_P(buf, default_env+src, sizeof(buf));
618 for (i=0; i < (len < sizeof(buf) ? len : sizeof(buf)) &&
619 !(c == 0 && c0 == 0);
620 i++) {
621 c0 = c; c = buf[i];
622 }
623 eeprom_update_block(buf, (char *) eep, i);
624 #if 0
625 printf_P(PSTR("eeprom_update_block: eep: 0x%.4x, i:%d\n"),
626 (unsigned int) eep, i);
627 dump_ram((uint8_t *) buf, i, "=== buf:");
628 dump_eep((const uint8_t *) eep, i);
629 #endif
630 if (c == 0 && c0 == 0)
631 len = 0;
632 if (len > sizeof(buf))
633 len -= sizeof(buf);
634 src += sizeof(buf);
635 eep += sizeof(buf);
636 }
637
638 #if 0
639 dump_eep(0, 128);
640 #endif
641 return 0;
642 }
643
644
645 int env_check(uint16_t offset)
646 {
647 uint16_t crc, stored_crc;
648
649 /* read old CRC */
650 stored_crc = eeprom_read_word(
651 (const uint16_t *) offset + offsetof(env_t, crc));
652 crc = env_crc(offset + offsetof(env_t, data));
653
654 debug_cond((crc != stored_crc),
655 "** crc eep: 0x%.4x, crc new: 0x%.4x\n", stored_crc, crc);
656
657 return crc == stored_crc;
658 }
659
660 int _env_init(void)
661 {
662 unsigned int off_env[2];
663 uint8_t flags[2], crc_ok[2];
664
665 off_env[0] = CONFIG_ENV_OFFSET;
666 off_env[1] = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
667
668 for (uint_fast8_t i = 0; i < 2; i++) {
669 /* read FLAGS */
670 flags[i] = eeprom_read_byte ((uint8_t *) off_env[i] +
671 offsetof(env_t, flags));
672
673 /* check CRC */
674 crc_ok[i] = (
675 eeprom_read_word((uint16_t *) off_env[i] +
676 offsetof(env_t, crc))
677 == env_crc(off_env[i] + offsetof(env_t, data))
678 );
679 }
680
681 if (!crc_ok[0] && !crc_ok[1]) {
682 env_valid = 0;
683 debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n",
684 crc_ok[0], crc_ok[1], flags[0], flags[1], env_valid);
685 return 0;
686
687 } else if (crc_ok[0] && !crc_ok[1]) {
688 env_valid = 1;
689 } else if (!crc_ok[0] && crc_ok[1]) {
690 env_valid = 2;
691 } else {
692 /* both ok - check serial */
693 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
694 env_valid = 1;
695 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
696 env_valid = 2;
697 else if (flags[0] == 0xFF && flags[1] == 0)
698 env_valid = 2;
699 else if (flags[1] == 0xFF && flags[0] == 0)
700 env_valid = 1;
701 else /* flags are equal - almost impossible */
702 env_valid = 1;
703 }
704
705 debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n",
706 crc_ok[0], crc_ok[1], flags[0], flags[1], env_valid);
707
708 return 0;
709 }
710
711
712 int env_init(void)
713 {
714 _env_init();
715 if (env_valid == 0) {
716 printf_P(PSTR("*** Warning - bad CRC, "
717 "using default environment\n\n"));
718 set_default_env();
719 }
720 entrycount = 0;
721 envlist_import();
722 return 0;
723 }
724
725
726 /*
727 * Command interface: print one or all environment variables
728 *
729 * Returns -1 in case of error, or length of printed string
730 */
731 static int env_print(char *name)
732 {
733 int len = -1;
734
735 if (name) { /* print a single name */
736
737 env_item_t *ep = envlist_search(name);
738 if (ep != NULL)
739 len = env_item_print(ep);
740
741 } else { /* print whole list */
742 len = 0;
743 for (int i = 0 ; i < entrycount; i++) {
744 len += env_item_print(&env_list[i]);
745 }
746 }
747 return len;
748 }
749
750 int env_print_ramsize(void)
751 {
752 int size = 0;
753 uint8_t name_cnt = 0;
754 uint8_t val_cnt = 0;
755
756 for (int i = 0 ; i < entrycount; i++) {
757 if ((env_list[i].flags & EF_N_EEP) == 0 &&
758 (env_list[i].name.ram != NULL)) {
759 name_cnt++;
760 size += strlen(env_list[i].name.ram) + 3;
761 }
762 if ((env_list[i].flags & EF_V_EEP) == 0 &&
763 (env_list[i].val.ram != NULL)) {
764 val_cnt++;
765 size += strlen(env_list[i].val.ram) + 3;
766 }
767 }
768 printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
769 size, name_cnt, val_cnt);
770 return size;
771 }
772
773 int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
774 char * const argv[])
775 {
776 int i;
777 int rcode = 0;
778
779 (void) cmdtp; (void) flag;
780
781 if (argc == 1) {
782 /* print all env vars */
783 rcode = env_print(NULL);
784 if (rcode < 0)
785 return 1;
786 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
787 rcode, ENV_SIZE);
788 env_print_ramsize();
789 return 0;
790 }
791
792 /* print selected env vars */
793 for (i = 1; i < argc; ++i) {
794 int rc = env_print(argv[i]);
795 if (rc < 0) {
796 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
797 ++rcode;
798 }
799 }
800
801 return rcode;
802 }
803
804
805 /**
806 * Set or delete environment variable
807 *
808 * Set a new environment variable,
809 * or replace or delete an existing one.
810 *
811 * @param flag (not used)
812 * @param argc
813 * @param argv[1] Environment variable to set or delete
814 * if no more args
815 * @param argv[2] ... Value to set it to
816 *
817 * @return 0 if ok, 1 on error
818 */
819 static int _do_env_set(int flag, int argc, char * const argv[])
820 {
821 int i, len;
822 char *name, *value, *s;
823 env_item_t e, *ep;
824
825 (void) flag;
826 debug("Initial value for argc=%d\n", argc);
827
828 name = argv[1];
829 value = argv[2];
830
831 if (strchr(name, '=')) {
832 printf_P(PSTR("## Error: illegal character '='"
833 "in variable name \"%s\"\n"), name);
834 return 1;
835 }
836 if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) {
837 printf_P(PSTR("## Error: Variable name \"%s\" too long. "
838 "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
839 return 1;
840 }
841 /*
842 env_id++;
843 */
844 /* Delete only ? */
845 if (argc < 3 || argv[2] == NULL) {
846 int rc = envlist_delete(name);
847 return rc != 0;
848 }
849
850 /*
851 * Insert / replace new value
852 */
853 for (i = 2, len = 0; i < argc; ++i)
854 len += strlen(argv[i]) + 1;
855
856 value = xmalloc(len);
857 if (value == NULL) {
858 printf_P(PSTR("## Can't malloc %d bytes\n"), len);
859 return 1;
860 }
861 for (i = 2, s = value; i < argc; ++i) {
862 char *v = argv[i];
863
864 while ((*s++ = *v++) != '\0')
865 ;
866 *(s - 1) = ' ';
867 }
868 if (s != value)
869 *--s = '\0';
870
871 e.flags = EF_DIRTY;
872 e.name.ram = name;
873 e.val.ram = value;
874 ep = envlist_enter(&e);
875 if (!ep) {
876 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
877 name);
878 return 1;
879 }
880
881 return 0;
882 }
883
884 /**
885 * Set an environment variable
886 *
887 * @param varname Environment variable to set
888 * @param varvalue Value to set it to
889 * @return 0 if ok, 1 on error
890 */
891 int setenv(const char *varname, const char *varvalue)
892 {
893 const char * const argv[3] = { NULL, varname, varvalue };
894 int argc = 3;
895
896 if (varvalue == NULL || varvalue[0] == '\0')
897 --argc;
898
899 return _do_env_set(0, argc, (char * const *)argv);
900 }
901
902 #if 0
903 /**
904 * Set an environment variable to an integer value
905 *
906 * @param varname Environment variable to set
907 * @param value Value to set it to
908 * @return 0 if ok, 1 on error
909 */
910 int setenv_ulong(const char *varname, unsigned long value)
911 {
912 /* TODO: this should be unsigned */
913 char *str = simple_itoa(value);
914
915 return setenv(varname, str);
916 }
917 #endif
918
919
920 /**
921 * Set an environment variable to an value in hex
922 *
923 * @param varname Environment variable to set
924 * @param value Value to set it to
925 * @return 0 if ok, 1 on error
926 */
927 int setenv_hex(const char *varname, unsigned long value)
928 {
929 char str[sizeof(unsigned long) *2 + 1];
930
931 sprintf_P(str, PSTR("%lx"), value);
932 return setenv(varname, str);
933 }
934
935 /**
936 * Get an environment variable as a hex value
937 *
938 * @param varname Environment variable to get
939 * @param default_val Return this, if variable does not exist
940 * @return hex value of variable or default_val
941 */
942 unsigned long getenv_hex(const char *varname, unsigned long default_val)
943 {
944 const char *s;
945 unsigned long value;
946 char *endp;
947
948 s = getenv(varname);
949 if (s)
950 value = strtoul(s, &endp, 16);
951 if (!s || endp == s)
952 return default_val;
953
954 return value;
955 }
956
957 int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
958 {
959 (void) cmdtp;
960
961 if (argc < 2)
962 return CMD_RET_USAGE;
963
964 return _do_env_set(flag, argc, argv);
965 }
966
967
968 int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
969 {
970 (void) cmdtp; (void) flag; (void) argc; (void) argv;
971
972 printf_P(PSTR("Saving Environment ...\n"));
973
974 return saveenv() ? 1 : 0;
975 }
976
977 #if defined(CONFIG_AUTO_COMPLETE)
978 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
979 {
980 ENTRY *match;
981 int found, idx;
982
983 idx = 0;
984 found = 0;
985 cmdv[0] = NULL;
986
987 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
988 int vallen = strlen(match->key) + 1;
989
990 if (found >= maxv - 2 || bufsz < vallen)
991 break;
992
993 cmdv[found++] = buf;
994 memcpy(buf, match->key, vallen);
995 buf += vallen;
996 bufsz -= vallen;
997 }
998
999 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
1000
1001 if (idx)
1002 cmdv[found++] = "...";
1003
1004 cmdv[found] = NULL;
1005 return found;
1006 }
1007 #endif