wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de> |
| 29 | * |
| 30 | * It might not be possible in all cases to use 'memcpy()' to copy |
| 31 | * the environment to NVRAM, as the NVRAM might not be mapped into |
| 32 | * the memory space. (I.e. this is the case for the BAB750). In those |
| 33 | * cases it might be possible to access the NVRAM using a different |
| 34 | * method. For example, the RTC on the BAB750 is accessible in IO |
| 35 | * space using its address and data registers. To enable usage of |
| 36 | * NVRAM in those cases I invented the functions 'nvram_read()' and |
| 37 | * 'nvram_write()', which will be activated upon the configuration |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 38 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | * strongly dependent on the used HW, and must be redefined for each |
| 40 | * board that wants to use them. |
| 41 | */ |
| 42 | |
| 43 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | #include <command.h> |
| 45 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | #include <linux/stddef.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 47 | #include <search.h> |
| 48 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | |
Jean-Christophe PLAGNIOL-VILLARD | 957a0e6 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 50 | DECLARE_GLOBAL_DATA_PTR; |
| 51 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | extern void *nvram_read(void *dest, const long src, size_t count); |
| 54 | extern void nvram_write(long dest, const void *src, size_t count); |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 55 | env_t *env_ptr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 57 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 60 | char *env_name_spec = "NVRAM"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 61 | |
Igor Grinberg | bf95df4 | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 63 | uchar env_get_char_spec(int index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | uchar c; |
| 66 | |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 67 | nvram_read(&c, CONFIG_ENV_ADDR + index, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | |
| 69 | return c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 70 | } |
Igor Grinberg | bf95df4 | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 71 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 73 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | char buf[CONFIG_ENV_SIZE]; |
| 76 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 79 | #else |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 80 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 82 | env_import(buf, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 85 | int saveenv(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 87 | env_t env_new; |
| 88 | ssize_t len; |
| 89 | char *res; |
| 90 | int rcode = 0; |
| 91 | |
| 92 | res = (char *)&env_new.data; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 93 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 94 | if (len < 0) { |
| 95 | error("Cannot export environment: errno = %d\n", errno); |
| 96 | return 1; |
| 97 | } |
| 98 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 99 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 101 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | #else |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 104 | rcode = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | #endif |
| 106 | return rcode; |
| 107 | } |
| 108 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 109 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | * Initialize Environment use |
| 111 | * |
| 112 | * We are still running from ROM, so data use is limited |
| 113 | */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | int env_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 115 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | ulong crc; |
| 118 | uchar data[ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 119 | |
| 120 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 121 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | |
| 123 | if (crc32(0, data, ENV_SIZE) == crc) { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 124 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | #else |
| 126 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 127 | gd->env_addr = (ulong)&env_ptr->data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | #endif |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 129 | gd->env_valid = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | } else { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 131 | gd->env_addr = (ulong)&default_environment[0]; |
| 132 | gd->env_valid = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | } |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 134 | |
| 135 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | } |