env_dataflash.c 2.8 KB

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