env_nvram.c 3.0 KB

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