]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | #include "env.h" | |
8 | #include <avr/eeprom.h> | |
9 | ||
10 | #include "config.h" | |
11 | #include "debug.h" | |
12 | #include "crc.h" | |
13 | #include "getopt-min.h" | |
14 | ||
15 | #define DEBUG_ENV 0 /* set to 1 to debug */ | |
16 | ||
17 | #define debug_env(fmt, args...) \ | |
18 | debug_cond(DEBUG_ENV, fmt, ##args) | |
19 | ||
20 | ||
21 | /* | |
22 | * Default Environment | |
23 | */ | |
24 | ||
25 | #define DELIM "\0" | |
26 | ||
27 | const FLASH char default_env[] = { | |
28 | ENV_BAUDRATE "=" stringify(CONFIG_BAUDRATE) DELIM | |
29 | ENV_BOOTDELAY "=" stringify(CONFIG_BOOTDELAY) DELIM | |
30 | ENV_BOOTCMD "=" "pin ${pins};loadcpm3;go ${startaddress}" DELIM | |
31 | ENV_CPM3_SYSFILE "=" CONFIG_CPM3_SYSFILE DELIM | |
32 | ENV_PINALIAS "=" "0:PG5,1:PG4,2:PB4,3:PB5,4:PB6,5:PB7," | |
33 | "6:PG3,7:PG2,8:PG1,9:PG0,10:PE7" DELIM | |
34 | "pins" "=" "2,8 low 9 high 3 2" DELIM | |
35 | DELIM | |
36 | }; | |
37 | ||
38 | ||
39 | #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) - sizeof(uint8_t)) | |
40 | ||
41 | #define ENVLIST_DELETE (1<<0) | |
42 | ||
43 | ||
44 | /* EEPROM storage */ | |
45 | typedef struct environment_s { | |
46 | uint16_t crc; /* CRC16 over data bytes */ | |
47 | uint8_t flags; /* active/obsolete flags */ | |
48 | #define ACTIVE_FLAG 1 | |
49 | #define OBSOLETE_FLAG 0 | |
50 | char data[ENV_SIZE]; /* Environment data */ | |
51 | } env_t; | |
52 | ||
53 | ||
54 | static uint8_t env_valid; | |
55 | static char env_list[ENV_SIZE]; | |
56 | ||
57 | static | |
58 | char env_get_char(uint_fast16_t index) | |
59 | { | |
60 | unsigned int off = CONFIG_ENV_OFFSET; | |
61 | char ret; | |
62 | ||
63 | switch (env_valid) { | |
64 | case 2: | |
65 | off += CONFIG_ENV_SIZE; | |
66 | case 1: | |
67 | ret = (char) eeprom_read_byte((const uint8_t *)off + index + | |
68 | offsetof(env_t, data)); | |
69 | break; | |
70 | ||
71 | default: | |
72 | ret = default_env[index]; | |
73 | } | |
74 | ||
75 | return ret; | |
76 | } | |
77 | ||
78 | static void envlist_clear(void) | |
79 | { | |
80 | memset(env_list, 0, sizeof env_list); | |
81 | } | |
82 | ||
83 | static | |
84 | int env_item_delete(char *ep) | |
85 | { | |
86 | char *next = ep + strlen(ep) + 1; | |
87 | char *end = next; | |
88 | while (*end++ != 0) | |
89 | while (*end++ != 0); | |
90 | ||
91 | memmove(ep, next, end - next); | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static int envcmp(const char *s1, const char *s2) | |
97 | { | |
98 | const unsigned char *u1 = (const unsigned char *)s1; | |
99 | const unsigned char *u2 = (const unsigned char *)s2; | |
100 | unsigned char c1, c2; | |
101 | ||
102 | do { | |
103 | c1 = *u1++; c2 = *u2++; | |
104 | if (c1 == '=') c1 = '\0'; | |
105 | if (c2 == '=') c2 = '\0'; | |
106 | } while (c1 != 0 && c1 - c2 == 0); | |
107 | ||
108 | return c1 - c2; | |
109 | } | |
110 | ||
111 | static char *envlist_search(const MEMX char *name) | |
112 | { | |
113 | char key[CONFIG_SYS_ENV_NAMELEN + 1]; | |
114 | const MEMX char *np = name; | |
115 | int keylen = 0; | |
116 | char c; | |
117 | ||
118 | while (keylen <= CONFIG_SYS_ENV_NAMELEN && (c = *np++) != '\0') | |
119 | key[keylen++] = c; | |
120 | key[keylen] = '\0'; | |
121 | ||
122 | for (char *lp = env_list; *lp != 0; ++lp) { | |
123 | ||
124 | if (envcmp(lp, key) == 0) | |
125 | return lp; | |
126 | ||
127 | while (*lp != 0) | |
128 | ++lp; | |
129 | } | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
134 | static char *env_item_insert_name(char *pos, const char *name, int len) | |
135 | { | |
136 | char *dstp = pos + len + 1; | |
137 | char *end = pos; | |
138 | while (*end++ != 0) | |
139 | while (*end++ != 0); | |
140 | ||
141 | if (end + len >= env_list + ENV_SIZE) | |
142 | return NULL; | |
143 | ||
144 | memmove(dstp, pos, end - pos); | |
145 | strcpy(pos, name); | |
146 | memset(pos + strlen(name), '=', dstp - pos - strlen(name)); | |
147 | *(dstp - 1) = '\0'; | |
148 | ||
149 | return pos; | |
150 | } | |
151 | ||
152 | ||
153 | static char *envlist_alloc(const char *name, int len) | |
154 | { | |
155 | char *lp; | |
156 | int rc; | |
157 | ||
158 | ||
159 | for (lp = env_list; *lp != 0; ++lp) { | |
160 | ||
161 | rc = envcmp(lp, name); | |
162 | if (rc >= 0) | |
163 | break; | |
164 | ||
165 | /* rc < 0 : check next place */ | |
166 | while (*lp != 0) | |
167 | ++lp; | |
168 | } | |
169 | ||
170 | /* rc == 0 : entry found, replace */ | |
171 | if (rc == 0) | |
172 | env_item_delete(lp); | |
173 | ||
174 | /* rc > 0 : entry not in list, insert */ | |
175 | /* *lp == 0 : entry not in list, insert */ | |
176 | ||
177 | lp = env_item_insert_name(lp, name, len); | |
178 | ||
179 | return lp; | |
180 | } | |
181 | ||
182 | #if 0 | |
183 | static char *env_item_insert(char *pos, const char *envstr) | |
184 | { | |
185 | char *dstp = pos + strlen(envstr) + 1; | |
186 | char *end = pos; | |
187 | while (*end++ != 0) | |
188 | while (*end++ != 0); | |
189 | ||
190 | if (end > env_list + ENV_SIZE) | |
191 | return NULL; | |
192 | ||
193 | memmove(dstp, pos, end - pos); | |
194 | strcpy(pos, envstr); | |
195 | ||
196 | return pos; | |
197 | } | |
198 | ||
199 | static char *envlist_enter(const char *ep) | |
200 | { | |
201 | char *lp; | |
202 | int rc; | |
203 | ||
204 | for (lp = env_list; *lp != 0; ++lp) { | |
205 | ||
206 | rc = envcmp(lp, ep); | |
207 | if (rc >= 0) | |
208 | break; | |
209 | ||
210 | /* rc < 0 : check next place */ | |
211 | while (*lp != 0) | |
212 | ++lp; | |
213 | } | |
214 | ||
215 | /* rc == 0 : entry found, replace */ | |
216 | if (rc == 0) | |
217 | env_item_delete(lp); | |
218 | ||
219 | /* rc > 0 : entry not in list, insert */ | |
220 | /* *lp == 0 : entry not in list, insert */ | |
221 | ||
222 | lp = env_item_insert(lp, ep); | |
223 | ||
224 | return lp; | |
225 | } | |
226 | #endif | |
227 | ||
228 | static | |
229 | int envlist_delete(const MEMX char *name) | |
230 | { | |
231 | char *ep = envlist_search(name); | |
232 | ||
233 | if (ep != NULL) | |
234 | return env_item_delete(ep); | |
235 | ||
236 | return 1; | |
237 | } | |
238 | ||
239 | static | |
240 | int envlist_import(uint8_t flags) | |
241 | { | |
242 | if ((flags & ENVLIST_DELETE) != 0) | |
243 | envlist_clear(); | |
244 | ||
245 | for (uint_fast16_t index = 0; env_get_char(index) != '\0'; ) { | |
246 | char name[CONFIG_SYS_ENV_NAMELEN+1]; | |
247 | uint_fast8_t nlen = 0; | |
248 | size_t len; | |
249 | char ch; | |
250 | for (len = 0; (ch = env_get_char(index + len)) != '\0'; ++len) { | |
251 | if ((index + len) >= ENV_SIZE) | |
252 | return -1; | |
253 | if (nlen == 0) { | |
254 | if (ch == '=') { | |
255 | nlen = len; | |
256 | name[nlen] = '\0'; | |
257 | } else { | |
258 | if (len > CONFIG_SYS_ENV_NAMELEN) | |
259 | return -1; | |
260 | name[len] = ch; | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | char *ep = envlist_alloc(name, len); | |
266 | if (ep == NULL) { | |
267 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); | |
268 | return 1; | |
269 | } | |
270 | ||
271 | index += nlen; | |
272 | char *p = ep + nlen; | |
273 | while ((*p++ = env_get_char(index++)) != '\0') | |
274 | ; | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | ||
280 | static | |
281 | uint16_t env_crc(uint16_t data_offset) | |
282 | { | |
283 | uint16_t crc; | |
284 | uint16_t i; | |
285 | char c, c0; | |
286 | ||
287 | crc = 0xffff; | |
288 | c = 0xff; | |
289 | for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++) | |
290 | { | |
291 | c0 = c; | |
292 | c = eeprom_read_byte((const uint8_t *) data_offset + i); | |
293 | crc = crc16(crc, c); | |
294 | } | |
295 | return crc ; | |
296 | } | |
297 | ||
298 | ||
299 | /** | |
300 | * return valid env | |
301 | */ | |
302 | static | |
303 | int env_check_valid(void) | |
304 | { | |
305 | const uint16_t offset[2] = {CONFIG_ENV_OFFSET, | |
306 | CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE}; | |
307 | uint_fast8_t flags[2], crc_ok[2]; | |
308 | int rc; | |
309 | ||
310 | /* read FLAGS */ | |
311 | flags[0] = eeprom_read_byte ((uint8_t *) offset[0] + | |
312 | offsetof(env_t, flags)); | |
313 | flags[1] = eeprom_read_byte ((uint8_t *) offset[1] + | |
314 | offsetof(env_t, flags)); | |
315 | ||
316 | /* check CRC */ | |
317 | crc_ok[0] = ( | |
318 | eeprom_read_word((uint16_t *) offset[0] + | |
319 | offsetof(env_t, crc)) | |
320 | == env_crc(offset[0] + offsetof(env_t, data)) | |
321 | ); | |
322 | crc_ok[1] = ( | |
323 | eeprom_read_word((uint16_t *) offset[1] + | |
324 | offsetof(env_t, crc)) | |
325 | == env_crc(offset[1] + offsetof(env_t, data)) | |
326 | ); | |
327 | ||
328 | if (!crc_ok[0] && !crc_ok[1]) { | |
329 | rc = 0; | |
330 | ||
331 | } else if (crc_ok[0] && !crc_ok[1]) { | |
332 | rc = 1; | |
333 | } else if (!crc_ok[0] && crc_ok[1]) { | |
334 | rc = 2; | |
335 | } else { | |
336 | /* both ok - check serial */ | |
337 | #if 1 | |
338 | if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG) | |
339 | rc = 2; | |
340 | else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF) | |
341 | rc = 2; | |
342 | else | |
343 | rc = 1; | |
344 | #else | |
345 | if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG) | |
346 | rc = 1; | |
347 | else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG) | |
348 | rc = 2; | |
349 | else if (flags[0] == 0xFF && flags[1] == 0) | |
350 | rc = 2; | |
351 | else if (flags[1] == 0xFF && flags[0] == 0) | |
352 | rc = 1; | |
353 | else /* flags are equal - almost impossible */ | |
354 | rc = 1; | |
355 | #endif | |
356 | } | |
357 | ||
358 | return rc; | |
359 | } | |
360 | ||
361 | ||
362 | int env_init(void) | |
363 | { | |
364 | env_valid = env_check_valid(); | |
365 | if (env_valid == 0) { | |
366 | printf_P(PSTR("*** Warning - bad CRC, " | |
367 | "using default environment\n\n")); | |
368 | } | |
369 | ||
370 | envlist_import(ENVLIST_DELETE); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | ||
375 | char *getenv_str(const MEMX char *name) | |
376 | { | |
377 | char *ep = envlist_search(name); | |
378 | if (ep != NULL) | |
379 | return strchr(ep, '=') + 1; | |
380 | else | |
381 | return NULL; | |
382 | } | |
383 | ||
384 | static | |
385 | int env_item_save(char *ep, uint16_t offset, int space_left) | |
386 | { | |
387 | int len; | |
388 | ||
389 | len = strlen(ep); | |
390 | if (len == 0) | |
391 | return 0; | |
392 | len += 1; | |
393 | if (len > space_left) | |
394 | return 0; | |
395 | ||
396 | eeprom_update_block(ep, (uint8_t *) offset, len); | |
397 | ||
398 | return len; | |
399 | } | |
400 | ||
401 | ||
402 | static | |
403 | int saveenv(void) | |
404 | { | |
405 | unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
406 | unsigned int off_red = CONFIG_ENV_OFFSET; | |
407 | unsigned int pos; | |
408 | int len, left; | |
409 | uint16_t crc; | |
410 | int rc = 0; | |
411 | ||
412 | if (env_valid == 2) { | |
413 | off = CONFIG_ENV_OFFSET; | |
414 | off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
415 | } | |
416 | ||
417 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); | |
418 | ||
419 | pos = off + offsetof(env_t, data); | |
420 | left = ENV_SIZE - 1; | |
421 | ||
422 | char *ep = env_list; | |
423 | while (*ep) { | |
424 | len = env_item_save(ep, pos, left); | |
425 | if (len == 0) { | |
426 | return 1; | |
427 | } | |
428 | pos += len; | |
429 | left -= len; | |
430 | while (*ep++); | |
431 | } | |
432 | ||
433 | /* terminate list */ | |
434 | eeprom_update_byte((uint8_t *) pos, 0); | |
435 | crc = env_crc(off + offsetof(env_t, data)); | |
436 | eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc); | |
437 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), | |
438 | ACTIVE_FLAG); | |
439 | ||
440 | if (rc == 0) { | |
441 | eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), | |
442 | OBSOLETE_FLAG); | |
443 | env_valid = (env_valid == 2) ? 1 : 2; | |
444 | } | |
445 | ||
446 | return rc; | |
447 | } | |
448 | ||
449 | ||
450 | static | |
451 | int env_item_print(char *ep, bool mode) | |
452 | { | |
453 | int len = strlen(ep) + 1; | |
454 | ||
455 | if (mode) { | |
456 | my_puts_P(PSTR("setenv ")); | |
457 | char ch; | |
458 | while ((ch = *ep++) != '=') | |
459 | putchar(ch); | |
460 | printf_P(PSTR(" '%s'\n"), ep); | |
461 | } else { | |
462 | puts(ep); | |
463 | } | |
464 | ||
465 | return len; | |
466 | } | |
467 | ||
468 | ||
469 | /* | |
470 | * Command interface: print one or all environment variables | |
471 | * | |
472 | * Returns -1 in case of error, or length of printed string | |
473 | */ | |
474 | static | |
475 | int env_print(const char *name, bool mode) | |
476 | { | |
477 | int len = -1; | |
478 | char *ep; | |
479 | ||
480 | if (name != NULL) { /* print a single name */ | |
481 | ||
482 | ep = envlist_search(name); | |
483 | if (ep != NULL) | |
484 | len = env_item_print(ep, mode); | |
485 | ||
486 | } else { /* print whole list */ | |
487 | len = 0; | |
488 | ep = env_list; | |
489 | while (*ep) { | |
490 | len += env_item_print(ep, mode); | |
491 | while (*ep++); | |
492 | } | |
493 | } | |
494 | return len; | |
495 | } | |
496 | ||
497 | ||
498 | /** | |
499 | * Set or delete environment variable | |
500 | * | |
501 | * Set a new environment variable, | |
502 | * or replace or delete an existing one. | |
503 | * | |
504 | * @param flag (not used) | |
505 | * @param argc | |
506 | * @param argv[1] Environment variable to set or delete | |
507 | * if no more args | |
508 | * @param argv[2] ... Value to set it to | |
509 | * | |
510 | * @return 0 if ok, 1 on error | |
511 | */ | |
512 | static | |
513 | command_ret_t _do_env_set(uint_fast8_t flag UNUSED, int argc, char * const argv[]) | |
514 | { | |
515 | char *name = argv[1]; | |
516 | ||
517 | if (strchr(name, '=')) { | |
518 | printf_P(PSTR("## Error: illegal character '='" | |
519 | "in variable name \"%s\"\n"), name); | |
520 | return CMD_RET_FAILURE; | |
521 | } | |
522 | size_t len = strlen(name); | |
523 | if (len > CONFIG_SYS_ENV_NAMELEN) { | |
524 | printf_P(PSTR("## Error: Variable name \"%s\" too long. " | |
525 | "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN); | |
526 | return CMD_RET_FAILURE; | |
527 | } | |
528 | ||
529 | /* Delete only ? */ | |
530 | if (argc < 3) { | |
531 | int rc = envlist_delete(name); | |
532 | return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS; | |
533 | } | |
534 | ||
535 | /* Insert / replace new value */ | |
536 | ||
537 | for (int_fast8_t i = 2; i < argc; ++i) | |
538 | len += strlen(argv[i]) + 1; | |
539 | ||
540 | char *pos = envlist_alloc(name, len); | |
541 | if (pos == NULL) { | |
542 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); | |
543 | return CMD_RET_FAILURE; | |
544 | } | |
545 | ||
546 | char *vp = pos + strlen(name) + 1; | |
547 | for (int_fast8_t i = 2; i < argc; ++i) { | |
548 | char *ap = argv[i]; | |
549 | while ((*vp++ = *ap++) != '\0'); | |
550 | //if (i < argc-1) | |
551 | *(vp-1) = ' '; | |
552 | } | |
553 | *(vp-1) = '\0'; | |
554 | ||
555 | return CMD_RET_SUCCESS; | |
556 | } | |
557 | ||
558 | /** | |
559 | * Set an environment variable | |
560 | * | |
561 | * @param varname Environment variable to set | |
562 | * @param varvalue Value to set it to | |
563 | * @return 0 if ok, 1 on error | |
564 | */ | |
565 | static | |
566 | int setenv(const MEMX char *varname, const char *varvalue) | |
567 | { | |
568 | int rc; | |
569 | ||
570 | #ifdef __MEMX | |
571 | char *tmpname; | |
572 | if (__builtin_avr_flash_segment(varname) != -1) { | |
573 | tmpname = malloc(strlen_P(varname)+1); | |
574 | if (tmpname == NULL) { | |
575 | printf_P(PSTR("setenv: Out of Memory!\n")); | |
576 | return 1; | |
577 | } | |
578 | strcpy_P(tmpname, varname); | |
579 | } else | |
580 | tmpname = (char *) varname; | |
581 | ||
582 | const char * const argv[3] = { NULL, tmpname, varvalue }; | |
583 | #else | |
584 | const char * const argv[3] = { NULL, varname, varvalue }; | |
585 | #endif | |
586 | ||
587 | int argc = 3; | |
588 | ||
589 | if (varvalue == NULL || varvalue[0] == '\0') | |
590 | --argc; | |
591 | ||
592 | rc = (int) _do_env_set(0, argc, (char * const *)argv); | |
593 | ||
594 | #ifdef __MEMX | |
595 | if (__builtin_avr_flash_segment(varname) != -1) | |
596 | free(tmpname); | |
597 | #endif | |
598 | return rc; | |
599 | } | |
600 | ||
601 | /** | |
602 | * Set an environment variable to an integer value | |
603 | * | |
604 | * @param name Environment variable to set | |
605 | * @param value Value to set it to | |
606 | * @param radix Base | |
607 | * @return 0 if ok, 1 on error | |
608 | */ | |
609 | static | |
610 | int setenv_intval(const MEMX char *name, unsigned long value, int radix) | |
611 | { | |
612 | char buf[11]; | |
613 | ||
614 | ultoa(value, buf, radix); | |
615 | ||
616 | return setenv(name, buf); | |
617 | } | |
618 | ||
619 | /** | |
620 | * Set an environment variable to a decimal integer value | |
621 | * | |
622 | * @param name Environment variable to set | |
623 | * @param value Value to set it to | |
624 | * @return 0 if ok, 1 on error | |
625 | */ | |
626 | int setenv_ulong(const MEMX char *name, unsigned long value) | |
627 | { | |
628 | return setenv_intval(name, value, 10); | |
629 | } | |
630 | ||
631 | ||
632 | /** | |
633 | * Set an environment variable to a value in hex | |
634 | * | |
635 | * @param name Environment variable to set | |
636 | * @param value Value to set it to | |
637 | * @return 0 if ok, 1 on error | |
638 | */ | |
639 | int setenv_hex(const MEMX char *name, unsigned long value) | |
640 | { | |
641 | return setenv_intval(name, value, 16); | |
642 | } | |
643 | ||
644 | ||
645 | /** | |
646 | * Decode the integer value of an environment variable and return it. | |
647 | * | |
648 | * @param name Name of environemnt variable | |
649 | * @param base Number base to use (normally 10, or 16 for hex) | |
650 | * @param default_val Default value to return if the variable is not | |
651 | * found | |
652 | * @return the decoded value, or default_val if not found | |
653 | */ | |
654 | unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val) | |
655 | { | |
656 | unsigned long value; | |
657 | char *vp, *endp; | |
658 | ||
659 | vp = getenv_str(name); | |
660 | ||
661 | if (vp != NULL ) { | |
662 | value = strtoul(vp, &endp, base); | |
663 | if (endp != vp) | |
664 | return value; | |
665 | } | |
666 | ||
667 | return default_val; | |
668 | } | |
669 | ||
670 | ||
671 | /* | |
672 | * Read an environment variable as a boolean | |
673 | */ | |
674 | bool getenv_yesno(const MEMX char *name) | |
675 | { | |
676 | char *s = getenv_str(name); | |
677 | ||
678 | if (s == NULL) | |
679 | return false; | |
680 | ||
681 | return strchr_P(PSTR("1yYtT"), *s) != NULL; | |
682 | ||
683 | /* | |
684 | return *s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T' ? | |
685 | 1 : 0; | |
686 | */ | |
687 | } | |
688 | ||
689 | command_ret_t do_env_print(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, | |
690 | int argc, char * const argv[]) | |
691 | { | |
692 | bool mode = 0; | |
693 | command_ret_t rc = CMD_RET_SUCCESS; | |
694 | ||
695 | int opt; | |
696 | while ((opt = getopt(argc, argv, PSTR("s"))) != -1) { | |
697 | switch (opt) { | |
698 | case 's': | |
699 | mode = 1; | |
700 | break; | |
701 | default: /* '?' */ | |
702 | return CMD_RET_USAGE; | |
703 | } | |
704 | } | |
705 | ||
706 | if (optind == argc) { | |
707 | /* print all env vars */ | |
708 | int size = env_print(NULL, mode); | |
709 | if (size < 0) | |
710 | return CMD_RET_FAILURE; | |
711 | if (mode == 0) | |
712 | printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"), | |
713 | size, ENV_SIZE - 1); | |
714 | return CMD_RET_SUCCESS; | |
715 | } | |
716 | ||
717 | /* print selected env vars */ | |
718 | while (optind < argc) { | |
719 | int len = env_print(argv[optind], mode); | |
720 | if (len < 0) { | |
721 | printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[optind]); | |
722 | rc = CMD_RET_FAILURE; | |
723 | } | |
724 | optind++; | |
725 | } | |
726 | ||
727 | return rc; | |
728 | } | |
729 | ||
730 | ||
731 | command_ret_t do_env_set(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[]) | |
732 | { | |
733 | if (argc < 2) | |
734 | return CMD_RET_USAGE; | |
735 | ||
736 | return _do_env_set(flag, argc, argv); | |
737 | } | |
738 | ||
739 | ||
740 | command_ret_t do_env_default(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, | |
741 | int argc UNUSED, char * const argv[] UNUSED) | |
742 | { | |
743 | uint8_t tmp = env_valid; | |
744 | env_valid = 0; | |
745 | ||
746 | /* Reset the whole environment */ | |
747 | printf_P(PSTR("## Resetting to default environment\n")); | |
748 | envlist_import(ENVLIST_DELETE); | |
749 | ||
750 | env_valid = tmp; | |
751 | ||
752 | return CMD_RET_SUCCESS; | |
753 | } | |
754 | ||
755 | ||
756 | command_ret_t do_env_save(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) | |
757 | { | |
758 | printf_P(PSTR("Saving Environment ...\n")); | |
759 | return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS; | |
760 | } | |
761 | ||
762 | ||
763 | #if defined(CONFIG_AUTO_COMPLETE) | |
764 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) | |
765 | { | |
766 | ENTRY *match; | |
767 | int found, idx; | |
768 | ||
769 | idx = 0; | |
770 | found = 0; | |
771 | cmdv[0] = NULL; | |
772 | ||
773 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { | |
774 | int vallen = strlen(match->key) + 1; | |
775 | ||
776 | if (found >= maxv - 2 || bufsz < vallen) | |
777 | break; | |
778 | ||
779 | cmdv[found++] = buf; | |
780 | memcpy(buf, match->key, vallen); | |
781 | buf += vallen; | |
782 | bufsz -= vallen; | |
783 | } | |
784 | ||
785 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); | |
786 | ||
787 | if (idx) | |
788 | cmdv[found++] = "..."; | |
789 | ||
790 | cmdv[found] = NULL; | |
791 | return found; | |
792 | } | |
793 | #endif | |
794 | ||
795 | cmd_tbl_t cmd_tbl_env[] = { | |
796 | CMD_TBL_ITEM_COMPLETE( | |
797 | printenv, CONFIG_SYS_MAXARGS, 1|CTBL_SUBCMDAUTO, do_env_print, | |
798 | "print environment variables", | |
799 | "[-s] [name ...]\n" | |
800 | " Print value of environment variable(s) 'name'\n" | |
801 | " If no names are given, print values of all environment variables\n" | |
802 | " -s Print in setenv form", | |
803 | var_complete | |
804 | ), | |
805 | CMD_TBL_ITEM_COMPLETE( | |
806 | setenv, CONFIG_SYS_MAXARGS, CTBL_SUBCMDAUTO, do_env_set, | |
807 | "set environment variables", | |
808 | "name value ...\n" | |
809 | " - set environment variable 'name' to 'value ...'\n" | |
810 | "setenv name\n" | |
811 | " - delete environment variable 'name'", | |
812 | var_complete | |
813 | ), | |
814 | CMD_TBL_ITEM( | |
815 | saveenv, 1, CTBL_SUBCMDAUTO, do_env_save, | |
816 | "save environment variables to persistent storage", | |
817 | "" | |
818 | ), | |
819 | CMD_TBL_ITEM( | |
820 | defaultenv, 1, CTBL_SUBCMDAUTO, do_env_default, | |
821 | "set all environment variables to their default values", | |
822 | "" | |
823 | ), | |
824 | ||
825 | CMD_TBL_ITEM_COMPLETE( | |
826 | print, CONFIG_SYS_MAXARGS, 1, do_env_print, | |
827 | "print environment variables", | |
828 | "[-s] [name ...]\n" | |
829 | " Print value of environment variable(s) 'name'\n" | |
830 | " If no names are given, print values of all environment variables\n" | |
831 | " -s Print in setenv form", | |
832 | var_complete | |
833 | ), | |
834 | CMD_TBL_ITEM_COMPLETE( | |
835 | set, CONFIG_SYS_MAXARGS, 0, do_env_set, | |
836 | "set environment variables", | |
837 | "name value ...\n" | |
838 | " - set environment variable 'name' to 'value ...'\n" | |
839 | "setenv name\n" | |
840 | " - delete environment variable 'name'", | |
841 | var_complete | |
842 | ), | |
843 | CMD_TBL_ITEM( | |
844 | save, 1, 0, do_env_save, | |
845 | "save environment variables to persistent storage", | |
846 | "" | |
847 | ), | |
848 | CMD_TBL_ITEM( | |
849 | default, 1, 0, do_env_default, | |
850 | "set all environment variables to their default values", | |
851 | "" | |
852 | ), | |
853 | ||
854 | CMD_TBL_ITEM( | |
855 | help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, | |
856 | "Print sub command description/usage", | |
857 | "\n" | |
858 | " - print brief description of all sub commands\n" | |
859 | "env help command ...\n" | |
860 | " - print detailed usage of sub cmd 'command'" | |
861 | ), | |
862 | ||
863 | /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ | |
864 | {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, | |
865 | NULL, | |
866 | #ifdef CONFIG_SYS_LONGHELP | |
867 | FSTR(""), | |
868 | #endif /* CONFIG_SYS_LONGHELP */ | |
869 | NULL, | |
870 | #ifdef CONFIG_AUTO_COMPLETE | |
871 | NULL, | |
872 | #endif | |
873 | }, | |
874 | /* Mark end of table */ | |
875 | CMD_TBL_END(cmd_tbl_env) | |
876 | }; | |
877 | ||
878 | command_ret_t do_env(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) | |
879 | { | |
880 | return CMD_RET_USAGE; | |
881 | } |