]>
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; | |
49475345 | 66 | /* FALL TROUGH */ |
55027f3b L |
67 | case 1: |
68 | ret = (char) eeprom_read_byte((const uint8_t *)off + index + | |
d684c216 | 69 | offsetof(env_t, data)); |
55027f3b | 70 | break; |
d684c216 | 71 | |
55027f3b L |
72 | default: |
73 | ret = default_env[index]; | |
d684c216 | 74 | } |
d684c216 | 75 | |
55027f3b | 76 | return ret; |
d684c216 L |
77 | } |
78 | ||
1816a698 L |
79 | static void envlist_clear(void) |
80 | { | |
81 | memset(env_list, 0, sizeof env_list); | |
82 | } | |
04cffff0 | 83 | |
d684c216 | 84 | static |
1816a698 | 85 | int env_item_delete(char *ep) |
d684c216 | 86 | { |
1816a698 L |
87 | char *next = ep + strlen(ep) + 1; |
88 | char *end = next; | |
dd2c2ec0 | 89 | while (*end++ != 0) |
1816a698 | 90 | while (*end++ != 0); |
7e24905c | 91 | |
1816a698 | 92 | memmove(ep, next, end - next); |
d684c216 | 93 | |
1816a698 L |
94 | return 0; |
95 | } | |
1da3acc4 | 96 | |
dd2c2ec0 L |
97 | static int envcmp(const char *s1, const char *s2) |
98 | { | |
99 | const unsigned char *u1 = (const unsigned char *)s1; | |
100 | const unsigned char *u2 = (const unsigned char *)s2; | |
101 | unsigned char c1, c2; | |
102 | ||
103 | do { | |
104 | c1 = *u1++; c2 = *u2++; | |
105 | if (c1 == '=') c1 = '\0'; | |
106 | if (c2 == '=') c2 = '\0'; | |
107 | } while (c1 != 0 && c1 - c2 == 0); | |
108 | ||
109 | return c1 - c2; | |
110 | } | |
111 | ||
1816a698 | 112 | static char *envlist_search(const MEMX char *name) |
7e24905c | 113 | { |
1816a698 L |
114 | char key[CONFIG_SYS_ENV_NAMELEN + 1]; |
115 | const MEMX char *np = name; | |
116 | int keylen = 0; | |
117 | char c; | |
118 | ||
119 | while (keylen <= CONFIG_SYS_ENV_NAMELEN && (c = *np++) != '\0') | |
120 | key[keylen++] = c; | |
121 | key[keylen] = '\0'; | |
122 | ||
1816a698 | 123 | for (char *lp = env_list; *lp != 0; ++lp) { |
04cffff0 | 124 | |
dd2c2ec0 | 125 | if (envcmp(lp, key) == 0) |
1816a698 | 126 | return lp; |
04cffff0 | 127 | |
1816a698 L |
128 | while (*lp != 0) |
129 | ++lp; | |
7e24905c | 130 | } |
7e24905c | 131 | |
1816a698 | 132 | return NULL; |
7e24905c L |
133 | } |
134 | ||
8b2c7c22 L |
135 | static char *env_item_insert_name(char *pos, const char *name, int len) |
136 | { | |
137 | char *dstp = pos + len + 1; | |
138 | char *end = pos; | |
139 | while (*end++ != 0) | |
140 | while (*end++ != 0); | |
141 | ||
b0a9d14c | 142 | if (end + len >= env_list + ENV_SIZE) |
8b2c7c22 L |
143 | return NULL; |
144 | ||
145 | memmove(dstp, pos, end - pos); | |
146 | strcpy(pos, name); | |
147 | memset(pos + strlen(name), '=', dstp - pos - strlen(name)); | |
148 | *(dstp - 1) = '\0'; | |
149 | ||
150 | return pos; | |
151 | } | |
152 | ||
153 | ||
154 | static char *envlist_alloc(const char *name, int len) | |
155 | { | |
156 | char *lp; | |
157 | int rc; | |
158 | ||
159 | ||
160 | for (lp = env_list; *lp != 0; ++lp) { | |
161 | ||
162 | rc = envcmp(lp, name); | |
163 | if (rc >= 0) | |
164 | break; | |
165 | ||
166 | /* rc < 0 : check next place */ | |
167 | while (*lp != 0) | |
168 | ++lp; | |
169 | } | |
170 | ||
171 | /* rc == 0 : entry found, replace */ | |
172 | if (rc == 0) | |
173 | env_item_delete(lp); | |
174 | ||
175 | /* rc > 0 : entry not in list, insert */ | |
176 | /* *lp == 0 : entry not in list, insert */ | |
177 | ||
178 | lp = env_item_insert_name(lp, name, len); | |
179 | ||
180 | return lp; | |
181 | } | |
182 | ||
183 | #if 0 | |
1816a698 | 184 | static char *env_item_insert(char *pos, const char *envstr) |
7e24905c | 185 | { |
1816a698 L |
186 | char *dstp = pos + strlen(envstr) + 1; |
187 | char *end = pos; | |
dd2c2ec0 | 188 | while (*end++ != 0) |
1816a698 | 189 | while (*end++ != 0); |
7e24905c | 190 | |
1816a698 L |
191 | if (end > env_list + ENV_SIZE) |
192 | return NULL; | |
7e24905c | 193 | |
1816a698 L |
194 | memmove(dstp, pos, end - pos); |
195 | strcpy(pos, envstr); | |
7e24905c | 196 | |
1816a698 | 197 | return pos; |
7e24905c L |
198 | } |
199 | ||
1816a698 | 200 | static char *envlist_enter(const char *ep) |
7e24905c | 201 | { |
1816a698 | 202 | char *lp; |
1816a698 | 203 | int rc; |
55027f3b | 204 | |
1816a698 | 205 | for (lp = env_list; *lp != 0; ++lp) { |
7e24905c | 206 | |
dd2c2ec0 | 207 | rc = envcmp(lp, ep); |
1816a698 L |
208 | if (rc >= 0) |
209 | break; | |
210 | ||
211 | /* rc < 0 : check next place */ | |
212 | while (*lp != 0) | |
213 | ++lp; | |
214 | } | |
215 | ||
216 | /* rc == 0 : entry found, replace */ | |
217 | if (rc == 0) | |
218 | env_item_delete(lp); | |
219 | ||
220 | /* rc > 0 : entry not in list, insert */ | |
221 | /* *lp == 0 : entry not in list, insert */ | |
222 | ||
223 | lp = env_item_insert(lp, ep); | |
224 | ||
225 | return lp; | |
7e24905c | 226 | } |
8b2c7c22 | 227 | #endif |
1816a698 | 228 | |
7e24905c | 229 | static |
04cffff0 | 230 | int envlist_delete(const MEMX char *name) |
7e24905c | 231 | { |
1816a698 | 232 | char *ep = envlist_search(name); |
7e24905c L |
233 | |
234 | if (ep != NULL) | |
235 | return env_item_delete(ep); | |
236 | ||
237 | return 1; | |
238 | } | |
239 | ||
7e24905c L |
240 | static |
241 | int envlist_import(uint8_t flags) | |
d684c216 | 242 | { |
7e24905c | 243 | if ((flags & ENVLIST_DELETE) != 0) |
1816a698 | 244 | envlist_clear(); |
7e24905c | 245 | |
8b2c7c22 L |
246 | for (uint_fast16_t index = 0; env_get_char(index) != '\0'; ) { |
247 | char name[CONFIG_SYS_ENV_NAMELEN+1]; | |
248 | uint_fast8_t nlen = 0; | |
249 | size_t len; | |
250 | char ch; | |
251 | for (len = 0; (ch = env_get_char(index + len)) != '\0'; ++len) { | |
55027f3b L |
252 | if ((index + len) >= ENV_SIZE) |
253 | return -1; | |
8b2c7c22 L |
254 | if (nlen == 0) { |
255 | if (ch == '=') { | |
256 | nlen = len; | |
257 | name[nlen] = '\0'; | |
258 | } else { | |
259 | if (len > CONFIG_SYS_ENV_NAMELEN) | |
260 | return -1; | |
261 | name[len] = ch; | |
262 | } | |
263 | } | |
1da3acc4 | 264 | } |
d684c216 | 265 | |
8b2c7c22 | 266 | char *ep = envlist_alloc(name, len); |
1816a698 | 267 | if (ep == NULL) { |
8b2c7c22 | 268 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); |
55027f3b | 269 | return 1; |
d684c216 | 270 | } |
8b2c7c22 L |
271 | |
272 | index += nlen; | |
273 | char *p = ep + nlen; | |
1816a698 L |
274 | while ((*p++ = env_get_char(index++)) != '\0') |
275 | ; | |
d684c216 | 276 | } |
1da3acc4 L |
277 | return 0; |
278 | } | |
d684c216 L |
279 | |
280 | ||
1da3acc4 L |
281 | static |
282 | uint16_t env_crc(uint16_t data_offset) | |
323398b1 | 283 | { |
7e24905c | 284 | uint16_t crc; |
1da3acc4 L |
285 | uint16_t i; |
286 | char c, c0; | |
323398b1 | 287 | |
1da3acc4 L |
288 | crc = 0xffff; |
289 | c = 0xff; | |
290 | for (i = 0; !(c == 0 && c0 == 0) && i < ENV_SIZE; i++) | |
291 | { | |
7e24905c | 292 | c0 = c; |
1da3acc4 L |
293 | c = eeprom_read_byte((const uint8_t *) data_offset + i); |
294 | crc = crc16(crc, c); | |
323398b1 | 295 | } |
1da3acc4 L |
296 | return crc ; |
297 | } | |
323398b1 L |
298 | |
299 | ||
1da3acc4 L |
300 | /** |
301 | * return valid env | |
323398b1 | 302 | */ |
1da3acc4 L |
303 | static |
304 | int env_check_valid(void) | |
305 | { | |
306 | const uint16_t offset[2] = {CONFIG_ENV_OFFSET, | |
307 | CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE}; | |
308 | uint_fast8_t flags[2], crc_ok[2]; | |
309 | int rc; | |
310 | ||
311 | /* read FLAGS */ | |
312 | flags[0] = eeprom_read_byte ((uint8_t *) offset[0] + | |
313 | offsetof(env_t, flags)); | |
314 | flags[1] = eeprom_read_byte ((uint8_t *) offset[1] + | |
315 | offsetof(env_t, flags)); | |
316 | ||
317 | /* check CRC */ | |
318 | crc_ok[0] = ( | |
319 | eeprom_read_word((uint16_t *) offset[0] + | |
320 | offsetof(env_t, crc)) | |
321 | == env_crc(offset[0] + offsetof(env_t, data)) | |
322 | ); | |
323 | crc_ok[1] = ( | |
324 | eeprom_read_word((uint16_t *) offset[1] + | |
325 | offsetof(env_t, crc)) | |
326 | == env_crc(offset[1] + offsetof(env_t, data)) | |
327 | ); | |
323398b1 | 328 | |
1da3acc4 L |
329 | if (!crc_ok[0] && !crc_ok[1]) { |
330 | rc = 0; | |
323398b1 | 331 | |
1da3acc4 L |
332 | } else if (crc_ok[0] && !crc_ok[1]) { |
333 | rc = 1; | |
334 | } else if (!crc_ok[0] && crc_ok[1]) { | |
335 | rc = 2; | |
336 | } else { | |
337 | /* both ok - check serial */ | |
338 | #if 1 | |
339 | if (flags[1] == ACTIVE_FLAG && flags[0] != ACTIVE_FLAG) | |
340 | rc = 2; | |
341 | else if (flags[1] == OBSOLETE_FLAG && flags[0] == 0xFF) | |
342 | rc = 2; | |
343 | else | |
344 | rc = 1; | |
345 | #else | |
346 | if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG) | |
347 | rc = 1; | |
348 | else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG) | |
349 | rc = 2; | |
350 | else if (flags[0] == 0xFF && flags[1] == 0) | |
351 | rc = 2; | |
352 | else if (flags[1] == 0xFF && flags[0] == 0) | |
353 | rc = 1; | |
354 | else /* flags are equal - almost impossible */ | |
355 | rc = 1; | |
356 | #endif | |
323398b1 | 357 | } |
7e24905c | 358 | |
1da3acc4 L |
359 | return rc; |
360 | } | |
323398b1 L |
361 | |
362 | ||
1da3acc4 | 363 | int env_init(void) |
323398b1 | 364 | { |
1da3acc4 L |
365 | env_valid = env_check_valid(); |
366 | if (env_valid == 0) { | |
55027f3b | 367 | printf_P(PSTR("*** Warning - bad CRC, " |
1da3acc4 | 368 | "using default environment\n\n")); |
d684c216 | 369 | } |
55027f3b | 370 | |
7e24905c L |
371 | envlist_import(ENVLIST_DELETE); |
372 | return 0; | |
323398b1 | 373 | } |
d684c216 | 374 | |
1da3acc4 | 375 | |
bddc7e77 | 376 | char *getenv_str(const MEMX char *name) |
d684c216 | 377 | { |
1816a698 | 378 | char *ep = envlist_search(name); |
d684c216 | 379 | if (ep != NULL) |
1816a698 L |
380 | return strchr(ep, '=') + 1; |
381 | else | |
382 | return NULL; | |
d684c216 L |
383 | } |
384 | ||
1da3acc4 | 385 | static |
1816a698 | 386 | int env_item_save(char *ep, uint16_t offset, int space_left) |
323398b1 | 387 | { |
1816a698 | 388 | int len; |
323398b1 | 389 | |
1816a698 L |
390 | len = strlen(ep); |
391 | if (len == 0) | |
1da3acc4 | 392 | return 0; |
1816a698 L |
393 | len += 1; |
394 | if (len > space_left) | |
1da3acc4 | 395 | return 0; |
7e24905c | 396 | |
1816a698 | 397 | eeprom_update_block(ep, (uint8_t *) offset, len); |
55027f3b | 398 | |
1816a698 | 399 | return len; |
7e24905c | 400 | } |
1da3acc4 | 401 | |
323398b1 | 402 | |
1da3acc4 | 403 | static |
d684c216 L |
404 | int saveenv(void) |
405 | { | |
323398b1 L |
406 | unsigned int off = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
407 | unsigned int off_red = CONFIG_ENV_OFFSET; | |
408 | unsigned int pos; | |
409 | int len, left; | |
410 | uint16_t crc; | |
411 | int rc = 0; | |
d684c216 | 412 | |
323398b1 L |
413 | if (env_valid == 2) { |
414 | off = CONFIG_ENV_OFFSET; | |
415 | off_red = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
416 | } | |
417 | ||
7e24905c | 418 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), 0xff); |
d684c216 | 419 | |
323398b1 L |
420 | pos = off + offsetof(env_t, data); |
421 | left = ENV_SIZE - 1; | |
1816a698 L |
422 | |
423 | char *ep = env_list; | |
424 | while (*ep) { | |
425 | len = env_item_save(ep, pos, left); | |
323398b1 L |
426 | if (len == 0) { |
427 | return 1; | |
428 | } | |
429 | pos += len; | |
430 | left -= len; | |
1816a698 | 431 | while (*ep++); |
323398b1 | 432 | } |
1816a698 | 433 | |
323398b1 L |
434 | /* terminate list */ |
435 | eeprom_update_byte((uint8_t *) pos, 0); | |
436 | crc = env_crc(off + offsetof(env_t, data)); | |
437 | eeprom_update_word((uint16_t *) off + offsetof(env_t, crc), crc); | |
7e24905c | 438 | eeprom_update_byte((uint8_t *) off + offsetof(env_t, flags), |
323398b1 L |
439 | ACTIVE_FLAG); |
440 | ||
441 | if (rc == 0) { | |
7e24905c | 442 | eeprom_update_byte((uint8_t *) off_red + offsetof(env_t, flags), |
323398b1 L |
443 | OBSOLETE_FLAG); |
444 | env_valid = (env_valid == 2) ? 1 : 2; | |
323398b1 | 445 | } |
7e24905c | 446 | |
d684c216 L |
447 | return rc; |
448 | } | |
449 | ||
d684c216 | 450 | |
1da3acc4 | 451 | static |
1816a698 | 452 | int env_item_print(char *ep, bool mode) |
d684c216 | 453 | { |
1816a698 | 454 | int len = strlen(ep) + 1; |
55027f3b | 455 | |
8eb73d17 | 456 | if (mode) { |
1816a698 L |
457 | my_puts_P(PSTR("setenv ")); |
458 | char ch; | |
459 | while ((ch = *ep++) != '=') | |
460 | putchar(ch); | |
461 | printf_P(PSTR(" '%s'\n"), ep); | |
8eb73d17 | 462 | } else { |
1816a698 | 463 | puts(ep); |
8eb73d17 | 464 | } |
1816a698 | 465 | |
1da3acc4 | 466 | return len; |
7e24905c | 467 | } |
323398b1 L |
468 | |
469 | ||
d684c216 L |
470 | /* |
471 | * Command interface: print one or all environment variables | |
472 | * | |
473 | * Returns -1 in case of error, or length of printed string | |
474 | */ | |
7e24905c | 475 | static |
1816a698 | 476 | int env_print(const char *name, bool mode) |
d684c216 L |
477 | { |
478 | int len = -1; | |
1816a698 | 479 | char *ep; |
d684c216 | 480 | |
1816a698 | 481 | if (name != NULL) { /* print a single name */ |
7e24905c | 482 | |
1816a698 | 483 | ep = envlist_search(name); |
d684c216 | 484 | if (ep != NULL) |
8eb73d17 | 485 | len = env_item_print(ep, mode); |
d684c216 L |
486 | |
487 | } else { /* print whole list */ | |
488 | len = 0; | |
1816a698 L |
489 | ep = env_list; |
490 | while (*ep) { | |
491 | len += env_item_print(ep, mode); | |
492 | while (*ep++); | |
d684c216 L |
493 | } |
494 | } | |
495 | return len; | |
496 | } | |
497 | ||
1da3acc4 | 498 | |
72f58822 L |
499 | /** |
500 | * Set or delete environment variable | |
501 | * | |
d684c216 L |
502 | * Set a new environment variable, |
503 | * or replace or delete an existing one. | |
72f58822 L |
504 | * |
505 | * @param flag (not used) | |
506 | * @param argc | |
507 | * @param argv[1] Environment variable to set or delete | |
508 | * if no more args | |
509 | * @param argv[2] ... Value to set it to | |
510 | * | |
511 | * @return 0 if ok, 1 on error | |
d684c216 | 512 | */ |
1da3acc4 | 513 | static |
1816a698 | 514 | command_ret_t _do_env_set(uint_fast8_t flag UNUSED, int argc, char * const argv[]) |
d684c216 | 515 | { |
1816a698 | 516 | char *name = argv[1]; |
d684c216 L |
517 | |
518 | if (strchr(name, '=')) { | |
519 | printf_P(PSTR("## Error: illegal character '='" | |
520 | "in variable name \"%s\"\n"), name); | |
d0581f88 | 521 | return CMD_RET_FAILURE; |
d684c216 | 522 | } |
1816a698 | 523 | size_t len = strlen(name); |
55027f3b | 524 | if (len > CONFIG_SYS_ENV_NAMELEN) { |
d684c216 L |
525 | printf_P(PSTR("## Error: Variable name \"%s\" too long. " |
526 | "(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN); | |
d0581f88 | 527 | return CMD_RET_FAILURE; |
d684c216 | 528 | } |
1816a698 | 529 | |
d684c216 | 530 | /* Delete only ? */ |
1816a698 | 531 | if (argc < 3) { |
d684c216 | 532 | int rc = envlist_delete(name); |
d0581f88 | 533 | return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
d684c216 L |
534 | } |
535 | ||
1816a698 | 536 | /* Insert / replace new value */ |
8b2c7c22 | 537 | |
1816a698 | 538 | for (int_fast8_t i = 2; i < argc; ++i) |
d684c216 L |
539 | len += strlen(argv[i]) + 1; |
540 | ||
8b2c7c22 L |
541 | char *pos = envlist_alloc(name, len); |
542 | if (pos == NULL) { | |
543 | printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name); | |
d0581f88 | 544 | return CMD_RET_FAILURE; |
d684c216 | 545 | } |
1816a698 | 546 | |
8b2c7c22 L |
547 | char *vp = pos + strlen(name) + 1; |
548 | for (int_fast8_t i = 2; i < argc; ++i) { | |
549 | char *ap = argv[i]; | |
550 | while ((*vp++ = *ap++) != '\0'); | |
551 | //if (i < argc-1) | |
552 | *(vp-1) = ' '; | |
d684c216 | 553 | } |
8b2c7c22 | 554 | *(vp-1) = '\0'; |
d684c216 | 555 | |
d0581f88 | 556 | return CMD_RET_SUCCESS; |
d684c216 L |
557 | } |
558 | ||
72f58822 L |
559 | /** |
560 | * Set an environment variable | |
561 | * | |
562 | * @param varname Environment variable to set | |
563 | * @param varvalue Value to set it to | |
564 | * @return 0 if ok, 1 on error | |
565 | */ | |
d655d20a | 566 | |
c0abd68b | 567 | int setenv(const MEMX char *varname, const char *varvalue) |
d684c216 | 568 | { |
c0abd68b L |
569 | int rc; |
570 | ||
571 | #ifdef __MEMX | |
1816a698 | 572 | char *tmpname; |
c0abd68b L |
573 | if (__builtin_avr_flash_segment(varname) != -1) { |
574 | tmpname = malloc(strlen_P(varname)+1); | |
575 | if (tmpname == NULL) { | |
576 | printf_P(PSTR("setenv: Out of Memory!\n")); | |
577 | return 1; | |
578 | } | |
579 | strcpy_P(tmpname, varname); | |
580 | } else | |
581 | tmpname = (char *) varname; | |
c0abd68b L |
582 | |
583 | const char * const argv[3] = { NULL, tmpname, varvalue }; | |
1816a698 L |
584 | #else |
585 | const char * const argv[3] = { NULL, varname, varvalue }; | |
586 | #endif | |
587 | ||
d684c216 L |
588 | int argc = 3; |
589 | ||
590 | if (varvalue == NULL || varvalue[0] == '\0') | |
591 | --argc; | |
7e24905c | 592 | |
c0abd68b L |
593 | rc = (int) _do_env_set(0, argc, (char * const *)argv); |
594 | ||
595 | #ifdef __MEMX | |
1816a698 L |
596 | if (__builtin_avr_flash_segment(varname) != -1) |
597 | free(tmpname); | |
c0abd68b L |
598 | #endif |
599 | return rc; | |
d684c216 L |
600 | } |
601 | ||
d684c216 L |
602 | /** |
603 | * Set an environment variable to an integer value | |
604 | * | |
f76ca346 L |
605 | * @param name Environment variable to set |
606 | * @param value Value to set it to | |
607 | * @param radix Base | |
d684c216 L |
608 | * @return 0 if ok, 1 on error |
609 | */ | |
f76ca346 L |
610 | static |
611 | int setenv_intval(const MEMX char *name, unsigned long value, int radix) | |
d684c216 | 612 | { |
f76ca346 | 613 | char buf[11]; |
d684c216 | 614 | |
f76ca346 | 615 | ultoa(value, buf, radix); |
d684c216 | 616 | |
f76ca346 L |
617 | return setenv(name, buf); |
618 | } | |
d684c216 L |
619 | |
620 | /** | |
f76ca346 | 621 | * Set an environment variable to a decimal integer value |
d684c216 | 622 | * |
f76ca346 L |
623 | * @param name Environment variable to set |
624 | * @param value Value to set it to | |
d684c216 L |
625 | * @return 0 if ok, 1 on error |
626 | */ | |
f76ca346 | 627 | int setenv_ulong(const MEMX char *name, unsigned long value) |
d684c216 | 628 | { |
f76ca346 L |
629 | return setenv_intval(name, value, 10); |
630 | } | |
631 | ||
d684c216 | 632 | |
f76ca346 L |
633 | /** |
634 | * Set an environment variable to a value in hex | |
635 | * | |
636 | * @param name Environment variable to set | |
637 | * @param value Value to set it to | |
638 | * @return 0 if ok, 1 on error | |
639 | */ | |
640 | int setenv_hex(const MEMX char *name, unsigned long value) | |
641 | { | |
642 | return setenv_intval(name, value, 16); | |
d684c216 L |
643 | } |
644 | ||
35e9ec0c | 645 | |
72f58822 | 646 | /** |
35e9ec0c | 647 | * Decode the integer value of an environment variable and return it. |
72f58822 | 648 | * |
35e9ec0c L |
649 | * @param name Name of environemnt variable |
650 | * @param base Number base to use (normally 10, or 16 for hex) | |
651 | * @param default_val Default value to return if the variable is not | |
652 | * found | |
653 | * @return the decoded value, or default_val if not found | |
72f58822 | 654 | */ |
fc30b18e | 655 | unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val) |
d684c216 | 656 | { |
d684c216 | 657 | unsigned long value; |
35e9ec0c L |
658 | char *vp, *endp; |
659 | ||
1816a698 | 660 | vp = getenv_str(name); |
d684c216 | 661 | |
1816a698 | 662 | if (vp != NULL ) { |
55027f3b L |
663 | value = strtoul(vp, &endp, base); |
664 | if (endp != vp) | |
665 | return value; | |
35e9ec0c | 666 | } |
d684c216 | 667 | |
35e9ec0c | 668 | return default_val; |
d684c216 L |
669 | } |
670 | ||
35e9ec0c | 671 | |
be84a704 L |
672 | /* |
673 | * Read an environment variable as a boolean | |
674 | */ | |
675 | bool getenv_yesno(const MEMX char *name) | |
676 | { | |
bddc7e77 | 677 | char *s = getenv_str(name); |
be84a704 L |
678 | |
679 | if (s == NULL) | |
680 | return false; | |
681 | ||
682 | return strchr_P(PSTR("1yYtT"), *s) != NULL; | |
683 | ||
684 | /* | |
685 | return *s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T' ? | |
686 | 1 : 0; | |
687 | */ | |
688 | } | |
689 | ||
1816a698 L |
690 | command_ret_t do_env_print(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, |
691 | int argc, char * const argv[]) | |
f76ca346 | 692 | { |
8eb73d17 | 693 | bool mode = 0; |
f76ca346 L |
694 | command_ret_t rc = CMD_RET_SUCCESS; |
695 | ||
8eb73d17 L |
696 | int opt; |
697 | while ((opt = getopt(argc, argv, PSTR("s"))) != -1) { | |
698 | switch (opt) { | |
699 | case 's': | |
700 | mode = 1; | |
701 | break; | |
702 | default: /* '?' */ | |
703 | return CMD_RET_USAGE; | |
704 | } | |
705 | } | |
f76ca346 | 706 | |
8eb73d17 | 707 | if (optind == argc) { |
f76ca346 | 708 | /* print all env vars */ |
8eb73d17 | 709 | int size = env_print(NULL, mode); |
f76ca346 L |
710 | if (size < 0) |
711 | return CMD_RET_FAILURE; | |
8eb73d17 L |
712 | if (mode == 0) |
713 | printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"), | |
b0a9d14c | 714 | size, ENV_SIZE - 1); |
f76ca346 L |
715 | return CMD_RET_SUCCESS; |
716 | } | |
717 | ||
718 | /* print selected env vars */ | |
8eb73d17 | 719 | while (optind < argc) { |
f1e16f88 L |
720 | int len = env_print(argv[optind], mode); |
721 | if (len < 0) { | |
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 | } |