]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/env.c
Merge tag 'fatfs-0.10b'
[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 #define ENV_GET_VAL (1<<0)
20
21
22 /*
23 * Default Environment
24 */
25
26 #define DELIM "\0"
27
28 const FLASH char default_env[] = {
29 "bootdelay=" "3" DELIM
30 "bootcmd=" "reset; loadf; go $(startaddr)" DELIM
31 "baudrate=" "115200" DELIM
32 "startaddr=" "0" DELIM
33 DELIM
34 };
35
36 /* EEPROM storage */
37 typedef struct environment_s {
38 uint16_t crc; /* CRC16 over data bytes */
39 uint8_t flags; /* active/obsolete flags */
40 char data[ENV_SIZE]; /* Environment data */
41 } env_t;
42
43
44 /* */
45 typedef struct env_item_s {
46 #define EF_N_EEP (1<<7) /* Variable name is in EEPROM */
47 #define EF_V_EEP (1<<6) /* Variable value is in EEPROM */
48 #define EF_DIRTY (1<<0) /* Variable is new or value changed */
49 uint8_t flags;
50 union {
51 uint16_t eep;
52 char *ram;
53 } name;
54 union {
55 uint16_t eep;
56 char *ram;
57 } val;
58 } env_item_t;
59
60
61 static uint8_t env_valid;
62 static env_item_t env_list[CONFIG_ENVVAR_MAX];
63 static int entrycount;
64
65
66 static
67 char env_get_char(uint16_t index)
68 {
69 unsigned int off = CONFIG_ENV_OFFSET;
70
71 if (env_valid == 2)
72 off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
73
74 return (char) eeprom_read_byte((const uint8_t *)off + index +
75 offsetof(env_t, data));
76 }
77
78
79 static
80 uint16_t varname_get(char *buf, env_item_t *ep)
81 {
82 int i = 0;
83 char c;
84
85 if (ep->flags & EF_N_EEP) {
86
87 while ((c = env_get_char(ep->name.eep + i)) != '=' &&
88 c != '\0' && i < CONFIG_SYS_ENV_NAMELEN) {
89
90 buf[i] = c;
91 i++;
92 }
93 if (i > 0 && c != '=') {
94 debug("** ee_name: '%s' not '=' terminated!\n", buf);
95 }
96 } else {
97 strncpy(buf, ep->name.ram, CONFIG_SYS_ENV_NAMELEN);
98 i = strnlen(buf, CONFIG_SYS_ENV_NAMELEN);
99 }
100 buf[i] = '\0';
101 return i;
102 }
103
104
105 static
106 uint16_t varval_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 varname_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 varname_get(b1, ep1);
144 varname_get(b2, ep2);
145
146 return strcmp(b1, b2);
147 }
148
149
150 static
151 int envlist_import(void)
152 {
153 char name[CONFIG_SYS_ENV_NAMELEN+1];
154 uint16_t idx = 0;
155 int nlen;
156 env_item_t e;
157
158 e.flags = EF_N_EEP | EF_V_EEP;
159 e.name.eep = idx;
160 while ((nlen = varname_get(name, &e)) != 0 && idx < ENV_SIZE) {
161
162 if (entrycount <= CONFIG_ENVVAR_MAX) {
163 e.val.eep = idx + nlen + 1;
164
165 env_list[entrycount++] = e;
166
167 idx += nlen + 1;
168 while (env_get_char(idx++) != 0 && idx < ENV_SIZE)
169 ;
170 e.name.eep = idx;
171 } else {
172 debug("** Too many environment variables!\n");
173 break;
174 }
175 }
176 qsort(env_list, entrycount, sizeof(env_item_t), comp_env_items);
177
178 return 0;
179 }
180
181
182 static
183 int set_default_env(void)
184 {
185 char buf[56];
186 uint16_t eep = CONFIG_ENV_OFFSET + offsetof(env_t, data);
187 unsigned int len = ENV_SIZE;
188 unsigned int i, src = 0;
189 char c = 0xff, c0 = c;
190
191 if (env_valid == 1) {
192 eep = CONFIG_ENV_OFFSET + offsetof(env_t, data) + CONFIG_ENV_SIZE;
193 }
194
195 while (len) {
196 memcpy_P(buf, default_env+src, sizeof(buf));
197 for (i=0; i < (len < sizeof(buf) ? len : sizeof(buf)) &&
198 !(c == 0 && c0 == 0);
199 i++) {
200 c0 = c; c = buf[i];
201 }
202 eeprom_update_block(buf, (char *) eep, i);
203 if (c == 0 && c0 == 0)
204 len = 0;
205 if (len > sizeof(buf))
206 len -= sizeof(buf);
207 src += sizeof(buf);
208 eep += sizeof(buf);
209 }
210
211 return 0;
212 }
213
214
215 static
216 uint16_t env_crc(uint16_t data_offset)
217 {
218 uint16_t crc;
219 uint16_t i;
220 char c, c0;
221
222 crc = 0xffff;
223 c = 0xff;
224 for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++)
225 {
226 c0 = c;
227 c = eeprom_read_byte((const uint8_t *) data_offset + i);
228 crc = crc16(crc, c);
229 }
230 return crc ;
231 }
232
233
234 /**
235 * return valid env
236 */
237 static
238 int env_check_valid(void)
239 {
240 const uint16_t offset[2] = {CONFIG_ENV_OFFSET,
241 CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE};
242 uint_fast8_t flags[2], crc_ok[2];
243 int rc;
244
245 /* read FLAGS */
246 flags[0] = eeprom_read_byte ((uint8_t *) offset[0] +
247 offsetof(env_t, flags));
248 flags[1] = eeprom_read_byte ((uint8_t *) offset[1] +
249 offsetof(env_t, flags));
250
251 /* check CRC */
252 crc_ok[0] = (
253 eeprom_read_word((uint16_t *) offset[0] +
254 offsetof(env_t, crc))
255 == env_crc(offset[0] + offsetof(env_t, data))
256 );
257 crc_ok[1] = (
258 eeprom_read_word((uint16_t *) offset[1] +
259 offsetof(env_t, crc))
260 == env_crc(offset[1] + offsetof(env_t, data))
261 );
262
263 if (!crc_ok[0] && !crc_ok[1]) {
264 rc = 0;
265
266 } else if (crc_ok[0] && !crc_ok[1]) {
267 rc = 1;
268 } else if (!crc_ok[0] && crc_ok[1]) {
269 rc = 2;
270 } else {
271 /* both ok - check serial */
272 #if 1
273 if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG)
274 rc = 2;
275 else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF)
276 rc = 2;
277 else
278 rc = 1;
279 #else
280 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
281 rc = 1;
282 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
283 rc = 2;
284 else if (flags[0] == 0xFF && flags[1] == 0)
285 rc = 2;
286 else if (flags[1] == 0xFF && flags[0] == 0)
287 rc = 1;
288 else /* flags are equal - almost impossible */
289 rc = 1;
290 #endif
291 }
292
293 return rc;
294 }
295
296
297 int env_init(void)
298 {
299 env_valid = env_check_valid();
300 if (env_valid == 0) {
301 printf_P(PSTR("*** Warning - bad CRC, "
302 "using default environment\n\n"));
303 set_default_env();
304 }
305 entrycount = 0;
306 envlist_import();
307 return 0;
308 }
309
310
311 static
312 env_item_t *envlist_search(const char *name)
313 {
314 return bsearch(name, env_list, entrycount,
315 sizeof(env_item_t), comp_env_key_item);
316 }
317
318
319 static
320 env_item_t *envlist_enter(env_item_t *e)
321 {
322 char *key = e->name.ram;
323 const size_t size = sizeof(env_item_t);
324 env_item_t *ep;
325
326 ep = bsearch(key, env_list, entrycount,
327 size, comp_env_key_item);
328
329 if (ep == NULL) {
330 if (entrycount < CONFIG_ENVVAR_MAX) {
331
332 env_list[entrycount++] = *e;
333 qsort(env_list, entrycount, size, comp_env_items);
334 ep = bsearch(key, env_list, entrycount,
335 size, comp_env_key_item);
336 }
337 } else {
338 if ((ep->flags & EF_V_EEP) == 0) {
339 free(ep->val.ram);
340 }
341 ep->val.ram = e->val.ram;
342 }
343
344 if (ep != NULL) {
345 ep->flags |= EF_DIRTY;
346 ep->flags &= ~EF_V_EEP;
347
348 if ((ep->flags & EF_N_EEP) == 0) {
349 int nlen = strnlen(key, CONFIG_SYS_ENV_NAMELEN);
350 char *name = xmalloc(nlen + 1);
351 if (name == NULL) {
352 printf_P(PSTR("## Can't malloc %d bytes\n"),
353 nlen + 1);
354 return NULL;
355 }
356 strcpy(name, key);
357 name[nlen] = '\0';
358 ep->name.ram = name;
359 }
360 }
361
362 return ep;
363 }
364
365
366 static
367 int env_item_delete(env_item_t *ep)
368 {
369 if (entrycount == 0)
370 return -1;
371 if ((ep->flags & EF_V_EEP) == 0)
372 free(ep->val.ram);
373 if ((ep->flags & EF_N_EEP) == 0)
374 free(ep->name.ram);
375
376 entrycount--;
377 size_t size = sizeof(env_item_t);
378 memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
379
380 return 0;
381 }
382
383 static
384 int envlist_delete(const char *name)
385 {
386 env_item_t *ep = bsearch(name, env_list, entrycount,
387 sizeof(env_item_t), comp_env_key_item);
388
389 if (ep != NULL)
390 return env_item_delete(ep);
391
392 return 1;
393 }
394
395
396 static
397 env_item_t *envlist_get(const char *name, uint_fast8_t flag)
398 {
399 env_item_t *ep;
400
401 ep = envlist_search(name);
402
403 if (ep != NULL && (flag & ENV_GET_VAL)) {
404 if (ep->flags & EF_V_EEP) {
405 char *vp;
406 uint_fast8_t len;
407 /* TODO: function that gets len of val,
408 to get rid of xrealloc */
409 vp = xmalloc(CONFIG_SYS_CBSIZE);
410 len = varval_get(vp, ep->val.eep, CONFIG_SYS_CBSIZE);
411 ep->val.ram = xrealloc(vp, len + 1);
412 ep->flags &= ~EF_V_EEP;
413 }
414 }
415
416 return ep;
417 }
418
419
420 char *getenv(const char *name)
421 {
422 env_item_t *ep;
423 char *ret = NULL;
424
425 ep = envlist_get(name, ENV_GET_VAL);
426 if (ep != NULL)
427 ret = ep->val.ram;
428 return ret;
429 }
430
431 static
432 int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
433 {
434 char buf[CONFIG_SYS_ENV_NAMELEN+1];
435 int len;
436 env_item_t e = *ep;
437
438 len = varname_get(buf, ep);
439 if (len == 0)
440 return 0;
441 buf[len++] = '=';
442 space_left -= len;
443
444 if (space_left <= 0)
445 return 0;
446
447 eeprom_update_block(buf, (uint8_t *) offset, len);
448 offset += len;
449
450 if (e.val.ram != NULL) {
451 char c;
452 do {
453 if (e.flags & EF_V_EEP)
454 c = env_get_char(e.val.eep++);
455 else
456 c = *e.val.ram++;
457
458 eeprom_update_byte((uint8_t *) offset, c);
459 offset++;
460 space_left--;
461 len++;
462 } while ((c != '\0') && space_left );
463 }
464 return len;
465 }
466
467
468 static
469 int saveenv(void)
470 {
471 unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
472 unsigned int off_red = CONFIG_ENV_OFFSET;
473 unsigned int pos;
474 int len, left;
475 uint16_t crc;
476 int rc = 0;
477
478 if (env_valid == 2) {
479 off = CONFIG_ENV_OFFSET;
480 off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
481 }
482
483 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
484
485 pos = off + offsetof(env_t, data);
486 left = ENV_SIZE - 1;
487 for (int i = 0 ; i < entrycount; i++) {
488 len = env_item_save(&env_list[i], pos, left);
489 if (len == 0) {
490 return 1;
491 }
492 pos += len;
493 left -= len;
494 }
495 /* terminate list */
496 eeprom_update_byte((uint8_t *) pos, 0);
497 crc = env_crc(off + offsetof(env_t, data));
498 eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc);
499 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags),
500 ACTIVE_FLAG);
501
502 if (rc == 0) {
503 while (entrycount != 0) {
504 env_item_delete(&env_list[entrycount-1]);
505 }
506 eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags),
507 OBSOLETE_FLAG);
508 env_valid = (env_valid == 2) ? 1 : 2;
509
510 envlist_import();
511 }
512
513 return rc;
514 }
515
516
517 static
518 int env_item_print(env_item_t *ep)
519 {
520 char buf[CONFIG_SYS_ENV_NAMELEN+1];
521 int len;
522 env_item_t e = *ep;
523
524 varname_get(buf, ep);
525 len = printf_P(PSTR("%s="), buf);
526
527 if (e.val.ram != NULL) {
528 while (1) {
529 char c;
530 if (e.flags & EF_V_EEP)
531 c = env_get_char(e.val.eep++);
532 else
533 c = *e.val.ram++;
534
535 if (c != '\0') {
536 putchar(c);
537 len++;
538 } else
539 break;
540 }
541 }
542 putchar('\n');
543 len ++;
544
545 return len;
546 }
547
548
549 /*
550 * Command interface: print one or all environment variables
551 *
552 * Returns -1 in case of error, or length of printed string
553 */
554 static
555 int env_print(char *name)
556 {
557 int len = -1;
558
559 if (name) { /* print a single name */
560
561 env_item_t *ep = envlist_search(name);
562 if (ep != NULL)
563 len = env_item_print(ep);
564
565 } else { /* print whole list */
566 len = 0;
567 for (int i = 0 ; i < entrycount; i++) {
568 len += env_item_print(&env_list[i]);
569 }
570 }
571 return len;
572 }
573
574 static
575 int env_print_ramsize(void)
576 {
577 int size = 0;
578 uint8_t name_cnt = 0;
579 uint8_t val_cnt = 0;
580
581 for (int i = 0 ; i < entrycount; i++) {
582 if ((env_list[i].flags & EF_N_EEP) == 0 &&
583 (env_list[i].name.ram != NULL)) {
584 name_cnt++;
585 size += strlen(env_list[i].name.ram) + 3;
586 }
587 if ((env_list[i].flags & EF_V_EEP) == 0 &&
588 (env_list[i].val.ram != NULL)) {
589 val_cnt++;
590 size += strlen(env_list[i].val.ram) + 3;
591 }
592 }
593 printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
594 size, name_cnt, val_cnt);
595 return size;
596 }
597
598
599 int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
600 {
601 int i;
602 int rcode = 0;
603
604 (void) cmdtp; (void) flag;
605
606 if (argc == 1) {
607 /* print all env vars */
608 rcode = env_print(NULL);
609 if (rcode < 0)
610 return 1;
611 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
612 rcode, ENV_SIZE);
613 env_print_ramsize();
614 return 0;
615 }
616
617 /* print selected env vars */
618 for (i = 1; i < argc; ++i) {
619 int rc = env_print(argv[i]);
620 if (rc < 0) {
621 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
622 ++rcode;
623 }
624 }
625
626 return rcode;
627 }
628
629
630 /**
631 * Set or delete environment variable
632 *
633 * Set a new environment variable,
634 * or replace or delete an existing one.
635 *
636 * @param flag (not used)
637 * @param argc
638 * @param argv[1] Environment variable to set or delete
639 * if no more args
640 * @param argv[2] ... Value to set it to
641 *
642 * @return 0 if ok, 1 on error
643 */
644 static
645 int _do_env_set(int flag, int argc, char * const argv[])
646 {
647 int i, len;
648 char *name, *value, *s;
649 env_item_t e, *ep;
650
651 (void) flag;
652 debug("Initial value for argc=%d\n", argc);
653
654 name = argv[1];
655 value = argv[2];
656
657 if (strchr(name, '=')) {
658 printf_P(PSTR("## Error: illegal character '='"
659 "in variable name \"%s\"\n"), name);
660 return 1;
661 }
662 if (strlen(name) > CONFIG_SYS_ENV_NAMELEN) {
663 printf_P(PSTR("## Error: Variable name \"%s\" too long. "
664 "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
665 return 1;
666 }
667 /*
668 env_id++;
669 */
670 /* Delete only ? */
671 if (argc < 3 || argv[2] == NULL) {
672 int rc = envlist_delete(name);
673 return rc != 0;
674 }
675
676 /*
677 * Insert / replace new value
678 */
679 for (i = 2, len = 0; i < argc; ++i)
680 len += strlen(argv[i]) + 1;
681
682 value = xmalloc(len);
683 if (value == NULL) {
684 printf_P(PSTR("## Can't malloc %d bytes\n"), len);
685 return 1;
686 }
687 for (i = 2, s = value; i < argc; ++i) {
688 char *v = argv[i];
689
690 while ((*s++ = *v++) != '\0')
691 ;
692 *(s - 1) = ' ';
693 }
694 if (s != value)
695 *--s = '\0';
696
697 e.flags = EF_DIRTY;
698 e.name.ram = name;
699 e.val.ram = value;
700 ep = envlist_enter(&e);
701 if (!ep) {
702 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
703 name);
704 return 1;
705 }
706
707 return 0;
708 }
709
710 /**
711 * Set an environment variable
712 *
713 * @param varname Environment variable to set
714 * @param varvalue Value to set it to
715 * @return 0 if ok, 1 on error
716 */
717 static
718 int setenv(const char *varname, const char *varvalue)
719 {
720 const char * const argv[3] = { NULL, varname, varvalue };
721 int argc = 3;
722
723 if (varvalue == NULL || varvalue[0] == '\0')
724 --argc;
725
726 return _do_env_set(0, argc, (char * const *)argv);
727 }
728
729 #if 0
730 /**
731 * Set an environment variable to an integer value
732 *
733 * @param varname Environment variable to set
734 * @param value Value to set it to
735 * @return 0 if ok, 1 on error
736 */
737 int setenv_ulong(const char *varname, unsigned long value)
738 {
739 /* TODO: this should be unsigned */
740 char *str = simple_itoa(value);
741
742 return setenv(varname, str);
743 }
744 #endif
745
746
747 /**
748 * Set an environment variable to an value in hex
749 *
750 * @param varname Environment variable to set
751 * @param value Value to set it to
752 * @return 0 if ok, 1 on error
753 */
754 int setenv_hex(const char *varname, unsigned long value)
755 {
756 char str[sizeof(unsigned long) *2 + 1];
757
758 sprintf_P(str, PSTR("%lx"), value);
759 return setenv(varname, str);
760 }
761
762 /**
763 * Get an environment variable as a hex value
764 *
765 * @param varname Environment variable to get
766 * @param default_val Return this, if variable does not exist
767 * @return hex value of variable or default_val
768 */
769 unsigned long getenv_hex(const char *varname, unsigned long default_val)
770 {
771 const char *s;
772 unsigned long value;
773 char *endp;
774
775 s = getenv(varname);
776 if (s)
777 value = strtoul(s, &endp, 16);
778 if (!s || endp == s)
779 return default_val;
780
781 return value;
782 }
783
784 int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
785 {
786 (void) cmdtp;
787
788 if (argc < 2)
789 return CMD_RET_USAGE;
790
791 return _do_env_set(flag, argc, argv);
792 }
793
794
795 int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
796 {
797 (void) cmdtp; (void) flag; (void) argc; (void) argv;
798
799 printf_P(PSTR("Saving Environment ...\n"));
800 return saveenv() ? 1 : 0;
801 }
802
803 #if defined(CONFIG_AUTO_COMPLETE)
804 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
805 {
806 ENTRY *match;
807 int found, idx;
808
809 idx = 0;
810 found = 0;
811 cmdv[0] = NULL;
812
813 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
814 int vallen = strlen(match->key) + 1;
815
816 if (found >= maxv - 2 || bufsz < vallen)
817 break;
818
819 cmdv[found++] = buf;
820 memcpy(buf, match->key, vallen);
821 buf += vallen;
822 bufsz -= vallen;
823 }
824
825 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
826
827 if (idx)
828 cmdv[found++] = "...";
829
830 cmdv[found] = NULL;
831 return found;
832 }
833 #endif