env_eeprom.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. extern uchar env_get_char_memory (int index);
  34. uchar env_get_char_spec (int index)
  35. {
  36. uchar c;
  37. eeprom_read (CFG_DEF_EEPROM_ADDR,
  38. CFG_ENV_OFFSET+index+offsetof(env_t,data),
  39. &c, 1);
  40. return (c);
  41. }
  42. void env_relocate_spec (void)
  43. {
  44. eeprom_read (CFG_DEF_EEPROM_ADDR,
  45. CFG_ENV_OFFSET,
  46. (uchar*)env_ptr,
  47. CFG_ENV_SIZE);
  48. }
  49. int saveenv(void)
  50. {
  51. return eeprom_write (CFG_DEF_EEPROM_ADDR,
  52. CFG_ENV_OFFSET,
  53. (uchar *)env_ptr,
  54. CFG_ENV_SIZE);
  55. }
  56. /************************************************************************
  57. * Initialize Environment use
  58. *
  59. * We are still running from ROM, so data use is limited
  60. * Use a (moderately small) buffer on the stack
  61. */
  62. int env_init(void)
  63. {
  64. ulong crc, len, new;
  65. unsigned off;
  66. uchar buf[64];
  67. eeprom_init (); /* prepare for EEPROM read/write */
  68. /* read old CRC */
  69. eeprom_read (CFG_DEF_EEPROM_ADDR,
  70. CFG_ENV_OFFSET+offsetof(env_t,crc),
  71. (uchar *)&crc, sizeof(ulong));
  72. new = 0;
  73. len = ENV_SIZE;
  74. off = offsetof(env_t,data);
  75. while (len > 0) {
  76. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  77. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
  78. new = crc32 (new, buf, n);
  79. len -= n;
  80. off += n;
  81. }
  82. if (crc == new) {
  83. gd->env_addr = offsetof(env_t,data);
  84. gd->env_valid = 1;
  85. } else {
  86. gd->env_addr = 0;
  87. gd->env_valid = 0;
  88. }
  89. return (0);
  90. }
  91. #endif /* CFG_ENV_IS_IN_EEPROM */