env_nvram.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * (C) Copyright 2000-2010
  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. /*
  26. * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
  27. *
  28. * It might not be possible in all cases to use 'memcpy()' to copy
  29. * the environment to NVRAM, as the NVRAM might not be mapped into
  30. * the memory space. (I.e. this is the case for the BAB750). In those
  31. * cases it might be possible to access the NVRAM using a different
  32. * method. For example, the RTC on the BAB750 is accessible in IO
  33. * space using its address and data registers. To enable usage of
  34. * NVRAM in those cases I invented the functions 'nvram_read()' and
  35. * 'nvram_write()', which will be activated upon the configuration
  36. * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
  37. * strongly dependent on the used HW, and must be redefined for each
  38. * board that wants to use them.
  39. */
  40. #include <common.h>
  41. #include <command.h>
  42. #include <environment.h>
  43. #include <linux/stddef.h>
  44. #include <search.h>
  45. #include <errno.h>
  46. DECLARE_GLOBAL_DATA_PTR;
  47. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  48. extern void *nvram_read(void *dest, const long src, size_t count);
  49. extern void nvram_write(long dest, const void *src, size_t count);
  50. env_t *env_ptr;
  51. #else
  52. env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
  53. #endif
  54. char *env_name_spec = "NVRAM";
  55. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  56. static char env_buf[CONFIG_ENV_SIZE];
  57. #endif
  58. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  59. uchar env_get_char_spec(int index)
  60. {
  61. uchar c;
  62. nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
  63. return c;
  64. }
  65. #endif
  66. void env_relocate_spec(void)
  67. {
  68. char *buf;
  69. #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
  70. buf = env_buf;
  71. nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
  72. #else
  73. buf = (void *)CONFIG_ENV_ADDR;
  74. #endif
  75. env_import(buf, 1);
  76. }
  77. int saveenv(void)
  78. {
  79. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  80. env_t *env_new = (env_t *)env_buf;
  81. #else
  82. env_t *env_new = (env_t *)CONFIG_ENV_ADDR;
  83. #endif
  84. ssize_t len;
  85. char *res;
  86. int rcode = 0;
  87. res = (char *)env_new->data;
  88. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  89. if (len < 0) {
  90. error("Cannot export environment: errno = %d\n", errno);
  91. return 1;
  92. }
  93. env_new->crc = crc32(0, env_new->data, ENV_SIZE);
  94. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  95. nvram_write(CONFIG_ENV_ADDR, env_new, CONFIG_ENV_SIZE);
  96. #endif
  97. return rcode;
  98. }
  99. /*
  100. * Initialize Environment use
  101. *
  102. * We are still running from ROM, so data use is limited
  103. */
  104. int env_init(void)
  105. {
  106. #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
  107. ulong crc;
  108. uchar *data = env_buf;
  109. nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
  110. nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
  111. if (crc32(0, data, ENV_SIZE) == crc) {
  112. gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
  113. #else
  114. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  115. gd->env_addr = (ulong)&env_ptr->data;
  116. #endif
  117. gd->env_valid = 1;
  118. } else {
  119. gd->env_addr = (ulong)&default_environment[0];
  120. gd->env_valid = 0;
  121. }
  122. return 0;
  123. }