]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/env.c
9063a17f8a259a0de93a6b3838a2db6bb5186bfb
5 #include <avr/eeprom.h>
16 #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
18 #define OBSOLETE_FLAG 0
24 #define ENV_GET_VAL (1<<0)
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
44 * TODO: move elsewhere
48 static void print_blanks(uint_fast8_t count
)
55 void dump_ram(const uint8_t *startaddr
, int len
, char *title
)
58 uint8_t pre
= (size_t) startaddr
% 16;
59 const uint8_t *addr
= (uint8_t *) ((size_t) startaddr
& ~0x0f);
64 printf_P(PSTR("%s\n"),title
);
70 printf_P(PSTR(" %.4x:"), (size_t) addr
);
71 print_blanks(3 * pre
);
72 for (i
= pre
; i
< llen
; i
++)
73 printf_P(PSTR(" %.2x"), addr
[i
]);
74 print_blanks(3 * (16 - i
+ 1) + pre
);
75 for (i
= pre
; i
< llen
; i
++)
76 printf_P(PSTR("%c"), isprint(addr
[i
]) ? addr
[i
] : '.');
87 extern unsigned int __brkval
;
89 dump_ram((uint8_t *) __malloc_heap_start
,
90 __brkval
- (unsigned int) __malloc_heap_start
,
94 /* TODO: combine with dump_ram() */
95 void dump_eep(const uint8_t *addr
, unsigned int len
)
100 printf_P(PSTR("eeprom dump:"));
102 printf_P(PSTR("\n 0x%.4x:"), (unsigned int) addr
);
103 for (i
= 0; i
<16; i
++)
104 buf
[i
] = eeprom_read_byte(addr
+ i
);
105 for (i
= 0; i
<16; i
++)
106 printf_P(PSTR(" %.2x"), buf
[i
]);
108 for (i
= 0; i
<16; i
++)
109 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
112 len
-= len
> 16 ? 16 : len
;
121 int do_dump_eep(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
129 /* We use the last specified parameters, unless new ones are
136 return CMD_RET_USAGE
;
138 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
139 /* Address is specified since argc > 1 */
140 addr
= (const uint8_t *) (size_t) strtoul(argv
[1], NULL
, 16);
142 /* If another parameter, it is the length to display. */
144 length
= (uint16_t) strtoul(argv
[2], NULL
, 16);
147 /* Print the lines. */
148 dump_eep(addr
, length
);
153 int do_eep_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
155 uint16_t src
, dest
, count
;
162 return CMD_RET_USAGE
;
164 src
= (size_t) strtoul(argv
[1], NULL
, 16);
165 dest
= (size_t) strtoul(argv
[2], NULL
, 16);
166 count
= (size_t) strtoul(argv
[3], NULL
, 16);
169 debug("src > EEPROM size: 0x%04x\n", src
);
173 debug("dest > EEPROM size: 0x%04x\n", dest
);
176 if (count
> E2END
+1) {
177 debug("count > EEPROM size: 0x%04x\n", count
);
181 debug("Zero length ???\n");
192 while (count
-- > 0) {
194 data
= eeprom_read_byte((uint8_t *) src
);
195 eeprom_write_byte((uint8_t *) dest
, data
);
205 typedef struct environment_s
{
206 uint16_t crc
; /* CRC16 over data bytes */
207 uint8_t flags
; /* active/obsolete flags */
208 char data
[ENV_SIZE
]; /* Environment data */
213 typedef struct env_item_s
{
214 #define EF_N_EEP (1<<7) /* Variable name is in EEPROM */
215 #define EF_V_EEP (1<<6) /* Variable value is in EEPROM */
216 #define EF_DIRTY (1<<0) /* Variable is new or value changed */
228 static uint8_t env_valid
;
229 static env_item_t env_list
[CONFIG_ENVVAR_MAX
];
230 static int entrycount
;
233 static char env_get_char(uint16_t index
)
235 unsigned int off
= CONFIG_ENV_OFFSET
;
238 off
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
240 return (char) eeprom_read_byte((const uint8_t *)off
+ index
+
241 offsetof(env_t
, data
));
245 uint16_t ee_name_get(char *buf
, env_item_t
*ep
)
250 if (ep
->flags
& EF_N_EEP
) {
252 while ((c
= env_get_char(ep
->name
.eep
+ i
)) != '=' &&
253 c
!= '\0' && i
< CONFIG_SYS_ENV_NAMELEN
) {
258 if (i
> 0 && c
!= '=') {
259 debug("** ee_name: '%s' not '=' terminated!\n", buf
);
262 strncpy(buf
, ep
->name
.ram
, CONFIG_SYS_ENV_NAMELEN
);
263 i
= strnlen(buf
, CONFIG_SYS_ENV_NAMELEN
);
270 uint16_t ee_val_get(char *buf
, uint16_t index
, int len
)
275 while ((c
= env_get_char (index
+ i
)) != '\0' && i
< len
) {
282 /* TODO: len check */
288 int comp_env_key_item(const void *key
, const void *item
)
290 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
291 env_item_t
*ep
= (env_item_t
*) item
;
293 ee_name_get(buf
, ep
);
295 return strcmp((char *) key
, buf
);
299 int comp_env_items(const void *m1
, const void *m2
)
301 char b1
[CONFIG_SYS_ENV_NAMELEN
+1];
302 char b2
[CONFIG_SYS_ENV_NAMELEN
+1];
304 env_item_t
*ep1
= (env_item_t
*) m1
;
305 env_item_t
*ep2
= (env_item_t
*) m2
;
307 ee_name_get(b1
, ep1
);
308 ee_name_get(b2
, ep2
);
310 return strcmp(b1
, b2
);
314 env_item_t
* ee_entry_get(const char *s
, env_item_t
*ep
, uint_fast8_t flag
)
316 char name
[CONFIG_SYS_ENV_NAMELEN
+1];
320 //printf_P(PSTR("*** ee_entry_get: >>>>>>>>> ENTER <<<<<<<<<\n"));
323 while ((nlen
= ee_name_get(name
, idx
)) != 0 && idx
< CONFIG_ENV_SIZE
) {
325 //printf_P(PSTR("** idx: %d, name: '%s', s: '%s', nlen: %d, cpres:%d\n"),
326 // idx, name, s, nlen, strncmp(name, s, nlen));
328 if (strncmp(name
, s
, nlen
) == 0)
332 while (env_get_char(idx
++) != 0)
338 ep
->flags
= EF_N_EEP
;
340 if (flag
& ENV_GET_VAL
) {
341 vp
= xmalloc(CONFIG_SYS_CBSIZE
);
342 nlen
= ee_val_get(vp
, idx
+ nlen
+ 1, CONFIG_SYS_CBSIZE
);
343 ep
->val
= xrealloc(vp
, nlen
+ 1);
348 //printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE 0x%.4x <<<<<\n"),
349 // (unsigned int) ep);
353 //printf_P(PSTR("*** ee_entry_get: >>>>>> LEAVE <NULL> <<<<<\n"));
359 static char p_env_name_buf
[CONFIG_SYS_ENV_NAMELEN
+1];
361 static char *dbg_p_env_name(env_item_t
*p
)
363 if (p
->flags
& EF_N_EEP
) {
364 if (ee_name_get(p_env_name_buf
, p
) != 0)
365 return p_env_name_buf
;
369 return "<NO EEP_NAME>";
373 int env_item_print(env_item_t
*ep
)
375 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
379 ee_name_get(buf
, ep
);
380 len
= printf_P(PSTR("%s="), buf
);
382 if (e
.val
.ram
!= NULL
) {
385 if (e
.flags
& EF_V_EEP
)
386 c
= env_get_char(e
.val
.eep
++);
404 int env_item_save(env_item_t
*ep
, uint16_t offset
, int space_left
)
406 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
410 debug("-- env_item_save(%04x, %04x, %d)\n",
411 ep
, offset
, space_left
);
413 len
= ee_name_get(buf
, ep
);
419 if ((unsigned) len
< sizeof(buf
))
420 buf
[len
] = '\0'; /* terminate for debugging */
421 debug(" len: %d, buf: '%s', space_left: %d\n", len
, buf
, space_left
);
426 eeprom_update_block(buf
, (uint8_t *) offset
, len
);
429 if (e
.val
.ram
!= NULL
) {
432 if (e
.flags
& EF_V_EEP
)
433 c
= env_get_char(e
.val
.eep
++);
437 eeprom_update_byte((uint8_t *) offset
, c
);
441 } while ((c
!= '\0') && space_left
);
451 int env_item_update(env_item_t
*ep
)
453 char buf
[CONFIG_SYS_ENV_NAMELEN
+1];
458 /* get name from old loc. (eeprom or ram */
459 len
= ee_name_get(buf
, ep
);
462 debug("-- env_item_update(%04x)\n", ep
);
463 if (len
< sizeof(buf
))
464 buf
[len
] = '\0'; /* terminate for debugging */
465 debug(" len: %d, buf: '%s'\n", len
, buf
);
467 /* search this name in new eeprom env */
468 /* TODO: eliminate this ugly hack */
469 uint8_t save_env_valid
= env_valid
;
470 env_valid
= (env_valid
== 2) ? 1 : 2;
472 debug(" len: %d, buf: '%s', env_valid: %d\n", len
, buf
, env_valid
);
474 while ((c
= env_get_char(pos
)) != '\0' && pos
< (ENV_SIZE
- len
)) {
477 debug(" while: c: %02x, pos: %d\n ", c
, pos
);
479 while (c
== buf
[i
] && i
<= len
) {
484 c
= env_get_char(pos
+ i
);
487 debug("\n c: %02x, i: %d, pos: %d\n", c
, i
, pos
);
490 if ((ep
->flags
& EF_N_EEP
) == 0)
493 if ((ep
->flags
& EF_V_EEP
) == 0)
495 ep
->val
.eep
= pos
+ i
;
496 ep
->flags
&= ~EF_DIRTY
;
497 ep
->flags
|= EF_N_EEP
| EF_V_EEP
;
500 env_valid
= save_env_valid
;
504 while (((c
= env_get_char(pos
++)) != '\0') && pos
< (ENV_SIZE
- len
))
507 debug("\n c: %02x, i: %d, pos: %d\n", c
, i
, pos
);
511 env_valid
= save_env_valid
;
518 int envlist_import(void)
520 char name
[CONFIG_SYS_ENV_NAMELEN
+1];
525 e
.flags
= EF_N_EEP
| EF_V_EEP
;
527 while ((nlen
= ee_name_get(name
, &e
)) != 0 && idx
< ENV_SIZE
) {
529 if (entrycount
<= CONFIG_ENVVAR_MAX
) {
530 e
.val
.eep
= idx
+ nlen
+ 1;
532 env_list
[entrycount
++] = e
;
535 while (env_get_char(idx
++) != 0 && idx
< ENV_SIZE
)
539 debug("** Too many environment variables!\n");
543 qsort(env_list
, entrycount
, sizeof(env_item_t
), comp_env_items
);
548 static env_item_t
*envlist_search(const char *name
)
550 return bsearch(name
, env_list
, entrycount
,
551 sizeof(env_item_t
), comp_env_key_item
);
555 env_item_t
*envlist_insert(const char *key
, env_item_t
*e
)
557 const size_t size
= sizeof(env_item_t
);
559 if (entrycount
< CONFIG_ENVVAR_MAX
) {
560 env_list
[entrycount
++] = *e
;
561 qsort(env_list
, entrycount
, size
, comp_env_items
);
563 return bsearch(key
, env_list
, entrycount
,
564 size
, comp_env_key_item
);
571 env_item_t
*envlist_enter(env_item_t
*e
)
573 char *key
= e
->name
.ram
;
574 const size_t size
= sizeof(env_item_t
);
577 ep
= bsearch(key
, env_list
, entrycount
,
578 size
, comp_env_key_item
);
581 if (entrycount
< CONFIG_ENVVAR_MAX
) {
583 env_list
[entrycount
++] = *e
;
584 qsort(env_list
, entrycount
, size
, comp_env_items
);
585 ep
= bsearch(key
, env_list
, entrycount
,
586 size
, comp_env_key_item
);
589 if ((ep
->flags
& EF_V_EEP
) == 0) {
592 ep
->val
.ram
= e
->val
.ram
;
596 ep
->flags
|= EF_DIRTY
;
597 ep
->flags
&= ~EF_V_EEP
;
599 if ((ep
->flags
& EF_N_EEP
) == 0) {
600 int nlen
= strnlen(key
, CONFIG_SYS_ENV_NAMELEN
);
601 char *name
= xmalloc(nlen
+ 1);
603 printf_P(PSTR("## Can't malloc %d bytes\n"),
617 static env_item_t
*envlist_get(const char *name
, uint_fast8_t flag
)
621 ep
= envlist_search(name
);
623 if (ep
!= NULL
&& (flag
& ENV_GET_VAL
)) {
624 if (ep
->flags
& EF_V_EEP
) {
627 /* TODO: function that gets len of val,
628 to get rid of xrealloc */
629 vp
= xmalloc(CONFIG_SYS_CBSIZE
);
630 len
= ee_val_get(vp
, ep
->val
.eep
, CONFIG_SYS_CBSIZE
);
631 ep
->val
.ram
= xrealloc(vp
, len
+ 1);
632 ep
->flags
&= ~EF_V_EEP
;
639 int env_item_delete(env_item_t
*ep
)
643 if ((ep
->flags
& EF_V_EEP
) == 0)
645 if ((ep
->flags
& EF_N_EEP
) == 0)
649 size_t size
= sizeof(env_item_t
);
650 memmove(ep
, ep
+ 1, (env_list
- ep
)*size
+ entrycount
*size
);
655 static int envlist_delete(const char *name
)
657 env_item_t
*ep
= bsearch(name
, env_list
, entrycount
,
658 sizeof(env_item_t
), comp_env_key_item
);
661 return env_item_delete(ep
);
664 dump_ram((uint8_t *) &env_list
[0], entrycount
* sizeof(env_item_t
),
667 debug("** entrycount: %d\n", entrycount
);
668 for (int i
=0; i
<entrycount
; i
++) {
669 printf_P(PSTR("** env var [%d] "), i
);
670 env_item_print(&env_list
[i
]);
677 char *getenv(const char *name
)
682 debug("** getenv: %s\n", name
);
684 ep
= envlist_get(name
, ENV_GET_VAL
);
688 dump_ram((uint8_t *) &env_list
[0], entrycount
* sizeof(env_item_t
),
691 debug("** entrycount: %d\n", entrycount
);
692 for (int i
=0; i
<entrycount
; i
++) {
693 printf_P(PSTR("** env var [%d] "), i
);
694 env_item_print(&env_list
[i
]);
700 uint16_t env_crc(uint16_t data_offset
)
708 for (i
= 0; !(c
== 0 && c0
== 0) && i
< ENV_SIZE
; i
++)
711 c
= eeprom_read_byte((const uint8_t *) data_offset
+ i
);
719 unsigned int off
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
720 unsigned int off_red
= CONFIG_ENV_OFFSET
;
726 if (env_valid
== 2) {
727 off
= CONFIG_ENV_OFFSET
;
728 off_red
= CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
731 eeprom_update_byte((uint8_t *) off
+ offsetof(env_t
, flags
), 0xff);
733 pos
= off
+ offsetof(env_t
, data
);
735 for (int i
= 0 ; i
< entrycount
; i
++) {
736 len
= env_item_save(&env_list
[i
], pos
, left
);
744 eeprom_update_byte((uint8_t *) pos
, 0);
745 crc
= env_crc(off
+ offsetof(env_t
, data
));
746 eeprom_update_word((uint16_t *) off
+ offsetof(env_t
, crc
), crc
);
747 eeprom_update_byte((uint8_t *) off
+ offsetof(env_t
, flags
),
751 while (entrycount
!= 0) {
752 env_item_delete(&env_list
[entrycount
-1]);
754 eeprom_update_byte((uint8_t *) off_red
+ offsetof(env_t
, flags
),
756 env_valid
= (env_valid
== 2) ? 1 : 2;
764 /*-----------------------------------------------------------------------------*/
766 int set_default_env(void)
769 uint16_t eep
= CONFIG_ENV_OFFSET
+ offsetof(env_t
, data
);
770 unsigned int len
= ENV_SIZE
;
771 unsigned int i
, src
= 0;
772 char c
= 0xff, c0
= c
;
774 printf_P(PSTR("\n\n** set_default_env()\n"));
776 if (env_valid
== 1) {
777 eep
= CONFIG_ENV_OFFSET
+ offsetof(env_t
, data
) + CONFIG_ENV_SIZE
;
781 memcpy_P(buf
, default_env
+src
, sizeof(buf
));
782 for (i
=0; i
< (len
< sizeof(buf
) ? len
: sizeof(buf
)) &&
783 !(c
== 0 && c0
== 0);
787 eeprom_update_block(buf
, (char *) eep
, i
);
789 printf_P(PSTR("eeprom_update_block: eep: 0x%.4x, i:%d\n"),
790 (unsigned int) eep
, i
);
791 dump_ram((uint8_t *) buf
, i
, "=== buf:");
792 dump_eep((const uint8_t *) eep
, i
);
794 if (c
== 0 && c0
== 0)
796 if (len
> sizeof(buf
))
809 int env_check(uint16_t offset
)
811 uint16_t crc
, stored_crc
;
814 stored_crc
= eeprom_read_word(
815 (const uint16_t *) offset
+ offsetof(env_t
, crc
));
816 crc
= env_crc(offset
+ offsetof(env_t
, data
));
818 debug_cond((crc
!= stored_crc
),
819 "** crc eep: 0x%.4x, crc new: 0x%.4x\n", stored_crc
, crc
);
821 return crc
== stored_crc
;
826 unsigned int off_env
[2];
827 uint8_t flags
[2], crc_ok
[2];
829 off_env
[0] = CONFIG_ENV_OFFSET
;
830 off_env
[1] = CONFIG_ENV_OFFSET
+ CONFIG_ENV_SIZE
;
832 for (uint_fast8_t i
= 0; i
< 2; i
++) {
834 flags
[i
] = eeprom_read_byte ((uint8_t *) off_env
[i
] +
835 offsetof(env_t
, flags
));
839 eeprom_read_word((uint16_t *) off_env
[i
] +
840 offsetof(env_t
, crc
))
841 == env_crc(off_env
[i
] + offsetof(env_t
, data
))
845 if (!crc_ok
[0] && !crc_ok
[1]) {
847 debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n",
848 crc_ok
[0], crc_ok
[1], flags
[0], flags
[1], env_valid
);
851 } else if (crc_ok
[0] && !crc_ok
[1]) {
853 } else if (!crc_ok
[0] && crc_ok
[1]) {
856 /* both ok - check serial */
857 if (flags
[0] == ACTIVE_FLAG
&& flags
[1] == OBSOLETE_FLAG
)
859 else if (flags
[0] == OBSOLETE_FLAG
&& flags
[1] == ACTIVE_FLAG
)
861 else if (flags
[0] == 0xFF && flags
[1] == 0)
863 else if (flags
[1] == 0xFF && flags
[0] == 0)
865 else /* flags are equal - almost impossible */
869 debug("crc1: %02x, crc2: %02x, flag1 %02x, flag2 %02x, env_valid: %d\n",
870 crc_ok
[0], crc_ok
[1], flags
[0], flags
[1], env_valid
);
879 if (env_valid
== 0) {
880 printf_P(PSTR("*** Warning - bad CRC, "
881 "using default environment\n\n"));
891 * Command interface: print one or all environment variables
893 * Returns -1 in case of error, or length of printed string
895 static int env_print(char *name
)
899 if (name
) { /* print a single name */
901 env_item_t
*ep
= envlist_search(name
);
903 len
= env_item_print(ep
);
905 } else { /* print whole list */
907 for (int i
= 0 ; i
< entrycount
; i
++) {
908 len
+= env_item_print(&env_list
[i
]);
914 int env_print_ramsize(void)
917 uint8_t name_cnt
= 0;
920 for (int i
= 0 ; i
< entrycount
; i
++) {
921 if ((env_list
[i
].flags
& EF_N_EEP
) == 0 &&
922 (env_list
[i
].name
.ram
!= NULL
)) {
924 size
+= strlen(env_list
[i
].name
.ram
) + 3;
926 if ((env_list
[i
].flags
& EF_V_EEP
) == 0 &&
927 (env_list
[i
].val
.ram
!= NULL
)) {
929 size
+= strlen(env_list
[i
].val
.ram
) + 3;
932 printf_P(PSTR("%d bytes RAM used for %u names and %u values\n"),
933 size
, name_cnt
, val_cnt
);
937 int do_env_print(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
943 (void) cmdtp
; (void) flag
;
946 /* print all env vars */
947 rcode
= env_print(NULL
);
950 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
956 /* print selected env vars */
957 for (i
= 1; i
< argc
; ++i
) {
958 int rc
= env_print(argv
[i
]);
960 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
970 * Set or delete environment variable
972 * Set a new environment variable,
973 * or replace or delete an existing one.
975 * @param flag (not used)
977 * @param argv[1] Environment variable to set or delete
979 * @param argv[2] ... Value to set it to
981 * @return 0 if ok, 1 on error
983 static int _do_env_set(int flag
, int argc
, char * const argv
[])
986 char *name
, *value
, *s
;
990 debug("Initial value for argc=%d\n", argc
);
995 if (strchr(name
, '=')) {
996 printf_P(PSTR("## Error: illegal character '='"
997 "in variable name \"%s\"\n"), name
);
1000 if (strlen(name
) > CONFIG_SYS_ENV_NAMELEN
) {
1001 printf_P(PSTR("## Error: Variable name \"%s\" too long. "
1002 "(max %d characters)\n"), name
, CONFIG_SYS_ENV_NAMELEN
);
1009 if (argc
< 3 || argv
[2] == NULL
) {
1010 int rc
= envlist_delete(name
);
1015 * Insert / replace new value
1017 for (i
= 2, len
= 0; i
< argc
; ++i
)
1018 len
+= strlen(argv
[i
]) + 1;
1020 value
= xmalloc(len
);
1021 if (value
== NULL
) {
1022 printf_P(PSTR("## Can't malloc %d bytes\n"), len
);
1025 for (i
= 2, s
= value
; i
< argc
; ++i
) {
1028 while ((*s
++ = *v
++) != '\0')
1038 ep
= envlist_enter(&e
);
1040 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
1049 * Set an environment variable
1051 * @param varname Environment variable to set
1052 * @param varvalue Value to set it to
1053 * @return 0 if ok, 1 on error
1055 int setenv(const char *varname
, const char *varvalue
)
1057 const char * const argv
[3] = { NULL
, varname
, varvalue
};
1060 if (varvalue
== NULL
|| varvalue
[0] == '\0')
1063 return _do_env_set(0, argc
, (char * const *)argv
);
1068 * Set an environment variable to an integer value
1070 * @param varname Environment variable to set
1071 * @param value Value to set it to
1072 * @return 0 if ok, 1 on error
1074 int setenv_ulong(const char *varname
, unsigned long value
)
1076 /* TODO: this should be unsigned */
1077 char *str
= simple_itoa(value
);
1079 return setenv(varname
, str
);
1085 * Set an environment variable to an value in hex
1087 * @param varname Environment variable to set
1088 * @param value Value to set it to
1089 * @return 0 if ok, 1 on error
1091 int setenv_hex(const char *varname
, unsigned long value
)
1093 char str
[sizeof(unsigned long) *2 + 1];
1095 sprintf_P(str
, PSTR("%lx"), value
);
1096 return setenv(varname
, str
);
1100 * Get an environment variable as a hex value
1102 * @param varname Environment variable to get
1103 * @param default_val Return this, if variable does not exist
1104 * @return hex value of variable or default_val
1106 unsigned long getenv_hex(const char *varname
, unsigned long default_val
)
1109 unsigned long value
;
1112 s
= getenv(varname
);
1114 value
= strtoul(s
, &endp
, 16);
1115 if (!s
|| endp
== s
)
1121 int do_env_set(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1126 return CMD_RET_USAGE
;
1128 return _do_env_set(flag
, argc
, argv
);
1132 int do_env_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1134 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
1136 printf_P(PSTR("Saving Environment ...\n"));
1138 return saveenv() ? 1 : 0;
1141 #if defined(CONFIG_AUTO_COMPLETE)
1142 int env_complete(char *var
, int maxv
, char *cmdv
[], int bufsz
, char *buf
)
1151 while ((idx
= hmatch_r(var
, idx
, &match
, &env_htab
))) {
1152 int vallen
= strlen(match
->key
) + 1;
1154 if (found
>= maxv
- 2 || bufsz
< vallen
)
1157 cmdv
[found
++] = buf
;
1158 memcpy(buf
, match
->key
, vallen
);
1163 qsort(cmdv
, found
, sizeof(cmdv
[0]), strcmp_compar
);
1166 cmdv
[found
++] = "...";