env_dataflash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * LowLevel function for DataFlash environment support
  3. * Author : Gilles Gastaldi (Atmel)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <command.h>
  22. #include <environment.h>
  23. #include <linux/stddef.h>
  24. #include <dataflash.h>
  25. #include <search.h>
  26. #include <errno.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. env_t *env_ptr = NULL;
  29. char * env_name_spec = "dataflash";
  30. extern int read_dataflash(unsigned long addr, unsigned long size,
  31. char *result);
  32. extern int write_dataflash(unsigned long addr_dest,
  33. unsigned long addr_src, unsigned long size);
  34. extern int AT91F_DataflashInit(void);
  35. extern uchar default_environment[];
  36. uchar env_get_char_spec(int index)
  37. {
  38. uchar c;
  39. read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t,data),
  40. 1, (char *)&c);
  41. return (c);
  42. }
  43. void env_relocate_spec(void)
  44. {
  45. char buf[CONFIG_ENV_SIZE];
  46. read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
  47. env_import(buf, 1);
  48. }
  49. #ifdef CONFIG_ENV_OFFSET_REDUND
  50. #error No support for redundant environment on dataflash yet!
  51. #endif
  52. int saveenv(void)
  53. {
  54. env_t env_new;
  55. ssize_t len;
  56. char *res;
  57. res = (char *)&env_new.data;
  58. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
  59. if (len < 0) {
  60. error("Cannot export environment: errno = %d\n", errno);
  61. return 1;
  62. }
  63. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  64. return write_dataflash(CONFIG_ENV_ADDR,
  65. (unsigned long)&env_new,
  66. CONFIG_ENV_SIZE);
  67. }
  68. /*
  69. * Initialize environment use
  70. *
  71. * We are still running from ROM, so data use is limited.
  72. * Use a (moderately small) buffer on the stack
  73. */
  74. int env_init(void)
  75. {
  76. ulong crc, len, new;
  77. unsigned off;
  78. uchar buf[64];
  79. if (gd->env_valid)
  80. return 0;
  81. AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
  82. /* read old CRC */
  83. read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
  84. sizeof(ulong), (char *)&crc);
  85. new = 0;
  86. len = ENV_SIZE;
  87. off = offsetof(env_t,data);
  88. while (len > 0) {
  89. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  90. read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
  91. new = crc32 (new, buf, n);
  92. len -= n;
  93. off += n;
  94. }
  95. if (crc == new) {
  96. gd->env_addr = offsetof(env_t,data);
  97. gd->env_valid = 1;
  98. } else {
  99. gd->env_addr = (ulong)&default_environment[0];
  100. gd->env_valid = 0;
  101. }
  102. return 0;
  103. }