wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 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 | #include <common.h> |
| 28 | |
| 29 | #if defined(CFG_ENV_IS_IN_EEPROM) /* Environment is in EEPROM */ |
| 30 | |
| 31 | #include <command.h> |
| 32 | #include <environment.h> |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 33 | #include <linux/stddef.h> |
| 34 | #include <malloc.h> |
| 35 | |
| 36 | env_t *env_ptr = NULL; |
| 37 | |
| 38 | char * env_name_spec = "EEPROM"; |
| 39 | |
| 40 | extern uchar (*env_get_char)(int); |
| 41 | extern uchar env_get_char_memory (int index); |
| 42 | |
| 43 | |
| 44 | uchar env_get_char_spec (int index) |
| 45 | { |
| 46 | uchar c; |
| 47 | |
| 48 | eeprom_read (CFG_DEF_EEPROM_ADDR, |
| 49 | CFG_ENV_OFFSET+index+offsetof(env_t,data), |
| 50 | &c, 1); |
| 51 | |
| 52 | return (c); |
| 53 | } |
| 54 | |
| 55 | void env_relocate_spec (void) |
| 56 | { |
| 57 | eeprom_read (CFG_DEF_EEPROM_ADDR, |
| 58 | CFG_ENV_OFFSET, |
| 59 | (uchar*)env_ptr, |
| 60 | CFG_ENV_SIZE); |
| 61 | } |
| 62 | |
| 63 | int saveenv(void) |
| 64 | { |
| 65 | return eeprom_write (CFG_DEF_EEPROM_ADDR, |
| 66 | CFG_ENV_OFFSET, |
| 67 | (uchar *)env_ptr, |
| 68 | CFG_ENV_SIZE); |
| 69 | } |
| 70 | |
| 71 | /************************************************************************ |
| 72 | * Initialize Environment use |
| 73 | * |
| 74 | * We are still running from ROM, so data use is limited |
| 75 | * Use a (moderately small) buffer on the stack |
| 76 | */ |
| 77 | int env_init(void) |
| 78 | { |
| 79 | DECLARE_GLOBAL_DATA_PTR; |
| 80 | |
| 81 | ulong crc, len, new; |
| 82 | unsigned off; |
| 83 | uchar buf[64]; |
| 84 | |
| 85 | eeprom_init (); /* prepare for EEPROM read/write */ |
| 86 | |
| 87 | /* read old CRC */ |
| 88 | eeprom_read (CFG_DEF_EEPROM_ADDR, |
| 89 | CFG_ENV_OFFSET+offsetof(env_t,crc), |
| 90 | (uchar *)&crc, sizeof(ulong)); |
| 91 | |
| 92 | new = 0; |
| 93 | len = ENV_SIZE; |
| 94 | off = offsetof(env_t,data); |
| 95 | while (len > 0) { |
| 96 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 97 | |
| 98 | eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n); |
| 99 | new = crc32 (new, buf, n); |
| 100 | len -= n; |
| 101 | off += n; |
| 102 | } |
| 103 | |
| 104 | if (crc == new) { |
| 105 | gd->env_addr = offsetof(env_t,data); |
| 106 | gd->env_valid = 1; |
| 107 | } else { |
| 108 | gd->env_addr = 0; |
| 109 | gd->env_valid = 0; |
| 110 | } |
| 111 | |
| 112 | return (0); |
| 113 | } |
| 114 | |
| 115 | #endif /* CFG_ENV_IS_IN_EEPROM */ |