nvram.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2010
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Andreas Heppel <aheppel@sysgo.de>
  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 <env.h>
  27. #include <env_internal.h>
  28. #include <asm/global_data.h>
  29. #include <linux/stddef.h>
  30. #include <search.h>
  31. #include <errno.h>
  32. #include <u-boot/crc.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  35. extern void *nvram_read(void *dest, const long src, size_t count);
  36. extern void nvram_write(long dest, const void *src, size_t count);
  37. #else
  38. static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
  39. #endif
  40. static int env_nvram_load(void)
  41. {
  42. char buf[CONFIG_ENV_SIZE];
  43. #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
  44. nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
  45. #else
  46. memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
  47. #endif
  48. return env_import(buf, 1, H_EXTERNAL);
  49. }
  50. static int env_nvram_save(void)
  51. {
  52. env_t env_new;
  53. int rcode = 0;
  54. rcode = env_export(&env_new);
  55. if (rcode)
  56. return rcode;
  57. #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
  58. nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
  59. #else
  60. if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
  61. rcode = 1;
  62. #endif
  63. return rcode;
  64. }
  65. /*
  66. * Initialize Environment use
  67. *
  68. * We are still running from ROM, so data use is limited
  69. */
  70. static int env_nvram_init(void)
  71. {
  72. #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
  73. ulong crc;
  74. uchar data[ENV_SIZE];
  75. nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
  76. nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
  77. if (crc32(0, data, ENV_SIZE) == crc) {
  78. gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
  79. #else
  80. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  81. gd->env_addr = (ulong)&env_ptr->data;
  82. #endif
  83. gd->env_valid = ENV_VALID;
  84. } else {
  85. gd->env_valid = ENV_INVALID;
  86. }
  87. return 0;
  88. }
  89. U_BOOT_ENV_LOCATION(nvram) = {
  90. .location = ENVL_NVRAM,
  91. ENV_NAME("NVRAM")
  92. .load = env_nvram_load,
  93. .save = env_save_ptr(env_nvram_save),
  94. .init = env_nvram_init,
  95. };