]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/env.c
5 #include <avr/eeprom.h>
16 #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
18 #define OBSOLETE_FLAG 0
19 #define ENV_GET_VAL (1<<0)
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
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 */
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 */
61 static uint8_t env_valid
;
62 static env_item_t env_list
[CONFIG_ENVVAR_MAX
];
63 static int entrycount
;
67 char env_get_char(uint16_t index
)
69 unsigned int off
= CONFIG_ENV_OFFSET
;
72 off
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
74 return (char) eeprom_read_byte((const uint8_t *)off
+ index
+
75 offsetof(env_t
, data
));
80 uint16_t varname_get(char *buf
, env_item_t
*ep
)
85 if (ep
->flags
& EF_N_EEP
) {
87 while ((c
= env_get_char(ep
->name
.eep
+ i
)) != '=' &&
88 c
!= '\0' && i
< CONFIG_SYS_ENV_NAMELEN
) {
93 if (i
> 0 && c
!= '=') {
94 debug("** ee_name: '%s' not '=' terminated!\n", buf
);
97 strncpy(buf
, ep
->name
.ram
, CONFIG_SYS_ENV_NAMELEN
);
98 i
= strnlen(buf
, CONFIG_SYS_ENV_NAMELEN
);
106 uint16_t varval_get(char *buf
, uint16_t index
, int len
)
111 while ((c
= env_get_char (index
+ i
)) != '\0' && i
< len
) {
118 /* TODO: len check */
124 int comp_env_key_item(const void *key
, const void *item
)
126 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
127 env_item_t
*ep
= (env_item_t
*) item
;
129 varname_get(buf
, ep
);
131 return strcmp((char *) key
, buf
);
135 int comp_env_items(const void *m1
, const void *m2
)
137 char b1
[CONFIG_SYS_ENV_NAMELEN
+1];
138 char b2
[CONFIG_SYS_ENV_NAMELEN
+1];
140 env_item_t
*ep1
= (env_item_t
*) m1
;
141 env_item_t
*ep2
= (env_item_t
*) m2
;
143 varname_get(b1
, ep1
);
144 varname_get(b2
, ep2
);
146 return strcmp(b1
, b2
);
151 int envlist_import(void)
153 char name
[CONFIG_SYS_ENV_NAMELEN
+1];
158 e
.flags
= EF_N_EEP
| EF_V_EEP
;
160 while ((nlen
= varname_get(name
, &e
)) != 0 && idx
< ENV_SIZE
) {
162 if (entrycount
<= CONFIG_ENVVAR_MAX
) {
163 e
.val
.eep
= idx
+ nlen
+ 1;
165 env_list
[entrycount
++] = e
;
168 while (env_get_char(idx
++) != 0 && idx
< ENV_SIZE
)
172 debug("** Too many environment variables!\n");
176 qsort(env_list
, entrycount
, sizeof(env_item_t
), comp_env_items
);
183 int set_default_env(void)
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
;
191 if (env_valid
== 1) {
192 eep
= CONFIG_ENV_OFFSET
+ offsetof(env_t
, data
) + CONFIG_ENV_SIZE
;
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);
202 eeprom_update_block(buf
, (char *) eep
, i
);
203 if (c
== 0 && c0
== 0)
205 if (len
> sizeof(buf
))
216 uint16_t env_crc(uint16_t data_offset
)
224 for (i
= 0; !(c
== 0 && c0
== 0) && i
< ENV_SIZE
; i
++)
227 c
= eeprom_read_byte((const uint8_t *) data_offset
+ i
);
238 int env_check_valid(void)
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];
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
));
253 eeprom_read_word((uint16_t *) offset
[0] +
254 offsetof(env_t
, crc
))
255 == env_crc(offset
[0] + offsetof(env_t
, data
))
258 eeprom_read_word((uint16_t *) offset
[1] +
259 offsetof(env_t
, crc
))
260 == env_crc(offset
[1] + offsetof(env_t
, data
))
263 if (!crc_ok
[0] && !crc_ok
[1]) {
266 } else if (crc_ok
[0] && !crc_ok
[1]) {
268 } else if (!crc_ok
[0] && crc_ok
[1]) {
271 /* both ok - check serial */
273 if (flags
[1] == ACTIVE_FLAG
&& flags
[0] != ACTIVE_FLAG
)
275 else if (flags
[1] == OBSOLETE_FLAG
&& flags
[0] == 0xFF)
280 if (flags
[0] == ACTIVE_FLAG
&& flags
[1] == OBSOLETE_FLAG
)
282 else if (flags
[0] == OBSOLETE_FLAG
&& flags
[1] == ACTIVE_FLAG
)
284 else if (flags
[0] == 0xFF && flags
[1] == 0)
286 else if (flags
[1] == 0xFF && flags
[0] == 0)
288 else /* flags are equal - almost impossible */
299 env_valid
= env_check_valid();
300 if (env_valid
== 0) {
301 printf_P(PSTR("*** Warning - bad CRC, "
302 "using default environment\n\n"));
312 env_item_t
*envlist_search(const char *name
)
314 return bsearch(name
, env_list
, entrycount
,
315 sizeof(env_item_t
), comp_env_key_item
);
320 env_item_t
*envlist_enter(env_item_t
*e
)
322 char *key
= e
->name
.ram
;
323 const size_t size
= sizeof(env_item_t
);
326 ep
= bsearch(key
, env_list
, entrycount
,
327 size
, comp_env_key_item
);
330 if (entrycount
< CONFIG_ENVVAR_MAX
) {
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
);
338 if ((ep
->flags
& EF_V_EEP
) == 0) {
341 ep
->val
.ram
= e
->val
.ram
;
345 ep
->flags
|= EF_DIRTY
;
346 ep
->flags
&= ~EF_V_EEP
;
348 if ((ep
->flags
& EF_N_EEP
) == 0) {
349 int nlen
= strnlen(key
, CONFIG_SYS_ENV_NAMELEN
);
350 char *name
= xmalloc(nlen
+ 1);
352 printf_P(PSTR("## Can't malloc %d bytes\n"),
367 int env_item_delete(env_item_t
*ep
)
371 if ((ep
->flags
& EF_V_EEP
) == 0)
373 if ((ep
->flags
& EF_N_EEP
) == 0)
377 size_t size
= sizeof(env_item_t
);
378 memmove(ep
, ep
+ 1, (env_list
- ep
)*size
+ entrycount
*size
);
384 int envlist_delete(const char *name
)
386 env_item_t
*ep
= bsearch(name
, env_list
, entrycount
,
387 sizeof(env_item_t
), comp_env_key_item
);
390 return env_item_delete(ep
);
397 env_item_t
*envlist_get(const char *name
, uint_fast8_t flag
)
401 ep
= envlist_search(name
);
403 if (ep
!= NULL
&& (flag
& ENV_GET_VAL
)) {
404 if (ep
->flags
& EF_V_EEP
) {
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
;
420 char *getenv(const char *name
)
425 ep
= envlist_get(name
, ENV_GET_VAL
);
432 int env_item_save(env_item_t
*ep
, uint16_t offset
, int space_left
)
434 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
438 len
= varname_get(buf
, ep
);
447 eeprom_update_block(buf
, (uint8_t *) offset
, len
);
450 if (e
.val
.ram
!= NULL
) {
453 if (e
.flags
& EF_V_EEP
)
454 c
= env_get_char(e
.val
.eep
++);
458 eeprom_update_byte((uint8_t *) offset
, c
);
462 } while ((c
!= '\0') && space_left
);
471 unsigned int off
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
472 unsigned int off_red
= CONFIG_ENV_OFFSET
;
478 if (env_valid
== 2) {
479 off
= CONFIG_ENV_OFFSET
;
480 off_red
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
483 eeprom_update_byte((uint8_t *) off
+ offsetof(env_t
, flags
), 0xff);
485 pos
= off
+ offsetof(env_t
, data
);
487 for (int i
= 0 ; i
< entrycount
; i
++) {
488 len
= env_item_save(&env_list
[i
], pos
, left
);
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
),
503 while (entrycount
!= 0) {
504 env_item_delete(&env_list
[entrycount
-1]);
506 eeprom_update_byte((uint8_t *) off_red
+ offsetof(env_t
, flags
),
508 env_valid
= (env_valid
== 2) ? 1 : 2;
518 int env_item_print(env_item_t
*ep
)
520 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
524 varname_get(buf
, ep
);
525 len
= printf_P(PSTR("%s="), buf
);
527 if (e
.val
.ram
!= NULL
) {
530 if (e
.flags
& EF_V_EEP
)
531 c
= env_get_char(e
.val
.eep
++);
550 * Command interface: print one or all environment variables
552 * Returns -1 in case of error, or length of printed string
555 int env_print(char *name
)
559 if (name
) { /* print a single name */
561 env_item_t
*ep
= envlist_search(name
);
563 len
= env_item_print(ep
);
565 } else { /* print whole list */
567 for (int i
= 0 ; i
< entrycount
; i
++) {
568 len
+= env_item_print(&env_list
[i
]);
575 int env_print_ramsize(void)
578 uint8_t name_cnt
= 0;
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
)) {
585 size
+= strlen(env_list
[i
].name
.ram
) + 3;
587 if ((env_list
[i
].flags
& EF_V_EEP
) == 0 &&
588 (env_list
[i
].val
.ram
!= NULL
)) {
590 size
+= strlen(env_list
[i
].val
.ram
) + 3;
593 printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
594 size
, name_cnt
, val_cnt
);
599 int do_env_print(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
604 (void) cmdtp
; (void) flag
;
607 /* print all env vars */
608 rcode
= env_print(NULL
);
611 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
617 /* print selected env vars */
618 for (i
= 1; i
< argc
; ++i
) {
619 int rc
= env_print(argv
[i
]);
621 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
631 * Set or delete environment variable
633 * Set a new environment variable,
634 * or replace or delete an existing one.
636 * @param flag (not used)
638 * @param argv[1] Environment variable to set or delete
640 * @param argv[2] ... Value to set it to
642 * @return 0 if ok, 1 on error
645 int _do_env_set(int flag
, int argc
, char * const argv
[])
648 char *name
, *value
, *s
;
652 debug("Initial value for argc=%d\n", argc
);
657 if (strchr(name
, '=')) {
658 printf_P(PSTR("## Error: illegal character '='"
659 "in variable name \"%s\"\n"), name
);
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
);
671 if (argc
< 3 || argv
[2] == NULL
) {
672 int rc
= envlist_delete(name
);
677 * Insert / replace new value
679 for (i
= 2, len
= 0; i
< argc
; ++i
)
680 len
+= strlen(argv
[i
]) + 1;
682 value
= xmalloc(len
);
684 printf_P(PSTR("## Can't malloc %d bytes\n"), len
);
687 for (i
= 2, s
= value
; i
< argc
; ++i
) {
690 while ((*s
++ = *v
++) != '\0')
700 ep
= envlist_enter(&e
);
702 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
711 * Set an environment variable
713 * @param varname Environment variable to set
714 * @param varvalue Value to set it to
715 * @return 0 if ok, 1 on error
718 int setenv(const char *varname
, const char *varvalue
)
720 const char * const argv
[3] = { NULL
, varname
, varvalue
};
723 if (varvalue
== NULL
|| varvalue
[0] == '\0')
726 return _do_env_set(0, argc
, (char * const *)argv
);
731 * Set an environment variable to an integer value
733 * @param varname Environment variable to set
734 * @param value Value to set it to
735 * @return 0 if ok, 1 on error
737 int setenv_ulong(const char *varname
, unsigned long value
)
739 /* TODO: this should be unsigned */
740 char *str
= simple_itoa(value
);
742 return setenv(varname
, str
);
748 * Set an environment variable to an value in hex
750 * @param varname Environment variable to set
751 * @param value Value to set it to
752 * @return 0 if ok, 1 on error
754 int setenv_hex(const char *varname
, unsigned long value
)
756 char str
[sizeof(unsigned long) *2 + 1];
758 sprintf_P(str
, PSTR("%lx"), value
);
759 return setenv(varname
, str
);
763 * Get an environment variable as a hex value
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
769 unsigned long getenv_hex(const char *varname
, unsigned long default_val
)
777 value
= strtoul(s
, &endp
, 16);
784 int do_env_set(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
789 return CMD_RET_USAGE
;
791 return _do_env_set(flag
, argc
, argv
);
795 int do_env_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
797 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
799 printf_P(PSTR("Saving Environment ...\n"));
800 return saveenv() ? 1 : 0;
803 #if defined(CONFIG_AUTO_COMPLETE)
804 int env_complete(char *var
, int maxv
, char *cmdv
[], int bufsz
, char *buf
)
813 while ((idx
= hmatch_r(var
, idx
, &match
, &env_htab
))) {
814 int vallen
= strlen(match
->key
) + 1;
816 if (found
>= maxv
- 2 || bufsz
< vallen
)
820 memcpy(buf
, match
->key
, vallen
);
825 qsort(cmdv
, found
, sizeof(cmdv
[0]), strcmp_compar
);
828 cmdv
[found
++] = "...";