env_eeprom.c 2.5 KB

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