env_eeprom.c 2.6 KB

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