dataflash.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. static unsigned char env_dataflash_get_char(int index)
  17. {
  18. uchar c;
  19. read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
  20. 1, (char *)&c);
  21. return c;
  22. }
  23. static void env_dataflash_load(void)
  24. {
  25. ulong crc, new = 0;
  26. unsigned off;
  27. char buf[CONFIG_ENV_SIZE];
  28. /* Read old CRC */
  29. read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
  30. sizeof(ulong), (char *)&crc);
  31. /* Read whole environment */
  32. read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
  33. /* Calculate the CRC */
  34. off = offsetof(env_t, data);
  35. new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
  36. if (crc == new)
  37. env_import(buf, 1);
  38. else
  39. set_default_env("!bad CRC");
  40. }
  41. #ifdef CONFIG_ENV_OFFSET_REDUND
  42. #error No support for redundant environment on dataflash yet!
  43. #endif
  44. static int env_dataflash_save(void)
  45. {
  46. env_t env_new;
  47. int ret;
  48. ret = env_export(&env_new);
  49. if (ret)
  50. return ret;
  51. return write_dataflash(CONFIG_ENV_ADDR,
  52. (unsigned long)&env_new,
  53. CONFIG_ENV_SIZE);
  54. }
  55. U_BOOT_ENV_LOCATION(dataflash) = {
  56. .location = ENVL_DATAFLASH,
  57. ENV_NAME("dataflash")
  58. .get_char = env_dataflash_get_char,
  59. .load = env_dataflash_load,
  60. .save = env_save_ptr(env_dataflash_save),
  61. };