env_eeprom.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #if defined(CFG_ENV_IS_IN_EEPROM) /* Environment is in EEPROM */
  27. #include <command.h>
  28. #include <environment.h>
  29. #include <linux/stddef.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. env_t *env_ptr = NULL;
  32. char * env_name_spec = "EEPROM";
  33. uchar env_get_char_spec (int index)
  34. {
  35. uchar c;
  36. eeprom_read (CFG_DEF_EEPROM_ADDR,
  37. CFG_ENV_OFFSET+index+offsetof(env_t,data),
  38. &c, 1);
  39. return (c);
  40. }
  41. void env_relocate_spec (void)
  42. {
  43. eeprom_read (CFG_DEF_EEPROM_ADDR,
  44. CFG_ENV_OFFSET,
  45. (uchar*)env_ptr,
  46. CFG_ENV_SIZE);
  47. }
  48. int saveenv(void)
  49. {
  50. return eeprom_write (CFG_DEF_EEPROM_ADDR,
  51. CFG_ENV_OFFSET,
  52. (uchar *)env_ptr,
  53. CFG_ENV_SIZE);
  54. }
  55. /************************************************************************
  56. * Initialize Environment use
  57. *
  58. * We are still running from ROM, so data use is limited
  59. * Use a (moderately small) buffer on the stack
  60. */
  61. int env_init(void)
  62. {
  63. ulong crc, len, new;
  64. unsigned off;
  65. uchar buf[64];
  66. eeprom_init (); /* prepare for EEPROM read/write */
  67. /* read old CRC */
  68. eeprom_read (CFG_DEF_EEPROM_ADDR,
  69. CFG_ENV_OFFSET+offsetof(env_t,crc),
  70. (uchar *)&crc, sizeof(ulong));
  71. new = 0;
  72. len = ENV_SIZE;
  73. off = offsetof(env_t,data);
  74. while (len > 0) {
  75. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  76. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
  77. new = crc32 (new, buf, n);
  78. len -= n;
  79. off += n;
  80. }
  81. if (crc == new) {
  82. gd->env_addr = offsetof(env_t,data);
  83. gd->env_valid = 1;
  84. } else {
  85. gd->env_addr = 0;
  86. gd->env_valid = 0;
  87. }
  88. return (0);
  89. }
  90. #endif /* CFG_ENV_IS_IN_EEPROM */