]>
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_getlen(uint16_t index
)
111 while ((c
= env_get_char (index
+ i
)) != '\0') {
119 uint16_t varval_get(char *buf
, uint16_t index
, int len
)
124 while ((c
= env_get_char (index
+ i
)) != '\0' && i
< len
) {
131 /* TODO: len check */
137 int comp_env_key_item(const void *key
, const void *item
)
139 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
140 env_item_t
*ep
= (env_item_t
*) item
;
142 varname_get(buf
, ep
);
144 return strcmp((char *) key
, buf
);
148 int comp_env_items(const void *m1
, const void *m2
)
150 char b1
[CONFIG_SYS_ENV_NAMELEN
+1];
151 char b2
[CONFIG_SYS_ENV_NAMELEN
+1];
153 env_item_t
*ep1
= (env_item_t
*) m1
;
154 env_item_t
*ep2
= (env_item_t
*) m2
;
156 varname_get(b1
, ep1
);
157 varname_get(b2
, ep2
);
159 return strcmp(b1
, b2
);
164 int envlist_import(void)
166 char name
[CONFIG_SYS_ENV_NAMELEN
+1];
171 e
.flags
= EF_N_EEP
| EF_V_EEP
;
173 while ((nlen
= varname_get(name
, &e
)) != 0 && idx
< ENV_SIZE
) {
175 if (entrycount
<= CONFIG_ENVVAR_MAX
) {
176 e
.val
.eep
= idx
+ nlen
+ 1;
178 env_list
[entrycount
++] = e
;
181 while (env_get_char(idx
++) != 0 && idx
< ENV_SIZE
)
185 debug("** Too many environment variables!\n");
189 qsort(env_list
, entrycount
, sizeof(env_item_t
), comp_env_items
);
196 int set_default_env(void)
199 uint16_t eep
= CONFIG_ENV_OFFSET
+ offsetof(env_t
, data
);
200 unsigned int len
= ENV_SIZE
;
201 unsigned int i
, src
= 0;
202 char c
= 0xff, c0
= c
;
204 if (env_valid
== 1) {
205 eep
= CONFIG_ENV_OFFSET
+ offsetof(env_t
, data
) + CONFIG_ENV_SIZE
;
209 memcpy_P(buf
, default_env
+src
, sizeof(buf
));
210 for (i
=0; i
< (len
< sizeof(buf
) ? len
: sizeof(buf
)) &&
211 !(c
== 0 && c0
== 0);
215 eeprom_update_block(buf
, (char *) eep
, i
);
216 if (c
== 0 && c0
== 0)
218 if (len
> sizeof(buf
))
229 uint16_t env_crc(uint16_t data_offset
)
237 for (i
= 0; !(c
== 0 && c0
== 0) && i
< ENV_SIZE
; i
++)
240 c
= eeprom_read_byte((const uint8_t *) data_offset
+ i
);
251 int env_check_valid(void)
253 const uint16_t offset
[2] = {CONFIG_ENV_OFFSET
,
254 CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
};
255 uint_fast8_t flags
[2], crc_ok
[2];
259 flags
[0] = eeprom_read_byte ((uint8_t *) offset
[0] +
260 offsetof(env_t
, flags
));
261 flags
[1] = eeprom_read_byte ((uint8_t *) offset
[1] +
262 offsetof(env_t
, flags
));
266 eeprom_read_word((uint16_t *) offset
[0] +
267 offsetof(env_t
, crc
))
268 == env_crc(offset
[0] + offsetof(env_t
, data
))
271 eeprom_read_word((uint16_t *) offset
[1] +
272 offsetof(env_t
, crc
))
273 == env_crc(offset
[1] + offsetof(env_t
, data
))
276 if (!crc_ok
[0] && !crc_ok
[1]) {
279 } else if (crc_ok
[0] && !crc_ok
[1]) {
281 } else if (!crc_ok
[0] && crc_ok
[1]) {
284 /* both ok - check serial */
286 if (flags
[1] == ACTIVE_FLAG
&& flags
[0] != ACTIVE_FLAG
)
288 else if (flags
[1] == OBSOLETE_FLAG
&& flags
[0] == 0xFF)
293 if (flags
[0] == ACTIVE_FLAG
&& flags
[1] == OBSOLETE_FLAG
)
295 else if (flags
[0] == OBSOLETE_FLAG
&& flags
[1] == ACTIVE_FLAG
)
297 else if (flags
[0] == 0xFF && flags
[1] == 0)
299 else if (flags
[1] == 0xFF && flags
[0] == 0)
301 else /* flags are equal - almost impossible */
312 env_valid
= env_check_valid();
313 if (env_valid
== 0) {
314 printf_P(PSTR("*** Warning - bad CRC, "
315 "using default environment\n\n"));
325 env_item_t
*envlist_search(const __memx
char *name
)
327 return bsearch(name
, env_list
, entrycount
,
328 sizeof(env_item_t
), comp_env_key_item
);
333 env_item_t
*envlist_enter(env_item_t
*e
)
335 char *key
= e
->name
.ram
;
336 const size_t size
= sizeof(env_item_t
);
339 ep
= bsearch(key
, env_list
, entrycount
,
340 size
, comp_env_key_item
);
343 if (entrycount
< CONFIG_ENVVAR_MAX
) {
345 env_list
[entrycount
++] = *e
;
346 qsort(env_list
, entrycount
, size
, comp_env_items
);
347 ep
= bsearch(key
, env_list
, entrycount
,
348 size
, comp_env_key_item
);
351 if ((ep
->flags
& EF_V_EEP
) == 0) {
354 ep
->val
.ram
= e
->val
.ram
;
358 ep
->flags
|= EF_DIRTY
;
359 ep
->flags
&= ~EF_V_EEP
;
361 if ((ep
->flags
& EF_N_EEP
) == 0) {
362 int nlen
= strnlen(key
, CONFIG_SYS_ENV_NAMELEN
);
363 char *name
= xmalloc(nlen
+ 1);
365 printf_P(PSTR("## Can't malloc %d bytes\n"),
380 int env_item_delete(env_item_t
*ep
)
384 if ((ep
->flags
& EF_V_EEP
) == 0)
386 if ((ep
->flags
& EF_N_EEP
) == 0)
390 size_t size
= sizeof(env_item_t
);
391 memmove(ep
, ep
+ 1, (env_list
- ep
)*size
+ entrycount
*size
);
397 int envlist_delete(const char *name
)
399 env_item_t
*ep
= bsearch(name
, env_list
, entrycount
,
400 sizeof(env_item_t
), comp_env_key_item
);
403 return env_item_delete(ep
);
410 env_item_t
*envlist_get(const __memx
char *name
, uint_fast8_t flag
)
414 ep
= envlist_search(name
);
416 if (ep
!= NULL
&& (flag
& ENV_GET_VAL
)) {
417 if (ep
->flags
& EF_V_EEP
) {
420 len
= varval_getlen(ep
->val
.eep
);
421 if (len
> CONFIG_SYS_CBSIZE
)
422 len
= CONFIG_SYS_CBSIZE
;
423 vp
= xmalloc(len
+ 1);
425 varval_get(vp
, ep
->val
.eep
, len
+ 1);
427 ep
->flags
&= ~EF_V_EEP
;
429 printf_P(PSTR("Out of memory!\n"));
437 char *getenv(const __memx
char *name
)
442 ep
= envlist_get(name
, ENV_GET_VAL
);
449 int env_item_save(env_item_t
*ep
, uint16_t offset
, int space_left
)
451 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
455 len
= varname_get(buf
, ep
);
464 eeprom_update_block(buf
, (uint8_t *) offset
, len
);
467 if (e
.val
.ram
!= NULL
) {
470 if (e
.flags
& EF_V_EEP
)
471 c
= env_get_char(e
.val
.eep
++);
475 eeprom_update_byte((uint8_t *) offset
, c
);
479 } while ((c
!= '\0') && space_left
);
488 unsigned int off
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
489 unsigned int off_red
= CONFIG_ENV_OFFSET
;
495 if (env_valid
== 2) {
496 off
= CONFIG_ENV_OFFSET
;
497 off_red
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
500 eeprom_update_byte((uint8_t *) off
+ offsetof(env_t
, flags
), 0xff);
502 pos
= off
+ offsetof(env_t
, data
);
504 for (int i
= 0 ; i
< entrycount
; i
++) {
505 len
= env_item_save(&env_list
[i
], pos
, left
);
513 eeprom_update_byte((uint8_t *) pos
, 0);
514 crc
= env_crc(off
+ offsetof(env_t
, data
));
515 eeprom_update_word((uint16_t *) off
+ offsetof(env_t
, crc
), crc
);
516 eeprom_update_byte((uint8_t *) off
+ offsetof(env_t
, flags
),
520 while (entrycount
!= 0) {
521 env_item_delete(&env_list
[entrycount
-1]);
523 eeprom_update_byte((uint8_t *) off_red
+ offsetof(env_t
, flags
),
525 env_valid
= (env_valid
== 2) ? 1 : 2;
535 int env_item_print(env_item_t
*ep
)
537 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
541 varname_get(buf
, ep
);
542 len
= printf_P(PSTR("%s="), buf
);
544 if (e
.val
.ram
!= NULL
) {
547 if (e
.flags
& EF_V_EEP
)
548 c
= env_get_char(e
.val
.eep
++);
567 * Command interface: print one or all environment variables
569 * Returns -1 in case of error, or length of printed string
572 int env_print(char *name
)
576 if (name
) { /* print a single name */
578 env_item_t
*ep
= envlist_search(name
);
580 len
= env_item_print(ep
);
582 } else { /* print whole list */
584 for (int i
= 0 ; i
< entrycount
; i
++) {
585 len
+= env_item_print(&env_list
[i
]);
592 int env_print_ramsize(void)
595 uint8_t name_cnt
= 0;
598 for (int i
= 0 ; i
< entrycount
; i
++) {
599 if ((env_list
[i
].flags
& EF_N_EEP
) == 0 &&
600 (env_list
[i
].name
.ram
!= NULL
)) {
602 size
+= strlen(env_list
[i
].name
.ram
) + 3;
604 if ((env_list
[i
].flags
& EF_V_EEP
) == 0 &&
605 (env_list
[i
].val
.ram
!= NULL
)) {
607 size
+= strlen(env_list
[i
].val
.ram
) + 3;
610 printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
611 size
, name_cnt
, val_cnt
);
616 command_ret_t
do_env_print(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
618 command_ret_t rc
= CMD_RET_SUCCESS
;
620 (void) cmdtp
; (void) flag
;
623 /* print all env vars */
624 int size
= env_print(NULL
);
626 return CMD_RET_FAILURE
;
627 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
630 return CMD_RET_SUCCESS
;
633 /* print selected env vars */
634 for (int i
= 1; i
< argc
; ++i
) {
635 int rc
= env_print(argv
[i
]);
637 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
638 rc
= CMD_RET_FAILURE
;
647 * Set or delete environment variable
649 * Set a new environment variable,
650 * or replace or delete an existing one.
652 * @param flag (not used)
654 * @param argv[1] Environment variable to set or delete
656 * @param argv[2] ... Value to set it to
658 * @return 0 if ok, 1 on error
661 command_ret_t
_do_env_set(int flag
, int argc
, char * const argv
[])
664 char *name
, *value
, *s
;
668 debug("Initial value for argc=%d\n", argc
);
673 if (strchr(name
, '=')) {
674 printf_P(PSTR("## Error: illegal character '='"
675 "in variable name \"%s\"\n"), name
);
676 return CMD_RET_FAILURE
;
678 if (strlen(name
) > CONFIG_SYS_ENV_NAMELEN
) {
679 printf_P(PSTR("## Error: Variable name \"%s\" too long. "
680 "(max %d characters)\n"), name
, CONFIG_SYS_ENV_NAMELEN
);
681 return CMD_RET_FAILURE
;
687 if (argc
< 3 || argv
[2] == NULL
) {
688 int rc
= envlist_delete(name
);
689 return rc
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
693 * Insert / replace new value
695 for (i
= 2, len
= 0; i
< argc
; ++i
)
696 len
+= strlen(argv
[i
]) + 1;
698 value
= xmalloc(len
);
700 printf_P(PSTR("## Can't malloc %d bytes\n"), len
);
701 return CMD_RET_FAILURE
;
703 for (i
= 2, s
= value
; i
< argc
; ++i
) {
706 while ((*s
++ = *v
++) != '\0')
716 ep
= envlist_enter(&e
);
718 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
720 return CMD_RET_FAILURE
;
723 return CMD_RET_SUCCESS
;
727 * Set an environment variable
729 * @param varname Environment variable to set
730 * @param varvalue Value to set it to
731 * @return 0 if ok, 1 on error
734 int setenv(const char *varname
, const char *varvalue
)
736 const char * const argv
[3] = { NULL
, varname
, varvalue
};
739 if (varvalue
== NULL
|| varvalue
[0] == '\0')
742 return (int) _do_env_set(0, argc
, (char * const *)argv
);
747 * Set an environment variable to an integer value
749 * @param varname Environment variable to set
750 * @param value Value to set it to
751 * @return 0 if ok, 1 on error
753 int setenv_ulong(const char *varname
, unsigned long value
)
755 /* TODO: this should be unsigned */
756 char *str
= simple_itoa(value
);
758 return setenv(varname
, str
);
764 * Set an environment variable to an value in hex
766 * @param varname Environment variable to set
767 * @param value Value to set it to
768 * @return 0 if ok, 1 on error
770 int setenv_hex(const char *varname
, unsigned long value
)
772 char str
[sizeof(unsigned long) *2 + 1];
774 sprintf_P(str
, PSTR("%lx"), value
);
775 return setenv(varname
, str
);
780 * Decode the integer value of an environment variable and return it.
782 * @param name Name of environemnt variable
783 * @param base Number base to use (normally 10, or 16 for hex)
784 * @param default_val Default value to return if the variable is not
786 * @return the decoded value, or default_val if not found
788 unsigned long getenv_ulong(const __memx
char *name
, int base
, unsigned long default_val
)
794 env_item_t
*ep
= envlist_search(name
);
797 if (ep
->flags
& EF_V_EEP
) {
798 varval_get(buf
, ep
->val
.eep
, sizeof(buf
));
803 value
= strtoul(vp
, &endp
, base
);
813 command_ret_t
do_env_set(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
818 return CMD_RET_USAGE
;
820 return _do_env_set(flag
, argc
, argv
);
824 command_ret_t
do_env_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
826 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
828 printf_P(PSTR("Saving Environment ...\n"));
829 return saveenv() ? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
832 #if defined(CONFIG_AUTO_COMPLETE)
833 int env_complete(char *var
, int maxv
, char *cmdv
[], int bufsz
, char *buf
)
842 while ((idx
= hmatch_r(var
, idx
, &match
, &env_htab
))) {
843 int vallen
= strlen(match
->key
) + 1;
845 if (found
>= maxv
- 2 || bufsz
< vallen
)
849 memcpy(buf
, match
->key
, vallen
);
854 qsort(cmdv
, found
, sizeof(cmdv
[0]), strcmp_compar
);
857 cmdv
[found
++] = "...";