Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * LowLevel function for DataFlash environment support |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 3 | * Author : Gilles Gastaldi (Atmel) |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 8 | #include <command.h> |
| 9 | #include <environment.h> |
| 10 | #include <linux/stddef.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 11 | #include <dataflash.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 12 | #include <search.h> |
| 13 | #include <errno.h> |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 14 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 17 | env_t *env_ptr; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 18 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 19 | char *env_name_spec = "dataflash"; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 20 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 21 | uchar env_get_char_spec(int index) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 22 | { |
| 23 | uchar c; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 24 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 25 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 26 | 1, (char *)&c); |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 27 | return c; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 30 | void env_relocate_spec(void) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 31 | { |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame^] | 32 | ulong crc, new = 0; |
| 33 | unsigned off; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 34 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 35 | |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame^] | 36 | /* Read old CRC */ |
| 37 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
| 38 | sizeof(ulong), (char *)&crc); |
| 39 | |
| 40 | /* Read whole environment */ |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 41 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 42 | |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame^] | 43 | /* Calculate the CRC */ |
| 44 | off = offsetof(env_t, data); |
| 45 | new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE); |
| 46 | |
| 47 | if (crc == new) |
| 48 | env_import(buf, 1); |
| 49 | else |
| 50 | set_default_env("!bad CRC"); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 53 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 54 | #error No support for redundant environment on dataflash yet! |
| 55 | #endif |
| 56 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 57 | int saveenv(void) |
| 58 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 59 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | ssize_t len; |
| 61 | char *res; |
| 62 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 63 | res = (char *)&env_new.data; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 64 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | if (len < 0) { |
| 66 | error("Cannot export environment: errno = %d\n", errno); |
| 67 | return 1; |
| 68 | } |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 69 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 70 | |
| 71 | return write_dataflash(CONFIG_ENV_ADDR, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 72 | (unsigned long)&env_new, |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 73 | CONFIG_ENV_SIZE); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 76 | /* |
| 77 | * Initialize environment use |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 78 | * |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 79 | * We are still running from ROM, so data use is limited. |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 80 | * Use a (moderately small) buffer on the stack |
| 81 | */ |
| 82 | int env_init(void) |
| 83 | { |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame^] | 84 | /* use default */ |
| 85 | gd->env_addr = (ulong)&default_environment[0]; |
| 86 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 87 | |
| 88 | return 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 89 | } |