Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * LowLevel function for DataFlash environment support |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 3 | * Author : Gilles Gastaldi (Atmel) |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 8 | #include <command.h> |
| 9 | #include <environment.h> |
| 10 | #include <linux/stddef.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 11 | #include <dataflash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 12 | #include <search.h> |
| 13 | #include <errno.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 14 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 17 | env_t *env_ptr; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 18 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 19 | char *env_name_spec = "dataflash"; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 20 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 21 | uchar env_get_char_spec(int index) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 22 | { |
| 23 | uchar c; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 24 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 25 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 26 | 1, (char *)&c); |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 27 | return c; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 30 | void env_relocate_spec(void) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 31 | { |
Bo Shen | 5c7bad5 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 32 | ulong crc, new = 0; |
| 33 | unsigned off; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 34 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 35 | |
Bo Shen | 5c7bad5 | 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 | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 41 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 42 | |
Bo Shen | 5c7bad5 | 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 | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Wolfgang Denk | 460a9ff | 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 | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 57 | int saveenv(void) |
| 58 | { |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame^] | 59 | env_t env_new; |
| 60 | int ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 61 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame^] | 62 | ret = env_export(&env_new); |
| 63 | if (ret) |
| 64 | return ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | |
| 66 | return write_dataflash(CONFIG_ENV_ADDR, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 67 | (unsigned long)&env_new, |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 68 | CONFIG_ENV_SIZE); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 71 | /* |
| 72 | * Initialize environment use |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 73 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 74 | * We are still running from ROM, so data use is limited. |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 75 | * Use a (moderately small) buffer on the stack |
| 76 | */ |
| 77 | int env_init(void) |
| 78 | { |
Bo Shen | 5c7bad5 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 79 | /* use default */ |
| 80 | gd->env_addr = (ulong)&default_environment[0]; |
| 81 | gd->env_valid = 1; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 82 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 83 | return 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 84 | } |