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 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include <common.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 21 | #include <command.h> |
| 22 | #include <environment.h> |
| 23 | #include <linux/stddef.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 24 | #include <dataflash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 25 | #include <search.h> |
| 26 | #include <errno.h> |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 27 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 30 | env_t *env_ptr; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 31 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 32 | char *env_name_spec = "dataflash"; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 33 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | uchar env_get_char_spec(int index) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 35 | { |
| 36 | uchar c; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 37 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 38 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 39 | 1, (char *)&c); |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 40 | return c; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 43 | void env_relocate_spec(void) |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 44 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 45 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 46 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 47 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
| 48 | |
| 49 | env_import(buf, 1); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 53 | #error No support for redundant environment on dataflash yet! |
| 54 | #endif |
| 55 | |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 56 | int saveenv(void) |
| 57 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 58 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | ssize_t len; |
| 60 | char *res; |
| 61 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 62 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 63 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 64 | if (len < 0) { |
| 65 | error("Cannot export environment: errno = %d\n", errno); |
| 66 | return 1; |
| 67 | } |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 68 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | |
| 70 | return write_dataflash(CONFIG_ENV_ADDR, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 71 | (unsigned long)&env_new, |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 72 | CONFIG_ENV_SIZE); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Initialize environment use |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 77 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | * We are still running from ROM, so data use is limited. |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 79 | * Use a (moderately small) buffer on the stack |
| 80 | */ |
| 81 | int env_init(void) |
| 82 | { |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 83 | ulong crc, len = ENV_SIZE, new = 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 84 | unsigned off; |
| 85 | uchar buf[64]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 86 | |
| 87 | if (gd->env_valid) |
| 88 | return 0; |
| 89 | |
| 90 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ |
| 91 | |
| 92 | /* read old CRC */ |
| 93 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), |
| 94 | sizeof(ulong), (char *)&crc); |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 95 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 96 | off = offsetof(env_t, data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 97 | while (len > 0) { |
| 98 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 99 | |
| 100 | read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); |
| 101 | |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 102 | new = crc32(new, buf, n); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | len -= n; |
| 104 | off += n; |
| 105 | } |
| 106 | |
| 107 | if (crc == new) { |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 108 | gd->env_addr = offsetof(env_t, data); |
| 109 | gd->env_valid = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 110 | } else { |
Igor Grinberg | b90c1fc | 2011-11-07 01:14:02 +0000 | [diff] [blame] | 111 | gd->env_addr = (ulong)&default_environment[0]; |
| 112 | gd->env_valid = 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 115 | return 0; |
wdenk | 8676590 | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 116 | } |