]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
b51d2360 | 4 | * SPDX-License-Identifier: GPL-2.0 |
35edb766 L |
5 | */ |
6 | ||
7eecbdac | 7 | #include "env.h" |
d684c216 L |
8 | #include <avr/eeprom.h> |
9 | ||
10 | #include "config.h" | |
11 | #include "debug.h" | |
d684c216 | 12 | #include "crc.h" |
c3ce66a8 | 13 | #include "getopt-min.h" |
d684c216 | 14 | |
dd2c2ec0 | 15 | #define DEBUG_ENV 0 /* set to 1 to debug */ |
1816a698 | 16 | |
2c60e1dc | 17 | #define debug_env(fmt, args...) \ |
1816a698 L |
18 | debug_cond(DEBUG_ENV, fmt, ##args) |
19 | ||
d684c216 | 20 | |
534e1dfc L |
21 | /* |
22 | * Default Environment | |
23 | */ | |
1da3acc4 L |
24 | |
25 | #define DELIM "\0" | |
26 | ||
534e1dfc | 27 | const FLASH char default_env[] = { |
2c60e1dc L |
28 | ENV_BAUDRATE "=" stringify(CONFIG_BAUDRATE) DELIM |
29 | ENV_BOOTDELAY "=" stringify(CONFIG_BOOTDELAY) DELIM | |
1816a698 | 30 | ENV_BOOTCMD "=" "pin ${pins};loadcpm3;go ${startaddress}" DELIM |
057817cb | 31 | ENV_CPM3_SYSFILE "=" CONFIG_CPM3_SYSFILE DELIM |
c0abd68b L |
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 | |
b51d2360 | 34 | "pins" "=" "2,8 low 9 high 3 2" DELIM |
534e1dfc L |
35 | DELIM |
36 | }; | |
37 | ||
c0abd68b | 38 | |
b0a9d14c L |
39 | #define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) - sizeof(uint8_t)) |
40 | ||
41 | #define ENVLIST_DELETE (1<<0) | |
42 | ||
43 | ||
1da3acc4 | 44 | /* EEPROM storage */ |
d684c216 L |
45 | typedef struct environment_s { |
46 | uint16_t crc; /* CRC16 over data bytes */ | |
323398b1 | 47 | uint8_t flags; /* active/obsolete flags */ |
b0a9d14c L |
48 | #define ACTIVE_FLAG 1 |
49 | #define OBSOLETE_FLAG 0 | |
d684c216 L |
50 | char data[ENV_SIZE]; /* Environment data */ |
51 | } env_t; | |
52 | ||
d684c216 | 53 | |
1816a698 L |
54 | static uint8_t env_valid; |
55 | static char env_list[ENV_SIZE]; | |
d684c216 | 56 | |
1da3acc4 | 57 | static |
1816a698 | 58 | char env_get_char(uint_fast16_t index) |
d684c216 L |
59 | { |
60 | unsigned int off = CONFIG_ENV_OFFSET; | |
55027f3b | 61 | char ret; |
d684c216 | 62 | |
55027f3b L |
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 + | |
d684c216 | 68 | offsetof(env_t, data)); |
55027f3b | 69 | break; |
d684c216 | 70 | |
55027f3b L |
71 | default: |
72 | ret = default_env[index]; | |
d684c216 | 73 | } |
d684c216 | 74 | |
55027f3b | 75 | return ret; |
d684c216 L |
76 | } |
77 | ||
1816a698 L |
78 | static void envlist_clear(void) |
79 | { | |
80 | memset(env_list, 0, sizeof env_list); | |
81 | } | |
04cffff0 | 82 | |
d684c216 | 83 | static |
1816a698 | 84 | int env_item_delete(char *ep) |
d684c216 | 85 | { |
1816a698 L |
86 | char *next = ep + strlen(ep) + 1; |
87 | char *end = next; | |
dd2c2ec0 | 88 | while (*end++ != 0) |
1816a698 | 89 | while (*end++ != 0); |
7e24905c | 90 | |
1816a698 | 91 | memmove(ep, next, end - next); |
d684c216 | 92 | |
1816a698 L |
93 | return 0; |
94 | } | |
1da3acc4 | 95 | |
dd2c2ec0 L |
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 | ||
1816a698 | 111 | static char *envlist_search(const MEMX char *name) |
7e24905c | 112 | { |
1816a698 L |
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 | ||
1816a698 | 122 | for (char *lp = env_list; *lp != 0; ++lp) { |
04cffff0 | 123 | |
dd2c2ec0 | 124 | if (envcmp(lp, key) == 0) |
1816a698 | 125 | return lp; |
04cffff0 | 126 | |
1816a698 L |
127 | while (*lp != 0) |
128 | ++lp; | |
7e24905c | 129 | } |
7e24905c | 130 | |
1816a698 | 131 | return NULL; |
7e24905c L |
132 | } |
133 | ||
8b2c7c22 L |
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 | ||
b0a9d14c | 141 | if (end + len >= env_list + ENV_SIZE) |
8b2c7c22 L |
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 | |
1816a698 | 183 | static char *env_item_insert(char *pos, const char *envstr) |
7e24905c | 184 | { |
1816a698 L |
185 | char *dstp = pos + strlen(envstr) + 1; |
186 | char *end = pos; | |
dd2c2ec0 | 187 | while (*end++ != 0) |
1816a698 | 188 | while (*end++ != 0); |
7e24905c | 189 | |
1816a698 L |
190 | if (end > env_list + ENV_SIZE) |
191 | return NULL; | |
7e24905c | 192 | |
1816a698 L |
193 | memmove(dstp, pos, end - pos); |
194 | strcpy(pos, envstr); | |
7e24905c | 195 | |
1816a698 | 196 | return pos; |
7e24905c L |
197 | } |
198 | ||
1816a698 | 199 | static char *envlist_enter(const char *ep) |
7e24905c | 200 | { |
1816a698 | 201 | char *lp; |
1816a698 | 202 | int rc; |
55027f3b | 203 | |
1816a698 | 204 | for (lp = env_list; *lp != 0; ++lp) { |
7e24905c | 205 | |
dd2c2ec0 | 206 | rc = envcmp(lp, ep); |
1816a698 L |
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; | |
7e24905c | 225 | } |
8b2c7c22 | 226 | #endif |
1816a698 | 227 | |
7e24905c | 228 | static |
04cffff0 | 229 | int envlist_delete(const MEMX char *name) |
7e24905c | 230 | { |
1816a698 | 231 | char *ep = envlist_search(name); |
7e24905c L |
232 | |
233 | if (ep != NULL) | |
234 | return env_item_delete(ep); | |
235 | ||
236 | return 1; | |
237 | } | |
238 | ||
7e24905c L |
239 | static |
240 | int envlist_import(uint8_t flags) | |
d684c216 | 241 | { |
7e24905c | 242 | if ((flags & ENVLIST_DELETE) != 0) |
1816a698 | 243 | envlist_clear(); |
7e24905c | 244 | |
8b2c7c22 L |
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) { | |
55027f3b L |
251 | if ((index + len) >= ENV_SIZE) |
252 | return -1; | |
8b2c7c22 L |
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 | } | |
1da3acc4 | 263 | } |
d684c216 | 264 | |
8b2c7c22 | 265 | char *ep = envlist_alloc(name, len); |
1816a698 | 266 | if (ep == NULL) { |
8b2c7c22 | 267 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); |
55027f3b | 268 | return 1; |
d684c216 | 269 | } |
8b2c7c22 L |
270 | |
271 | index += nlen; | |
272 | char *p = ep + nlen; | |
1816a698 L |
273 | while ((*p++ = env_get_char(index++)) != '\0') |
274 | ; | |
d684c216 | 275 | } |
1da3acc4 L |
276 | return 0; |
277 | } | |
d684c216 L |
278 | |
279 | ||
1da3acc4 L |
280 | static |
281 | uint16_t env_crc(uint16_t data_offset) | |
323398b1 | 282 | { |
7e24905c | 283 | uint16_t crc; |
1da3acc4 L |
284 | uint16_t i; |
285 | char c, c0; | |
323398b1 | 286 | |
1da3acc4 L |
287 | crc = 0xffff; |
288 | c = 0xff; | |
289 | for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++) | |
290 | { | |
7e24905c | 291 | c0 = c; |
1da3acc4 L |
292 | c = eeprom_read_byte((const uint8_t *) data_offset + i); |
293 | crc = crc16(crc, c); | |
323398b1 | 294 | } |
1da3acc4 L |
295 | return crc ; |
296 | } | |
323398b1 L |
297 | |
298 | ||
1da3acc4 L |
299 | /** |
300 | * return valid env | |
323398b1 | 301 | */ |
1da3acc4 L |
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 | ); | |
323398b1 | 327 | |
1da3acc4 L |
328 | if (!crc_ok[0] && !crc_ok[1]) { |
329 | rc = 0; | |
323398b1 | 330 | |
1da3acc4 L |
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 | |
323398b1 | 356 | } |
7e24905c | 357 | |
1da3acc4 L |
358 | return rc; |
359 | } | |
323398b1 L |
360 | |
361 | ||
1da3acc4 | 362 | int env_init(void) |
323398b1 | 363 | { |
1da3acc4 L |
364 | env_valid = env_check_valid(); |
365 | if (env_valid == 0) { | |
55027f3b | 366 | printf_P(PSTR("*** Warning - bad CRC, " |
1da3acc4 | 367 | "using default environment\n\n")); |
d684c216 | 368 | } |
55027f3b | 369 | |
7e24905c L |
370 | envlist_import(ENVLIST_DELETE); |
371 | return 0; | |
323398b1 | 372 | } |
d684c216 | 373 | |
1da3acc4 | 374 | |
bddc7e77 | 375 | char *getenv_str(const MEMX char *name) |
d684c216 | 376 | { |
1816a698 | 377 | char *ep = envlist_search(name); |
d684c216 | 378 | if (ep != NULL) |
1816a698 L |
379 | return strchr(ep, '=') + 1; |
380 | else | |
381 | return NULL; | |
d684c216 L |
382 | } |
383 | ||
1da3acc4 | 384 | static |
1816a698 | 385 | int env_item_save(char *ep, uint16_t offset, int space_left) |
323398b1 | 386 | { |
1816a698 | 387 | int len; |
323398b1 | 388 | |
1816a698 L |
389 | len = strlen(ep); |
390 | if (len == 0) | |
1da3acc4 | 391 | return 0; |
1816a698 L |
392 | len += 1; |
393 | if (len > space_left) | |
1da3acc4 | 394 | return 0; |
7e24905c | 395 | |
1816a698 | 396 | eeprom_update_block(ep, (uint8_t *) offset, len); |
55027f3b | 397 | |
1816a698 | 398 | return len; |
7e24905c | 399 | } |
1da3acc4 | 400 | |
323398b1 | 401 | |
1da3acc4 | 402 | static |
d684c216 L |
403 | int saveenv(void) |
404 | { | |
323398b1 L |
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; | |
d684c216 | 411 | |
323398b1 L |
412 | if (env_valid == 2) { |
413 | off = CONFIG_ENV_OFFSET; | |
414 | off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
415 | } | |
416 | ||
7e24905c | 417 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); |
d684c216 | 418 | |
323398b1 L |
419 | pos = off + offsetof(env_t, data); |
420 | left = ENV_SIZE - 1; | |
1816a698 L |
421 | |
422 | char *ep = env_list; | |
423 | while (*ep) { | |
424 | len = env_item_save(ep, pos, left); | |
323398b1 L |
425 | if (len == 0) { |
426 | return 1; | |
427 | } | |
428 | pos += len; | |
429 | left -= len; | |
1816a698 | 430 | while (*ep++); |
323398b1 | 431 | } |
1816a698 | 432 | |
323398b1 L |
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); | |
7e24905c | 437 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), |
323398b1 L |
438 | ACTIVE_FLAG); |
439 | ||
440 | if (rc == 0) { | |
7e24905c | 441 | eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), |
323398b1 L |
442 | OBSOLETE_FLAG); |
443 | env_valid = (env_valid == 2) ? 1 : 2; | |
323398b1 | 444 | } |
7e24905c | 445 | |
d684c216 L |
446 | return rc; |
447 | } | |
448 | ||
d684c216 | 449 | |
1da3acc4 | 450 | static |
1816a698 | 451 | int env_item_print(char *ep, bool mode) |
d684c216 | 452 | { |
1816a698 | 453 | int len = strlen(ep) + 1; |
55027f3b | 454 | |
8eb73d17 | 455 | if (mode) { |
1816a698 L |
456 | my_puts_P(PSTR("setenv ")); |
457 | char ch; | |
458 | while ((ch = *ep++) != '=') | |
459 | putchar(ch); | |
460 | printf_P(PSTR(" '%s'\n"), ep); | |
8eb73d17 | 461 | } else { |
1816a698 | 462 | puts(ep); |
8eb73d17 | 463 | } |
1816a698 | 464 | |
1da3acc4 | 465 | return len; |
7e24905c | 466 | } |
323398b1 L |
467 | |
468 | ||
d684c216 L |
469 | /* |
470 | * Command interface: print one or all environment variables | |
471 | * | |
472 | * Returns -1 in case of error, or length of printed string | |
473 | */ | |
7e24905c | 474 | static |
1816a698 | 475 | int env_print(const char *name, bool mode) |
d684c216 L |
476 | { |
477 | int len = -1; | |
1816a698 | 478 | char *ep; |
d684c216 | 479 | |
1816a698 | 480 | if (name != NULL) { /* print a single name */ |
7e24905c | 481 | |
1816a698 | 482 | ep = envlist_search(name); |
d684c216 | 483 | if (ep != NULL) |
8eb73d17 | 484 | len = env_item_print(ep, mode); |
d684c216 L |
485 | |
486 | } else { /* print whole list */ | |
487 | len = 0; | |
1816a698 L |
488 | ep = env_list; |
489 | while (*ep) { | |
490 | len += env_item_print(ep, mode); | |
491 | while (*ep++); | |
d684c216 L |
492 | } |
493 | } | |
494 | return len; | |
495 | } | |
496 | ||
1da3acc4 | 497 | |
72f58822 L |
498 | /** |
499 | * Set or delete environment variable | |
500 | * | |
d684c216 L |
501 | * Set a new environment variable, |
502 | * or replace or delete an existing one. | |
72f58822 L |
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 | |
d684c216 | 511 | */ |
1da3acc4 | 512 | static |
1816a698 | 513 | command_ret_t _do_env_set(uint_fast8_t flag UNUSED, int argc, char * const argv[]) |
d684c216 | 514 | { |
1816a698 | 515 | char *name = argv[1]; |
d684c216 L |
516 | |
517 | if (strchr(name, '=')) { | |
518 | printf_P(PSTR("## Error: illegal character '='" | |
519 | "in variable name \"%s\"\n"), name); | |
d0581f88 | 520 | return CMD_RET_FAILURE; |
d684c216 | 521 | } |
1816a698 | 522 | size_t len = strlen(name); |
55027f3b | 523 | if (len > CONFIG_SYS_ENV_NAMELEN) { |
d684c216 L |
524 | printf_P(PSTR("## Error: Variable name \"%s\" too long. " |
525 | "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN); | |
d0581f88 | 526 | return CMD_RET_FAILURE; |
d684c216 | 527 | } |
1816a698 | 528 | |
d684c216 | 529 | /* Delete only ? */ |
1816a698 | 530 | if (argc < 3) { |
d684c216 | 531 | int rc = envlist_delete(name); |
d0581f88 | 532 | return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
d684c216 L |
533 | } |
534 | ||
1816a698 | 535 | /* Insert / replace new value */ |
8b2c7c22 | 536 | |
1816a698 | 537 | for (int_fast8_t i = 2; i < argc; ++i) |
d684c216 L |
538 | len += strlen(argv[i]) + 1; |
539 | ||
8b2c7c22 L |
540 | char *pos = envlist_alloc(name, len); |
541 | if (pos == NULL) { | |
542 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); | |
d0581f88 | 543 | return CMD_RET_FAILURE; |
d684c216 | 544 | } |
1816a698 | 545 | |
8b2c7c22 L |
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) = ' '; | |
d684c216 | 552 | } |
8b2c7c22 | 553 | *(vp-1) = '\0'; |
d684c216 | 554 | |
d0581f88 | 555 | return CMD_RET_SUCCESS; |
d684c216 L |
556 | } |
557 | ||
72f58822 L |
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 | */ | |
1da3acc4 | 565 | static |
c0abd68b | 566 | int setenv(const MEMX char *varname, const char *varvalue) |
d684c216 | 567 | { |
c0abd68b L |
568 | int rc; |
569 | ||
570 | #ifdef __MEMX | |
1816a698 | 571 | char *tmpname; |
c0abd68b L |
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; | |
c0abd68b L |
581 | |
582 | const char * const argv[3] = { NULL, tmpname, varvalue }; | |
1816a698 L |
583 | #else |
584 | const char * const argv[3] = { NULL, varname, varvalue }; | |
585 | #endif | |
586 | ||
d684c216 L |
587 | int argc = 3; |
588 | ||
589 | if (varvalue == NULL || varvalue[0] == '\0') | |
590 | --argc; | |
7e24905c | 591 | |
c0abd68b L |
592 | rc = (int) _do_env_set(0, argc, (char * const *)argv); |
593 | ||
594 | #ifdef __MEMX | |
1816a698 L |
595 | if (__builtin_avr_flash_segment(varname) != -1) |
596 | free(tmpname); | |
c0abd68b L |
597 | #endif |
598 | return rc; | |
d684c216 L |
599 | } |
600 | ||
d684c216 L |
601 | /** |
602 | * Set an environment variable to an integer value | |
603 | * | |
f76ca346 L |
604 | * @param name Environment variable to set |
605 | * @param value Value to set it to | |
606 | * @param radix Base | |
d684c216 L |
607 | * @return 0 if ok, 1 on error |
608 | */ | |
f76ca346 L |
609 | static |
610 | int setenv_intval(const MEMX char *name, unsigned long value, int radix) | |
d684c216 | 611 | { |
f76ca346 | 612 | char buf[11]; |
d684c216 | 613 | |
f76ca346 | 614 | ultoa(value, buf, radix); |
d684c216 | 615 | |
f76ca346 L |
616 | return setenv(name, buf); |
617 | } | |
d684c216 L |
618 | |
619 | /** | |
f76ca346 | 620 | * Set an environment variable to a decimal integer value |
d684c216 | 621 | * |
f76ca346 L |
622 | * @param name Environment variable to set |
623 | * @param value Value to set it to | |
d684c216 L |
624 | * @return 0 if ok, 1 on error |
625 | */ | |
f76ca346 | 626 | int setenv_ulong(const MEMX char *name, unsigned long value) |
d684c216 | 627 | { |
f76ca346 L |
628 | return setenv_intval(name, value, 10); |
629 | } | |
630 | ||
d684c216 | 631 | |
f76ca346 L |
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); | |
d684c216 L |
642 | } |
643 | ||
35e9ec0c | 644 | |
72f58822 | 645 | /** |
35e9ec0c | 646 | * Decode the integer value of an environment variable and return it. |
72f58822 | 647 | * |
35e9ec0c L |
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 | |
72f58822 | 653 | */ |
fc30b18e | 654 | unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val) |
d684c216 | 655 | { |
d684c216 | 656 | unsigned long value; |
35e9ec0c L |
657 | char *vp, *endp; |
658 | ||
1816a698 | 659 | vp = getenv_str(name); |
d684c216 | 660 | |
1816a698 | 661 | if (vp != NULL ) { |
55027f3b L |
662 | value = strtoul(vp, &endp, base); |
663 | if (endp != vp) | |
664 | return value; | |
35e9ec0c | 665 | } |
d684c216 | 666 | |
35e9ec0c | 667 | return default_val; |
d684c216 L |
668 | } |
669 | ||
35e9ec0c | 670 | |
be84a704 L |
671 | /* |
672 | * Read an environment variable as a boolean | |
673 | */ | |
674 | bool getenv_yesno(const MEMX char *name) | |
675 | { | |
bddc7e77 | 676 | char *s = getenv_str(name); |
be84a704 L |
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 | ||
1816a698 L |
689 | command_ret_t do_env_print(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, |
690 | int argc, char * const argv[]) | |
f76ca346 | 691 | { |
8eb73d17 | 692 | bool mode = 0; |
f76ca346 L |
693 | command_ret_t rc = CMD_RET_SUCCESS; |
694 | ||
8eb73d17 L |
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 | } | |
f76ca346 | 705 | |
8eb73d17 | 706 | if (optind == argc) { |
f76ca346 | 707 | /* print all env vars */ |
8eb73d17 | 708 | int size = env_print(NULL, mode); |
f76ca346 L |
709 | if (size < 0) |
710 | return CMD_RET_FAILURE; | |
8eb73d17 L |
711 | if (mode == 0) |
712 | printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"), | |
b0a9d14c | 713 | size, ENV_SIZE - 1); |
f76ca346 L |
714 | return CMD_RET_SUCCESS; |
715 | } | |
716 | ||
717 | /* print selected env vars */ | |
8eb73d17 | 718 | while (optind < argc) { |
f1e16f88 L |
719 | int len = env_print(argv[optind], mode); |
720 | if (len < 0) { | |
8eb73d17 | 721 | printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[optind]); |
f76ca346 L |
722 | rc = CMD_RET_FAILURE; |
723 | } | |
8eb73d17 | 724 | optind++; |
f76ca346 L |
725 | } |
726 | ||
727 | return rc; | |
728 | } | |
729 | ||
730 | ||
1816a698 | 731 | command_ret_t do_env_set(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[]) |
d684c216 | 732 | { |
d684c216 L |
733 | if (argc < 2) |
734 | return CMD_RET_USAGE; | |
735 | ||
736 | return _do_env_set(flag, argc, argv); | |
737 | } | |
738 | ||
739 | ||
1816a698 L |
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) | |
7e24905c | 742 | { |
55027f3b L |
743 | uint8_t tmp = env_valid; |
744 | env_valid = 0; | |
745 | ||
7e24905c | 746 | /* Reset the whole environment */ |
55027f3b | 747 | printf_P(PSTR("## Resetting to default environment\n")); |
7e24905c L |
748 | envlist_import(ENVLIST_DELETE); |
749 | ||
55027f3b L |
750 | env_valid = tmp; |
751 | ||
7e24905c L |
752 | return CMD_RET_SUCCESS; |
753 | } | |
754 | ||
55027f3b | 755 | |
1816a698 | 756 | command_ret_t do_env_save(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) |
323398b1 | 757 | { |
323398b1 | 758 | printf_P(PSTR("Saving Environment ...\n")); |
d0581f88 | 759 | return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
323398b1 L |
760 | } |
761 | ||
55027f3b | 762 | |
7e24905c | 763 | #if defined(CONFIG_AUTO_COMPLETE) |
d684c216 L |
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 | |
dab74a42 L |
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 | } |