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 | |
Simon Glass | 21f6394 | 2017-08-03 12:22:16 -0600 | [diff] [blame] | 17 | static int env_dataflash_get_char(int index) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 18 | { |
| 19 | uchar c; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 20 | |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 21 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 22 | 1, (char *)&c); |
Igor Grinberg | 0901d9f | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 23 | return c; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 26 | static int env_dataflash_load(void) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 27 | { |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 28 | ulong crc, new = 0; |
| 29 | unsigned off; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 30 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 32 | /* Read old CRC */ |
| 33 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
| 34 | sizeof(ulong), (char *)&crc); |
| 35 | |
| 36 | /* Read whole environment */ |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 37 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 38 | |
Bo Shen | cca2011 | 2013-10-08 16:30:21 +0800 | [diff] [blame] | 39 | /* Calculate the CRC */ |
| 40 | off = offsetof(env_t, data); |
| 41 | new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE); |
| 42 | |
| 43 | if (crc == new) |
| 44 | env_import(buf, 1); |
| 45 | else |
| 46 | set_default_env("!bad CRC"); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 47 | |
| 48 | return 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 52 | #error No support for redundant environment on dataflash yet! |
| 53 | #endif |
| 54 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 55 | static int env_dataflash_save(void) |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 56 | { |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 57 | env_t env_new; |
| 58 | int ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 60 | ret = env_export(&env_new); |
| 61 | if (ret) |
| 62 | return ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 63 | |
| 64 | return write_dataflash(CONFIG_ENV_ADDR, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 65 | (unsigned long)&env_new, |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 66 | CONFIG_ENV_SIZE); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 69 | U_BOOT_ENV_LOCATION(dataflash) = { |
| 70 | .location = ENVL_DATAFLASH, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 71 | ENV_NAME("dataflash") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 72 | .get_char = env_dataflash_get_char, |
| 73 | .load = env_dataflash_load, |
| 74 | .save = env_save_ptr(env_dataflash_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 75 | }; |