env_dataflash.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * LowLevel function for DataFlash environment support
  3. * Author : Gilles Gastaldi (Atmel)
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <environment.h>
  10. #include <linux/stddef.h>
  11. #include <dataflash.h>
  12. #include <search.h>
  13. #include <errno.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. env_t *env_ptr;
  16. char *env_name_spec = "dataflash";
  17. uchar env_get_char_spec(int index)
  18. {
  19. uchar c;
  20. read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
  21. 1, (char *)&c);
  22. return c;
  23. }
  24. void env_relocate_spec(void)
  25. {
  26. ulong crc, new = 0;
  27. unsigned off;
  28. char buf[CONFIG_ENV_SIZE];
  29. /* Read old CRC */
  30. read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
  31. sizeof(ulong), (char *)&crc);
  32. /* Read whole environment */
  33. read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
  34. /* Calculate the CRC */
  35. off = offsetof(env_t, data);
  36. new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
  37. if (crc == new)
  38. env_import(buf, 1);
  39. else
  40. set_default_env("!bad CRC");
  41. }
  42. #ifdef CONFIG_ENV_OFFSET_REDUND
  43. #error No support for redundant environment on dataflash yet!
  44. #endif
  45. int saveenv(void)
  46. {
  47. env_t env_new;
  48. int ret;
  49. ret = env_export(&env_new);
  50. if (ret)
  51. return ret;
  52. return write_dataflash(CONFIG_ENV_ADDR,
  53. (unsigned long)&env_new,
  54. CONFIG_ENV_SIZE);
  55. }
  56. /*
  57. * Initialize environment use
  58. *
  59. * We are still running from ROM, so data use is limited.
  60. * Use a (moderately small) buffer on the stack
  61. */
  62. int env_init(void)
  63. {
  64. /* use default */
  65. gd->env_addr = (ulong)&default_environment[0];
  66. gd->env_valid = 1;
  67. return 0;
  68. }