env_dataflash.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ssize_t len;
  49. char *res;
  50. res = (char *)&env_new.data;
  51. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  52. if (len < 0) {
  53. error("Cannot export environment: errno = %d\n", errno);
  54. return 1;
  55. }
  56. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  57. return write_dataflash(CONFIG_ENV_ADDR,
  58. (unsigned long)&env_new,
  59. CONFIG_ENV_SIZE);
  60. }
  61. /*
  62. * Initialize environment use
  63. *
  64. * We are still running from ROM, so data use is limited.
  65. * Use a (moderately small) buffer on the stack
  66. */
  67. int env_init(void)
  68. {
  69. /* use default */
  70. gd->env_addr = (ulong)&default_environment[0];
  71. gd->env_valid = 1;
  72. return 0;
  73. }