]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/env.c
Rename getenv() to getenv_char()
[z180-stamp.git] / avr / env.c
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include "common.h"
8 #include <string.h>
9 #include <stdlib.h>
10 #include <avr/eeprom.h>
11
12 #include "config.h"
13 #include "debug.h"
14 #include "xmalloc.h"
15 #include "crc.h"
16 #include "command.h"
17 #include "env.h"
18
19
20 #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
21 #define ACTIVE_FLAG 1
22 #define OBSOLETE_FLAG 0
23
24 #define ENVLIST_DELETE (1<<0)
25
26
27 /*
28 * Default Environment
29 */
30
31 #define DELIM "\0"
32
33 const FLASH char default_env[] = {
34 ENV_BAUDRATE "=" "115200" DELIM
35 ENV_BOOTDELAY "=" "3" DELIM
36 ENV_BOOTCMD "=" "reset; loadf; go ${startaddr}" DELIM
37 ENV_CPM3_SYSFILE "=" CONFIG_CPM3_SYSFILE DELIM
38 ENV_PINALIAS "=" "0:PG5,1:PG4,2:PB4,3:PB5,4:PB6,5:PB7,"
39 "6:PG3,7:PG2,8:PG1,9:PG0,10:PE7" DELIM
40 ENV_STARTADDRESS "=" "0" DELIM
41 DELIM
42 };
43
44
45 /* EEPROM storage */
46 typedef struct environment_s {
47 uint16_t crc; /* CRC16 over data bytes */
48 uint8_t flags; /* active/obsolete flags */
49 char data[ENV_SIZE]; /* Environment data */
50 } env_t;
51
52
53 /* */
54 typedef struct env_item_s {
55 char * envvar;
56 } env_item_t;
57
58
59 static uint8_t env_valid;
60 static env_item_t env_list[CONFIG_ENVVAR_MAX];
61 static int entrycount;
62
63
64 static
65 char env_get_char(uint16_t index)
66 {
67 unsigned int off = CONFIG_ENV_OFFSET;
68 char ret;
69
70 switch (env_valid) {
71 case 2:
72 off += CONFIG_ENV_SIZE;
73 case 1:
74 ret = (char) eeprom_read_byte((const uint8_t *)off + index +
75 offsetof(env_t, data));
76 break;
77
78 default:
79 ret = default_env[index];
80 }
81
82 return ret;
83 }
84
85
86 static const FLASH char *comp_key;
87
88 static
89 int comp_env_items(const void *m1, const void *m2)
90 {
91 env_item_t *ep1 = (env_item_t *) m1;
92 env_item_t *ep2 = (env_item_t *) m2;
93
94 if (ep1 == NULL)
95 return - strcmp_P(ep2->envvar, comp_key);
96 else
97 return strcmp(ep1->envvar, ep2->envvar);
98 }
99
100
101 env_item_t *envlist_search(const MEMX char *name)
102 {
103 #ifdef __MEMX
104 if (__builtin_avr_flash_segment(name) != -1) {
105 comp_key = name;
106 return bsearch(0, env_list, entrycount,
107 sizeof(env_item_t), comp_env_items);
108 } else {
109
110 env_item_t e;
111 e.envvar = (char *) name;
112
113 return bsearch(&e, env_list, entrycount,
114 sizeof(env_item_t), comp_env_items);
115 }
116 #else
117 env_item_t e;
118 e.envvar = (char *) name;
119
120 return bsearch(&e, env_list, entrycount,
121 sizeof(env_item_t), comp_env_items);
122 #endif /* __MEMX */
123 }
124
125
126 static
127 env_item_t *envlist_enter(env_item_t *e)
128 {
129 const size_t size = sizeof(env_item_t);
130 env_item_t *ep;
131
132 ep = bsearch(e, env_list, entrycount,
133 size, comp_env_items);
134
135 if (ep == NULL) {
136 if (entrycount < CONFIG_ENVVAR_MAX) {
137
138 env_list[entrycount++] = *e;
139 qsort(env_list, entrycount, size, comp_env_items);
140 ep = bsearch(e, env_list, entrycount,
141 size, comp_env_items);
142 }
143 } else {
144 free(ep->envvar);
145 ep->envvar = e->envvar;
146 }
147
148 return ep;
149 }
150
151
152 static
153 int env_item_delete(env_item_t *ep)
154 {
155 if (entrycount == 0)
156 return -1;
157
158 free(ep->envvar);
159
160 entrycount--;
161 size_t size = sizeof(env_item_t);
162 memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
163
164 return 0;
165 }
166
167 static
168 int envlist_delete(const MEMX char *name)
169 {
170 env_item_t *ep = envlist_search(name);
171
172 if (ep != NULL)
173 return env_item_delete(ep);
174
175 return 1;
176 }
177
178
179
180 static
181 int envlist_import(uint8_t flags)
182 {
183 uint16_t index;
184
185 int len;
186 env_item_t e;
187 char *np, c;
188 uint_fast8_t ef;
189
190 if ((flags & ENVLIST_DELETE) != 0)
191 while (entrycount != 0)
192 env_item_delete(&env_list[entrycount-1]);
193
194 for (index = 0; env_get_char(index) != '\0'; ) {
195 for (len = 0; env_get_char(index + len) != '\0'; ++len) {
196 if ((index + len) >= ENV_SIZE)
197 return -1;
198 }
199
200 np = (char *) xmalloc(len+1);
201 if (np == NULL) {
202 printf_P(PSTR("## Can't malloc %d bytes\n"), len+1);
203 return 1;
204 }
205 e.envvar = np;
206 ef = 0;
207 while ((c = env_get_char(index++)) != '\0') {
208 if (!ef && (c == '=')) {
209 *np = '\0';
210 ef = 1;
211 } else
212 *np = c;
213 np++;
214 }
215 *np = '\0';
216 envlist_enter(&e);
217 }
218
219
220 return 0;
221 }
222
223
224 static
225 uint16_t env_crc(uint16_t data_offset)
226 {
227 uint16_t crc;
228 uint16_t i;
229 char c, c0;
230
231 crc = 0xffff;
232 c = 0xff;
233 for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++)
234 {
235 c0 = c;
236 c = eeprom_read_byte((const uint8_t *) data_offset + i);
237 crc = crc16(crc, c);
238 }
239 return crc ;
240 }
241
242
243 /**
244 * return valid env
245 */
246 static
247 int env_check_valid(void)
248 {
249 const uint16_t offset[2] = {CONFIG_ENV_OFFSET,
250 CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE};
251 uint_fast8_t flags[2], crc_ok[2];
252 int rc;
253
254 /* read FLAGS */
255 flags[0] = eeprom_read_byte ((uint8_t *) offset[0] +
256 offsetof(env_t, flags));
257 flags[1] = eeprom_read_byte ((uint8_t *) offset[1] +
258 offsetof(env_t, flags));
259
260 /* check CRC */
261 crc_ok[0] = (
262 eeprom_read_word((uint16_t *) offset[0] +
263 offsetof(env_t, crc))
264 == env_crc(offset[0] + offsetof(env_t, data))
265 );
266 crc_ok[1] = (
267 eeprom_read_word((uint16_t *) offset[1] +
268 offsetof(env_t, crc))
269 == env_crc(offset[1] + offsetof(env_t, data))
270 );
271
272 if (!crc_ok[0] && !crc_ok[1]) {
273 rc = 0;
274
275 } else if (crc_ok[0] && !crc_ok[1]) {
276 rc = 1;
277 } else if (!crc_ok[0] && crc_ok[1]) {
278 rc = 2;
279 } else {
280 /* both ok - check serial */
281 #if 1
282 if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG)
283 rc = 2;
284 else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF)
285 rc = 2;
286 else
287 rc = 1;
288 #else
289 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
290 rc = 1;
291 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
292 rc = 2;
293 else if (flags[0] == 0xFF && flags[1] == 0)
294 rc = 2;
295 else if (flags[1] == 0xFF && flags[0] == 0)
296 rc = 1;
297 else /* flags are equal - almost impossible */
298 rc = 1;
299 #endif
300 }
301
302 return rc;
303 }
304
305
306 int env_init(void)
307 {
308 env_valid = env_check_valid();
309 if (env_valid == 0) {
310 printf_P(PSTR("*** Warning - bad CRC, "
311 "using default environment\n\n"));
312 }
313
314 envlist_import(ENVLIST_DELETE);
315 return 0;
316 }
317
318
319 char *getenv_char(const MEMX char *name)
320 {
321 env_item_t *ep;
322 char *ret = NULL;
323
324 ep = envlist_search(name);
325 if (ep != NULL)
326 ret = ep->envvar + strlen(ep->envvar) +1;
327 return ret;
328 }
329
330 static
331 int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
332 {
333 int nlen, vlen;
334 char *var = ep->envvar;
335
336 nlen = strlen(var);
337 if (nlen == 0)
338 return 0;
339 vlen = strlen(var + nlen + 1);
340 if (vlen == 0)
341 return 0;
342 if (nlen + vlen + 2 > space_left)
343 return 0;
344
345 eeprom_update_block(var, (uint8_t *) offset, nlen);
346 offset += nlen;
347 eeprom_update_byte((uint8_t *) offset++, '=');
348 eeprom_update_block(var + nlen +1, (uint8_t *) offset, vlen+1);
349
350 return nlen + vlen + 2;
351 }
352
353
354 static
355 int saveenv(void)
356 {
357 unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
358 unsigned int off_red = CONFIG_ENV_OFFSET;
359 unsigned int pos;
360 int len, left;
361 uint16_t crc;
362 int rc = 0;
363
364 if (env_valid == 2) {
365 off = CONFIG_ENV_OFFSET;
366 off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
367 }
368
369 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff);
370
371 pos = off + offsetof(env_t, data);
372 left = ENV_SIZE - 1;
373 for (int i = 0 ; i < entrycount; i++) {
374 len = env_item_save(&env_list[i], pos, left);
375 if (len == 0) {
376 return 1;
377 }
378 pos += len;
379 left -= len;
380 }
381 /* terminate list */
382 eeprom_update_byte((uint8_t *) pos, 0);
383 crc = env_crc(off + offsetof(env_t, data));
384 eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc);
385 eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags),
386 ACTIVE_FLAG);
387
388 if (rc == 0) {
389 eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags),
390 OBSOLETE_FLAG);
391 env_valid = (env_valid == 2) ? 1 : 2;
392 }
393
394 return rc;
395 }
396
397
398 static
399 int env_item_print(env_item_t *ep)
400 {
401 int len;
402 char *ev = ep->envvar;
403
404 len = printf_P(PSTR("%s="), ev);
405 len += printf_P(PSTR("%s\n"), ev + len);
406
407 return len;
408 }
409
410
411 /*
412 * Command interface: print one or all environment variables
413 *
414 * Returns -1 in case of error, or length of printed string
415 */
416 static
417 int env_print(const MEMX char *name)
418 {
419 int len = -1;
420
421 if (name) { /* print a single name */
422
423 env_item_t *ep = envlist_search(name);
424 if (ep != NULL)
425 len = env_item_print(ep);
426
427 } else { /* print whole list */
428 len = 0;
429 for (int i = 0 ; i < entrycount; i++) {
430 len += env_item_print(&env_list[i]);
431 }
432 }
433 return len;
434 }
435
436
437 /**
438 * Set or delete environment variable
439 *
440 * Set a new environment variable,
441 * or replace or delete an existing one.
442 *
443 * @param flag (not used)
444 * @param argc
445 * @param argv[1] Environment variable to set or delete
446 * if no more args
447 * @param argv[2] ... Value to set it to
448 *
449 * @return 0 if ok, 1 on error
450 */
451 static
452 command_ret_t _do_env_set(int flag, int argc, char * const argv[])
453 {
454 int i, len;
455 char *name, *value, *valp, *p;
456 env_item_t e, *ep;
457
458 (void) flag;
459
460 name = argv[1];
461 value = argv[2];
462
463 if (strchr(name, '=')) {
464 printf_P(PSTR("## Error: illegal character '='"
465 "in variable name \"%s\"\n"), name);
466 return CMD_RET_FAILURE;
467 }
468 len = strlen(name);
469 if (len > CONFIG_SYS_ENV_NAMELEN) {
470 printf_P(PSTR("## Error: Variable name \"%s\" too long. "
471 "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
472 return CMD_RET_FAILURE;
473 }
474 /*
475 env_id++;
476 */
477 /* Delete only ? */
478 if (argc < 3 || argv[2] == NULL) {
479 int rc = envlist_delete(name);
480 return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
481 }
482
483 /*
484 * Insert / replace new value
485 */
486 for (i = 2, len += 1; i < argc; ++i)
487 len += strlen(argv[i]) + 1;
488
489 value = xmalloc(len);
490 if (value == NULL) {
491 printf_P(PSTR("## Can't malloc %d bytes\n"), len);
492 return CMD_RET_FAILURE;
493 }
494 strcpy(value, name);
495 valp = value + strlen(name) + 1;
496 for (i = 2, p = valp; i < argc; ++i) {
497 char *v = argv[i];
498
499 while ((*p++ = *v++) != '\0')
500 ;
501 *(p - 1) = ' ';
502 }
503 if (p != valp)
504 *--p = '\0';
505
506 e.envvar = value;
507 ep = envlist_enter(&e);
508 if (!ep) {
509 printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
510 name);
511 return CMD_RET_FAILURE;
512 }
513
514 return CMD_RET_SUCCESS;
515 }
516
517 /**
518 * Set an environment variable
519 *
520 * @param varname Environment variable to set
521 * @param varvalue Value to set it to
522 * @return 0 if ok, 1 on error
523 */
524 static
525 int setenv(const MEMX char *varname, const char *varvalue)
526 {
527 int rc;
528
529 #ifdef __MEMX
530 char *tmpname = NULL;
531 if (__builtin_avr_flash_segment(varname) != -1) {
532 tmpname = malloc(strlen_P(varname)+1);
533 if (tmpname == NULL) {
534 printf_P(PSTR("setenv: Out of Memory!\n"));
535 return 1;
536 }
537 strcpy_P(tmpname, varname);
538 } else
539 tmpname = (char *) varname;
540 #endif
541
542 const char * const argv[3] = { NULL, tmpname, varvalue };
543 int argc = 3;
544
545 if (varvalue == NULL || varvalue[0] == '\0')
546 --argc;
547
548 rc = (int) _do_env_set(0, argc, (char * const *)argv);
549
550 #ifdef __MEMX
551 free(tmpname);
552 #endif
553 return rc;
554 }
555
556 /**
557 * Set an environment variable to an integer value
558 *
559 * @param name Environment variable to set
560 * @param value Value to set it to
561 * @param radix Base
562 * @return 0 if ok, 1 on error
563 */
564 static
565 int setenv_intval(const MEMX char *name, unsigned long value, int radix)
566 {
567 char buf[11];
568
569 ultoa(value, buf, radix);
570
571 return setenv(name, buf);
572 }
573
574 /**
575 * Set an environment variable to a decimal integer value
576 *
577 * @param name Environment variable to set
578 * @param value Value to set it to
579 * @return 0 if ok, 1 on error
580 */
581 int setenv_ulong(const MEMX char *name, unsigned long value)
582 {
583 return setenv_intval(name, value, 10);
584 }
585
586
587 /**
588 * Set an environment variable to a value in hex
589 *
590 * @param name Environment variable to set
591 * @param value Value to set it to
592 * @return 0 if ok, 1 on error
593 */
594 int setenv_hex(const MEMX char *name, unsigned long value)
595 {
596 return setenv_intval(name, value, 16);
597 }
598
599
600 /**
601 * Decode the integer value of an environment variable and return it.
602 *
603 * @param name Name of environemnt variable
604 * @param base Number base to use (normally 10, or 16 for hex)
605 * @param default_val Default value to return if the variable is not
606 * found
607 * @return the decoded value, or default_val if not found
608 */
609 unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val)
610 {
611 unsigned long value;
612 char *vp, *endp;
613
614 env_item_t *ep = envlist_search(name);
615
616 if (ep != NULL ) {
617 vp = ep->envvar + strlen(ep->envvar) +1;
618 value = strtoul(vp, &endp, base);
619 if (endp != vp)
620 return value;
621 }
622
623 return default_val;
624 }
625
626
627 command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
628 {
629 command_ret_t rc = CMD_RET_SUCCESS;
630
631 (void) cmdtp; (void) flag;
632
633 if (argc == 1) {
634 /* print all env vars */
635 int size = env_print(NULL);
636 if (size < 0)
637 return CMD_RET_FAILURE;
638 printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
639 size, ENV_SIZE);
640 return CMD_RET_SUCCESS;
641 }
642
643 /* print selected env vars */
644 for (int i = 1; i < argc; ++i) {
645 int rc = env_print(argv[i]);
646 if (rc < 0) {
647 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
648 rc = CMD_RET_FAILURE;
649 }
650 }
651
652 return rc;
653 }
654
655
656 command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
657 {
658 (void) cmdtp;
659
660 if (argc < 2)
661 return CMD_RET_USAGE;
662
663 return _do_env_set(flag, argc, argv);
664 }
665
666
667 command_ret_t do_env_default(cmd_tbl_t *cmdtp, int flag,
668 int argc, char * const argv[])
669 {
670 (void) cmdtp; (void) flag; (void) argc; (void) argv;
671
672 uint8_t tmp = env_valid;
673 env_valid = 0;
674
675 /* Reset the whole environment */
676 printf_P(PSTR("## Resetting to default environment\n"));
677 envlist_import(ENVLIST_DELETE);
678
679 env_valid = tmp;
680
681 return CMD_RET_SUCCESS;
682 }
683
684
685 command_ret_t do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
686 {
687 (void) cmdtp; (void) flag; (void) argc; (void) argv;
688
689 printf_P(PSTR("Saving Environment ...\n"));
690 return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
691 }
692
693
694 #if defined(CONFIG_AUTO_COMPLETE)
695 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
696 {
697 ENTRY *match;
698 int found, idx;
699
700 idx = 0;
701 found = 0;
702 cmdv[0] = NULL;
703
704 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
705 int vallen = strlen(match->key) + 1;
706
707 if (found >= maxv - 2 || bufsz < vallen)
708 break;
709
710 cmdv[found++] = buf;
711 memcpy(buf, match->key, vallen);
712 buf += vallen;
713 bufsz -= vallen;
714 }
715
716 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
717
718 if (idx)
719 cmdv[found++] = "...";
720
721 cmdv[found] = NULL;
722 return found;
723 }
724 #endif